move sections
[python/dscho.git] / Mac / Demo / PICTbrowse / cicnbrowse.py
blob4bc7008ed46f1047fceeb76af8535718b4f74521
1 """browsepict - Display all "cicn" resources found"""
3 import FrameWork
4 import EasyDialogs
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
10 from Carbon import Icn
11 import macresource
14 # Resource definitions
15 ID_MAIN=512
16 MAIN_LIST=1
17 MAIN_SHOW=2
19 # Where is the picture window?
20 LEFT=200
21 TOP=64
22 MINWIDTH=32
23 MINHEIGHT=32
24 MAXWIDTH=320
25 MAXHEIGHT=320
27 def main():
28 macresource.need('DLOG', ID_MAIN, "PICTbrowse.rsrc")
29 CIconbrowse()
31 class CIconbrowse(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.findcicnresources()
39 self.main_dialog.open(ID_MAIN, contents)
40 # Finally, go into the event loop
41 self.mainloop()
43 def makeusermenus(self):
44 self.filemenu = m = FrameWork.Menu(self.menubar, "File")
45 self.quititem = FrameWork.MenuItem(m, "Quit", "Q", self.quit)
47 def quit(self, *args):
48 self._quit()
50 def showCIcon(self, resid):
51 w = CIconwindow(self)
52 w.open(resid)
53 #EasyDialogs.Message('Show cicn %r' % (resid,))
55 def findcicnresources(self):
56 num = Res.CountResources('cicn')
57 rv = []
58 for i in range(1, num+1):
59 Res.SetResLoad(0)
60 try:
61 r = Res.GetIndResource('cicn', i)
62 finally:
63 Res.SetResLoad(1)
64 id, type, name = r.GetResInfo()
65 rv.append((id, name))
66 return rv
68 class CIconwindow(FrameWork.Window):
69 def open(self, (resid, resname)):
70 if not resname:
71 resname = '#%r' % (resid,)
72 self.resid = resid
73 self.picture = Icn.GetCIcon(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)
84 self.wid = Win.NewWindow(bounds, resname, 1, 0, -1, 1, 0)
85 self.do_postopen()
87 def do_update(self, *args):
88 currect = self.fitrect()
89 Icn.PlotCIcon(currect, self.picture)
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))
108 class MyDialog(FrameWork.DialogWindow):
109 "Main dialog window for cicnbrowse"
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()
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)
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
149 def do_show(self, *args):
150 selection = self.getselection()
151 for resid in selection:
152 self.parent.showCIcon(resid)
154 def do_close(self):
155 self.close()
157 def do_itemhit(self, item, event):
158 if item == MAIN_SHOW:
159 self.do_show()
161 main()