simple_notepad_4.e 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. class SIMPLE_NOTEPAD_4
  2. insert
  3. IUP_INTERFACE
  4. create {ANY}
  5. make
  6. feature {ANY}
  7. ml: IUP_MULTILINE
  8. str_find (str, str_to_find, casesensitive: STRING; pos: INTEGER): INTEGER
  9. do
  10. if str.count.is_equal(0) or str_to_find.count.is_equal(0) then
  11. Result := -1
  12. elseif casesensitive.is_equal("OFF") then
  13. str.to_lower
  14. str_to_find.to_lower
  15. end
  16. Result := str.substring_index(str_to_find, pos)
  17. end
  18. read_file (file_name: STRING): STRING
  19. local
  20. tfr: TEXT_FILE_READ
  21. str: STRING
  22. ms: IUP_MESSAGE
  23. do
  24. create str.make_empty
  25. create tfr.connect_to(file_name)
  26. if tfr.is_connected then
  27. from
  28. tfr.read_line
  29. until
  30. tfr.end_of_input
  31. loop
  32. str.append(tfr.last_string)
  33. str.append_character('%N')
  34. tfr.read_line
  35. end
  36. str.append(tfr.last_string)
  37. tfr.disconnect
  38. else
  39. str.append("Fail when reading from file: ")
  40. str.append(file_name)
  41. create ms.message("Error", str)
  42. end
  43. Result := str
  44. end
  45. write_file (file_name, text: STRING)
  46. local
  47. tfw: TEXT_FILE_WRITE
  48. ms: IUP_MESSAGE
  49. str: STRING
  50. do
  51. create tfw.connect_to(file_name)
  52. if tfw.is_connected then
  53. tfw.put_string(text)
  54. tfw.disconnect
  55. else
  56. str.make_empty
  57. str.append("Fail when writing to file: ")
  58. str.append(file_name)
  59. create ms.message("Error", str)
  60. end
  61. end
  62. --********************************** Callbacks *****************************************
  63. item_open_action_cb (widget: IUP_MENU_ITEM): STRING
  64. local
  65. status: INTEGER
  66. rv, name, text: STRING
  67. fd: IUP_FILE_DIALOG
  68. do
  69. create fd.file_dialog
  70. fd.set_dialog_type("OPEN")
  71. fd.set_ext_filter("Text files|*.txt|All Files|*.*|")
  72. fd.set_title("Select a text file")
  73. rv := fd.popup_predefined_xy("IUP_CENTER", "IUP_CENTER")
  74. if rv.is_equal("IUP_NOERROR") then
  75. status := fd.get_status
  76. if status.is_equal(0) then
  77. name := fd.get_value
  78. text := read_file(name)
  79. ml.set_value(text)
  80. end
  81. end
  82. fd.destroy
  83. Result := "IUP_DEFAULT"
  84. end
  85. item_saveas_action_cb (widget: IUP_MENU_ITEM): STRING
  86. local
  87. status: INTEGER
  88. rv, name, text: STRING
  89. fd: IUP_FILE_DIALOG
  90. do
  91. create fd.file_dialog
  92. fd.set_dialog_type("SAVE")
  93. fd.set_ext_filter("Text files|*.txt|All Files|*.*|")
  94. fd.set_title("Select a text file")
  95. rv := fd.popup_predefined_xy("IUP_CENTER", "IUP_CENTER")
  96. if rv.is_equal("IUP_NOERROR") then
  97. status := fd.get_status
  98. if not status.is_equal(-1) then
  99. name := fd.get_value
  100. text := ml.get_value
  101. write_file(name, text)
  102. end
  103. end
  104. fd.destroy
  105. Result := "IUP_DEFAULT"
  106. end
  107. item_font_action_cb (widget: IUP_MENU_ITEM): STRING
  108. local
  109. status: INTEGER
  110. rv, ft, selected: STRING
  111. fd: IUP_FONT_DIALOG
  112. do
  113. create fd.font_dialog
  114. ft := ml.get_font
  115. fd.set_value(ft)
  116. rv := fd.popup_predefined_xy("IUP_CENTER", "IUP_CENTER")
  117. if rv.is_equal("IUP_NOERROR") then
  118. status := fd.get_status
  119. if status.is_equal(1) then
  120. selected := fd.get_value
  121. ml.set_font(selected)
  122. end
  123. end
  124. fd.destroy
  125. Result := "IUP_DEFAULT"
  126. end
  127. item_about_action_cb (widget: IUP_MENU_ITEM): STRING
  128. local
  129. ms: IUP_MESSAGE
  130. do
  131. create ms.message("About"," Simple Notepad%N%NAutors:%N Gustavo Lyrio%N Antonio Scuri")
  132. Result := "IUP_DEFAULT"
  133. end
  134. item_exit_action_cb (widget: IUP_MENU_ITEM): STRING
  135. do
  136. Result := "IUP_CLOSE"
  137. end
  138. goto_ok_action_cb(widget: IUP_BUTTON): STRING
  139. local
  140. line_count, line: INTEGER
  141. txt: IUP_TEXT
  142. ms: IUP_MESSAGE
  143. do
  144. if txt ?:= widget.get_dialog_child("LINE_TEXT") then
  145. txt ::= widget.get_dialog_child("LINE_TEXT")
  146. line_count := widget.get_attribute_integer("TEXT_LINECOUNT")
  147. line := txt.get_value.to_integer
  148. if line < 1 and line >= line_count then
  149. create ms.message("Error", "Invalid line number.")
  150. Result := "IUP_DEFAULT"
  151. end
  152. widget.get_dialog.set_attribute_integer("STATUS", 1)
  153. Result := "IUP_CLOSE"
  154. end
  155. end
  156. goto_cancel_action_cb(widget: IUP_BUTTON): STRING
  157. do
  158. widget.get_dialog.set_attribute_integer("STATUS", 0)
  159. Result := "IUP_CLOSE"
  160. end
  161. item_goto_action_cb (widget: IUP_MENU_ITEM): STRING
  162. local
  163. dlg: IUP_DIALOG
  164. vbox: IUP_VBOX
  165. hbox: IUP_HBOX
  166. bt_ok, bt_cancel: IUP_BUTTON
  167. f: IUP_FILL
  168. lbl: IUP_LABEL
  169. txt: IUP_TEXT
  170. str, s: STRING
  171. line_count, line, pos: INTEGER
  172. do
  173. line_count := ml.get_line_count
  174. str := "Line Number [1-"
  175. str.append_string(ml.get_line_count.to_string)
  176. str.append_string("]:")
  177. create lbl.label(str)
  178. create txt.text
  179. txt.set_mask("IUP_MASK_UINT") -- unsigned integer numbers only
  180. txt.set_name("LINE_TEXT")
  181. txt.set_visible_columns(20)
  182. create bt_ok.button("OK")
  183. bt_ok.set_attribute_integer("TEXT_LINECOUNT", line_count)
  184. bt_ok.set_padding(10, 2)
  185. bt_ok.set_cb_action(agent goto_ok_action_cb(?))
  186. create bt_cancel.button("Cancel")
  187. bt_cancel.set_padding(10, 2)
  188. bt_cancel.set_cb_action(agent goto_cancel_action_cb(?))
  189. create f.fill
  190. create hbox.hbox({ARRAY[IUP_WIDGET] 1, << f, bt_ok, bt_cancel >>})
  191. hbox.set_normalize_size("HORIZONTAL")
  192. create vbox.vbox({ARRAY[IUP_WIDGET] 1, << lbl, txt, hbox >>})
  193. vbox.set_margin(10, 10)
  194. vbox.set_gap(5)
  195. create dlg.dialog(vbox)
  196. dlg.set_title("Go To Line")
  197. dlg.set_modal_dialog_frame(True)
  198. dlg.set_default_enter_widget(bt_ok)
  199. dlg.set_default_escape_widget(bt_cancel)
  200. dlg.set_parent_dialog_widget(widget.get_dialog)
  201. s := dlg.popup_predefined_xy ("IUP_CENTERPARENT", "IUP_CENTERPARENT")
  202. if s.is_equal("IUP_NOERROR") and dlg.get_attribute_integer("STATUS").is_equal(1) then
  203. line := txt.get_value.to_integer
  204. pos := ml.text_convert_lin_col_to_pos(line, 0)
  205. ml.set_multiline_caret_pos(pos)
  206. ml.scroll_to_pos(pos)
  207. end
  208. dlg.destroy
  209. Result := "IUP_DEFAULT"
  210. end
  211. find_next_action_cb (widget: IUP_BUTTON): STRING
  212. local
  213. str, str_to_find, casesensitive: STRING
  214. find_pos, pos, end_pos: INTEGER
  215. txt: IUP_TEXT
  216. find_case: IUP_TOGGLE
  217. tup: TUPLE[INTEGER, INTEGER]
  218. pw: IUP_WIDGET
  219. ms: IUP_MESSAGE
  220. do
  221. str := ml.get_value
  222. find_pos := ml.get_attribute_integer("FIND_POS")
  223. if find_pos = 0 then
  224. find_pos := 1
  225. end
  226. if txt ?:= widget.get_dialog_child("FIND_TEXT") then
  227. txt ::= widget.get_dialog_child("FIND_TEXT")
  228. str_to_find := txt.get_value
  229. if find_case ?:= widget.get_dialog_child("FIND_CASE") then
  230. find_case ::= widget.get_dialog_child("FIND_CASE")
  231. casesensitive := find_case.get_value
  232. -- Rest 1 since the string start at 1, but multiline position
  233. -- start at 0.
  234. pos := str_find(str, str_to_find, casesensitive, find_pos) - 1
  235. if pos < 0 and find_pos > 0 then
  236. -- try again from the start
  237. pos := str_find(str, str_to_find, casesensitive, 1) - 1
  238. end
  239. if pos > 0 then
  240. end_pos := pos + str_to_find.count
  241. ml.set_attribute_integer("FIND_POS", end_pos)
  242. pw := ml.set_focus
  243. ml.set_multiline_selection_pos(pos, end_pos)
  244. tup := ml.text_convert_pos_to_lin_col(pos)
  245. -- position at col=0, just scroll lines
  246. pos := ml.text_convert_lin_col_to_pos(tup.item_1, 0)
  247. ml.scroll_to_pos(pos)
  248. else
  249. create ms.message("Warning", "Text not found.")
  250. end
  251. end
  252. end
  253. Result := "IUP_DEFAULT"
  254. end
  255. find_close_action_cb (widget: IUP_BUTTON): STRING
  256. do
  257. widget.get_dialog.hide
  258. Result := "IUP_DEFAULT"
  259. end
  260. item_find_action_cb (widget: IUP_MENU_ITEM): STRING
  261. local
  262. dlg: IUP_DIALOG
  263. lbl: IUP_LABEL
  264. txt: IUP_TEXT
  265. bt_next, bt_close: IUP_BUTTON
  266. find_case: IUP_TOGGLE
  267. f: IUP_FILL
  268. hbox: IUP_HBOX
  269. vbox: IUP_VBOX
  270. s: STRING
  271. do
  272. if dlg ?:= widget.get_attribute_widget("FIND_DIALOG") then
  273. dlg ::= widget.get_attribute_widget("FIND_DIALOG")
  274. end
  275. if dlg = Void then
  276. create lbl.label("Find What:")
  277. create txt.text
  278. txt.set_name("FIND_TEXT")
  279. txt.set_visible_columns(20)
  280. create find_case.toggle("Case Sensitive")
  281. find_case.set_name("FIND_CASE")
  282. create bt_next.button("Find Next")
  283. bt_next.set_padding(10, 2)
  284. bt_next.set_cb_action(agent find_next_action_cb(?))
  285. create bt_close.button("Close")
  286. bt_close.set_padding(10, 2)
  287. bt_close.set_cb_action(agent find_close_action_cb(?))
  288. create f.fill
  289. create hbox.hbox({ARRAY[IUP_WIDGET] 1, << f, bt_next, bt_close >>})
  290. hbox.set_normalize_size("HORIZONTAL")
  291. create vbox.vbox({ARRAY[IUP_WIDGET] 1, << lbl, txt, find_case, hbox >>})
  292. vbox.set_margin(10, 10)
  293. vbox.set_gap(10)
  294. create dlg.dialog(vbox)
  295. dlg.set_title("Find")
  296. dlg.set_modal_dialog_frame(True)
  297. dlg.set_default_enter_widget(bt_next)
  298. dlg.set_default_escape_widget(bt_close)
  299. dlg.set_parent_dialog_widget(widget.get_dialog)
  300. -- Save the dialog to reuse it
  301. widget.set_attribute_widget("FIND_DIALOG", dlg)
  302. end
  303. -- centerparent first time, next time reuse the last position
  304. s := dlg.show_predefined_xy("IUP_CURRENT", "IUP_CURRENT")
  305. Result := "IUP_DEFAULT"
  306. end
  307. --********************************** Make *****************************************
  308. make
  309. local
  310. i: STRING
  311. gui: IUP
  312. v: IUP_VBOX
  313. w: IUP_DIALOG
  314. item_exit, item_open, item_saveas, item_font, item_about: IUP_MENU_ITEM
  315. item_find, item_goto: IUP_MENU_ITEM
  316. sep: IUP_MENU_SEPARATOR
  317. file_menu, edit_menu, format_menu, help_menu, menu: IUP_MENU
  318. sub_menu_file, sub_menu_edit, sub_menu_format, sub_menu_help: IUP_SUBMENU
  319. do
  320. gui := iup_open
  321. create ml.multiline
  322. ml.set_expand("YES")
  323. create v.vbox({ARRAY[IUP_WIDGET] 1, << ml >>})
  324. create item_open.item("Open...")
  325. item_open.set_hide_mark(True)
  326. item_open.set_cb_action(agent item_open_action_cb(?))
  327. create item_saveas.item("Save As...")
  328. item_saveas.set_hide_mark(True)
  329. item_saveas.set_cb_action(agent item_saveas_action_cb(?))
  330. create item_exit.item("Exit")
  331. item_exit.set_hide_mark(True)
  332. item_exit.set_cb_action(agent item_exit_action_cb(?))
  333. create item_find.item("Find...")
  334. item_find.set_hide_mark(True)
  335. item_find.set_cb_action(agent item_find_action_cb(?))
  336. create item_goto.item("Go To...")
  337. item_goto.set_hide_mark(True)
  338. item_goto.set_cb_action(agent item_goto_action_cb(?))
  339. create item_font.item("Font...")
  340. item_font.set_hide_mark(True)
  341. item_font.set_cb_action(agent item_font_action_cb(?))
  342. create item_about.item("About...")
  343. item_about.set_hide_mark(True)
  344. item_about.set_cb_action(agent item_about_action_cb(?))
  345. create sep.separator
  346. create file_menu.menu({ARRAY[IUP_MENU_ELEMENT] 1, << item_open, item_saveas, sep, item_exit >>})
  347. create edit_menu.menu({ARRAY[IUP_MENU_ELEMENT] 1, << item_find, item_goto >>})
  348. create format_menu.menu({ARRAY[IUP_MENU_ELEMENT] 1, << item_font >>})
  349. create help_menu.menu({ARRAY[IUP_MENU_ELEMENT] 1, << item_about >>})
  350. create sub_menu_file.submenu("File", file_menu)
  351. create sub_menu_edit.submenu("Edit", edit_menu)
  352. create sub_menu_format.submenu("Format", format_menu)
  353. create sub_menu_help.submenu("Help", help_menu)
  354. create menu.menu({ARRAY[IUP_MENU_ELEMENT] 1, << sub_menu_file, sub_menu_edit, sub_menu_format, sub_menu_help >>})
  355. create w.dialog(v)
  356. w.set_menu_widget(menu)
  357. w.set_title("Simple Notepad")
  358. w.set_predefined_size("HALF", "HALF")
  359. -- parent for pre-defined dialogs in callbak functions
  360. gui.set_global_parent_dialog_widget(w)
  361. i := w.show_predefined_xy("IUP_CENTER", "IUP_CENTER")
  362. w.set_user_size(0, 0)
  363. gui.main_loop
  364. gui.close
  365. end
  366. end