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