Prevent GTK from setting unreasonable size hints with large menu bars
authorPo Lu <luangruo@yahoo.com>
Mon, 21 Feb 2022 06:29:58 +0000 (14:29 +0800)
committerPo Lu <luangruo@yahoo.com>
Mon, 21 Feb 2022 06:29:58 +0000 (14:29 +0800)
* src/gtkutil.c (struct _EmacsMenuBar): New struct.
(emacs_menu_bar_init):
(emacs_menu_bar_class_init):
(emacs_menu_bar_get_preferred_width):
(emacs_menu_bar_new): New functions.
(xg_update_menu_item): Use our own menu bar class on GTK 3.
* src/gtkutil.h (EmacsMenuBar): New class.

src/gtkutil.c
src/gtkutil.h

index 158c29272f5264c56c60ed57a1c175c0608213f4..72eb7ef2bb1ac08a1a7acdf8007a936d8b6a6b76 100644 (file)
@@ -76,6 +76,17 @@ typedef struct pgtk_output xp_output;
 #define XG_TEXT_OPEN   GTK_STOCK_OPEN
 #endif
 
+#ifdef HAVE_GTK3
+static void emacs_menu_bar_get_preferred_width (GtkWidget *, gint *, gint *);
+
+struct _EmacsMenuBar
+{
+  GtkMenuBar parent;
+};
+
+G_DEFINE_TYPE (EmacsMenuBar, emacs_menu_bar, GTK_TYPE_MENU_BAR)
+#endif
+
 #ifndef HAVE_PGTK
 static void xg_im_context_commit (GtkIMContext *, gchar *, gpointer);
 static void xg_im_context_preedit_changed (GtkIMContext *, gpointer);
@@ -127,6 +138,45 @@ bool xg_gtk_initialized;        /* Used to make sure xwidget calls are possible
 
 static GtkWidget * xg_get_widget_from_map (ptrdiff_t idx);
 
+\f
+
+#ifdef HAVE_GTK3
+static void
+emacs_menu_bar_init (EmacsMenuBar *menu_bar)
+{
+  return;
+}
+
+static void
+emacs_menu_bar_class_init (EmacsMenuBarClass *klass)
+{
+  GtkWidgetClass *widget_class;
+
+  widget_class = GTK_WIDGET_CLASS (klass);
+  widget_class->get_preferred_width = emacs_menu_bar_get_preferred_width;
+}
+
+static void
+emacs_menu_bar_get_preferred_width (GtkWidget *widget,
+                                   gint *minimum, gint *natural)
+{
+  GtkWidgetClass *widget_class;
+
+  widget_class = GTK_WIDGET_CLASS (emacs_menu_bar_parent_class);
+  widget_class->get_preferred_width (widget, minimum, natural);
+
+  if (minimum)
+    *minimum = 0;
+}
+
+static GtkWidget *
+emacs_menu_bar_new (void)
+{
+  return GTK_WIDGET (g_object_new (emacs_menu_bar_get_type (), NULL));
+}
+
+#endif
+
 \f
 /***********************************************************************
                       Display handling functions
@@ -3287,7 +3337,12 @@ create_menus (widget_value *data,
       }
       else
         {
+#ifndef HAVE_GTK3
           wmenu = gtk_menu_bar_new ();
+#else
+         wmenu = emacs_menu_bar_new ();
+#endif
+
 #ifdef HAVE_PGTK
          g_signal_connect (G_OBJECT (wmenu), "button-press-event",
                            G_CALLBACK (menu_bar_button_pressed_cb), f);
index b74244d84d04db51e9f7a01abfd3ffdf0dbb48cb..f850ecc4219d50d159b6b4ae533d06e9030061fb 100644 (file)
@@ -83,6 +83,10 @@ typedef struct xg_menu_item_cb_data_
 
 } xg_menu_item_cb_data;
 
+#ifdef HAVE_GTK3
+G_DECLARE_FINAL_TYPE (EmacsMenuBar, emacs_menu_bar, EMACS, MENU_BAR, GtkMenuBar)
+#endif
+
 extern bool xg_uses_old_file_dialog (void);
 
 extern char *xg_get_file_name (struct frame *f,