Added menus.
[rox-edit.git] / EditWindow.py
blobe1208c186ab52bdba1772b655c57c6e919b56c5a
1 import rox
2 from rox import g, filer
3 import sys
4 #from rox.XDSLoader import XDSLoader
6 FALSE = g.FALSE
7 TRUE = g.TRUE
9 from rox.Menu import Menu, set_save_name
11 set_save_name('Edit')
13 menu = Menu('main', [
14 ('/Save', 'save', ''),
15 ('/Options', 'show_options', ''),
16 ('/Open Parent', 'up', ''),
17 ('/Help', 'help', ''),
18 ('/Window', '', '<Branch>'),
19 ('/Window/New', 'new', ''),
20 ('/Window/Close', 'close', ''),
23 class EditWindow(g.Window):
24 def __init__(self, filename = None):
25 g.Window.__init__(self)
26 self.set_position(g.WIN_POS_CENTER)
27 self.set_default_size(g.gdk.screen_width() * 2 / 3,
28 g.gdk.screen_height() / 2)
30 self.savebox = None
32 scrollbar = g.VScrollbar()
33 self.text = g.TextView()
34 adj = scrollbar.get_adjustment()
35 self.text.set_scroll_adjustments(None, adj)
36 self.text.set_size_request(10, 10)
38 self.buffer = self.text.get_buffer()
39 self.insert_mark = self.buffer.get_mark('insert')
40 self.selection_bound_mark = self.buffer.get_mark('selection_bound')
41 start = self.buffer.get_start_iter()
42 self.mark_start = self.buffer.create_mark('mark_start', start, TRUE)
43 self.mark_end = self.buffer.create_mark('mark_end', start, FALSE)
44 tag = self.buffer.create_tag('marked')
45 tag.set_property('background', 'green')
46 self.marked = 0
48 vbox = g.VBox(FALSE)
49 self.add(vbox)
51 tools = g.Toolbar()
52 tools.set_style(g.TOOLBAR_ICONS)
53 vbox.pack_start(tools, FALSE, TRUE, 0)
54 tools.show()
56 tools.insert_stock(g.STOCK_HELP, 'Help', None, self.help, None, 0)
57 tools.insert_stock(g.STOCK_SAVE, 'Save', None, self.save, None, 0)
58 tools.insert_stock(g.STOCK_GO_UP, 'Up', None, self.up, None, 0)
59 tools.insert_stock(g.STOCK_CLOSE, 'Close', None, self.close, None, 0)
61 hbox = g.HBox(FALSE)
62 vbox.pack_start(hbox, expand = TRUE, fill = TRUE)
63 hbox.pack_start(self.text, expand = TRUE, fill = TRUE)
64 hbox.pack_start(scrollbar, expand = FALSE, fill = TRUE)
66 rox.toplevel_ref()
67 self.connect('destroy', lambda x: rox.toplevel_unref())
69 self.connect('delete-event', self.delete_event)
70 self.text.grab_focus()
71 self.show_all()
73 if filename:
74 import os.path
75 self.uri = os.path.abspath(filename)
76 else:
77 self.uri = None
78 self.modified = 0
79 self.update_title()
81 # Loading might take a while, so get something on the screen
82 # now...
83 g.gdk.flush()
85 if filename:
86 self.load_file(filename)
88 self.buffer.connect('changed', self.changed)
90 self.text.connect('button-press-event', self.button_press)
92 menu.attach(self, self)
94 def button_press(self, text, event):
95 if event.button != 3:
96 return 0
97 menu.popup(self, event)
98 return 1
100 def delete_event(self, window, event):
101 if self.modified:
102 self.save(discard = 1)
103 return 1
104 return 0
106 def update_title(self):
107 if self.uri:
108 title = self.uri
109 else:
110 title = "<Untitled>"
111 if self.modified:
112 title = title + " *"
113 self.set_title(title)
115 def changed(self, buffer):
116 if not self.modified:
117 self.modified = 1
118 self.update_title()
120 #def xds_load_data(self, data):
121 # if self.has_selection():
122 # self.text.delete_selection()
123 # self.text.insert_defaults(data)
124 # self.modified = 1
125 # self.update_title()
127 def load_file(self, path):
128 try:
129 if path == '-':
130 file = sys.stdin
131 else:
132 file = open(path, 'r')
133 contents = file.read()
134 if path != '-':
135 file.close()
136 self.buffer.insert_at_cursor(contents, -1)
137 except:
138 rox.report_exception()
140 def close(self, button = None):
141 if self.modified:
142 self.save(discard = 1)
143 else:
144 self.destroy()
146 def discard(self):
147 self.destroy()
149 def up(self, button = None):
150 if self.uri:
151 filer.show_file(self.uri)
152 else:
153 rox.alert("File is not saved to disk yet")
155 def has_selection(self):
156 s, e = self.get_selection_range()
157 return not e.equal(s)
159 def get_marked_range(self):
160 s = self.buffer.get_iter_at_mark(self.mark_start)
161 e = self.buffer.get_iter_at_mark(self.mark_end)
162 return s, e
164 def get_selection_range(self):
165 s = self.buffer.get_iter_at_mark(self.insert_mark)
166 e = self.buffer.get_iter_at_mark(self.selection_bound_mark)
167 return s, e
169 def save(self, widget = None, discard = 0):
170 from rox.SaveBox import SaveBox
172 if self.savebox:
173 self.savebox.destroy()
175 if self.has_selection() and not discard:
176 saver = SelectionSaver(self)
177 self.savebox = SaveBox(saver, 'Selection', 'text/plain')
178 self.savebox.connect('destroy', lambda w: saver.destroy())
179 else:
180 uri = self.uri or options.get('edit_default_name')
181 self.savebox = SaveBox(self, uri, 'text/plain', discard)
182 self.savebox.show()
184 def help(self, button = None):
185 filer.open_dir(rox.app_dir + '/Help')
187 def save_to_stream(self, stream):
188 s = self.buffer.get_start_iter()
189 e = self.buffer.get_end_iter()
190 stream.write(self.buffer.get_text(s, e, TRUE))
192 def set_uri(self, uri):
193 self.uri = uri
194 self.modified = 0
195 self.update_title()
197 def new(self):
198 EditWindow()
200 def change_font(self):
201 style = self.text.get_style().copy()
202 style.font = load_font(options.get('edit_font'))
203 self.text.set_style(style)
205 def show_options(self):
206 options.edit()
208 def set_marked_from_selection(self):
209 self.clear_marked()
210 assert not self.marked
212 buffer = self.buffer
213 s, e = self.get_selection_range()
214 buffer.move_mark(self.mark_start, s)
215 buffer.move_mark(self.mark_end, e)
216 buffer.apply_tag_by_name('marked',
217 buffer.get_iter_at_mark(self.mark_start),
218 buffer.get_iter_at_mark(self.mark_end))
219 self.marked = 1
221 def clear_marked(self):
222 if not self.marked:
223 return
224 self.marked = 0
225 buffer = self.buffer
226 buffer.remove_tag_by_name('marked',
227 buffer.get_iter_at_mark(self.mark_start),
228 buffer.get_iter_at_mark(self.mark_end))
230 class SelectionSaver:
231 def __init__(self, window):
232 self.window = window
233 window.set_marked_from_selection()
235 def save_to_stream(self, stream):
236 s, e = self.window.get_marked_range()
237 stream.write(self.window.buffer.get_text(s, e, TRUE))
239 def destroy(self):
240 # Called when savebox is remove. Get rid of the selection marker
241 self.window.clear_marked()