0
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
|
3 local Io = require "luan:Io.luan"
|
|
4 local print = Io.print or error()
|
|
5 local Swing = require "luan:swing/Swing.luan"
|
|
6 local new_frame = require("luan:swing/Frame.luan").new or error()
|
|
7 local new_label = require("luan:swing/Label.luan").new or error()
|
|
8 local new_text_area = require("luan:swing/Text_area.luan").new or error()
|
|
9 local new_scroll_pane = require("luan:swing/Scroll_pane.luan").new or error()
|
|
10 local new_list = require("luan:swing/List.luan").new or error()
|
|
11 local new_menu_bar = require("luan:swing/Menu_bar.luan").new or error()
|
|
12 local new_menu = require("luan:swing/Menu.luan").new or error()
|
|
13 local new_menu_item = require("luan:swing/Menu_item.luan").new or error()
|
2
|
14 local new_check_box_menu_item = require("luan:swing/Check_box_menu_item.luan").new or error()
|
0
|
15 local Logging = require "luan:logging/Logging.luan"
|
|
16 local logger = Logging.logger "editor"
|
|
17
|
|
18
|
|
19 local new_window
|
|
20
|
1
|
21 local function make_menu_bar(window)
|
0
|
22 local menu_bar = new_menu_bar()
|
1
|
23 local file_menu = new_menu()
|
|
24 file_menu.text = "File"
|
|
25 local new_file = new_menu_item()
|
|
26 new_file.text = "New File"
|
|
27 new_file.accelerator = "meta N"
|
|
28 new_file.add_action_listener(function()
|
|
29 new_window()
|
|
30 end)
|
|
31 file_menu.add(new_file)
|
|
32 local open = new_menu_item()
|
|
33 open.text = "Open..."
|
|
34 open.accelerator = "meta O"
|
|
35 open.add_action_listener(function()
|
|
36 local file_chooser = window.frame.file_chooser_load()
|
|
37 if window.file ~= nil then
|
|
38 file_chooser.directory = window.file.parent()
|
|
39 end
|
|
40 file_chooser.visible = true
|
|
41 local file = file_chooser.file
|
|
42 if file ~= nil then
|
|
43 new_window(file)
|
|
44 end
|
|
45 end)
|
|
46 file_menu.add(open)
|
|
47 menu_bar.add(file_menu)
|
2
|
48 local view_menu = new_menu()
|
|
49 view_menu.text = "View"
|
|
50 local word_wrap = new_check_box_menu_item()
|
|
51 word_wrap.text = "Word Wrap"
|
|
52 word_wrap.state = window.text_area.line_wrap
|
|
53 word_wrap.add_action_listener(function()
|
|
54 window.text_area.line_wrap = word_wrap.state
|
|
55 end)
|
|
56 view_menu.add(word_wrap)
|
|
57 menu_bar.add(view_menu)
|
0
|
58 return menu_bar
|
|
59 end
|
|
60
|
|
61 local n_windows = 0
|
|
62
|
1
|
63 function new_window(file)
|
0
|
64 local frame = new_frame()
|
1
|
65 frame.title = file and file.to_string() or "untitled"
|
0
|
66 frame.add_close_listener(function()
|
|
67 n_windows = n_windows - 1
|
|
68 if n_windows == 0 then
|
|
69 Luan.exit()
|
|
70 end
|
|
71 end)
|
|
72 local text_area = new_text_area()
|
1
|
73 if file ~= nil then
|
|
74 text_area.text = file.read_text()
|
|
75 end
|
0
|
76 text_area.rows = 10
|
|
77 text_area.columns = 20
|
|
78 text_area.wrap_style_word = true
|
|
79 text_area.line_wrap = true
|
|
80 text_area.tab_size = 4
|
|
81 text_area.set_font{ family="Monospaced", size=13 }
|
1
|
82 text_area.select(0,0)
|
0
|
83 --print(text_area.line_count)
|
|
84 local scroll_pane = new_scroll_pane(text_area)
|
|
85 local list = new_list()
|
|
86 list.add_element("1")
|
|
87 list.add_element("2")
|
|
88 list.add_element("3")
|
|
89 --scroll_pane.set_row_header_view(list)
|
|
90 frame.add(scroll_pane)
|
1
|
91 local window = {
|
|
92 frame = frame
|
|
93 file = file
|
2
|
94 text_area = text_area
|
1
|
95 }
|
|
96 local menu_bar = make_menu_bar(window)
|
0
|
97 frame.set_menu_bar(menu_bar)
|
|
98 frame.pack()
|
|
99 frame.visible = true
|
|
100 text_area.request_focus_in_window()
|
|
101 n_windows = n_windows + 1
|
|
102 end
|
|
103
|
1
|
104 Swing.run(function()
|
|
105 new_window()
|
|
106 end)
|