1
0

msgbox.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include <precomp.h>
  2. #include "msgbox.h"
  3. #include <api/wndmgr/skinwnd.h>
  4. #include <api/wnd/wndclass/guiobjwnd.h>
  5. #include <api/script/scriptguid.h>
  6. #include <api/script/objects/c_script/c_container.h>
  7. #include <api/script/objects/c_script/c_text.h>
  8. #include <api/script/objects/c_script/c_button.h>
  9. _btnstruct msgboxbtns[] =
  10. {
  11. { L"<<", MSGBOX_PREVIOUS },
  12. { L"OK", MSGBOX_OK },
  13. { L"Yes", MSGBOX_YES },
  14. { L"All", MSGBOX_ALL },
  15. { L"No", MSGBOX_NO },
  16. { L">>", MSGBOX_NEXT },
  17. { L"Cancel", MSGBOX_CANCEL },
  18. };
  19. MsgBox::MsgBox(const wchar_t *_text, const wchar_t *_title, int _flags, const wchar_t *notanymore)
  20. {
  21. text = _text;
  22. title = _title;
  23. flags = _flags;
  24. #ifdef WIN32
  25. #ifdef _DEBUG
  26. DebugStringW(L"msgbox: %s: %s", title, text.getValue());
  27. #endif
  28. #endif
  29. notanymore_id = notanymore;
  30. sw = NULL;
  31. if (flags == 0) flags = MSGBOX_OK;
  32. }
  33. MsgBox::~MsgBox()
  34. {
  35. foreach(buttons)
  36. WASABI_API_SKIN->xui_delete(buttons.getfor());
  37. endfor
  38. }
  39. int MsgBox::run()
  40. {
  41. int _r = -1;
  42. #ifdef WASABI_COMPILE_CONFIG
  43. if (!notanymore_id.isempty())
  44. {
  45. StringPrintfW txt(L"msgbox_defaultvalue_%s", notanymore_id);
  46. if ((GetKeyState(VK_SHIFT) & 0x8000))
  47. WASABI_API_CONFIG->setIntPublic(txt, -1);
  48. _r = WASABI_API_CONFIG->getIntPublic(txt, -1);
  49. }
  50. #endif
  51. if (_r == -1)
  52. {
  53. sw = new SkinWnd(L"msgbox.custom.group", L"modal", FALSE, NULL, 1, 1);
  54. if (!sw->getWindow()) return -1;
  55. ifc_window *grp = sw->getWindow();
  56. ifc_window *l = grp->getDesktopParent();
  57. GuiObject *_p = sw->findObject(L"msgbox.custom.group");
  58. //CUT: api_window *p = _p->guiobject_getRootWnd();
  59. C_Container cont(sw->getContainer());
  60. cont.setName(title);
  61. createButtons();
  62. int min_w = reposButtons();
  63. GuiObject *go_txt = sw->findObject(L"msgbox.text");
  64. if (go_txt != NULL)
  65. {
  66. GuiObject *to = go_txt->guiobject_findObject(L"wasabi.text");
  67. GuiObject *gto = sw->findObject(L"text");
  68. if (to != NULL && gto != NULL)
  69. {
  70. go_txt->guiobject_setXmlParam(L"text", text);
  71. C_GuiObject t(*go_txt);
  72. int _w = t.getAutoWidth();
  73. int _h = t.getAutoHeight();
  74. to->guiobject_setXmlParam(L"w", StringPrintfW(_w));
  75. to->guiobject_setXmlParam(L"h", StringPrintfW(_h));
  76. to->guiobject_setXmlParam(L"relatw", L"0");
  77. to->guiobject_setXmlParam(L"relath", L"0");
  78. int x, rx, w, rw;
  79. int y, ry, h, rh;
  80. int gtow, gtoh;
  81. go_txt->guiobject_getGuiPosition(&x, &y, &w, &h, &rx, &ry, &rw, &rh);
  82. if (rw == 1)
  83. _w += -w;
  84. else
  85. _w += x * 2;
  86. if (rh == 1)
  87. _h += -h;
  88. else
  89. _h += y * 2;
  90. gtow = _w;
  91. gtoh = _h;
  92. GuiObject *grpo = grp->getGuiObject();
  93. ASSERT(grpo != NULL);
  94. gto->guiobject_getGuiPosition(&x, &y, &w, &h, &rx, &ry, &rw, &rh);
  95. if (rw == 1)
  96. _w += -w;
  97. else
  98. _w += x * 2;
  99. if (rh == 1)
  100. _h += -h;
  101. else
  102. _h += y * 2;
  103. gto->guiobject_setXmlParam(L"w", StringPrintfW(gtow));
  104. gto->guiobject_setXmlParam(L"h", StringPrintfW(gtoh));
  105. grpo->guiobject_setXmlParam(L"w", StringPrintfW(_w));
  106. grpo->guiobject_setXmlParam(L"h", StringPrintfW(_h));
  107. grpo->guiobject_setXmlParam(L"lockminmax", L"1");
  108. grpo->guiobject_setXmlParam(L"propagatesize", L"1");
  109. XmlObject *xl = static_cast<XmlObject *>(l->getInterface(xmlObjectGuid));
  110. xl->setXmlParam(L"minimum_h", StringPrintfW(L"%d", _h));
  111. xl->setXmlParam(L"minimum_w", StringPrintfW(L"%d", (_w < min_w) ? min_w : _w));
  112. }
  113. }
  114. if (!notanymore_id.isempty())
  115. {
  116. GuiObject *o = WASABI_API_SKIN->xui_new(L"Wasabi:CheckBox"); // that'll be deleted automatically when our parent group destroys
  117. if (o != NULL)
  118. {
  119. ifc_window *w = o->guiobject_getRootWnd();
  120. C_GuiObject go(*o);
  121. go.init(_p->guiobject_getScriptObject());
  122. o->guiobject_setXmlParam(L"text", L"Do not show this message anymore");
  123. o->guiobject_setXmlParam(L"y", L"-50");
  124. o->guiobject_setXmlParam(L"relaty", L"1");
  125. o->guiobject_setXmlParam(L"x", L"12");
  126. o->guiobject_setXmlParam(L"relatx", L"0");
  127. o->guiobject_setXmlParam(L"w", L"-24");
  128. o->guiobject_setXmlParam(L"relatw", L"1");
  129. min_w = MAX(w->getPreferences(SUGGESTED_W), min_w);
  130. }
  131. }
  132. sw->notifyMinMaxChanged();
  133. reposButtons();
  134. _r = sw->runModal(1);
  135. }
  136. #ifdef WASABI_COMPILE_CONFIG
  137. if (!notanymore_id.isempty() && _r != -1 && sw != NULL)
  138. {
  139. GuiObject *o = sw->findObject(L"checkbox.toggle");
  140. if (o != NULL)
  141. {
  142. C_Button b(*o);
  143. if (b.getActivated())
  144. {
  145. StringPrintfW txt(L"msgbox_defaultvalue_%s", notanymore_id);
  146. WASABI_API_CONFIG->setIntPublic(txt, _r);
  147. }
  148. }
  149. }
  150. #endif
  151. if (sw) sw->destroy(); sw = NULL;
  152. return _r;
  153. }
  154. void MsgBox::addButton(const wchar_t *text, int retcode)
  155. {
  156. GuiObject *o = WASABI_API_SKIN->xui_new(L"Wasabi:Button"); // that wil NOT be deleted automatically when our parent group destroys because we did not init with guiobject
  157. if (o != NULL)
  158. {
  159. o->guiobject_setXmlParam(L"action", L"endmodal");
  160. o->guiobject_setXmlParam(L"retcode", StringPrintfW(retcode));
  161. o->guiobject_setXmlParam(L"text", text);
  162. buttons.addItem(o);
  163. }
  164. }
  165. void MsgBox::createButtons()
  166. {
  167. GuiObject *_p = sw->findObject(L"msgbox.custom.group");
  168. if (!_p) return ;
  169. ASSERT(buttons.getNumItems() == 0);
  170. buttons.deleteAll();
  171. for (int i = 0;i < sizeof(msgboxbtns) / sizeof(_btnstruct);i++)
  172. {
  173. if (flags & msgboxbtns[i].id)
  174. {
  175. addButton(msgboxbtns[i].txt, msgboxbtns[i].id);
  176. }
  177. }
  178. ifc_window *p = _p->guiobject_getRootWnd();
  179. foreach(buttons)
  180. ifc_window *wnd = buttons.getfor()->guiobject_getRootWnd();
  181. if (wnd != NULL)
  182. {
  183. wnd->setStartHidden(1);
  184. wnd->setParent(p);
  185. wnd->init(p);
  186. }
  187. endfor;
  188. }
  189. int MsgBox::reposButtons()
  190. {
  191. RECT r;
  192. GuiObject *_p = sw->findObject(L"msgbox.custom.group");
  193. ifc_window *p = _p->guiobject_getRootWnd();
  194. p->getClientRect(&r);
  195. int shift = 0;
  196. //CUT: int _w = 0;
  197. //CUT: int _h = 0;
  198. for (int i = buttons.getNumItems() - 1;i >= 0;i--)
  199. {
  200. ifc_window *wnd = buttons.enumItem(i)->guiobject_getRootWnd();
  201. if (wnd != NULL)
  202. {
  203. int _w = wnd->getPreferences(SUGGESTED_W);
  204. int _h = wnd->getPreferences(SUGGESTED_H);
  205. if (_w == AUTOWH) _w = -1;
  206. int w = MAX(_w, 64);
  207. wnd->resize(r.right - w - 16 - shift, r.bottom - 8 - _h, w, _h);
  208. _w = MAX(w, _w);
  209. _h = MAX(_h, _h);
  210. shift += w + 4;
  211. }
  212. }
  213. foreach(buttons)
  214. ifc_window *wnd = buttons.getfor()->guiobject_getRootWnd();
  215. if (wnd != NULL)
  216. wnd->setVisible(1);
  217. endfor;
  218. return shift;
  219. }