Jack Jansen | c9c99f2 | 1995-08-31 13:50:16 +0000 | [diff] [blame] | 1 | # |
| 2 | # MkDistr - User Interface. |
| 3 | # |
| 4 | # Jack Jansen, CWI, August 1995 |
| 5 | # |
| 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) |
| 14 | # |
| 15 | import Res |
| 16 | import Dlg |
| 17 | import Ctl |
| 18 | import List |
| 19 | import Win |
| 20 | import Qd |
| 21 | from FrameWork import * |
| 22 | import EasyDialogs |
| 23 | import macfs |
| 24 | |
| 25 | # Resource IDs |
| 26 | ID_MAIN = 514 |
| 27 | MAIN_LIST=1 |
| 28 | MAIN_MKDISTR=2 |
| 29 | MAIN_CHECK=3 |
| 30 | MAIN_INCLUDE=4 |
| 31 | MAIN_EXCLUDE=5 |
| 32 | |
| 33 | ID_INCEXC=515 |
| 34 | INCEXC_DELETE=2 |
| 35 | INCEXC_CHANGE=3 |
| 36 | INCEXC_ADD=4 |
| 37 | |
| 38 | ID_INCLUDE=512 |
| 39 | ID_EXCLUDE=513 |
| 40 | DLG_OK=1 |
| 41 | DLG_CANCEL=2 |
| 42 | DLG_FULL=3 |
| 43 | DLG_PPCDEV=4 |
| 44 | DLG_68K=5 |
| 45 | DLG_PPC=6 |
| 46 | DLG_BUTTONS=[DLG_FULL, DLG_PPCDEV, DLG_68K, DLG_PPC] |
| 47 | DLG_LETTERS=['S', 'P', 'm', 'p'] |
| 48 | DLG_SRCPATH=7 |
| 49 | DLG_DSTPATH=8 |
| 50 | |
| 51 | ID_DTYPE=516 |
| 52 | |
| 53 | class EditDialogWindow(DialogWindow): |
| 54 | """Include/exclude editor (modeless dialog window)""" |
| 55 | |
| 56 | def open(self, id, (type, src, dst), callback, cancelrv): |
| 57 | self.id = id |
| 58 | if id == ID_INCLUDE: |
| 59 | title = "Include file dialog" |
| 60 | else: |
| 61 | title = "Exclude pattern dialog" |
| 62 | #self.wid.as_Window().SetWTitle(title) |
| 63 | self.callback = callback |
| 64 | self.cancelrv = cancelrv |
| 65 | DialogWindow.open(self, id) |
| 66 | tp, h, rect = self.wid.GetDialogItem(DLG_SRCPATH) |
| 67 | Dlg.SetDialogItemText(h, src) |
| 68 | if id == ID_INCLUDE: |
| 69 | tp, h, rect = self.wid.GetDialogItem(DLG_DSTPATH) |
| 70 | Dlg.SetDialogItemText(h, dst) |
| 71 | for b in range(len(DLG_BUTTONS)): |
| 72 | if type == None or DLG_LETTERS[b] in type: |
| 73 | self.setbutton(DLG_BUTTONS[b], 1) |
| 74 | |
| 75 | def setbutton(self, num, value): |
| 76 | tp, h, rect = self.wid.GetDialogItem(num) |
| 77 | h.as_Control().SetControlValue(value) |
| 78 | |
| 79 | def getbutton(self, num): |
| 80 | tp, h, rect = self.wid.GetDialogItem(num) |
| 81 | return h.as_Control().GetControlValue() |
| 82 | |
| 83 | def do_itemhit(self, item, event): |
| 84 | if item in (DLG_OK, DLG_CANCEL): |
| 85 | self.done(item) |
| 86 | elif item in DLG_BUTTONS: |
| 87 | v = self.getbutton(item) |
| 88 | self.setbutton(item, (not v)) |
| 89 | # else it is not interesting |
| 90 | |
| 91 | def done(self, item): |
| 92 | if item == DLG_OK: |
| 93 | distlist = '' |
| 94 | for i in range(len(DLG_BUTTONS)): |
| 95 | if self.getbutton(DLG_BUTTONS[i]): |
| 96 | distlist = distlist + DLG_LETTERS[i] |
| 97 | tp, h, rect = self.wid.GetDialogItem(DLG_SRCPATH) |
| 98 | src = Dlg.GetDialogItemText(h) |
| 99 | if self.id == ID_INCLUDE: |
| 100 | tp, h, rect = self.wid.GetDialogItem(DLG_DSTPATH) |
| 101 | dst = Dlg.GetDialogItemText(h) |
| 102 | rv = (distlist, src, dst) |
| 103 | else: |
| 104 | rv = (distlist, src) |
| 105 | else: |
| 106 | rv = self.cancelrv |
| 107 | self.close() |
| 108 | self.callback((item==DLG_OK), rv) |
| 109 | |
| 110 | class ListWindow(DialogWindow): |
| 111 | """A dialog window containing a list as its main item""" |
| 112 | |
| 113 | def open(self, id, contents): |
| 114 | self.id = id |
| 115 | DialogWindow.open(self, id) |
| 116 | tp, h, rect = self.wid.GetDialogItem(MAIN_LIST) |
| 117 | rect2 = rect[0], rect[1], rect[2]-16, rect[3]-16 # Scroll bar space |
| 118 | self.list = List.LNew(rect2, (0, 0, 1, len(contents)), (0,0), 0, self.wid, |
| 119 | 0, 1, 1, 1) |
| 120 | self.setlist(contents) |
| 121 | |
| 122 | def setlist(self, contents): |
| 123 | self.list.LDelRow(0, 0) |
| 124 | self.list.LSetDrawingMode(0) |
| 125 | if contents: |
| 126 | self.list.LAddRow(len(contents), 0) |
| 127 | for i in range(len(contents)): |
| 128 | self.list.LSetCell(contents[i], (0, i)) |
| 129 | self.list.LSetDrawingMode(1) |
| 130 | self.list.LUpdate() |
| 131 | |
| 132 | def additem(self, item): |
| 133 | where = self.list.LAddRow(1, 0) |
| 134 | self.list.LSetCell(item, (0, where)) |
| 135 | |
| 136 | def delgetitem(self, item): |
| 137 | data = self.list.LGetCell(1000, (0, item)) |
| 138 | self.list.LDelRow(1, item) |
| 139 | return data |
| 140 | |
| 141 | def do_listhit(self, event): |
| 142 | (what, message, when, where, modifiers) = event |
| 143 | Qd.SetPort(self.wid) |
| 144 | where = Qd.GlobalToLocal(where) |
| 145 | if self.list.LClick(where, modifiers): |
| 146 | self.do_dclick(self.delgetselection()) |
| 147 | |
| 148 | def delgetselection(self): |
| 149 | items = [] |
| 150 | point = (0,0) |
| 151 | while 1: |
| 152 | ok, point = self.list.LGetSelect(1, point) |
| 153 | if not ok: |
| 154 | break |
| 155 | items.append(point[1]) |
| 156 | point = point[0], point[1]+1 |
| 157 | values = [] |
| 158 | items.reverse() |
| 159 | for i in items: |
| 160 | values.append(self.delgetitem(i)) |
| 161 | return values |
| 162 | |
| 163 | def do_rawupdate(self, window, event): |
| 164 | self.list.LUpdate() |
| 165 | |
| 166 | def do_close(self): |
| 167 | self.close() |
| 168 | |
| 169 | def close(self): |
| 170 | del self.list |
| 171 | DialogWindow.close(self) |
| 172 | |
| 173 | def mycb_add(self, ok, item): |
| 174 | if item: |
| 175 | self.additem(item[1]) |
| 176 | self.cb_add(item) |
| 177 | |
| 178 | class MainListWindow(ListWindow): |
| 179 | """The main window""" |
| 180 | |
| 181 | def open(self, id, cb_check, cb_run, cb_add): |
| 182 | ListWindow.open(self, id, []) |
| 183 | title = "MkDistr: Unresolved files" |
| 184 | #self.wid.as_Window().SetWTitle(title) |
| 185 | self.cb_run = cb_run |
| 186 | self.cb_check = cb_check |
| 187 | self.cb_add = cb_add |
| 188 | |
| 189 | def do_itemhit(self, item, event): |
| 190 | if item == MAIN_LIST: |
| 191 | self.do_listhit(event) |
| 192 | if item == MAIN_MKDISTR: |
| 193 | fss, ok = macfs.StandardPutFile('Destination folder:') |
| 194 | if not ok: |
| 195 | return |
| 196 | self.cb_run(fss.as_pathname()) |
| 197 | if item == MAIN_CHECK: |
| 198 | list = self.cb_check() |
| 199 | self.setlist(list) |
| 200 | if item == MAIN_INCLUDE: |
| 201 | self.do_dclick(self.delgetselection()) |
| 202 | if item == MAIN_EXCLUDE: |
| 203 | for i in self.delgetselection(): |
| 204 | self.cb_add(('', i, '')) |
| 205 | |
| 206 | def do_dclick(self, list): |
| 207 | if not list: |
| 208 | list = [''] |
| 209 | for l in list: |
| 210 | w = EditDialogWindow(self.parent) |
| 211 | w.open(ID_INCLUDE, (None, l, ''), self.mycb_add, None) |
| 212 | |
| 213 | def mycb_add(self, ok, item): |
| 214 | if item: |
| 215 | self.cb_add(item) |
| 216 | |
| 217 | class IncListWindow(ListWindow): |
| 218 | """An include/exclude window""" |
| 219 | def open(self, id, editid, contents, cb_add, cb_del, cb_get): |
| 220 | ListWindow.open(self, id, contents) |
| 221 | if editid == ID_INCLUDE: |
| 222 | title = "MkDistr: files to include" |
| 223 | else: |
| 224 | title = "MkDistr: patterns to exclude" |
| 225 | #self.wid.as_Window().SetWTitle(title) |
| 226 | self.editid = editid |
| 227 | self.cb_add = cb_add |
| 228 | self.cb_del = cb_del |
| 229 | self.cb_get = cb_get |
| 230 | |
| 231 | def do_itemhit(self, item, event): |
| 232 | if item == MAIN_LIST: |
| 233 | self.do_listhit(event) |
| 234 | if item == INCEXC_DELETE: |
| 235 | old = self.delgetselection() |
| 236 | for i in old: |
| 237 | self.cb_del(i) |
| 238 | if item == INCEXC_CHANGE: |
| 239 | self.do_dclick(self.delgetselection()) |
| 240 | if item == INCEXC_ADD: |
| 241 | w = EditDialogWindow(self.parent) |
| 242 | w.open(self.editid, (None, '', ''), self.mycb_add, None) |
| 243 | |
| 244 | def do_dclick(self, list): |
| 245 | if not list: |
| 246 | list = [''] |
| 247 | for l in list: |
| 248 | old = self.cb_get(l) |
| 249 | self.cb_del(l) |
| 250 | w = EditDialogWindow(self.parent) |
| 251 | w.open(self.editid, old, self.mycb_add, old) |
| 252 | |
| 253 | class MkDistrUI(Application): |
| 254 | def __init__(self, main): |
| 255 | self.main = main |
| 256 | Application.__init__(self) |
| 257 | self.mwin = MainListWindow(self) |
| 258 | self.mwin.open(ID_MAIN, self.main.check, self.main.run, self.main.inc.add) |
| 259 | self.iwin = None |
| 260 | self.ewin = None |
| 261 | |
| 262 | def makeusermenus(self): |
| 263 | self.filemenu = m = Menu(self.menubar, "File") |
| 264 | self.includeitem = MenuItem(m, "Show Include window", "", self.showinc) |
| 265 | self.excludeitem = MenuItem(m, "Show Exclude window", "", self.showexc) |
| 266 | self.saveitem = MenuItem(m, "Save databases", "S", self.save) |
| 267 | self.quititem = MenuItem(m, "Quit", "Q", self.quit) |
| 268 | |
| 269 | def quit(self, *args): |
| 270 | if self.main.is_modified(): |
| 271 | rv = EasyDialogs.AskYesNoCancel('Database modified. Save?', -1) |
| 272 | if rv == -1: |
| 273 | return |
| 274 | if rv == 1: |
| 275 | self.main.save() |
| 276 | raise self |
| 277 | |
| 278 | def save(self, *args): |
| 279 | self.main.save() |
| 280 | |
| 281 | def showinc(self, *args): |
| 282 | if self.iwin: |
| 283 | if self._windows.has_key(self.iwin): |
| 284 | self.iwin.close() |
| 285 | del self.iwin |
| 286 | self.iwin = IncListWindow(self) |
| 287 | self.iwin.open(ID_INCEXC, ID_INCLUDE, self.main.inc.getall(), self.main.inc.add, |
| 288 | self.main.inc.delete, self.main.inc.get) |
| 289 | |
| 290 | def showexc(self, *args): |
| 291 | if self.ewin: |
| 292 | if self._windows.has_key(self.ewin): |
| 293 | self.ewin.close() |
| 294 | del self.ewin |
| 295 | self.ewin = IncListWindow(self) |
| 296 | self.ewin.open(ID_INCEXC, ID_EXCLUDE, self.main.exc.getall(), self.main.exc.add, |
| 297 | self.main.exc.delete, self.main.exc.get) |
| 298 | |
| 299 | def do_about(self, id, item, window, event): |
| 300 | EasyDialogs.Message("Test the MkDistr user interface.") |
| 301 | |
| 302 | def GetType(): |
| 303 | """Ask user for distribution type""" |
| 304 | d = Dlg.GetNewDialog(ID_DTYPE, -1) |
| 305 | while 1: |
| 306 | rv = ModalDialog(None) |
| 307 | if rv >= 1 and rv <= 4: |
| 308 | return DLG_LETTERS[rv-1] |
| 309 | |
| 310 | def InitUI(): |
| 311 | """Initialize stuff needed by UI (a resource file)""" |
| 312 | Res.OpenResFile('MkDistr.rsrc') |
| 313 | |
| 314 | class _testerhelp: |
| 315 | def __init__(self, which): |
| 316 | self.which = which |
| 317 | |
| 318 | def get(self): |
| 319 | return [self.which+'-one', self.which+'-two'] |
| 320 | |
| 321 | def add(self, value): |
| 322 | if value: |
| 323 | print 'ADD', self.which, value |
| 324 | |
| 325 | def delete(self, value): |
| 326 | print 'DEL', self.which, value |
| 327 | |
| 328 | class _test: |
| 329 | def __init__(self): |
| 330 | import sys |
| 331 | Res.OpenResFile('MkDistr.rsrc') |
| 332 | self.inc = _testerhelp('include') |
| 333 | self.exc = _testerhelp('exclude') |
| 334 | self.ui = MkDistrUI(self) |
| 335 | self.ui.mainloop() |
| 336 | sys.exit(1) |
| 337 | |
| 338 | def check(self): |
| 339 | print 'CHECK' |
| 340 | return ['rv1', 'rv2'] |
| 341 | |
| 342 | def run(self): |
| 343 | print 'RUN' |
| 344 | |
| 345 | if __name__ == '__main__': |
| 346 | _test() |