X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=unix%2Fgtkmisc.c;h=6ee68be2e87b6d861635b4b4938636a3af29f650;hb=095072fa46b2d7b8beafaddb2f873d2f500a1e10;hp=d6cfccc2959a989df239f22bf288fc5af80912a0;hpb=dc11417aeeec0735071cf98347af6f9616ba6a2e;p=PuTTY.git diff --git a/unix/gtkmisc.c b/unix/gtkmisc.c index d6cfccc2..6ee68be2 100644 --- a/unix/gtkmisc.c +++ b/unix/gtkmisc.c @@ -15,6 +15,60 @@ #include "putty.h" #include "gtkcompat.h" +void get_label_text_dimensions(const char *text, int *width, int *height) +{ + /* + * Determine the dimensions of a piece of text in the standard + * font used in GTK interface elements like labels. We do this by + * instantiating an actual GtkLabel, and then querying its size. + * + * But GTK2 and GTK3 require us to query the size completely + * differently. I'm sure there ought to be an easier approach than + * the way I'm doing this in GTK3, too! + */ + GtkWidget *label = gtk_label_new(text); + +#if GTK_CHECK_VERSION(3,0,0) + PangoLayout *layout = gtk_label_get_layout(GTK_LABEL(label)); + PangoRectangle logrect; + pango_layout_get_extents(layout, NULL, &logrect); + if (width) + *width = logrect.width / PANGO_SCALE; + if (height) + *height = logrect.height / PANGO_SCALE; +#else + GtkRequisition req; + gtk_widget_size_request(label, &req); + if (width) + *width = req.width; + if (height) + *height = req.height; +#endif + + g_object_ref_sink(G_OBJECT(label)); +#if GTK_CHECK_VERSION(2,10,0) + g_object_unref(label); +#endif +} + +int string_width(const char *text) +{ + int ret; + get_label_text_dimensions(text, &ret, NULL); + return ret; +} + +void align_label_left(GtkLabel *label) +{ +#if GTK_CHECK_VERSION(3,16,0) + gtk_label_set_xalign(label, 0.0); +#elif GTK_CHECK_VERSION(3,14,0) + gtk_widget_set_halign(GTK_WIDGET(label), GTK_ALIGN_START); +#else + gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0); +#endif +} + /* ---------------------------------------------------------------------- * Functions to arrange controls in a basically dialog-like window. * @@ -104,14 +158,15 @@ void our_dialog_set_action_area(GtkWindow *dlg, GtkWidget *w) /* GtkWindow is a GtkBin, hence contains exactly one child, which * here we always expect to be a vbox */ GtkBox *vbox = GTK_BOX(gtk_bin_get_child(GTK_BIN(dlg))); - - GtkWidget *sep = gtk_hseparator_new(); - gtk_box_pack_end(vbox, sep, FALSE, TRUE, 0); - gtk_widget_show(sep); + GtkWidget *sep; g_object_set(G_OBJECT(w), "margin", 8, (const char *)NULL); gtk_box_pack_end(vbox, w, FALSE, TRUE, 0); + sep = gtk_hseparator_new(); + gtk_box_pack_end(vbox, sep, FALSE, TRUE, 0); + gtk_widget_show(sep); + #endif } @@ -120,6 +175,8 @@ GtkBox *our_dialog_make_action_hbox(GtkWindow *dlg) #if GTK_CHECK_VERSION(3,0,0) GtkWidget *hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); our_dialog_set_action_area(dlg, hbox); + g_object_set(G_OBJECT(hbox), "margin", 0, (const char *)NULL); + g_object_set(G_OBJECT(hbox), "spacing", 8, (const char *)NULL); gtk_widget_show(hbox); return GTK_BOX(hbox); #else /* not GTK 3 */