home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d3xx / d386 / xlispstat.lha / XLispStat / src3.lzh / UNIX / X11toggle.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-30  |  5.2 KB  |  175 lines

  1. /* X11toggle - toggle items for X11 dialogs                            */
  2. /* XLISP-STAT 2.1 Copyright (c) 1990, by Luke Tierney                  */
  3. /* Additions to Xlisp 2.1, Copyright (c) 1989 by David Michael Betz    */
  4. /* You may give out copies of this software; for conditions see the    */
  5. /* file COPYING included with this distribution.                       */
  6.  
  7. /***********************************************************************/
  8. /**                                                                   **/
  9. /**                    General Includes and Definitions               **/
  10. /**                                                                   **/
  11. /***********************************************************************/
  12.  
  13. #include <X11/Xlib.h>
  14. #include <X11/Xutil.h>
  15. #include <X11/Xos.h>
  16.  
  17. #include "dialogs.h"
  18.  
  19. extern Display *StX11Display();
  20. extern Point DialogStringSize();
  21. extern LVAL StX11ItemObject();
  22. extern char *checkstring();
  23.  
  24. typedef struct {
  25.   unsigned long fore, back;
  26. } ColorPair;
  27.  
  28. /* layout defines */
  29. # define TOGGLE_PAD 20
  30. # define TOGGLE_MARK_HEIGHT 16
  31.  
  32. /***********************************************************************/
  33. /**                                                                   **/
  34. /**                        Global Variables                           **/
  35. /**                                                                   **/
  36. /***********************************************************************/
  37.  
  38. /* configuration parameters - should be set using the defaults database */
  39. extern XFontStruct *DialogFont;
  40. extern unsigned long DialogBorderColor;
  41. extern ColorPair DialogC;
  42. extern unsigned int dialog_border_width;
  43. extern int min_toggle_height;
  44.  
  45. extern GC DialogGC, DialogRGC;
  46.  
  47. extern XContext ObjectContext;
  48.  
  49. extern Pixmap ToggleOffPM, ToggleOnPM;
  50.  
  51. /***********************************************************************/
  52. /**                                                                   **/
  53. /**                         Toggle Items                              **/
  54. /**                                                                   **/
  55. /***********************************************************************/
  56.  
  57. static draw_toggle(dpy, win, item)
  58.      Display *dpy;
  59.      Window win;
  60.      LVAL item;
  61. {
  62.   char *text;
  63.   int x, y, len, ascent, on;
  64.   Pixmap mark;
  65.  
  66.   ascent = DialogFont->max_bounds.ascent;
  67.  
  68.   text = checkstring(slot_value(item, s_text));
  69.   on = (slot_value(item, s_value) != NIL) ? TRUE : FALSE;
  70.   mark = (on) ? ToggleOnPM : ToggleOffPM;
  71.   XCopyPlane(dpy, mark, win, DialogGC, 
  72.          0, 0, TOGGLE_MARK_HEIGHT, TOGGLE_MARK_HEIGHT,
  73.          0, max(0, ascent - TOGGLE_MARK_HEIGHT), 1);
  74.   x = TOGGLE_PAD;
  75.   y = max(TOGGLE_MARK_HEIGHT, ascent);
  76.   len = strlen(text);
  77.   XDrawString(dpy, win, DialogGC, x, y, text, len);
  78. }
  79.   
  80. static LVAL toggle_handler(report, modal)
  81.      XEvent report;
  82.      int modal;
  83. {
  84.   Display *dpy = StX11Display();
  85.   Window win;
  86.   LVAL item;
  87.   LVAL result = NIL;
  88.  
  89.   win = report.xany.window;
  90.   item = StX11ItemObject(dpy, win);
  91.   if (item != NIL) {
  92.     switch (report.type) {
  93.     case Expose:
  94.       draw_toggle(dpy, win, item);
  95.       break;
  96.     case ButtonPress:
  97.       set_slot_value(item, s_value, 
  98.              (slot_value(item, s_value) != NIL) ? NIL : s_true);
  99.       draw_toggle(dpy, win, item);
  100.       if (! modal) send_message(item, sk_do_action);
  101.       break;
  102.     case ButtonRelease:
  103.       break;
  104.     default: 
  105.       break;
  106.     }
  107.   }
  108.   return(result);
  109. }
  110.  
  111. InstallToggleItem(win, item)
  112.      Window win;
  113.      LVAL item;
  114. {
  115.   Display *dpy = StX11Display();
  116.   Point loc, size;
  117.   Window toggle;
  118.   LVAL s_window_id = xlenter("WINDOW-ID");
  119.  
  120.   loc = ListToPoint(slot_value(item, s_location));
  121.   size = ListToPoint(slot_value(item, s_size));
  122.   toggle = XCreateSimpleWindow(dpy, win, loc.h, loc.v, size.h, size.v,
  123.                    0, DialogBorderColor, DialogC.back);
  124.   XSelectInput(dpy, toggle, 
  125.            ExposureMask | ButtonPressMask | ButtonReleaseMask);
  126.  
  127.   set_slot_value(item, s_window_id, cvfixnum((FIXTYPE) toggle));
  128.  
  129.   install_dialog_item_handler(dpy, toggle, toggle_handler, item);
  130.   if (XSaveContext(dpy, toggle, ObjectContext, (XContext) item) != 0)
  131.     xlfail("could not install object in window");
  132. }
  133.  
  134. DeleteToggleItem(win, item)
  135.      Window win;
  136.      LVAL item;
  137. {
  138.   Display *dpy = StX11Display();
  139.   Window toggle;
  140.   LVAL s_window_id = xlenter("WINDOW-ID");
  141.  
  142.   toggle = (Window) getfixnum(slot_value(item, s_window_id));
  143.  
  144.   delete_dialog_item_handler(dpy, toggle);
  145.   if (XDeleteContext(dpy, toggle, ObjectContext) != 0)
  146.     xlfail("cound not delete object context");
  147.   set_slot_value(item, s_window_id, NIL);
  148. }
  149.  
  150. DialogToggleGetDefaultSize(item, width, height)
  151.      LVAL item;
  152.      int *width, *height;
  153. {
  154.   Point sz;
  155.   sz = DialogStringSize(checkstring(slot_value(item, s_text)));
  156.   if (width != nil) *width = sz.h + TOGGLE_PAD;
  157.   if (height != nil) *height = max(sz.v, min_toggle_height);
  158. }
  159.  
  160. LVAL DialogToggleItemValue(item, set, value)
  161.      LVAL item, value;
  162.      int set;
  163. {
  164.   Display *dpy = StX11Display();
  165.   LVAL win_id;
  166.   LVAL s_window_id = xlenter("WINDOW-ID");
  167.  
  168.   if (set) {
  169.     set_slot_value(item, s_value, (value != NIL) ? s_true : NIL);
  170.     win_id = slot_value(item, s_window_id);
  171.     if (fixp(win_id)) draw_toggle(dpy, (Window) getfixnum(win_id), item);
  172.   }
  173.   return(slot_value(item, s_value));
  174. }
  175.