blob: f55070bf53eb2223f86395f9f4cf2d3b9772d3ac [file] [log] [blame]
Jack Jansen1c4d96f1999-01-21 14:30:55 +00001"""browsepict - Display all "ICON" resources found"""
2
3import FrameWork
4import EasyDialogs
Jack Jansen5a6fdcd2001-08-25 12:15:04 +00005from Carbon import Res
6from Carbon import Qd
7from Carbon import Win
8from Carbon import Controls
9from Carbon import List
Jack Jansen5a6fdcd2001-08-25 12:15:04 +000010from Carbon import Icn
Jack Jansen3c06b9a2001-08-27 21:41:23 +000011import macresource
Jack Jansen1c4d96f1999-01-21 14:30:55 +000012
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():
Tim Peters182b5ac2004-07-18 06:16:08 +000028 macresource.need('DLOG', ID_MAIN, "PICTbrowse.rsrc")
29 ICONbrowse()
Jack Jansen1c4d96f1999-01-21 14:30:55 +000030
31class ICONbrowse(FrameWork.Application):
Tim Peters182b5ac2004-07-18 06:16:08 +000032 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
Jack Jansen1c4d96f1999-01-21 14:30:55 +000068class ICONwindow(FrameWork.Window):
Tim Peters182b5ac2004-07-18 06:16:08 +000069 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
Jack Jansen1c4d96f1999-01-21 14:30:55 +0000108class MyDialog(FrameWork.DialogWindow):
Tim Peters182b5ac2004-07-18 06:16:08 +0000109 "Main dialog window for ICONbrowse"
Jack Jansen1c4d96f1999-01-21 14:30:55 +0000110
Tim Peters182b5ac2004-07-18 06:16:08 +0000111 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()
Jack Jansen1c4d96f1999-01-21 14:30:55 +0000121
Tim Peters182b5ac2004-07-18 06:16:08 +0000122 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()
Jack Jansen1c4d96f1999-01-21 14:30:55 +0000160
161main()