Removed some verbose debugging.
[dom-editor.git] / Dome / Window.py
blob3849fd9b67376cb7722e483f0fa0c12c3f497ded
1 import rox
2 from rox import g, TRUE, FALSE, saving
4 import os.path
5 from Ft.Xml.Domlette import PrettyPrint
7 import __main__
9 #from support import *
10 from rox.saving import SaveBox
12 code = None
14 from codecs import lookup
15 utf8_encoder = lookup('UTF-8')[0]
17 class Window(rox.Window, saving.Saveable):
18 def __init__(self, path = None, data = None):
19 # 'data' is used when 'path' is a stylesheet...
20 rox.Window.__init__(self)
22 self.status = g.Statusbar()
23 self.status.push(0, 'Welcome to Dome. Click the right-hand mouse button for menus.')
24 self.showing_message = False
26 # Make it square, to cope with Xinerama
27 size = min(g.gdk.screen_width(), g.gdk.screen_height())
28 size = size * 3 / 4
30 self.set_default_size(size, size)
31 self.set_position(g.WIN_POS_CENTER)
32 self.savebox = None
34 self.show()
35 g.gdk.flush()
37 if path:
38 import os.path
39 path = os.path.abspath(path)
41 import Model
42 self.model = Model.Model(path, dome_data = data)
43 self.gui_view = None
44 self.dome_state = ""
46 from GUIView import GUIView
47 #from Display2 import Display as GUIView
48 from List import List
50 vbox = g.VBox(FALSE, 0)
51 self.add(vbox)
53 tools = g.Toolbar()
54 tools.set_style(g.TOOLBAR_ICONS)
55 vbox.pack_start(tools, FALSE, TRUE, 0)
56 tools.show()
58 tools.insert_stock(g.STOCK_HELP, 'Help', None, self.tool_help, None, 0)
59 tools.insert_stock(g.STOCK_JUMP_TO, 'Step', None, self.tool_step, None, 0)
60 tools.insert_stock(g.STOCK_GO_FORWARD, 'Next', None, self.tool_next, None, 0)
61 tools.insert_stock(g.STOCK_GOTO_LAST, 'Play', None, self.tool_play, None, 0)
62 tools.insert_stock(g.STOCK_STOP, 'Stop', None, self.tool_stop, None, 0)
63 tools.insert_stock(g.STOCK_NO, 'Record', None, self.tool_record, None, 0)
64 tools.insert_stock(g.STOCK_SAVE, 'Save', None, self.tool_save, None, 0)
66 paned = g.HPaned()
67 vbox.pack_start(paned)
69 import View
70 view = View.View(self.model)
71 self.view = view
72 self.list = List(view)
74 paned.add1(self.list)
75 self.list.show()
77 #swin = g.ScrolledWindow()
78 #swin.set_policy(g.POLICY_AUTOMATIC, g.POLICY_ALWAYS)
79 #paned.add2(swin)
80 paned.set_position(200)
82 self.gui_view = GUIView(self, view)
83 #swin.add(self.gui_view)
84 paned.add2(self.gui_view)
85 #swin.set_hadjustment(self.gui_view.get_hadjustment())
86 #swin.set_vadjustment(self.gui_view.get_vadjustment())
88 vbox.pack_start(self.status, False, True, 0)
90 vbox.show_all()
92 self.gui_view.grab_focus()
93 self.update_title()
95 def delete(window, event):
96 if self.model.root_program.modified:
97 self.save(discard = 1)
98 return 1
99 return 0
100 self.connect('delete-event', delete)
102 def set_status(self, message = None):
103 if self.showing_message:
104 self.status.pop(0)
105 if message:
106 self.status.push(0, message)
107 self.showing_message = bool(message)
108 self.status.draw((0, 0, self.status.allocation.width, self.status.allocation.height))
109 g.gdk.flush()
111 def discard(self):
112 self.destroy()
114 def set_state(self, state):
115 if state == self.dome_state:
116 return
117 if state:
118 self.dome_state = " " + state
119 else:
120 self.dome_state = ""
122 self.update_title()
124 def update_title(self):
125 title = self.model.uri
126 self.set_title(title + self.dome_state)
128 def save(self, discard = 0):
129 if self.savebox:
130 self.savebox.destroy()
131 path = self.model.uri
133 self.savebox = saving.SaveBox(self, path,
134 'application/x-dome', discard = discard)
135 self.savebox.set_transient_for(self)
136 self.savebox.set_destroy_with_parent(True)
137 radio_dome = g.RadioButton(None, 'Save everything (.dome)')
138 radio_xml = g.RadioButton(radio_dome, 'Export data as XML')
139 radio_html = g.RadioButton(radio_xml, 'Export data as XHTML')
141 self.save_radios = (
142 (radio_dome, 'dome', 'application/x-dome', self.save_as_dome),
143 (radio_xml, 'xml', 'text/xml', self.save_as_xml),
144 (radio_html, 'xhtml', 'application/xhtml+xml', self.save_as_html))
146 def changed(toggle = None):
147 name = self.savebox.save_area.entry.get_text()
148 if name.endswith('.xml'):
149 name = name[:-4]
150 elif name.endswith('.dome'):
151 name = name[:-5]
152 elif name.endswith('.xhtml'):
153 name = name[:-6]
155 for radio, ext, mime, fn in self.save_radios:
156 if radio.get_active():
157 name += '.' + ext
158 self.savebox.set_type(mime)
159 self.savebox.save_area.entry.set_text(name)
161 for radio, ext, mime, fn in self.save_radios:
162 self.savebox.vbox.pack_start(radio, False, True, 0)
163 radio.connect('toggled', changed)
165 for radio, ext, mime, fn in self.save_radios:
166 if path.endswith('.' + ext):
167 radio.set_active(True)
169 self.savebox.vbox.show_all()
170 self.savebox.show()
172 def write(self, text):
173 if type(text) == unicode:
174 text = utf8_encoder(text)[0]
175 self.output_data.write(text)
177 def save_to_stream(self, stream):
178 self.set_status('Saving...')
179 try:
180 for radio, ext, mime, fn in self.save_radios:
181 if radio.get_active():
182 fn(stream)
183 finally:
184 self.set_status()
186 def save_as_dome(self, stream):
187 print "Saving", self.view.root
188 #self.view.model.strip_space()
189 doc = self.view.export_all()
190 print "Writing data..."
191 PrettyPrint(doc, stream)
193 def save_as_xml(self, stream):
194 print "Saving XML", self.view.root
195 doc = self.view.model.doc
196 print "Writing data..."
197 PrettyPrint(doc, stream)
199 def save_as_html(self, stream):
200 from xml.dom.html import HTMLDocument
201 import to_html
202 print "Saving HTML", self.view.root
203 print >>stream, to_html.to_html(self.view.root.ownerDocument)
205 def set_uri(self, uri):
206 if self.save_radios[0][0].get_active():
207 self.model.uri = uri
208 self.model.root_program.modified = 0
209 self.update_title()
211 # Toolbar bits
213 def tool_save(self, button = None):
214 self.save()
216 def tool_stop(self, button = None):
217 if self.view.rec_point:
218 self.view.stop_recording()
219 if self.view.running():
220 self.view.single_step = 1
221 else:
222 self.view.run_new()
224 def tool_play(self, button = None):
225 from View import InProgress, Done
226 # Step first, in case we're on a breakpoint
227 self.view.single_step = 1
228 try:
229 self.view.do_one_step()
230 except InProgress, Done:
231 pass
232 self.view.single_step = 0
233 self.view.sched()
235 def tool_next(self, button = None):
236 from View import InProgress, Done
237 self.view.single_step = 2
238 try:
239 self.view.do_one_step()
240 except InProgress, Done:
241 pass
243 def tool_step(self, button = None):
244 from View import InProgress, Done
245 self.view.single_step = 1
246 try:
247 self.view.do_one_step()
248 except InProgress, Done:
249 pass
251 def tool_record(self, button = None):
252 if self.view.rec_point:
253 self.view.stop_recording()
254 else:
255 self.view.record_at_point()
257 def tool_help(self, button = None):
258 rox.filer.show_file(os.path.join(rox.app_dir, 'Help', 'README'))