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