dashlist.c revision e53c48bf
1/* 2** dashlist.c 3** 4** How to make a widget to choose a dashlist. 5** 6** NOTE: This file uses static variables. Therefore, trying to use these 7** functions to create more than one of these dashlist choice things 8** will fail in a big way. 9*/ 10 11#include <stdio.h> 12#include <X11/Xos.h> 13#include <stdlib.h> 14#include <X11/Intrinsic.h> 15#include <X11/StringDefs.h> 16#include <X11/Xaw/Form.h> 17#include <X11/Xaw/Label.h> 18#include <X11/Xaw/Toggle.h> 19#include "xgc.h" 20 21static void change_dashlist(Widget, caddr_t, caddr_t); 22 23static short dashlist = 240; /* in binary, becomes the dashlist 24 (240 = XXXX____) */ 25static Widget *dashes; /* the toggle widgets */ 26 27/* create_dashlist_choice(w) 28** ------------------------- 29** Inside w (a form widget), creates a bunch of little toggle buttons 30** in a row, representing the dash list. There's also a label so 31** the user knows what it is. 32*/ 33 34void 35create_dashlist_choice(Widget w) 36{ 37 /* callback list for the toggle widgets */ 38 static XtCallbackRec callbacklist[] = { 39 {(XtCallbackProc) change_dashlist, NULL}, 40 {NULL, NULL} 41 }; 42 43 /* ArgList for the label */ 44 static Arg labelargs[] = { 45 {XtNborderWidth, (XtArgVal) 0}, 46 {XtNjustify, (XtArgVal) XtJustifyRight}, 47 {XtNvertDistance, (XtArgVal) 4} 48 }; 49 50 /* ArgList for the toggles */ 51 static Arg dashargs[] = { 52 {XtNcallback, (XtArgVal) NULL}, 53 {XtNhorizDistance, (XtArgVal) NULL}, 54 {XtNfromHoriz, (XtArgVal) NULL}, 55 {XtNwidth, (XtArgVal) 10}, 56 {XtNheight, (XtArgVal) 10}, 57 {XtNhighlightThickness, (XtArgVal) 1}, 58 {XtNstate, (XtArgVal) False}, 59 {XtNlabel, (XtArgVal) ""} 60 }; 61 62 static Widget label; /* the label, of course */ 63 static int *dashinfo; /* contains integers saying which bit 64 a particular button is; sent to 65 change_dashlist to tell it which 66 bit got changed */ 67 int i; /* counter */ 68 69 char name[11]; 70 71 /* allocate space for stuff that we don't know the size of yet */ 72 dashes = (Widget *) malloc(DASHLENGTH * sizeof(Widget)); 73 dashinfo = (int *) malloc(DASHLENGTH * sizeof(int)); 74 75 /* make the label widget */ 76 label = XtCreateManagedWidget("dashlist",labelWidgetClass,w, 77 labelargs,XtNumber(labelargs)); 78 79 dashargs[0].value = (XtArgVal) callbacklist; 80 81 for (i=0;i<DASHLENGTH;++i) { /* go through all the buttons */ 82 if (i==0) { /* offset the first one from the label */ 83 dashargs[1].value = (XtArgVal) 10; 84 dashargs[2].value = (XtArgVal) label; 85 } 86 else { /* put it directly to the right of the 87 last one, no space in between */ 88 dashargs[1].value = (XtArgVal) -1; 89 dashargs[2].value = (XtArgVal) dashes[i-1]; 90 } 91 92 /* set its original state depending on the state of that 93 ** bit of the dashlist */ 94 95 if (dashlist&1<<i) 96 dashargs[6].value = (XtArgVal) True; 97 else 98 dashargs[6].value = (XtArgVal) False; 99 100 snprintf(name, sizeof name, "dashlist%d",i); 101 102 dashinfo[i] = i; /* which bit we're on; this is needed 103 in change_dashlist (the callback) */ 104 callbacklist[0].closure = (caddr_t) &dashinfo[i]; 105 106 dashes[i] = XtCreateManagedWidget(name,toggleWidgetClass,w, 107 dashargs,XtNumber(dashargs)); 108 } 109} 110 111/* change_dashlist(w,closure,call_data) 112** ------------------------------------ 113** This function is called when the user toggles a toggle widget. It 114** makes the appropriate change to the dashlist and sends it off 115** to interpret(). 116** Funny args are because it's a callback. 117*/ 118 119/*ARGSUSED*/ 120static void 121change_dashlist(Widget w, caddr_t closure, caddr_t call_data) 122{ 123 int num; /* what number button it is */ 124 Boolean on; /* is it currently on or off? */ 125 126 char buf[80]; /* string to send to interpret() */ 127 128 static Arg args[] = { 129 {XtNstate, (XtArgVal) NULL} 130 }; 131 132 /* set up ArgList so that 'on' will contain the state */ 133 args[0].value = (XtArgVal) &on; 134 135 num = * (int *) closure; /* we put it here back in the last function */ 136 XtGetValues(w,args,XtNumber(args)); 137 138 /* modify the dashlist as appropriate. */ 139 if (on) { 140 dashlist |= 1<<num; 141 } 142 else { 143 dashlist &= ~(1<<num); 144 } 145 146 /* now tell interpret() about it */ 147 snprintf(buf, sizeof buf, "dashlist %d\n",dashlist); 148 interpret(buf); 149} 150 151/* update_dashlist(newdash) 152** ------------------------ 153** Updates the display of the dashlist so that it corresponds to 154** newdash. 155*/ 156 157void 158update_dashlist(int newdash) 159{ 160 int i; /* counter */ 161 static Arg dashargs[] = { /* Arglist for setting toggle state */ 162 {XtNstate, (XtArgVal) NULL} 163 }; 164 165 /* first set the internal representation */ 166 dashlist = newdash; 167 168 for (i = 0; i < DASHLENGTH; ++i) { 169 if (newdash & 1<<i) /* if it's set, make it look that way */ 170 dashargs[0].value = (XtArgVal) True; 171 else 172 dashargs[0].value = (XtArgVal) False; 173 174 XtSetValues(dashes[i],dashargs,XtNumber(dashargs)); 175 } 176} 177