simple_notepad_5.e 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. class SIMPLE_NOTEPAD_5
  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. multitext_caret_cb (m: IUP_TEXT; lin, col, pos: INTEGER): STRING
  64. local
  65. lbl_statusbar: IUP_LABEL
  66. str: STRING
  67. do
  68. if lbl_statusbar ?:= m.get_dialog_child("STATUSBAR") then
  69. lbl_statusbar ::= m.get_dialog_child("STATUSBAR")
  70. str := "Line "
  71. str.append(lin.to_string)
  72. str.append(", Col ")
  73. str.append(col.to_string)
  74. lbl_statusbar.set_title(str)
  75. end
  76. Result := "IUP_DEFAULT"
  77. end
  78. item_open_action_cb (widget: IUP_WIDGET): STRING
  79. local
  80. status: INTEGER
  81. rv, name, text: STRING
  82. fd: IUP_FILE_DIALOG
  83. do
  84. create fd.file_dialog
  85. fd.set_dialog_type("OPEN")
  86. fd.set_ext_filter("Text files|*.txt|All Files|*.*|")
  87. fd.set_title("Select a text file")
  88. rv := fd.popup_predefined_xy("IUP_CENTER", "IUP_CENTER")
  89. if rv.is_equal("IUP_NOERROR") then
  90. status := fd.get_status
  91. if status.is_equal(0) then
  92. name := fd.get_value
  93. text := read_file(name)
  94. ml.set_value(text)
  95. end
  96. end
  97. fd.destroy
  98. Result := "IUP_DEFAULT"
  99. end
  100. item_saveas_action_cb (widget: IUP_WIDGET): STRING
  101. local
  102. status: INTEGER
  103. rv, name, text: STRING
  104. fd: IUP_FILE_DIALOG
  105. do
  106. create fd.file_dialog
  107. fd.set_dialog_type("SAVE")
  108. fd.set_ext_filter("Text files|*.txt|All Files|*.*|")
  109. fd.set_title("Select a text file")
  110. rv := fd.popup_predefined_xy("IUP_CENTER", "IUP_CENTER")
  111. if rv.is_equal("IUP_NOERROR") then
  112. status := fd.get_status
  113. if not status.is_equal(-1) then
  114. name := fd.get_value
  115. text := ml.get_value
  116. write_file(name, text)
  117. end
  118. end
  119. fd.destroy
  120. Result := "IUP_DEFAULT"
  121. end
  122. item_font_action_cb (widget: IUP_MENU_ITEM): STRING
  123. local
  124. status: INTEGER
  125. rv, ft, selected: STRING
  126. fd: IUP_FONT_DIALOG
  127. do
  128. create fd.font_dialog
  129. ft := ml.get_font
  130. fd.set_value(ft)
  131. rv := fd.popup_predefined_xy("IUP_CENTER", "IUP_CENTER")
  132. if rv.is_equal("IUP_NOERROR") then
  133. status := fd.get_status
  134. if status.is_equal(1) then
  135. selected := fd.get_value
  136. ml.set_font(selected)
  137. end
  138. end
  139. fd.destroy
  140. Result := "IUP_DEFAULT"
  141. end
  142. item_about_action_cb (widget: IUP_MENU_ITEM): STRING
  143. local
  144. ms: IUP_MESSAGE
  145. do
  146. create ms.message("About"," Simple Notepad%N%NAutors:%N Gustavo Lyrio%N Antonio Scuri")
  147. Result := "IUP_DEFAULT"
  148. end
  149. item_exit_action_cb (widget: IUP_MENU_ITEM): STRING
  150. do
  151. Result := "IUP_CLOSE"
  152. end
  153. goto_ok_action_cb(widget: IUP_BUTTON): STRING
  154. local
  155. line_count, line: INTEGER
  156. txt: IUP_TEXT
  157. ms: IUP_MESSAGE
  158. do
  159. if txt ?:= widget.get_dialog_child("LINE_TEXT") then
  160. txt ::= widget.get_dialog_child("LINE_TEXT")
  161. line_count := widget.get_attribute_integer("TEXT_LINECOUNT")
  162. line := txt.get_value.to_integer
  163. if line < 1 and line >= line_count then
  164. create ms.message("Error", "Invalid line number.")
  165. Result := "IUP_DEFAULT"
  166. end
  167. widget.get_dialog.set_attribute_integer("STATUS", 1)
  168. Result := "IUP_CLOSE"
  169. end
  170. end
  171. goto_cancel_action_cb(widget: IUP_BUTTON): STRING
  172. do
  173. widget.get_dialog.set_attribute_integer("STATUS", 0)
  174. Result := "IUP_CLOSE"
  175. end
  176. item_goto_action_cb (widget: IUP_MENU_ITEM): STRING
  177. local
  178. dlg: IUP_DIALOG
  179. vbox: IUP_VBOX
  180. hbox: IUP_HBOX
  181. bt_ok, bt_cancel: IUP_BUTTON
  182. f: IUP_FILL
  183. lbl: IUP_LABEL
  184. txt: IUP_TEXT
  185. str, s: STRING
  186. line_count, line, pos: INTEGER
  187. do
  188. line_count := ml.get_line_count
  189. str := "Line Number [1-"
  190. str.append_string(ml.get_line_count.to_string)
  191. str.append_string("]:")
  192. create lbl.label(str)
  193. create txt.text
  194. txt.set_mask("IUP_MASK_UINT") -- unsigned integer numbers only
  195. txt.set_name("LINE_TEXT")
  196. txt.set_visible_columns(20)
  197. create bt_ok.button("OK")
  198. bt_ok.set_attribute_integer("TEXT_LINECOUNT", line_count)
  199. bt_ok.set_padding(10, 2)
  200. bt_ok.set_cb_action(agent goto_ok_action_cb(?))
  201. create bt_cancel.button("Cancel")
  202. bt_cancel.set_padding(10, 2)
  203. bt_cancel.set_cb_action(agent goto_cancel_action_cb(?))
  204. create f.fill
  205. create hbox.hbox({ARRAY[IUP_WIDGET] 1, << f, bt_ok, bt_cancel >>})
  206. hbox.set_normalize_size("HORIZONTAL")
  207. create vbox.vbox({ARRAY[IUP_WIDGET] 1, << lbl, txt, hbox >>})
  208. vbox.set_margin(10, 10)
  209. vbox.set_gap(5)
  210. create dlg.dialog(vbox)
  211. dlg.set_title("Go To Line")
  212. dlg.set_modal_dialog_frame(True)
  213. dlg.set_default_enter_widget(bt_ok)
  214. dlg.set_default_escape_widget(bt_cancel)
  215. dlg.set_parent_dialog_widget(widget.get_dialog)
  216. s := dlg.popup_predefined_xy ("IUP_CENTERPARENT", "IUP_CENTERPARENT")
  217. if s.is_equal("IUP_NOERROR") and dlg.get_attribute_integer("STATUS").is_equal(1) then
  218. line := txt.get_value.to_integer
  219. pos := ml.text_convert_lin_col_to_pos(line, 0)
  220. ml.set_multiline_caret_pos(pos)
  221. ml.scroll_to_pos(pos)
  222. end
  223. dlg.destroy
  224. Result := "IUP_DEFAULT"
  225. end
  226. find_next_action_cb (widget: IUP_BUTTON): STRING
  227. local
  228. str, str_to_find, casesensitive: STRING
  229. find_pos, pos, end_pos: INTEGER
  230. txt: IUP_TEXT
  231. find_case: IUP_TOGGLE
  232. tup: TUPLE[INTEGER, INTEGER]
  233. pw: IUP_WIDGET
  234. ms: IUP_MESSAGE
  235. do
  236. str := ml.get_value
  237. find_pos := ml.get_attribute_integer("FIND_POS")
  238. if find_pos = 0 then
  239. find_pos := 1
  240. end
  241. if txt ?:= widget.get_dialog_child("FIND_TEXT") then
  242. txt ::= widget.get_dialog_child("FIND_TEXT")
  243. str_to_find := txt.get_value
  244. if find_case ?:= widget.get_dialog_child("FIND_CASE") then
  245. find_case ::= widget.get_dialog_child("FIND_CASE")
  246. casesensitive := find_case.get_value
  247. -- Rest 1 since the string start at 1, but multiline position
  248. -- start at 0.
  249. pos := str_find(str, str_to_find, casesensitive, find_pos) - 1
  250. if pos < 0 and find_pos > 0 then
  251. -- try again from the start
  252. pos := str_find(str, str_to_find, casesensitive, 1) - 1
  253. end
  254. if pos > 0 then
  255. end_pos := pos + str_to_find.count
  256. ml.set_attribute_integer("FIND_POS", end_pos)
  257. pw := ml.set_focus
  258. ml.set_multiline_selection_pos(pos, end_pos)
  259. tup := ml.text_convert_pos_to_lin_col(pos)
  260. -- position at col=0, just scroll lines
  261. pos := ml.text_convert_lin_col_to_pos(tup.item_1, 0)
  262. ml.scroll_to_pos(pos)
  263. else
  264. create ms.message("Warning", "Text not found.")
  265. end
  266. end
  267. end
  268. Result := "IUP_DEFAULT"
  269. end
  270. find_close_action_cb (widget: IUP_BUTTON): STRING
  271. do
  272. widget.get_dialog.hide
  273. Result := "IUP_DEFAULT"
  274. end
  275. item_find_action_cb (widget: IUP_WIDGET): STRING
  276. local
  277. dlg, main_dlg: IUP_DIALOG
  278. lbl: IUP_LABEL
  279. txt: IUP_TEXT
  280. btn, bt_next, bt_close: IUP_BUTTON
  281. find_case: IUP_TOGGLE
  282. f: IUP_FILL
  283. hbox: IUP_HBOX
  284. vbox: IUP_VBOX
  285. s: STRING
  286. item: IUP_MENU_ITEM
  287. do
  288. if btn ?:= widget then
  289. btn ::= widget
  290. main_dlg := btn.get_dialog
  291. elseif item ?:= widget then
  292. item ::= widget
  293. main_dlg := item.get_dialog
  294. end
  295. if dlg ?:= main_dlg.get_attribute_widget("FIND_DIALOG") then
  296. dlg ::= main_dlg.get_attribute_widget("FIND_DIALOG")
  297. end
  298. if dlg = Void then
  299. create lbl.label("Find What:")
  300. create txt.text
  301. txt.set_name("FIND_TEXT")
  302. txt.set_visible_columns(20)
  303. create find_case.toggle("Case Sensitive")
  304. find_case.set_name("FIND_CASE")
  305. create bt_next.button("Find Next")
  306. bt_next.set_padding(10, 2)
  307. bt_next.set_cb_action(agent find_next_action_cb(?))
  308. create bt_close.button("Close")
  309. bt_close.set_padding(10, 2)
  310. bt_close.set_cb_action(agent find_close_action_cb(?))
  311. create f.fill
  312. create hbox.hbox({ARRAY[IUP_WIDGET] 1, << f, bt_next, bt_close >>})
  313. hbox.set_normalize_size("HORIZONTAL")
  314. create vbox.vbox({ARRAY[IUP_WIDGET] 1, << lbl, txt, find_case, hbox >>})
  315. vbox.set_margin(10, 10)
  316. vbox.set_gap(10)
  317. create dlg.dialog(vbox)
  318. dlg.set_title("Find")
  319. dlg.set_modal_dialog_frame(True)
  320. dlg.set_default_enter_widget(bt_next)
  321. dlg.set_default_escape_widget(bt_close)
  322. dlg.set_parent_dialog_widget(main_dlg)
  323. -- Save the dialog to reuse it
  324. main_dlg.set_attribute_widget("FIND_DIALOG", dlg)
  325. end
  326. -- center parent first time, next time reuse the last position
  327. s := dlg.show_predefined_xy("IUP_CURRENT", "IUP_CURRENT")
  328. Result := "IUP_DEFAULT"
  329. end
  330. --********************************** Make *****************************************
  331. make
  332. local
  333. i: STRING
  334. gui: IUP
  335. v: IUP_VBOX
  336. w: IUP_DIALOG
  337. item_exit, item_open, item_saveas, item_font, item_about: IUP_MENU_ITEM
  338. item_find, item_goto: IUP_MENU_ITEM
  339. sep: IUP_MENU_SEPARATOR
  340. file_menu, edit_menu, format_menu, help_menu, menu: IUP_MENU
  341. sub_menu_file, sub_menu_edit, sub_menu_format, sub_menu_help: IUP_SUBMENU
  342. btn_open, btn_save, btn_find: IUP_BUTTON
  343. lbl_statusbar, toolbar_sep: IUP_LABEL
  344. toolbar_hb: IUP_HBOX
  345. do
  346. gui := iup_open
  347. gui.load_images
  348. create ml.multiline
  349. ml.set_expand("YES")
  350. ml.set_cb_caret(agent multitext_caret_cb(?,?,?,?))
  351. create item_open.item("Open...")
  352. item_open.set_hide_mark(True)
  353. item_open.set_cb_action(agent item_open_action_cb(?))
  354. create btn_open.button("")
  355. btn_open.set_image("IUP_FileOpen")
  356. btn_open.set_flat(True)
  357. btn_open.set_can_focus(False)
  358. btn_open.set_cb_action(agent item_open_action_cb(?))
  359. create item_saveas.item("Save As...")
  360. item_saveas.set_hide_mark(True)
  361. item_saveas.set_cb_action(agent item_saveas_action_cb(?))
  362. create btn_save.button("")
  363. btn_save.set_image("IUP_FileSave")
  364. btn_save.set_flat(True)
  365. btn_save.set_can_focus(False)
  366. btn_save.set_cb_action(agent item_saveas_action_cb(?))
  367. create item_exit.item("Exit")
  368. item_exit.set_hide_mark(True)
  369. item_exit.set_cb_action(agent item_exit_action_cb(?))
  370. create item_find.item("Find...")
  371. item_find.set_hide_mark(True)
  372. item_find.set_cb_action(agent item_find_action_cb(?))
  373. create btn_find.button("")
  374. btn_find.set_image("IUP_EditFind")
  375. btn_find.set_flat(True)
  376. btn_find.set_can_focus(False)
  377. btn_find.set_cb_action(agent item_find_action_cb(?))
  378. create item_goto.item("Go To...")
  379. item_goto.set_hide_mark(True)
  380. item_goto.set_cb_action(agent item_goto_action_cb(?))
  381. create item_font.item("Font...")
  382. item_font.set_hide_mark(True)
  383. item_font.set_cb_action(agent item_font_action_cb(?))
  384. create item_about.item("About...")
  385. item_about.set_hide_mark(True)
  386. item_about.set_cb_action(agent item_about_action_cb(?))
  387. create sep.separator
  388. create toolbar_sep.label_empty
  389. toolbar_sep.set_vertical_separator
  390. create file_menu.menu({ARRAY[IUP_MENU_ELEMENT] 1, << item_open, item_saveas, sep, item_exit >>})
  391. create edit_menu.menu({ARRAY[IUP_MENU_ELEMENT] 1, << item_find, item_goto >>})
  392. create format_menu.menu({ARRAY[IUP_MENU_ELEMENT] 1, << item_font >>})
  393. create help_menu.menu({ARRAY[IUP_MENU_ELEMENT] 1, << item_about >>})
  394. create sub_menu_file.submenu("File", file_menu)
  395. create sub_menu_edit.submenu("Edit", edit_menu)
  396. create sub_menu_format.submenu("Format", format_menu)
  397. create sub_menu_help.submenu("Help", help_menu)
  398. create menu.menu({ARRAY[IUP_MENU_ELEMENT] 1, << sub_menu_file, sub_menu_edit, sub_menu_format, sub_menu_help >>})
  399. create toolbar_hb.hbox({ARRAY[IUP_WIDGET] 1, << btn_open, btn_save, toolbar_sep, btn_find >>})
  400. toolbar_hb.set_margin(5, 5)
  401. toolbar_hb.set_gap(2)
  402. create lbl_statusbar.label("Lin 1, Col 1")
  403. lbl_statusbar.set_name("STATUSBAR")
  404. lbl_statusbar.set_expand("HORIZONTAL")
  405. lbl_statusbar.set_padding(10, 5)
  406. create v.vbox({ARRAY[IUP_WIDGET] 1, << toolbar_hb, ml, lbl_statusbar >>})
  407. create w.dialog(v)
  408. w.set_menu_widget(menu)
  409. w.set_title("Simple Notepad")
  410. w.set_predefined_size("HALF", "HALF")
  411. -- parent for some pre-defined dialogs in callbak functions
  412. gui.set_global_parent_dialog_widget(w)
  413. i := w.show_predefined_xy("IUP_CENTER", "IUP_CENTER")
  414. w.set_user_size(0, 0)
  415. gui.main_loop
  416. gui.close
  417. end
  418. end