123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- class SIMPLE_NOTEPAD_4
- insert
- IUP_INTERFACE
- create {ANY}
- make
- feature {ANY}
- ml: IUP_MULTILINE
- str_find (str, str_to_find, casesensitive: STRING; pos: INTEGER): INTEGER
- do
- if str.count.is_equal(0) or str_to_find.count.is_equal(0) then
- Result := -1
- elseif casesensitive.is_equal("OFF") then
- str.to_lower
- str_to_find.to_lower
- end
- Result := str.substring_index(str_to_find, pos)
- end
- read_file (file_name: STRING): STRING
- local
- tfr: TEXT_FILE_READ
- str: STRING
- ms: IUP_MESSAGE
- do
- create str.make_empty
- create tfr.connect_to(file_name)
- if tfr.is_connected then
- from
- tfr.read_line
- until
- tfr.end_of_input
- loop
- str.append(tfr.last_string)
- str.append_character('%N')
- tfr.read_line
- end
-
- str.append(tfr.last_string)
- tfr.disconnect
- else
- str.append("Fail when reading from file: ")
- str.append(file_name)
- create ms.message("Error", str)
- end
-
- Result := str
- end
- write_file (file_name, text: STRING)
- local
- tfw: TEXT_FILE_WRITE
- ms: IUP_MESSAGE
- str: STRING
- do
- create tfw.connect_to(file_name)
- if tfw.is_connected then
- tfw.put_string(text)
- tfw.disconnect
- else
- str.make_empty
- str.append("Fail when writing to file: ")
- str.append(file_name)
- create ms.message("Error", str)
- end
- end
- --********************************** Callbacks *****************************************
- item_open_action_cb (widget: IUP_MENU_ITEM): STRING
- local
- status: INTEGER
- rv, name, text: STRING
- fd: IUP_FILE_DIALOG
- do
- create fd.file_dialog
- fd.set_dialog_type("OPEN")
- fd.set_ext_filter("Text files|*.txt|All Files|*.*|")
- 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 status.is_equal(0) then
- name := fd.get_value
- text := read_file(name)
- ml.set_value(text)
- end
- end
- fd.destroy
- Result := "IUP_DEFAULT"
- end
- item_saveas_action_cb (widget: IUP_MENU_ITEM): STRING
- local
- status: INTEGER
- rv, name, text: STRING
- fd: IUP_FILE_DIALOG
- do
- create fd.file_dialog
- fd.set_dialog_type("SAVE")
- fd.set_ext_filter("Text files|*.txt|All Files|*.*|")
- 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 not status.is_equal(-1) then
- name := fd.get_value
- text := ml.get_value
- write_file(name, text)
- end
- end
- fd.destroy
- Result := "IUP_DEFAULT"
- end
- item_font_action_cb (widget: IUP_MENU_ITEM): STRING
- local
- status: INTEGER
- rv, ft, selected: STRING
- fd: IUP_FONT_DIALOG
- do
- create fd.font_dialog
- ft := ml.get_font
- fd.set_value(ft)
- rv := fd.popup_predefined_xy("IUP_CENTER", "IUP_CENTER")
- if rv.is_equal("IUP_NOERROR") then
- status := fd.get_status
- if status.is_equal(1) then
- selected := fd.get_value
- ml.set_font(selected)
- end
- end
- fd.destroy
- Result := "IUP_DEFAULT"
- end
- item_about_action_cb (widget: IUP_MENU_ITEM): STRING
- local
- ms: IUP_MESSAGE
- do
- create ms.message("About"," Simple Notepad%N%NAutors:%N Gustavo Lyrio%N Antonio Scuri")
- Result := "IUP_DEFAULT"
- end
- item_exit_action_cb (widget: IUP_MENU_ITEM): STRING
- do
- Result := "IUP_CLOSE"
- end
- goto_ok_action_cb(widget: IUP_BUTTON): STRING
- local
- line_count, line: INTEGER
- txt: IUP_TEXT
- ms: IUP_MESSAGE
- do
- if txt ?:= widget.get_dialog_child("LINE_TEXT") then
- txt ::= widget.get_dialog_child("LINE_TEXT")
- line_count := widget.get_attribute_integer("TEXT_LINECOUNT")
- line := txt.get_value.to_integer
- if line < 1 and line >= line_count then
- create ms.message("Error", "Invalid line number.")
- Result := "IUP_DEFAULT"
- end
- widget.get_dialog.set_attribute_integer("STATUS", 1)
- Result := "IUP_CLOSE"
- end
- end
- goto_cancel_action_cb(widget: IUP_BUTTON): STRING
- do
- widget.get_dialog.set_attribute_integer("STATUS", 0)
- Result := "IUP_CLOSE"
- end
- item_goto_action_cb (widget: IUP_MENU_ITEM): STRING
- local
- dlg: IUP_DIALOG
- vbox: IUP_VBOX
- hbox: IUP_HBOX
- bt_ok, bt_cancel: IUP_BUTTON
- f: IUP_FILL
- lbl: IUP_LABEL
- txt: IUP_TEXT
- str, s: STRING
- line_count, line, pos: INTEGER
- do
- line_count := ml.get_line_count
- str := "Line Number [1-"
- str.append_string(ml.get_line_count.to_string)
- str.append_string("]:")
- create lbl.label(str)
- create txt.text
- txt.set_mask("IUP_MASK_UINT") -- unsigned integer numbers only
- txt.set_name("LINE_TEXT")
- txt.set_visible_columns(20)
- create bt_ok.button("OK")
- bt_ok.set_attribute_integer("TEXT_LINECOUNT", line_count)
- bt_ok.set_padding(10, 2)
- bt_ok.set_cb_action(agent goto_ok_action_cb(?))
- create bt_cancel.button("Cancel")
- bt_cancel.set_padding(10, 2)
- bt_cancel.set_cb_action(agent goto_cancel_action_cb(?))
- create f.fill
- create hbox.hbox({ARRAY[IUP_WIDGET] 1, << f, bt_ok, bt_cancel >>})
- hbox.set_normalize_size("HORIZONTAL")
- create vbox.vbox({ARRAY[IUP_WIDGET] 1, << lbl, txt, hbox >>})
- vbox.set_margin(10, 10)
- vbox.set_gap(5)
- create dlg.dialog(vbox)
- dlg.set_title("Go To Line")
- dlg.set_modal_dialog_frame(True)
- dlg.set_default_enter_widget(bt_ok)
- dlg.set_default_escape_widget(bt_cancel)
- dlg.set_parent_dialog_widget(widget.get_dialog)
- s := dlg.popup_predefined_xy ("IUP_CENTERPARENT", "IUP_CENTERPARENT")
- if s.is_equal("IUP_NOERROR") and dlg.get_attribute_integer("STATUS").is_equal(1) then
- line := txt.get_value.to_integer
- pos := ml.text_convert_lin_col_to_pos(line, 0)
- ml.set_multiline_caret_pos(pos)
- ml.scroll_to_pos(pos)
- end
- dlg.destroy
-
- Result := "IUP_DEFAULT"
- end
- find_next_action_cb (widget: IUP_BUTTON): STRING
- local
- str, str_to_find, casesensitive: STRING
- find_pos, pos, end_pos: INTEGER
- txt: IUP_TEXT
- find_case: IUP_TOGGLE
- tup: TUPLE[INTEGER, INTEGER]
- pw: IUP_WIDGET
- ms: IUP_MESSAGE
- do
- str := ml.get_value
- find_pos := ml.get_attribute_integer("FIND_POS")
- if find_pos = 0 then
- find_pos := 1
- end
- if txt ?:= widget.get_dialog_child("FIND_TEXT") then
- txt ::= widget.get_dialog_child("FIND_TEXT")
- str_to_find := txt.get_value
- if find_case ?:= widget.get_dialog_child("FIND_CASE") then
- find_case ::= widget.get_dialog_child("FIND_CASE")
- casesensitive := find_case.get_value
- -- Rest 1 since the string start at 1, but multiline position
- -- start at 0.
- pos := str_find(str, str_to_find, casesensitive, find_pos) - 1
- if pos < 0 and find_pos > 0 then
- -- try again from the start
- pos := str_find(str, str_to_find, casesensitive, 1) - 1
- end
- if pos > 0 then
- end_pos := pos + str_to_find.count
- ml.set_attribute_integer("FIND_POS", end_pos)
- pw := ml.set_focus
- ml.set_multiline_selection_pos(pos, end_pos)
- tup := ml.text_convert_pos_to_lin_col(pos)
- -- position at col=0, just scroll lines
- pos := ml.text_convert_lin_col_to_pos(tup.item_1, 0)
- ml.scroll_to_pos(pos)
- else
- create ms.message("Warning", "Text not found.")
- end
- end
-
- end
-
- Result := "IUP_DEFAULT"
- end
- find_close_action_cb (widget: IUP_BUTTON): STRING
- do
- widget.get_dialog.hide
- Result := "IUP_DEFAULT"
- end
- item_find_action_cb (widget: IUP_MENU_ITEM): STRING
- local
- dlg: IUP_DIALOG
- lbl: IUP_LABEL
- txt: IUP_TEXT
- bt_next, bt_close: IUP_BUTTON
- find_case: IUP_TOGGLE
- f: IUP_FILL
- hbox: IUP_HBOX
- vbox: IUP_VBOX
- s: STRING
- do
- if dlg ?:= widget.get_attribute_widget("FIND_DIALOG") then
- dlg ::= widget.get_attribute_widget("FIND_DIALOG")
- end
- if dlg = Void then
- create lbl.label("Find What:")
- create txt.text
- txt.set_name("FIND_TEXT")
- txt.set_visible_columns(20)
- create find_case.toggle("Case Sensitive")
- find_case.set_name("FIND_CASE")
- create bt_next.button("Find Next")
- bt_next.set_padding(10, 2)
- bt_next.set_cb_action(agent find_next_action_cb(?))
- create bt_close.button("Close")
- bt_close.set_padding(10, 2)
- bt_close.set_cb_action(agent find_close_action_cb(?))
- create f.fill
- create hbox.hbox({ARRAY[IUP_WIDGET] 1, << f, bt_next, bt_close >>})
- hbox.set_normalize_size("HORIZONTAL")
- create vbox.vbox({ARRAY[IUP_WIDGET] 1, << lbl, txt, find_case, hbox >>})
- vbox.set_margin(10, 10)
- vbox.set_gap(10)
- create dlg.dialog(vbox)
- dlg.set_title("Find")
- dlg.set_modal_dialog_frame(True)
- dlg.set_default_enter_widget(bt_next)
- dlg.set_default_escape_widget(bt_close)
- dlg.set_parent_dialog_widget(widget.get_dialog)
- -- Save the dialog to reuse it
- widget.set_attribute_widget("FIND_DIALOG", dlg)
- end
-
- -- centerparent first time, next time reuse the last position
- s := dlg.show_predefined_xy("IUP_CURRENT", "IUP_CURRENT")
- Result := "IUP_DEFAULT"
- end
- --********************************** Make *****************************************
- make
- local
- i: STRING
- gui: IUP
- v: IUP_VBOX
- w: IUP_DIALOG
- item_exit, item_open, item_saveas, item_font, item_about: IUP_MENU_ITEM
- item_find, item_goto: IUP_MENU_ITEM
- sep: IUP_MENU_SEPARATOR
- file_menu, edit_menu, format_menu, help_menu, menu: IUP_MENU
- sub_menu_file, sub_menu_edit, sub_menu_format, sub_menu_help: IUP_SUBMENU
- do
- gui := iup_open
-
- create ml.multiline
- ml.set_expand("YES")
-
- create v.vbox({ARRAY[IUP_WIDGET] 1, << ml >>})
- create item_open.item("Open...")
- item_open.set_hide_mark(True)
- item_open.set_cb_action(agent item_open_action_cb(?))
- create item_saveas.item("Save As...")
- item_saveas.set_hide_mark(True)
- item_saveas.set_cb_action(agent item_saveas_action_cb(?))
- create item_exit.item("Exit")
- item_exit.set_hide_mark(True)
- item_exit.set_cb_action(agent item_exit_action_cb(?))
- create item_find.item("Find...")
- item_find.set_hide_mark(True)
- item_find.set_cb_action(agent item_find_action_cb(?))
- create item_goto.item("Go To...")
- item_goto.set_hide_mark(True)
- item_goto.set_cb_action(agent item_goto_action_cb(?))
- create item_font.item("Font...")
- item_font.set_hide_mark(True)
- item_font.set_cb_action(agent item_font_action_cb(?))
- create item_about.item("About...")
- item_about.set_hide_mark(True)
- item_about.set_cb_action(agent item_about_action_cb(?))
- create sep.separator
- create file_menu.menu({ARRAY[IUP_MENU_ELEMENT] 1, << item_open, item_saveas, sep, item_exit >>})
- create edit_menu.menu({ARRAY[IUP_MENU_ELEMENT] 1, << item_find, item_goto >>})
- create format_menu.menu({ARRAY[IUP_MENU_ELEMENT] 1, << item_font >>})
- create help_menu.menu({ARRAY[IUP_MENU_ELEMENT] 1, << item_about >>})
- create sub_menu_file.submenu("File", file_menu)
- create sub_menu_edit.submenu("Edit", edit_menu)
- create sub_menu_format.submenu("Format", format_menu)
- create sub_menu_help.submenu("Help", help_menu)
-
- create menu.menu({ARRAY[IUP_MENU_ELEMENT] 1, << sub_menu_file, sub_menu_edit, sub_menu_format, sub_menu_help >>})
-
- create w.dialog(v)
- w.set_menu_widget(menu)
- w.set_title("Simple Notepad")
- w.set_predefined_size("HALF", "HALF")
- -- parent for pre-defined dialogs in callbak functions
- gui.set_global_parent_dialog_widget(w)
-
- i := w.show_predefined_xy("IUP_CENTER", "IUP_CENTER")
- w.set_user_size(0, 0)
-
- gui.main_loop
- gui.close
- end
- end
|