| 1 |
/* GuiControl.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1993-2012,2013,2015,2017,2020,2021,2023 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 |
|
| 20 |
#include "GuiP.h" |
| 21 |
#include "machine.h" |
| 22 |
|
| 23 |
void structGuiControl :: v_positionInForm (GuiObject widget, int left, int right, int top, int bottom, GuiForm parent) { |
| 24 |
#if gtk |
| 25 |
/* |
| 26 |
* Remember the location settings for resizing. |
| 27 |
*/ |
| 28 |
d_left = left; |
| 29 |
d_right = right; |
| 30 |
d_top = top; |
| 31 |
d_bottom = bottom; |
| 32 |
/* |
| 33 |
*/ |
| 34 |
if (! parent) |
| 35 |
return; |
| 36 |
Melder_assert (parent -> d_widget); |
| 37 |
Melder_assert (GTK_IS_FIXED (parent -> d_widget)); |
| 38 |
gint parentWidth, parentHeight; |
| 39 |
gtk_widget_get_size_request (GTK_WIDGET (parent -> d_widget), & parentWidth, & parentHeight); |
| 40 |
//parentWidth = gtk_widget_get_allocated_width (GTK_WIDGET (parent -> d_widget)); |
| 41 |
//parentHeight = gtk_widget_get_allocated_height (GTK_WIDGET (parent -> d_widget)); |
| 42 |
//parentWidth = parent -> d_right; |
| 43 |
//parentHeight = parent -> d_top; |
| 44 |
if (left < 0) left += parentWidth; |
| 45 |
if (right <= 0) right += parentWidth; |
| 46 |
if (top < 0) top += parentHeight; |
| 47 |
if (bottom <= 0) bottom += parentHeight; |
| 48 |
trace (U"fixed: parent width ", parentWidth, U" height ", parentHeight); |
| 49 |
gtk_widget_set_size_request (GTK_WIDGET (widget), right - left, bottom - top); |
| 50 |
gtk_fixed_put (GTK_FIXED (parent -> d_widget), GTK_WIDGET (widget), left, top); |
| 51 |
#elif motif |
| 52 |
(void) parent; |
| 53 |
if (left >= 0) { |
| 54 |
if (right > 0) { |
| 55 |
XtVaSetValues (widget, XmNx, left, XmNwidth, right - left, nullptr); |
| 56 |
} else { |
| 57 |
XtVaSetValues (widget, XmNleftAttachment, XmATTACH_FORM, XmNleftOffset, left, XmNrightAttachment, XmATTACH_FORM, XmNrightOffset, - right, nullptr); |
| 58 |
} |
| 59 |
} else { |
| 60 |
Melder_assert (right <= 0); |
| 61 |
trace (U"parent width ", parent -> d_widget -> width); |
| 62 |
XtVaSetValues (widget, XmNrightAttachment, XmATTACH_FORM, XmNrightOffset, - right, XmNwidth, right - left, nullptr); |
| 63 |
trace (U"parent width ", parent -> d_widget -> width); |
| 64 |
} |
| 65 |
if (top >= 0) { |
| 66 |
if (bottom > 0) { |
| 67 |
XtVaSetValues (widget, XmNy, top, XmNheight, bottom - top, nullptr); |
| 68 |
} else { |
| 69 |
XtVaSetValues (widget, XmNtopAttachment, XmATTACH_FORM, XmNtopOffset, top, XmNbottomAttachment, XmATTACH_FORM, XmNbottomOffset, - bottom, nullptr); |
| 70 |
} |
| 71 |
} else { |
| 72 |
Melder_assert (bottom <= 0); |
| 73 |
XtVaSetValues (widget, XmNbottomAttachment, XmATTACH_FORM, XmNbottomOffset, - bottom, XmNheight, bottom - top, nullptr); |
| 74 |
} |
| 75 |
#elif cocoa |
| 76 |
NSView *superView = (NSView *) parent -> d_widget; |
| 77 |
NSView *widgetView = (NSView *) widget; |
| 78 |
NSRect parentRect = [superView frame]; |
| 79 |
int parentWidth = parentRect.size.width; |
| 80 |
int parentHeight = parentRect.size.height; |
| 81 |
|
| 82 |
NSUInteger horizMask = 0; |
| 83 |
if (left >= 0) { |
| 84 |
if (right <= 0) |
| 85 |
horizMask = NSViewWidthSizable; |
| 86 |
} else { |
| 87 |
horizMask = NSViewMinXMargin; |
| 88 |
} |
| 89 |
|
| 90 |
NSUInteger vertMask = 0; |
| 91 |
if (top >= 0) { |
| 92 |
vertMask = NSViewMinYMargin; |
| 93 |
if (bottom <= 0) |
| 94 |
vertMask = NSViewHeightSizable; |
| 95 |
} |
| 96 |
|
| 97 |
if (left < 0) left += parentWidth; |
| 98 |
if (right <= 0) right += parentWidth; |
| 99 |
if (top < 0) top += parentHeight; |
| 100 |
if (bottom <= 0) bottom += parentHeight; |
| 101 |
top = parentHeight - top; // flip |
| 102 |
bottom = parentHeight - bottom; // flip |
| 103 |
int width = right - left; |
| 104 |
int height = top - bottom; |
| 105 |
if ([widgetView isKindOfClass: [NSButton class]]) { |
| 106 |
if (! [widgetView isKindOfClass: [NSPopUpButton class]]) { |
| 107 |
/* |
| 108 |
On Cocoa, NSButton views show up 12 pixels less wide and 5 pixels less high than their frame. |
| 109 |
Compensate for this (undocumented?) Cocoa phenomenon. |
| 110 |
*/ |
| 111 |
if (Melder_systemVersion < 260000) { |
| 112 |
left -= 6; |
| 113 |
width += 12; |
| 114 |
bottom -= 5; |
| 115 |
height += 5; |
| 116 |
} else { |
| 117 |
left -= 2; |
| 118 |
width += 4; |
| 119 |
bottom -= 2; |
| 120 |
height += 4; |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
NSRect rect = NSMakeRect (left, bottom, width, height); |
| 125 |
[widgetView setAutoresizingMask: horizMask | vertMask]; |
| 126 |
[superView addSubview: widgetView]; // parent will retain the subview... |
| 127 |
[widgetView setFrame: rect]; |
| 128 |
[widgetView setClipsToBounds: true]; // 20231019: seems to be needed from SDK 14.0 |
| 129 |
[widgetView release]; // ... so we can release the item already |
| 130 |
#endif |
| 131 |
} |
| 132 |
|
| 133 |
void structGuiControl :: v_positionInScrolledWindow (GuiObject widget, int width, int height, GuiScrolledWindow parent) { |
| 134 |
#if gtk |
| 135 |
if (! parent) |
| 136 |
return; |
| 137 |
Melder_assert (GTK_IS_SCROLLED_WINDOW (parent -> d_widget)); |
| 138 |
gtk_widget_set_size_request (GTK_WIDGET (widget), width, height); |
| 139 |
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (parent -> d_widget), GTK_WIDGET (widget)); |
| 140 |
#elif motif |
| 141 |
(void) parent; |
| 142 |
XtVaSetValues (widget, XmNwidth, width, XmNheight, height, nullptr); |
| 143 |
#elif cocoa |
| 144 |
GuiCocoaScrolledWindow *scrolledWindow = (GuiCocoaScrolledWindow *) parent -> d_widget; |
| 145 |
NSView *widgetView = (NSView *) widget; |
| 146 |
NSRect rect = NSMakeRect (0, 0, width, height); |
| 147 |
[widgetView initWithFrame: rect]; |
| 148 |
[widgetView setBounds: rect]; |
| 149 |
[scrolledWindow setDocumentView: widgetView]; |
| 150 |
[widgetView release]; // ... so we can release the item already |
| 151 |
#endif |
| 152 |
} |
| 153 |
|
| 154 |
int GuiControl_getX (GuiControl me) { |
| 155 |
#if gtk |
| 156 |
//return GTK_WIDGET (my d_widget) -> allocation.x; |
| 157 |
GtkAllocation allocation; |
| 158 |
gtk_widget_get_allocation (GTK_WIDGET (my d_widget), & allocation); |
| 159 |
return allocation.x; |
| 160 |
#elif motif |
| 161 |
return my d_widget -> x; |
| 162 |
#elif cocoa |
| 163 |
return [(NSView *) my d_widget frame]. origin. x; |
| 164 |
#else |
| 165 |
return 0; |
| 166 |
#endif |
| 167 |
} |
| 168 |
|
| 169 |
int GuiControl_getY (GuiControl me) { |
| 170 |
#if gtk |
| 171 |
//return GTK_WIDGET (my d_widget) -> allocation.y; |
| 172 |
GtkAllocation allocation; |
| 173 |
gtk_widget_get_allocation (GTK_WIDGET (my d_widget), & allocation); |
| 174 |
return allocation.y; |
| 175 |
#elif motif |
| 176 |
return my d_widget -> y; |
| 177 |
#elif cocoa |
| 178 |
return [(NSView *) my d_widget frame]. origin. y; |
| 179 |
#else |
| 180 |
return 0; |
| 181 |
#endif |
| 182 |
} |
| 183 |
|
| 184 |
int GuiControl_getWidth (GuiControl me) { |
| 185 |
#if gtk |
| 186 |
//return GTK_WIDGET (my d_widget) -> allocation.width; |
| 187 |
GtkAllocation allocation; |
| 188 |
gtk_widget_get_allocation (GTK_WIDGET (my d_widget), & allocation); |
| 189 |
return allocation.width; |
| 190 |
#elif motif |
| 191 |
return my d_widget -> width; |
| 192 |
#elif cocoa |
| 193 |
return [(NSView *) my d_widget frame]. size. width; |
| 194 |
#else |
| 195 |
return 0; |
| 196 |
#endif |
| 197 |
} |
| 198 |
|
| 199 |
int GuiControl_getHeight (GuiControl me) { |
| 200 |
#if gtk |
| 201 |
//return GTK_WIDGET (my d_widget) -> allocation.height; |
| 202 |
GtkAllocation allocation; |
| 203 |
gtk_widget_get_allocation (GTK_WIDGET (my d_widget), & allocation); |
| 204 |
return allocation.height; |
| 205 |
#elif motif |
| 206 |
return my d_widget -> height; |
| 207 |
#elif cocoa |
| 208 |
return [(NSView *) my d_widget frame]. size. height; |
| 209 |
#else |
| 210 |
return 0; |
| 211 |
#endif |
| 212 |
} |
| 213 |
|
| 214 |
void GuiControl_move (GuiControl me, int x, int y) { |
| 215 |
#if gtk |
| 216 |
GuiObject parent = gtk_widget_get_parent (GTK_WIDGET (my d_widget)); |
| 217 |
if (GTK_IS_FIXED (parent)) { |
| 218 |
gtk_fixed_move (GTK_FIXED (parent), GTK_WIDGET (my d_widget), x, y); |
| 219 |
} |
| 220 |
#elif motif |
| 221 |
XtVaSetValues (my d_widget, XmNx, (Position) x, XmNy, (Position) y, nullptr); // 64-bit-compatible |
| 222 |
#elif cocoa |
| 223 |
#endif |
| 224 |
} |
| 225 |
|
| 226 |
void GuiControl_setSize (GuiControl me, int width, int height) { |
| 227 |
#if gtk |
| 228 |
gtk_widget_set_size_request (GTK_WIDGET (my d_widget), width, height); |
| 229 |
#elif motif |
| 230 |
XtVaSetValues (my d_widget, XmNwidth, (Dimension) width, XmNheight, (Dimension) height, nullptr); // 64-bit-compatible |
| 231 |
#elif cocoa |
| 232 |
#endif |
| 233 |
} |
| 234 |
|
| 235 |
/* End of file GuiControl.cpp */ |