Activate.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /* Copyright Massachusetts Institute of Technology 1985 */
  2. #include "copyright.h"
  3. /*
  4. Copyright (C) 2001-2015 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 "XMenuInt.h"
  92. #include <X11/keysym.h>
  93. /* For debug, set this to 0 to not grab the keyboard on menu popup */
  94. int x_menu_grab_keyboard = 1;
  95. static Wait_func wait_func;
  96. static void* wait_data;
  97. void
  98. XMenuActivateSetWaitFunction (Wait_func func, void *data)
  99. {
  100. wait_func = func;
  101. wait_data = data;
  102. }
  103. int
  104. XMenuActivate(
  105. register Display *display, /* Display to put menu on. */
  106. register XMenu *menu, /* Menu to activate. */
  107. int *p_num, /* Pane number selected. */
  108. int *s_num, /* Selection number selected. */
  109. int x_pos, /* X coordinate of menu position. */
  110. int y_pos, /* Y coordinate of menu position. */
  111. unsigned int event_mask, /* Mouse button event mask. */
  112. char **data, /* Pointer to return data value. */
  113. void (*help_callback) (char const *, int, int)) /* Help callback. */
  114. {
  115. int status; /* X routine call status. */
  116. int orig_x; /* Upper left menu origin X coord. */
  117. int orig_y; /* Upper left menu origin Y coord. */
  118. int ret_val; /* Return value. */
  119. register XMPane *p_ptr; /* Current XMPane. */
  120. register XMPane *event_xmp; /* Event XMPane pointer. */
  121. register XMPane *cur_p; /* Current pane. */
  122. register XMSelect *cur_s; /* Current selection. */
  123. XMWindow *event_xmw; /* Event XMWindow pointer. */
  124. XEvent event; /* X input event. */
  125. XEvent peek_event; /* X input peek ahead event. */
  126. Bool selection = False; /* Selection has been made. */
  127. Bool forward = True; /* Moving forward in the pane list. */
  128. Window root, child;
  129. int root_x, root_y, win_x, win_y;
  130. unsigned int mask;
  131. KeySym keysym;
  132. /*
  133. * Define and allocate a foreign event queue to hold events
  134. * that don't belong to XMenu. These events are later restored
  135. * to the X event queue.
  136. */
  137. typedef struct _xmeventque {
  138. XEvent event;
  139. struct _xmeventque *next;
  140. } XMEventQue;
  141. XMEventQue *feq = NULL; /* Foreign event queue. */
  142. XMEventQue *feq_tmp; /* Foreign event queue temporary. */
  143. /*
  144. * If there are no panes in the menu then return failure
  145. * because the menu is not initialized.
  146. */
  147. if (menu->p_count == 0) {
  148. _XMErrorCode = XME_NOT_INIT;
  149. return(XM_FAILURE);
  150. }
  151. /*
  152. * Find the desired current pane.
  153. */
  154. cur_p = _XMGetPanePtr(menu, *p_num);
  155. if (cur_p == NULL) {
  156. return(XM_FAILURE);
  157. }
  158. cur_p->activated = cur_p->active;
  159. /*
  160. * Find the desired current selection.
  161. * If the current selection index is out of range a null current selection
  162. * will be assumed and the cursor will be placed in the current pane
  163. * header.
  164. */
  165. cur_s = _XMGetSelectionPtr(cur_p, *s_num);
  166. /*
  167. * Compute origin of menu so that cursor is in
  168. * Correct pane and selection.
  169. */
  170. _XMTransToOrigin(display,
  171. menu,
  172. cur_p, cur_s,
  173. x_pos, y_pos,
  174. &orig_x, &orig_y);
  175. menu->x_pos = orig_x; /* Store X and Y coords of menu. */
  176. menu->y_pos = orig_y;
  177. if (XMenuRecompute(display, menu) == XM_FAILURE) {
  178. return(XM_FAILURE);
  179. }
  180. /*
  181. * Flush the window creation queue.
  182. * This batches all window creates since lazy evaluation
  183. * is more efficient than individual evaluation.
  184. * This routine also does an XFlush().
  185. */
  186. if (_XMWinQueFlush(display, menu, cur_p, cur_s) == _FAILURE) {
  187. return(XM_FAILURE);
  188. }
  189. /*
  190. * Make sure windows are in correct order (in case we were passed
  191. * an already created menu in incorrect order.)
  192. */
  193. for(p_ptr = menu->p_list->next; p_ptr != cur_p; p_ptr = p_ptr->next)
  194. XRaiseWindow(display, p_ptr->window);
  195. for(p_ptr = menu->p_list->prev; p_ptr != cur_p->prev; p_ptr = p_ptr->prev)
  196. XRaiseWindow(display, p_ptr->window);
  197. /*
  198. * Make sure all selection windows are mapped.
  199. */
  200. for (
  201. p_ptr = menu->p_list->next;
  202. p_ptr != menu->p_list;
  203. p_ptr = p_ptr->next
  204. ){
  205. XMapSubwindows(display, p_ptr->window);
  206. }
  207. /*
  208. * Synchronize the X buffers and the event queue.
  209. * From here on, all events in the queue that don't belong to
  210. * XMenu are sent back to the application via an application
  211. * provided event handler or discarded if the application has
  212. * not provided an event handler.
  213. */
  214. XSync(display, 0);
  215. /*
  216. * Grab the mouse for menu input.
  217. */
  218. status = XGrabPointer(
  219. display,
  220. menu->parent,
  221. True,
  222. event_mask,
  223. GrabModeAsync,
  224. GrabModeAsync,
  225. None,
  226. menu->mouse_cursor,
  227. CurrentTime
  228. );
  229. if (status == Success && x_menu_grab_keyboard)
  230. {
  231. status = XGrabKeyboard (display,
  232. menu->parent,
  233. False,
  234. GrabModeAsync,
  235. GrabModeAsync,
  236. CurrentTime);
  237. if (status != Success)
  238. XUngrabPointer(display, CurrentTime);
  239. }
  240. if (status == _X_FAILURE) {
  241. _XMErrorCode = XME_GRAB_MOUSE;
  242. return(XM_FAILURE);
  243. }
  244. /*
  245. * Map the menu panes.
  246. */
  247. XMapWindow(display, cur_p->window);
  248. for (p_ptr = menu->p_list->next;
  249. p_ptr != cur_p;
  250. p_ptr = p_ptr->next)
  251. XMapWindow(display, p_ptr->window);
  252. for (p_ptr = cur_p->next;
  253. p_ptr != menu->p_list;
  254. p_ptr = p_ptr->next)
  255. XMapWindow(display, p_ptr->window);
  256. XRaiseWindow(display, cur_p->window); /* Make sure current */
  257. /* pane is on top. */
  258. cur_s = NULL; /* Clear current selection. */
  259. /*
  260. * Begin event processing loop.
  261. */
  262. while (1) {
  263. if (wait_func) (*wait_func) (wait_data);
  264. XNextEvent(display, &event); /* Get next event. */
  265. switch (event.type) { /* Dispatch on the event type. */
  266. case Expose:
  267. event_xmp = (XMPane *)XLookUpAssoc(display,
  268. menu->assoc_tab,
  269. event.xexpose.window);
  270. if (event_xmp == NULL) {
  271. /*
  272. * If AEQ mode is enabled then queue the event.
  273. */
  274. if (menu->aeq) {
  275. feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
  276. if (feq_tmp == NULL) {
  277. _XMErrorCode = XME_CALLOC;
  278. return(XM_FAILURE);
  279. }
  280. feq_tmp->event = event;
  281. feq_tmp->next = feq;
  282. feq = feq_tmp;
  283. }
  284. else if (_XMEventHandler) (*_XMEventHandler)(&event);
  285. break;
  286. }
  287. if (event_xmp->activated) {
  288. XSetWindowBackground(display,
  289. event_xmp->window,
  290. menu->bkgnd_color);
  291. }
  292. else {
  293. XSetWindowBackgroundPixmap(display,
  294. event_xmp->window,
  295. menu->inact_pixmap);
  296. }
  297. _XMRefreshPane(display, menu, event_xmp);
  298. break;
  299. case EnterNotify:
  300. /*
  301. * First wait a small period of time, and see
  302. * if another EnterNotify event follows hard on the
  303. * heels of this one. i.e., the user is simply
  304. * "passing through". If so, ignore this one.
  305. */
  306. event_xmw = (XMWindow *)XLookUpAssoc(display,
  307. menu->assoc_tab,
  308. event.xcrossing.window);
  309. if (event_xmw == NULL) break;
  310. if (event_xmw->type == SELECTION) {
  311. /*
  312. * We have entered a selection.
  313. */
  314. /* if (XPending(display) == 0) usleep(150000); */
  315. if (XPending(display) != 0) {
  316. XPeekEvent(display, &peek_event);
  317. if(peek_event.type == LeaveNotify) {
  318. break;
  319. }
  320. }
  321. cur_s = (XMSelect *)event_xmw;
  322. help_callback (cur_s->help_string,
  323. cur_p->serial, cur_s->serial);
  324. /*
  325. * If the pane we are in is active and the
  326. * selection entered is active then activate
  327. * the selection.
  328. */
  329. if (cur_p->active && cur_s->active > 0) {
  330. cur_s->activated = 1;
  331. _XMRefreshSelection(display, menu, cur_s);
  332. }
  333. }
  334. else {
  335. /*
  336. * We have entered a pane.
  337. */
  338. /* if (XPending(display) == 0) usleep(150000); */
  339. if (XPending(display) != 0) {
  340. XPeekEvent(display, &peek_event);
  341. if (peek_event.type == EnterNotify) break;
  342. }
  343. XQueryPointer(display,
  344. menu->parent,
  345. &root, &child,
  346. &root_x, &root_y,
  347. &win_x, &win_y,
  348. &mask);
  349. event_xmp = (XMPane *)XLookUpAssoc(display,
  350. menu->assoc_tab,
  351. child);
  352. if (event_xmp == NULL) break;
  353. if (event_xmp == cur_p) break;
  354. if (event_xmp->serial > cur_p->serial) forward = True;
  355. else forward = False;
  356. p_ptr = cur_p;
  357. while (p_ptr != event_xmp) {
  358. if (forward) p_ptr = p_ptr->next;
  359. else p_ptr = p_ptr->prev;
  360. XRaiseWindow(display, p_ptr->window);
  361. }
  362. if (cur_p->activated) {
  363. cur_p->activated = False;
  364. XSetWindowBackgroundPixmap(display,
  365. cur_p->window,
  366. menu->inact_pixmap);
  367. _XMRefreshPane(display, menu, cur_p);
  368. }
  369. if (event_xmp->active) event_xmp->activated = True;
  370. #if 1
  371. /*
  372. * i suspect the we don't get an EXPOSE event when backing
  373. * store is enabled; the menu windows content is probably
  374. * not drawn in when it should be in that case.
  375. * in that case, this is probably an ugly fix!
  376. * i hope someone more familiar with this code would
  377. * take it from here. -- caveh@eng.sun.com.
  378. */
  379. XSetWindowBackground(display,
  380. event_xmp->window,
  381. menu->bkgnd_color);
  382. _XMRefreshPane(display, menu, event_xmp);
  383. #endif
  384. cur_p = event_xmp;
  385. }
  386. break;
  387. case LeaveNotify:
  388. event_xmw = (XMWindow *)XLookUpAssoc(
  389. display,
  390. menu->assoc_tab,
  391. event.xcrossing.window
  392. );
  393. if (event_xmw == NULL) break;
  394. if(cur_s == NULL) break;
  395. /*
  396. * If the current selection was activated then
  397. * deactivate it.
  398. */
  399. if (cur_s->activated) {
  400. cur_s->activated = False;
  401. _XMRefreshSelection(display, menu, cur_s);
  402. }
  403. cur_s = NULL;
  404. break;
  405. case ButtonPress:
  406. case ButtonRelease:
  407. *p_num = cur_p->serial;
  408. /*
  409. * Check to see if there is a current selection.
  410. */
  411. if (cur_s != NULL) {
  412. /*
  413. * Set the selection number to the current selection.
  414. */
  415. *s_num = cur_s->serial;
  416. /*
  417. * If the current selection was activated then
  418. * we have a valid selection otherwise we have
  419. * an inactive selection.
  420. */
  421. if (cur_s->activated) {
  422. *data = cur_s->data;
  423. ret_val = XM_SUCCESS;
  424. }
  425. else {
  426. ret_val = XM_IA_SELECT;
  427. }
  428. }
  429. else {
  430. /*
  431. * No selection was current.
  432. */
  433. ret_val = XM_NO_SELECT;
  434. }
  435. selection = True;
  436. break;
  437. case KeyPress:
  438. case KeyRelease:
  439. keysym = XLookupKeysym (&event.xkey, 0);
  440. /* Pop down on C-g and Escape. */
  441. if ((keysym == XK_g && (event.xkey.state & ControlMask) != 0)
  442. || keysym == XK_Escape) /* Any escape, ignore modifiers. */
  443. {
  444. ret_val = XM_NO_SELECT;
  445. selection = True;
  446. }
  447. break;
  448. default:
  449. /*
  450. * If AEQ mode is enabled then queue the event.
  451. */
  452. if (menu->aeq) {
  453. feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
  454. if (feq_tmp == NULL) {
  455. _XMErrorCode = XME_CALLOC;
  456. return(XM_FAILURE);
  457. }
  458. feq_tmp->event = event;
  459. feq_tmp->next = feq;
  460. feq = feq_tmp;
  461. }
  462. else if (_XMEventHandler) (*_XMEventHandler)(&event);
  463. }
  464. /*
  465. * If a selection has been made, break out of the event loop.
  466. */
  467. if (selection == True) break;
  468. }
  469. /*
  470. * Unmap the menu.
  471. */
  472. for ( p_ptr = menu->p_list->next;
  473. p_ptr != menu->p_list;
  474. p_ptr = p_ptr->next)
  475. {
  476. XUnmapWindow(display, p_ptr->window);
  477. }
  478. /*
  479. * Ungrab the mouse.
  480. */
  481. XUngrabPointer(display, CurrentTime);
  482. XUngrabKeyboard(display, CurrentTime);
  483. /*
  484. * Restore bits under where the menu was if we managed
  485. * to save them and free the pixmap.
  486. */
  487. /*
  488. * If there is a current selection deactivate it.
  489. */
  490. if (cur_s != NULL) cur_s->activated = 0;
  491. /*
  492. * Deactivate the current pane.
  493. */
  494. cur_p->activated = 0;
  495. XSetWindowBackgroundPixmap(display, cur_p->window, menu->inact_pixmap);
  496. /*
  497. * Synchronize the X buffers and the X event queue.
  498. */
  499. XSync(display, 0);
  500. /*
  501. * Dispatch any events remaining on the queue.
  502. */
  503. while (QLength(display)) {
  504. /*
  505. * Fetch the next event.
  506. */
  507. XNextEvent(display, &event);
  508. /*
  509. * Discard any events left on the queue that belong to XMenu.
  510. * All others are held and then returned to the event queue.
  511. */
  512. switch (event.type) {
  513. case Expose:
  514. case EnterNotify:
  515. case LeaveNotify:
  516. case ButtonPress:
  517. case ButtonRelease:
  518. /*
  519. * Does this event belong to one of XMenu's windows?
  520. * If so, discard it and process the next event.
  521. * If not fall through and treat it as a foreign event.
  522. */
  523. event_xmp = (XMPane *)XLookUpAssoc(
  524. display,
  525. menu->assoc_tab,
  526. event.xbutton.window
  527. );
  528. if (event_xmp != NULL) continue;
  529. default:
  530. /*
  531. * This is a foreign event.
  532. * Queue it for later return to the X event queue.
  533. */
  534. feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
  535. if (feq_tmp == NULL) {
  536. _XMErrorCode = XME_CALLOC;
  537. return(XM_FAILURE);
  538. }
  539. feq_tmp->event = event;
  540. feq_tmp->next = feq;
  541. feq = feq_tmp;
  542. }
  543. }
  544. /*
  545. * Return any foreign events that were queued to the X event queue.
  546. */
  547. while (feq != NULL) {
  548. feq_tmp = feq;
  549. XPutBackEvent(display, &feq_tmp->event);
  550. feq = feq_tmp->next;
  551. free((char *)feq_tmp);
  552. }
  553. wait_func = 0;
  554. /*
  555. * Return successfully.
  556. */
  557. _XMErrorCode = XME_NO_ERROR;
  558. return(ret_val);
  559. }