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