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