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