0d30b34a202e9f77d6a1da000bb2ac6e8eb56b66
[minwii.git] / src / pgu / gui / dialog.py
1 """
2 """
3 import os
4
5 from const import *
6 import table, area
7 import basic, input, button
8 import pguglobals
9
10 class Dialog(table.Table):
11 """A dialog window with a title bar and an "close" button on the bar.
12
13 <pre>Dialog(title,main)</pre>
14
15 <dl>
16 <dt>title<dd>title widget, usually a label
17 <dt>main<dd>main widget, usually a container
18 </dl>
19
20 <strong>Example</strong>
21 <code>
22 title = gui.Label("My Title")
23 main = gui.Container()
24 #add stuff to the container...
25
26 d = gui.Dialog(title,main)
27 d.open()
28 </code>
29 """
30 def __init__(self,title,main,**params):
31 params.setdefault('cls','dialog')
32 table.Table.__init__(self,**params)
33
34
35 self.tr()
36 self.td(title,align=-1,cls=self.cls+'.bar')
37 clos = button.Icon(self.cls+".bar.close")
38 clos.connect(CLICK,self.close,None)
39 self.td(clos,align=1,cls=self.cls+'.bar')
40
41 self.tr()
42 self.td(main,colspan=2,cls=self.cls+".main")
43
44
45 # self.tr()
46 #
47 #
48 # t = table.Table(cls=self.cls+".bar")
49 # t.tr()
50 # t.td(title)
51 # clos = button.Icon(self.cls+".bar.close")
52 # t.td(clos,align=1)
53 # clos.connect(CLICK,self.close,None)
54 # self.add(t,0,0)
55 #
56 # main.rect.w,main.rect.h = main.resize()
57 # clos.rect.w,clos.rect.h = clos.resize()
58 # title.container.style.width = main.rect.w - clos.rect.w
59 #
60 # self.tr()
61 # self.td(main,cls=self.cls+".main")
62 #
63
64
65 class FileDialog(Dialog):
66 """A file picker dialog window.
67
68 <pre>FileDialog()</pre>
69 <p>Some optional parameters:</p>
70 <dl>
71 <dt>title_txt<dd>title text
72 <dt>button_txt<dd>button text
73 <dt>path<dd>initial path
74 </dl>
75 """
76
77 def __init__(self, title_txt="File Browser", button_txt="Okay", cls="dialog", folderText = "Folder", fileText = "File", path=None, customFont = None, showCurDir = True, customWidth = 350, customHeight = 150):
78
79 self.customFont = customFont
80 self.showCurDir= showCurDir
81 cls1 = 'filedialog'
82 if not path: self.curdir = os.getcwd()
83 else: self.curdir = path
84 self.dir_img = basic.Image(
85 pguglobals.app.theme.get(cls1+'.folder', '', 'image'))
86 td_style = {'padding_left': 4,
87 'padding_right': 4,
88 'padding_top': 2,
89 'padding_bottom': 2}
90 self.title = basic.Label(title_txt, cls=cls+".title.label")
91 self.body = table.Table()
92 self.list = area.List(width=customWidth, height=customHeight)
93 self.input_dir = input.Input(customFont = self.customFont)
94 self.input_file = input.Input(customFont = self.customFont)
95 self._list_dir_()
96 self.button_ok = button.Button(button_txt)
97 self.body.tr()
98 if self.showCurDir :
99 self.body.td(basic.Label(folderText), style=td_style, align=-1)
100 self.body.td(self.input_dir, style=td_style)
101 self.body.tr()
102 self.body.td(self.list, colspan=3, style=td_style)
103 self.list.connect(CHANGE, self._item_select_changed_, None)
104 self.button_ok.connect(CLICK, self._button_okay_clicked_, None)
105 self.body.tr()
106 self.body.td(basic.Label(fileText), style=td_style, align=-1)
107 self.body.td(self.input_file, style=td_style)
108 self.body.td(self.button_ok, style=td_style)
109 self.value = None
110 Dialog.__init__(self, self.title, self.body)
111
112 def _list_dir_(self):
113 self.input_dir.value = self.curdir
114 self.input_dir.pos = len(self.curdir)
115 self.input_dir.vpos = 0
116 dirs = []
117 files = []
118 try:
119 for i in os.listdir(self.curdir):
120 if os.path.isdir(os.path.join(self.curdir, i)): dirs.append(i)
121 else: files.append(i)
122 except:
123 self.input_file.value = "Opps! no access"
124 #if '..' not in dirs: dirs.append('..')
125 dirs.sort()
126 dirs = ['..'] + dirs
127
128 files.sort()
129 for i in dirs:
130 #item = ListItem(image=self.dir_img, text=i, value=i)
131 if self.customFont == None :
132 self.list.add(i,image=self.dir_img,value=i)
133 else :
134 label = basic.Label(i,font = self.customFont)
135 self.list.add(label,image=self.dir_img,value=i)
136 for i in files:
137 #item = ListItem(image=None, text=i, value=i)
138 if self.customFont == None :
139 self.list.add(i,value=i)
140 else:
141 label = basic.Label(i,font = self.customFont)
142 self.list.add(label,value=i)
143 #self.list.resize()
144 self.list.set_vertical_scroll(0)
145 #self.list.repaintall()
146
147
148 def _item_select_changed_(self, arg):
149 self.input_file.value = self.list.value
150 fname = os.path.abspath(os.path.join(self.curdir, self.input_file.value))
151 if os.path.isdir(fname):
152 self.input_file.value = ""
153 self.curdir = fname
154 self.list.clear()
155 self._list_dir_()
156
157
158 def _button_okay_clicked_(self, arg):
159 if self.input_dir.value != self.curdir:
160 if os.path.isdir(self.input_dir.value):
161 self.input_file.value = ""
162 self.curdir = os.path.abspath(self.input_dir.value)
163 self.list.clear()
164 self._list_dir_()
165 else:
166 self.value = os.path.join(self.curdir, self.input_file.value)
167 self.send(CHANGE)
168 self.close()