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