| 1 | /* GuiWindow.cpp |
| 2 | * |
| 3 | * Copyright (C) 1993-2018,2020,2021,2024,2025 Paul Boersma, 2013 Tom Naughton |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "GuiP.h" |
| 20 | #include "../kar/UnicodeData.h" |
| 21 | #include "machine.h" |
| 22 | #include <locale.h> |
| 23 | |
| 24 | Thing_implement (GuiWindow, GuiShell, 0); |
| 25 | |
| 26 | #undef iam |
| 27 | #define iam(x) x me = (x) void_me |
| 28 | |
| 29 | #if gtk |
| 30 | static gboolean _GuiWindow_destroyCallback (GuiObject widget, GdkEvent *event, gpointer void_me) { |
| 31 | (void) widget; |
| 32 | iam (GuiWindow); |
| 33 | forget (me); |
| 34 | return true; |
| 35 | } |
| 36 | static gboolean _GuiWindow_goAwayCallback (GuiObject widget, GdkEvent *event, gpointer void_me) { |
| 37 | (void) widget; |
| 38 | iam (GuiWindow); |
| 39 | if (my d_goAwayCallback) { |
| 40 | my d_goAwayCallback (my d_goAwayBoss); |
| 41 | } |
| 42 | return true; |
| 43 | } |
| 44 | static void _GuiWindow_child_resizeCallback (GtkWidget *childWidget, gpointer data) { |
| 45 | GtkAllocation *parentAllocation = (GtkAllocation *) data; |
| 46 | GtkWidget *parentWidget = gtk_widget_get_parent (childWidget); |
| 47 | Thing_cast (GuiThing, child, _GuiObject_getUserData (childWidget)); |
| 48 | if (child) { |
| 49 | GuiControl control = nullptr; |
| 50 | if (Thing_isa (child, classGuiWindow)) { |
| 51 | control = static_cast <GuiWindow> (child); |
| 52 | //Melder_casual (U"menu bar"); |
| 53 | } else if (Thing_isa (child, classGuiControl)) { |
| 54 | control = static_cast <GuiControl> (child); |
| 55 | //Melder_casual (U"control"); |
| 56 | } else if (Thing_isa (child, classGuiMenu)) { |
| 57 | Thing_cast (GuiMenu, menu, child); |
| 58 | control = menu -> d_cascadeButton.get(); |
| 59 | //Melder_casual (U"menu"); |
| 60 | } |
| 61 | if (control) { |
| 62 | /* |
| 63 | Move and resize. |
| 64 | */ |
| 65 | trace (U"moving child of class ", Thing_className (control)); |
| 66 | int left = control -> d_left, right = control -> d_right, top = control -> d_top, bottom = control -> d_bottom; |
| 67 | if (left < 0) left += parentAllocation -> width; // this replicates structGuiControl :: v_positionInForm () |
| 68 | if (right <= 0) right += parentAllocation -> width; |
| 69 | if (top < 0) top += parentAllocation -> height; |
| 70 | if (bottom <= 0) bottom += parentAllocation -> height; |
| 71 | trace (U"moving child to (", left, U",", top, U") with size ", right - left, U" x ", bottom - top, U"."); |
| 72 | GtkAllocation childAllocation { parentAllocation -> x + left, parentAllocation -> y + top, right - left, bottom - top }; |
| 73 | gtk_widget_size_allocate (GTK_WIDGET (childWidget), & childAllocation); |
| 74 | trace (U"moved child of class ", Thing_className (control)); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | static void _GuiWindow_resizeCallback (GuiObject widget, GtkAllocation *allocation, gpointer void_me) { |
| 79 | (void) widget; |
| 80 | iam (GuiWindow); |
| 81 | trace (U"fixed received size allocation: (", allocation -> x, U", ", allocation -> y, |
| 82 | U"), ", allocation -> width, U" x ", allocation -> height, U"."); |
| 83 | /* |
| 84 | Apparently, GTK sends the size allocation message both to the shell and to its fixed-container child. |
| 85 | we could capture the message either from the shell or from the fixed; we choose to do it from the fixed. |
| 86 | */ |
| 87 | Melder_assert (GTK_IS_FIXED (widget)); |
| 88 | Melder_assert (GTK_IS_FIXED (my d_widget)); |
| 89 | /* |
| 90 | We move and resize all the children of the fixed. |
| 91 | */ |
| 92 | #if defined (chrome) |
| 93 | { |
| 94 | GtkAllocation surrogateShellTitleLabelWidgetAllocation { allocation -> x, allocation -> y, allocation -> width, 31 }; |
| 95 | gtk_widget_size_allocate (GTK_WIDGET (my chrome_surrogateShellTitleLabelWidget), & surrogateShellTitleLabelWidgetAllocation); |
| 96 | } |
| 97 | #endif |
| 98 | gtk_container_foreach (GTK_CONTAINER (widget), _GuiWindow_child_resizeCallback, allocation); |
| 99 | //gtk_container_foreach (GTK_CONTAINER (my d_widget), _GuiWindow_child_resizeCallback, allocation); |
| 100 | my d_width = allocation -> width; |
| 101 | my d_height = allocation -> height; |
| 102 | trace (U"end"); |
| 103 | } |
| 104 | #elif motif |
| 105 | static void _GuiMotifWindow_destroyCallback (GuiObject widget, XtPointer void_me, XtPointer call) { |
| 106 | (void) widget; (void) call; |
| 107 | iam (GuiWindow); |
| 108 | if (my d_xmMenuBar) { |
| 109 | } |
| 110 | trace (U"destroying window widget"); |
| 111 | forget (me); |
| 112 | } |
| 113 | static void _GuiMotifWindow_goAwayCallback (GuiObject widget, XtPointer void_me, XtPointer call) { |
| 114 | (void) widget; (void) call; |
| 115 | iam (GuiWindow); |
| 116 | if (my d_goAwayCallback) { |
| 117 | //Melder_casual (U"_GuiMotifWindow_goAwayCallback: before"); |
| 118 | my d_goAwayCallback (my d_goAwayBoss); |
| 119 | //Melder_casual (U"_GuiMotifWindow_goAwayCallback: after"); |
| 120 | } |
| 121 | } |
| 122 | #endif |
| 123 | |
| 124 | GuiWindow GuiWindow_create (int x, int y, int width, int height, int minimumWidth, int minimumHeight, |
| 125 | conststring32 title /* cattable */, GuiShell_GoAwayCallback goAwayCallback, Thing goAwayBoss, uint32 flags) |
| 126 | { |
| 127 | autoGuiWindow me = Thing_new (GuiWindow); |
| 128 | if (Melder_debug == 55) |
| 129 | Melder_casual (U"\t", Thing_messageNameAndAddress (me.get()), U" init"); |
| 130 | my d_parent = nullptr; |
| 131 | my d_goAwayCallback = goAwayCallback; |
| 132 | my d_goAwayBoss = goAwayBoss; |
| 133 | #if gtk |
| 134 | (void) flags; |
| 135 | GuiGtk_initialize (); |
| 136 | my d_gtkWindow = (GtkWindow *) gtk_window_new (GTK_WINDOW_TOPLEVEL); |
| 137 | #if 0 |
| 138 | static const GdkRGBA backgroundColour { 0.92, 0.92, 0.92, 1.0 }; |
| 139 | gtk_widget_override_background_color (GTK_WIDGET (my d_gtkWindow), GTK_STATE_FLAG_NORMAL, & backgroundColour); |
| 140 | #endif |
| 141 | g_signal_connect (G_OBJECT (my d_gtkWindow), "delete-event", goAwayCallback ? G_CALLBACK (_GuiWindow_goAwayCallback) : G_CALLBACK (gtk_widget_hide_on_delete), me.get()); |
| 142 | g_signal_connect (G_OBJECT (my d_gtkWindow), "destroy-event", G_CALLBACK (_GuiWindow_destroyCallback), me.get()); |
| 143 | |
| 144 | gtk_window_set_default_size (GTK_WINDOW (my d_gtkWindow), width, height); |
| 145 | gtk_window_set_resizable (GTK_WINDOW (my d_gtkWindow), true); |
| 146 | |
| 147 | my d_widget = gtk_fixed_new (); |
| 148 | _GuiObject_setUserData (my d_widget, me.get()); |
| 149 | gtk_widget_set_size_request (GTK_WIDGET (my d_widget), minimumWidth, minimumHeight); |
| 150 | gtk_container_add (GTK_CONTAINER (my d_gtkWindow), GTK_WIDGET (my d_widget)); |
| 151 | GdkGeometry geometry = { minimumWidth, minimumHeight, 0, 0, 0, 0, 0, 0, 0, 0, GDK_GRAVITY_NORTH_WEST }; |
| 152 | gtk_window_set_geometry_hints (my d_gtkWindow, GTK_WIDGET (my d_widget), & geometry, GDK_HINT_MIN_SIZE); |
| 153 | g_signal_connect (G_OBJECT (my d_widget), "size-allocate", G_CALLBACK (_GuiWindow_resizeCallback), me.get()); |
| 154 | //g_signal_connect (G_OBJECT (my d_gtkWindow), "size-allocate", G_CALLBACK (_GuiWindow_resizeCallback), me.get()); |
| 155 | #if defined (chrome) |
| 156 | my chrome_surrogateShellTitleLabelWidget = gtk_label_new (Melder_peek32to8 (title)); |
| 157 | gtk_label_set_use_markup (GTK_LABEL (my chrome_surrogateShellTitleLabelWidget), true); |
| 158 | gtk_widget_set_size_request (GTK_WIDGET (my chrome_surrogateShellTitleLabelWidget), minimumWidth, 31 /*Machine_getTextHeight()*/); |
| 159 | gtk_misc_set_alignment (GTK_MISC (my chrome_surrogateShellTitleLabelWidget), 0.5, 0.0); |
| 160 | gtk_fixed_put (GTK_FIXED (my d_widget), GTK_WIDGET (my chrome_surrogateShellTitleLabelWidget), 8, 0); |
| 161 | gtk_widget_show (GTK_WIDGET (my chrome_surrogateShellTitleLabelWidget)); |
| 162 | #endif |
| 163 | GuiShell_setTitle (me.get(), title); |
| 164 | gint rootX, rootY; |
| 165 | gtk_window_get_position (my d_gtkWindow, & rootX, & rootY); |
| 166 | trace (rootX, U" root ", rootY); |
| 167 | #elif motif |
| 168 | my d_xmShell = XmCreateShell (nullptr, flags & GuiWindow_FULLSCREEN ? "Praatwulgfullscreen" : "Praatwulg", nullptr, 0); |
| 169 | XtVaSetValues (my d_xmShell, XmNdeleteResponse, goAwayCallback ? XmDO_NOTHING : XmUNMAP, nullptr); |
| 170 | XtVaSetValues (my d_xmShell, XmNx, x, XmNy, y, XmNwidth, (Dimension) width, XmNheight, (Dimension) height, nullptr); |
| 171 | if (goAwayCallback) |
| 172 | XmAddWMProtocolCallback (my d_xmShell, 'delw', _GuiMotifWindow_goAwayCallback, (char *) me.get()); |
| 173 | GuiShell_setTitle (me.get(), title); |
| 174 | my d_widget = XmCreateForm (my d_xmShell, "dialog", nullptr, 0); |
| 175 | _GuiObject_setUserData (my d_widget, me.get()); |
| 176 | XtAddCallback (my d_widget, XmNdestroyCallback, _GuiMotifWindow_destroyCallback, me.get()); |
| 177 | XtVaSetValues (my d_widget, XmNdialogStyle, XmDIALOG_MODELESS, XmNautoUnmanage, False, nullptr); |
| 178 | #elif cocoa |
| 179 | (void) flags; |
| 180 | NSRect rect = { { static_cast<CGFloat>(x), static_cast<CGFloat>(y) }, { static_cast<CGFloat>(width), static_cast<CGFloat>(height) } }; |
| 181 | my d_cocoaShell = [[GuiCocoaShell alloc] |
| 182 | initWithContentRect: rect |
| 183 | styleMask: NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask |
| 184 | backing: NSBackingStoreBuffered |
| 185 | defer: false]; |
| 186 | //GuiCocoaShellDelegate *delegate = [[GuiCocoaShellDelegate alloc] init]; |
| 187 | //my d_cocoaShell. delegate = delegate; |
| 188 | if (Melder_debug == 55) |
| 189 | Melder_casual (U"\t\tGuiCocoaShell-", Melder_pointer (my d_cocoaShell), U" init in ", Thing_messageNameAndAddress (me.get())); |
| 190 | [my d_cocoaShell setCollectionBehavior: NSWindowCollectionBehaviorFullScreenPrimary]; |
| 191 | [my d_cocoaShell setMinSize: NSMakeSize (minimumWidth, minimumHeight)]; |
| 192 | GuiShell_setTitle (me.get(), title); |
| 193 | [my d_cocoaShell makeKeyAndOrderFront: nil]; |
| 194 | [my d_cocoaShell layoutIfNeeded]; |
| 195 | [my d_cocoaShell setDistinctiveBackGround]; |
| 196 | my d_widget = (GuiObject) [my d_cocoaShell contentView]; // BUG: this d_widget doesn't have the GuiCocoaAny protocol |
| 197 | _GuiObject_setUserData (my d_cocoaShell, me.get()); |
| 198 | //if (! theGuiCocoaWindowDelegate) { |
| 199 | // theGuiCocoaWindowDelegate = [[GuiCocoaWindowDelegate alloc] init]; |
| 200 | //} |
| 201 | //[my d_cocoaWindow setDelegate: theGuiCocoaWindowDelegate]; |
| 202 | #endif |
| 203 | my d_width = width; |
| 204 | my d_height = height; |
| 205 | my d_shell = me.get(); |
| 206 | return me.releaseToAmbiguousOwner(); |
| 207 | } |
| 208 | |
| 209 | uinteger theGuiTopLowAccelerators [8]; |
| 210 | |
| 211 | void GuiWindow_addMenuBar (GuiWindow me) { |
| 212 | #if gtk |
| 213 | my d_gtkMenuBar = (GtkMenuBar *) gtk_menu_bar_new (); |
| 214 | _GuiObject_setUserData (my d_gtkMenuBar, me); |
| 215 | my v_positionInForm (my d_gtkMenuBar, 0, 0, Machine_getMenuBarTop (), Machine_getMenuBarBottom (), me); // BUG? |
| 216 | |
| 217 | // we need an accelerator group for each window we're creating accelerated menus on |
| 218 | GuiObject topwin = gtk_widget_get_toplevel (GTK_WIDGET (my d_widget)); |
| 219 | Melder_assert (topwin == my d_gtkWindow); |
| 220 | GtkAccelGroup *ag = gtk_accel_group_new (); |
| 221 | gtk_window_add_accel_group (GTK_WINDOW (topwin), ag); |
| 222 | // unfortunately, menu-bars don't fiddle with accel-groups, so we need a way |
| 223 | // to pass it to the sub-menus created upon this bar for their items to have |
| 224 | // access to the accel-group |
| 225 | g_object_set_data (G_OBJECT (my d_gtkMenuBar), "accel-group", ag); |
| 226 | gtk_widget_show (GTK_WIDGET (my d_gtkMenuBar)); |
| 227 | #elif motif |
| 228 | my d_xmMenuBar = XmCreateMenuBar (my d_widget, "menuBar", nullptr, 0); |
| 229 | XtVaSetValues (my d_xmMenuBar, XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM, nullptr); |
| 230 | XtManageChild (my d_xmMenuBar); |
| 231 | #elif cocoa |
| 232 | (void) me; // this is handled elsewhere |
| 233 | #endif |
| 234 | } |
| 235 | |
| 236 | bool GuiWindow_setDirty (GuiWindow me, bool dirty) { |
| 237 | #if gtk |
| 238 | (void) dirty; |
| 239 | return false; |
| 240 | #elif motif |
| 241 | (void) dirty; |
| 242 | return false; |
| 243 | #elif cocoa |
| 244 | [my d_cocoaShell setDocumentEdited: dirty]; |
| 245 | return true; |
| 246 | #else |
| 247 | (void) dirty; |
| 248 | return false; |
| 249 | #endif |
| 250 | } |
| 251 | |
| 252 | /* End of file GuiWindow.cpp */ |