menu.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * menu.c
  3. * Copyright © 2008, 2009 Martin Duquesnoy <xorg62@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of the nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "wmfs.h"
  33. void
  34. menu_init(Menu *menu, char *name, int nitem, uint bg_f, char *fg_f, uint bg_n, char *fg_n)
  35. {
  36. /* Item */
  37. menu->nitem = nitem;
  38. menu->item = emalloc(sizeof(MenuItem), nitem);
  39. menu->name = name;
  40. /* Colors */
  41. menu->colors.focus.bg = bg_f;
  42. menu->colors.focus.fg = fg_f;
  43. menu->colors.normal.bg = bg_n;
  44. menu->colors.normal.fg = fg_n;
  45. return;
  46. }
  47. void
  48. menu_new_item(MenuItem *mi, char *name, void *func, char *cmd)
  49. {
  50. mi->name = name;
  51. mi->func = func;
  52. mi->cmd = cmd;
  53. return;
  54. }
  55. void
  56. menu_draw(Menu menu, int x, int y)
  57. {
  58. int i, width;
  59. XEvent ev;
  60. BarWindow *item[menu.nitem];
  61. BarWindow *frame;
  62. width = menu_get_longer_string(menu.item, menu.nitem);
  63. /* Frame barwin */
  64. frame = barwin_create(ROOT, x, y, width + SHADH, menu.nitem * (INFOBARH - SHADH) + SHADH * 2,
  65. menu.colors.normal.bg, menu.colors.normal.fg, False, False, True);
  66. barwin_map(frame);
  67. barwin_map_subwin(frame);
  68. barwin_refresh_color(frame);
  69. for(i = 0; i < menu.nitem; ++i)
  70. {
  71. item[i] = barwin_create(frame->win,
  72. SHADH,
  73. (i * (INFOBARH - SHADH) + SHADH),
  74. width - SHADH,
  75. INFOBARH,
  76. menu.colors.normal.bg,
  77. menu.colors.normal.fg,
  78. True, False, False);
  79. barwin_map(item[i]);
  80. barwin_refresh_color(item[i]);
  81. menu_draw_item_name(&menu, i, item);
  82. barwin_refresh(item[i]);
  83. }
  84. /* Select the first item */
  85. menu_focus_item(&menu, 0, item);
  86. XGrabKeyboard(dpy, ROOT, True, GrabModeAsync, GrabModeAsync, CurrentTime);
  87. while(!menu_manage_event(&ev, &menu, item));
  88. XUngrabKeyboard(dpy, CurrentTime);
  89. for(i = 0; i < menu.nitem; ++i)
  90. barwin_delete(item[i]);
  91. barwin_delete(frame);
  92. return;
  93. }
  94. Bool
  95. menu_manage_event(XEvent *ev, Menu *menu, BarWindow *winitem[])
  96. {
  97. int i, c = 0;
  98. KeySym ks;
  99. Bool quit = False;
  100. switch(ev->type)
  101. {
  102. /* Mouse events */
  103. case ButtonPress:
  104. /* Execute the function linked with the item */
  105. for(i = 0; i < menu->nitem; ++i)
  106. {
  107. if(ev->xbutton.window == winitem[i]->win
  108. && (ev->xbutton.button == Button1 || ev->xbutton.button == Button2))
  109. {
  110. if(menu->item[i].func)
  111. menu->item[i].func(menu->item[i].cmd);
  112. quit = True;
  113. }
  114. else if(ev->xbutton.window != winitem[i]->win)
  115. ++c;
  116. else if(ev->xbutton.button == Button4)
  117. menu_focus_item(menu, menu->focus_item - 1, winitem);
  118. else if(ev->xbutton.button == Button5)
  119. menu_focus_item(menu, menu->focus_item + 1, winitem);
  120. }
  121. /* If the clicked window is not one of menu wins (items), quit. */
  122. if(c == i)
  123. quit = True;
  124. break;
  125. /* Keys */
  126. case KeyPress:
  127. XLookupString(&ev->xkey, NULL, 0, &ks, 0);
  128. switch(ks)
  129. {
  130. case XK_Up:
  131. menu_focus_item(menu, menu->focus_item - 1, winitem);
  132. break;
  133. case XK_Down:
  134. menu_focus_item(menu, menu->focus_item + 1, winitem);
  135. break;
  136. case XK_Return:
  137. if(menu->item[menu->focus_item].func)
  138. menu->item[menu->focus_item].func(menu->item[menu->focus_item].cmd);
  139. quit = True;
  140. break;
  141. case XK_Escape:
  142. quit = True;
  143. break;
  144. }
  145. break;
  146. /* Focus (with mouse) management */
  147. case EnterNotify:
  148. /* For focus an item with the mouse */
  149. for(i = 0; i < menu->nitem; ++i)
  150. if(ev->xcrossing.window == winitem[i]->win)
  151. menu_focus_item(menu, i, winitem);
  152. break;
  153. default:
  154. getevent(*ev);
  155. break;
  156. }
  157. XNextEvent(dpy, ev);
  158. return quit;
  159. }
  160. void
  161. menu_focus_item(Menu *menu, int item, BarWindow *winitem[])
  162. {
  163. int i;
  164. menu->focus_item = item;
  165. if(menu->focus_item > menu->nitem - 1)
  166. menu->focus_item = 0;
  167. else if(menu->focus_item < 0)
  168. menu->focus_item = menu->nitem - 1;
  169. for(i = 0; i < menu->nitem; ++i)
  170. {
  171. winitem[i]->fg = ((i == menu->focus_item) ? menu->colors.focus.fg : menu->colors.normal.fg);
  172. winitem[i]->bg = ((i == menu->focus_item) ? menu->colors.focus.bg : menu->colors.normal.bg);
  173. barwin_refresh_color(winitem[i]);
  174. menu_draw_item_name(menu, i, winitem);
  175. barwin_refresh(winitem[i]);
  176. }
  177. return;
  178. }
  179. void
  180. menu_draw_item_name(Menu *menu, int item, BarWindow *winitem[])
  181. {
  182. int width = menu_get_longer_string(menu->item, menu->nitem);
  183. barwin_draw_text(winitem[item],
  184. ((width / 2) - (textw(menu->item[item].name) / 2)),
  185. FHINFOBAR,
  186. menu->item[item].name);
  187. return;
  188. }
  189. int
  190. menu_get_longer_string(MenuItem *mt, int nitem)
  191. {
  192. int i, l = 0;
  193. for(i = 0; i < nitem; ++i)
  194. if(textw(mt[i].name) > l)
  195. l = textw(mt[i].name);
  196. return l + PAD;
  197. }
  198. void
  199. uicb_menu(uicb_t cmd)
  200. {
  201. int i, d, u, x, y;
  202. Window w;
  203. if(!strcmp(cmd, "menulayout"))
  204. menu_draw(menulayout, menulayout.x, menulayout.y);
  205. for(i = 0; i < conf.nmenu; ++i)
  206. if(!strcmp(cmd, conf.menu[i].name))
  207. {
  208. if(conf.menu[i].place_at_mouse)
  209. XQueryPointer(dpy, ROOT, &w, &w, &x, &y, &d, &d, (uint *)&u);
  210. else
  211. {
  212. screen_get_sel();
  213. x = conf.menu[i].x + spgeo[selscreen].x;
  214. y = conf.menu[i].y + spgeo[selscreen].y;
  215. }
  216. menu_draw(conf.menu[i], x, y);
  217. }
  218. return;
  219. }