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

  1. /* X11choice - choice 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 CHOICE_PAD 20
  30. # define CHOICE_MARK_HEIGHT 16
  31. # define CHOICE_LEAD 5
  32.  
  33. /***********************************************************************/
  34. /**                                                                   **/
  35. /**                        Global Variables                           **/
  36. /**                                                                   **/
  37. /***********************************************************************/
  38.  
  39. /* configuration parameters - should be set using the defaults database */
  40. extern XFontStruct *DialogFont;
  41. extern unsigned long DialogBorderColor;
  42. extern ColorPair DialogC;
  43. extern unsigned int dialog_border_width;
  44. extern int min_choice_height;
  45.  
  46. extern GC DialogGC, DialogRGC;
  47.  
  48. extern XContext ObjectContext;
  49.  
  50. extern Pixmap ChoiceOffPM, ChoiceOnPM;
  51.  
  52. /***********************************************************************/
  53. /**                                                                   **/
  54. /**                         Choice Items                              **/
  55. /**                                                                   **/
  56. /***********************************************************************/
  57.  
  58. static draw_choice(dpy, win, item)
  59.      Display *dpy;
  60.      Window win;
  61.      LVAL item;
  62. {
  63.   LVAL text, val;
  64.   char *text_item;
  65.   int x, y, len, ascent, descent, value, i, step;
  66.   Pixmap mark;
  67.  
  68.   ascent = DialogFont->max_bounds.ascent;
  69.   descent = DialogFont->max_bounds.descent;
  70.  
  71.   text = slot_value(item, s_text);
  72.   val = slot_value(item, s_value);
  73.   value = (fixp(val)) ? getfixnum(val) : 0;
  74.   x = CHOICE_PAD;
  75.   step = descent + max(CHOICE_MARK_HEIGHT, ascent) + CHOICE_LEAD;
  76.  
  77.   for (i = 0, y = max(ascent, CHOICE_MARK_HEIGHT);
  78.        consp(text);
  79.        i++, y += step, text = cdr(text)) {
  80.     mark = (value == i) ? ChoiceOnPM : ChoiceOffPM;
  81.     text_item = checkstring(car(text));
  82.     XCopyPlane(dpy, mark, win, DialogGC, 
  83.            0, 0, CHOICE_MARK_HEIGHT, CHOICE_MARK_HEIGHT,
  84.            0, y - CHOICE_MARK_HEIGHT, 1);
  85.     len = strlen(text_item);
  86.     XDrawString(dpy, win, DialogGC, x, y, text_item, len);
  87.   }
  88. }
  89.   
  90. static LVAL choice_handler(report, modal)
  91.      XEvent report;
  92.      int modal;
  93. {
  94.   Display *dpy = StX11Display();
  95.   Window win;
  96.   LVAL item, result = NIL;
  97.   int i, item_height, ascent, descent;
  98.  
  99.   ascent = DialogFont->max_bounds.ascent;
  100.   descent = DialogFont->max_bounds.descent;
  101.  
  102.   win = report.xany.window;
  103.   item = StX11ItemObject(dpy, win);
  104.   if (item != NIL) {
  105.     switch (report.type) {
  106.     case Expose:
  107.       draw_choice(dpy, win, item);
  108.       break;
  109.     case ButtonPress:
  110.       item_height = descent + max(CHOICE_MARK_HEIGHT, ascent) + CHOICE_LEAD;
  111.       i = report.xbutton.y / item_height;
  112.       DialogChoiceItemValue(item, TRUE, i);
  113.       if (! modal) send_message(item, sk_do_action);
  114.       break;
  115.     case ButtonRelease:
  116.       break;
  117.     default: 
  118.       break;
  119.     }
  120.   }
  121.   return(result);
  122. }
  123.  
  124. InstallChoiceItem(win, item)
  125.      Window win;
  126.      LVAL item;
  127. {
  128.   Display *dpy = StX11Display();
  129.   Point loc, size;
  130.   Window choice;
  131.   LVAL s_window_id = xlenter("WINDOW-ID");
  132.  
  133.   loc = ListToPoint(slot_value(item, s_location));
  134.   size = ListToPoint(slot_value(item, s_size));
  135.   choice = XCreateSimpleWindow(dpy, win, loc.h, loc.v, size.h, size.v,
  136.                    0, DialogBorderColor, DialogC.back);
  137.   XSelectInput(dpy, choice, 
  138.            ExposureMask | ButtonPressMask | ButtonReleaseMask);
  139.  
  140.   set_slot_value(item, s_window_id, cvfixnum((FIXTYPE) choice));
  141.   if (! fixp(slot_value(item, s_value)))
  142.     set_slot_value(item, s_value, cvfixnum((FIXTYPE) 0));
  143.  
  144.   install_dialog_item_handler(dpy, choice, choice_handler, item);
  145.   if (XSaveContext(dpy, choice, ObjectContext, (XContext) item) != 0)
  146.     xlfail("could not install object in window");
  147. }
  148.  
  149. DeleteChoiceItem(win, item)
  150.      Window win;
  151.      LVAL item;
  152. {
  153.   Display *dpy = StX11Display();
  154.   Window choice;
  155.   LVAL s_window_id = xlenter("WINDOW-ID");
  156.  
  157.   choice = (Window) getfixnum(slot_value(item, s_window_id));
  158.  
  159.   delete_dialog_item_handler(dpy, choice);
  160.   if (XDeleteContext(dpy, choice, ObjectContext) != 0)
  161.     xlfail("cound not delete object context");
  162.   set_slot_value(item, s_window_id, NIL);
  163. }
  164.  
  165. DialogChoiceGetDefaultSize(item, width, height)
  166.      LVAL item;
  167.      int *width, *height;
  168. {
  169.   Point i_sz, s_sz;
  170.   LVAL text = slot_value(item, s_text);
  171.  
  172.   for (i_sz.h = 0, i_sz.v = 0; consp(text); text = cdr(text)) {
  173.     s_sz = DialogStringSize(getstring(car(text)));
  174.     if (i_sz.v != 0) i_sz.v += CHOICE_LEAD; /* only add lead between lines */
  175.     i_sz.v += max(s_sz.v, min_choice_height);
  176.     i_sz.h = max(i_sz.h, s_sz.h);
  177.   }
  178.   if (width != nil) *width = i_sz.h + CHOICE_PAD;
  179.   if (height != nil) *height = i_sz.v;
  180. }
  181.  
  182. LVAL DialogChoiceItemValue(item, set, value)
  183.      LVAL item;
  184.      int set, value;
  185. {
  186.   Display *dpy = StX11Display();
  187.   LVAL win_id, text;
  188.   LVAL s_window_id = xlenter("WINDOW-ID");
  189.   int n;
  190.  
  191.   if (set) {
  192.     text = slot_value(item, s_text);
  193.     n = (consp(text)) ? llength(text) : 0;
  194.     value = max(0, min(value, n - 1));
  195.     set_slot_value(item, s_value, cvfixnum((FIXTYPE) value));
  196.     win_id = slot_value(item, s_window_id);
  197.     if (fixp(win_id)) draw_choice(dpy, (Window) getfixnum(win_id), item);
  198.   }
  199.   return(slot_value(item, s_value));
  200. }
  201.