mousesupport.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. static void
  2. buttonpress(XEvent *e)
  3. {
  4. struct item *item;
  5. XButtonPressedEvent *ev = &e->xbutton;
  6. int x = 0, y = 0, h = bh, w;
  7. if (ev->window != win)
  8. return;
  9. /* right-click: exit */
  10. if (ev->button == Button3)
  11. exit(1);
  12. if (prompt && *prompt)
  13. x += promptw;
  14. /* input field */
  15. w = (lines > 0 || !matches) ? mw - x : inputw;
  16. /* left-click on input: clear input,
  17. * NOTE: if there is no left-arrow the space for < is reserved so
  18. * add that to the input width */
  19. if (ev->button == Button1 &&
  20. ((lines <= 0 && ev->x >= 0 && ev->x <= x + w +
  21. ((!prev || !curr->left) ? TEXTW("<") : 0)) ||
  22. (lines > 0 && ev->y >= y && ev->y <= y + h))) {
  23. insert(NULL, -cursor);
  24. drawmenu();
  25. return;
  26. }
  27. /* middle-mouse click: paste selection */
  28. if (ev->button == Button2) {
  29. XConvertSelection(dpy, (ev->state & ShiftMask) ? clip : XA_PRIMARY,
  30. utf8, utf8, win, CurrentTime);
  31. drawmenu();
  32. return;
  33. }
  34. /* scroll up */
  35. if (ev->button == Button4 && prev) {
  36. sel = curr = prev;
  37. calcoffsets();
  38. drawmenu();
  39. return;
  40. }
  41. /* scroll down */
  42. if (ev->button == Button5 && next) {
  43. sel = curr = next;
  44. calcoffsets();
  45. drawmenu();
  46. return;
  47. }
  48. if (ev->button != Button1)
  49. return;
  50. if (ev->state & ~ControlMask)
  51. return;
  52. if (lines > 0) {
  53. /* vertical list: (ctrl)left-click on item */
  54. w = mw - x;
  55. for (item = curr; item != next; item = item->right) {
  56. y += h;
  57. if (ev->y >= y && ev->y <= (y + h)) {
  58. puts(item->text);
  59. if (!(ev->state & ControlMask)) {
  60. exit(0);
  61. }
  62. sel = item;
  63. if (sel) {
  64. sel->out = 1;
  65. drawmenu();
  66. }
  67. return;
  68. }
  69. }
  70. } else if (matches) {
  71. /* left-click on left arrow */
  72. x += inputw;
  73. w = TEXTW("<");
  74. if (prev && curr->left) {
  75. if (ev->x >= x && ev->x <= x + w) {
  76. sel = curr = prev;
  77. calcoffsets();
  78. drawmenu();
  79. return;
  80. }
  81. }
  82. /* horizontal list: (ctrl)left-click on item */
  83. for (item = curr; item != next; item = item->right) {
  84. x += w;
  85. w = MIN(TEXTW(item->text), mw - x - TEXTW(">"));
  86. if (ev->x >= x && ev->x <= x + w) {
  87. puts(item->text);
  88. if (!(ev->state & ControlMask)) {
  89. exit(0);
  90. }
  91. sel = item;
  92. if (sel) {
  93. sel->out = 1;
  94. drawmenu();
  95. }
  96. return;
  97. }
  98. }
  99. /* left-click on right arrow */
  100. w = TEXTW(">");
  101. x = mw - w;
  102. if (next && ev->x >= x && ev->x <= x + w) {
  103. sel = curr = next;
  104. calcoffsets();
  105. drawmenu();
  106. return;
  107. }
  108. }
  109. }