]>
Commit | Line | Data |
---|---|---|
1 | #---------------------------------------------------------------------- | |
2 | # Name: wxMimeTypesManager | |
3 | # Purpose: Demonstrate use of wx.MimeTypesManager, wx.FileType | |
4 | # | |
5 | # Author: Jeff Grimmett (grimmtoo@softhome.net), adapted from original | |
6 | # .wdr-derived demo | |
7 | # | |
8 | # Created: 12/31/03 | |
9 | # RCS-ID: $Id$ | |
10 | # Copyright: | |
11 | # Licence: wxWindows license | |
12 | #---------------------------------------------------------------------- | |
13 | # | |
14 | ||
15 | ||
16 | import pprint | |
17 | import wx | |
18 | import images | |
19 | ||
20 | ||
21 | # helper function to make sure we don't convert unicode objects to strings | |
22 | # or vice versa when converting lists and None values to text. | |
23 | convert = str | |
24 | if 'unicode' in wx.PlatformInfo: | |
25 | convert = unicode | |
26 | ||
27 | #---------------------------------------------------------------------------- | |
28 | ||
29 | class MimeTypesDemoPanel(wx.Panel): | |
30 | def __init__(self, parent, log): | |
31 | ||
32 | self.log = log | |
33 | ||
34 | wx.Panel.__init__(self, parent, -1) | |
35 | ||
36 | # This will be used for all of the labels that follow (bold label) | |
37 | bfont = wx.Font( | |
38 | self.GetFont().GetPointSize(), | |
39 | self.GetFont().GetFamily(), | |
40 | self.GetFont().GetStyle(), | |
41 | wx.BOLD | |
42 | ) | |
43 | ||
44 | # Contains everything | |
45 | tsizer = wx.BoxSizer(wx.VERTICAL) | |
46 | ||
47 | # Contains upper controls | |
48 | usizer = wx.BoxSizer(wx.HORIZONTAL) | |
49 | ||
50 | # Text control for ext / type entry plus label. | |
51 | t = wx.StaticText(self, -1, 'Extension / MIME type: ', style = wx.ALIGN_RIGHT ) | |
52 | t.SetFont(bfont) | |
53 | usizer.Add(t, 0, wx.ALL | wx.ALIGN_CENTER | wx.ALIGN_CENTER_VERTICAL, 2) | |
54 | ||
55 | self.ext = wx.TextCtrl(self, -1, value="wav", style = wx.TE_PROCESS_ENTER ) | |
56 | usizer.Add(self.ext, 0, wx.ALL | wx.ALIGN_TOP, 4) | |
57 | self.Bind(wx.EVT_TEXT_ENTER, self.OnLookup, self.ext) | |
58 | ||
59 | # Select how to look it up | |
60 | self.useExt = wx.RadioButton(self, -1, "By extension", style = wx.RB_GROUP) | |
61 | self.useMime = wx.RadioButton(self, -1, "By MIME type") | |
62 | ||
63 | usizer.Add(self.useExt, 0, wx.ALL | wx.ALIGN_CENTER | wx.ALIGN_CENTER_VERTICAL, 4) | |
64 | usizer.Add(self.useMime, 0, wx.ALL | wx.ALIGN_CENTER | wx.ALIGN_CENTER_VERTICAL, 4) | |
65 | self.useExt.SetValue(1) | |
66 | ||
67 | # Trigger a lookup (hitting ENTER in the text ctrl will do the same thing) | |
68 | self.go = wx.Button(self, -1, "Go get it!") | |
69 | usizer.Add(self.go, 0, wx.ALL | wx.ALIGN_CENTER | wx.ALIGN_CENTER_VERTICAL, 4) | |
70 | self.Bind(wx.EVT_BUTTON, self.OnLookup, self.go) | |
71 | ||
72 | # StaticBox with larger label than usual | |
73 | lbox = wx.StaticBox(self, -1, 'wx.FileType') | |
74 | lbox.SetFont( | |
75 | wx.Font( | |
76 | self.GetFont().GetPointSize() * 2, | |
77 | self.GetFont().GetFamily(), | |
78 | self.GetFont().GetStyle(), | |
79 | wx.BOLD | |
80 | )) | |
81 | ||
82 | lsizer = wx.StaticBoxSizer(lbox, wx.HORIZONTAL) | |
83 | ||
84 | # Contains the wx.FileType info | |
85 | llsizer = wx.GridBagSizer(2, 2) | |
86 | llsizer.AddGrowableCol(2) | |
87 | ||
88 | #------- Icon info | |
89 | ||
90 | t = wx.StaticText(self, -1, 'GetIconInfo: ', style = wx.ALIGN_RIGHT ) | |
91 | t.SetFont(bfont) | |
92 | llsizer.Add(t, (0, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2) | |
93 | ||
94 | self.icon = wx.StaticBitmap(self, -1, images.getNoIconBitmap()) | |
95 | llsizer.Add(self.icon, (0, 1), (1, 1), wx.ALL | wx.ALIGN_CENTER, 2) | |
96 | ||
97 | self.iconsource = wx.TextCtrl(self, -1, value="", size=(125, -1), style = wx.TE_READONLY ) | |
98 | llsizer.Add(self.iconsource, (0, 2), (1, 1), wx.ALL | wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL, 2) | |
99 | ||
100 | self.iconoffset = wx.TextCtrl(self, -1, value="", size=(25,-1), style = wx.TE_READONLY ) | |
101 | llsizer.Add(self.iconoffset, (0, 3), (1, 1), wx.ALL | wx.ALIGN_CENTER_VERTICAL, 2) | |
102 | ||
103 | #------- MIME Type | |
104 | ||
105 | t = wx.StaticText(self, -1, 'GetMimeType: ', style = wx.ALIGN_RIGHT ) | |
106 | t.SetFont(bfont) | |
107 | llsizer.Add(t, (1, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2) | |
108 | ||
109 | self.mimetype = wx.TextCtrl(self, -1, value="", style = wx.TE_READONLY ) | |
110 | llsizer.Add(self.mimetype, (1, 1), (1, 3), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2) | |
111 | ||
112 | #------- MIME Types | |
113 | ||
114 | t = wx.StaticText(self, -1, 'GetMimeTypes: ', style = wx.ALIGN_RIGHT ) | |
115 | t.SetFont(bfont) | |
116 | llsizer.Add(t, (2, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2) | |
117 | ||
118 | self.mimetypes = wx.TextCtrl(self, -1, value="", style = wx.TE_READONLY ) | |
119 | llsizer.Add(self.mimetypes, (2, 1), (1, 3), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2) | |
120 | ||
121 | #------- Extensions | |
122 | ||
123 | t = wx.StaticText(self, -1, 'GetExtensions: ', style = wx.ALIGN_RIGHT ) | |
124 | t.SetFont(bfont) | |
125 | llsizer.Add(t, (3, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2) | |
126 | ||
127 | self.extensions = wx.TextCtrl(self, -1, value="", style = wx.TE_READONLY ) | |
128 | llsizer.Add(self.extensions, (3, 1), (1, 3), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2) | |
129 | ||
130 | #------- Description | |
131 | ||
132 | t = wx.StaticText(self, -1, 'GetDescription: ', style = wx.ALIGN_RIGHT ) | |
133 | t.SetFont(bfont) | |
134 | llsizer.Add(t, (4, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2) | |
135 | ||
136 | self.description = wx.TextCtrl(self, -1, value="", style = wx.TE_READONLY) | |
137 | llsizer.Add(self.description, (4, 1), (1, 3), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2) | |
138 | ||
139 | #------- Open command | |
140 | ||
141 | t = wx.StaticText(self, -1, 'GetOpenCommand: ', style = wx.ALIGN_RIGHT ) | |
142 | t.SetFont(bfont) | |
143 | llsizer.Add(t, (5, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2) | |
144 | ||
145 | self.opencommand = wx.TextCtrl(self, -1, value="", style = wx.TE_READONLY ) | |
146 | llsizer.Add(self.opencommand, (5, 1), (1, 3), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2) | |
147 | ||
148 | #------- Print command | |
149 | ||
150 | t = wx.StaticText(self, -1, 'GetPrintCommand: ', style = wx.ALIGN_RIGHT ) | |
151 | t.SetFont(bfont) | |
152 | llsizer.Add(t, (6, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2) | |
153 | ||
154 | self.printcommand = wx.TextCtrl(self, -1, value="", style = wx.TE_READONLY ) | |
155 | llsizer.Add(self.printcommand, (6, 1), (1, 3), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2) | |
156 | ||
157 | #------- All commands | |
158 | ||
159 | t = wx.StaticText(self, -1, 'GetAllCommands: ', style = wx.ALIGN_RIGHT ) | |
160 | t.SetFont(bfont) | |
161 | llsizer.Add(t, (7, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2) | |
162 | ||
163 | self.allcommands = wx.TextCtrl(self, -1, value="", style = wx.TE_READONLY | wx.TE_DONTWRAP | wx.TE_MULTILINE ) | |
164 | ||
165 | # Set the default height to be smaller than normal (for | |
166 | # multi-line) so the sizer can then expand it to whatever | |
167 | # space is available | |
168 | self.allcommands.SetSize((-1, 20)) | |
169 | ||
170 | llsizer.Add(self.allcommands, (7, 1), (1, 3), wx.ALL | wx.GROW | wx.ALIGN_CENTER, 2) | |
171 | ||
172 | # Tell the sizer to expand this row as needed | |
173 | llsizer.AddGrowableRow(7) | |
174 | ||
175 | #---------------------------------------------------------------------------- | |
176 | ||
177 | lrsizer = wx.BoxSizer(wx.VERTICAL) | |
178 | ||
179 | #------- List box with known MIME types | |
180 | ||
181 | t = wx.StaticText(self, -1, 'Known MIME types') | |
182 | t.SetFont(bfont) | |
183 | lrsizer.Add(t, 0, wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 4) | |
184 | ||
185 | self.mimelist = wx.ListBox(self, -1, choices=[], style = wx.LB_SINGLE | wx.LB_SORT) | |
186 | lrsizer.Add(self.mimelist, 1, wx.ALL | wx.EXPAND | wx.ALIGN_CENTER | wx.FIXED_MINSIZE, 4) | |
187 | self.Bind(wx.EVT_LISTBOX, self.OnListbox, self.mimelist) | |
188 | ||
189 | #---------------------------------------------------------------------------- | |
190 | ||
191 | lsizer.Add(llsizer, 1, wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 4) | |
192 | lsizer.Add(lrsizer, 0, wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 4) | |
193 | ||
194 | #---------------------------------------------------------------------------- | |
195 | ||
196 | tsizer.Add(usizer, 0, wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 4) | |
197 | tsizer.Add(lsizer, 1, wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 4) | |
198 | ||
199 | #---------------------------------------------------------------------------- | |
200 | ||
201 | self.SetSizer(tsizer) | |
202 | tsizer.Fit(self) | |
203 | ||
204 | # Populate the Known MIME types list with what is in the database | |
205 | try: | |
206 | mtypes = wx.TheMimeTypesManager.EnumAllFileTypes() | |
207 | except wx.PyAssertionError: | |
208 | mtypes = [] | |
209 | ||
210 | # TODO: On wxMac, EnumAllFileTypes produces tons of dupes, which | |
211 | # causes quirky behavior because the list control doesn't expect | |
212 | # dupes, and simply wastes space. So remove the dupes for now, | |
213 | # then remove this hack when we fix EnumAllFileTypes on Mac. | |
214 | mimes = [] | |
215 | for mt in mtypes: | |
216 | if mt not in mimes: | |
217 | self.mimelist.Append(mt) | |
218 | mimes.append(mt) | |
219 | ||
220 | # Do a lookup of *.wav for a starting position | |
221 | self.OnLookup() | |
222 | ||
223 | # Grab the selection from the listbox, push that into | |
224 | # the text box at top, select 'MIME', and then look it up. | |
225 | def OnListbox(self, event): | |
226 | mimetype = event.GetString() | |
227 | self.ext.SetValue(mimetype) | |
228 | self.useMime.SetValue(1) | |
229 | self.OnLookup() | |
230 | ||
231 | # Look up a given file extension or MIME type. | |
232 | def OnLookup(self, event=None): | |
233 | txt = self.ext.GetValue() | |
234 | ||
235 | # For MIME lookups | |
236 | if self.useMime.GetValue() == 1: | |
237 | fileType = wx.TheMimeTypesManager.GetFileTypeFromMimeType(txt) | |
238 | msg = "Mime type" | |
239 | ||
240 | # Select the entered value in the list | |
241 | if fileType: | |
242 | if self.mimelist.FindString(txt) != -1: | |
243 | self.mimelist.SetSelection(self.mimelist.FindString(txt)) | |
244 | ||
245 | # Must be an extension lookup | |
246 | else: | |
247 | fileType = wx.TheMimeTypesManager.GetFileTypeFromExtension(txt) | |
248 | msg = "File extension" | |
249 | ||
250 | # Select the entered value in the list | |
251 | if fileType: | |
252 | if self.mimelist.FindString(convert(fileType.GetMimeType())) != -1: | |
253 | # Using CallAfter to ensure that GUI is ready before trying to | |
254 | # select it (otherwise, it's selected but not visible) | |
255 | wx.CallAfter(self.mimelist.SetSelection, self.mimelist.FindString(convert(fileType.GetMimeType()))) | |
256 | ||
257 | ||
258 | if fileType is None: | |
259 | wx.MessageBox(msg + " not found.", "Oops!") | |
260 | else: | |
261 | self.Update(fileType) | |
262 | ||
263 | # Populate the wx.FileType fields with actual values. | |
264 | def Update(self, ft): | |
265 | ||
266 | #------- Icon info | |
267 | info = ft.GetIconInfo() | |
268 | ||
269 | if info is None: | |
270 | bmp = images.getNoIconBitmap() | |
271 | self.icon.SetBitmap(bmp) | |
272 | self.iconsource.SetValue("") | |
273 | self.iconoffset.SetValue("") | |
274 | else: | |
275 | icon, file, idx = info | |
276 | if icon.Ok(): | |
277 | self.icon.SetIcon(icon) | |
278 | else: | |
279 | bmp = images.getNoIconBitmap() | |
280 | self.icon.SetBitmap(bmp) | |
281 | self.iconsource.SetValue(file) | |
282 | self.iconoffset.SetValue(convert(idx)) | |
283 | ||
284 | #------- MIME type | |
285 | self.mimetype.SetValue(convert(ft.GetMimeType())) | |
286 | #------- MIME types | |
287 | self.mimetypes.SetValue(convert(ft.GetMimeTypes())) | |
288 | #------- Associated extensions | |
289 | self.extensions.SetValue(convert(ft.GetExtensions())) | |
290 | #------- Description of file type | |
291 | self.description.SetValue(convert(ft.GetDescription())) | |
292 | ||
293 | #------- Prep a fake command line command | |
294 | extList = ft.GetExtensions() | |
295 | ||
296 | if extList: | |
297 | ext = extList[0] | |
298 | if len(ext) > 0 and ext[0] == ".": ext = ext[1:] | |
299 | else: | |
300 | ext = "" | |
301 | ||
302 | filename = "SPAM" + "." + ext | |
303 | mime = ft.GetMimeType() or "" | |
304 | ||
305 | #------- OPEN command | |
306 | cmd = ft.GetOpenCommand(filename, mime) | |
307 | self.opencommand.SetValue(convert(cmd)) | |
308 | ||
309 | #------- PRINT command | |
310 | cmd = ft.GetPrintCommand(filename, mime) | |
311 | self.printcommand.SetValue(convert(cmd)) | |
312 | ||
313 | #------- All commands | |
314 | all = ft.GetAllCommands(filename, mime) | |
315 | ||
316 | if all is None: | |
317 | self.allcommands.SetValue("") | |
318 | else: | |
319 | verbs, commands = all | |
320 | text = pprint.pformat(map(None, verbs, commands)) | |
321 | self.allcommands.SetValue(text) | |
322 | ||
323 | ||
324 | #---------------------------------------------------------------------- | |
325 | ||
326 | def runTest(frame, nb, log): | |
327 | win = MimeTypesDemoPanel(nb, log) | |
328 | return win | |
329 | ||
330 | #---------------------------------------------------------------------- | |
331 | ||
332 | overview = """\ | |
333 | ||
334 | The <b>wx.MimeTypesManager</b> class allows the application to retrieve the | |
335 | information about all known MIME types from a system-specific location and the | |
336 | filename extensions to the MIME types and vice versa. After initialization the | |
337 | methods <b>GetFileTypeFromMimeType()</b> and <b>GetFileTypeFromExtension()</b> | |
338 | may be called: they will return a <b>wx.FileType</b> object which may be further | |
339 | queried for file description, icon and other attributes. | |
340 | ||
341 | A global instance of <b>wx.MimeTypesManager</b> is always available as | |
342 | <b>wx.TheMimeTypesManager</b>. It is recommended to use this instance instead | |
343 | of creating your own because gathering MIME information may take quite a long | |
344 | on Unix systems. | |
345 | ||
346 | This demo shows how to use wx.TheMimeTypesManager to list all known MIME types | |
347 | and retrieve that information as a wx.FileType from either an extension or | |
348 | MIME type. | |
349 | ||
350 | For further information please consult the wxWindows documentation for | |
351 | <b>wx.MimeTypesManager</b> and <b>wx.FileType</b>. | |
352 | ||
353 | """ | |
354 | ||
355 | #---------------------------------------------------------------------- | |
356 | ||
357 | if __name__ == '__main__': | |
358 | import sys,os | |
359 | import run | |
360 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) |