From d9f217323e5218bb4b881901255ee46ced3d6777 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sat, 24 Apr 2021 07:51:15 +0100 Subject: [PATCH] Break up gtkmisc.c. It's another file that should have been subdivided into lots of tiny separate things in the utils library - especially since for some reason I made a completely separate 'guimisc' cmake-level library for it when there was no need. --- unix/CMakeLists.txt | 21 +++--- unix/utils/align_label_left.c | 20 ++++++ unix/utils/buildinfo_gtk_version.c | 14 ++++ unix/utils/get_label_text_dimensions.c | 42 +++++++++++ unix/utils/get_x11_display.c | 34 +++++++++ unix/{gtkmisc.c => utils/our_dialog.c} | 98 ++------------------------ unix/utils/string_width.c | 18 +++++ 7 files changed, 145 insertions(+), 102 deletions(-) create mode 100644 unix/utils/align_label_left.c create mode 100644 unix/utils/buildinfo_gtk_version.c create mode 100644 unix/utils/get_label_text_dimensions.c create mode 100644 unix/utils/get_x11_display.c rename unix/{gtkmisc.c => utils/our_dialog.c} (68%) create mode 100644 unix/utils/string_width.c diff --git a/unix/CMakeLists.txt b/unix/CMakeLists.txt index c13abdc5..e33fbff2 100644 --- a/unix/CMakeLists.txt +++ b/unix/CMakeLists.txt @@ -145,14 +145,17 @@ target_link_libraries(uppity if(GTK_FOUND) add_sources_from_current_dir(utils + utils/align_label_left.c + utils/buildinfo_gtk_version.c + utils/get_label_text_dimensions.c + utils/get_x11_display.c + utils/our_dialog.c + utils/string_width.c gtkcols.c) add_sources_from_current_dir(guiterminal gtkwin.c gtkfont.c gtkdlg.c gtkcfg.c gtkcomm.c uxcfg.c uxucs.c uxprint.c) add_dependencies(guiterminal generated_licence_h) # gtkdlg.c uses licence.h - add_library(guimisc STATIC - gtkmisc.c) - add_executable(pageant uxpgnt.c ${CMAKE_SOURCE_DIR}/be_misc.c @@ -163,7 +166,7 @@ if(GTK_FOUND) uxnoise.c ${CMAKE_SOURCE_DIR}/ssh/x11fwd.c) target_link_libraries(pageant - guimisc eventloop console agent settings network crypto utils + eventloop console agent settings network crypto utils ${GTK_LIBRARIES}) installed_program(pageant) @@ -174,7 +177,7 @@ if(GTK_FOUND) ${CMAKE_SOURCE_DIR}/nogss.c uxpty.c) target_link_libraries(pterm - guiterminal guimisc eventloop settings charset utils + guiterminal eventloop settings charset utils ${GTK_LIBRARIES} ${X11_LIBRARIES}) installed_program(pterm) @@ -186,7 +189,7 @@ if(GTK_FOUND) ${CMAKE_SOURCE_DIR}/nogss.c uxpty.c) target_link_libraries(ptermapp - guiterminal guimisc eventloop settings charset utils + guiterminal eventloop settings charset utils ${GTK_LIBRARIES} ${X11_LIBRARIES}) add_executable(putty @@ -194,7 +197,7 @@ if(GTK_FOUND) gtkmain.c ${CMAKE_SOURCE_DIR}/be_all_s.c) target_link_libraries(putty - guiterminal guimisc eventloop sshclient otherbackends settings + guiterminal eventloop sshclient otherbackends settings network crypto charset utils ${GTK_LIBRARIES} ${X11_LIBRARIES}) set_target_properties(putty @@ -207,7 +210,7 @@ if(GTK_FOUND) ${CMAKE_SOURCE_DIR}/nocmdline.c ${CMAKE_SOURCE_DIR}/be_all_s.c) target_link_libraries(puttyapp - guiterminal guimisc eventloop sshclient otherbackends settings + guiterminal eventloop sshclient otherbackends settings network crypto charset utils ${GTK_LIBRARIES} ${X11_LIBRARIES}) @@ -219,6 +222,6 @@ if(GTK_FOUND) ${CMAKE_SOURCE_DIR}/norand.c ${CMAKE_SOURCE_DIR}/nocproxy.c) target_link_libraries(puttytel - guiterminal guimisc eventloop otherbackends settings network charset utils + guiterminal eventloop otherbackends settings network charset utils ${GTK_LIBRARIES} ${X11_LIBRARIES}) endif() diff --git a/unix/utils/align_label_left.c b/unix/utils/align_label_left.c new file mode 100644 index 00000000..eaae01ab --- /dev/null +++ b/unix/utils/align_label_left.c @@ -0,0 +1,20 @@ +/* + * Helper function to align the text in a GtkLabel to the left, which + * has to be done in several different ways depending on GTK version. + */ + +#include +#include "putty.h" +#include "gtkcompat.h" +#include "gtkmisc.h" + +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 +} diff --git a/unix/utils/buildinfo_gtk_version.c b/unix/utils/buildinfo_gtk_version.c new file mode 100644 index 00000000..a2ec187e --- /dev/null +++ b/unix/utils/buildinfo_gtk_version.c @@ -0,0 +1,14 @@ +/* + * Return the version of GTK we were compiled against, for buildinfo. + */ + +#include +#include "putty.h" +#include "gtkcompat.h" +#include "gtkmisc.h" + +char *buildinfo_gtk_version(void) +{ + return dupprintf("%d.%d.%d", + GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION); +} diff --git a/unix/utils/get_label_text_dimensions.c b/unix/utils/get_label_text_dimensions.c new file mode 100644 index 00000000..fe1c13af --- /dev/null +++ b/unix/utils/get_label_text_dimensions.c @@ -0,0 +1,42 @@ +/* + * 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. + */ + +#include +#include "putty.h" +#include "gtkcompat.h" +#include "gtkmisc.h" + +void get_label_text_dimensions(const char *text, int *width, int *height) +{ + GtkWidget *label = gtk_label_new(text); + + /* + * 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! + */ +#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 +} diff --git a/unix/utils/get_x11_display.c b/unix/utils/get_x11_display.c new file mode 100644 index 00000000..d9462c27 --- /dev/null +++ b/unix/utils/get_x11_display.c @@ -0,0 +1,34 @@ +/* + * Return the Xlib 'Display *' underlying our GTK environment, if any. + */ + +#include +#include "putty.h" +#include "gtkcompat.h" +#include "gtkmisc.h" + +#ifndef NOT_X_WINDOWS + +#include +#include + +Display *get_x11_display(void) +{ +#if GTK_CHECK_VERSION(3,0,0) + if (!GDK_IS_X11_DISPLAY(gdk_display_get_default())) + return NULL; +#endif + return GDK_DISPLAY_XDISPLAY(gdk_display_get_default()); +} + +#else + +/* + * Include _something_ in this file to prevent an annoying compiler + * warning, and to avoid having to condition out this file in + * CMakeLists. It's in a library, so this variable shouldn't end up in + * any actual program, because nothing will refer to it. + */ +const int get_x11_display_dummy_variable = 0; + +#endif diff --git a/unix/gtkmisc.c b/unix/utils/our_dialog.c similarity index 68% rename from unix/gtkmisc.c rename to unix/utils/our_dialog.c index c0c9682a..5b9995d4 100644 --- a/unix/gtkmisc.c +++ b/unix/utils/our_dialog.c @@ -1,80 +1,4 @@ /* - * Miscellaneous GTK helper functions. - */ - -#include -#include -#include -#include - -#include -#if !GTK_CHECK_VERSION(3,0,0) -#include -#endif - -#include "putty.h" -#include "gtkcompat.h" - -#ifndef NOT_X_WINDOWS -#include -#include -#endif - -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. * * The best method for doing this has varied wildly with versions of @@ -108,6 +32,11 @@ void align_label_left(GtkLabel *label) * vbox. */ +#include +#include "putty.h" +#include "gtkcompat.h" +#include "gtkmisc.h" + GtkWidget *our_dialog_new(void) { #if GTK_CHECK_VERSION(3,0,0) @@ -204,20 +133,3 @@ void our_dialog_add_to_content_area(GtkWindow *dlg, GtkWidget *w, w, expand, fill, padding); #endif } - -char *buildinfo_gtk_version(void) -{ - return dupprintf("%d.%d.%d", - GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION); -} - -#ifndef NOT_X_WINDOWS -Display *get_x11_display(void) -{ -#if GTK_CHECK_VERSION(3,0,0) - if (!GDK_IS_X11_DISPLAY(gdk_display_get_default())) - return NULL; -#endif - return GDK_DISPLAY_XDISPLAY(gdk_display_get_default()); -} -#endif diff --git a/unix/utils/string_width.c b/unix/utils/string_width.c new file mode 100644 index 00000000..c944308f --- /dev/null +++ b/unix/utils/string_width.c @@ -0,0 +1,18 @@ +/* + * Return the width of a string in the font used in GTK controls. Used + * as a means of picking a sensible size for dialog boxes and pieces + * of them, in a way that should adapt sensibly to changes in font and + * resolution. + */ + +#include +#include "putty.h" +#include "gtkcompat.h" +#include "gtkmisc.h" + +int string_width(const char *text) +{ + int ret; + get_label_text_dimensions(text, &ret, NULL); + return ret; +}