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