dlg/fileopen.py

Go to the documentation of this file.
00001 #
00002 # Copyright (c) 2006 Cristian L. Vlasceanu
00003 #
00004 # Permission is hereby granted, free of charge, to any person
00005 # obtaining a copy of this software and associated documentation
00006 # files (the "Software"), to deal in the Software without
00007 # restriction, including without limitation the rights to use,
00008 # copy, modify, merge, publish, distribute, sublicense, and/or
00009 # sell copies of the Software, and to permit persons to whom
00010 # the Software is furnished to do so, subject to the following
00011 # conditions:
00012 # 
00013 # The above copyright notice and this permission notice shall be
00014 # included in all copies or substantial portions of the Software.
00015 # 
00016 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
00017 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
00018 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
00019 # AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
00020 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
00021 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00022 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
00023 # OR OTHER DEALINGS IN THE SOFTWARE.
00024 # 
00025 # NOTE: The above applies solely to Python source code provided 
00026 # herein, and it DOES NOT COVER the Zero Debugger Engine and plug-ins.
00027 #
00028 import gtk
00029 import os.path
00030 import zero
00031 
00032 
00033 def set_filename(fileButton, process):
00034         name = process.name()
00035         basename = os.path.basename(name)
00036         btn = fileButton.get_children()[0]
00037         box = btn.get_children()[0]
00038         image = box.get_children()[0]
00039         label = box.get_children()[1]
00040         iconName = "gnome-mime-application-x-executable"
00041         if process.origin() == zero.Process.Origin.Core:
00042                 iconName = "gnome-mime-application-x-core"
00043 
00044         image.set_from_icon_name(iconName, gtk.ICON_SIZE_MENU)
00045         label.set_text(basename)
00046 
00047 
00048 class FileOpen(object):
00049         """
00050         Implements the logic associated with File Chooser Dialog
00051         """
00052         def __init__(self, fileChooser, filters = ()):
00053                 self.__filters = filters
00054 
00055                 box = gtk.HBox()
00056                 try:
00057                         fileChooser.set_extra_widget(box)
00058                         fileChooser.connect("file-activated", self.on_file_activated)
00059                         fileChooser.connect("update-preview", self.on_update_preview, box)
00060                         for f in filters:
00061                                 fileChooser.set_filter(f.filter)
00062                                 f.connect("update_preview", self.on_update_preview)
00063                 except:
00064                         fileChooser.connect("response", self.__on_response)
00065 
00066 
00067         def on_update_preview(self, fileChooser, preview):
00068                 #clear the preview:
00069                 for w in preview.get_children():
00070                         preview.remove(w)
00071                 #pass the preview to filters
00072                 filename = fileChooser.get_preview_filename()
00073                 if filename:
00074                         for f in self.__filters:
00075                                 if f.update_preview(preview, filename):
00076                                         break
00077 
00078 
00079         def on_file_activated(self, chooser):
00080                 filename = chooser.get_filename()
00081                 if filename:
00082                         for f in self.__filters:
00083                                 if f.open(filename):
00084                                         chooser.hide()
00085                                         break
00086 
00087         def __on_response(self, dlg, respID):
00088                 if respID == gtk.RESPONSE_OK:
00089                         self.on_file_activated(dlg)
00090                 else:
00091                         dlg.hide()