2
* Toggle.c, vi: tabstop=4
4
* Copyright (C) 1998 Rasca Gmelch, Berlin
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Library General Public
9
* License as published by the Free Software Foundation; either
10
* version 2 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Library General Public License for more details.
17
* You should have received a copy of the GNU Library General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23
#define __FILE__ "Toggle.c"
28
#include <X11/StringDefs.h>
29
#include <X11/IntrinsicP.h>
31
#include <Xw/ToggleP.h>
33
#define offset(field) XtOffsetOf (XwToggleRec, field)
37
static XtResource resources [] = {
39
{ XtNstate, XtCState, XtRBoolean, sizeof(Boolean),
40
offset(toggle.state), XtRBoolean, (XtPointer)0 },
41
{ XtNdoubleClickCallback, XtCCallback, XtRCallback, sizeof(XtPointer),
42
offset(toggle.double_callbacks), XtRCallback, (XtPointer)NULL },
44
{ XtNactiveColor, XtCActiveColor, XtRPixel, sizeof(Pixel),
45
offset(button.active_pixel), XtRString, "white" },
46
{ XtNaccel, XtCAccel, XtRString, sizeof(String),
47
offset(button.accel), XtRImmediate, "toggle() notify()" },
49
{ XtNlabel, XtCLabel, XtRString, sizeof(String),
50
offset (label.label), XtRImmediate, NULL },
52
{ XtNshadowType, XtCShadowType, XtRInt, sizeof(int),
53
offset(base.shadow_type), XtRImmediate, (XtPointer)XtShadowUp },
57
* action function declarations
59
static void Highlight (Widget, XEvent*, String*, Cardinal *),
60
Unhighlight (Widget, XEvent*, String*, Cardinal *),
61
Focus (Widget, XEvent*, String*, Cardinal *),
62
GetFocus (Widget, XEvent*, String*, Cardinal *),
63
Reset (Widget, XEvent*, String*, Cardinal *),
64
Set (Widget, XEvent*, String*, Cardinal *),
65
Toggle (Widget, XEvent*, String*, Cardinal *),
66
Notify (Widget, XEvent*, String*, Cardinal *),
67
Unset (Widget, XEvent*, String*, Cardinal *),
68
Popup (Widget, XEvent*, String*, Cardinal *),
69
Tab (Widget, XEvent*, String*, Cardinal *);
74
static XtActionsRec actions [] = {
75
{"highlight", Highlight },
76
{"unhighlight", Unhighlight },
78
{"get-focus", GetFocus },
85
{"tab", Tab }, /* execute the XtNtabCallback functions */
89
* default translation table
91
static char defaultTranslations [] = "\
92
<EnterWindow>: highlight()\n\
93
<LeaveWindow>: unhighlight()\n\
94
<FocusIn>: focus(in)\n\
95
<FocusOut>: focus(out)\n\
96
<Btn1Down>,<Btn1Up>: toggle() notify() get-focus()\n\
97
<Btn2Down>,<Btn2Up>: toggle() notify() get-focus()\n\
98
<Btn3Down>: notify() popup()\n\
99
Shift<Key>Tab: tab(prev)\n\
100
<Key>Tab: tab(next)\n\
101
<Key>space: toggle() notify()";
104
* method function declarations
108
Initialize (Widget treq, Widget tnew, ArgList args, Cardinal *nums),
109
ClassInitialize (void);
111
SetValues (Widget, Widget, Widget, ArgList, Cardinal* ),
112
AcceptFocus (Widget w, Time *t);
116
Redisplay (Widget w, XEvent *event, Region region);
121
static void DrawToggle (Widget w);
124
* class record initialization
126
#define BaseClass ((WidgetClass)&xwBaseClassRec)
127
#define SuperClass ((WidgetClass)&xwButtonClassRec)
128
#define ThisClass ((WidgetClass)&xwToggleClassRec)
129
XwToggleClassRec xwToggleClassRec = {
131
/* core_class fields */
132
/* superclass */ SuperClass,
133
/* class_name */ "XwToggle",
134
/* widget_size */ sizeof(XwToggleRec),
135
/* class_initialize */ ClassInitialize,
136
/* class_part_initialize */ NULL,
137
/* class_inited */ False,
138
/* initialize */ Initialize,
139
/* initialize_hook */ NULL,
140
/* realize */ XtInheritRealize,
141
/* actions */ actions,
142
/* num_actions */ XtNumber(actions),
143
/* resources */ resources,
144
/* num_resources */ XtNumber(resources),
145
/* xrm_class */ NULLQUARK,
146
/* compress_motion */ True,
147
/* compress_exposure */ XtExposeCompressMultiple,
148
/* compress_enterleave */ True,
149
/* visible_interest */ True,
150
/* destroy */ Destroy,
152
/* expose */ Redisplay,
153
/* set_values */ SetValues,
154
/* set_values_hook */ NULL,
155
/* set_values_almost */ XtInheritSetValuesAlmost,
156
/* get_values_hook */ NULL,
157
/* accept_focus */ AcceptFocus,
158
/* version */ XtVersion,
159
/* callback_private */ NULL,
160
/* tm_table */ defaultTranslations,
161
/* query_geometry */ XtInheritQueryGeometry,
162
/* display_accelerator */ XtInheritDisplayAccelerator,
165
{ /* base_class fields */
167
XtInheritFocusAction,
168
XtInheritGetFocusAction,
170
{ /* label_class fields */
173
{ /* button_class fields */
177
{ /* toggle_class fields */
182
WidgetClass xwToggleWidgetClass = ThisClass;
186
* method function definitions
188
#define FontHeight(f) (int)(f->max_bounds.ascent + f->max_bounds.descent)
189
#define FontAscent(f) (int)(f->max_bounds.ascent)
190
#define FontDescent(f) (int)(f->max_bounds.descent)
191
#define TextWidth(f,s) XTextWidth (f,s,strlen(s))
196
ClassInitialize (void)
198
XtRegisterGrabAction (Popup, True,
199
ButtonPressMask | ButtonReleaseMask,
200
GrabModeAsync, GrabModeAsync);
207
Initialize (Widget treq, Widget tnew, ArgList args, Cardinal *num_args)
209
XwToggleWidget new = (XwToggleWidget) tnew;
211
printf ("%s: Initialize(%s) state=%d\n",
212
__FILE__, new->core.name, new->toggle.state);
214
if (new->base.shadow_type != XtShadowNone) {
215
if (new->toggle.state > 0)
216
new->base.shadow_type = XtShadowDown;
218
new->base.shadow_type = XtShadowUp;
220
new->toggle.multi_click_time = XtGetMultiClickTime (XtDisplay(new)) * 1.2;
228
SetValues (Widget current, Widget request, Widget super,
229
ArgList args, Cardinal *nargs)
231
Boolean redraw = False;
232
XwToggleWidget tw = (XwToggleWidget) current;
233
XwToggleWidget newi = (XwToggleWidget) super;
235
if (tw->toggle.state != newi->toggle.state) {
237
if (newi->base.shadow_type != XtShadowNone) {
238
if (newi->toggle.state) {
239
newi->base.shadow_type = XtShadowDown;
241
newi->base.shadow_type = XtShadowUp;
245
if (tw->core.sensitive != newi->core.sensitive) {
249
printf ("%s: SetValues(%s ..) redraw=%d\n", __FILE__, newi->core.name, (int)redraw);
262
printf ("%s: Destroy (%s)\n", __FILE__, w->core.name);
274
printf ("%s: Resize(%s)\n", __FILE__, w->core.name);
282
Redisplay (Widget w, XEvent *event, Region region)
285
printf ("%s: Redisplay(%s)\n", __FILE__, w->core.name);
287
if (!XtIsRealized(w))
289
if (!((XwToggleWidget)w)->base.shadow_width) {
292
(*xwButtonClassRec.core_class.expose)(w, event, region);
299
AcceptFocus (Widget w, Time *t)
302
printf ("%s: AcceptFocus(%s)\n", __FILE__, w->core.name);
304
if (!XtIsRealized(w))
308
if (!XtIsSensitive(w))
315
* action function definitions
320
Highlight (Widget w, XEvent *event, String *params, Cardinal *num_params)
322
/* static Pixel bg; */
323
XwToggleWidget tw = (XwToggleWidget) w;
325
printf ("%s: Highlight(%s) event=%d\n", __FILE__, tw->label.text, event->type);
327
if (!tw->core.sensitive)
329
tw->label.textGC = tw->button.focusGC;
330
tw->button.bgGC = tw->button.focus_bg_gc;
336
Unhighlight (Widget w, XEvent *event, String *params, Cardinal *num_params)
338
/* static Pixel bg; */
339
XwToggleWidget tw = (XwToggleWidget) w;
341
printf ("%s: Unhighlight(%s) event=%d\n", __FILE__, tw->label.text, event->type);
343
if (!tw->core.sensitive)
345
tw->label.textGC = tw->label.sensitiveGC;
346
tw->button.bgGC = tw->button.std_bg_gc;
352
Reset (Widget w, XEvent *event, String *params, Cardinal *num_params)
354
XwToggleWidget bw = (XwToggleWidget) w;
356
printf ("%s:Reset(%s)\n", __FILE__, bw->label.text);
358
if (bw->toggle.state) {
359
Unset (w, event, params, num_params);
361
Unhighlight (w, event, params, num_params);
366
Notify (Widget w, XEvent *event, String *params, Cardinal *num_params)
368
XwToggleWidget tw = (XwToggleWidget) w;
369
Boolean doubleClick = False;
371
printf ("%s: Notify(%s)\n", __FILE__, tw->label.text);
373
if (event->type == ButtonRelease) {
374
if ( event->xbutton.time - tw->toggle.time
375
< tw->toggle.multi_click_time ) {
379
if (!tw->toggle.state) {
380
tw->toggle.state = True;
381
XtVaSetValues (w, XtNshadowType, XtShadowDown, NULL);
383
XtCallCallbackList (w, tw->toggle.double_callbacks, event);
385
tw->toggle.time = event->xbutton.time;
388
XtCallCallbackList (w, tw->button.notify_callbacks, (XtPointer)event);
393
Set (Widget w, XEvent *event, String *params, Cardinal *num_params)
395
XwToggleWidget bw = (XwToggleWidget) w;
397
printf ("%s:Set(%s)\n", __FILE__, bw->label.text);
399
if (bw->toggle.state)
401
bw->toggle.state = True;
402
XtVaSetValues (w, XtNshadowType, XtShadowDown, NULL);
406
Unset (Widget w, XEvent *event, String *params, Cardinal *num_params)
408
XwToggleWidget bw = (XwToggleWidget) w;
411
printf ("%s:Unset(%s)\n", __FILE__, bw->label.text);
413
if (!bw->toggle.state)
415
bw->toggle.state = False;
416
XtVaSetValues (w, XtNshadowType, XtShadowUp, NULL);
422
Popup (Widget w, XEvent *event, String *params, Cardinal *num_params)
425
printf ("%s: Popup(%s)\n", __FILE__, w->core.name);
427
(*xwToggleClassRec.button_class.popup_menu)(w, event, params, num_params);
434
Toggle (Widget w, XEvent *event, String *params, Cardinal *num_params)
436
XwToggleWidget tw = (XwToggleWidget) w;
438
printf ("%s: Toggle(%s)\n", __FILE__, tw->label.text);
441
if (tw->toggle.state) {
442
tw->toggle.state = False;
443
XtVaSetValues (w, XtNshadowType, XtShadowUp, NULL);
445
tw->toggle.state = True;
446
XtVaSetValues (w, XtNshadowType, XtShadowDown, NULL);
452
* called if tab is pressed
457
Tab (Widget w, XEvent *event, String *params, Cardinal *num_params)
460
fprintf (stderr, "%s: Tab_action()\n", __FILE__);
462
(*xwToggleClassRec.base_class.tab_action)(w, event, params, num_params);
467
Focus (Widget w, XEvent *event, String *params, Cardinal *num_params)
470
fprintf (stderr, "%s: Focus()\n", __FILE__);
472
(*xwToggleClassRec.base_class.focus_action)(w, event, params, num_params);
477
GetFocus (Widget w, XEvent *event, String *params, Cardinal *num_params)
480
fprintf (stderr, "%s: GetFocus()\n", __FILE__);
482
(*xwToggleClassRec.base_class.getfocus_action)(w,event, params, num_params);
488
DrawToggle (Widget w)
490
XwToggleWidget tw = (XwToggleWidget) w;
493
printf ("%s: DrawToggle(%s)\n", __FILE__, w->core.name);
495
if (!XtIsRealized(w))
497
(*xwToggleClassRec.button_class.draw_button)(w);
498
if (!tw->base.shadow_width) {
499
if (tw->toggle.state) {
501
gc = tw->label.textGC;
503
gc = tw->button.bgGC;
505
XDrawRectangle (XtDisplay(w), XtWindow(w), gc, 0, 0,
506
tw->core.width-1, tw->core.height-1);
518
XwToggleWidget tw = (XwToggleWidget) w;
519
return ((Boolean)tw->toggle.state);