]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/MimeTypesManager.py
1 #----------------------------------------------------------------------
2 # Name: wxMimeTypesManager
3 # Purpose: Demonstrate use of wx.MimeTypesManager, wx.FileType
5 # Author: Jeff Grimmett (grimmtoo@softhome.net), adapted from original
11 # Licence: wxWindows license
12 #----------------------------------------------------------------------
20 #----------------------------------------------------------------------------
22 class MimeTypesDemoPanel(wx
.Panel
):
23 def __init__(self
, parent
, log
):
27 wx
.Panel
.__init
__(self
, parent
, -1)
29 # This will be used for all of the labels that follow (bold label)
31 self
.GetFont().GetPointSize(),
32 self
.GetFont().GetFamily(),
33 self
.GetFont().GetStyle(),
38 tsizer
= wx
.BoxSizer(wx
.VERTICAL
)
40 # Contains upper controls
41 usizer
= wx
.BoxSizer(wx
.HORIZONTAL
)
43 # Text control for ext / type entry plus label.
44 t
= wx
.StaticText(self
, -1, 'Extension / MIME type: ', style
= wx
.ALIGN_RIGHT
)
46 usizer
.Add(t
, 0, wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER | wx
.ALIGN_CENTER_VERTICAL
, 2)
48 self
.ext
= wx
.TextCtrl(self
, -1, value
="wav", style
= wx
.TE_PROCESS_ENTER
)
49 usizer
.Add(self
.ext
, 0, wx
.ALL | wx
.ALIGN_TOP
, 4)
50 self
.Bind(wx
.EVT_TEXT_ENTER
, self
.OnLookup
, self
.ext
)
52 # Select how to look it up
53 self
.useExt
= wx
.RadioButton(self
, -1, "By extension", style
= wx
.RB_GROUP
)
54 self
.useMime
= wx
.RadioButton(self
, -1, "By MIME type")
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)
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!")
62 usizer
.Add(self
.go
, 0, wx
.ALL | wx
.ALIGN_CENTER | wx
.ALIGN_CENTER_VERTICAL
, 4)
63 self
.Bind(wx
.EVT_BUTTON
, self
.OnLookup
, self
.go
)
65 # StaticBox with larger label than usual
66 lbox
= wx
.StaticBox(self
, -1, 'wx.FileType')
69 self
.GetFont().GetPointSize() * 2,
70 self
.GetFont().GetFamily(),
71 self
.GetFont().GetStyle(),
75 lsizer
= wx
.StaticBoxSizer(lbox
, wx
.HORIZONTAL
)
77 # Contains the wx.FileType info
78 llsizer
= wx
.GridBagSizer(2, 2)
79 llsizer
.AddGrowableCol(2)
83 t
= wx
.StaticText(self
, -1, 'GetIconInfo: ', style
= wx
.ALIGN_RIGHT
)
85 llsizer
.Add(t
, (0, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
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)
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)
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)
98 t
= wx
.StaticText(self
, -1, 'GetMimeType: ', style
= wx
.ALIGN_RIGHT
)
100 llsizer
.Add(t
, (1, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
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)
107 t
= wx
.StaticText(self
, -1, 'GetMimeTypes: ', style
= wx
.ALIGN_RIGHT
)
109 llsizer
.Add(t
, (2, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
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)
116 t
= wx
.StaticText(self
, -1, 'GetExtensions: ', style
= wx
.ALIGN_RIGHT
)
118 llsizer
.Add(t
, (3, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
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)
125 t
= wx
.StaticText(self
, -1, 'GetDescription: ', style
= wx
.ALIGN_RIGHT
)
127 llsizer
.Add(t
, (4, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
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)
132 #------- Open command
134 t
= wx
.StaticText(self
, -1, 'GetOpenCommand: ', style
= wx
.ALIGN_RIGHT
)
136 llsizer
.Add(t
, (5, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
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)
141 #------- Print command
143 t
= wx
.StaticText(self
, -1, 'GetPrintCommand: ', style
= wx
.ALIGN_RIGHT
)
145 llsizer
.Add(t
, (6, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
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)
150 #------- All commands
152 t
= wx
.StaticText(self
, -1, 'GetAllCommands: ', style
= wx
.ALIGN_RIGHT
)
154 llsizer
.Add(t
, (7, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
156 self
.allcommands
= wx
.TextCtrl(self
, -1, value
="", style
= wx
.TE_READONLY | wx
.TE_DONTWRAP | wx
.TE_MULTILINE
)
158 # Set the default height to be smaller than normal (for
159 # multi-line) so the sizer can then expand it to whatever
161 self
.allcommands
.SetSize((-1, 20))
163 llsizer
.Add(self
.allcommands
, (7, 1), (1, 3), wx
.ALL | wx
.GROW | wx
.ALIGN_CENTER
, 2)
165 # Tell the sizer to expand this row as needed
166 llsizer
.AddGrowableRow(7)
168 #----------------------------------------------------------------------------
170 lrsizer
= wx
.BoxSizer(wx
.VERTICAL
)
172 #------- List box with known MIME types
174 t
= wx
.StaticText(self
, -1, 'Known MIME types')
176 lrsizer
.Add(t
, 0, wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 4)
178 self
.mimelist
= wx
.ListBox(self
, -1, choices
=[], style
= wx
.LB_SINGLE | wx
.LB_SORT
)
179 lrsizer
.Add(self
.mimelist
, 1, wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 4)
180 self
.Bind(wx
.EVT_LISTBOX
, self
.OnListbox
, self
.mimelist
)
182 #----------------------------------------------------------------------------
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)
187 #----------------------------------------------------------------------------
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)
192 #----------------------------------------------------------------------------
194 self
.SetSizer(tsizer
)
197 # Populate the Known MIME types list with what is in the database
199 mtypes
= wx
.TheMimeTypesManager
.EnumAllFileTypes()
200 except wx
.PyAssertionError
:
203 self
.mimelist
.Append(mt
)
205 # Do a lookup of *.wav for a starting position
208 # Grab the selection from the listbox, push that into
209 # the text box at top, select 'MIME', and then look it up.
210 def OnListbox(self
, event
):
211 mimetype
= event
.GetString()
212 self
.ext
.SetValue(mimetype
)
213 self
.useMime
.SetValue(1)
216 # Look up a given file extension or MIME type.
217 def OnLookup(self
, event
=None):
218 txt
= self
.ext
.GetValue()
221 if self
.useMime
.GetValue() == 1:
222 fileType
= wx
.TheMimeTypesManager
.GetFileTypeFromMimeType(txt
)
225 # Select the entered value in the list
227 if self
.mimelist
.FindString(txt
) != -1:
228 self
.mimelist
.SetSelection(self
.mimelist
.FindString(txt
))
230 # Must be an extension lookup
232 fileType
= wx
.TheMimeTypesManager
.GetFileTypeFromExtension(txt
)
233 msg
= "File extension"
235 # Select the entered value in the list
237 if self
.mimelist
.FindString(str(fileType
.GetMimeType())) != -1:
238 # Using CallAfter to ensure that GUI is ready before trying to
239 # select it (otherwise, it's selected but not visible)
240 wx
.CallAfter(self
.mimelist
.SetSelection
, self
.mimelist
.FindString(str(fileType
.GetMimeType())))
244 wx
.MessageBox(msg
+ " not found.", "Oops!")
246 self
.Update(fileType
)
248 # Populate the wx.FileType fields with actual values.
249 def Update(self
, ft
):
252 info
= ft
.GetIconInfo()
255 bmp
= images
.getNoIconBitmap()
256 self
.icon
.SetBitmap(bmp
)
257 self
.iconsource
.SetValue("")
258 self
.iconoffset
.SetValue("")
260 icon
, file, idx
= info
262 self
.icon
.SetIcon(icon
)
264 bmp
= images
.getNoIconBitmap()
265 self
.icon
.SetBitmap(bmp
)
266 self
.iconsource
.SetValue(file)
267 self
.iconoffset
.SetValue(str(idx
))
270 self
.mimetype
.SetValue(str(ft
.GetMimeType()))
272 self
.mimetypes
.SetValue(str(ft
.GetMimeTypes()))
273 #------- Associated extensions
274 self
.extensions
.SetValue(str(ft
.GetExtensions()))
275 #------- Description of file type
276 self
.description
.SetValue(str(ft
.GetDescription()))
278 #------- Prep a fake command line command
279 extList
= ft
.GetExtensions()
283 if ext
[0] == ".": ext
= ext
[1:]
287 filename
= "SPAM" + "." + ext
288 mime
= ft
.GetMimeType() or ""
290 #------- OPEN command
291 cmd
= ft
.GetOpenCommand(filename
, mime
)
292 self
.opencommand
.SetValue(str(cmd
))
294 #------- PRINT command
295 cmd
= ft
.GetPrintCommand(filename
, mime
)
296 self
.printcommand
.SetValue(str(cmd
))
298 #------- All commands
299 all
= ft
.GetAllCommands(filename
, mime
)
302 self
.allcommands
.SetValue("")
304 verbs
, commands
= all
305 text
= pprint
.pformat(map(None, verbs
, commands
))
306 self
.allcommands
.SetValue(text
)
309 #----------------------------------------------------------------------
311 def runTest(frame
, nb
, log
):
312 win
= MimeTypesDemoPanel(nb
, log
)
315 #----------------------------------------------------------------------
319 The <b>wx.MimeTypesManager</b> class allows the application to retrieve the
320 information about all known MIME types from a system-specific location and the
321 filename extensions to the MIME types and vice versa. After initialization the
322 methods <b>GetFileTypeFromMimeType()</b> and <b>GetFileTypeFromExtension()</b>
323 may be called: they will return a <b>wx.FileType</b> object which may be further
324 queried for file description, icon and other attributes.
326 A global instance of <b>wx.MimeTypesManager</b> is always available as
327 <b>wx.TheMimeTypesManager</b>. It is recommended to use this instance instead
328 of creating your own because gathering MIME information may take quite a long
331 This demo shows how to use wx.TheMimeTypesManager to list all known MIME types
332 and retrieve that information as a wx.FileType from either an extension or
335 For further information please consult the wxWindows documentation for
336 <b>wx.MimeTypesManager</b> and <b>wx.FileType</b>.
340 #----------------------------------------------------------------------
342 if __name__
== '__main__':
345 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])