Re: How to set initial size of TextView?
- From: cecashon aol com
- To: cl isbd net, gtk-app-devel-list gnome org
- Subject: Re: How to set initial size of TextView?
- Date: Tue, 14 Mar 2017 16:40:10 -0400
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<gtk/gtk.h>
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
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;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]