2 from rox
import g
, filer
4 #from rox.XDSLoader import XDSLoader
9 from rox
.Menu
import Menu
, set_save_name
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)
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')
52 tools
.set_style(g
.TOOLBAR_ICONS
)
53 vbox
.pack_start(tools
, FALSE
, TRUE
, 0)
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)
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
)
67 self
.connect('destroy', lambda x
: rox
.toplevel_unref())
69 self
.connect('delete-event', self
.delete_event
)
70 self
.text
.grab_focus()
75 self
.uri
= os
.path
.abspath(filename
)
81 # Loading might take a while, so get something on the screen
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
):
97 menu
.popup(self
, event
)
100 def delete_event(self
, window
, event
):
102 self
.save(discard
= 1)
106 def update_title(self
):
113 self
.set_title(title
)
115 def changed(self
, buffer):
116 if not self
.modified
:
120 #def xds_load_data(self, data):
121 # if self.has_selection():
122 # self.text.delete_selection()
123 # self.text.insert_defaults(data)
125 # self.update_title()
127 def load_file(self
, path
):
132 file = open(path
, 'r')
133 contents
= file.read()
136 self
.buffer.insert_at_cursor(contents
, -1)
138 rox
.report_exception()
140 def close(self
, button
= None):
142 self
.save(discard
= 1)
149 def up(self
, button
= None):
151 filer
.show_file(self
.uri
)
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
)
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
)
169 def save(self
, widget
= None, discard
= 0):
170 from rox
.SaveBox
import 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())
180 uri
= self
.uri
or options
.get('edit_default_name')
181 self
.savebox
= SaveBox(self
, uri
, 'text/plain', discard
)
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
):
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
):
208 def set_marked_from_selection(self
):
210 assert not self
.marked
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
))
221 def clear_marked(self
):
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
):
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
))
240 # Called when savebox is remove. Get rid of the selection marker
241 self
.window
.clear_marked()