GuiShell.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* GuiShell.cpp
  2. *
  3. * Copyright (C) 1993-2012,2015,2016,2017 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 2 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. #include "GuiP.h"
  19. #include "../kar/UnicodeData.h"
  20. Thing_implement (GuiShell, GuiForm, 0);
  21. #if cocoa
  22. @implementation GuiCocoaShell {
  23. GuiShell d_userData;
  24. }
  25. - (void) dealloc { // override
  26. GuiShell me = d_userData;
  27. my d_cocoaShell = nullptr; // this is already under destruction, so undangle
  28. forget (me);
  29. trace (U"deleting a window or dialog");
  30. [super dealloc];
  31. }
  32. - (GuiThing) getUserData {
  33. return d_userData;
  34. }
  35. - (void) setUserData: (GuiThing) userData {
  36. Melder_assert (userData == nullptr || Thing_isa (userData, classGuiShell));
  37. d_userData = static_cast <GuiShell> (userData);
  38. }
  39. - (void) keyDown: (NSEvent *) theEvent {
  40. trace (U"key down");
  41. [super keyDown: theEvent]; // for automatic behaviour in dialog boxes; do GuiWindows have to override this to do nothing?
  42. }
  43. - (BOOL) windowShouldClose: (id) sender {
  44. GuiCocoaShell *widget = (GuiCocoaShell *) sender;
  45. GuiShell me = (GuiShell) [widget getUserData];
  46. if (my d_goAwayCallback) {
  47. trace (U"calling goAwayCallback)");
  48. my d_goAwayCallback (my d_goAwayBoss);
  49. } else {
  50. trace (U"hiding window or dialog");
  51. [widget orderOut: nil];
  52. }
  53. return false;
  54. }
  55. @end
  56. #endif
  57. void structGuiShell :: v_destroy () noexcept {
  58. #if cocoa
  59. if (our d_cocoaShell) {
  60. [our d_cocoaShell setUserData: nullptr]; // undangle reference to this
  61. Melder_fatal (U"ordering out?");
  62. [our d_cocoaShell orderOut: nil];
  63. [our d_cocoaShell close];
  64. [our d_cocoaShell release];
  65. our d_cocoaShell = nullptr; // undangle
  66. }
  67. #endif
  68. GuiShell_Parent :: v_destroy ();
  69. }
  70. int GuiShell_getShellWidth (GuiShell me) {
  71. int width = 0;
  72. #if gtk
  73. width = GTK_WIDGET (my d_gtkWindow) -> allocation.width;
  74. #elif motif
  75. width = my d_xmShell -> width;
  76. #elif cocoa
  77. return [my d_cocoaShell frame].size.width;
  78. #endif
  79. return width;
  80. }
  81. int GuiShell_getShellHeight (GuiShell me) {
  82. int height = 0;
  83. #if gtk
  84. height = GTK_WIDGET (my d_gtkWindow) -> allocation.height;
  85. #elif motif
  86. height = my d_xmShell -> height;
  87. #elif cocoa
  88. return [my d_cocoaShell frame].size.height;
  89. #endif
  90. return height;
  91. }
  92. void GuiShell_setTitle (GuiShell me, conststring32 title /* cattable */) {
  93. #if gtk
  94. gtk_window_set_title (my d_gtkWindow, Melder_peek32to8 (title));
  95. #elif motif
  96. SetWindowTextW (my d_xmShell -> window, Melder_peek32toW (title));
  97. #elif cocoa
  98. [my d_cocoaShell setTitle: (NSString *) Melder_peek32toCfstring (title)];
  99. #endif
  100. }
  101. void GuiShell_drain (GuiShell me) {
  102. #if gtk
  103. //gdk_window_flush (gtk_widget_get_window (my d_gtkWindow));
  104. gdk_flush ();
  105. #elif motif
  106. /*
  107. On Windows Motif, there is no graphics buffering.
  108. */
  109. #elif cocoa
  110. Melder_assert (my d_cocoaShell);
  111. [my d_cocoaShell display]; // not just flushWindow
  112. [NSApp
  113. nextEventMatchingMask: NSAnyEventMask
  114. untilDate: [NSDate distantPast]
  115. inMode: NSDefaultRunLoopMode
  116. dequeue: NO
  117. ];
  118. #endif
  119. }
  120. /* End of file GuiShell.cpp */