PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/demo/widget_show/demo.cpp03.cpp

http://nanapro.codeplex.com
C++ | 425 lines | 387 code | 31 blank | 7 comment | 15 complexity | 2ab68918e09e3c22cde421505dbe0b6b MD5 | raw file
  1. /*
  2. * This is a demo of Nana C++ Library
  3. * Author: Jinhao
  4. * The demo requires Nana 0.2.5 and C++03 compiler
  5. * Screenshot at http://sourceforge.net/projects/stdex
  6. */
  7. #include <nana/gui/wvl.hpp>
  8. #include <nana/gui/layout.hpp>
  9. #include <nana/gui/widgets/button.hpp>
  10. #include <nana/gui/widgets/categorize.hpp>
  11. #include <nana/gui/widgets/combox.hpp>
  12. #include <nana/gui/widgets/label.hpp>
  13. #include <nana/gui/widgets/progress.hpp>
  14. #include <nana/gui/widgets/tabbar.hpp>
  15. #include <nana/gui/widgets/panel.hpp>
  16. #include <nana/gui/widgets/listbox.hpp>
  17. #include <nana/gui/widgets/treebox.hpp>
  18. #include <nana/gui/widgets/checkbox.hpp>
  19. #include <nana/filesystem/file_iterator.hpp>
  20. #include <nana/gui/widgets/date_chooser.hpp>
  21. #include <nana/gui/widgets/textbox.hpp>
  22. #include <nana/gui/timer.hpp>
  23. #include <nana/gui/tooltip.hpp>
  24. #include <memory>
  25. #include <vector>
  26. namespace demo
  27. {
  28. using namespace nana::gui;
  29. class tab_page_listbox
  30. : public panel<false>
  31. {
  32. public:
  33. tab_page_listbox(window wd)
  34. : panel<false>(wd)
  35. {
  36. gird_.bind(*this);
  37. listbox_.create(*this);
  38. listbox_.append_header(STR("Supported compilers"), 200);
  39. listbox_.append_categ(STR("Nana.C++03"));
  40. listbox_.append_item(1, STR("GCC 3.4 and later"));
  41. listbox_.append_item(1, STR("Visual C++ 2003 and later"));
  42. listbox_.append_categ(STR("Nana.C++11"));
  43. listbox_.append_item(2, STR("GCC 4.6 and later"));
  44. listbox_.append_item(2, STR("Visual C++ 2012 and later"));
  45. checkbox_.create(*this);
  46. checkbox_.caption(STR("Checkable Listbox"));
  47. checkbox_.make_event<events::click>(nana::make_fun(*this, &tab_page_listbox::_m_checked));
  48. gird_.add(listbox_, 0, 0);
  49. gird * vgird = gird_.add(10, 140);
  50. vgird->push(checkbox_, 0, 40);
  51. }
  52. private:
  53. void _m_checked()
  54. {
  55. this->listbox_.checkable(this->checkbox_.checked());
  56. }
  57. private:
  58. gird gird_;
  59. listbox listbox_;
  60. checkbox checkbox_;
  61. };
  62. class tab_page_treebox
  63. : public panel<false>
  64. {
  65. public:
  66. typedef treebox<int>::node_type node_type;
  67. tab_page_treebox(window wd)
  68. : panel<false>(wd)
  69. {
  70. gird_.bind(*this);
  71. treebox_.create(*this);
  72. gird_.push(treebox_, 0, 0);
  73. #if defined(NANA_WINDOWS)
  74. node_type node = treebox_.insert(STR("C:"), STR("Local Drive(C:)"), 0);
  75. nana::filesystem::file_iterator i(STR("C:\\")), end;
  76. #elif defined(NANA_LINUX)
  77. //Use a whitespace for the root key string under Linux
  78. node_type node = treebox_.insert(STR(" "), STR("Root"), 0);
  79. nana::filesystem::file_iterator i(STR("/")), end;
  80. #endif
  81. for(; i != end; ++i)
  82. {
  83. if(false == i->directory) continue;
  84. treebox_.insert(node, i->name, i->name, 0);
  85. break;
  86. }
  87. treebox_.ext_event().expand = nana::make_fun(*this, &tab_page_treebox::_m_expand);
  88. }
  89. private:
  90. void _m_expand(nana::gui::window, node_type node, bool exp)
  91. {
  92. if(!exp) return; //If this is contracted.
  93. nana::string path = treebox_.make_key_path(node, STR("/")) + STR("/");
  94. //Trim the head of whitespace, more portable, because the root
  95. //under Linux is a whitespace
  96. nana::string::size_type path_start_pos = path.find_first_not_of(STR(" "));
  97. if(path_start_pos != nana::string::npos)
  98. path.erase(0, path_start_pos);
  99. //Walk in the path directory for sub directories.
  100. nana::filesystem::file_iterator i(path), end;
  101. for(; i != end; ++i)
  102. {
  103. if(false == i->directory) continue; //If it is not a directory.
  104. node_type child = treebox_.insert(node, i->name, i->name, 0);
  105. if(0 == child) continue;
  106. //Find a directory in child directory, if there is a directory,
  107. //insert it into the child, just insert one node to indicate the
  108. //node has a child and an arrow symbol will be displayed in the
  109. //front of the node.
  110. nana::filesystem::file_iterator u(path + i->name);
  111. for(; u != end; ++u)
  112. {
  113. if(false == u->directory) continue; //If it is not a directory.
  114. treebox_.insert(child, u->name, u->name, 0);
  115. break;
  116. }
  117. }
  118. }
  119. private:
  120. gird gird_;
  121. treebox<int> treebox_;
  122. };
  123. class tab_page_datechooser
  124. : public panel<false>
  125. {
  126. public:
  127. tab_page_datechooser(window wd)
  128. : panel<false>(wd)
  129. {
  130. date_.create(*this, nana::rectangle(10, 10, 260, 200));
  131. textbox_.create(*this, nana::rectangle(280, 10, 170, 23));
  132. textbox_.tip_string(STR("Input a date:"));
  133. }
  134. private:
  135. date_chooser date_;
  136. textbox textbox_;
  137. };
  138. class tab_page_radiogroup
  139. : public panel<false>
  140. {
  141. public:
  142. tab_page_radiogroup(window wd)
  143. : panel<false>(wd)
  144. {
  145. gird_.bind(*this);
  146. gird * gdcontext = gird_.add(10, 0);
  147. gird * gdbox = gdcontext->push(0, 0);
  148. const nana::string str[6] = {
  149. STR("Airbus"), STR("AHTOHOB"),
  150. STR("Boeing"), STR("Bombardier"),
  151. STR("Cessna"), STR("EMBRAER")};
  152. for(int i = 0; i < 6; ++i)
  153. {
  154. box_[i].create(*this);
  155. //Add the checkbox to the radio group. The radio group does not
  156. //manage the life of checkboxs.
  157. group_.add(box_[i]);
  158. gdbox->push(box_[i], 5, 20);
  159. box_[i].caption(str[i]);
  160. box_[i].make_event<events::click>(nana::make_fun(*this, &tab_page_radiogroup::_m_selected));
  161. }
  162. gird_.add(0, 10);
  163. label_.create(*this);
  164. label_.caption(STR("Select an airplane manufacturer"));
  165. gdcontext->push(label_, 10, 20);
  166. categorize_.create(*this);
  167. gdcontext->push(categorize_, 10, 22);
  168. gdcontext->push(0, 10);
  169. std::map<nana::string, std::vector<nana::string> > map;
  170. std::vector<nana::string>* p = &(map[str[0]]);
  171. p->push_back(STR("320"));
  172. p->push_back(STR("330"));
  173. p = &(map[str[1]]);
  174. p->push_back(STR("An-124"));
  175. p->push_back(STR("An-225"));
  176. p = &(map[str[2]]);
  177. p->push_back(STR("737"));
  178. p->push_back(STR("747"));
  179. p->push_back(STR("757"));
  180. p->push_back(STR("767"));
  181. p->push_back(STR("777"));
  182. p->push_back(STR("787"));
  183. p = &(map[str[3]]);
  184. p->push_back(STR("CRJ"));
  185. p->push_back(STR("Dash 8"));
  186. p = &(map[str[4]]);
  187. p->push_back(STR("C-170"));
  188. p->push_back(STR("C-172"));
  189. p = &(map[str[5]]);
  190. p->push_back(STR("ERJ-145"));
  191. p->push_back(STR("E-195"));
  192. for(int i = 0; i < 6; ++i)
  193. {
  194. categorize_.caption(STR("Manufacturer"));
  195. categorize_.insert(str[i], 0);
  196. std::vector<nana::string> & v = map[str[i]];
  197. for(std::vector<nana::string>::iterator i = v.begin(); i != v.end(); ++i)
  198. categorize_.childset(*i, 0);
  199. }
  200. }
  201. private:
  202. void _m_selected()
  203. {
  204. std::size_t index = group_.checked();
  205. nana::string str = box_[index].caption();
  206. label_.caption(STR("You have selected ") + str);
  207. categorize_.caption(STR("Manufacturer\\") + str);
  208. }
  209. private:
  210. gird gird_;
  211. radio_group group_;
  212. checkbox box_[6];
  213. label label_;
  214. categorize<int> categorize_;
  215. };
  216. class widget_show
  217. : public form
  218. {
  219. public:
  220. widget_show()
  221. : form(API::make_center(500, 400), appear::decorate<appear::sizable>())
  222. {
  223. this->caption(STR("This is a demo of Nana C++ Library"));
  224. gird_.bind(*this);
  225. _m_init_buttons();
  226. _m_init_comboxs();
  227. _m_init_labels();
  228. _m_init_progresses();
  229. _m_init_tabbar();
  230. this->make_event<events::unload>(nana::make_fun(*this, &widget_show::_m_ask_for_quit));
  231. }
  232. ~widget_show()
  233. {
  234. for(std::vector<panel<false>*>::iterator i = this->tabpages_.begin(); i != this->tabpages_.end(); ++i)
  235. delete (*i);
  236. }
  237. private:
  238. void _m_ask_for_quit(const eventinfo& ei)
  239. {
  240. msgbox mb(this->handle(), STR("Question"), msgbox::yes_no);
  241. mb.icon(mb.icon_question);
  242. mb<<STR("Are you sure you want to exit the demo?");
  243. ei.unload.cancel = (mb.pick_no == mb());
  244. }
  245. private:
  246. void _m_init_buttons()
  247. {
  248. gird * gdbutton = gird_.push(10, 22);
  249. msgbox mb(*this, STR("Msgbox"));
  250. mb.icon(mb.icon_information);
  251. mb<<STR("Button Clicked");
  252. for(int i = 0; i < 3; ++i)
  253. {
  254. buttons_[i].create(*this);
  255. gdbutton->add(buttons_[i], 10, 0);
  256. buttons_[i].make_event<events::click>(mb);
  257. }
  258. gdbutton->add(0, 10);
  259. button * ptr = &(buttons_[0]);
  260. ptr->caption(STR("Normal Button"));
  261. ptr = &(buttons_[1]);
  262. ptr->icon(STR("image.ico"));
  263. ptr->caption(STR("Button with An Image"));
  264. ptr = &(buttons_[2]);
  265. ptr->caption(STR("Pushed Button"));
  266. ptr->enable_pushed(true);
  267. }
  268. void _m_init_comboxs()
  269. {
  270. gird * gd = gird_.push(10, 24);
  271. for(int i = 0; i < 2; ++i)
  272. {
  273. comboxs_[i].create(*this);
  274. gd->add(comboxs_[i], 10, 0);
  275. comboxs_[i].push_back(STR("Item 0"));
  276. comboxs_[i].push_back(STR("Item 1"));
  277. }
  278. gd->add(0, 10);
  279. combox* ptr = &(comboxs_[0]);
  280. ptr->editable(true);
  281. ptr->caption(STR("This is an editable combox"));
  282. ptr->ext_event().selected = nana::make_fun(*this, &widget_show::_m_combox_selected);
  283. ptr = &(comboxs_[1]);
  284. ptr->caption(STR("This is an uneditable combox"));
  285. ptr->ext_event().selected = nana::make_fun(*this, &widget_show::_m_combox_selected);
  286. }
  287. void _m_combox_selected(combox & cmb)
  288. {
  289. msgbox mb(*this, STR("Item Selected"));
  290. mb.icon(mb.icon_information);
  291. mb<<STR("The item ")<<cmb.option()<<(cmb.editable() ?
  292. STR(" is selected in editable combox") : STR(" is selected in uneditable combox"));
  293. mb();
  294. }
  295. void _m_init_labels()
  296. {
  297. gird * gd = gird_.push(10, 20);
  298. for(int i = 0; i < 2; ++i)
  299. {
  300. labels_[i].create(*this);
  301. gd->add(labels_[i], 10, 0);
  302. }
  303. label * wd = &(labels_[0]);
  304. wd->caption(STR("This is a normal label"));
  305. wd = &(labels_[1]);
  306. wd->format(true);
  307. wd->caption(STR("This is a <bold, color=0xFF0000, font=\"Consolas\">formatted label</>"));
  308. }
  309. void _m_init_progresses()
  310. {
  311. gird * gd = gird_.push(10, 20);
  312. const nana::string tipstr[] = {STR("Unknwon in progress"), STR("Known in progress")};
  313. for(int i = 0; i < 2; ++i)
  314. {
  315. progresses_[i].create(*this);
  316. gd->add(progresses_[i], 10, 0);
  317. progresses_[i].unknown(i == 0); //The first progress is known style, the second is unknown.
  318. tooltip_.set(progresses_[i], tipstr[i]);
  319. }
  320. gd->add(0, 10);
  321. timer_.make_tick(nana::make_fun(*this, &widget_show::_m_timer_tick));
  322. timer_.interval(80);
  323. }
  324. void _m_timer_tick()
  325. {
  326. for(int i = 0; i < 2; ++i)
  327. {
  328. progress * p = &(progresses_[i]);
  329. if(false == p->unknown())
  330. {
  331. if(p->value() == p->amount())
  332. p->value(0);
  333. }
  334. p->inc();
  335. }
  336. }
  337. void _m_init_tabbar()
  338. {
  339. tabbar_.create(*this);
  340. gird_.push(tabbar_, 10, 24);
  341. tabbar_.push_back(STR("listbox"));
  342. tabpages_.push_back(new tab_page_listbox(*this));
  343. tabbar_.push_back(STR("treebox"));
  344. tabpages_.push_back(new tab_page_treebox(*this));
  345. tabbar_.push_back(STR("date_chooser"));
  346. tabpages_.push_back(new tab_page_datechooser(*this));
  347. tabbar_.push_back(STR("radio_group"));
  348. tabpages_.push_back(new tab_page_radiogroup(*this));
  349. gird * gd_tabpage = gird_.push(0, 0);
  350. std::size_t index = 0;
  351. for(std::vector<panel<false>*>::iterator i = tabpages_.begin(); i != tabpages_.end(); ++i)
  352. {
  353. tabbar_.relate(index++, (*i)->handle());
  354. gd_tabpage->fasten((*i)->handle());
  355. }
  356. }
  357. private:
  358. //A gird layout management
  359. gird gird_;
  360. nana::gui::timer timer_;
  361. nana::gui::tooltip tooltip_;
  362. button buttons_[3];
  363. combox comboxs_[2];
  364. label labels_[2];
  365. progress progresses_[2];
  366. tabbar<nana::string> tabbar_;
  367. std::vector<panel<false>*> tabpages_;
  368. };//end class nana_demo
  369. void go()
  370. {
  371. widget_show wdshow;
  372. wdshow.show();
  373. exec();
  374. }
  375. }
  376. int main()
  377. {
  378. demo::go();
  379. }