1 | Allo... (warning: this was written as I thought it up :) |
---|
2 | |
---|
3 | Ideas for implementing menus... |
---|
4 | |
---|
5 | We've got a data structure called "Menu_Item", which has (for starters): |
---|
6 | |
---|
7 | Title -- Text to display |
---|
8 | Child -- Sub-Menu to recurse into, if item is picked |
---|
9 | Exec Function -- Function to call if the item is picked |
---|
10 | |
---|
11 | An item must not have both a child and an exec function. |
---|
12 | |
---|
13 | The DoMenu(main_menu) handles all input and either calls ExecFunction, |
---|
14 | if the picked item has one; or recurses into the Child menu. The |
---|
15 | ExecFunction returns a value to specify whether the menus should go |
---|
16 | away, back up one level, or stay as-is. Also, a menu title (label) |
---|
17 | can be specified by giving neither a Child nor a Function. |
---|
18 | |
---|
19 | The up/down arrows, blinking, scrolling, etc, are handled by DoMenu(). |
---|
20 | |
---|
21 | This gives us the functionality of a standard pull-down menu. |
---|
22 | However, we need more functionality than that. We need checkboxes, |
---|
23 | sliders, and a way to move items up/down. |
---|
24 | |
---|
25 | So... |
---|
26 | |
---|
27 | The ExecFunction should take a parameter, telling whether the item was |
---|
28 | simply picked, or if it had a +/- pressed on it. This lets us change |
---|
29 | a value from the menu. |
---|
30 | |
---|
31 | We should also add another function to the MenuItem struct: |
---|
32 | |
---|
33 | Data Function -- returns true, false, or 0-255. |
---|
34 | |
---|
35 | This lets us find out if a checkbox should be checked, or where a |
---|
36 | slider should be. DoMenu() will figure out what widget it's dealing |
---|
37 | with (if any), and adjust its display accordingly. |
---|
38 | |
---|
39 | But we still can't move menu items up/down. |
---|
40 | |
---|
41 | |
---|
42 | ... |
---|
43 | |
---|
44 | |
---|
45 | Perhaps... This might work better: |
---|
46 | |
---|
47 | Title -- Text |
---|
48 | Type -- menu, function, checkbox, slider, mover |
---|
49 | Data -- Child, ExecFunc, CheckFunc, SlidFunc, ??? |
---|
50 | |
---|
51 | When an item is picked, DoMenu() decides what to do based on type. |
---|
52 | --"Menus" will recurse into the "data", assuming it's a child menu. |
---|
53 | --"Function"-type items will have their function called. |
---|
54 | --CheckBox-type items will have their function called with a "read" |
---|
55 | parameter to get an on/off signal, and called with a "set" signal when |
---|
56 | picked. |
---|
57 | --Sliders will have the same "read" thing, and the "set" function will |
---|
58 | take a plus or minus parameter. |
---|
59 | --The Movers will act like a label until picked, and then the +/- keys |
---|
60 | will both rearrange the menu, and send the item a signal of some sort |
---|
61 | to indicate what happened. It'll act like a label again after the |
---|
62 | user presses Enter again. |
---|
63 | |
---|
64 | The "Data" field will really be a "void *", which is C's "generic" |
---|
65 | data type... |
---|
66 | |
---|
67 | Anyway, this sort of thing would be declared this way: |
---|
68 | |
---|
69 | ======================================================================== |
---|
70 | |
---|
71 | Menu MainMenu = { |
---|
72 | "MENU", NULL, NULL, // Title |
---|
73 | "Options", MENU_TYPE, (void *)OptionsMenu, |
---|
74 | "Kill LCDproc", FUNC_TYPE, (void *)Shutdown_func, |
---|
75 | NULL, NULL, NULL, |
---|
76 | }; |
---|
77 | |
---|
78 | Menu OptionsMenu = { |
---|
79 | "OPTIONS", NULL, NULL, // Title |
---|
80 | "24-hour Time", CHEK_TYPE, (void *)Time24_func, |
---|
81 | "Contrast...", SLID_TYPE, (void *)Contrast_func, |
---|
82 | NULL, NULL, NULL, |
---|
83 | }; |
---|
84 | |
---|
85 | |
---|
86 | ///////////////// Elsewhere, we declare these... |
---|
87 | |
---|
88 | void Shutdown_func() |
---|
89 | { |
---|
90 | // Do something here... |
---|
91 | return MENU_KILL; // or MENU_CLOSE, or MENU_OK, or MENU_ERROR |
---|
92 | } |
---|
93 | |
---|
94 | int Time24_func(int input) |
---|
95 | { |
---|
96 | if(input == READ) return status; |
---|
97 | if(input == SELECT) toggle_status(); // does something. |
---|
98 | return (status | MENU_OK); |
---|
99 | // The status is "or"-ed with the MENU value to let DoMenu() |
---|
100 | // know what to do after selecting the item. (two return |
---|
101 | // values in one. :) |
---|
102 | |
---|
103 | // Also, "MENU_OK" happens to be zero, so it doesn't matter |
---|
104 | // unless you want something else (like MENU_CLOSE) |
---|
105 | } |
---|
106 | |
---|
107 | int Contrast_func(int input) |
---|
108 | { |
---|
109 | if(input == READ) return status; |
---|
110 | if(input == PLUS) increment_status(); // does something. |
---|
111 | if(input == MINUS) decrement_status();// does something. |
---|
112 | return (status | MENU_OK); |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | ======================================================================== |
---|
117 | |
---|
118 | The main reason I like this is that it completely separates the menu |
---|
119 | definitions from the code which actually handles it. We have *one* |
---|
120 | function which does everything menu-related. |
---|
121 | |
---|
122 | Also, we'd include a table of some sort to match names to functions, |
---|
123 | so that the user can create their own menus with the functionality |
---|
124 | already provided. (including user-defined "functions", which will be |
---|
125 | rather limited but still useful) |
---|
126 | |
---|