Activate.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /* Copyright Massachusetts Institute of Technology 1985 */
  2. #include "copyright.h"
  3. /*
  4. Copyright (C) 2001-2012 Free Software Foundation, Inc.
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * XMenu: MIT Project Athena, X Window system menu package
  17. *
  18. * XMenuActivate - Maps a given menu to the display and activates
  19. * the menu for user selection. The user is allowed to
  20. * specify which pane and selection will be current,
  21. * the X and Y location of the menu (relative to the
  22. * parent window) and the mouse button event mask that
  23. * will be used to identify a selection request.
  24. *
  25. * A menu selection is shown to be current by placing
  26. * a highlight box around the selection as the mouse
  27. * cursor enters its active region. Inactive selections
  28. * will not be highlighted. As the mouse cursor moved
  29. * from one menu pane to another menu pane the pane being
  30. * entered is raised and made current and the pane being
  31. * left is lowered.
  32. *
  33. * Anytime XMenuActivate returns, the p_num and
  34. * s_num are left at their last known values (i.e.,
  35. * the last known current pane and selection indices).
  36. * The following are the defined return states:
  37. *
  38. * 1) If at any time an error occurs the data
  39. * pointer is left untouched and XM_FAILURE
  40. * is returned.
  41. *
  42. * 2) When a selection request is received (i.e.,
  43. * when the specified mouse event occurs) the
  44. * data pointer will be set to the data
  45. * associated with the particular selection
  46. * current at the time of the selection request
  47. * and XM_SUCCESS is returned.
  48. *
  49. * 3) If no selection was current at the time a
  50. * selection request is made the data pointer
  51. * will be left untouched and XM_NO_SELECT will
  52. * be returned.
  53. *
  54. * 4) If the selection that was current at the time
  55. * a selection request is made is not an active
  56. * selection the data pointer will be left
  57. * untouched and XM_IA_SELECT will be returned.
  58. *
  59. * Since X processes events in an asynchronous manner
  60. * it is likely that XMenuActivate will encounter
  61. * a "foreign event" while it is executing. Foreign
  62. * events are handled in one of three ways:
  63. *
  64. * 1) The event is discarded. This is the default
  65. * mode and requires no action on the part of the
  66. * application.
  67. *
  68. * 2) The application has identified an asynchronous
  69. * event handler that will be called and the
  70. * foreign event handed off to it. Note:
  71. * AEQ mode disables this mode temporarily.
  72. *
  73. * 3) The application has enabled asynchronous event
  74. * queuing mode. In this mode all foreign events
  75. * will be queued up until XMenuActivate
  76. * terminates; at which time they will be
  77. * returned to the X event queue. As long as
  78. * AEQ mode is enabled any asynchronous event
  79. * handler as temporarily disabled.
  80. *
  81. * Any events encountered while taking down the menu
  82. * (i.e., exposure events from occluded windows) will
  83. * automatically be returned to the X event queue after
  84. * XMenuActivate has cleaned the queue of any of its own
  85. * events that are no longer needed.
  86. *
  87. * Author: Tony Della Fera, DEC
  88. * March 12, 1986
  89. *
  90. */
  91. #include <config.h>
  92. #include "XMenuInt.h"
  93. #include <X11/keysym.h>
  94. /* For debug, set this to 0 to not grab the keyboard on menu popup */
  95. int x_menu_grab_keyboard = 1;
  96. static Wait_func wait_func;
  97. static void* wait_data;
  98. void
  99. XMenuActivateSetWaitFunction (Wait_func func, void *data)
  100. {
  101. wait_func = func;
  102. wait_data = data;
  103. }
  104. int
  105. XMenuActivate(
  106. register Display *display, /* Display to put menu on. */
  107. register XMenu *menu, /* Menu to activate. */
  108. int *p_num, /* Pane number selected. */
  109. int *s_num, /* Selection number selected. */
  110. int x_pos, /* X coordinate of menu position. */
  111. int y_pos, /* Y coordinate of menu position. */
  112. unsigned int event_mask, /* Mouse button event mask. */
  113. char **data, /* Pointer to return data value. */
  114. void (*help_callback) (char const *, int, int)) /* Help callback. */
  115. {
  116. int status; /* X routine call status. */
  117. int orig_x; /* Upper left menu origin X coord. */
  118. int orig_y; /* Upper left menu origin Y coord. */
  119. int ret_val; /* Return value. */
  120. register XMPane *p_ptr; /* Current XMPane. */
  121. register XMPane *event_xmp; /* Event XMPane pointer. */
  122. register XMPane *cur_p; /* Current pane. */
  123. register XMSelect *cur_s; /* Current selection. */
  124. XMWindow *event_xmw; /* Event XMWindow pointer. */
  125. XEvent event; /* X input event. */
  126. XEvent peek_event; /* X input peek ahead event. */
  127. Bool selection = False; /* Selection has been made. */
  128. Bool forward = True; /* Moving forward in the pane list. */
  129. Window root, child;
  130. int root_x, root_y, win_x, win_y;
  131. unsigned int mask;
  132. KeySym keysym;
  133. /*
  134. * Define and allocate a foreign event queue to hold events
  135. * that don't belong to XMenu. These events are later restored
  136. * to the X event queue.
  137. */
  138. typedef struct _xmeventque {
  139. XEvent event;
  140. struct _xmeventque *next;
  141. } XMEventQue;
  142. XMEventQue *feq = NULL; /* Foreign event queue. */
  143. XMEventQue *feq_tmp; /* Foreign event queue temporary. */
  144. /*
  145. * If there are no panes in the menu then return failure
  146. * because the menu is not initialized.
  147. */
  148. if (menu->p_count == 0) {
  149. _XMErrorCode = XME_NOT_INIT;
  150. return(XM_FAILURE);
  151. }
  152. /*
  153. * Find the desired current pane.
  154. */
  155. cur_p = _XMGetPanePtr(menu, *p_num);
  156. if (cur_p == NULL) {
  157. return(XM_FAILURE);
  158. }
  159. cur_p->activated = cur_p->active;
  160. /*
  161. * Find the desired current selection.
  162. * If the current selection index is out of range a null current selection
  163. * will be assumed and the cursor will be placed in the current pane
  164. * header.
  165. */
  166. cur_s = _XMGetSelectionPtr(cur_p, *s_num);
  167. /*
  168. * Compute origin of menu so that cursor is in
  169. * Correct pane and selection.
  170. */
  171. _XMTransToOrigin(display,
  172. menu,
  173. cur_p, cur_s,
  174. x_pos, y_pos,
  175. &orig_x, &orig_y);
  176. menu->x_pos = orig_x; /* Store X and Y coords of menu. */
  177. menu->y_pos = orig_y;
  178. if (XMenuRecompute(display, menu) == XM_FAILURE) {
  179. return(XM_FAILURE);
  180. }
  181. /*
  182. * Flush the window creation queue.
  183. * This batches all window creates since lazy evaluation
  184. * is more efficient than individual evaluation.
  185. * This routine also does an XFlush().
  186. */
  187. if (_XMWinQueFlush(display, menu, cur_p, cur_s) == _FAILURE) {
  188. return(XM_FAILURE);
  189. }
  190. /*
  191. * Make sure windows are in correct order (in case we were passed
  192. * an already created menu in incorrect order.)
  193. */
  194. for(p_ptr = menu->p_list->next; p_ptr != cur_p; p_ptr = p_ptr->next)
  195. XRaiseWindow(display, p_ptr->window);
  196. for(p_ptr = menu->p_list->prev; p_ptr != cur_p->prev; p_ptr = p_ptr->prev)
  197. XRaiseWindow(display, p_ptr->window);
  198. /*
  199. * Make sure all selection windows are mapped.
  200. */
  201. for (
  202. p_ptr = menu->p_list->next;
  203. p_ptr != menu->p_list;
  204. p_ptr = p_ptr->next
  205. ){
  206. XMapSubwindows(display, p_ptr->window);
  207. }
  208. /*
  209. * Synchronize the X buffers and the event queue.
  210. * From here on, all events in the queue that don't belong to
  211. * XMenu are sent back to the application via an application
  212. * provided event handler or discarded if the application has
  213. * not provided an event handler.
  214. */
  215. XSync(display, 0);
  216. /*
  217. * Grab the mouse for menu input.
  218. */
  219. status = XGrabPointer(
  220. display,
  221. menu->parent,
  222. True,
  223. event_mask,
  224. GrabModeAsync,
  225. GrabModeAsync,
  226. None,
  227. menu->mouse_cursor,
  228. CurrentTime
  229. );
  230. if (status == Success && x_menu_grab_keyboard)
  231. {
  232. status = XGrabKeyboard (display,
  233. menu->parent,
  234. False,
  235. GrabModeAsync,
  236. GrabModeAsync,
  237. CurrentTime);
  238. if (status != Success)
  239. XUngrabPointer(display, CurrentTime);
  240. }
  241. if (status == _X_FAILURE) {
  242. _XMErrorCode = XME_GRAB_MOUSE;
  243. return(XM_FAILURE);
  244. }
  245. /*
  246. * Map the menu panes.
  247. */
  248. XMapWindow(display, cur_p->window);
  249. for (p_ptr = menu->p_list->next;
  250. p_ptr != cur_p;
  251. p_ptr = p_ptr->next)
  252. XMapWindow(display, p_ptr->window);
  253. for (p_ptr = cur_p->next;
  254. p_ptr != menu->p_list;
  255. p_ptr = p_ptr->next)
  256. XMapWindow(display, p_ptr->window);
  257. XRaiseWindow(display, cur_p->window); /* Make sure current */
  258. /* pane is on top. */
  259. cur_s = NULL; /* Clear current selection. */
  260. /*
  261. * Begin event processing loop.
  262. */
  263. while (1) {
  264. if (wait_func) (*wait_func) (wait_data);
  265. XNextEvent(display, &event); /* Get next event. */
  266. switch (event.type) { /* Dispatch on the event type. */
  267. case Expose:
  268. event_xmp = (XMPane *)XLookUpAssoc(display,
  269. menu->assoc_tab,
  270. event.xexpose.window);
  271. if (event_xmp == NULL) {
  272. /*
  273. * If AEQ mode is enabled then queue the event.
  274. */
  275. if (menu->aeq) {
  276. feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
  277. if (feq_tmp == NULL) {
  278. _XMErrorCode = XME_CALLOC;
  279. return(XM_FAILURE);
  280. }
  281. feq_tmp->event = event;
  282. feq_tmp->next = feq;
  283. feq = feq_tmp;
  284. }
  285. else if (_XMEventHandler) (*_XMEventHandler)(&event);
  286. break;
  287. }
  288. if (event_xmp->activated) {
  289. XSetWindowBackground(display,
  290. event_xmp->window,
  291. menu->bkgnd_color);
  292. }
  293. else {
  294. XSetWindowBackgroundPixmap(display,
  295. event_xmp->window,
  296. menu->inact_pixmap);
  297. }
  298. _XMRefreshPane(display, menu, event_xmp);
  299. break;
  300. case EnterNotify:
  301. /*
  302. * First wait a small period of time, and see
  303. * if another EnterNotify event follows hard on the
  304. * heels of this one. i.e., the user is simply
  305. * "passing through". If so, ignore this one.
  306. */
  307. event_xmw = (XMWindow *)XLookUpAssoc(display,
  308. menu->assoc_tab,
  309. event.xcrossing.window);
  310. if (event_xmw == NULL) break;
  311. if (event_xmw->type == SELECTION) {
  312. /*
  313. * We have entered a selection.
  314. */
  315. /* if (XPending(display) == 0) usleep(150000); */
  316. if (XPending(display) != 0) {
  317. XPeekEvent(display, &peek_event);
  318. if(peek_event.type == LeaveNotify) {
  319. break;
  320. }
  321. }
  322. cur_s = (XMSelect *)event_xmw;
  323. help_callback (cur_s->help_string,
  324. cur_p->serial, cur_s->serial);
  325. /*
  326. * If the pane we are in is active and the
  327. * selection entered is active then activate
  328. * the selection.
  329. */
  330. if (cur_p->active && cur_s->active > 0) {
  331. cur_s->activated = 1;
  332. _XMRefreshSelection(display, menu, cur_s);
  333. }
  334. }
  335. else {
  336. /*
  337. * We have entered a pane.
  338. */
  339. /* if (XPending(display) == 0) usleep(150000); */
  340. if (XPending(display) != 0) {
  341. XPeekEvent(display, &peek_event);
  342. if (peek_event.type == EnterNotify) break;
  343. }
  344. XQueryPointer(display,
  345. menu->parent,
  346. &root, &child,
  347. &root_x, &root_y,
  348. &win_x, &win_y,
  349. &mask);
  350. event_xmp = (XMPane *)XLookUpAssoc(display,
  351. menu->assoc_tab,
  352. child);
  353. if (event_xmp == NULL) break;
  354. if (event_xmp == cur_p) break;
  355. if (event_xmp->serial > cur_p->serial) forward = True;
  356. else forward = False;
  357. p_ptr = cur_p;
  358. while (p_ptr != event_xmp) {
  359. if (forward) p_ptr = p_ptr->next;
  360. else p_ptr = p_ptr->prev;
  361. XRaiseWindow(display, p_ptr->window);
  362. }
  363. if (cur_p->activated) {
  364. cur_p->activated = False;
  365. XSetWindowBackgroundPixmap(display,
  366. cur_p->window,
  367. menu->inact_pixmap);
  368. _XMRefreshPane(display, menu, cur_p);
  369. }
  370. if (event_xmp->active) event_xmp->activated = True;
  371. #if 1
  372. /*
  373. * i suspect the we don't get an EXPOSE event when backing
  374. * store is enabled; the menu windows content is probably
  375. * not drawn in when it should be in that case.
  376. * in that case, this is probably an ugly fix!
  377. * i hope someone more familiar with this code would
  378. * take it from here. -- caveh@eng.sun.com.
  379. */
  380. XSetWindowBackground(display,
  381. event_xmp->window,
  382. menu->bkgnd_color);
  383. _XMRefreshPane(display, menu, event_xmp);
  384. #endif
  385. cur_p = event_xmp;
  386. }
  387. break;
  388. case LeaveNotify:
  389. event_xmw = (XMWindow *)XLookUpAssoc(
  390. display,
  391. menu->assoc_tab,
  392. event.xcrossing.window
  393. );
  394. if (event_xmw == NULL) break;
  395. if(cur_s == NULL) break;
  396. /*
  397. * If the current selection was activated then
  398. * deactivate it.
  399. */
  400. if (cur_s->activated) {
  401. cur_s->activated = False;
  402. _XMRefreshSelection(display, menu, cur_s);
  403. }
  404. cur_s = NULL;
  405. break;
  406. case ButtonPress:
  407. case ButtonRelease:
  408. *p_num = cur_p->serial;
  409. /*
  410. * Check to see if there is a current selection.
  411. */
  412. if (cur_s != NULL) {
  413. /*
  414. * Set the selection number to the current selection.
  415. */
  416. *s_num = cur_s->serial;
  417. /*
  418. * If the current selection was activated then
  419. * we have a valid selection otherwise we have
  420. * an inactive selection.
  421. */
  422. if (cur_s->activated) {
  423. *data = cur_s->data;
  424. ret_val = XM_SUCCESS;
  425. }
  426. else {
  427. ret_val = XM_IA_SELECT;
  428. }
  429. }
  430. else {
  431. /*
  432. * No selection was current.
  433. */
  434. ret_val = XM_NO_SELECT;
  435. }
  436. selection = True;
  437. break;
  438. case KeyPress:
  439. case KeyRelease:
  440. keysym = XLookupKeysym (&event.xkey, 0);
  441. /* Pop down on C-g and Escape. */
  442. if ((keysym == XK_g && (event.xkey.state & ControlMask) != 0)
  443. || keysym == XK_Escape) /* Any escape, ignore modifiers. */
  444. {
  445. ret_val = XM_NO_SELECT;
  446. selection = True;
  447. }
  448. break;
  449. default:
  450. /*
  451. * If AEQ mode is enabled then queue the event.
  452. */
  453. if (menu->aeq) {
  454. feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
  455. if (feq_tmp == NULL) {
  456. _XMErrorCode = XME_CALLOC;
  457. return(XM_FAILURE);
  458. }
  459. feq_tmp->event = event;
  460. feq_tmp->next = feq;
  461. feq = feq_tmp;
  462. }
  463. else if (_XMEventHandler) (*_XMEventHandler)(&event);
  464. }
  465. /*
  466. * If a selection has been made, break out of the event loop.
  467. */
  468. if (selection == True) break;
  469. }
  470. /*
  471. * Unmap the menu.
  472. */
  473. for ( p_ptr = menu->p_list->next;
  474. p_ptr != menu->p_list;
  475. p_ptr = p_ptr->next)
  476. {
  477. XUnmapWindow(display, p_ptr->window);
  478. }
  479. /*
  480. * Ungrab the mouse.
  481. */
  482. XUngrabPointer(display, CurrentTime);
  483. XUngrabKeyboard(display, CurrentTime);
  484. /*
  485. * Restore bits under where the menu was if we managed
  486. * to save them and free the pixmap.
  487. */
  488. /*
  489. * If there is a current selection deactivate it.
  490. */
  491. if (cur_s != NULL) cur_s->activated = 0;
  492. /*
  493. * Deactivate the current pane.
  494. */
  495. cur_p->activated = 0;
  496. XSetWindowBackgroundPixmap(display, cur_p->window, menu->inact_pixmap);
  497. /*
  498. * Synchronize the X buffers and the X event queue.
  499. */
  500. XSync(display, 0);
  501. /*
  502. * Dispatch any events remaining on the queue.
  503. */
  504. while (QLength(display)) {
  505. /*
  506. * Fetch the next event.
  507. */
  508. XNextEvent(display, &event);
  509. /*
  510. * Discard any events left on the queue that belong to XMenu.
  511. * All others are held and then returned to the event queue.
  512. */
  513. switch (event.type) {
  514. case Expose:
  515. case EnterNotify:
  516. case LeaveNotify:
  517. case ButtonPress:
  518. case ButtonRelease:
  519. /*
  520. * Does this event belong to one of XMenu's windows?
  521. * If so, discard it and process the next event.
  522. * If not fall through and treat it as a foreign event.
  523. */
  524. event_xmp = (XMPane *)XLookUpAssoc(
  525. display,
  526. menu->assoc_tab,
  527. event.xbutton.window
  528. );
  529. if (event_xmp != NULL) continue;
  530. default:
  531. /*
  532. * This is a foreign event.
  533. * Queue it for later return to the X event queue.
  534. */
  535. feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
  536. if (feq_tmp == NULL) {
  537. _XMErrorCode = XME_CALLOC;
  538. return(XM_FAILURE);
  539. }
  540. feq_tmp->event = event;
  541. feq_tmp->next = feq;
  542. feq = feq_tmp;
  543. }
  544. }
  545. /*
  546. * Return any foreign events that were queued to the X event queue.
  547. */
  548. while (feq != NULL) {
  549. feq_tmp = feq;
  550. XPutBackEvent(display, &feq_tmp->event);
  551. feq = feq_tmp->next;
  552. free((char *)feq_tmp);
  553. }
  554. wait_func = 0;
  555. /*
  556. * Return successfully.
  557. */
  558. _XMErrorCode = XME_NO_ERROR;
  559. return(ret_val);
  560. }