1 | // ============================================================== // |
---|
2 | // // |
---|
3 | // File : AW_option_toggle.cxx // |
---|
4 | // Purpose : option-menu- and toggle-code // |
---|
5 | // // |
---|
6 | // Institute of Microbiology (Technical University Munich) // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // ============================================================== // |
---|
10 | |
---|
11 | #include "aw_at.hxx" |
---|
12 | #include "aw_window.hxx" |
---|
13 | #include "aw_awar.hxx" |
---|
14 | #include "aw_window_Xm.hxx" |
---|
15 | #include "aw_root.hxx" |
---|
16 | #include "aw_xargs.hxx" |
---|
17 | #include "aw_varupdate.hxx" |
---|
18 | #include "aw_localdef.hxx" |
---|
19 | |
---|
20 | #include <Xm/MenuShell.h> |
---|
21 | #include <Xm/RowColumn.h> |
---|
22 | #include <Xm/ToggleB.h> |
---|
23 | #include <Xm/Label.h> |
---|
24 | #include <Xm/PushB.h> |
---|
25 | |
---|
26 | __ATTR__NORETURN inline void option_type_mismatch(const char *triedType) { type_mismatch(triedType, "option-menu"); } |
---|
27 | __ATTR__NORETURN inline void toggle_type_mismatch(const char *triedType) { type_mismatch(triedType, "toggle"); } |
---|
28 | |
---|
29 | // ---------------------- |
---|
30 | // Options-Menu |
---|
31 | |
---|
32 | AW_option_menu_struct *AW_window::create_option_menu(const char *awar_name, bool /*fallback2default*/) { // @@@ remove parameter fallback2default (gtk-relict) |
---|
33 | // Note: fallback2default has no meaning in motif-version (always acts like 'false', i.e. never does fallback) |
---|
34 | // see also .@create_selection_list |
---|
35 | |
---|
36 | Widget optionMenu_shell; |
---|
37 | Widget optionMenu; |
---|
38 | Widget optionMenu1; |
---|
39 | int x_for_position_of_menu; |
---|
40 | |
---|
41 | const char *tmp_label = _at->label_for_inputfield; |
---|
42 | if (tmp_label && !tmp_label[0]) { |
---|
43 | aw_assert(0); // do not specify empty labels (causes misalignment) |
---|
44 | tmp_label = NULp; |
---|
45 | } |
---|
46 | |
---|
47 | _at->saved_x = _at->x_for_next_button - (tmp_label ? 0 : 10); |
---|
48 | x_for_position_of_menu = 10; |
---|
49 | |
---|
50 | optionMenu_shell = XtVaCreatePopupShell ("optionMenu shell", |
---|
51 | xmMenuShellWidgetClass, |
---|
52 | INFO_WIDGET, |
---|
53 | XmNwidth, 1, |
---|
54 | XmNheight, 1, |
---|
55 | XmNallowShellResize, true, |
---|
56 | XmNoverrideRedirect, true, |
---|
57 | XmNfontList, p_global->fontlist, |
---|
58 | NULp); |
---|
59 | |
---|
60 | optionMenu = XtVaCreateWidget("optionMenu_p1", |
---|
61 | xmRowColumnWidgetClass, |
---|
62 | optionMenu_shell, |
---|
63 | XmNrowColumnType, XmMENU_PULLDOWN, |
---|
64 | XmNfontList, p_global->fontlist, |
---|
65 | NULp); |
---|
66 | { |
---|
67 | aw_xargs args(3); |
---|
68 | args.add(XmNfontList, (XtArgVal)p_global->fontlist); |
---|
69 | if (!_at->attach_x && !_at->attach_lx) args.add(XmNx, x_for_position_of_menu); |
---|
70 | if (!_at->attach_y && !_at->attach_ly) args.add(XmNy, _at->y_for_next_button-5); |
---|
71 | |
---|
72 | if (tmp_label) { |
---|
73 | int width_help_label, height_help_label; |
---|
74 | calculate_label_size(&width_help_label, &height_help_label, false, tmp_label); |
---|
75 | // @@@ FIXME: use height_help_label for Y-alignment |
---|
76 | #if defined(DUMP_BUTTON_CREATION) |
---|
77 | printf("width_help_label=%i label='%s'\n", width_help_label, tmp_label); |
---|
78 | #endif // DUMP_BUTTON_CREATION |
---|
79 | |
---|
80 | { |
---|
81 | aw_assert(!AW_IS_IMAGEREF(tmp_label)); // using images as labels for option menus does not work in motif |
---|
82 | |
---|
83 | char *help_label = this->align_string(tmp_label, width_help_label); |
---|
84 | optionMenu1 = XtVaCreateManagedWidget("optionMenu1", |
---|
85 | xmRowColumnWidgetClass, |
---|
86 | (_at->attach_any) ? INFO_FORM : INFO_WIDGET, |
---|
87 | XmNrowColumnType, XmMENU_OPTION, |
---|
88 | XmNsubMenuId, optionMenu, |
---|
89 | RES_CONVERT(XmNlabelString, help_label), |
---|
90 | NULp); |
---|
91 | free(help_label); |
---|
92 | } |
---|
93 | } |
---|
94 | else { |
---|
95 | _at->x_for_next_button = _at->saved_x; |
---|
96 | |
---|
97 | optionMenu1 = XtVaCreateManagedWidget("optionMenu1", |
---|
98 | xmRowColumnWidgetClass, |
---|
99 | (_at->attach_any) ? INFO_FORM : INFO_WIDGET, |
---|
100 | XmNrowColumnType, XmMENU_OPTION, |
---|
101 | XmNsubMenuId, optionMenu, |
---|
102 | NULp); |
---|
103 | } |
---|
104 | args.assign_to_widget(optionMenu1); |
---|
105 | } |
---|
106 | |
---|
107 | #if 0 |
---|
108 | // setting background color for radio button only does not work. |
---|
109 | // works only for label and button together, but that's not what we want. |
---|
110 | TuneBackground(optionMenu_shell, TUNE_BUTTON); // set background color for radio button |
---|
111 | XtVaSetValues(optionMenu1, // colorizes background and label |
---|
112 | XmNbackground, _at->background_color, |
---|
113 | NULp); |
---|
114 | #endif |
---|
115 | |
---|
116 | get_root()->number_of_option_menus++; |
---|
117 | |
---|
118 | AW_awar *vs = root->awar(awar_name); |
---|
119 | { |
---|
120 | AW_option_menu_struct *next = |
---|
121 | new AW_option_menu_struct(get_root()->number_of_option_menus, |
---|
122 | awar_name, |
---|
123 | vs->variable_type, |
---|
124 | optionMenu1, |
---|
125 | optionMenu, |
---|
126 | _at->x_for_next_button - 7, |
---|
127 | _at->y_for_next_button, |
---|
128 | _at->correct_for_at_center); |
---|
129 | |
---|
130 | if (p_global->option_menu_list) { |
---|
131 | p_global->last_option_menu->next = next; |
---|
132 | p_global->last_option_menu = p_global->last_option_menu->next; |
---|
133 | } |
---|
134 | else { |
---|
135 | p_global->last_option_menu = p_global->option_menu_list = next; |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | p_global->current_option_menu = p_global->last_option_menu; |
---|
140 | |
---|
141 | vs->tie_widget((AW_CL)p_global->current_option_menu, optionMenu, AW_WIDGET_CHOICE_MENU, this); |
---|
142 | root->make_sensitive(optionMenu1, _at->widget_mask); |
---|
143 | |
---|
144 | return p_global->current_option_menu; |
---|
145 | } |
---|
146 | |
---|
147 | static void remove_option_from_option_menu(AW_root *aw_root, AW_widget_value_pair *os) { |
---|
148 | aw_root->remove_button_from_sens_list(os->widget); |
---|
149 | XtDestroyWidget(os->widget); |
---|
150 | } |
---|
151 | |
---|
152 | void AW_window::clear_option_menu(AW_option_menu_struct *oms) { |
---|
153 | p_global->current_option_menu = oms; // define as current (for subsequent inserts) |
---|
154 | |
---|
155 | AW_widget_value_pair *next_os; |
---|
156 | for (AW_widget_value_pair *os = oms->first_choice; os; os = next_os) { |
---|
157 | next_os = os->next; |
---|
158 | os->next = NULp; |
---|
159 | remove_option_from_option_menu(root, os); |
---|
160 | delete os; |
---|
161 | } |
---|
162 | |
---|
163 | if (oms->default_choice) { |
---|
164 | remove_option_from_option_menu(root, oms->default_choice); |
---|
165 | oms->default_choice = NULp; |
---|
166 | } |
---|
167 | |
---|
168 | oms->first_choice = NULp; |
---|
169 | oms->last_choice = NULp; |
---|
170 | } |
---|
171 | |
---|
172 | void *AW_window::_create_option_entry(AW_VARIABLE_TYPE IF_ASSERTION_USED(type), const char *name, const char */*mnemonic*/, const char *name_of_color) { |
---|
173 | Widget entry; |
---|
174 | AW_option_menu_struct *oms = p_global->current_option_menu; |
---|
175 | |
---|
176 | aw_assert(oms->variable_type == type); // adding wrong entry type |
---|
177 | |
---|
178 | TuneOrSetBackground(oms->menu_widget, name_of_color, TUNE_BUTTON); // set background color for radio button entries |
---|
179 | entry = XtVaCreateManagedWidget("optionMenu_entry", |
---|
180 | xmPushButtonWidgetClass, |
---|
181 | oms->menu_widget, |
---|
182 | RES_CONVERT(XmNlabelString, name), // force text |
---|
183 | XmNfontList, p_global->fontlist, |
---|
184 | XmNbackground, _at->background_color, |
---|
185 | NULp); |
---|
186 | AW_label_in_awar_list(this, entry, name); |
---|
187 | return (void *)entry; |
---|
188 | } |
---|
189 | |
---|
190 | inline void option_menu_add_option(AW_option_menu_struct *oms, AW_widget_value_pair *os, bool default_option) { |
---|
191 | if (default_option) { |
---|
192 | oms->default_choice = os; |
---|
193 | } |
---|
194 | else { |
---|
195 | if (oms->first_choice) { |
---|
196 | oms->last_choice->next = os; |
---|
197 | oms->last_choice = oms->last_choice->next; |
---|
198 | } |
---|
199 | else { |
---|
200 | oms->last_choice = oms->first_choice = os; |
---|
201 | } |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | void AW_window::insert_option_internal(AW_label option_name, const char *mnemonic, const char *var_value, const char *name_of_color, bool default_option) { |
---|
206 | AW_option_menu_struct *oms = p_global->current_option_menu; |
---|
207 | aw_assert(oms); // "current" option menu has to be set (insert-functions may only be used between create_option_menu/clear_option_menu and update_option_menu) |
---|
208 | |
---|
209 | if (oms->variable_type != AW_STRING) { |
---|
210 | option_type_mismatch("string"); |
---|
211 | } |
---|
212 | else { |
---|
213 | Widget entry = (Widget)_create_option_entry(AW_STRING, option_name, mnemonic, name_of_color); |
---|
214 | AW_cb *cbs = _callback; // user-own callback |
---|
215 | |
---|
216 | // callback for new choice |
---|
217 | XtAddCallback(entry, XmNactivateCallback, |
---|
218 | (XtCallbackProc) AW_variable_update_callback, |
---|
219 | (XtPointer) new VarUpdateInfo(this, NULp, AW_WIDGET_CHOICE_MENU, root->awar(oms->variable_name), var_value, cbs)); |
---|
220 | |
---|
221 | option_menu_add_option(p_global->current_option_menu, new AW_widget_value_pair(var_value, entry), default_option); |
---|
222 | root->make_sensitive(entry, _at->widget_mask); |
---|
223 | this->unset_at_commands(); |
---|
224 | } |
---|
225 | } |
---|
226 | void AW_window::insert_option_internal(AW_label option_name, const char *mnemonic, int var_value, const char *name_of_color, bool default_option) { |
---|
227 | AW_option_menu_struct *oms = p_global->current_option_menu; |
---|
228 | aw_assert(oms); // "current" option menu has to be set (insert-functions may only be used between create_option_menu/clear_option_menu and update_option_menu) |
---|
229 | |
---|
230 | if (oms->variable_type != AW_INT) { |
---|
231 | option_type_mismatch("int"); |
---|
232 | } |
---|
233 | else { |
---|
234 | Widget entry = (Widget)_create_option_entry(AW_INT, option_name, mnemonic, name_of_color); |
---|
235 | AW_cb *cbs = _callback; // user-own callback |
---|
236 | |
---|
237 | // callback for new choice |
---|
238 | XtAddCallback(entry, XmNactivateCallback, |
---|
239 | (XtCallbackProc) AW_variable_update_callback, |
---|
240 | (XtPointer) new VarUpdateInfo(this, NULp, AW_WIDGET_CHOICE_MENU, root->awar(oms->variable_name), var_value, cbs)); |
---|
241 | |
---|
242 | option_menu_add_option(p_global->current_option_menu, new AW_widget_value_pair(var_value, entry), default_option); |
---|
243 | root->make_sensitive(entry, _at->widget_mask); |
---|
244 | this->unset_at_commands(); |
---|
245 | } |
---|
246 | } |
---|
247 | void AW_window::insert_option_internal(AW_label option_name, const char *mnemonic, float var_value, const char *name_of_color, bool default_option) { |
---|
248 | AW_option_menu_struct *oms = p_global->current_option_menu; |
---|
249 | aw_assert(oms); // "current" option menu has to be set (insert-functions may only be used between create_option_menu/clear_option_menu and update_option_menu) |
---|
250 | |
---|
251 | if (oms->variable_type != AW_FLOAT) { |
---|
252 | option_type_mismatch("float"); |
---|
253 | } |
---|
254 | else { |
---|
255 | Widget entry = (Widget)_create_option_entry(AW_FLOAT, option_name, mnemonic, name_of_color); |
---|
256 | AW_cb *cbs = _callback; // user-own callback |
---|
257 | |
---|
258 | // callback for new choice |
---|
259 | XtAddCallback(entry, XmNactivateCallback, |
---|
260 | (XtCallbackProc) AW_variable_update_callback, |
---|
261 | (XtPointer) new VarUpdateInfo(this, NULp, AW_WIDGET_CHOICE_MENU, root->awar(oms->variable_name), var_value, cbs)); |
---|
262 | |
---|
263 | option_menu_add_option(p_global->current_option_menu, new AW_widget_value_pair(var_value, entry), default_option); |
---|
264 | root->make_sensitive(entry, _at->widget_mask); |
---|
265 | this->unset_at_commands(); |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | void AW_window::insert_option (const char *on, const char *mn, const char *vv, const char *noc) { insert_option_internal(on, mn, vv, noc, false); } |
---|
270 | void AW_window::insert_default_option(const char *on, const char *mn, const char *vv, const char *noc) { insert_option_internal(on, mn, vv, noc, true); } |
---|
271 | void AW_window::insert_option (const char *on, const char *mn, int vv, const char *noc) { insert_option_internal(on, mn, vv, noc, false); } |
---|
272 | void AW_window::insert_default_option(const char *on, const char *mn, int vv, const char *noc) { insert_option_internal(on, mn, vv, noc, true); } |
---|
273 | void AW_window::insert_option (const char *on, const char *mn, float vv, const char *noc) { insert_option_internal(on, mn, vv, noc, false); } |
---|
274 | void AW_window::insert_default_option(const char *on, const char *mn, float vv, const char *noc) { insert_option_internal(on, mn, vv, noc, true); } |
---|
275 | // (see insert_option_internal for longer parameter names) |
---|
276 | |
---|
277 | void AW_window::update_option_menu() { |
---|
278 | AW_option_menu_struct *oms = p_global->current_option_menu; |
---|
279 | refresh_option_menu(oms); |
---|
280 | |
---|
281 | if (_at->attach_any) aw_attach_widget(oms->label_widget, _at); |
---|
282 | |
---|
283 | short width; |
---|
284 | short height; |
---|
285 | XtVaGetValues(oms->label_widget, XmNwidth, &width, XmNheight, &height, NULp); |
---|
286 | int width_of_last_widget = width; |
---|
287 | int height_of_last_widget = height; |
---|
288 | |
---|
289 | if (!_at->to_position_exists) { |
---|
290 | if (oms->correct_for_at_center_intern == 0) { // left aligned |
---|
291 | XtVaSetValues(oms->label_widget, XmNx, short(_at->saved_x), NULp); |
---|
292 | } |
---|
293 | if (oms->correct_for_at_center_intern == 1) { // middle centered |
---|
294 | XtVaSetValues(oms->label_widget, XmNx, short(_at->saved_x - width/2), NULp); |
---|
295 | width_of_last_widget = width_of_last_widget / 2; |
---|
296 | } |
---|
297 | if (oms->correct_for_at_center_intern == 2) { // right aligned |
---|
298 | XtVaSetValues(oms->label_widget, XmNx, short(_at->saved_x - width), NULp); |
---|
299 | width_of_last_widget = 0; |
---|
300 | } |
---|
301 | } |
---|
302 | |
---|
303 | width_of_last_widget += SPACE_BEHIND_BUTTON; |
---|
304 | |
---|
305 | this->unset_at_commands(); |
---|
306 | this->increment_at_commands(width_of_last_widget, height_of_last_widget); |
---|
307 | } |
---|
308 | |
---|
309 | void AW_window::refresh_option_menu(AW_option_menu_struct *oms) { |
---|
310 | if (get_root()->changer_of_variable != oms->label_widget) { |
---|
311 | AW_widget_value_pair *active_choice = oms->first_choice; |
---|
312 | { |
---|
313 | AW_scalar global_var_value(root->awar(oms->variable_name)); |
---|
314 | while (active_choice && global_var_value != active_choice->value) { |
---|
315 | active_choice = active_choice->next; |
---|
316 | } |
---|
317 | } |
---|
318 | |
---|
319 | if (!active_choice) active_choice = oms->default_choice; |
---|
320 | if (active_choice) XtVaSetValues(oms->label_widget, XmNmenuHistory, active_choice->widget, NULp); |
---|
321 | |
---|
322 | } |
---|
323 | } |
---|
324 | |
---|
325 | // ------------------------------------------------------- |
---|
326 | // toggle field (actually this are radio buttons) |
---|
327 | |
---|
328 | void AW_window::create_toggle_field(const char *var_name, AW_label labeli, const char */*mnemonic*/) { |
---|
329 | /*! |
---|
330 | * Begins a radio button group with a label |
---|
331 | */ |
---|
332 | if (labeli) { |
---|
333 | this->label(labeli); |
---|
334 | } |
---|
335 | create_toggle_field(var_name); |
---|
336 | } |
---|
337 | |
---|
338 | void AW_window::create_toggle_field(const char *var_name, int orientation /*= 0*/) { |
---|
339 | /*! |
---|
340 | * Begins a radio button group |
---|
341 | * @param var_name name of awar |
---|
342 | * @param orientation 0 -> vertical, != 0 horizontal layout |
---|
343 | */ |
---|
344 | |
---|
345 | Widget label_for_toggle; |
---|
346 | Widget toggle_field; |
---|
347 | |
---|
348 | int xoff_for_label = 0; |
---|
349 | int width_of_label = 0; |
---|
350 | int x_for_position_of_option = 0; |
---|
351 | |
---|
352 | const char *tmp_label = ""; |
---|
353 | |
---|
354 | if (_at->label_for_inputfield) { |
---|
355 | tmp_label = _at->label_for_inputfield; |
---|
356 | } |
---|
357 | |
---|
358 | if (_at->correct_for_at_center) { |
---|
359 | _at->saved_x = _at->x_for_next_button; |
---|
360 | x_for_position_of_option = 10; |
---|
361 | } |
---|
362 | else { |
---|
363 | x_for_position_of_option = _at->x_for_next_button; |
---|
364 | } |
---|
365 | |
---|
366 | if (tmp_label) { |
---|
367 | int height_of_label; |
---|
368 | calculate_label_size(&width_of_label, &height_of_label, true, tmp_label); |
---|
369 | // @@@ FIXME: use height_of_label for Y-alignment |
---|
370 | // width_of_label = this->calculate_string_width( this->calculate_label_length() ); |
---|
371 | label_for_toggle = XtVaCreateManagedWidget("label", |
---|
372 | xmLabelWidgetClass, |
---|
373 | INFO_WIDGET, |
---|
374 | XmNx, (int)_at->x_for_next_button, |
---|
375 | XmNy, (int)(_at->y_for_next_button) + this->get_root()->y_correction_for_input_labels, |
---|
376 | XmNwidth, (int)(width_of_label + 2), |
---|
377 | RES_CONVERT(XmNlabelString, tmp_label), |
---|
378 | XmNrecomputeSize, false, |
---|
379 | XmNalignment, XmALIGNMENT_BEGINNING, |
---|
380 | XmNfontList, p_global->fontlist, |
---|
381 | NULp); |
---|
382 | |
---|
383 | _at->saved_xoff_for_label = xoff_for_label = width_of_label + 10; |
---|
384 | |
---|
385 | p_w->toggle_label = label_for_toggle; |
---|
386 | } |
---|
387 | else { |
---|
388 | p_w->toggle_label = NULp; |
---|
389 | _at->saved_xoff_for_label = 0; |
---|
390 | } |
---|
391 | |
---|
392 | { |
---|
393 | aw_xargs args(6); |
---|
394 | args.add(XmNx, x_for_position_of_option + xoff_for_label); |
---|
395 | args.add(XmNy, _at->y_for_next_button - 2); |
---|
396 | args.add(XmNradioBehavior, True); |
---|
397 | args.add(XmNradioAlwaysOne, True); |
---|
398 | args.add(XmNfontList, (XtArgVal)p_global->fontlist); |
---|
399 | args.add(XmNorientation, orientation ? XmHORIZONTAL : XmVERTICAL); |
---|
400 | |
---|
401 | toggle_field = XtVaCreateManagedWidget("rowColumn for toggle field", xmRowColumnWidgetClass, (_at->attach_any) ? INFO_FORM : INFO_WIDGET, NULp); |
---|
402 | |
---|
403 | args.assign_to_widget(toggle_field); |
---|
404 | } |
---|
405 | if (_at->attach_any) { |
---|
406 | aw_attach_widget(toggle_field, _at, 300); |
---|
407 | } |
---|
408 | |
---|
409 | AW_awar *vs = root->awar(var_name); |
---|
410 | |
---|
411 | p_w->toggle_field = toggle_field; |
---|
412 | free((p_w->toggle_field_var_name)); |
---|
413 | p_w->toggle_field_var_name = strdup(var_name); |
---|
414 | p_w->toggle_field_var_type = vs->variable_type; |
---|
415 | |
---|
416 | get_root()->number_of_toggle_fields++; |
---|
417 | |
---|
418 | if (p_global->toggle_field_list) { |
---|
419 | p_global->last_toggle_field->next = new AW_toggle_field_struct(get_root()->number_of_toggle_fields, var_name, vs->variable_type, toggle_field, _at->correct_for_at_center); |
---|
420 | p_global->last_toggle_field = p_global->last_toggle_field->next; |
---|
421 | } |
---|
422 | else { |
---|
423 | p_global->last_toggle_field = p_global->toggle_field_list = new AW_toggle_field_struct(get_root()->number_of_toggle_fields, var_name, vs->variable_type, toggle_field, _at->correct_for_at_center); |
---|
424 | } |
---|
425 | |
---|
426 | vs->tie_widget(get_root()->number_of_toggle_fields, toggle_field, AW_WIDGET_TOGGLE_FIELD, this); |
---|
427 | root->make_sensitive(toggle_field, _at->widget_mask); |
---|
428 | } |
---|
429 | |
---|
430 | static Widget _aw_create_toggle_entry(AW_window *aww, Widget toggle_field, |
---|
431 | const char *labeltext, const char *mnemonic, |
---|
432 | VarUpdateInfo *awus, |
---|
433 | AW_widget_value_pair *toggle, bool default_toggle) { |
---|
434 | AW_root *root = aww->get_root(); |
---|
435 | |
---|
436 | Label label(labeltext, aww); |
---|
437 | Widget toggleButton = XtVaCreateManagedWidget("toggleButton", |
---|
438 | xmToggleButtonWidgetClass, |
---|
439 | toggle_field, |
---|
440 | RES_LABEL_CONVERT(label), |
---|
441 | RES_CONVERT(XmNmnemonic, mnemonic), |
---|
442 | XmNindicatorSize, 16, |
---|
443 | XmNfontList, p_global->fontlist, |
---|
444 | NULp); |
---|
445 | |
---|
446 | toggle->widget = toggleButton; |
---|
447 | awus->set_widget(toggleButton); |
---|
448 | XtAddCallback(toggleButton, XmNvalueChangedCallback, |
---|
449 | (XtCallbackProc) AW_variable_update_callback, |
---|
450 | (XtPointer) awus); |
---|
451 | if (default_toggle) { |
---|
452 | delete p_global->last_toggle_field->default_toggle; |
---|
453 | p_global->last_toggle_field->default_toggle = toggle; |
---|
454 | } |
---|
455 | else { |
---|
456 | if (p_global->last_toggle_field->first_toggle) { |
---|
457 | p_global->last_toggle_field->last_toggle->next = toggle; |
---|
458 | p_global->last_toggle_field->last_toggle = toggle; |
---|
459 | } |
---|
460 | else { |
---|
461 | p_global->last_toggle_field->last_toggle = toggle; |
---|
462 | p_global->last_toggle_field->first_toggle = toggle; |
---|
463 | } |
---|
464 | } |
---|
465 | root->make_sensitive(toggleButton, aww->get_at().widget_mask); |
---|
466 | |
---|
467 | aww->unset_at_commands(); |
---|
468 | return toggleButton; |
---|
469 | } |
---|
470 | |
---|
471 | |
---|
472 | void AW_window::insert_toggle_internal(AW_label toggle_label, const char *mnemonic, const char *var_value, bool default_toggle) { |
---|
473 | if (p_w->toggle_field_var_type != AW_STRING) { |
---|
474 | toggle_type_mismatch("string"); |
---|
475 | } |
---|
476 | else { |
---|
477 | _aw_create_toggle_entry(this, p_w->toggle_field, toggle_label, mnemonic, |
---|
478 | new VarUpdateInfo(this, NULp, AW_WIDGET_TOGGLE_FIELD, root->awar(p_w->toggle_field_var_name), var_value, _callback), |
---|
479 | new AW_widget_value_pair(var_value, NULp), |
---|
480 | default_toggle); |
---|
481 | } |
---|
482 | } |
---|
483 | void AW_window::insert_toggle_internal(AW_label toggle_label, const char *mnemonic, int var_value, bool default_toggle) { |
---|
484 | if (p_w->toggle_field_var_type != AW_INT) { |
---|
485 | toggle_type_mismatch("int"); |
---|
486 | } |
---|
487 | else { |
---|
488 | _aw_create_toggle_entry(this, p_w->toggle_field, toggle_label, mnemonic, |
---|
489 | new VarUpdateInfo(this, NULp, AW_WIDGET_TOGGLE_FIELD, root->awar(p_w->toggle_field_var_name), var_value, _callback), |
---|
490 | new AW_widget_value_pair(var_value, NULp), |
---|
491 | default_toggle); |
---|
492 | } |
---|
493 | } |
---|
494 | void AW_window::insert_toggle_internal(AW_label toggle_label, const char *mnemonic, float var_value, bool default_toggle) { |
---|
495 | if (p_w->toggle_field_var_type != AW_FLOAT) { |
---|
496 | toggle_type_mismatch("float"); |
---|
497 | } |
---|
498 | else { |
---|
499 | _aw_create_toggle_entry(this, p_w->toggle_field, toggle_label, mnemonic, |
---|
500 | new VarUpdateInfo(this, NULp, AW_WIDGET_TOGGLE_FIELD, root->awar(p_w->toggle_field_var_name), var_value, _callback), |
---|
501 | new AW_widget_value_pair(var_value, NULp), |
---|
502 | default_toggle); |
---|
503 | } |
---|
504 | } |
---|
505 | |
---|
506 | |
---|
507 | void AW_window::insert_toggle (const char *toggle_label, const char *mnemonic, const char *var_value) { insert_toggle_internal(toggle_label, mnemonic, var_value, false); } |
---|
508 | void AW_window::insert_default_toggle(const char *toggle_label, const char *mnemonic, const char *var_value) { insert_toggle_internal(toggle_label, mnemonic, var_value, true); } |
---|
509 | void AW_window::insert_toggle (const char *toggle_label, const char *mnemonic, int var_value) { insert_toggle_internal(toggle_label, mnemonic, var_value, false); } |
---|
510 | void AW_window::insert_default_toggle(const char *toggle_label, const char *mnemonic, int var_value) { insert_toggle_internal(toggle_label, mnemonic, var_value, true); } |
---|
511 | void AW_window::insert_toggle (const char *toggle_label, const char *mnemonic, float var_value) { insert_toggle_internal(toggle_label, mnemonic, var_value, false); } |
---|
512 | void AW_window::insert_default_toggle(const char *toggle_label, const char *mnemonic, float var_value) { insert_toggle_internal(toggle_label, mnemonic, var_value, true); } |
---|
513 | |
---|
514 | void AW_window::update_toggle_field() { |
---|
515 | this->refresh_toggle_field(get_root()->number_of_toggle_fields); |
---|
516 | } |
---|
517 | |
---|
518 | |
---|
519 | void AW_window::refresh_toggle_field(int toggle_field_number) { |
---|
520 | #if defined(DEBUG) |
---|
521 | static int inside_here = 0; |
---|
522 | aw_assert(!inside_here); |
---|
523 | inside_here++; |
---|
524 | #endif // DEBUG |
---|
525 | |
---|
526 | AW_toggle_field_struct *toggle_field_list = p_global->toggle_field_list; |
---|
527 | { |
---|
528 | while (toggle_field_list) { |
---|
529 | if (toggle_field_number == toggle_field_list->toggle_field_number) { |
---|
530 | break; |
---|
531 | } |
---|
532 | toggle_field_list = toggle_field_list->next; |
---|
533 | } |
---|
534 | } |
---|
535 | |
---|
536 | if (toggle_field_list) { |
---|
537 | AW_widget_value_pair *active_toggle = toggle_field_list->first_toggle; |
---|
538 | { |
---|
539 | AW_scalar global_value(root->awar(toggle_field_list->variable_name)); |
---|
540 | while (active_toggle && active_toggle->value != global_value) { |
---|
541 | active_toggle = active_toggle->next; |
---|
542 | } |
---|
543 | if (!active_toggle) active_toggle = toggle_field_list->default_toggle; |
---|
544 | } |
---|
545 | |
---|
546 | // iterate over all toggles including default_toggle and set their state |
---|
547 | for (AW_widget_value_pair *toggle = toggle_field_list->first_toggle; toggle;) { |
---|
548 | XmToggleButtonSetState(toggle->widget, toggle == active_toggle, False); |
---|
549 | |
---|
550 | if (toggle->next) toggle = toggle->next; |
---|
551 | else if (toggle != toggle_field_list->default_toggle) toggle = toggle_field_list->default_toggle; |
---|
552 | else toggle = NULp; |
---|
553 | } |
---|
554 | |
---|
555 | // @@@ code below should go to update_toggle_field |
---|
556 | { |
---|
557 | short length; |
---|
558 | short height; |
---|
559 | XtVaGetValues(p_w->toggle_field, XmNwidth, &length, XmNheight, &height, NULp); |
---|
560 | length += (short)_at->saved_xoff_for_label; |
---|
561 | |
---|
562 | int width_of_last_widget = length; |
---|
563 | int height_of_last_widget = height; |
---|
564 | |
---|
565 | if (toggle_field_list->correct_for_at_center_intern) { |
---|
566 | if (toggle_field_list->correct_for_at_center_intern == 1) { // middle centered |
---|
567 | XtVaSetValues(p_w->toggle_field, XmNx, (short)((short)_at->saved_x - (short)(length/2) + (short)_at->saved_xoff_for_label), NULp); |
---|
568 | if (p_w->toggle_label) { |
---|
569 | XtVaSetValues(p_w->toggle_label, XmNx, (short)((short)_at->saved_x - (short)(length/2)), NULp); |
---|
570 | } |
---|
571 | width_of_last_widget = width_of_last_widget / 2; |
---|
572 | } |
---|
573 | if (toggle_field_list->correct_for_at_center_intern == 2) { // right centered |
---|
574 | XtVaSetValues(p_w->toggle_field, XmNx, (short)((short)_at->saved_x - length + (short)_at->saved_xoff_for_label), NULp); |
---|
575 | if (p_w->toggle_label) { |
---|
576 | XtVaSetValues(p_w->toggle_label, XmNx, (short)((short)_at->saved_x - length), NULp); |
---|
577 | } |
---|
578 | width_of_last_widget = 0; |
---|
579 | } |
---|
580 | } |
---|
581 | |
---|
582 | this->unset_at_commands(); |
---|
583 | this->increment_at_commands(width_of_last_widget, height_of_last_widget); |
---|
584 | } |
---|
585 | } |
---|
586 | else { |
---|
587 | GBK_terminatef("update_toggle_field: toggle field %i does not exist", toggle_field_number); |
---|
588 | } |
---|
589 | |
---|
590 | #if defined(DEBUG) |
---|
591 | inside_here--; |
---|
592 | #endif // DEBUG |
---|
593 | } |
---|