example4.e 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. class EXAMPLE4
  2. -- A small app to how build menus and use mnemonics and shortcut keys (also
  3. -- known as Accelerator or Hot Key). The example also show how use an file
  4. -- dialog and a detach box. Notice only some tool bar buttons works.
  5. -- See simple_notepad tutorial to a more complex example.
  6. inherit
  7. IUP_INTERFACE
  8. create {ANY}
  9. make
  10. feature {ANY}
  11. code: INTEGER
  12. make
  13. local
  14. gui: IUP
  15. i: STRING
  16. main_menu, menu1, menu2: IUP_MENU
  17. bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, bt9: IUP_BUTTON
  18. lb1, lb2, lb3: IUP_LABEL
  19. im: IUP_IMAGE
  20. sub1, sub2: IUP_SUBMENU
  21. open, about, exit, size1, size2, size3: IUP_MENU_ITEM
  22. spt: IUP_MENU_SEPARATOR
  23. v: IUP_VBOX
  24. h: IUP_HBOX
  25. db: IUP_DETACH_BOX
  26. restore: IUP_BUTTON
  27. ml: IUP_MULTILINE
  28. w: IUP_DIALOG
  29. do
  30. gui := iup_open
  31. gui.load_images -- Load the predefined images at IUP stock.
  32. -- Get the code of the combination Ctrl+c, that will be used
  33. -- to close the program. Here we should use always uppercase
  34. -- letter, even if the combination use lower.
  35. code := gui.x_key_ctrl('C')
  36. -- Create the image for the "underline" tool-bar button.
  37. -- We should pass the width and height of the image created
  38. -- with the predefined colors we want use (1 is black and 15
  39. -- is white). See IUP_IMAGE class for more info.
  40. create im.image( 20, 20, {ARRAY[INTEGER]} << 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  41. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  42. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  43. 15, 15, 15, 15, 15, 15, 15, 15, 15, 1, 15, 1, 15, 15, 15, 15, 15, 15, 15, 15,
  44. 15, 15, 15, 15, 15, 15, 15, 15, 1, 15, 15, 15, 1, 15, 15, 15, 15, 15, 15, 15,
  45. 15, 15, 15, 15, 15, 15, 15, 15, 1, 15, 15, 15, 1, 15, 15, 15, 15, 15, 15, 15,
  46. 15, 15, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 15,
  47. 15, 15, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 15,
  48. 15, 15, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 15,
  49. 15, 15, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 15,
  50. 15, 15, 15, 15, 15, 15, 15, 1, 1, 1, 1, 1, 1, 1, 15, 15, 15, 15, 15, 15,
  51. 15, 15, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 15,
  52. 15, 15, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 15,
  53. 15, 15, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 15,
  54. 15, 15, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 15,
  55. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  56. 15, 15, 15, 15, 15, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 15, 15, 15, 15,
  57. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  58. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  59. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15 >>)
  60. im.set_widget_name("my_image")
  61. -- Create the menu items.
  62. create open.item("Open...")
  63. open.set_hide_mark(True)
  64. open.set_cb_action(agent open_file)
  65. create spt.separator
  66. create about.item("About...")
  67. about.set_hide_mark(True)
  68. about.set_cb_action(agent show_about)
  69. create exit.item("Close%TCtrl+C")
  70. exit.set_hide_mark(True)
  71. exit.set_cb_action(agent terminate)
  72. create size1.item("14")
  73. size1.set_cb_action(agent set_text_size)
  74. create size2.item("16")
  75. size2.set_cb_action(agent set_text_size)
  76. create size3.item("18")
  77. size3.set_cb_action(agent set_text_size)
  78. -- Create menus 1 and 2
  79. create menu1.menu({ARRAY[IUP_MENU_ELEMENT]} << open, spt, about, exit >>)
  80. create menu2.menu({ARRAY[IUP_MENU_ELEMENT]} << size1, size2, size3 >>)
  81. menu2.set_radio(True)
  82. -- Create submenus
  83. create sub1.submenu("&App", menu1)
  84. create sub2.submenu("Text Size", menu2)
  85. -- Create main menu and set its name
  86. create main_menu.menu({ARRAY[IUP_MENU_ELEMENT]} << sub1, sub2 >>)
  87. main_menu.set_widget_name("Menu")
  88. -- Tool bar
  89. create bt1.button("")
  90. bt1.set_image("IUP_FileNew")
  91. bt1.set_flat(True)
  92. bt1.set_can_focus(False)
  93. create bt2.button("")
  94. bt2.set_image("IUP_FileOpen")
  95. bt2.set_flat(True)
  96. bt2.set_can_focus(False)
  97. create bt3.button("")
  98. bt3.set_image("IUP_FileSave")
  99. bt3.set_flat(True)
  100. bt3.set_can_focus(False)
  101. create lb1.label_empty
  102. lb1.set_vertical_separator
  103. create bt4.button("")
  104. bt4.set_image("IUP_EditUndo")
  105. bt4.set_flat(True)
  106. bt4.set_can_focus(False)
  107. create bt5.button("")
  108. bt5.set_image("IUP_EditRedo")
  109. bt5.set_flat(True)
  110. bt5.set_can_focus(False)
  111. create lb2.label_empty
  112. lb2.set_vertical_separator
  113. create bt6.button("")
  114. bt6.set_image("IUP_EditCut")
  115. bt6.set_flat(True)
  116. bt6.set_can_focus(False)
  117. create bt7.button("")
  118. bt7.set_image("IUP_EditCopy")
  119. bt7.set_flat(True)
  120. bt7.set_can_focus(False)
  121. create bt8.button("")
  122. bt8.set_image("IUP_EditPaste")
  123. bt8.set_flat(True)
  124. bt8.set_can_focus(False)
  125. bt8.set_cb_action(agent paste)
  126. create lb3.label_empty
  127. lb3.set_vertical_separator
  128. create bt9.button("")
  129. bt9.set_flat(True)
  130. bt9.set_can_focus(False)
  131. bt9.set_image("my_image")
  132. bt9.set_cb_action(agent underline)
  133. create restore.button("Restore")
  134. restore.set_name("RESTORE")
  135. restore.set_cb_action(agent restore_bar)
  136. restore.set_visible(False)
  137. -- Create multiline
  138. create ml.multiline
  139. ml.set_name("TEXT")
  140. ml.expand_both_directions
  141. ml.set_formatting(True)
  142. ml.set_font_size(14)
  143. -- Put the buttons inside a horizontal box and inside the detachbox.
  144. create h.hbox({ARRAY[IUP_WIDGET]} << bt1, bt2, bt3, lb1, bt4, bt5, lb2,
  145. bt6, bt7, bt8, lb3, bt9, restore >>)
  146. -- Set a vertical margin (with some themes, the icons at toolbar
  147. -- could overlap the line draw at the bottom of menu).
  148. h.set_margin(0, 8)
  149. create db.detach_box(h)
  150. db.set_name("DB")
  151. db.set_cb_detached(agent detach_bar)
  152. -- Put the detach box and multiline inside a vertical box.
  153. create v.vbox({ARRAY[IUP_WIDGET]} << db, ml >>)
  154. -- Create the window.
  155. create w.dialog(v)
  156. w.set_cb_k_any(agent key)
  157. w.set_title("Simple note pad")
  158. w.set_menu("Menu")
  159. w.set_predefined_size("HALF", "HALF")
  160. i := w.show
  161. gui.main_loop
  162. gui.close
  163. end
  164. restore_bar(widget: IUP_BUTTON): STRING
  165. do
  166. widget.set_visible(False)
  167. if attached {IUP_DETACH_BOX} widget.get_dialog_child("DB") as dbox then
  168. dbox.restore_widget("NULL")
  169. end
  170. Result := "IUP_DEFAULT"
  171. end
  172. detach_bar(wgt: IUP_DETACH_BOX; dlg: IUP_DIALOG; x: INTEGER; y: INTEGER): STRING
  173. do
  174. if attached {IUP_BUTTON} wgt.get_dialog_child("RESTORE") as btn then
  175. btn.set_visible(True)
  176. end
  177. Result := "IUP_DEFAULT"
  178. end
  179. open_file (widget: IUP_MENU_ITEM): STRING
  180. local
  181. status: INTEGER
  182. rv, name: STRING
  183. fd: IUP_FILE_DIALOG
  184. do
  185. create fd.file_dialog
  186. fd.set_dialog_type("OPEN")
  187. fd.set_ext_filter("Text files|*.txt|")
  188. fd.set_title("Select a text file")
  189. rv := fd.popup_predefined_xy("IUP_CENTER", "IUP_CENTER")
  190. if rv.is_equal("IUP_NOERROR") then
  191. status := fd.get_status
  192. -- If user has selected a file, get its name and add this
  193. -- (the name) to the multiline.
  194. if status.is_equal(0) then
  195. name := fd.get_value
  196. else
  197. name := ""
  198. end
  199. fd.destroy -- We should destroy the dialog.
  200. if attached {IUP_MULTILINE} widget.get_dialog_child("TEXT") as text then
  201. text.append(name)
  202. end
  203. end
  204. Result := "IUP_DEFAULT"
  205. end
  206. show_about (widget: IUP_MENU_ITEM): STRING
  207. local
  208. msg: IUP_MESSAGE
  209. do
  210. create msg.message("About", "Simple example with eiffel-iup")
  211. Result := "IUP_DEFAULT"
  212. end
  213. terminate (widget: IUP_MENU_ITEM): STRING
  214. do
  215. Result := "IUP_CLOSE"
  216. end
  217. set_text_size (widget: IUP_MENU_ITEM): STRING
  218. local
  219. title: STRING
  220. do
  221. title := widget.get_title
  222. if attached {IUP_MULTILINE} widget.get_dialog_child("TEXT") as text then
  223. if title.is_equal("14") then
  224. text.set_font_size(14)
  225. elseif title.is_equal("16") then
  226. text.set_font_size(16)
  227. else
  228. text.set_font_size(18)
  229. end
  230. end
  231. Result := "IUP_DEFAULT"
  232. end
  233. key (dlg: IUP_DIALOG; k: INTEGER): STRING
  234. do
  235. if code.is_equal(k) then
  236. Result := "IUP_CLOSE"
  237. else
  238. Result := "IUP_DEFAULT"
  239. end
  240. end
  241. underline (widget: IUP_BUTTON): STRING
  242. local
  243. underline_tag: IUP_FORMATTING
  244. tup: TUPLE[INTEGER, INTEGER]
  245. do
  246. -- Create the formatting tag for underline text.
  247. create underline_tag.formatting
  248. underline_tag.set_underline("SINGLE")
  249. if attached {IUP_MULTILINE} widget.get_dialog_child("TEXT") as text then
  250. -- Get current selection (if any)
  251. tup := text.get_multiline_selection_pos
  252. --underline_tag.set_widget_name("UND")
  253. -- Set selection at tag.
  254. underline_tag.set_selection_pos(tup.integer_32_item(1), tup.integer_32_item(2))
  255. text.add_format_widget(underline_tag)
  256. --ml.add_format_tag("UND")
  257. end
  258. Result := "IUP_DEFAULT"
  259. end
  260. paste (widget: IUP_BUTTON): STRING
  261. local
  262. cp: IUP_CLIPBOARD
  263. s: STRING
  264. do
  265. cp := get_clipboard
  266. if cp.is_text_available then
  267. s := cp.get_text
  268. if attached {IUP_MULTILINE} widget.get_dialog_child("TEXT") as text then
  269. text.append(s)
  270. end
  271. end
  272. Result := "IUP_DEFAULT"
  273. end
  274. end -- class EXAMPLE4