Use full package paths in imports.
[python/dscho.git] / Mac / scripts / MkDistr_ui.py
blob53869505d2f7495e3183d72d93824018ff7ae39c
2 # MkDistr - User Interface.
4 # Jack Jansen, CWI, August 1995
6 # XXXX To be done (requires mods of FrameWork and toolbox interfaces too):
7 # - Give dialogs titles (need dlg->win conversion)
8 # - Place dialogs better (???)
9 # - <return> as <ok>
10 # - big box around ok button
11 # - window-close crashes on reopen (why?)
12 # - Box around lists (???)
13 # - Change cursor while busy (need cursor support in Qd)
15 from Carbon import Res
16 from Carbon import Dlg
17 from Carbon import Ctl
18 from Carbon import List
19 from Carbon import Win
20 from Carbon import Qd
21 from FrameWork import *
22 import EasyDialogs
23 import macfs
24 import os
25 import sys
26 import macresource
28 # Resource IDs
29 ID_MAIN = 514
30 MAIN_LIST=1
31 MAIN_MKDISTR=2
32 MAIN_CHECK=3
33 MAIN_INCLUDE=4
34 MAIN_EXCLUDE=5
36 ID_INCWINDOW=515
37 ID_EXCWINDOW=517
38 INCEXC_DELETE=2
39 INCEXC_CHANGE=3
40 INCEXC_ADD=4
42 ID_INCLUDE=512
43 ID_EXCLUDE=513
44 DLG_OK=1 # Include for include, exclude for exclude
45 DLG_CANCEL=2
46 DLG_SRCPATH=3
47 DLG_DSTPATH=4 # include dialog only
48 DLG_EXCLUDE=5 # Exclude, include dialog only
50 ID_DTYPE=516
51 DTYPE_EXIST=1
52 DTYPE_NEW=2
53 DTYPE_CANCEL=3
55 class EditDialogWindow(DialogWindow):
56 """Include/exclude editor (modeless dialog window)"""
58 def open(self, id, (src, dst), callback, cancelrv):
59 self.id = id
60 self.callback = callback
61 self.cancelrv = cancelrv
62 DialogWindow.open(self, id)
63 tp, h, rect = self.dlg.GetDialogItem(DLG_SRCPATH)
64 Dlg.SetDialogItemText(h, src)
65 self.dlg.SetDialogDefaultItem(DLG_OK)
66 self.dlg.SetDialogCancelItem(DLG_CANCEL)
67 if id == ID_INCLUDE:
68 tp, h, rect = self.dlg.GetDialogItem(DLG_DSTPATH)
69 if dst == None:
70 dst = ''
71 Dlg.SetDialogItemText(h, dst)
72 self.dlg.DrawDialog()
74 def do_itemhit(self, item, event):
75 if item in (DLG_OK, DLG_CANCEL, DLG_EXCLUDE):
76 self.done(item)
77 # else it is not interesting
79 def done(self, item):
80 tp, h, rect = self.dlg.GetDialogItem(DLG_SRCPATH)
81 src = Dlg.GetDialogItemText(h)
82 if item == DLG_OK:
83 if self.id == ID_INCLUDE:
84 tp, h, rect = self.dlg.GetDialogItem(DLG_DSTPATH)
85 dst = Dlg.GetDialogItemText(h)
86 rv = (src, dst)
87 else:
88 rv = (src, None)
89 elif item == DLG_EXCLUDE:
90 rv = (src, None)
91 else:
92 rv = self.cancelrv
93 self.close()
94 self.callback((item in (DLG_OK, DLG_EXCLUDE)), rv)
96 class ListWindow(DialogWindow):
97 """A dialog window containing a list as its main item"""
99 def open(self, id, contents):
100 self.id = id
101 DialogWindow.open(self, id)
102 Qd.SetPort(self.wid)
103 tp, h, rect = self.dlg.GetDialogItem(MAIN_LIST)
104 self.listrect = rect
105 rect2 = rect[0]+1, rect[1]+1, rect[2]-16, rect[3]-16 # Scroll bar space
106 self.list = List.LNew(rect2, (0, 0, 1, len(contents)), (0,0), 0, self.wid,
107 0, 1, 1, 1)
108 self.setlist(contents)
110 def setlist(self, contents):
111 self.list.LDelRow(0, 0)
112 self.list.LSetDrawingMode(0)
113 if contents:
114 self.list.LAddRow(len(contents), 0)
115 for i in range(len(contents)):
116 self.list.LSetCell(contents[i], (0, i))
117 self.list.LSetDrawingMode(1)
118 ##self.list.LUpdate(self.wid.GetWindowPort().visRgn)
119 self.wid.InvalWindowRect(self.listrect)
121 def additem(self, item):
122 where = self.list.LAddRow(1, 0)
123 self.list.LSetCell(item, (0, where))
125 def delgetitem(self, item):
126 data = self.list.LGetCell(1000, (0, item))
127 self.list.LDelRow(1, item)
128 return data
130 def do_listhit(self, event):
131 (what, message, when, where, modifiers) = event
132 Qd.SetPort(self.wid)
133 where = Qd.GlobalToLocal(where)
134 if self.list.LClick(where, modifiers):
135 self.do_dclick(self.delgetselection())
137 def delgetselection(self):
138 items = []
139 point = (0,0)
140 while 1:
141 ok, point = self.list.LGetSelect(1, point)
142 if not ok:
143 break
144 items.append(point[1])
145 point = point[0], point[1]+1
146 values = []
147 items.reverse()
148 for i in items:
149 values.append(self.delgetitem(i))
150 return values
152 def do_rawupdate(self, window, event):
153 Qd.SetPort(window)
154 Qd.FrameRect(self.listrect)
155 self.list.LUpdate(self.wid.GetWindowPort().visRgn)
157 def do_close(self):
158 self.close()
160 def close(self):
161 del self.list
162 DialogWindow.close(self)
164 def mycb_add(self, ok, item):
165 if item:
166 self.additem(item[0])
167 self.cb_add(item)
169 class MainListWindow(ListWindow):
170 """The main window"""
172 def open(self, id, cb_check, cb_run, cb_add):
173 ListWindow.open(self, id, [])
174 self.dlg.SetDialogDefaultItem(MAIN_INCLUDE)
175 self.cb_run = cb_run
176 self.cb_check = cb_check
177 self.cb_add = cb_add
178 setwatchcursor()
179 list = self.cb_check()
180 self.setlist(list)
181 setarrowcursor()
183 def do_itemhit(self, item, event):
184 if item == MAIN_LIST:
185 self.do_listhit(event)
186 if item == MAIN_MKDISTR:
187 setwatchcursor()
188 self.cb_run()
189 setarrowcursor()
190 if item == MAIN_CHECK:
191 setwatchcursor()
192 list = self.cb_check()
193 self.setlist(list)
194 setarrowcursor()
195 if item == MAIN_INCLUDE:
196 self.do_dclick(self.delgetselection())
197 if item == MAIN_EXCLUDE:
198 for i in self.delgetselection():
199 self.cb_add((i, None))
201 def do_dclick(self, list):
202 if not list:
203 list = ['']
204 for l in list:
205 w = EditDialogWindow(self.parent)
206 w.open(ID_INCLUDE, (l, None), self.mycb_add, None)
208 def mycb_add(self, ok, item):
209 if item:
210 self.cb_add(item)
212 class IncListWindow(ListWindow):
213 """An include/exclude window"""
214 def open(self, id, editid, contents, cb_add, cb_del, cb_get):
215 ListWindow.open(self, id, contents)
216 self.dlg.SetDialogDefaultItem(INCEXC_CHANGE)
217 self.editid = editid
218 self.cb_add = cb_add
219 self.cb_del = cb_del
220 self.cb_get = cb_get
222 def do_itemhit(self, item, event):
223 if item == MAIN_LIST:
224 self.do_listhit(event)
225 if item == INCEXC_DELETE:
226 old = self.delgetselection()
227 for i in old:
228 self.cb_del(i)
229 if item == INCEXC_CHANGE:
230 self.do_dclick(self.delgetselection())
231 if item == INCEXC_ADD:
232 w = EditDialogWindow(self.parent)
233 w.open(self.editid, ('', None), self.mycb_add, None)
235 def do_dclick(self, list):
236 if not list:
237 list = ['']
238 for l in list:
239 old = self.cb_get(l)
240 self.cb_del(l)
241 w = EditDialogWindow(self.parent)
242 w.open(self.editid, old, self.mycb_add, old)
244 class MkDistrUI(Application):
245 def __init__(self, main):
246 self.main = main
247 Application.__init__(self)
248 self.mwin = MainListWindow(self)
249 self.mwin.open(ID_MAIN, self.main.check, self.main.run, self.main.inc.add)
250 self.iwin = None
251 self.ewin = None
253 def makeusermenus(self):
254 self.filemenu = m = Menu(self.menubar, "File")
255 self.includeitem = MenuItem(m, "Show Include window", "", self.showinc)
256 self.excludeitem = MenuItem(m, "Show Exclude window", "", self.showexc)
257 self.saveitem = MenuItem(m, "Save databases", "S", self.save)
258 self.quititem = MenuItem(m, "Quit", "Q", self.quit)
260 def quit(self, *args):
261 if self.main.is_modified():
262 rv = EasyDialogs.AskYesNoCancel('Database modified. Save?', -1)
263 if rv == -1:
264 return
265 if rv == 1:
266 self.main.save()
267 self._quit()
269 def save(self, *args):
270 self.main.save()
272 def showinc(self, *args):
273 if self.iwin:
274 if self._windows.has_key(self.iwin):
275 self.iwin.close()
276 del self.iwin
277 self.iwin = IncListWindow(self)
278 self.iwin.open(ID_INCWINDOW, ID_INCLUDE, self.main.inc.getall(), self.main.inc.add,
279 self.main.inc.delete, self.main.inc.get)
281 def showexc(self, *args):
282 if self.ewin:
283 if self._windows.has_key(self.ewin):
284 self.ewin.close()
285 del self.ewin
286 self.ewin = IncListWindow(self)
287 self.ewin.open(ID_EXCWINDOW, ID_EXCLUDE, self.main.exc.getall(), self.main.exc.add,
288 self.main.exc.delete, self.main.exc.get)
290 def do_about(self, id, item, window, event):
291 EasyDialogs.Message("Test the MkDistr user interface.")
293 def GetType():
294 """Ask user for distribution type"""
295 while 1:
296 d = Dlg.GetNewDialog(ID_DTYPE, -1)
297 d.SetDialogDefaultItem(DTYPE_EXIST)
298 d.SetDialogCancelItem(DTYPE_CANCEL)
299 while 1:
300 rv = ModalDialog(None)
301 if rv in (DTYPE_EXIST, DTYPE_NEW, DTYPE_CANCEL):
302 break
303 del d
304 if rv == DTYPE_CANCEL:
305 sys.exit(0)
306 if rv == DTYPE_EXIST:
307 ## macfs.SetFolder(':(MkDistr)')
308 fss, ok = macfs.StandardGetFile('TEXT')
309 if not ok:
310 sys.exit(0)
311 path = fss.as_pathname()
312 basename = os.path.split(path)[-1]
313 if basename[-8:] <> '.include':
314 EasyDialogs.Message('That is not a distribution include file')
315 else:
316 return basename[:-8]
317 else:
318 name = EasyDialogs.AskString('Distribution name:')
319 if name:
320 return name
321 sys.exit(0)
323 def InitUI():
324 """Initialize stuff needed by UI (a resource file)"""
325 macresource.need('DLOG', ID_MAIN, 'MkDistr.rsrc', modname=__name__)
327 class _testerhelp:
328 def __init__(self, which):
329 self.which = which
331 def get(self):
332 return [self.which+'-one', self.which+'-two']
334 def add(self, value):
335 if value:
336 print 'ADD', self.which, value
338 def delete(self, value):
339 print 'DEL', self.which, value
341 class _test:
342 def __init__(self):
343 import sys
344 InitUI()
345 self.inc = _testerhelp('include')
346 self.exc = _testerhelp('exclude')
347 self.ui = MkDistrUI(self)
348 self.ui.mainloop()
349 sys.exit(1)
351 def check(self):
352 print 'CHECK'
353 return ['rv1', 'rv2']
355 def run(self):
356 print 'RUN'
358 if __name__ == '__main__':
359 _test()