x86UNIXMessageBox.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <X11/Xlib.h>
  26. #include <X11/Xutil.h>
  27. #include <X11/Xatom.h>
  28. #include "platformX86UNIX/x86UNIXMessageBox.h"
  29. #define MessageBox_MaxWinWidth 800
  30. #define MessageBox_MaxWinHeight 600
  31. #define MessageBox_MinWinWidth 450
  32. #define MessageBox_ButtonBoxWidth 60
  33. #define MessageBox_ButtonBoxHeight 22
  34. #define MessageBox_ButtonSpacer 20
  35. #define MessageBox_ButtonVMargin 10
  36. #define MessageBox_ButtonHMargin 10
  37. #define MessageBox_LineSpacer 2
  38. #define MessageBox_LineVMargin 10
  39. #define MessageBox_LineHMargin 10
  40. XMessageBoxButton::XMessageBoxButton()
  41. {
  42. strcpy(mLabel, "");
  43. mClickVal = -1;
  44. mLabelWidth = mX = mY = mWidth = mHeight = mMouseX = mMouseX = -1;
  45. mMouseDown = false;
  46. }
  47. XMessageBoxButton::XMessageBoxButton(const char* label, int clickVal)
  48. {
  49. strncpy(mLabel, label, LabelSize);
  50. mClickVal = clickVal;
  51. mLabelWidth = mX = mY = mWidth = mHeight = mMouseX = mMouseX = -1;
  52. mMouseDown = false;
  53. }
  54. XMessageBox::XMessageBox(Display* display)
  55. {
  56. mMessage = "";
  57. mFS = NULL;
  58. mDisplay = display;
  59. }
  60. XMessageBox::~XMessageBox()
  61. {
  62. clearMessageLines();
  63. if (mDisplay != NULL)
  64. {
  65. mDisplay = NULL;
  66. }
  67. }
  68. int XMessageBox::alertOK(const char *windowTitle, const char *message)
  69. {
  70. mMessage = message;
  71. mTitle = windowTitle;
  72. mButtons.clear();
  73. mButtons.push_back(XMessageBoxButton("OK", OK));
  74. return show();
  75. }
  76. int XMessageBox::alertOKCancel(const char *windowTitle, const char *message)
  77. {
  78. mMessage = message;
  79. mTitle = windowTitle;
  80. mButtons.clear();
  81. mButtons.push_back(XMessageBoxButton("OK", OK));
  82. mButtons.push_back(XMessageBoxButton("Cancel", Cancel));
  83. return show();
  84. }
  85. int XMessageBox::alertRetryCancel(const char *windowTitle, const char *message)
  86. {
  87. mMessage = message;
  88. mTitle = windowTitle;
  89. mButtons.clear();
  90. mButtons.push_back(XMessageBoxButton("Retry", Retry));
  91. mButtons.push_back(XMessageBoxButton("Cancel", Cancel));
  92. return show();
  93. }
  94. //Luma: YesNo alert message
  95. int XMessageBox::alertYesNo(const char *windowTitle, const char *message)
  96. {
  97. mMessage = message;
  98. mTitle = windowTitle;
  99. mButtons.clear();
  100. mButtons.push_back(XMessageBoxButton("Yes", OK));
  101. mButtons.push_back(XMessageBoxButton("No", Cancel));
  102. return show();
  103. }
  104. void XMessageBox::repaint()
  105. {
  106. int white = WhitePixel(mDisplay, DefaultScreen(mDisplay));
  107. int black = BlackPixel(mDisplay, DefaultScreen(mDisplay));
  108. int x = 0;
  109. int y = 0;
  110. // line V margin
  111. y = y + MessageBox_LineVMargin * 2;
  112. // line H margin
  113. x = MessageBox_LineHMargin;
  114. XSetForeground(mDisplay, mGC, black);
  115. for (unsigned int i = 0; i < mMessageLines.size(); ++i)
  116. {
  117. XDrawString(mDisplay, mWin, mGC, x, y, mMessageLines[i],
  118. strlen(mMessageLines[i]));
  119. if (i < (mMessageLines.size() - 1))
  120. y = y + MessageBox_LineSpacer + mFontHeight;
  121. }
  122. XFlush(mDisplay);
  123. // line V margin
  124. y = y + MessageBox_LineVMargin;
  125. int maxButWidth = MessageBox_ButtonBoxWidth;
  126. int maxButHeight = MessageBox_ButtonBoxHeight;
  127. // compute size of text labels on buttons
  128. int fgColor, bgColor;
  129. int fontDirection, fontAscent, fontDescent;
  130. Vector<XMessageBoxButton>::iterator iter;
  131. for (iter = mButtons.begin(); iter != mButtons.end(); ++iter)
  132. {
  133. XCharStruct strInfo;
  134. XTextExtents(mFS, iter->getLabel(), strlen(iter->getLabel()),
  135. &fontDirection, &fontAscent, &fontDescent,
  136. &strInfo);
  137. // if (maxButWidth < strInfo.width)
  138. // maxButWidth = strInfo.width;
  139. // if (maxButHeight < (strInfo.ascent + strInfo.descent))
  140. // maxButHeight = (strInfo.ascent + strInfo.descent);
  141. iter->setLabelWidth(strInfo.width);
  142. }
  143. int buttonBoxWidth = maxButWidth;
  144. int buttonBoxHeight = maxButHeight;
  145. // draw buttons
  146. // button V margin
  147. y = y + MessageBox_ButtonVMargin;
  148. // center the buttons
  149. x = MessageBox_ButtonHMargin + (mMBWidth - getButtonLineWidth()) / 2;
  150. for (iter = mButtons.begin(); iter != mButtons.end(); ++iter)
  151. {
  152. if (iter->drawReverse())
  153. {
  154. fgColor = white;
  155. bgColor = black;
  156. }
  157. else
  158. {
  159. fgColor = black;
  160. bgColor = white;
  161. }
  162. XSetForeground(mDisplay, mGC, bgColor);
  163. XFillRectangle(mDisplay, mWin, mGC, x, y,
  164. buttonBoxWidth, buttonBoxHeight);
  165. XSetForeground(mDisplay, mGC, fgColor);
  166. XDrawRectangle(mDisplay, mWin, mGC, x, y,
  167. buttonBoxWidth, buttonBoxHeight);
  168. XDrawString(mDisplay, mWin, mGC,
  169. x + ((buttonBoxWidth - iter->getLabelWidth()) / 2),
  170. y + mFontAscent + ((buttonBoxHeight - mFontAscent) / 2),
  171. iter->getLabel(),
  172. strlen(iter->getLabel()));
  173. iter->setButtonRect(x, y, buttonBoxWidth, buttonBoxHeight);
  174. x = x + buttonBoxWidth + MessageBox_ButtonSpacer;
  175. }
  176. }
  177. template <class Type>
  178. static inline Type max(Type v1, Type v2)
  179. {
  180. if (v1 <= v2)
  181. return v2;
  182. else
  183. return v1;
  184. }
  185. template <class Type>
  186. static inline Type min(Type v1, Type v2)
  187. {
  188. if (v1 > v2)
  189. return v2;
  190. else
  191. return v1;
  192. }
  193. void XMessageBox::clearMessageLines()
  194. {
  195. Vector<char*>::iterator iter;
  196. for (iter = mMessageLines.begin(); iter != mMessageLines.end(); ++iter)
  197. delete [] *iter;
  198. mMessageLines.clear();
  199. }
  200. void XMessageBox::splitMessage()
  201. {
  202. clearMessageLines();
  203. if (mMessage == NULL || strlen(mMessage)==0)
  204. // JMQTODO: what to do with empty strings?
  205. return;
  206. // need to break message up in to lines, and store lines in
  207. // mMessageLines
  208. int numChars = strlen(mMessage);
  209. const int ScratchBufSize = 2048;
  210. char scratchBuf[ScratchBufSize];
  211. memset(scratchBuf, 0, ScratchBufSize);
  212. int fontDirection, fontAscent, fontDescent;
  213. XCharStruct strInfo;
  214. char *curChar = const_cast<char*>(mMessage);
  215. char *endChar;
  216. char *curWrapped = scratchBuf;
  217. int curWidth = 0;
  218. int maxWidth = mMaxWindowWidth - (MessageBox_LineHMargin);
  219. while ( // while pointers are in range...
  220. (curChar - mMessage) < numChars &&
  221. (curWrapped - scratchBuf) < ScratchBufSize)
  222. {
  223. // look for next space in remaining string
  224. endChar = index(curChar, ' ');
  225. if (endChar == NULL)
  226. endChar = index(curChar, '\0');
  227. if (endChar != NULL)
  228. // increment one char past the space to include it
  229. endChar++;
  230. else
  231. // otherwise, set the endchar to one char ahead
  232. endChar = curChar + 1;
  233. // compute length of substr
  234. int len = endChar - curChar;
  235. XTextExtents(mFS, curChar, len,
  236. &fontDirection, &fontAscent, &fontDescent,
  237. &strInfo);
  238. // if its too big, time to add a new line...
  239. if ((curWidth + strInfo.width) > maxWidth)
  240. {
  241. // create a new block for the line and add it
  242. *curWrapped = '\0';
  243. int len = strlen(scratchBuf);
  244. char* line = new char[len+1];
  245. strncpy(line, scratchBuf, len+1); // +1 gets the null char
  246. mMessageLines.push_back(line);
  247. // reset curWrapped to the beginning of the scratch buffer
  248. curWrapped = scratchBuf;
  249. curWidth = 0;
  250. }
  251. // copy the current string into curWrapped if we have enough room
  252. int bytesRemaining =
  253. ScratchBufSize - (curWrapped - scratchBuf);
  254. if (bytesRemaining >= len)
  255. strncpy(curWrapped, curChar, len);
  256. curWrapped += len;
  257. curWidth += strInfo.width;
  258. curChar = endChar;
  259. }
  260. // make a final line out of any leftover stuff in the scratch buffer
  261. if (curWrapped != scratchBuf)
  262. {
  263. *curWrapped = '\0';
  264. int len = strlen(scratchBuf);
  265. char* line = new char[len+1];
  266. strncpy(line, scratchBuf, len+1); // +1 gets the null char
  267. mMessageLines.push_back(line);
  268. }
  269. }
  270. int XMessageBox::loadFont()
  271. {
  272. // load the font
  273. mFS = XLoadQueryFont(mDisplay,
  274. "-*-helvetica-medium-r-*-*-12-*-*-*-*-*-*-*");
  275. if (mFS == NULL)
  276. mFS = XLoadQueryFont(mDisplay, "fixed");
  277. if (mFS == NULL)
  278. return -1;
  279. // dummy call to XTextExtents to get the font specs
  280. XCharStruct strInfo;
  281. XTextExtents(mFS, "foo", 1,
  282. &mFontDirection, &mFontAscent, &mFontDescent,
  283. &strInfo);
  284. mFontHeight = mFontAscent + mFontDescent;
  285. return 0;
  286. }
  287. int XMessageBox::getButtonLineWidth()
  288. {
  289. return mButtons.size() * MessageBox_ButtonBoxWidth +
  290. (mButtons.size() - 1) * MessageBox_ButtonSpacer +
  291. MessageBox_ButtonHMargin * 2;
  292. }
  293. void XMessageBox::setDimensions()
  294. {
  295. mMBWidth = MessageBox_MaxWinWidth;
  296. mMBHeight = MessageBox_MaxWinHeight;
  297. // determine width of button line
  298. int buttonWidth = getButtonLineWidth();
  299. // if there is only one line, the desired width is the greater of the
  300. // line width and the buttonWidth, otherwise the lineWidth is the
  301. // max possible width which we already set.
  302. if (mMessageLines.size() == 1)
  303. {
  304. XCharStruct strInfo;
  305. int fontDirection, fontAscent, fontDescent;
  306. XTextExtents(mFS, mMessageLines[0], strlen(mMessageLines[0]),
  307. &fontDirection, &fontAscent, &fontDescent,
  308. &strInfo);
  309. mMBWidth = max(MessageBox_LineHMargin * 2 + strInfo.width,
  310. buttonWidth);
  311. mMBWidth = max(mMBWidth, MessageBox_MinWinWidth);
  312. }
  313. // determine the height of the button line
  314. int buttonHeight = MessageBox_ButtonBoxHeight +
  315. MessageBox_ButtonVMargin * 2;
  316. int lineHeight = mFontHeight * mMessageLines.size() +
  317. (mMessageLines.size() - 1) * MessageBox_LineSpacer +
  318. MessageBox_LineVMargin * 2;
  319. mMBHeight = buttonHeight + lineHeight;
  320. }
  321. int XMessageBox::show()
  322. {
  323. if (mDisplay == NULL)
  324. return -1;
  325. int retVal = 0;
  326. retVal = loadFont();
  327. if (retVal < 0)
  328. return retVal;
  329. // set the maximum window dimensions
  330. mScreenWidth = DisplayWidth(mDisplay, DefaultScreen(mDisplay));
  331. mScreenHeight = DisplayHeight(mDisplay, DefaultScreen(mDisplay));
  332. mMaxWindowWidth = min(mScreenWidth, MessageBox_MaxWinWidth);
  333. mMaxWindowHeight = min(mScreenHeight, MessageBox_MaxWinHeight);
  334. // split the message into a vector of lines
  335. splitMessage();
  336. // set the dialog dimensions
  337. setDimensions();
  338. mWin = XCreateSimpleWindow(
  339. mDisplay,
  340. DefaultRootWindow(mDisplay),
  341. (mScreenWidth - mMBWidth) / 2, (mScreenHeight - mMBHeight) / 2,
  342. mMBWidth, mMBHeight,
  343. 1,
  344. BlackPixel(mDisplay, DefaultScreen(mDisplay)),
  345. WhitePixel(mDisplay, DefaultScreen(mDisplay)));
  346. mGC = XCreateGC(mDisplay, mWin, 0, 0);
  347. XSetFont(mDisplay, mGC, mFS->fid);
  348. // set input mask
  349. XSelectInput(mDisplay, mWin,
  350. ExposureMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask);
  351. // set wm protocols in case they hit X
  352. Atom wm_delete_window =
  353. XInternAtom(mDisplay, "WM_DELETE_WINDOW", False);
  354. Atom wm_protocols =
  355. XInternAtom(mDisplay, "WM_PROTOCOLS", False);
  356. XSetWMProtocols (mDisplay, mWin, &wm_delete_window, 1);
  357. // set pop up dialog hint
  358. XSetTransientForHint(mDisplay, mWin, mWin);
  359. // set title
  360. XTextProperty wtitle;
  361. wtitle.value = (unsigned char *)mTitle;
  362. wtitle.encoding = XA_STRING;
  363. wtitle.format = 8;
  364. wtitle.nitems = strlen(mTitle);
  365. XSetWMName(mDisplay, mWin, &wtitle);
  366. // show window
  367. XMapWindow(mDisplay, mWin);
  368. // move it in case some bozo window manager repositioned it
  369. XMoveWindow(mDisplay, mWin,
  370. (mScreenWidth - mMBWidth) / 2, (mScreenHeight - mMBHeight) / 2);
  371. // raise it to top
  372. XRaiseWindow(mDisplay, mWin);
  373. XMessageBoxButton* clickedButton = NULL;
  374. XEvent event;
  375. Vector<XMessageBoxButton>::iterator iter;
  376. bool done = false;
  377. while (!done)
  378. {
  379. XNextEvent(mDisplay, &event);
  380. switch (event.type)
  381. {
  382. case Expose:
  383. repaint();
  384. break;
  385. case MotionNotify:
  386. for (iter = mButtons.begin(); iter != mButtons.end(); ++iter)
  387. iter->setMouseCoordinates(event.xmotion.x, event.xmotion.y);
  388. break;
  389. case ButtonPress:
  390. for (iter = mButtons.begin(); iter != mButtons.end(); ++iter)
  391. {
  392. if (iter->pointInRect(event.xbutton.x, event.xbutton.y))
  393. {
  394. iter->setMouseDown(true);
  395. iter->setMouseCoordinates(event.xbutton.x, event.xbutton.y);
  396. break;
  397. }
  398. }
  399. break;
  400. case ButtonRelease:
  401. for (iter = mButtons.begin(); iter != mButtons.end(); ++iter)
  402. {
  403. if (iter->pointInRect(event.xbutton.x, event.xbutton.y) &&
  404. iter->isMouseDown())
  405. {
  406. // we got a winner!
  407. clickedButton = iter;
  408. done = true;
  409. break;
  410. }
  411. }
  412. if (clickedButton == NULL)
  413. {
  414. // user released outside a button. clear the button states
  415. for (iter = mButtons.begin(); iter != mButtons.end(); ++iter)
  416. iter->setMouseDown(false);
  417. }
  418. break;
  419. case ClientMessage:
  420. if (event.xclient.message_type == wm_protocols &&
  421. event.xclient.data.l[0] == static_cast<long>(wm_delete_window))
  422. done = true;
  423. break;
  424. }
  425. repaint();
  426. }
  427. XUnmapWindow(mDisplay, mWin);
  428. XDestroyWindow(mDisplay, mWin);
  429. XFreeGC(mDisplay, mGC);
  430. XFreeFont(mDisplay, mFS);
  431. if (clickedButton != NULL)
  432. return clickedButton->getClickVal();
  433. else
  434. return -1;
  435. }