x86UNIXMessageBox.client.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. void XMessageBox::repaint()
  95. {
  96. int white = WhitePixel(mDisplay, DefaultScreen(mDisplay));
  97. int black = BlackPixel(mDisplay, DefaultScreen(mDisplay));
  98. int x = 0;
  99. int y = 0;
  100. // line V margin
  101. y = y + MessageBox_LineVMargin * 2;
  102. // line H margin
  103. x = MessageBox_LineHMargin;
  104. XSetForeground(mDisplay, mGC, black);
  105. for (unsigned int i = 0; i < mMessageLines.size(); ++i)
  106. {
  107. XDrawString(mDisplay, mWin, mGC, x, y, mMessageLines[i],
  108. strlen(mMessageLines[i]));
  109. if (i < (mMessageLines.size() - 1))
  110. y = y + MessageBox_LineSpacer + mFontHeight;
  111. }
  112. XFlush(mDisplay);
  113. // line V margin
  114. y = y + MessageBox_LineVMargin;
  115. int maxButWidth = MessageBox_ButtonBoxWidth;
  116. int maxButHeight = MessageBox_ButtonBoxHeight;
  117. // compute size of text labels on buttons
  118. int fgColor, bgColor;
  119. int fontDirection, fontAscent, fontDescent;
  120. Vector<XMessageBoxButton>::iterator iter;
  121. for (iter = mButtons.begin(); iter != mButtons.end(); ++iter)
  122. {
  123. XCharStruct strInfo;
  124. XTextExtents(mFS, iter->getLabel(), strlen(iter->getLabel()),
  125. &fontDirection, &fontAscent, &fontDescent,
  126. &strInfo);
  127. // if (maxButWidth < strInfo.width)
  128. // maxButWidth = strInfo.width;
  129. // if (maxButHeight < (strInfo.ascent + strInfo.descent))
  130. // maxButHeight = (strInfo.ascent + strInfo.descent);
  131. iter->setLabelWidth(strInfo.width);
  132. }
  133. int buttonBoxWidth = maxButWidth;
  134. int buttonBoxHeight = maxButHeight;
  135. // draw buttons
  136. // button V margin
  137. y = y + MessageBox_ButtonVMargin;
  138. // center the buttons
  139. x = MessageBox_ButtonHMargin + (mMBWidth - getButtonLineWidth()) / 2;
  140. for (iter = mButtons.begin(); iter != mButtons.end(); ++iter)
  141. {
  142. if (iter->drawReverse())
  143. {
  144. fgColor = white;
  145. bgColor = black;
  146. }
  147. else
  148. {
  149. fgColor = black;
  150. bgColor = white;
  151. }
  152. XSetForeground(mDisplay, mGC, bgColor);
  153. XFillRectangle(mDisplay, mWin, mGC, x, y,
  154. buttonBoxWidth, buttonBoxHeight);
  155. XSetForeground(mDisplay, mGC, fgColor);
  156. XDrawRectangle(mDisplay, mWin, mGC, x, y,
  157. buttonBoxWidth, buttonBoxHeight);
  158. XDrawString(mDisplay, mWin, mGC,
  159. x + ((buttonBoxWidth - iter->getLabelWidth()) / 2),
  160. y + mFontAscent + ((buttonBoxHeight - mFontAscent) / 2),
  161. iter->getLabel(),
  162. strlen(iter->getLabel()));
  163. iter->setButtonRect(x, y, buttonBoxWidth, buttonBoxHeight);
  164. x = x + buttonBoxWidth + MessageBox_ButtonSpacer;
  165. }
  166. }
  167. template <class Type>
  168. static inline Type max(Type v1, Type v2)
  169. {
  170. if (v1 <= v2)
  171. return v2;
  172. else
  173. return v1;
  174. }
  175. template <class Type>
  176. static inline Type min(Type v1, Type v2)
  177. {
  178. if (v1 > v2)
  179. return v2;
  180. else
  181. return v1;
  182. }
  183. void XMessageBox::clearMessageLines()
  184. {
  185. Vector<char*>::iterator iter;
  186. for (iter = mMessageLines.begin(); iter != mMessageLines.end(); ++iter)
  187. delete [] *iter;
  188. mMessageLines.clear();
  189. }
  190. void XMessageBox::splitMessage()
  191. {
  192. clearMessageLines();
  193. if (mMessage == NULL || strlen(mMessage)==0)
  194. // JMQTODO: what to do with empty strings?
  195. return;
  196. // need to break message up in to lines, and store lines in
  197. // mMessageLines
  198. int numChars = strlen(mMessage);
  199. const int ScratchBufSize = 2048;
  200. char scratchBuf[ScratchBufSize];
  201. memset(scratchBuf, 0, ScratchBufSize);
  202. int fontDirection, fontAscent, fontDescent;
  203. XCharStruct strInfo;
  204. char *curChar = const_cast<char*>(mMessage);
  205. char *endChar;
  206. char *curWrapped = scratchBuf;
  207. int curWidth = 0;
  208. int maxWidth = mMaxWindowWidth - (MessageBox_LineHMargin);
  209. while ( // while pointers are in range...
  210. (curChar - mMessage) < numChars &&
  211. (curWrapped - scratchBuf) < ScratchBufSize)
  212. {
  213. // look for next space in remaining string
  214. endChar = index(curChar, ' ');
  215. if (endChar == NULL)
  216. endChar = index(curChar, '\0');
  217. if (endChar != NULL)
  218. // increment one char past the space to include it
  219. endChar++;
  220. else
  221. // otherwise, set the endchar to one char ahead
  222. endChar = curChar + 1;
  223. // compute length of substr
  224. int len = endChar - curChar;
  225. XTextExtents(mFS, curChar, len,
  226. &fontDirection, &fontAscent, &fontDescent,
  227. &strInfo);
  228. // if its too big, time to add a new line...
  229. if ((curWidth + strInfo.width) > maxWidth)
  230. {
  231. // create a new block for the line and add it
  232. *curWrapped = '\0';
  233. int len = strlen(scratchBuf);
  234. char* line = new char[len+1];
  235. strncpy(line, scratchBuf, len+1); // +1 gets the null char
  236. mMessageLines.push_back(line);
  237. // reset curWrapped to the beginning of the scratch buffer
  238. curWrapped = scratchBuf;
  239. curWidth = 0;
  240. }
  241. // copy the current string into curWrapped if we have enough room
  242. int bytesRemaining =
  243. ScratchBufSize - (curWrapped - scratchBuf);
  244. if (bytesRemaining >= len)
  245. strncpy(curWrapped, curChar, len);
  246. curWrapped += len;
  247. curWidth += strInfo.width;
  248. curChar = endChar;
  249. }
  250. // make a final line out of any leftover stuff in the scratch buffer
  251. if (curWrapped != scratchBuf)
  252. {
  253. *curWrapped = '\0';
  254. int len = strlen(scratchBuf);
  255. char* line = new char[len+1];
  256. strncpy(line, scratchBuf, len+1); // +1 gets the null char
  257. mMessageLines.push_back(line);
  258. }
  259. }
  260. int XMessageBox::loadFont()
  261. {
  262. // load the font
  263. mFS = XLoadQueryFont(mDisplay,
  264. "-*-helvetica-medium-r-*-*-12-*-*-*-*-*-*-*");
  265. if (mFS == NULL)
  266. mFS = XLoadQueryFont(mDisplay, "fixed");
  267. if (mFS == NULL)
  268. return -1;
  269. // dummy call to XTextExtents to get the font specs
  270. XCharStruct strInfo;
  271. XTextExtents(mFS, "foo", 1,
  272. &mFontDirection, &mFontAscent, &mFontDescent,
  273. &strInfo);
  274. mFontHeight = mFontAscent + mFontDescent;
  275. return 0;
  276. }
  277. int XMessageBox::getButtonLineWidth()
  278. {
  279. return mButtons.size() * MessageBox_ButtonBoxWidth +
  280. (mButtons.size() - 1) * MessageBox_ButtonSpacer +
  281. MessageBox_ButtonHMargin * 2;
  282. }
  283. void XMessageBox::setDimensions()
  284. {
  285. mMBWidth = MessageBox_MaxWinWidth;
  286. mMBHeight = MessageBox_MaxWinHeight;
  287. // determine width of button line
  288. int buttonWidth = getButtonLineWidth();
  289. // if there is only one line, the desired width is the greater of the
  290. // line width and the buttonWidth, otherwise the lineWidth is the
  291. // max possible width which we already set.
  292. if (mMessageLines.size() == 1)
  293. {
  294. XCharStruct strInfo;
  295. int fontDirection, fontAscent, fontDescent;
  296. XTextExtents(mFS, mMessageLines[0], strlen(mMessageLines[0]),
  297. &fontDirection, &fontAscent, &fontDescent,
  298. &strInfo);
  299. mMBWidth = max(MessageBox_LineHMargin * 2 + strInfo.width,
  300. buttonWidth);
  301. mMBWidth = max(mMBWidth, MessageBox_MinWinWidth);
  302. }
  303. // determine the height of the button line
  304. int buttonHeight = MessageBox_ButtonBoxHeight +
  305. MessageBox_ButtonVMargin * 2;
  306. int lineHeight = mFontHeight * mMessageLines.size() +
  307. (mMessageLines.size() - 1) * MessageBox_LineSpacer +
  308. MessageBox_LineVMargin * 2;
  309. mMBHeight = buttonHeight + lineHeight;
  310. }
  311. int XMessageBox::show()
  312. {
  313. if (mDisplay == NULL)
  314. return -1;
  315. int retVal = 0;
  316. retVal = loadFont();
  317. if (retVal < 0)
  318. return retVal;
  319. // set the maximum window dimensions
  320. mScreenWidth = DisplayWidth(mDisplay, DefaultScreen(mDisplay));
  321. mScreenHeight = DisplayHeight(mDisplay, DefaultScreen(mDisplay));
  322. mMaxWindowWidth = min(mScreenWidth, MessageBox_MaxWinWidth);
  323. mMaxWindowHeight = min(mScreenHeight, MessageBox_MaxWinHeight);
  324. // split the message into a vector of lines
  325. splitMessage();
  326. // set the dialog dimensions
  327. setDimensions();
  328. mWin = XCreateSimpleWindow(
  329. mDisplay,
  330. DefaultRootWindow(mDisplay),
  331. (mScreenWidth - mMBWidth) / 2, (mScreenHeight - mMBHeight) / 2,
  332. mMBWidth, mMBHeight,
  333. 1,
  334. BlackPixel(mDisplay, DefaultScreen(mDisplay)),
  335. WhitePixel(mDisplay, DefaultScreen(mDisplay)));
  336. mGC = XCreateGC(mDisplay, mWin, 0, 0);
  337. XSetFont(mDisplay, mGC, mFS->fid);
  338. // set input mask
  339. XSelectInput(mDisplay, mWin,
  340. ExposureMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask);
  341. // set wm protocols in case they hit X
  342. Atom wm_delete_window =
  343. XInternAtom(mDisplay, "WM_DELETE_WINDOW", False);
  344. Atom wm_protocols =
  345. XInternAtom(mDisplay, "WM_PROTOCOLS", False);
  346. XSetWMProtocols (mDisplay, mWin, &wm_delete_window, 1);
  347. // set pop up dialog hint
  348. XSetTransientForHint(mDisplay, mWin, mWin);
  349. // set title
  350. XTextProperty wtitle;
  351. wtitle.value = (unsigned char *)mTitle;
  352. wtitle.encoding = XA_STRING;
  353. wtitle.format = 8;
  354. wtitle.nitems = strlen(mTitle);
  355. XSetWMName(mDisplay, mWin, &wtitle);
  356. // show window
  357. XMapWindow(mDisplay, mWin);
  358. // move it in case some bozo window manager repositioned it
  359. XMoveWindow(mDisplay, mWin,
  360. (mScreenWidth - mMBWidth) / 2, (mScreenHeight - mMBHeight) / 2);
  361. // raise it to top
  362. XRaiseWindow(mDisplay, mWin);
  363. XMessageBoxButton* clickedButton = NULL;
  364. XEvent event;
  365. Vector<XMessageBoxButton>::iterator iter;
  366. bool done = false;
  367. while (!done)
  368. {
  369. XNextEvent(mDisplay, &event);
  370. switch (event.type)
  371. {
  372. case Expose:
  373. repaint();
  374. break;
  375. case MotionNotify:
  376. for (iter = mButtons.begin(); iter != mButtons.end(); ++iter)
  377. iter->setMouseCoordinates(event.xmotion.x, event.xmotion.y);
  378. break;
  379. case ButtonPress:
  380. for (iter = mButtons.begin(); iter != mButtons.end(); ++iter)
  381. {
  382. if (iter->pointInRect(event.xbutton.x, event.xbutton.y))
  383. {
  384. iter->setMouseDown(true);
  385. iter->setMouseCoordinates(event.xbutton.x, event.xbutton.y);
  386. break;
  387. }
  388. }
  389. break;
  390. case ButtonRelease:
  391. for (iter = mButtons.begin(); iter != mButtons.end(); ++iter)
  392. {
  393. if (iter->pointInRect(event.xbutton.x, event.xbutton.y) &&
  394. iter->isMouseDown())
  395. {
  396. // we got a winner!
  397. clickedButton = iter;
  398. done = true;
  399. break;
  400. }
  401. }
  402. if (clickedButton == NULL)
  403. {
  404. // user released outside a button. clear the button states
  405. for (iter = mButtons.begin(); iter != mButtons.end(); ++iter)
  406. iter->setMouseDown(false);
  407. }
  408. break;
  409. case ClientMessage:
  410. if (event.xclient.message_type == wm_protocols &&
  411. event.xclient.data.l[0] == static_cast<long>(wm_delete_window))
  412. done = true;
  413. break;
  414. }
  415. repaint();
  416. }
  417. XUnmapWindow(mDisplay, mWin);
  418. XDestroyWindow(mDisplay, mWin);
  419. XFreeGC(mDisplay, mGC);
  420. XFreeFont(mDisplay, mFS);
  421. if (clickedButton != NULL)
  422. return clickedButton->getClickVal();
  423. else
  424. return -1;
  425. }