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