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