Re: How to set initial size of TextView?

2017-03-15 Thread Chris Green
Eric Cashon via gtk-app-devel-list  wrote:
> 
>  
> Hi Chris,
> 
> Try getting the font height and base the textview height on that. If you 
> use the font ascent you might have to pad it a little but it should give 
> you a consistent value to size your textviews with based on font size. 
> 
> 
OK, thanks (and thanks for the sample code too).

I'm surprised that there's no easier way of doing it but if there
isn't then that's just the way it is I suppose.

-- 
Chris Green
·

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Re: How to set initial size of TextView?

2017-03-14 Thread Eric Cashon via gtk-app-devel-list

 
Hi Chris,

Try getting the font height and base the textview height on that. If you use 
the font ascent you might have to pad it a little but it should give you a 
consistent value to size your textviews with based on font size.

Eric


/*
gcc -Wall textview_height1.c -o textview_height1 `pkg-config --cflags 
--libs gtk+-3.0`
Tested on Ubuntu16.04 with GTK3.18.
*/

#include
  
int main(int argc, char *argv[])
  {
gtk_init(, );

GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Textview Height");
gtk_window_set_default_size(GTK_WINDOW(window), 400, 400);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_container_set_border_width(GTK_CONTAINER(window), 20);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

GtkWidget *textview1=gtk_text_view_new();
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview1), GTK_WRAP_WORD);

//Testing different font sizes.
PangoFontDescription *font=pango_font_description_from_string("Monospace 
20");
G_GNUC_BEGIN_IGNORE_DEPRECATIONS 
gtk_widget_override_font(textview1, font);
G_GNUC_END_IGNORE_DEPRECATIONS

PangoContext *pango_context=gtk_widget_get_pango_context(textview1);
PangoFontDescription 
*desc=pango_context_get_font_description(pango_context);
PangoFontMetrics *metrics=pango_context_get_metrics(pango_context, desc, 
NULL);
gdouble ascent=(gdouble)pango_font_metrics_get_ascent(metrics)/PANGO_SCALE;
pango_font_metrics_unref(metrics);

g_print("Ascent %f\n", ascent);
   
GtkWidget *scroll1=gtk_scrolled_window_new(NULL, NULL);
gtk_widget_set_hexpand(scroll1, TRUE);
gtk_widget_set_size_request(scroll1, 360, 3.0*ascent);
gtk_container_add(GTK_CONTAINER(scroll1), textview1);

//Assume the same font size for the second textview.
GtkWidget *textview2=gtk_text_view_new();
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview2), GTK_WRAP_WORD);
G_GNUC_BEGIN_IGNORE_DEPRECATIONS 
gtk_widget_override_font(textview2, font);
G_GNUC_END_IGNORE_DEPRECATIONS

//Don't need this anymore.
pango_font_description_free(font);

GtkWidget *scroll2=gtk_scrolled_window_new(NULL, NULL);
gtk_widget_set_hexpand(scroll2, TRUE);
gtk_widget_set_size_request(scroll2, 360, 5.0*ascent);
gtk_container_add(GTK_CONTAINER(scroll2), textview2);

GtkWidget *grid=gtk_grid_new();
gtk_grid_set_row_spacing(GTK_GRID(grid), 10);
gtk_grid_set_row_homogeneous(GTK_GRID(grid), FALSE);
gtk_grid_attach(GTK_GRID(grid), scroll1, 0, 0, 1, 1);
gtk_grid_attach(GTK_GRID(grid), scroll2, 0, 1, 1, 1);

gtk_container_add(GTK_CONTAINER(window), grid);

gtk_widget_show_all(window);

gtk_main();

return 0;
  }


 

 

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


How to set initial size of TextView?

2017-03-14 Thread Chris Green
I have a top level window in which I have five or six TextView text
entry fields. I want to be able to set the fields to different sizes,
at least initially.  E.g. I have a couple of TextView fields that I
want to be just two lines high, two which should be four lines high
and one that needs to be five lines.

How should one do this?  I want this to work on different displays so
setting sizes of the widgets (or containing widgets) in pixels won't
work will it?

Is there no way to set a TextView's initial size to a specified number
of lines?  They may well get put inside ScrolledWindow widgets but I
can't see a way to set the default initial size of those either.

-- 
Chris Green
·

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list