Throttler.pm dependencies
[hband-tools.git] / xgui-tools / SelectDirectory
blobd2d75b0f1f26ed73976eabfc39fdd2d9d6abca38
1 #!/usr/bin/env python
3 """
4 Simple directory chooser GUI. Useful for embedding in GUI programms.
5 """
7 import os
8 import gtk
10 class StockButton(gtk.Button):
11 def __init__(self, label=None, stock=None, use_underline=True, icon_size=None):
12 if stock is not None and stock in gtk.stock_list_ids():
13 stock_tmp = stock
14 else:
15 stock_tmp = gtk.STOCK_ABOUT
16 super(self.__class__, self).__init__(stock=stock_tmp, use_underline=use_underline)
17 if label is not None:
18 self.set_markup(label)
19 if stock is None:
20 self.set_icon('')
21 elif stock not in gtk.stock_list_ids():
22 self.set_icon(stock)
23 if icon_size is not None:
24 self.set_icon(stock, icon_size)
25 def __get_children(self):
26 align = self.get_children()[0]
27 hbox = align.get_children()[0]
28 return hbox.get_children()
29 def set_label(self, label):
30 x, lbl = self.__get_children()
31 lbl.set_label(label)
32 def set_markup(self, label):
33 x, lbl = self.__get_children()
34 lbl.set_markup(label)
35 def set_icon(self, icon, size=gtk.ICON_SIZE_BUTTON):
36 img, x = self.__get_children()
37 if type(icon) == str:
38 if icon == '':
39 img.props.visible = False
40 else:
41 img.set_from_icon_name(icon, size)
42 img.props.visible = True
43 else:
44 img.set_from_pixbuf(icon)
45 img.props.visible = True
48 dialog = gtk.FileChooserDialog("Select a Directory", None, gtk.FILE_CHOOSER_ACTION_SAVE, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OK, gtk.RESPONSE_OK))
49 dialog.set_action(gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER)
50 button_cwd = StockButton(label="Working Dir", stock=gtk.STOCK_JUMP_TO)
51 dialog.add_action_widget(button_cwd, gtk.RESPONSE_NO)
52 button_cwd.show()
54 while True:
55 response = dialog.run()
56 if response == gtk.RESPONSE_NO:
57 dialog.set_current_folder(os.getcwd())
58 continue
60 if response == gtk.RESPONSE_OK:
61 print dialog.get_filename()
62 status = 0
63 else:
64 status = 1
65 break
67 dialog.destroy()
68 exit(status)