GuiControl.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* GuiControl.cpp
  2. *
  3. * Copyright (C) 1993-2012,2013,2015,2017 Paul Boersma,
  4. * 2008 Stefan de Koninck, 2010 Franz Brausse, 2013 Tom Naughton
  5. *
  6. * This code is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This code is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. * See the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "GuiP.h"
  20. #include "machine.h"
  21. void structGuiControl :: v_positionInForm (GuiObject widget, int left, int right, int top, int bottom, GuiForm parent) {
  22. #if gtk
  23. /*
  24. * Remember the location settings for resizing.
  25. */
  26. d_left = left;
  27. d_right = right;
  28. d_top = top;
  29. d_bottom = bottom;
  30. /*
  31. */
  32. if (! parent) return;
  33. Melder_assert (parent -> d_widget);
  34. Melder_assert (GTK_IS_FIXED (parent -> d_widget));
  35. gint parentWidth, parentHeight;
  36. gtk_widget_get_size_request (GTK_WIDGET (parent -> d_widget), & parentWidth, & parentHeight);
  37. //parentWidth = GTK_WIDGET (parent -> d_widget) -> allocation.width;
  38. //parentHeight = GTK_WIDGET (parent -> d_widget) -> allocation.height;
  39. if (left < 0) left += parentWidth;
  40. if (right <= 0) right += parentWidth;
  41. if (top < 0) top += parentHeight;
  42. if (bottom <= 0) bottom += parentHeight;
  43. trace (U"fixed: parent width ", parentWidth, U" height ", parentHeight);
  44. gtk_widget_set_size_request (GTK_WIDGET (widget), right - left, bottom - top);
  45. gtk_fixed_put (GTK_FIXED (parent -> d_widget), GTK_WIDGET (widget), left, top);
  46. #elif motif
  47. (void) parent;
  48. if (left >= 0) {
  49. if (right > 0) {
  50. XtVaSetValues (widget, XmNx, left, XmNwidth, right - left, nullptr);
  51. } else {
  52. XtVaSetValues (widget, XmNleftAttachment, XmATTACH_FORM, XmNleftOffset, left, XmNrightAttachment, XmATTACH_FORM, XmNrightOffset, - right, nullptr);
  53. }
  54. } else {
  55. Melder_assert (right <= 0);
  56. trace (U"parent width ", parent -> d_widget -> width);
  57. XtVaSetValues (widget, XmNrightAttachment, XmATTACH_FORM, XmNrightOffset, - right, XmNwidth, right - left, nullptr);
  58. trace (U"parent width ", parent -> d_widget -> width);
  59. }
  60. if (top >= 0) {
  61. if (bottom > 0) {
  62. XtVaSetValues (widget, XmNy, top, XmNheight, bottom - top, nullptr);
  63. } else {
  64. XtVaSetValues (widget, XmNtopAttachment, XmATTACH_FORM, XmNtopOffset, top, XmNbottomAttachment, XmATTACH_FORM, XmNbottomOffset, - bottom, nullptr);
  65. }
  66. } else {
  67. Melder_assert (bottom <= 0);
  68. XtVaSetValues (widget, XmNbottomAttachment, XmATTACH_FORM, XmNbottomOffset, - bottom, XmNheight, bottom - top, nullptr);
  69. }
  70. #elif cocoa
  71. NSView *superView = (NSView *) parent -> d_widget;
  72. NSView *widgetView = (NSView *) widget;
  73. NSRect parentRect = [superView frame];
  74. int parentWidth = parentRect.size.width;
  75. int parentHeight = parentRect.size.height;
  76. NSUInteger horizMask = 0;
  77. if (left >= 0) {
  78. if (right <= 0) {
  79. horizMask = NSViewWidthSizable;
  80. }
  81. } else {
  82. horizMask = NSViewMinXMargin;
  83. }
  84. NSUInteger vertMask = 0;
  85. if (top >= 0) {
  86. vertMask = NSViewMinYMargin;
  87. if (bottom <= 0) {
  88. vertMask = NSViewHeightSizable;
  89. }
  90. }
  91. if (left < 0) left += parentWidth;
  92. if (right <= 0) right += parentWidth;
  93. if (top < 0) top += parentHeight;
  94. if (bottom <= 0) bottom += parentHeight;
  95. top = parentHeight - top; // flip
  96. bottom = parentHeight - bottom; // flip
  97. int width = right - left;
  98. int height = top - bottom;
  99. if ([widgetView isKindOfClass: [NSButton class]]) {
  100. if (! [widgetView isKindOfClass: [NSPopUpButton class]]) {
  101. /*
  102. * On Cocoa, NSButton views show up 12 pixels less wide and 5 pixels less high than their frame.
  103. * Compensate for this (undocumented?) Cocoa phenomenon.
  104. */
  105. left -= 6;
  106. width += 12;
  107. bottom -= 5;
  108. height += 5;
  109. }
  110. }
  111. NSRect rect = NSMakeRect (left, bottom, width, height);
  112. [widgetView setAutoresizingMask: horizMask | vertMask];
  113. [superView addSubview: widgetView]; // parent will retain the subview...
  114. [widgetView setFrame: rect];
  115. [widgetView release]; // ... so we can release the item already
  116. #endif
  117. }
  118. void structGuiControl :: v_positionInScrolledWindow (GuiObject widget, int width, int height, GuiScrolledWindow parent) {
  119. #if gtk
  120. if (! parent) return;
  121. Melder_assert (GTK_IS_SCROLLED_WINDOW (parent -> d_widget));
  122. gtk_widget_set_size_request (GTK_WIDGET (widget), width, height);
  123. gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (parent -> d_widget), GTK_WIDGET (widget));
  124. #elif motif
  125. (void) parent;
  126. XtVaSetValues (widget, XmNwidth, width, XmNheight, height, nullptr);
  127. #elif cocoa
  128. GuiCocoaScrolledWindow *scrolledWindow = (GuiCocoaScrolledWindow *) parent -> d_widget;
  129. NSView *widgetView = (NSView *) widget;
  130. NSRect rect = NSMakeRect (0, 0, width, height);
  131. [widgetView initWithFrame: rect];
  132. [widgetView setBounds: rect];
  133. [scrolledWindow setDocumentView: widgetView];
  134. [widgetView release]; // ... so we can release the item already
  135. #endif
  136. }
  137. int GuiControl_getX (GuiControl me) {
  138. #if gtk
  139. return GTK_WIDGET (my d_widget) -> allocation.x;
  140. #elif motif
  141. return my d_widget -> x;
  142. #elif cocoa
  143. return [(NSView *) my d_widget frame]. origin. x;
  144. #else
  145. return 0;
  146. #endif
  147. }
  148. int GuiControl_getY (GuiControl me) {
  149. #if gtk
  150. return GTK_WIDGET (my d_widget) -> allocation.y;
  151. #elif motif
  152. return my d_widget -> y;
  153. #elif cocoa
  154. return [(NSView *) my d_widget frame]. origin. y;
  155. #else
  156. return 0;
  157. #endif
  158. }
  159. int GuiControl_getWidth (GuiControl me) {
  160. #if gtk
  161. return GTK_WIDGET (my d_widget) -> allocation.width;
  162. #elif motif
  163. return my d_widget -> width;
  164. #elif cocoa
  165. return [(NSView *) my d_widget frame]. size. width;
  166. #else
  167. return 0;
  168. #endif
  169. }
  170. int GuiControl_getHeight (GuiControl me) {
  171. #if gtk
  172. return GTK_WIDGET (my d_widget) -> allocation.height;
  173. #elif motif
  174. return my d_widget -> height;
  175. #elif cocoa
  176. return [(NSView *) my d_widget frame]. size. height;
  177. #else
  178. return 0;
  179. #endif
  180. }
  181. void GuiControl_move (GuiControl me, int x, int y) {
  182. #if gtk
  183. GuiObject parent = gtk_widget_get_parent (GTK_WIDGET (my d_widget));
  184. if (GTK_IS_FIXED (parent)) {
  185. gtk_fixed_move (GTK_FIXED (parent), GTK_WIDGET (my d_widget), x, y);
  186. }
  187. #elif motif
  188. XtVaSetValues (my d_widget, XmNx, (Position) x, XmNy, (Position) y, nullptr); // 64-bit-compatible
  189. #elif cocoa
  190. #endif
  191. }
  192. void GuiControl_setSize (GuiControl me, int width, int height) {
  193. #if gtk
  194. gtk_widget_set_size_request (GTK_WIDGET (my d_widget), width, height);
  195. #elif motif
  196. XtVaSetValues (my d_widget, XmNwidth, (Dimension) width, XmNheight, (Dimension) height, nullptr); // 64-bit-compatible
  197. #elif cocoa
  198. #endif
  199. }
  200. /* End of file GuiControl.cpp */