Jack Jansen | 1c4d96f | 1999-01-21 14:30:55 +0000 | [diff] [blame^] | 1 | """browsepict - Display all "ICON" resources found""" |
| 2 | |
| 3 | import FrameWork |
| 4 | import EasyDialogs |
| 5 | import Res |
| 6 | import Qd |
| 7 | import Win |
| 8 | import List |
| 9 | import sys |
| 10 | import struct |
| 11 | import Icn |
| 12 | |
| 13 | # |
| 14 | # Resource definitions |
| 15 | ID_MAIN=512 |
| 16 | MAIN_LIST=1 |
| 17 | MAIN_SHOW=2 |
| 18 | |
| 19 | # Where is the picture window? |
| 20 | LEFT=200 |
| 21 | TOP=64 |
| 22 | MINWIDTH=32 |
| 23 | MINHEIGHT=32 |
| 24 | MAXWIDTH=320 |
| 25 | MAXHEIGHT=320 |
| 26 | |
| 27 | def main(): |
| 28 | try: |
| 29 | dummy = Res.GetResource('DLOG', ID_MAIN) |
| 30 | except Res.Error: |
| 31 | try: |
| 32 | Res.OpenResFile("PICTbrowse.rsrc") |
| 33 | except Res.Error, arg: |
| 34 | EasyDialogs.Message("Cannot open PICTbrowse.rsrc: "+arg[1]) |
| 35 | sys.exit(1) |
| 36 | ICONbrowse() |
| 37 | |
| 38 | class ICONbrowse(FrameWork.Application): |
| 39 | def __init__(self): |
| 40 | # First init menus, etc. |
| 41 | FrameWork.Application.__init__(self) |
| 42 | # Next create our dialog |
| 43 | self.main_dialog = MyDialog(self) |
| 44 | # Now open the dialog |
| 45 | contents = self.findICONresources() |
| 46 | self.main_dialog.open(ID_MAIN, contents) |
| 47 | # Finally, go into the event loop |
| 48 | self.mainloop() |
| 49 | |
| 50 | def makeusermenus(self): |
| 51 | self.filemenu = m = FrameWork.Menu(self.menubar, "File") |
| 52 | self.quititem = FrameWork.MenuItem(m, "Quit", "Q", self.quit) |
| 53 | |
| 54 | def quit(self, *args): |
| 55 | self._quit() |
| 56 | |
| 57 | def showICON(self, resid): |
| 58 | w = ICONwindow(self) |
| 59 | w.open(resid) |
| 60 | #EasyDialogs.Message('Show ICON '+`resid`) |
| 61 | |
| 62 | def findICONresources(self): |
| 63 | num = Res.CountResources('ICON') |
| 64 | rv = [] |
| 65 | for i in range(1, num+1): |
| 66 | Res.SetResLoad(0) |
| 67 | try: |
| 68 | r = Res.GetIndResource('ICON', i) |
| 69 | finally: |
| 70 | Res.SetResLoad(1) |
| 71 | id, type, name = r.GetResInfo() |
| 72 | rv.append(id, name) |
| 73 | return rv |
| 74 | |
| 75 | class ICONwindow(FrameWork.Window): |
| 76 | def open(self, (resid, resname)): |
| 77 | if not resname: |
| 78 | resname = '#'+`resid` |
| 79 | self.resid = resid |
| 80 | self.picture = Icn.GetIcon(self.resid) |
| 81 | l, t, r, b = 0, 0, 32, 32 |
| 82 | self.pictrect = (l, t, r, b) |
| 83 | width = r-l |
| 84 | height = b-t |
| 85 | if width < MINWIDTH: width = MINWIDTH |
| 86 | elif width > MAXWIDTH: width = MAXWIDTH |
| 87 | if height < MINHEIGHT: height = MINHEIGHT |
| 88 | elif height > MAXHEIGHT: height = MAXHEIGHT |
| 89 | bounds = (LEFT, TOP, LEFT+width, TOP+height) |
| 90 | |
| 91 | self.wid = Win.NewWindow(bounds, resname, 1, 0, -1, 1, 0) |
| 92 | self.do_postopen() |
| 93 | |
| 94 | def do_update(self, *args): |
| 95 | currect = self.fitrect() |
| 96 | Icn.PlotIcon(currect, self.picture) |
| 97 | |
| 98 | def fitrect(self): |
| 99 | """Return self.pictrect scaled to fit in window""" |
| 100 | graf = self.wid.GetWindowPort() |
| 101 | screenrect = graf.portRect |
| 102 | picwidth = self.pictrect[2] - self.pictrect[0] |
| 103 | picheight = self.pictrect[3] - self.pictrect[1] |
| 104 | if picwidth > screenrect[2] - screenrect[0]: |
| 105 | factor = float(picwidth) / float(screenrect[2]-screenrect[0]) |
| 106 | picwidth = picwidth / factor |
| 107 | picheight = picheight / factor |
| 108 | if picheight > screenrect[3] - screenrect[1]: |
| 109 | factor = float(picheight) / float(screenrect[3]-screenrect[1]) |
| 110 | picwidth = picwidth / factor |
| 111 | picheight = picheight / factor |
| 112 | return (screenrect[0], screenrect[1], screenrect[0]+int(picwidth), |
| 113 | screenrect[1]+int(picheight)) |
| 114 | |
| 115 | class MyDialog(FrameWork.DialogWindow): |
| 116 | "Main dialog window for ICONbrowse" |
| 117 | |
| 118 | def open(self, id, contents): |
| 119 | self.id = id |
| 120 | FrameWork.DialogWindow.open(self, ID_MAIN) |
| 121 | self.wid.SetDialogDefaultItem(MAIN_SHOW) |
| 122 | tp, h, rect = self.wid.GetDialogItem(MAIN_LIST) |
| 123 | rect2 = rect[0]+1, rect[1]+1, rect[2]-17, rect[3]-17 # Scroll bar space |
| 124 | self.list = List.LNew(rect2, (0, 0, 1, len(contents)), (0,0), 0, self.wid, |
| 125 | 0, 1, 1, 1) |
| 126 | self.contents = contents |
| 127 | self.setlist() |
| 128 | |
| 129 | def setlist(self): |
| 130 | self.list.LDelRow(0, 0) |
| 131 | self.list.LSetDrawingMode(0) |
| 132 | if self.contents: |
| 133 | self.list.LAddRow(len(self.contents), 0) |
| 134 | for i in range(len(self.contents)): |
| 135 | v = `self.contents[i][0]` |
| 136 | if self.contents[i][1]: |
| 137 | v = v + '"' + self.contents[i][1] + '"' |
| 138 | self.list.LSetCell(v, (0, i)) |
| 139 | self.list.LSetDrawingMode(1) |
| 140 | self.list.LUpdate(self.wid.GetWindowPort().visRgn) |
| 141 | |
| 142 | def do_listhit(self, event): |
| 143 | (what, message, when, where, modifiers) = event |
| 144 | Qd.SetPort(self.wid) |
| 145 | where = Qd.GlobalToLocal(where) |
| 146 | if self.list.LClick(where, modifiers): |
| 147 | self.do_show() |
| 148 | |
| 149 | def getselection(self): |
| 150 | items = [] |
| 151 | point = (0,0) |
| 152 | while 1: |
| 153 | ok, point = self.list.LGetSelect(1, point) |
| 154 | if not ok: |
| 155 | break |
| 156 | items.append(point[1]) |
| 157 | point = point[0], point[1]+1 |
| 158 | values = [] |
| 159 | for i in items: |
| 160 | values.append(self.contents[i]) |
| 161 | return values |
| 162 | |
| 163 | def do_show(self, *args): |
| 164 | selection = self.getselection() |
| 165 | for resid in selection: |
| 166 | self.parent.showICON(resid) |
| 167 | |
| 168 | def do_rawupdate(self, window, event): |
| 169 | tp, h, rect = self.wid.GetDialogItem(MAIN_LIST) |
| 170 | Qd.SetPort(self.wid) |
| 171 | Qd.FrameRect(rect) |
| 172 | self.list.LUpdate(self.wid.GetWindowPort().visRgn) |
| 173 | |
| 174 | def do_activate(self, activate, event): |
| 175 | self.list.LActivate(activate) |
| 176 | |
| 177 | def do_close(self): |
| 178 | self.close() |
| 179 | |
| 180 | def do_itemhit(self, item, event): |
| 181 | if item == MAIN_LIST: |
| 182 | self.do_listhit(event) |
| 183 | if item == MAIN_SHOW: |
| 184 | self.do_show() |
| 185 | |
| 186 | main() |