File: X11buttons.c

package info (click to toggle)
xlispstat 3.52.14-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,560 kB
  • ctags: 12,676
  • sloc: ansic: 91,357; lisp: 21,759; sh: 1,525; makefile: 521; csh: 1
file content (512 lines) | stat: -rw-r--r-- 13,882 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/* X11buttons - buttons for X11 dialogs and windows                    */
/* XLISP-STAT 2.1 Copyright (c) 1990, by Luke Tierney                  */
/* Additions to Xlisp 2.1, Copyright (c) 1989 by David Michael Betz    */
/* You may give out copies of this software; for conditions see the    */
/* file COPYING included with this distribution.                       */
 
/***********************************************************************/
/**                                                                   **/
/**                    General Includes and Definitions               **/
/**                                                                   **/
/***********************************************************************/

#include "dialogs.h"

extern Display *StX11Display();
extern Point DialogStringSize();
extern LVAL StX11ItemObject();
extern char *checkstring();
extern LVAL s_window_id;

#define NullWindow ((Window) 0)

typedef struct {
  unsigned long fore, back;
} ColorPair;

/* layout defines */
# define BUTTON_PAD 15
# define BUTTON_LEAD 5

# define CloseButton 1
# define MenuButton 2

# define CLOSE_TEXT "Close"
# define MENU_TEXT "Menu"

/* forward declarations */
LOCAL LVAL special_button_object _((Display *dpy, Window button));
LOCAL VOID draw_button _((Display *dpy, Window win, LVAL item, int reversed));
LOCAL VOID draw_special_button _((Display *dpy, Window win,
				  int type, int reversed));
LOCAL VOID delete_special_button _((Window win, int type));

/***********************************************************************/
/**                                                                   **/
/**                        Global Variables                           **/
/**                                                                   **/
/***********************************************************************/

/* configuration parameters - should be set using the defaults database */
extern XFontStruct *DialogFont;
extern unsigned long DialogBorderColor, ButtonBorderColor;
extern ColorPair DialogC, ButtonC;
extern unsigned int dialog_border_width, button_border_width;
extern int min_button_height, min_button_width, dialog_item_gap;

extern GC DialogGC, DialogRGC;

extern XContext EventContext, ObjectContext, CloseContext, MenuContext;

extern LVAL s_menu, sk_select;

/***********************************************************************/
/**                                                                   **/
/**                         Button Items                              **/
/**                                                                   **/
/***********************************************************************/

static LVAL track_button(dpy, win, item, modal)
     Display *dpy;
     Window win;
     LVAL item;
     int modal;
{
  int done = FALSE;
  LVAL result = NIL;
  XEvent report;

  draw_button(dpy, win, item, TRUE);

  while (! done) {
    XNextEvent(dpy, &report);
    switch (report.type) {
    case ButtonRelease:
      done = TRUE;
      result = item;
      break;
    case LeaveNotify:
      done = TRUE;
      if (report.xcrossing.window == win)
	XSetWindowBorderWidth(dpy, win, button_border_width);
      break;
    default:
      break;
    }
  }
  draw_button(dpy, win, item, FALSE);
      
  if (! modal && result != NIL) send_message(item, sk_do_action);

  return(result);
}

LOCAL VOID draw_button(dpy, win, item, reversed)
     Display *dpy;
     Window win;
     LVAL item;
     int reversed;
{
  Point ssz, bsz;
  char *text;
  int x, y, len;
  GC gc;
  unsigned long color;

  gc = (reversed) ? DialogRGC : DialogGC;
  color = (reversed) ? DialogC.fore : DialogC.back;

  XSetWindowBackground(dpy, win, color);
  XClearWindow(dpy, win);
  text = checkstring(slot_value(item, s_text));
  ssz = DialogStringSize(text);
  bsz = ListToPoint(slot_value(item, s_size));
  x = (bsz.h - ssz.h) / 2;
  y = (bsz.v - ssz.v) / 2 + DialogFont->max_bounds.ascent;
  len = strlen(text);
  XDrawString(dpy, win, gc, x, y, text, len);
  XSetWindowBackground(dpy, win, DialogC.back);
}
  
static LVAL button_handler(report, modal)
     XEvent report;
     int modal;
{
  Display *dpy = StX11Display();
  Window win;
  LVAL item;
  LVAL result = NIL;

  win = report.xany.window;
  item = StX11ItemObject(dpy, win);
  if (item != NIL) {
    switch (report.type) {
    case Expose:
      draw_button(dpy, win, item, FALSE);
      break;
    case ButtonPress:
      result = track_button(dpy, win, item, modal);
      break;
    case ButtonRelease:
      break;
    case EnterNotify:
      XSetWindowBorderWidth(dpy,report.xcrossing.window, 
			    button_border_width + 1);
      break;
    case LeaveNotify:
      XSetWindowBorderWidth(dpy, report.xcrossing.window, button_border_width);
      break;
    default: 
      break;
    }
  }
  return(result);
}

VOID InstallButtonItem(win, item)
     Window win;
     LVAL item;
{
  Display *dpy = StX11Display();
  Point loc, size;
  Window button;

  loc = ListToPoint(slot_value(item, s_location));
  size = ListToPoint(slot_value(item, s_size));
  button = XCreateSimpleWindow(dpy, win, loc.h, loc.v, size.h, size.v,
			       button_border_width,
                               ButtonBorderColor, ButtonC.back);
  XSelectInput(dpy, button, 
	       StructureNotifyMask | ExposureMask | EnterWindowMask |
               LeaveWindowMask | ButtonPressMask | ButtonReleaseMask);

  set_slot_value(item, s_window_id, cvfixnum((FIXTYPE) button));

  install_dialog_item_handler(dpy, button, button_handler, item);
  if (XSaveContext(dpy, button, ObjectContext, (caddr_t) item) != 0)
    xlfail("could not install object in window");
}

VOID DeleteButtonItem(win, item)
     Window win;
     LVAL item;
{
  Display *dpy = StX11Display();
  Window button;

  button = (Window) getfixnum(slot_value(item, s_window_id));

  delete_dialog_item_handler(dpy, button);
  if (XDeleteContext(dpy, button, ObjectContext) != 0)
    xlfail("could not delete object context");
  set_slot_value(item, s_window_id, NIL);
}

VOID DialogButtonGetDefaultSize(item, width, height)
     LVAL item;
     int *width, *height;
{
  Point sz;

  sz = DialogStringSize(checkstring(slot_value(item, s_text)));
  if (width != NULL) *width = max(sz.h + BUTTON_PAD, min_button_width);
  if (height != NULL) *height = max(sz.v + BUTTON_LEAD, min_button_height);
}

/***********************************************************************/
/**                                                                   **/
/**                          Close Buttons                            **/
/**                                                                   **/
/***********************************************************************/

int ClosePanelHeight()
{
  Point sz;

  sz = DialogStringSize(CLOSE_TEXT);
  return (sz.v + 4 * BUTTON_LEAD);
}

LOCAL LVAL special_button_object(dpy, button)
     Display *dpy;
     Window button;
{
  LVAL object;

  if (XFindContext(dpy, button, ObjectContext, (caddr_t *) &object) == 0 
      && objectp(object))
    return(object);
  else return(NIL);
}

static Point special_button_size(type)
     int type;
{
  char *text = NULL;
  Point bsz, ssz;

  switch (type) {
  case CloseButton: text = CLOSE_TEXT; break;
  case MenuButton:  text = MENU_TEXT;  break;
  default: break;
  }

  ssz = DialogStringSize(text);
  bsz.h = max(ssz.h + BUTTON_PAD, min_button_width);
  bsz.v = max(ssz.v + BUTTON_LEAD, min_button_height);
  return(bsz);
}

static VOID track_special_button(dpy, win, type, modal)
     Display *dpy;
     Window win;
     int type, modal;
{
  int done = FALSE, result = FALSE;
  XEvent report;
  LVAL object;

  draw_special_button(dpy, win, type, TRUE);

  while (! done) {
    XNextEvent(dpy, &report);
    switch (report.type) {
    case ButtonRelease:
      done = TRUE;
      result = TRUE;
      break;
    case LeaveNotify:
      done = TRUE;
      if (report.xcrossing.window == win)
	XSetWindowBorderWidth(dpy, win, button_border_width);
      break;
    default:
      break;
    }
  }
  draw_special_button(dpy, win, type, FALSE);

  if (! modal && result == TRUE) {
    switch (type) {
    case CloseButton: 
      object = special_button_object(dpy, win);
      if (object != NIL) send_message(object, sk_close);
      break;
    default:
      break;
    }
  }
}

LOCAL VOID draw_special_button(dpy, win, type, reversed)
     Display *dpy;
     Window win;
     int type, reversed;
{
  Point ssz, bsz;
  char *text = NULL;
  int x, y, len;
  GC gc;
  unsigned long color;

  switch (type) {
  case CloseButton: text = CLOSE_TEXT; break;
  case MenuButton:  text = MENU_TEXT; break;
  default: break;
  }

  gc = (reversed) ? DialogRGC : DialogGC;
  color = (reversed) ? DialogC.fore : DialogC.back;

  XSetWindowBackground(dpy, win, color);
  XClearWindow(dpy, win);
  ssz = DialogStringSize(text);
  bsz = special_button_size(type);
  x = (bsz.h - ssz.h) / 2;
  y = (bsz.v - ssz.v) / 2 + DialogFont->max_bounds.ascent;
  len = strlen(text);
  XDrawString(dpy, win, gc, x, y, text, len);
  XSetWindowBackground(dpy, win, DialogC.back);
}
  
static LVAL basic_special_button_handler(report, modal, type)
     XEvent report;
     int modal, type;
{
  Display *dpy = StX11Display();
  int screen = StX11Screen();
  Window win, child;
  int x, y, item;
  LVAL object, menu;

  if (modal) return(NIL);

  win = report.xany.window;
  object = special_button_object(dpy, win);
  if (objectp(object) && GETWINDOWADDRESS(object) != NullWindow) {
    switch (report.type) {
    case Expose:
      draw_special_button(dpy, win, type, FALSE);
      break;
    case ButtonPress:
      switch(type) {
      case MenuButton:
	XTranslateCoordinates(dpy, win, RootWindow(dpy, screen),
			      report.xbutton.x, report.xbutton.y, &x, &y,
			      &child);
	menu = slot_value(object, s_menu);
	if (menu_p(menu)) {
	  draw_special_button(dpy, win, type, TRUE);
	  XSetWindowBorderWidth(dpy, report.xcrossing.window, 
				button_border_width);
	  item = StMObPopup(menu, x, y, NIL);
	  draw_special_button(dpy, win, type, FALSE);
	  if (item > 0) send_message1(menu, sk_select, item);
	}
	else 
	  XSetWindowBorderWidth(dpy, report.xcrossing.window, 
				button_border_width);
	break;
      default:
	track_special_button(dpy, win, type, modal);
	break;
      }
      break;
    case ButtonRelease:
      break;
    case EnterNotify:
      XSetWindowBorderWidth(dpy,report.xcrossing.window, 
			    button_border_width + 1);
      break;
    case LeaveNotify:
      XSetWindowBorderWidth(dpy, report.xcrossing.window, button_border_width);
      break;
    default: 
      break;
    }
  }
  return(NIL);
}

static LVAL close_button_handler(report, modal)
     XEvent report;
     int modal;
{
  return(basic_special_button_handler(report, modal, CloseButton));
}

static LVAL menu_button_handler(report, modal)
     XEvent report;
     int modal;
{
  return(basic_special_button_handler(report, modal, MenuButton));
}

static VOID install_special_button(win, object, type)
     Window win;
     LVAL object;
     int type;
{
  Display *dpy = StX11Display();
  Point loc, size;
  Window button;
  LVAL (*handler)() = NULL;
  int width, height, gravity = 0;
  XSetWindowAttributes winattr;

  switch (type) {
  case CloseButton:
    loc.h = BUTTON_LEAD;
    loc.v = BUTTON_LEAD;
    size = special_button_size(type);
    handler = close_button_handler;
    gravity = NorthWestGravity;
    break;
  case MenuButton:
    size = special_button_size(type);
    loc.v = BUTTON_LEAD;
    StWGetSize(win, &width, &height, TRUE);
    loc.h = width - size.h - BUTTON_LEAD;
    handler = menu_button_handler;
    gravity = NorthEastGravity;
  default:
    break;
  }

  button = XCreateSimpleWindow(dpy, win, loc.h, loc.v, size.h, size.v,
			       button_border_width,
                               ButtonBorderColor, ButtonC.back);
  XSelectInput(dpy, button, 
	       StructureNotifyMask | ExposureMask | EnterWindowMask |
               LeaveWindowMask | ButtonPressMask | ButtonReleaseMask);

  winattr.win_gravity = gravity;
  XChangeWindowAttributes(dpy, button, CWWinGravity, &winattr);

  switch (type) {
  case CloseButton:
    if (XSaveContext(dpy, win, CloseContext, (caddr_t) button) != 0)
      xlfail("could not install close button context");
    break;
  case MenuButton:
    if (XSaveContext(dpy, win, MenuContext, (caddr_t) button) != 0)
      xlfail("could not install menu button context");
    break;
  default:
    break;
  }
  if (XSaveContext(dpy, button, EventContext, (caddr_t) handler) != 0)
    xlfail("could not install event handler");
  if (XSaveContext(dpy, button, ObjectContext, (caddr_t) object) != 0)
    xlfail("could not install object in window");
}

LOCAL VOID delete_special_button(win, type)
     Window win;
     int type;
{
  Display *dpy = StX11Display();
  Window button;
  XContext context = (XContext) 0;

  switch (type) {
  case CloseButton: context = CloseContext; break;
  case MenuButton:  context = MenuContext;  break;
  default: break;
  }

  if (XFindContext(dpy, win, context, (caddr_t *) &button) == 0) {
    if (XDeleteContext(dpy, win, context) != 0)
      xlfail("could not delete buttont context");
    if (XDeleteContext(dpy, button, EventContext) != 0)
      xlfail("could not delete event context");
    if (XDeleteContext(dpy, button, ObjectContext) != 0)
      xlfail("could not delete object context");
  }
}

VOID InstallCloseButton(win, object)
     Window win;
     LVAL object;
{
  install_special_button(win, object, CloseButton);
}

VOID DeleteCloseButton(win)
     Window win;
{
  delete_special_button(win, CloseButton);
}

VOID InstallMenuButton(win, object)
     Window win;
     LVAL object;
{
  install_special_button(win, object, MenuButton);
}

VOID DeleteMenuButton(win)
     Window win;
{
  delete_special_button(win, MenuButton);
}