comparison src/luan_editor/menu.luan @ 37:b7ff52d45b9a default tip

copy from luan
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 21 Apr 2025 13:07:29 -0600
parents
children
comparison
equal deleted inserted replaced
36:0a8865de3d53 37:b7ff52d45b9a
1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error
3 local String = require "luan:String.luan"
4 local to_number = String.to_number or error()
5 local new_menu_item = require("luan:swing/Menu_item.luan").new or error()
6 local new_check_box_menu_item = require("luan:swing/Check_box_menu_item.luan").new or error()
7 local new_menu_bar = require("luan:swing/Menu_bar.luan").new or error()
8 local Menu = require "luan:swing/Menu.luan"
9 local new_menu = Menu.new or error()
10 local separator = Menu.separator or error()
11 local Option_pane = require "luan:swing/Option_pane.luan"
12 local show_message_dialog = Option_pane.show_message_dialog or error()
13 local show_input_dialog = Option_pane.show_input_dialog or error()
14 local Spell_checker = require "classpath:luan_editor/Spell_checker.luan"
15 local spell_check = Spell_checker.spell_check or error()
16
17
18 local function action_listener(fn)
19 return function(_)
20 fn()
21 end
22 end
23
24 local function add_menu_bar(window)
25 local text_area = window.text_area
26 local document = text_area.document
27 local status_bar = window.status_bar
28 local revert = new_menu_item{
29 text = "Revert"
30 enabled = window.has_file
31 action_listener = action_listener(window.revert)
32 }
33 local undo = new_menu_item{
34 text = "Undo"
35 accelerator = "meta Z"
36 action_listener = action_listener(document.undo)
37 }
38 local redo = new_menu_item{
39 text = "Redo"
40 accelerator = "meta shift Z"
41 action_listener = action_listener(document.redo)
42 }
43 local function update_undo_redo()
44 undo.set_enabled(document.can_undo())
45 redo.set_enabled(document.can_redo())
46 end
47 window.update_undo_redo = update_undo_redo -- dont gc
48 update_undo_redo()
49 document.add_undo_listener(update_undo_redo)
50
51 local find_menu_item = new_check_box_menu_item{
52 text = "Find and Replace"
53 accelerator = "meta F"
54 action_listener = function(event)
55 window.show_find_panel(event.source.state)
56 end
57 }
58 window.find_menu_item = find_menu_item
59
60 local menu_bar = new_menu_bar{
61 menus = {
62 new_menu{
63 text = "File"
64 menu_items = {
65 new_menu_item{
66 text = "New File"
67 accelerator = "meta N"
68 action_listener = action_listener(window.new)
69 }
70 new_menu_item{
71 text = "Open..."
72 accelerator = "meta O"
73 action_listener = action_listener(window.open)
74 }
75 new_menu_item{
76 text = "Save"
77 accelerator = "meta S"
78 action_listener = function(_)
79 if window.save() then
80 revert.set_enabled(true)
81 end
82 end
83 }
84 new_menu_item{
85 text = "Print"
86 action_listener = action_listener(text_area.print)
87 }
88 revert
89 --[[
90 new_menu_item{
91 text = "Test"
92 action_listener = function(_)
93 local location = window.frame.location
94 location.y = location.y - 20
95 window.frame.location = location
96 end
97 }
98 ]]
99 }
100 }
101 new_menu{
102 text = "Edit"
103 menu_items = {
104 undo
105 redo
106 separator
107 new_menu_item{
108 text = "Cut"
109 accelerator = "meta X"
110 action_listener = action_listener(text_area.cut)
111 }
112 new_menu_item{
113 text = "Copy"
114 accelerator = "meta C"
115 action_listener = action_listener(text_area.copy)
116 }
117 new_menu_item{
118 text = "Paste"
119 accelerator = "meta V"
120 action_listener = action_listener(text_area.paste)
121 }
122 separator
123 new_menu_item{
124 text = "Indent"
125 accelerator = "meta CLOSE_BRACKET"
126 action_listener = action_listener(window.indent)
127 }
128 new_menu_item{
129 text = "Unindent"
130 accelerator = "meta OPEN_BRACKET"
131 action_listener = action_listener(window.unindent)
132 }
133 separator
134 new_menu_item{
135 text = "Select All"
136 accelerator = "meta A"
137 action_listener = action_listener(text_area.select_all)
138 }
139 }
140 }
141 new_menu{
142 text = "Find"
143 menu_items = {
144 find_menu_item
145 new_menu_item{
146 text = "Find Case Insensitive"
147 action_listener = window.find_case_insensitive
148 }
149 new_menu_item{
150 text = "Convert Leading Tabs to Spaces"
151 action_listener = window.tabs_to_spaces
152 }
153 new_menu_item{
154 text = "Convert Leading Spaces to Tabs"
155 action_listener = window.spaces_to_tabs
156 }
157 }
158 }
159 new_menu{
160 text = "View"
161 menu_items = {
162 new_check_box_menu_item{
163 text = "Word Wrap"
164 state = text_area.line_wrap
165 action_listener = function(event)
166 window.set_line_wrap(event.source.state)
167 end
168 }
169 new_check_box_menu_item{
170 text = "Show Whitespace"
171 accelerator = "meta W"
172 state = text_area.whitespace_visible
173 action_listener = function(event)
174 window.set_whitespace_visible(event.source.state)
175 end
176 }
177 new_check_box_menu_item{
178 text = "Spell Check"
179 accelerator = "meta SEMICOLON"
180 action_listener = function(event)
181 spell_check(text_area,event.source.state)
182 end
183 }
184 new_menu_item{
185 text = "Cursor Column"
186 accelerator = "meta B"
187 action_listener = function(_)
188 status_bar.text = "Cursor Column: "..window.cursor_column()
189 end
190 }
191 new_menu_item{
192 text = "Goto Line"
193 accelerator = "meta G"
194 action_listener = function(_)
195 local input = show_input_dialog( window.frame, "Goto line" )
196 if input == nil then
197 return
198 end
199 local line = to_number(input)
200 try
201 window.goto(line)
202 status_bar.text = "Went to line "..line
203 catch e
204 status_bar.text = "Invalid line: "..input
205 end
206 end
207 }
208 new_menu_item{
209 text = "Tab Size"
210 action_listener = function(_)
211 local input = show_input_dialog( window.frame, "Tab size", text_area.tab_size )
212 if input == nil then
213 return
214 end
215 local size = to_number(input)
216 try
217 window.set_tab_size(size)
218 status_bar.text = "Set tab size to "..size
219 catch e
220 status_bar.text = "Invalid tab size: "..input
221 end
222 end
223 }
224 }
225 }
226 }
227 }
228 window.frame.set_menu_bar(menu_bar)
229 end
230
231 return add_menu_bar