simple_notepad_3.e 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. class SIMPLE_NOTEPAD_3
  2. insert
  3. IUP_INTERFACE
  4. create {ANY}
  5. make
  6. feature {ANY}
  7. -- attribute to be used inside the menu callbacks.
  8. ml: IUP_MULTILINE
  9. read_file (file_name: STRING): STRING
  10. local
  11. tfr: TEXT_FILE_READ
  12. str: STRING
  13. ms: IUP_MESSAGE
  14. do
  15. create str.make_empty
  16. create tfr.connect_to(file_name)
  17. if tfr.is_connected then
  18. from
  19. tfr.read_line
  20. until
  21. tfr.end_of_input
  22. loop
  23. str.append(tfr.last_string)
  24. str.append_character('%N')
  25. tfr.read_line
  26. end
  27. str.append(tfr.last_string)
  28. tfr.disconnect
  29. else
  30. str.append("Fail when reading from file: ")
  31. str.append(file_name)
  32. create ms.message("Error", str)
  33. end
  34. Result := str
  35. end
  36. write_file (file_name, text: STRING)
  37. local
  38. tfw: TEXT_FILE_WRITE
  39. ms: IUP_MESSAGE
  40. str: STRING
  41. do
  42. create tfw.connect_to(file_name)
  43. if tfw.is_connected then
  44. tfw.put_string(text)
  45. tfw.disconnect
  46. else
  47. str.make_empty
  48. str.append("Fail when writing to file: ")
  49. str.append(file_name)
  50. create ms.message("Error", str)
  51. end
  52. end
  53. open_cb (widget: IUP_MENU_ITEM): STRING
  54. local
  55. status: INTEGER
  56. rv, name, text: STRING
  57. fd: IUP_FILE_DIALOG
  58. do
  59. create fd.file_dialog
  60. fd.set_dialog_type("OPEN")
  61. fd.set_ext_filter("Text files|*.txt|All Files|*.*|")
  62. fd.set_title("Select a text file")
  63. rv := fd.popup_predefined_xy("IUP_CENTER", "IUP_CENTER")
  64. if rv.is_equal("IUP_NOERROR") then
  65. status := fd.get_status
  66. if status.is_equal(0) then
  67. name := fd.get_value
  68. text := read_file(name)
  69. ml.set_value(text)
  70. end
  71. end
  72. fd.destroy
  73. Result := "IUP_DEFAULT"
  74. end
  75. saveas_cb (widget: IUP_MENU_ITEM): STRING
  76. local
  77. status: INTEGER
  78. rv, name, text: STRING
  79. fd: IUP_FILE_DIALOG
  80. do
  81. create fd.file_dialog
  82. fd.set_dialog_type("SAVE")
  83. fd.set_ext_filter("Text files|*.txt|All Files|*.*|")
  84. fd.set_title("Select a text file")
  85. rv := fd.popup_predefined_xy("IUP_CENTER", "IUP_CENTER")
  86. if rv.is_equal("IUP_NOERROR") then
  87. status := fd.get_status
  88. if not status.is_equal(-1) then
  89. name := fd.get_value
  90. text := ml.get_value
  91. write_file(name, text)
  92. end
  93. end
  94. fd.destroy
  95. Result := "IUP_DEFAULT"
  96. end
  97. font_cb (widget: IUP_MENU_ITEM): STRING
  98. local
  99. status: INTEGER
  100. rv, ft, selected: STRING
  101. fd: IUP_FONT_DIALOG
  102. do
  103. create fd.font_dialog
  104. ft := ml.get_font
  105. fd.set_value(ft)
  106. rv := fd.popup_predefined_xy("IUP_CENTER", "IUP_CENTER")
  107. if rv.is_equal("IUP_NOERROR") then
  108. status := fd.get_status
  109. if status.is_equal(1) then
  110. selected := fd.get_value
  111. ml.set_font(selected)
  112. end
  113. end
  114. fd.destroy
  115. Result := "IUP_DEFAULT"
  116. end
  117. about_cb (widget: IUP_MENU_ITEM): STRING
  118. local
  119. ms: IUP_MESSAGE
  120. do
  121. create ms.message("About"," Simple Notepad%N%NAutors:%N Gustavo Lyrio%N Antonio Scuri%N German Arias")
  122. Result := "IUP_DEFAULT"
  123. end
  124. exit_cb (widget: IUP_MENU_ITEM): STRING
  125. do
  126. Result := "IUP_CLOSE"
  127. end
  128. make
  129. local
  130. i: STRING
  131. gui: IUP
  132. v: IUP_VBOX
  133. w: IUP_DIALOG
  134. item_exit, item_open, item_saveas, item_font, item_about: IUP_MENU_ITEM
  135. sep: IUP_MENU_SEPARATOR
  136. file_menu, format_menu, help_menu, menu: IUP_MENU
  137. sub_menu_file, sub_menu_format, sub_menu_help: IUP_SUBMENU
  138. do
  139. gui := iup_open
  140. create ml.multiline
  141. ml.set_expand("YES")
  142. create v.vbox({ARRAY[IUP_WIDGET] 1, << ml >>})
  143. create item_open.item("Open...")
  144. item_open.set_hide_mark(True)
  145. item_open.set_cb_action(agent open_cb(?))
  146. create item_saveas.item("Save As...")
  147. item_saveas.set_hide_mark(True)
  148. item_saveas.set_cb_action(agent saveas_cb(?))
  149. create item_exit.item("Exit")
  150. item_exit.set_hide_mark(True)
  151. item_exit.set_cb_action(agent exit_cb(?))
  152. create item_font.item("Font...")
  153. item_font.set_hide_mark(True)
  154. item_font.set_cb_action(agent font_cb(?))
  155. create item_about.item("About...")
  156. item_about.set_hide_mark(True)
  157. item_about.set_cb_action(agent about_cb(?))
  158. create sep.separator
  159. create file_menu.menu({ARRAY[IUP_MENU_ELEMENT] 1, << item_open, item_saveas, sep, item_exit >>})
  160. create format_menu.menu({ARRAY[IUP_MENU_ELEMENT] 1, << item_font >>})
  161. create help_menu.menu({ARRAY[IUP_MENU_ELEMENT] 1, << item_about >>})
  162. create sub_menu_file.submenu("File", file_menu)
  163. create sub_menu_format.submenu("Format", format_menu)
  164. create sub_menu_help.submenu("Help", help_menu)
  165. create menu.menu({ARRAY[IUP_MENU_ELEMENT] 1, << sub_menu_file, sub_menu_format, sub_menu_help >>})
  166. create w.dialog(v)
  167. w.set_menu_widget(menu)
  168. w.set_title("Simple Notepad")
  169. w.set_predefined_size("QUARTER", "QUARTER")
  170. i := w.show_predefined_xy("IUP_CENTER", "IUP_CENTER")
  171. w.set_user_size(0, 0)
  172. gui.main_loop
  173. gui.close
  174. end
  175. end