More tasteful colouring.
[dom-editor.git] / Dome / Window.py
blob45c0315b267e4241fa85b41ba0f32c30436434cd
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 # Make it square, to cope with Xinerama
23 size = min(g.gdk.screen_width(), g.gdk.screen_height())
24 size = size * 3 / 4
26 self.set_default_size(size, size)
27 self.set_position(g.WIN_POS_CENTER)
28 self.savebox = None
30 self.show()
31 g.gdk.flush()
33 if path:
34 import os.path
35 path = os.path.abspath(path)
37 import Model
38 self.model = Model.Model(path, dome_data = data)
39 self.gui_view = None
40 self.dome_state = ""
42 from GUIView import GUIView
43 #from Display2 import Display as GUIView
44 from List import List
46 vbox = g.VBox(FALSE, 0)
47 self.add(vbox)
49 tools = g.Toolbar()
50 tools.set_style(g.TOOLBAR_ICONS)
51 vbox.pack_start(tools, FALSE, TRUE, 0)
52 tools.show()
54 tools.insert_stock(g.STOCK_HELP, 'Help', None, self.tool_help, None, 0)
55 tools.insert_stock(g.STOCK_JUMP_TO, 'Step', None, self.tool_step, None, 0)
56 tools.insert_stock(g.STOCK_GO_FORWARD, 'Next', None, self.tool_next, None, 0)
57 tools.insert_stock(g.STOCK_GOTO_LAST, 'Play', None, self.tool_play, None, 0)
58 tools.insert_stock(g.STOCK_STOP, 'Stop', None, self.tool_stop, None, 0)
59 tools.insert_stock(g.STOCK_NO, 'Record', None, self.tool_record, None, 0)
60 tools.insert_stock(g.STOCK_SAVE, 'Save', None, self.tool_save, None, 0)
62 paned = g.HPaned()
63 vbox.pack_start(paned)
65 import View
66 view = View.View(self.model)
67 self.view = view
68 self.list = List(view)
70 paned.add1(self.list)
71 self.list.show()
73 #swin = g.ScrolledWindow()
74 #swin.set_policy(g.POLICY_AUTOMATIC, g.POLICY_ALWAYS)
75 #paned.add2(swin)
76 paned.set_position(200)
78 self.gui_view = GUIView(self, view)
79 #swin.add(self.gui_view)
80 paned.add2(self.gui_view)
81 #swin.set_hadjustment(self.gui_view.get_hadjustment())
82 #swin.set_vadjustment(self.gui_view.get_vadjustment())
84 vbox.show_all()
86 self.gui_view.grab_focus()
87 self.update_title()
89 def delete(window, event):
90 if self.model.root_program.modified:
91 self.save(discard = 1)
92 return 1
93 return 0
94 self.connect('delete-event', delete)
96 def discard(self):
97 self.destroy()
99 def set_state(self, state):
100 if state == self.dome_state:
101 return
102 if state:
103 self.dome_state = " " + state
104 else:
105 self.dome_state = ""
107 self.update_title()
109 def update_title(self):
110 title = self.model.uri
111 self.set_title(title + self.dome_state)
113 def save(self, discard = 0):
114 if self.savebox:
115 self.savebox.destroy()
116 path = self.model.uri
118 self.savebox = saving.SaveBox(self, path,
119 'application/x-dome', discard = discard)
120 self.savebox.set_transient_for(self)
121 self.savebox.set_destroy_with_parent(True)
122 radio_dome = g.RadioButton(None, 'Save everything (.dome)')
123 radio_xml = g.RadioButton(radio_dome, 'Export data as XML')
124 radio_html = g.RadioButton(radio_xml, 'Export data as HTML')
126 self.save_radios = (
127 (radio_dome, 'dome', 'application/x-dome', self.save_as_dome),
128 (radio_xml, 'xml', 'text/xml', self.save_as_xml),
129 (radio_html, 'html', 'text/html', self.save_as_html))
131 def changed(toggle = None):
132 name = self.savebox.save_area.entry.get_text()
133 if name.endswith('.xml'):
134 name = name[:-4]
135 elif name.endswith('.dome'):
136 name = name[:-5]
137 elif name.endswith('.html'):
138 name = name[:-5]
140 for radio, ext, mime, fn in self.save_radios:
141 if radio.get_active():
142 name += '.' + ext
143 self.savebox.set_type(mime)
144 self.savebox.save_area.entry.set_text(name)
146 for radio, ext, mime, fn in self.save_radios:
147 self.savebox.vbox.pack_start(radio, False, True, 0)
148 radio.connect('toggled', changed)
150 for radio, ext, mime, fn in self.save_radios:
151 if path.endswith('.' + ext):
152 radio.set_active(True)
154 self.savebox.vbox.show_all()
155 self.savebox.show()
157 def write(self, text):
158 if type(text) == unicode:
159 text = utf8_encoder(text)[0]
160 self.output_data.write(text)
162 def save_to_stream(self, stream):
163 for radio, ext, mime, fn in self.save_radios:
164 if radio.get_active():
165 fn(stream)
167 def save_as_dome(self, stream):
168 print "Saving", self.view.root
169 #self.view.model.strip_space()
170 doc = self.view.export_all()
171 print "Writing data..."
172 PrettyPrint(doc, stream)
174 def save_as_xml(self, stream):
175 print "Saving XML", self.view.root
176 doc = self.view.model.doc
177 print "Writing data..."
178 PrettyPrint(doc, stream)
180 def save_as_html(self, stream):
181 from xml.dom.html import HTMLDocument
182 import to_html
183 print "Saving HTML", self.view.root
184 print >>stream, to_html.to_html(self.view.root.ownerDocument)
186 def set_uri(self, uri):
187 if self.save_radios[0][0].get_active():
188 self.model.uri = uri
189 self.model.root_program.modified = 0
190 self.update_title()
192 # Toolbar bits
194 def tool_save(self, button = None):
195 self.save()
197 def tool_stop(self, button = None):
198 if self.view.rec_point:
199 self.view.stop_recording()
200 if self.view.running():
201 self.view.single_step = 1
202 else:
203 self.view.run_new()
205 def tool_play(self, button = None):
206 from View import InProgress, Done
207 # Step first, in case we're on a breakpoint
208 self.view.single_step = 1
209 try:
210 self.view.do_one_step()
211 except InProgress, Done:
212 pass
213 self.view.single_step = 0
214 self.view.sched()
216 def tool_next(self, button = None):
217 from View import InProgress, Done
218 self.view.single_step = 2
219 try:
220 self.view.do_one_step()
221 except InProgress, Done:
222 pass
224 def tool_step(self, button = None):
225 from View import InProgress, Done
226 self.view.single_step = 1
227 try:
228 self.view.do_one_step()
229 except InProgress, Done:
230 pass
232 def tool_record(self, button = None):
233 if self.view.rec_point:
234 self.view.stop_recording()
235 else:
236 self.view.record_at_point()
238 def tool_help(self, button = None):
239 rox.filer.show_file(os.path.join(rox.app_dir, 'Help', 'README'))