@@ -161,13 +161,168 @@ extern bool st_menu_reset_option(struct ST_MENU_STATE *mstate, int code, int opt
161161 configuration, and load state data.
162162
163163* The selected menu item can be accessed via function `st_menu_selected_item`. When `activated` flag
164- is true, then selected item was touched by mouse, accelerator or enter key.
164+ is true, then selected item was touched by mouse, accelerator or enter key. The activated flag will
165+ be false every time when no item is selected.
165166
166167* the menu state can be updated by function `st_menu_set_option` or function `st_menu_reset_option`.
167168
168-
169+ ## Example:
170+ ```c
171+ #include <langinfo.h>
172+ #include <locale.h>
173+ #include <ncurses.h>
174+ #include <panel.h>
175+ #include <string.h>
176+ #include <unicase.h>
177+
178+ #include "st_menu.h"
179+
180+ /*
181+ * Read event. When event is mouse event, read mouse data
182+ */
183+ static inline int
184+ get_event(MEVENT *mevent, bool *alt)
185+ {
186+ bool first_event = true;
187+ int c;
188+
189+ *alt = false;
190+
191+ repeat:
192+
193+ c = getch();
194+
195+ /*
196+ * Read mouse event when it is possible. Do it now, before st_meny_driver call,
197+ * as protection to unwanted multiple call of getmouse function. For one mouse
198+ * event, it returns data only once time.
199+ */
200+ if (c == KEY_MOUSE)
201+ {
202+ int ok = getmouse(mevent);
203+
204+ if (ok != OK)
205+ goto repeat;
206+ }
207+
208+ if (c == ST_MENU_ESCAPE)
209+ {
210+ if (first_event)
211+ {
212+ first_event = false;
213+ goto repeat;
214+ }
215+ }
216+
217+ *alt = !first_event;
218+
219+ return c;
220+ }
221+
222+ /*
223+ * Application demo
224+ */
225+ int
226+ main()
227+ {
228+ PANEL *mainpanel;
229+ ST_MENU_CONFIG config;
230+ ST_MENU *active_item;
231+ struct ST_MENU_STATE *mstate;
232+ bool activated;
233+ int c;
234+ MEVENT mevent;
235+ bool alt;
236+
237+ ST_MENU _file[] = {
238+ {"E~x~it", 34, "Alt-x"},
239+ {NULL, -1, NULL}
240+ };
241+
242+ ST_MENU menubar[] = {
243+ {"~F~ile", 61, NULL, 0, _file},
244+ {NULL, -1, NULL}
245+ };
246+
247+ setlocale(LC_ALL, "");
248+
249+ /* Don't use UTF when terminal doesn't use UTF */
250+ config.encoding = nl_langinfo(CODESET);
251+ config.language = uc_locale_language();
252+ config.force8bit = strcmp(config.encoding, "UTF-8") != 0;
253+
254+ initscr();
255+ start_color();
256+ clear();
257+ cbreak();
258+ noecho();
259+ keypad(stdscr, TRUE);
260+
261+ #ifdef NCURSES_EXT_FUNCS
262+
263+ set_escdelay(25);
264+
265+ #endif
266+
267+ refresh();
268+
269+ init_pair(1, COLOR_WHITE, COLOR_BLUE);
270+
271+ /* load style, possible alternatives: ST_MENU_STYLE_MC, ST_MENU_STYLE_DOS */
272+ st_menu_load_style(&config, ST_MENU_STYLE_VISION, 2);
169273
274+ mousemask(BUTTON1_PRESSED | BUTTON1_RELEASED, NULL);
275+ mouseinterval(0);
170276
171-
277+ /* prepare main window */
278+ wbkgd(stdscr, COLOR_PAIR(1));
279+ wrefresh(stdscr);
172280
281+ /*
282+ * main window should be panelized. Only panels can be
283+ * overlapped without unwanted effects.
284+ */
285+ mainpanel = new_panel(stdscr);
286+ st_menu_set_desktop_panel(mainpanel);
173287
288+ /* prepare state variable for menubar */
289+ mstate = st_menu_new_menubar(&config, menubar);
290+
291+ /* post meubar (display it) */
292+ st_menu_post(mstate);
293+
294+ c = get_event(&mevent, &alt);
295+
296+ refresh();
297+
298+ while (1)
299+ {
300+ bool processed = false;
301+
302+ processed = st_menu_driver(mstate, c, alt, &mevent);
303+
304+ doupdate();
305+
306+ active_item = st_menu_selected_item(&activated);
307+ if (processed && activated)
308+ {
309+ /* here is processing of menucode related to Exit menu item */
310+ if (active_item->code == 34)
311+ break;
312+ }
313+
314+ if (!processed && alt && c == 'x')
315+ break;
316+
317+ /* get new event */
318+ c = get_event(&mevent, &alt);
319+ }
320+
321+ endwin();
322+
323+ st_menu_unpost(mstate, true);
324+ st_menu_delete(mstate);
325+
326+ return 0;
327+ }
328+ ```
0 commit comments