Debian Codegraph @

sys/GuiLabel.cpp

raw
1 /* GuiLabel.cpp
2 *
3 * Copyright (C) 1993-2008,2010-2018,2020,2024 Paul Boersma, 2007 Stefan de Konink
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
19 #include "GuiP.h"
20
21 Thing_implement (GuiLabel, GuiControl, 0);
22
23 #if gtk
24 #define iam_label GuiLabel me = (GuiLabel) _GuiObject_getUserData (widget)
25 #elif motif
26 #define iam_label GuiLabel me = (GuiLabel) widget -> userData
27 #elif cocoa
28 #define iam_label GuiLabel me = (GuiLabel) [(GuiCocoaLabel *) widget userData];
29 #endif
30
31 #if gtk
32 static void _GuiGtkLabel_destroyCallback (GuiObject widget, gpointer void_me) {
33 (void) widget;
34 iam (GuiLabel);
35 forget (me);
36 }
37 #elif motif
38 void _GuiWinLabel_destroy (GuiObject widget) {
39 iam_label;
40 _GuiNativeControl_destroy (widget);
41 forget (me); // NOTE: my widget is not destroyed here
42 }
43 #elif cocoa
44 @implementation GuiCocoaLabel {
45 GuiLabel d_userData;
46 }
47 - (void) dealloc { // override
48 GuiLabel me = d_userData;
49 forget (me);
50 trace (U"deleting a label");
51 [super dealloc];
52 }
53 - (GuiThing) getUserData {
54 return d_userData;
55 }
56 - (void) setUserData: (GuiThing) userData {
57 Melder_assert (userData == nullptr || Thing_isa (userData, classGuiLabel));
58 d_userData = static_cast <GuiLabel> (userData);
59 }
60 @end
61 #endif
62
63 GuiLabel GuiLabel_create (GuiForm parent, int left, int right, int top, int bottom,
64 conststring32 labelText, uint32 flags)
65 {
66 autoGuiLabel me = Thing_new (GuiLabel);
67 my d_shell = parent -> d_shell;
68 my d_parent = parent;
69 #if gtk
70 if (flags & GuiLabel_BOLD) {
71 my d_widget = gtk_label_new (nullptr);
72 const char *format = "<b>%s</b>";
73 char *markedUpText = g_markup_printf_escaped (format, Melder_peek32to8 (labelText));
74 gtk_label_set_markup (GTK_LABEL (my d_widget), markedUpText);
75 g_free (markedUpText);
76 } else {
77 my d_widget = gtk_label_new (Melder_peek32to8 (labelText));
78 }
79 _GuiObject_setUserData (my d_widget, me.get());
80 my v_positionInForm (my d_widget, left, right, top, bottom, parent);
81 g_signal_connect (G_OBJECT (my d_widget), "destroy", G_CALLBACK (_GuiGtkLabel_destroyCallback), me.get());
82 /*
83 The following is deprecated in GTK 3:
84 */
85 //gtk_misc_set_alignment (GTK_MISC (my d_widget), flags & GuiLabel_RIGHT ? 1.0 : flags & GuiLabel_CENTRE ? 0.5 : 0.0, 0.5);
86 /*
87 So, what to do after the above function is removed from GTK?
88 First, it was meant to be this:
89 */
90 #if 0
91 gtk_widget_set_halign (GTK_WIDGET (my d_widget), (flags & GuiLabel_RIGHT ? GTK_ALIGN_END : (flags & GuiLabel_CENTRE ? GTK_ALIGN_CENTER : GTK_ALIGN_START)));
92 gtk_widget_set_valign (GTK_WIDGET (my d_widget), GTK_ALIGN_BASELINE);
93 #endif
94 /*
95 Unfortunately, those two functions do nothing at all.
96 Second, GTK 3.15 or so introduced the following:
97 */
98 #if 1
99 gtk_label_set_xalign (GTK_LABEL (my d_widget), flags & GuiLabel_RIGHT ? 1.0 : flags & GuiLabel_CENTRE ? 0.5 : 0.0);
100 gtk_label_set_yalign (GTK_LABEL (my d_widget), 0.5);
101 #endif
102 /*
103 A perhaps related comment by the authors of GTK 3 (https://bugzilla.gnome.org/show_bug.cgi?id=733981):
104 "
105 Matthias Clasen 2015-08-28 20:14:23 UTC
106 Don't think there is anything for us to fix here.
107 "
108 */
109 /*
110 For multiline labels:
111 */
112 gtk_label_set_line_wrap (GTK_LABEL (my d_widget), true);
113 gtk_label_set_justify (GTK_LABEL (my d_widget), flags & GuiLabel_RIGHT ? GTK_JUSTIFY_RIGHT : flags & GuiLabel_CENTRE ? GTK_JUSTIFY_CENTER : GTK_JUSTIFY_LEFT);
114 #elif motif
115 my d_widget = _Gui_initializeWidget (xmLabelWidgetClass, parent -> d_widget, labelText);
116 _GuiObject_setUserData (my d_widget, me.get());
117 my d_widget -> window = CreateWindow (L"static", Melder_peek32toW (_GuiWin_expandAmpersands (my d_widget -> name.get())),
118 WS_CHILD
119 | ( flags & GuiLabel_RIGHT ? SS_RIGHT : flags & GuiLabel_CENTRE ? SS_CENTER : SS_LEFT )
120 | ( flags & GuiLabel_MULTILINE ? 0 : SS_CENTERIMAGE),
121 my d_widget -> x, my d_widget -> y, my d_widget -> width, my d_widget -> height,
122 my d_widget -> parent -> window, (HMENU) 1, theGui.instance, nullptr);
123 SetWindowLongPtr (my d_widget -> window, GWLP_USERDATA, (LONG_PTR) my d_widget);
124 if (flags & GuiLabel_BOLD)
125 SetWindowFont (my d_widget -> window, theWinGuiBoldLabelFont (), false);
126 else
127 SetWindowFont (my d_widget -> window, theWinGuiNormalLabelFont (), false);
128 my v_positionInForm (my d_widget, left, right, top, bottom, parent);
129 #elif cocoa
130 trace (U"create");
131 GuiCocoaLabel *label = [[GuiCocoaLabel alloc] init];
132 my d_widget = label;
133 trace (U"position");
134 my v_positionInForm (my d_widget, left, right, top, bottom, parent);
135 trace (U"set user data");
136 [label setUserData: me.get()];
137 trace (U"set bezel style");
138 [label setBezelStyle: NSTextFieldRoundedBezel];
139 trace (U"set bordered");
140 [label setBordered: NO];
141 trace (U"set selectable");
142 [label setSelectable: NO];
143 trace (U"title");
144 [label setTitleWithMnemonic: (NSString *) Melder_peek32toCfstring (labelText)];
145 [label setAlignment:( flags & GuiLabel_RIGHT ? NSRightTextAlignment : flags & GuiLabel_CENTRE ? NSCenterTextAlignment : NSLeftTextAlignment )];
146 if (flags & GuiLabel_BOLD)
147 [label setFont: theMacGuiBoldLabelFont ()];
148 else
149 [label setFont: theMacGuiNormalLabelFont ()];
150 #endif
151 return me.releaseToAmbiguousOwner();
152 }
153
154 GuiLabel GuiLabel_createShown (GuiForm parent, int left, int right, int top, int bottom,
155 conststring32 labelText, uint32 flags)
156 {
157 GuiLabel me = GuiLabel_create (parent, left, right, top, bottom, labelText, flags);
158 GuiThing_show (me);
159 return me;
160 }
161
162 void GuiLabel_setText (GuiLabel me, conststring32 text /* cattable */) {
163 #if gtk
164 gtk_label_set_text (GTK_LABEL (my d_widget), Melder_peek32to8 (text));
165 #elif motif
166 my d_widget -> name = Melder_dup_f (text);
167 _GuiNativeControl_setTitle (my d_widget);
168 #elif cocoa
169 [(NSTextField *) my d_widget setTitleWithMnemonic: (NSString *) Melder_peek32toCfstring (text)];
170 #endif
171 }
172
173 /* End of file GuiLabel.cpp */