pgconsole/editor.py
changeset 76 3a41b351b122
parent 10 f3a1b9792cc9
--- a/pgconsole/editor.py	Thu Mar 07 18:26:52 2013 +0100
+++ b/pgconsole/editor.py	Sun Mar 10 16:14:53 2013 +0100
@@ -1,88 +1,90 @@
-import gtk, pango
-import gtksourceview2 as gtksourceview
+from gi.repository import Gtk
+from gi.repository import Pango
+from gi.repository import GtkSource
+
 from lxml import etree
 
-from panedext import HPanedExt
-from config import cfg
+from pgconsole.panedext import HPanedExt
+from pgconsole.config import cfg
 
 
 class Editor(HPanedExt):
     def __init__(self):
         super(Editor, self).__init__()
 
-        self.view = gtksourceview.View()
+        self.view = GtkSource.View()
         self.view.connect('toggle-overwrite', self.on_toggle_overwrite)
 
-        vbox = gtk.VBox()
+        vbox = Gtk.VBox()
         self.vbox1 = vbox
         self.add1(vbox)
 
-        sw = gtk.ScrolledWindow()
-        sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
-        sw.set_shadow_type(gtk.SHADOW_IN)
-        vbox.pack_start(sw)
-        tree = gtk.TreeView()
+        sw = Gtk.ScrolledWindow()
+        sw.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
+        sw.set_shadow_type(Gtk.ShadowType.IN)
+        vbox.pack_start(sw, expand=True, fill=True, padding=0)
+        tree = Gtk.TreeView()
         tree.set_headers_visible(False)
-        tree.get_selection().set_mode(gtk.SELECTION_BROWSE)
+        tree.get_selection().set_mode(Gtk.SelectionMode.BROWSE)
         sw.add(tree)
 
-        model = gtk.ListStore(str, str, object, bool)  # title, filename, buffer, modified
+        model = Gtk.ListStore(str, str, object, bool)  # title, filename, buffer, modified
         tree.set_model(model)
-        cell = gtk.CellRendererPixbuf()
-        cell.set_property('stock-id', gtk.STOCK_SAVE)
-        column = gtk.TreeViewColumn("File", cell, visible=3)
+        cell = Gtk.CellRendererPixbuf()
+        cell.set_property('stock-id', Gtk.STOCK_SAVE)
+        column = Gtk.TreeViewColumn("File", cell, visible=3)
         tree.append_column(column)
-        column = gtk.TreeViewColumn("File", gtk.CellRendererText(), text=0)
+        column = Gtk.TreeViewColumn("File", Gtk.CellRendererText(), text=0)
         tree.append_column(column)
         tree.get_selection().connect('changed', self.item_change)
         tree.set_property('can-focus', False)
         self.filelist = tree
 
 
-        hbox = gtk.HBox()
+        hbox = Gtk.HBox()
 
-        img = gtk.Image()
-        img.set_from_stock(gtk.STOCK_NEW, gtk.ICON_SIZE_SMALL_TOOLBAR)
-        btn = gtk.Button()
-        btn.set_relief(gtk.RELIEF_NONE)
+        img = Gtk.Image()
+        img.set_from_stock(Gtk.STOCK_NEW, Gtk.IconSize.SMALL_TOOLBAR)
+        btn = Gtk.Button()
+        btn.set_relief(Gtk.ReliefStyle.NONE)
         btn.set_focus_on_click(False)
         btn.set_image(img)
         btn.connect('clicked', self.item_new)
-        hbox.pack_start(btn, expand=False)
+        hbox.pack_start(btn, expand=False, fill=True, padding=0)
 
-        img = gtk.Image()
-        img.set_from_stock(gtk.STOCK_OPEN, gtk.ICON_SIZE_SMALL_TOOLBAR)
-        btn = gtk.Button()
-        btn.set_relief(gtk.RELIEF_NONE)
+        img = Gtk.Image()
+        img.set_from_stock(Gtk.STOCK_OPEN, Gtk.IconSize.SMALL_TOOLBAR)
+        btn = Gtk.Button()
+        btn.set_relief(Gtk.ReliefStyle.NONE)
         btn.set_focus_on_click(False)
         btn.set_image(img)
         btn.connect('clicked', self.item_open)
-        hbox.pack_start(btn, expand=False)
+        hbox.pack_start(btn, expand=False, fill=True, padding=0)
 
-        img = gtk.Image()
-        img.set_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_SMALL_TOOLBAR)
-        btn = gtk.Button()
-        btn.set_relief(gtk.RELIEF_NONE)
+        img = Gtk.Image()
+        img.set_from_stock(Gtk.STOCK_CLOSE, Gtk.IconSize.SMALL_TOOLBAR)
+        btn = Gtk.Button()
+        btn.set_relief(Gtk.ReliefStyle.NONE)
         btn.set_focus_on_click(False)
         btn.set_image(img)
         btn.connect('clicked', self.item_close)
-        hbox.pack_start(btn, expand=False)
-        hbox.connect('size-request', self.leftbuttons_size_request)
+        hbox.pack_start(btn, expand=False, fill=True, padding=0)
+        hbox.connect('size-allocate', self.leftbuttons_size_request)
 
-        vbox.pack_start(hbox, expand=False)
+        vbox.pack_start(hbox, expand=False, fill=True, padding=0)
 
-        vbox = gtk.VBox()
+        vbox = Gtk.VBox()
         vbox.set_property("width-request", 200)
         self.add2(vbox)
         self.child_set_property(vbox, 'shrink', False)
 
 
         # scroll
-        sw = gtk.ScrolledWindow()
-        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
-        sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
+        sw = Gtk.ScrolledWindow()
+        sw.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
+        sw.set_shadow_type(Gtk.ShadowType.ETCHED_IN)
         sw.add(self.view)
-        vbox.pack_start(sw)
+        vbox.pack_start(sw, expand=True, fill=True, padding=0)
 
 
         self.view.set_show_line_numbers(True)
@@ -92,76 +94,76 @@
         self.view.set_auto_indent(True)
 
         # font
-        font_desc = pango.FontDescription('monospace')
+        font_desc = Pango.FontDescription('monospace')
         if font_desc:
             self.view.modify_font(font_desc)
 
         # status bar
-        hbox = gtk.HBox()
+        hbox = Gtk.HBox()
 
         self.st_file, fr1 = self.construct_status('SQL snippet')
-        self.st_file.set_ellipsize(pango.ELLIPSIZE_START)
+        self.st_file.set_ellipsize(Pango.EllipsizeMode.START)
         self.st_insovr, fr2 = self.construct_status('INS')
         self.st_linecol, fr3 = self.construct_status('Line: 0 Col: 0')
 
 
-        img = gtk.Image()
-        img.set_from_stock(gtk.STOCK_SAVE, gtk.ICON_SIZE_SMALL_TOOLBAR)
+        img = Gtk.Image()
+        img.set_from_stock(Gtk.STOCK_SAVE, Gtk.IconSize.SMALL_TOOLBAR)
         #save = img
-        save = gtk.Button()
-        save.set_relief(gtk.RELIEF_NONE)
+        save = Gtk.Button()
+        save.set_relief(Gtk.ReliefStyle.NONE)
         save.set_focus_on_click(False)
         save.set_image(img)
         save.connect('clicked', self.item_save)
-        hbox.pack_start(save, expand=False)
+        hbox.pack_start(save, expand=False, fill=True, padding=0)
 
-        hbox.pack_start(fr1, expand=True)
-        hbox.pack_start(fr2, expand=False)
-        hbox.pack_start(fr3, expand=False)
+        hbox.pack_start(fr1, expand=True, fill=True, padding=0)
+        hbox.pack_start(fr2, expand=False, fill=True, padding=0)
+        hbox.pack_start(fr3, expand=False, fill=True, padding=0)
 
-        #sep = gtk.HSeparator()
+        #sep = Gtk.HSeparator()
         #vbox.pack_start(sep, expand=False, fill=False, padding=0)
 
-        #align = gtk.Alignment()
+        #align = Gtk.Alignment()
         #align.set_property("bottom-padding", 3)
         #align.set_property("xscale", 1.0)
         #align.add(hbox)
 
-        #frame = gtk.Frame()
+        #frame = Gtk.Frame()
         #frame.add(hbox)
-        #frame.set_shadow_type(gtk.SHADOW_ETCHED_IN)
-        vbox.pack_start(hbox, expand=False, padding=0)
+        #frame.set_shadow_type(Gtk.SHADOW_ETCHED_IN)
+        vbox.pack_start(hbox, expand=False, fill=True, padding=0)
 
         self.load_nodes()
         self.build_context_menu()
 
 
     def build_context_menu(self):
-        menu = gtk.Menu()
-        item = gtk.ImageMenuItem(gtk.STOCK_SAVE, "Save")
+        menu = Gtk.Menu()
+        item = Gtk.ImageMenuItem(use_stock=True, label=Gtk.STOCK_SAVE)
         item.connect("activate", self.item_save)
         item.show()
         menu.append(item)
 
-        item = gtk.ImageMenuItem(gtk.STOCK_SAVE_AS, "Save as")
+        item = Gtk.ImageMenuItem(use_stock=True, label=Gtk.STOCK_SAVE_AS)
         item.connect("activate", self.item_save_as)
         item.show()
         menu.append(item)
 
-        item = gtk.ImageMenuItem(gtk.STOCK_CLOSE, "Close")
+        item = Gtk.ImageMenuItem(use_stock=True, label=Gtk.STOCK_CLOSE)
         item.connect("activate", self.item_close)
         item.show()
         menu.append(item)
 
-        item = gtk.SeparatorMenuItem()
+        item = Gtk.SeparatorMenuItem()
         item.show()
         menu.append(item)
 
-        item = gtk.ImageMenuItem(gtk.STOCK_GO_UP, "Move up")
+        item = Gtk.ImageMenuItem(use_stock=True, label=Gtk.STOCK_GO_UP)
         item.show()
         menu.append(item)
 
-        item = gtk.ImageMenuItem(gtk.STOCK_GO_DOWN, "Move down")
+        item = Gtk.ImageMenuItem(use_stock=True, label=Gtk.STOCK_GO_DOWN)
         item.show()
         menu.append(item)
 
@@ -179,24 +181,24 @@
                 path, col, cellx, celly = pathinfo
                 self.filelist.grab_focus()
                 self.filelist.set_cursor(path, col, 0)
-                self.filelist_menu.popup(None, None, None, event.button, event.time)
+                self.filelist_menu.popup(None, None, None, None, event.button, event.time)
             return True
 
 
 
     def make_buffer(self):
-        buffer = gtksourceview.Buffer()
+        buffer = GtkSource.Buffer()
         buffer.connect('mark-set', self.buffer_mark_set)
         buffer.connect('changed', self.buffer_changed)
 
         # style
-        mgr = gtksourceview.style_scheme_manager_get_default()
+        mgr = GtkSource.StyleSchemeManager.get_default()
         style_scheme = mgr.get_scheme('kate')
         if style_scheme:
             buffer.set_style_scheme(style_scheme)
 
         # syntax
-        lngman = gtksourceview.language_manager_get_default()
+        lngman = GtkSource.LanguageManager.get_default()
         langsql = lngman.get_language('sql')
         buffer.set_language(langsql)
         buffer.set_highlight_syntax(True)
@@ -216,7 +218,10 @@
                 buffer.set_text(node.text)
             if type == 'file':
                 filename = node.text
-                content = open(filename).read()
+                try:
+                    content = open(filename).read()
+                except IOError:
+                    content = ''
                 buffer.set_text(content)
             iter = model.append([name, filename, buffer, False])
             if sel == name:
@@ -240,12 +245,12 @@
 
 
     def construct_status(self, text):
-        st = gtk.Label(text)
+        st = Gtk.Label(text)
         st.set_property("single-line-mode", True)
         st.set_property("xpad", 3)
         st.set_alignment(0.0, 0.5)
-        fr = gtk.Frame()
-        fr.set_shadow_type(gtk.SHADOW_ETCHED_OUT)
+        fr = Gtk.Frame()
+        fr.set_shadow_type(Gtk.ShadowType.ETCHED_OUT)
         fr.add(st)
         return st, fr
 
@@ -319,23 +324,23 @@
 
 
     def item_open(self, w):
-        dialog = gtk.FileChooserDialog(title='Open file', action=gtk.FILE_CHOOSER_ACTION_OPEN,
-            buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))
-        dialog.set_default_response(gtk.RESPONSE_OK)
+        dialog = Gtk.FileChooserDialog(title='Open file', action=Gtk.FileChooserAction.OPEN,
+            buttons=(Gtk.STOCK_CANCEL,Gtk.ResponseType.CANCEL,Gtk.STOCK_OPEN,Gtk.ResponseType.OK))
+        dialog.set_default_response(Gtk.ResponseType.OK)
         dialog.set_select_multiple(True)
 
-        filter = gtk.FileFilter()
+        filter = Gtk.FileFilter()
         filter.set_name("SQL files")
         filter.add_pattern("*.sql")
         dialog.add_filter(filter)
 
-        filter = gtk.FileFilter()
+        filter = Gtk.FileFilter()
         filter.set_name("All files")
         filter.add_pattern("*")
         dialog.add_filter(filter)
 
         response = dialog.run()
-        if response == gtk.RESPONSE_OK:
+        if response == Gtk.ResponseType.OK:
             filenames = dialog.get_filenames()
             for fname in filenames:
                 self.open_file(fname)
@@ -393,24 +398,24 @@
 
         path, file = filename.rsplit('/',1)
 
-        dialog = gtk.FileChooserDialog(title='Save as', action=gtk.FILE_CHOOSER_ACTION_SAVE,
-            buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_SAVE,gtk.RESPONSE_OK))
-        dialog.set_default_response(gtk.RESPONSE_OK)
+        dialog = Gtk.FileChooserDialog(title='Save as', action=Gtk.FileChooserAction.SAVE,
+            buttons=(Gtk.STOCK_CANCEL,Gtk.ResponseType.CANCEL,Gtk.STOCK_SAVE,Gtk.ResponseType.OK))
+        dialog.set_default_response(Gtk.ResponseType.OK)
         dialog.set_current_folder(path)
         dialog.set_current_name(file)
 
-        filter = gtk.FileFilter()
+        filter = Gtk.FileFilter()
         filter.set_name("SQL files")
         filter.add_pattern("*.sql")
         dialog.add_filter(filter)
 
-        filter = gtk.FileFilter()
+        filter = Gtk.FileFilter()
         filter.set_name("All files")
         filter.add_pattern("*")
         dialog.add_filter(filter)
 
         response = dialog.run()
-        if response == gtk.RESPONSE_OK:
+        if response == Gtk.ResponseType.OK:
             filename = dialog.get_filename()
             data = buffer.get_text(*buffer.get_bounds())
             open(filename, 'w').write(data)
@@ -430,8 +435,8 @@
             return
         iter = model.get_iter(sel[0])
         newiter = model.iter_next(iter)
-        if newiter is None and sel[0][0] > 0:
-            newiter = model.get_iter((sel[0][0]-1,))
+        if newiter is None and sel[0].get_indices()[0] > 0:
+            newiter = model.get_iter((sel[0].get_indices()[0]-1,))
 
         title, buffer = model.get(iter, 0, 2)
         #buffer.destroy()
@@ -450,6 +455,6 @@
 
 
     def leftbuttons_size_request(self, w, request):
-        self.set_snap1(request[0])
+        self.set_snap1(request.width)
         return True