blob: 701ef7f57fc0328d171b8fde6b7d0cb7da643212 [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 Jansen1c4d96f1999-01-21 14:30:55 +000010import sys
11import struct
Jack Jansen5a6fdcd2001-08-25 12:15:04 +000012from Carbon import Icn
Jack Jansen3c06b9a2001-08-27 21:41:23 +000013import macresource
Jack Jansen1c4d96f1999-01-21 14:30:55 +000014
15#
16# Resource definitions
17ID_MAIN=512
18MAIN_LIST=1
19MAIN_SHOW=2
20
21# Where is the picture window?
22LEFT=200
23TOP=64
24MINWIDTH=32
25MINHEIGHT=32
26MAXWIDTH=320
27MAXHEIGHT=320
28
29def main():
Tim Peters182b5ac2004-07-18 06:16:08 +000030 macresource.need('DLOG', ID_MAIN, "PICTbrowse.rsrc")
31 ICONbrowse()
Jack Jansen1c4d96f1999-01-21 14:30:55 +000032
33class ICONbrowse(FrameWork.Application):
Tim Peters182b5ac2004-07-18 06:16:08 +000034 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 %r' % (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()
67 rv.append((id, name))
68 return rv
69
Jack Jansen1c4d96f1999-01-21 14:30:55 +000070class ICONwindow(FrameWork.Window):
Tim Peters182b5ac2004-07-18 06:16:08 +000071 def open(self, (resid, resname)):
72 if not resname:
73 resname = '#%r' % (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.GetPortBounds()
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
Jack Jansen1c4d96f1999-01-21 14:30:55 +0000110class MyDialog(FrameWork.DialogWindow):
Tim Peters182b5ac2004-07-18 06:16:08 +0000111 "Main dialog window for ICONbrowse"
Jack Jansen1c4d96f1999-01-21 14:30:55 +0000112
Tim Peters182b5ac2004-07-18 06:16:08 +0000113 def open(self, id, contents):
114 self.id = id
115 FrameWork.DialogWindow.open(self, ID_MAIN)
116 self.dlg.SetDialogDefaultItem(MAIN_SHOW)
117 self.contents = contents
118 self.ctl = self.dlg.GetDialogItemAsControl(MAIN_LIST)
119 h = self.ctl.GetControlData_Handle(Controls.kControlListBoxPart,
120 Controls.kControlListBoxListHandleTag)
121 self.list = List.as_List(h)
122 self.setlist()
Jack Jansen1c4d96f1999-01-21 14:30:55 +0000123
Tim Peters182b5ac2004-07-18 06:16:08 +0000124 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 = repr(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
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
156 def do_close(self):
157 self.close()
158
159 def do_itemhit(self, item, event):
160 if item == MAIN_SHOW:
161 self.do_show()
Jack Jansen1c4d96f1999-01-21 14:30:55 +0000162
163main()