Guest

Glaview

By: a guest on Oct 10th, 2010  |  syntax: Python  |  size: 2.76 KB  |  hits: 58  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # Glaview - A fast preview for your glade files.
  4. # Copyright (C) 2010 Ba1T <byte-f@live.it>
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16. #
  17. # -------------------------------------------------------------------------------
  18.  
  19. from gtk import *
  20. from pygtk import *
  21. require ('2.0')
  22.  
  23. gf = ''
  24.  
  25. def boom (w, d=None):
  26.         w.destroy ()
  27.        
  28. def run_w (name):
  29.         global gf
  30.         if gf != '':
  31.                 try:
  32.                         bld = Builder ()
  33.                         bld.add_from_file (gf)
  34.                         w = bld.get_object (name)
  35.                         try:
  36.                                 w.set_title (w.get_title () + ' [Glaview Preview]')
  37.                         except:
  38.                                 w.set_title ('Glaview Preview of %s' % (name))
  39.                 except:
  40.                         print "%s:can't get widget %s.."  % (gf, name)
  41.                 w.connect ('destroy', boom)
  42.                 w.show_all ()
  43.                 del bld
  44.                 del w
  45.                
  46.  
  47. def click (w, row, col, d=None):
  48.         name = w.get_model ()[row][0].split (' [')[0]
  49.         run_w (name)   
  50.        
  51. def parse (s, t):
  52.         l = t.get_model ()
  53.         w = '<?xml '
  54.         l.clear ()
  55.         while True:
  56.                 try:
  57.                         w = s.split (w)[1].split ('<object class="GtkWindow" id="')[1].split ('"')[0]
  58.                         l.append ([w + ' [WINDOW]'])
  59.                         print 'Found a window: ' + str (w)
  60.                 except:
  61.                         try:
  62.                                 w = s.split (w)[1].split ('Dialog" id="')[1].split ('"')[0]
  63.                                 l.append ([w + ' [DIALOG]'])
  64.                                 print 'Found a dialog: ' + str (w)
  65.                         except:
  66.                                 break
  67.  
  68. def drag (tree, cntx, x, y, sel, i, tm):
  69.         global gf
  70.         data = sel.data
  71.         gf = data.split ('file://')[1].split ('\r\n')[0]
  72.         try:
  73.                 f = file (gf, 'r')
  74.                 r = f.read ()
  75.                 f.close ()
  76.         except:
  77.                 print "Can't open glade file."
  78.                 return
  79.         parse (r, tree)
  80.  
  81. #window
  82. win = Window (WINDOW_TOPLEVEL)
  83. win.set_title ('Drag your glade file in the window')
  84. win.set_size_request (350, 150);
  85. win.connect ('destroy', main_quit)
  86.  
  87. #liststore
  88. lst = ListStore (str)
  89.  
  90. #treeview
  91. tree = TreeView (lst)
  92. tree.enable_model_drag_source (gdk.BUTTON1_MASK, [('text/plain', 0, 0)],
  93.                                                                                                 gdk.ACTION_DEFAULT)
  94. tree.enable_model_drag_dest ([('text/plain', 0, 0)], gdk.ACTION_DEFAULT)               
  95. col = TreeViewColumn ("Windows", CellRendererText (), text=0)
  96. tree.append_column (col)
  97. tree.connect ('drag-data-received', drag)
  98. tree.connect ('row-activated', click)
  99. win.add (tree)
  100.  
  101. #show all and start program
  102. win.show_all ()
  103. main ()