Debian Codegraph @

wdoc/menus.html

raw
1 <HTML>
2
3 <HEAD>
4 <TITLE> ACEDB source code documentation: a new menu system </TITLE>
5
6 <LINK rel=stylesheet type="text/css"
7 href="./wdoc.css"
8 title="Style sheet for wdoc developer documents">
9
10 </HEAD>
11
12 <BODY>
13
14 <H1>A New menu system</H1>
15
16 <H2><FONT color=red>*** THIS IS A SPEC, CODE ONLY EXISTS AS A PROTOTYPE ***</FONT></H1>
17
18 <PRE>
19
20 Proposal for new menu system, based on Montpellier discussions and
21 Cyrus and Frank's letter. For discussion.
22
23 /*******************/
24
25 #define OPTION_DISABLED 0x01
26 #define OPTION_TOGGLE 0x02
27 #define OPTION_TOGGLE_STATE 0x04
28 #define OPTION_RADIO_SET 0x08
29 #define OPTION_RADIO_STATE 0x10
30
31 typedef struct
32 { char *label ;
33 void (*callBack)(OPTION *) ; /* gets called with entire option structure */
34 char *callMessage ; /* loose linking - see below */
35 unsigned int flag ; /* values from OPTION_* */
36 int value ; /* for simple specialisation */
37 void *ptr ; /* for more complex specialisation */
38 MENUOPT *submenu ;
39 /* other stuff I am less sure about - see below */
40 void *icon ;
41 int forecolor, backcolor ; /* for buttons */
42 char override ; /* override character */
43 } OPTION ;
44
45 /********************/
46
47 We have 3 choices for the way to specify a menu from its options.
48 Either we use the first item as a header, with its label becoming the
49 title, so we get
50 graphBoxMenu (int box, OPTION *options)
51 or we add the title to the command line
52 graphBoxMenu (int box, OPTION *options, char *title)
53 which would not allow titles on walking submenus if we have them. But
54 I don't think you need them on the Mac?
55 or we have a new structure
56 typedef struct
57 { char *title ;
58 OPTION *options ;
59 } MENU ;
60 graphBoxMenu (int box, MENU *menu)
61 which is more of a pain to initialise, which is important (see below).
62
63 In all cases the options can be zero terminated.
64
65 /*******************/
66
67 The callMessage field allows use of some loose-linking call-by-name system,
68 e.g. but not restricted to Simon's new call() system.
69
70 On making a menu:
71 if opt->callBack, then a line/button is created as normal
72 else if callExists(opt->callMessage) then a line is created
73 else no line created -- the function is not linked in.
74
75 On a menu selection:
76 if (opt->callBack)
77 (*opt->callBack)(opt) ;
78 else
79 call (opt->callMessage, opt) ;
80
81 Spacer lines should be uniform, i.e. always use the same symbol
82 (could be different on Mac than Unix).
83 Similarly the mark for on/off should be standard on toggles, and also
84 a different standard one for radio options.
85 Maybe we should have two sorts of toggles, one for presence or absence of
86 something, with a check mark, and one flipping between two text states.
87 Or perhaps that is better handled by a two-item radio set.
88
89 ->submenu can be used for menus on buttons, and also perhaps for walking
90 menus. Not sure whether to allow those in our "style guide".
91
92 Perhaps even a single button should take a single *OPTION argument.
93
94 The value and ptr fields are required for making menus on the fly. In
95 that case the function must be fixed, and these fields will tell which
96 option was picked. void* ptr allows arbitrary information to be attached.
97
98 The callback will not include the box number. The user can get it from
99
100 int box = graphBoxAt (graphEventX, graphEventY, 0, 0) ;
101
102 This works now.
103
104 /*******************/
105
106 Comments on Cyrus/Frank scheme:
107
108 (1) we need to be able to statically define menus, e.g.
109
110 OPTION menuOpts[] = {
111 { "Quit", graphDestroy, 0, 0, 0, 0, 0, 0, 0},
112 { "Print", graphPrint, 0, 0, 0, 0, 0, 0, 0},
113 { "Assemble", 0, "miegAssemble", 0, 0, 0, 0, 0, 0},
114 { 0, 0, 0, 0, 0, 0, 0, 0, 0}} ;
115 MENU menu = { "Test", menuOpts} ;
116
117 In your linked-list scheme this would be not really possible.
118
119 (2) keyboard overrides seem to present some problems. I guess they
120 would not mean anything for boxmenus?, or would they apply to the
121 menu on the innermost box.
122
123 (3) menu bars as you have them really don't exist in X. You mention emacs,
124 but that is one window. If I had them, I would have to have them per
125 window, which would create a nightmare for you.
126
127 ==========================================================================
128 A quick response to your comments:
129
130 The programmer may not care about the box, but in some cases it is
131 very important. They care when the action selected in the menu
132 applies to the box over which the menu action took place (most of the
133 uses of graphBoxMenu). I proposed not specifying the box in the
134 generated return call. It is easy and probably better to eliminate
135 graphEventX/Y with a convenience call:
136 int box = graphEventBox() ;
137 to give the box for the last event. In fact maybe we should also use
138 this for PICK events, to make things consistent. I agree one should
139 be wary of globals, but I think that in fact globals for the last
140 event coordinates are natural. Compare with the implicit "active
141 graph" global.
142
143 The "value" field is exactly to store a magic number. The "ptr" field
144 is completely flexible -- it lets you store anything you want, though
145 of course you must then allocate space for it. I think this caters
146 for what you want -- it was in fact missing in your original proposal,
147 and present in mine. Note that the called function always gets as
148 an argument a pointer to the ENTIRE structure, so you get value, ptr
149 etc. to use or ignore as you want.
150
151 Aha - I see the point about menuInitialise. It would allow us to add
152 extra fields in the future (e.g. icons), without changing the code
153 anywhere that doesn't have them. The downside is that to add or
154 access those fields requires extra calls. I guess we require labels
155 to be unique within a menu. I started off referring to items by
156 label, but for reasons later on with buttons I switched to introducing
157 a special handle for items.
158
159 This is what I have ended up with:
160
161 typedef struct
162 { char *label ;
163 void (*func)() ; /* NB can be 0 if using menuSetCall() below */
164 } MENUSPEC ;
165
166 typedef void* MENU ; /* public menu handle */
167 typedef void* MENUITEM ; /* public handle for menu items */
168
169 /* a menu is maintained internally as a header and a linked list of
170 items */
171
172 MENU menuInitialise (char *title, MENUSPEC *spec) ;
173 /* makes a simple menu from a spec terminated with label = 0 */
174 /* if called on same spec, give existing menu */
175 MENU menuCreate (char *title) ; /* makes a blank menu */
176 MENUITEM menuCreateItem (char *label, void (*func)()) ;
177 MENUITEM menuItem (MENU menu, char *label) ; /* find item from label */
178 BOOL menuAddItem (MENU menu, MENUITEM item, char *beforeLabel) ;
179 /* add an item; if before == 0 then add at end */
180 BOOL menuDeleteItem (MENU menu, char *label) ; /* also destroys item */
181 void menuDestroy (MENU menu) ; /* also destroys items */
182
183 The behaviour of menuInitialise when repeatedly called with the same
184 spec allows simple cases to be handled like now, e.g.
185 static MENUSPEC spec[] =
186 { "Quit", graphDestroy,
187 "Print", graphPrint,
188 0, 0
189 } ;
190 graphMenu (menuInitialise ("Toto", spec)) ;
191
192 It seems always simpler to use labels for deleting and the before
193 field of add, rather than items. We should definitely enforce that
194 labels must be unique within a menu. Note that the item can be
195 obtained transiently, e.g. typical use would be
196 menuAddItem (menu, menuCreateItem (label, func), 0) ;
197
198
199 To set extra information on an item:
200
201 BOOL menuSetCall (MENUITEM item, char *callFuncName) ;
202 BOOL menuSetFunc (MENUITEM item, void (*Func) ;
203 BOOL menuSetFlags (MENUITEM item, int flags) ;
204 BOOL menuUnsetFlags (MENUITEM item, int flags) ;
205 BOOL menuSetValue (MENUITEM item, int value) ;
206 BOOL menuSetPtr (MENUITEM item, void *ptr) ;
207 with perhaps menuSetIcon(), menuSetOverride() etc. as needed
208 The BOOL's all return success or failure. e.g. failure if label or
209 menu do not exist when they should. Again, it is easy to write
210 menuSetValue (menuItem (menu, "Frame 3"), 3) ;
211
212 Now what happens when an item is picked? We get called back with
213 func (MENUITEM item)
214 and can get the other information with
215 int menuGetFlags (MENUITEM item)
216 etc.
217
218 We have a rule that if the function for a menu item is zero and it doesn't
219 have a Call routine set which exists then the item becomes invisible.
220 This makes is easy to have options which only appear when the relevant
221 code has been linked in.
222
223 I still want graphBoxMenu (MENU menu, int box) to register the menu. I
224 need to know where the menu's domain of action is. For popup menus
225 you need to know where on the screen it can pop up. That is a box in
226 the graph library. We'll keep graphMenu (MENU menu) as a #define to
227 graphBoxMenu (menu, 0) ;
228
229 What about buttons? I like the idea of them using the same structure.
230 For a single button we could have
231 int graphButton (MENUITEM item, float x, float y) ;
232 allowing for simple cases the single call:
233 graphButton (menuCreateItem (label, func), x, y) ;
234 For more complex ones you would explicitly create the item and
235 decorate it. For a row of buttons, including for example radio button
236 features, one would want
237 int graphButtons (MENU menu, float x0, float y0, float xmax) ;
238 and to add a menu to a button, you would use
239 BOOL menuSetMenu (MENUITEM item, MENU menu) ;
240 I guess graphMenu() on the box would still work, but would be strongly
241 frowned upon.
242
243 I like this so much that I am going to start coding it (the standalone
244 menu handling side of it). But that does not mean it is fixed -
245 please feel free to comment. In most cases, as you say, this is an
246 overhead of at most one call (menuInitialise) above what we do now.
247
248 Note that all our horrible menu-adjusting stuff used for the main menu
249 becomes MUCH easier. Now you just say
250 menuAddItem (menu, menuCreateItem ("Save", saveFunc), "Write Access") ;
251 menuDeleteItem (menu, "Write Access") ;
252 to toggle them.
253
254
255 Altenatively, using the zero functions are invisible rule, we can create
256 a menu using menuInitialise which had both Write Access and Save, and do
257
258 menuSetFunc(menuItem(menu,"Save"), saveFunc);
259 menuSetFunc(menuitem(menu,"Write Access", 0);
260
261 There are some technical implementation problems: I have, and want to
262 keep, cached X menu structures. This is for two reasons: first I am
263 always using the same menu for new versions of the same display, and
264 worse I often have the same menu attached to thousands of boxes even
265 in one display. But what is "same" in this case? It can't be just
266 the header, because that stays the same when I add and delete items,
267 nor even the header and list of labels. Ah, maybe it should be the
268 header, and I should do the following: when I call graphMenu() I
269 create a copy of the state in the menu. Then when called again on the
270 same header, I can check if there are any changes since last time, and
271 if so bring the X menu up to date. There is also an issue of how to
272 update buttons. I guess toggles and radio sets will maintain
273 themselves.
274
275 I guess for these reasons, and also for the user who wants to
276 simultaneously show variant menus, I need
277
278 MENU menuCopy (MENU menu) ;
279
280 </PRE>
281
282 </BODY>
283 </HTML>