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
|
|
100 --[[
|
|
101 new_menu_item{
|
|
102 text = "Test"
|
|
103 action_listener = function(_)
|
|
104 local location = window.frame.location
|
|
105 location.y = location.y - 20
|
|
106 window.frame.location = location
|
|
107 end
|
|
108 }
|
|
109 ]]
|
|
110 }
|
|
111 }
|
|
112 new_menu{
|
|
113 text = "Edit"
|
|
114 menu_items = {
|
|
115 undo
|
|
116 redo
|
|
117 separator
|
|
118 new_menu_item{
|
|
119 text = "Cut"
|
|
120 accelerator = "meta X"
|
|
121 action_listener = action_listener(text_area.cut)
|
|
122 }
|
|
123 new_menu_item{
|
|
124 text = "Copy"
|
|
125 accelerator = "meta C"
|
|
126 action_listener = action_listener(text_area.copy)
|
|
127 }
|
|
128 new_menu_item{
|
|
129 text = "Paste"
|
|
130 accelerator = "meta V"
|
|
131 action_listener = action_listener(text_area.paste)
|
|
132 }
|
|
133 separator
|
|
134 new_menu_item{
|
|
135 text = "Indent"
|
|
136 accelerator = "meta CLOSE_BRACKET"
|
|
137 action_listener = action_listener(window.indent)
|
|
138 }
|
|
139 new_menu_item{
|
|
140 text = "Unindent"
|
|
141 accelerator = "meta OPEN_BRACKET"
|
|
142 action_listener = action_listener(window.unindent)
|
|
143 }
|
|
144 separator
|
|
145 new_menu_item{
|
|
146 text = "Select All"
|
|
147 accelerator = "meta A"
|
|
148 action_listener = action_listener(text_area.select_all)
|
|
149 }
|
|
150 }
|
|
151 }
|
|
152 new_menu{
|
|
153 text = "Find"
|
|
154 menu_items = {
|
|
155 find_menu_item
|
|
156 new_menu_item{
|
|
157 text = "Find Case Insensitive"
|
|
158 action_listener = window.find_case_insensitive
|
|
159 }
|
|
160 new_menu_item{
|
|
161 text = "Convert Leading Tabs to Spaces"
|
|
162 action_listener = window.tabs_to_spaces
|
|
163 }
|
|
164 new_menu_item{
|
|
165 text = "Convert Leading Spaces to Tabs"
|
|
166 action_listener = window.spaces_to_tabs
|
|
167 }
|
|
168 }
|
|
169 }
|
|
170 new_menu{
|
|
171 text = "View"
|
|
172 menu_items = {
|
|
173 new_check_box_menu_item{
|
|
174 text = "Word Wrap"
|
|
175 state = text_area.line_wrap
|
|
176 action_listener = function(event)
|
|
177 window.set_line_wrap(event.source.state)
|
|
178 end
|
|
179 }
|
|
180 new_check_box_menu_item{
|
|
181 text = "Show Whitespace"
|
|
182 accelerator = "meta W"
|
|
183 state = text_area.whitespace_visible
|
|
184 action_listener = function(event)
|
|
185 window.set_whitespace_visible(event.source.state)
|
|
186 end
|
|
187 }
|
|
188 new_check_box_menu_item{
|
|
189 text = "Spell Check"
|
|
190 accelerator = "meta SEMICOLON"
|
|
191 action_listener = function(event)
|
|
192 spell_check(text_area,event.source.state)
|
|
193 end
|
|
194 }
|
|
195 new_menu_item{
|
|
196 text = "Cursor Column"
|
|
197 accelerator = "meta B"
|
|
198 action_listener = function(_)
|
|
199 status_bar.text = "Cursor Column: "..window.cursor_column()
|
|
200 end
|
|
201 }
|
|
202 new_menu_item{
|
|
203 text = "Goto Line"
|
|
204 accelerator = "meta G"
|
|
205 action_listener = function(_)
|
|
206 local input = show_input_dialog( window.frame, "Goto line" )
|
|
207 if input == nil then
|
|
208 return
|
|
209 end
|
|
210 local line = to_number(input)
|
|
211 try
|
|
212 window.goto(line)
|
|
213 status_bar.text = "Went to line "..line
|
|
214 catch e
|
|
215 status_bar.text = "Invalid line: "..input
|
|
216 end
|
|
217 end
|
|
218 }
|
|
219 new_menu_item{
|
|
220 text = "Tab Size"
|
|
221 action_listener = function(_)
|
|
222 local input = show_input_dialog( window.frame, "Tab size", text_area.tab_size )
|
|
223 if input == nil then
|
|
224 return
|
|
225 end
|
|
226 local size = to_number(input)
|
|
227 try
|
|
228 window.set_tab_size(size)
|
|
229 status_bar.text = "Set tab size to "..size
|
|
230 catch e
|
|
231 status_bar.text = "Invalid tab size: "..input
|
|
232 end
|
|
233 end
|
|
234 }
|
48
|
235 view_file_path
|
37
|
236 }
|
|
237 }
|
38
|
238 new_menu{
|
|
239 text = "Window"
|
|
240 menu_items = {
|
|
241 new_menu_item{
|
|
242 text = "Duplicate Window"
|
|
243 action_listener = action_listener(window.duplicate)
|
|
244 }
|
40
|
245 new_menu_item{
|
|
246 text = "Align Windows"
|
|
247 action_listener = function(_)
|
|
248 local this_frame = window.frame
|
|
249 local location = this_frame.location
|
|
250 local size = this_frame.size
|
|
251 for _, frame in ipairs(get_all_frames()) do
|
|
252 frame.location = location
|
|
253 frame.size = size
|
|
254 end
|
|
255 end
|
|
256 }
|
41
|
257 new_menu_item{
|
|
258 text = "List Windows"
|
|
259 accelerator = "meta L"
|
|
260 action_listener = action_listener(window.show_list_window)
|
|
261 }
|
38
|
262 }
|
|
263 }
|
37
|
264 }
|
|
265 }
|
|
266 window.frame.set_menu_bar(menu_bar)
|
|
267 end
|
|
268
|
|
269 return add_menu_bar
|