aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xui.py56
-rw-r--r--ui.xml46
2 files changed, 51 insertions, 51 deletions
diff --git a/ui.py b/ui.py
index b2a3cb4..50d257e 100755
--- a/ui.py
+++ b/ui.py
@@ -3,7 +3,7 @@
'''GTK front-end for dcparse.'''
-import decimal
+import os.path
import gi
gi.require_version('Gtk', '3.0')
@@ -12,46 +12,14 @@ from gi.repository import Gtk as gtk, Gio as gio, Pango as pango
import dcparse
-class VisualDC(gtk.Application):
- '''Common application actions.'''
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs, application_id='uk.ac.cam.tw466.vdc',
- flags=gio.ApplicationFlags.HANDLES_COMMAND_LINE)
+class ApplicationEventHandler:
+ def __init__(self, stack):
+ self.stack = stack
- self.window = None
- if self.prefers_app_menu():
- self.set_app_menu(...)
- else:
- # menu bar etc
- pass
-
- def do_activate(self):
- if not self.window:
- self.window = ApplicationWindow(application=self)
- self.window.present()
-
- def do_command_line(self, command_line):
- options = command_line.get_options_dict().end().unpack()
- self.activate()
- return 0
-
-
-class ApplicationWindow(gtk.ApplicationWindow):
- '''Main application window.'''
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs, title='Visual dc', border_width=5)
+ def on_destroy_main(self, *_):
+ gtk.main_quit()
- box = gtk.Box(orientation=gtk.Orientation.VERTICAL, spacing=5)
- self.add(box)
-
- self.stack = DCStack()
- box.pack_end(self.stack, expand=True, fill=True, padding=0)
-
- code_entry = gtk.TextView(editable=True, cursor_visible=True)
- code_entry.get_buffer().connect('changed', self._on_code_changed)
- box.pack_start(code_entry, expand=True, fill=True, padding=0)
-
- def _on_code_changed(self, widget):
+ def on_change_code(self, widget):
cmds = dcparse.CommandSequence(widget.get_text(
widget.get_start_iter(), widget.get_end_iter(),
include_hidden_chars=False))
@@ -66,6 +34,10 @@ class ApplicationWindow(gtk.ApplicationWindow):
class DCStack(dcparse.Stack, gtk.ListBox):
'''A ListBox representing a dc stack.'''
+ # Needed for gtk.Builder to recognise this class.
+ # https://eeperry.wordpress.com/2013/01/05/pygtk-new-style-python-class-using-builder/
+ __gtype_name__ = 'DCStack'
+
def __init__(self):
super().__init__()
gtk.ListBox.__init__(self, selection_mode=gtk.SelectionMode.NONE)
@@ -109,9 +81,9 @@ class StackValue(gtk.ListBoxRow):
def main():
'''Main entry point.'''
- win = ApplicationWindow()
- win.connect('destroy', gtk.main_quit)
- win.show_all()
+ builder = gtk.Builder.new_from_file(os.path.join(os.path.dirname(__file__), 'ui.xml'))
+ builder.connect_signals(ApplicationEventHandler(builder.get_object('main_stack')))
+ builder.get_object('main_window').show_all()
gtk.main()
diff --git a/ui.xml b/ui.xml
index 5eb8a35..34a7148 100644
--- a/ui.xml
+++ b/ui.xml
@@ -1,12 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
- <menu id="app-menu">
- <section>
- <item>
- <attribute name="action">app.quit</attribute>
- <attribute name="label" translatable="yes">_Quit</attribute>
- <attribute name="accel">&lt;Primary&gt;q</attribute>
- </item>
- </section>
- </menu>
+ <!-- interface-requires gtk+ 3.0 -->
+ <object class="GtkTextBuffer" id="code_buffer">
+ <signal name="changed" handler="on_change_code" />
+ </object>
+
+ <object class="GtkApplicationWindow" id="main_window">
+ <property name="title">Visual dc</property>
+ <property name="border-width">5</property>
+ <signal name="destroy" handler="on_destroy_main" />
+ <child>
+ <object class="GtkBox">
+ <property name="orientation">vertical</property>
+ <property name="spacing">5</property>
+ <child>
+ <object class="GtkTextView">
+ <property name="editable">True</property>
+ <property name="cursor-visible">True</property>
+ <property name="buffer">code_buffer</property>
+ </object>
+ <packing>
+ <property name="expand">True</property>
+ <property name="fill">True</property>
+ <property name="padding">0</property>
+ </packing>
+ </child>
+ <child>
+ <object class="DCStack" id="main_stack">
+ </object>
+ <packing>
+ <property name="expand">True</property>
+ <property name="fill">True</property>
+ <property name="padding">0</property>
+ </packing>
+ </child>
+ </object>
+ </child>
+ </object>
</interface>