example4.e 8.4 KB

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