123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- class EXAMPLE4
- -- A small app to how build menus and use mnemonics and shortcut keys (also
- -- known as Accelerator or Hot Key). The example also show how use an file
- -- dialog and a detach box.
- insert
- IUP_INTERFACE
- create {ANY}
- make
- feature {ANY}
- ml: IUP_MULTILINE
- db: IUP_DETACH_BOX
- restore: IUP_BUTTON
- code: INTEGER
-
- make
- local
- gui: IUP
- i: STRING
- main_menu, menu1, menu2: IUP_MENU
- bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, bt9: IUP_BUTTON
- lb1, lb2, lb3: IUP_LABEL
- im: IUP_IMAGE
- sub1, sub2: IUP_SUBMENU
- open, about, exit, size1, size2, size3: IUP_MENU_ITEM
- spt: IUP_MENU_SEPARATOR
- v: IUP_VBOX
- h: IUP_HBOX
- w: IUP_DIALOG
- do
- gui := iup_open
- gui.load_images -- Load the predefined images at IUP stock.
- -- Get the code of the combination Ctrl+c, that will be used
- -- to close the program. Here we should use always uppercase
- -- letter, even if the combination use lower.
- code := gui.x_key_ctrl('C')
- -- Create the image for the "underline" tool-bar button.
- 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;
- 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15;
- 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 15, 15, 15, 15;
- 15, 15, 15, 15, 15, 15, 15, 15, 15, 1, 15, 1, 15, 15, 15, 15, 15, 15, 15, 15;
- 15, 15, 15, 15, 15, 15, 15, 15, 1, 15, 15, 15, 1, 15, 15, 15, 15, 15, 15, 15;
- 15, 15, 15, 15, 15, 15, 15, 15, 1, 15, 15, 15, 1, 15, 15, 15, 15, 15, 15, 15;
- 15, 15, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 15;
- 15, 15, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 15;
- 15, 15, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 15;
- 15, 15, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 15;
- 15, 15, 15, 15, 15, 15, 15, 1, 1, 1, 1, 1, 1, 1, 15, 15, 15, 15, 15, 15;
- 15, 15, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 15;
- 15, 15, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 15;
- 15, 15, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 15;
- 15, 15, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 1, 15, 15, 15, 15, 15, 15;
- 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15;
- 15, 15, 15, 15, 15, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 15, 15, 15, 15;
- 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15;
- 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15;
- 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15 >>})
- im.set_widget_name("my_image")
- -- Create the menu items.
- create open.item("Open...")
- open.set_hide_mark(True)
- open.set_cb_action(agent open_file(?))
- create spt.separator
-
- create about.item("About...")
- about.set_hide_mark(True)
- about.set_cb_action(agent show_about(?))
- create exit.item("Close%TCtrl+C")
- exit.set_hide_mark(True)
- exit.set_cb_action(agent terminate(?))
- create size1.item("14")
- size1.set_cb_action(agent set_text_size(?))
- create size2.item("16")
- size2.set_cb_action(agent set_text_size(?))
- create size3.item("18")
- size3.set_cb_action(agent set_text_size(?))
- -- Create menus 1 and 2
- create menu1.menu({ARRAY[IUP_MENU_ELEMENT] 1, << open, spt,
- about, exit >>})
- create menu2.menu({ARRAY[IUP_MENU_ELEMENT] 1, << size1, size2,
- size3 >>})
- menu2.set_radio(True)
- -- Create submenus
- create sub1.submenu("&App", menu1)
- create sub2.submenu("Text Size", menu2)
- -- Create main menu and set its name
- create main_menu.menu({ARRAY[IUP_MENU_ELEMENT] 1, << sub1, sub2 >>})
- main_menu.set_widget_name("Menu")
- -- Tool bar
- create bt1.button("")
- bt1.set_image("IUP_FileNew")
- bt1.set_flat(True)
- bt1.set_can_focus(False)
- create bt2.button("")
- bt2.set_image("IUP_FileOpen")
- bt2.set_flat(True)
- bt2.set_can_focus(False)
- create bt3.button("")
- bt3.set_image("IUP_FileSave")
- bt3.set_flat(True)
- bt3.set_can_focus(False)
- create lb1.label_empty
- lb1.set_vertical_separator
- create bt4.button("")
- bt4.set_image("IUP_EditUndo")
- bt4.set_flat(True)
- bt4.set_can_focus(False)
- create bt5.button("")
- bt5.set_image("IUP_EditRedo")
- bt5.set_flat(True)
- bt5.set_can_focus(False)
- create lb2.label_empty
- lb2.set_vertical_separator
- create bt6.button("")
- bt6.set_image("IUP_EditCut")
- bt6.set_flat(True)
- bt6.set_can_focus(False)
- create bt7.button("")
- bt7.set_image("IUP_EditCopy")
- bt7.set_flat(True)
- bt7.set_can_focus(False)
- create bt8.button("")
- bt8.set_image("IUP_EditPaste")
- bt8.set_flat(True)
- bt8.set_can_focus(False)
- create lb3.label_empty
- lb3.set_vertical_separator
- create bt9.button("")
- bt9.set_flat(True)
- bt9.set_can_focus(False)
- bt9.set_image("my_image")
- bt9.set_cb_action(agent underline(?))
- create restore.button("Restore")
- restore.set_cb_action(agent restore_bar(?))
- restore.set_visible(False)
- -- Create multiline
- create ml.multiline
- ml.set_expand("YES")
- ml.set_formatting(True)
- ml.set_font_size(14)
- -- Put the buttons inside a horizontal box and inside the detachbox.
- create h.hbox({ARRAY[IUP_WIDGET] 1, << bt1, bt2, bt3, lb1, bt4, bt5, lb2,
- bt6, bt7, bt8, lb3, bt9, restore >>})
-
- -- Set a vertical margin (with some themes, the icons at toolbar
- -- could overlap the line draw at the bottom of menu).
- h.set_margin(0, 8)
- create db.detach_box(h)
- db.set_cb_detached(agent detach_bar(?, ?, ?, ?))
- -- Put the detach box and multiline inside a vertical box.
- create v.vbox({ARRAY[IUP_WIDGET] 1, << db, ml >>})
- -- Create the window.
- create w.dialog(v)
- w.set_cb_k_any(agent key(?,?))
- w.set_title("Simple note pad")
- w.set_menu("Menu")
- w.set_predefined_size("HALF", "HALF")
- i := w.show
- gui.main_loop
- gui.close
- end
- restore_bar(widget: IUP_BUTTON): STRING
- do
- restore.set_visible(False)
- db.restore_widget("NULL")
- Result := "IUP_DEFAULT"
- end
- detach_bar(wgt: IUP_DETACH_BOX; dlg: IUP_DIALOG; x: INTEGER; y: INTEGER): STRING
- do
- restore.set_visible(True)
- Result := "IUP_DEFAULT"
- end
- open_file (widget: IUP_MENU_ITEM): STRING
- local
- status: INTEGER
- rv, name: STRING
- fd: IUP_FILE_DIALOG
- do
- create fd.file_dialog
- fd.set_dialog_type("OPEN")
- fd.set_ext_filter("Text files|*.txt|")
- fd.set_title("Select a text file")
- rv := fd.popup_predefined_xy("IUP_CENTER", "IUP_CENTER")
- if rv.is_equal("IUP_NOERROR") then
- status := fd.get_status
- -- If user has selected a file, get its name and add this
- -- (the name) to the multiline.
- if status.is_equal(0) then
- name := fd.get_value
- end
- fd.destroy -- We should destroy the dialog.
- ml.append(name)
- end
- end
- show_about (widget: IUP_MENU_ITEM): STRING
- local
- msg: IUP_MESSAGE
- do
- create msg.message("About", "Simple example with eiffel-iup")
- Result := "IUP_DEFAULT"
- end
- terminate (widget: IUP_MENU_ITEM): STRING
- do
- Result := "IUP_CLOSE"
- end
- set_text_size (widget: IUP_MENU_ITEM): STRING
- local
- title: STRING
- do
- title := widget.get_title
- if title.is_equal("14") then
- ml.set_font_size(14)
- elseif title.is_equal("16") then
- ml.set_font_size(16)
- else
- ml.set_font_size(18)
- end
-
- Result := "IUP_DEFAULT"
- end
- key (dlg: IUP_DIALOG; k: INTEGER): STRING
- do
- if code.is_equal(k) then
- Result := "IUP_CLOSE"
- else
- Result := "IUP_DEFAULT"
- end
- end
- underline (widget: IUP_BUTTON): STRING
- local
- underline_tag: IUP_FORMATTING
- tup: TUPLE[INTEGER, INTEGER]
- do
- -- Create the formatting tag for underline text.
- create underline_tag.formatting
- underline_tag.set_underline("SINGLE")
- -- Get current selection (if any)
- tup := ml.get_multiline_selection_pos
- -- Set selection at tag.
- underline_tag.set_selection_pos(tup.item_1, tup.item_2)
-
- ml.add_format_widget(underline_tag)
- Result := "IUP_DEFAULT"
- end
- end -- class EXAMPLE4
|