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