PageRenderTime 61ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/programs/view/popup.cpp

https://github.com/gromacs/gromacs
C++ | 251 lines | 188 code | 22 blank | 41 comment | 17 complexity | 77416fce107c9465be9b6c1e7dad1132 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, Apache-2.0
  1. /*
  2. * This file is part of the GROMACS molecular simulation package.
  3. *
  4. * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  5. * Copyright (c) 2001-2013, The GROMACS development team.
  6. * Copyright (c) 2013,2014,2015,2017,2019,2020, by the GROMACS development team, led by
  7. * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  8. * and including many others, as listed in the AUTHORS file in the
  9. * top-level source directory and at http://www.gromacs.org.
  10. *
  11. * GROMACS is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public License
  13. * as published by the Free Software Foundation; either version 2.1
  14. * of the License, or (at your option) any later version.
  15. *
  16. * GROMACS is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with GROMACS; if not, see
  23. * http://www.gnu.org/licenses, or write to the Free Software Foundation,
  24. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  25. *
  26. * If you want to redistribute modifications to GROMACS, please
  27. * consider that scientific software is very special. Version
  28. * control is crucial - bugs must be traceable. We will be happy to
  29. * consider code for inclusion in the official distribution, but
  30. * derived work must not be called official GROMACS. Details are found
  31. * in the README & COPYING files - if they are missing, get the
  32. * official version at http://www.gromacs.org.
  33. *
  34. * To help us fund GROMACS development, we humbly ask that you cite
  35. * the research papers on the package. Check out http://www.gromacs.org.
  36. */
  37. #include "gmxpre.h"
  38. #include "popup.h"
  39. #include <cmath>
  40. #include <cstring>
  41. #include <algorithm>
  42. #include "gromacs/utility/smalloc.h"
  43. #include "x11.h"
  44. #include "xutil.h"
  45. static bool ChildCallBack(t_x11* x11, XEvent* event, Window w, void* data)
  46. {
  47. t_child* child;
  48. t_mentry* m;
  49. t_windata* wd;
  50. XEvent letter;
  51. child = (t_child*)data;
  52. m = child->m;
  53. wd = &(child->wd);
  54. switch (event->type)
  55. {
  56. case Expose:
  57. XSetForeground(x11->disp, x11->gc, x11->fg);
  58. TextInRect(x11, w, m->str, 16, 0, wd->width - 16 - 2, wd->height - 2, eXLeft, eYCenter);
  59. if (m->bChecked)
  60. {
  61. int y = x11->font->ascent;
  62. XDrawLine(x11->disp, w, x11->gc, 2, (y * 2) / 3, 6, y);
  63. XDrawLine(x11->disp, w, x11->gc, 3, (y * 2) / 3, 7, y);
  64. XDrawLine(x11->disp, w, x11->gc, 7, y, 12, 2);
  65. }
  66. break;
  67. case EnterNotify: LightBorder(x11->disp, w, x11->fg); break;
  68. case LeaveNotify: LightBorder(x11->disp, w, x11->bg); break;
  69. case ButtonRelease:
  70. letter.type = ClientMessage;
  71. letter.xclient.display = x11->disp;
  72. letter.xclient.window = m->send_to ? m->send_to : child->Parent;
  73. letter.xclient.message_type = 0;
  74. letter.xclient.format = 32;
  75. letter.xclient.data.l[0] = m->nreturn;
  76. XSendEvent(x11->disp, letter.xclient.window, True, 0, &letter);
  77. break;
  78. default: break;
  79. }
  80. return false;
  81. }
  82. static bool MenuCallBack(t_x11* x11, XEvent* event, Window /*w*/, void* data)
  83. {
  84. t_menu* m;
  85. m = (t_menu*)data;
  86. switch (event->type)
  87. {
  88. case Expose:
  89. /* Nothing to be done */
  90. if (m->bGrabbed)
  91. {
  92. m->bGrabbed = GrabOK(stderr,
  93. XGrabPointer(x11->disp,
  94. m->wd.self,
  95. True,
  96. ButtonReleaseMask,
  97. GrabModeAsync,
  98. GrabModeAsync,
  99. m->wd.self,
  100. None,
  101. CurrentTime));
  102. }
  103. break;
  104. case ButtonRelease: hide_menu(x11, m); break;
  105. case ClientMessage:
  106. event->xclient.window = m->Parent;
  107. XSendEvent(x11->disp, m->Parent, True, 0, event);
  108. break;
  109. default: break;
  110. }
  111. return false;
  112. }
  113. t_menu* init_menu(t_x11* x11, Window Parent, unsigned long fg, unsigned long bg, int nent, t_mentry ent[], int ncol)
  114. {
  115. int i, mlen, mht, area, ht;
  116. int j, k, l;
  117. int frows, fcol;
  118. t_menu* m;
  119. t_child* kid;
  120. t_windata* w;
  121. snew(m, 1);
  122. m->nitem = nent;
  123. m->Parent = Parent;
  124. /* Calculate dimensions of the menu */
  125. mlen = 0;
  126. for (i = 0; (i < nent); i++)
  127. {
  128. mlen = std::max(mlen, XTextWidth(x11->font, ent[i].str, std::strlen(ent[i].str)));
  129. }
  130. mht = XTextHeight(x11->font);
  131. /* Now we have the biggest single box, add a border of 2 pixels */
  132. mlen += 20; /* We need extra space at the left for checkmarks */
  133. mht += 4;
  134. /* Calculate the area of the menu */
  135. area = mlen * mht;
  136. ht = std::sqrt(area);
  137. /* No the number of rows per column, only beyond 8 rows */
  138. if (ncol == 0)
  139. {
  140. if (nent > 8)
  141. {
  142. frows = (1 + ht / mht);
  143. }
  144. else
  145. {
  146. frows = nent;
  147. }
  148. fcol = nent / frows;
  149. }
  150. else
  151. {
  152. fcol = ncol;
  153. frows = nent / ncol;
  154. if (nent % ncol)
  155. {
  156. frows++;
  157. }
  158. }
  159. InitWin(&(m->wd), 10, 10, fcol * mlen, frows * mht, 1, "Menu");
  160. snew(m->item, nent);
  161. m->wd.self = XCreateSimpleWindow(
  162. x11->disp, Parent, m->wd.x, m->wd.y, m->wd.width, m->wd.height, m->wd.bwidth, fg, bg);
  163. x11->RegisterCallback(x11, m->wd.self, Parent, MenuCallBack, m);
  164. x11->SetInputMask(x11, m->wd.self, ExposureMask | OwnerGrabButtonMask | ButtonReleaseMask);
  165. for (j = l = 0; (j < fcol); j++)
  166. {
  167. for (k = 0; (k < frows) && (l < nent); k++, l++)
  168. {
  169. kid = &(m->item[l]);
  170. kid->m = &(ent[l]);
  171. kid->Parent = Parent;
  172. w = &(kid->wd);
  173. InitWin(w, j * mlen, k * mht, mlen - 2, mht - 2, 1, nullptr);
  174. w->self = XCreateSimpleWindow(
  175. x11->disp, m->wd.self, w->x, w->y, w->width, w->height, w->bwidth, bg, bg);
  176. x11->RegisterCallback(x11, w->self, m->wd.self, ChildCallBack, kid);
  177. x11->SetInputMask(x11,
  178. w->self,
  179. ButtonPressMask | ButtonReleaseMask | OwnerGrabButtonMask
  180. | ExposureMask | EnterWindowMask | LeaveWindowMask);
  181. }
  182. }
  183. return m;
  184. }
  185. void show_menu(t_x11* x11, t_menu* m, int x, int y, bool bGrab)
  186. {
  187. XMoveWindow(x11->disp, m->wd.self, x, y);
  188. m->bGrabbed = bGrab;
  189. XMapWindow(x11->disp, m->wd.self);
  190. XMapSubwindows(x11->disp, m->wd.self);
  191. }
  192. void hide_menu(t_x11* x11, t_menu* m)
  193. {
  194. if (m->bGrabbed)
  195. {
  196. XUngrabPointer(x11->disp, CurrentTime);
  197. }
  198. XUnmapWindow(x11->disp, m->wd.self);
  199. }
  200. void check_menu_item(t_menu* m, int nreturn, bool bStatus)
  201. {
  202. int i;
  203. for (i = 0; (i < m->nitem); i++)
  204. {
  205. if (m->item[i].m->nreturn == nreturn)
  206. {
  207. m->item[i].m->bChecked = bStatus;
  208. }
  209. }
  210. }
  211. void done_menu(t_x11* x11, t_menu* m)
  212. {
  213. int i;
  214. for (i = 0; (i < m->nitem); i++)
  215. {
  216. x11->UnRegisterCallback(x11, m->item[i].wd.self);
  217. }
  218. sfree(m->item);
  219. x11->UnRegisterCallback(x11, m->wd.self);
  220. sfree(m);
  221. }
  222. int menu_width(t_menu* m)
  223. {
  224. return m->wd.width;
  225. }
  226. int menu_height(t_menu* m)
  227. {
  228. return m->wd.height;
  229. }