]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxMimeTypesManager.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 # A convenient wrapper around wx.TextCtrl to give it a spiffy label.
23 class ExtStr(wx
.Panel
):
24 def __init__(self
, parent
):
25 wx
.Panel
.__init
__(self
, parent
, -1)
26 sizer
= wx
.StaticBoxSizer(wx
.StaticBox(self
, -1, 'Extension'), wx
.HORIZONTAL
)
27 self
.ctl
= wx
.TextCtrl(self
, -1, value
="wav", style
= wx
.TE_PROCESS_ENTER
)
28 sizer
.Add(self
.ctl
, 0, wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 3)
34 def Enable(self
, value
):
35 self
.ctl
.Enable(value
)
37 def SetValue(self
, value
):
38 self
.ctl
.SetValue(value
)
41 return(self
.ctl
.GetValue())
44 class MimeTypesDemoPanel(wx
.Panel
):
45 def __init__(self
, parent
, log
):
49 wx
.Panel
.__init
__(self
, parent
, -1)
52 tsizer
= wx
.BoxSizer(wx
.VERTICAL
)
54 # Contains upper controls
55 usizer
= wx
.BoxSizer(wx
.HORIZONTAL
)
57 # A little fancy stuff to make things align right.
58 self
.ext
= ExtStr(self
)
59 usizer
.Add(self
.ext
, 0, wx
.ALL | wx
.ALIGN_TOP
, 4)
60 self
.ext
.Bind(wx
.EVT_TEXT_ENTER
, self
.OnLookup
)
62 # Select how to look it up
63 self
.how
= wx
.RadioBox(
64 self
, -1, "Lookup method", choices
=['By extension', 'By MIME type'],
65 majorDimension
=2, style
=wx
.RA_SPECIFY_COLS
67 usizer
.Add(self
.how
, 0, wx
.ALL | wx
.ALIGN_TOP
, 4)
68 self
.how
.SetSelection(0)
70 # Trigger a lookup (hitting ENTER in the text ctrl will do the same thing)
71 self
.go
= wx
.Button(self
, -1, "Go get it!")
72 usizer
.Add(self
.go
, 0, wx
.ALL | wx
.ALIGN_CENTER
, 4)
73 self
.Bind(wx
.EVT_BUTTON
, self
.OnLookup
, self
.go
)
75 # StaticBox with larger label than usual
76 lbox
= wx
.StaticBox(self
, -1, 'wx.FileType')
79 self
.GetFont().GetPointSize() * 2,
80 self
.GetFont().GetFamily(),
81 self
.GetFont().GetStyle(),
85 lsizer
= wx
.StaticBoxSizer(lbox
, wx
.HORIZONTAL
)
87 # Contains the wx.FileType info
88 llsizer
= wx
.GridBagSizer(2, 2)
89 llsizer
.AddGrowableCol(2)
91 # This will be used for all of the labels that follow (bold label)
93 self
.GetFont().GetPointSize(),
94 self
.GetFont().GetFamily(),
95 self
.GetFont().GetStyle(),
101 t
= wx
.StaticText(self
, -1, 'GetIconInfo: ', style
= wx
.ALIGN_RIGHT
)
103 llsizer
.Add(t
, (0, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
105 self
.icon
= wx
.StaticBitmap(self
, -1, images
.getNoIconBitmap())
106 llsizer
.Add(self
.icon
, (0, 1), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
108 self
.iconsource
= wx
.TextCtrl(self
, -1, value
="", style
= wx
.TE_READONLY
)
109 llsizer
.Add(self
.iconsource
, (0, 2), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
111 self
.iconoffset
= wx
.TextCtrl(self
, -1, value
="", style
= wx
.TE_READONLY
)
112 llsizer
.Add(self
.iconoffset
, (0, 3), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
116 t
= wx
.StaticText(self
, -1, 'GetMimeType: ', style
= wx
.ALIGN_RIGHT
)
118 llsizer
.Add(t
, (1, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
120 self
.mimetype
= wx
.TextCtrl(self
, -1, value
="", style
= wx
.TE_READONLY
)
121 llsizer
.Add(self
.mimetype
, (1, 1), (1, 3), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
125 t
= wx
.StaticText(self
, -1, 'GetMimeTypes: ', style
= wx
.ALIGN_RIGHT
)
127 llsizer
.Add(t
, (2, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
129 self
.mimetypes
= wx
.TextCtrl(self
, -1, value
="", style
= wx
.TE_READONLY
)
130 llsizer
.Add(self
.mimetypes
, (2, 1), (1, 3), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
134 t
= wx
.StaticText(self
, -1, 'GetExtensions: ', style
= wx
.ALIGN_RIGHT
)
136 llsizer
.Add(t
, (3, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
138 self
.extensions
= wx
.TextCtrl(self
, -1, value
="", style
= wx
.TE_READONLY
)
139 llsizer
.Add(self
.extensions
, (3, 1), (1, 3), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
143 t
= wx
.StaticText(self
, -1, 'GetDescription: ', style
= wx
.ALIGN_RIGHT
)
145 llsizer
.Add(t
, (4, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
147 self
.description
= wx
.TextCtrl(self
, -1, value
="", style
= wx
.TE_READONLY
)
148 llsizer
.Add(self
.description
, (4, 1), (1, 3), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
150 #------- Open command
152 t
= wx
.StaticText(self
, -1, 'GetOpenCommand: ', style
= wx
.ALIGN_RIGHT
)
154 llsizer
.Add(t
, (5, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
156 self
.opencommand
= wx
.TextCtrl(self
, -1, value
="", style
= wx
.TE_READONLY
)
157 llsizer
.Add(self
.opencommand
, (5, 1), (1, 3), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
159 #------- Print command
161 t
= wx
.StaticText(self
, -1, 'GetPrintCommand: ', style
= wx
.ALIGN_RIGHT
)
163 llsizer
.Add(t
, (6, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
165 self
.printcommand
= wx
.TextCtrl(self
, -1, value
="", style
= wx
.TE_READONLY
)
166 llsizer
.Add(self
.printcommand
, (6, 1), (1, 3), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
168 #------- All commands
170 t
= wx
.StaticText(self
, -1, 'GetAllCommands: ', style
= wx
.ALIGN_RIGHT
)
172 llsizer
.Add(t
, (7, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2)
174 self
.allcommands
= wx
.TextCtrl(self
, -1, value
="", style
= wx
.TE_READONLY | wx
.TE_DONTWRAP | wx
.TE_MULTILINE
)
176 # Set the default height to be smaller than normal (for
177 # multi-line) so the sizer can then expand it to whatever
179 self
.allcommands
.SetSize((-1, 20))
181 llsizer
.Add(self
.allcommands
, (7, 1), (1, 3), wx
.ALL | wx
.GROW | wx
.ALIGN_CENTER
, 2)
183 # Tell the sizer to expand this row as needed
184 llsizer
.AddGrowableRow(7)
186 #----------------------------------------------------------------------------
188 lrsizer
= wx
.StaticBoxSizer(wx
.StaticBox(self
, -1, 'Known MIME types'), wx
.HORIZONTAL
)
190 #------- List box with known MIME types
191 self
.mimelist
= wx
.ListBox(self
, -1, choices
=[], style
= wx
.LB_SINGLE | wx
.LB_SORT
)
192 lrsizer
.Add(self
.mimelist
, 0, wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 4)
193 self
.Bind(wx
.EVT_LISTBOX
, self
.OnListbox
, self
.mimelist
)
195 #----------------------------------------------------------------------------
197 lsizer
.Add(llsizer
, 1, wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 4)
198 lsizer
.Add(lrsizer
, 0, wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 4)
200 #----------------------------------------------------------------------------
202 tsizer
.Add(usizer
, 0, wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 4)
203 tsizer
.Add(lsizer
, 1, wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 4)
205 #----------------------------------------------------------------------------
207 self
.SetSizer(tsizer
)
210 # Populate the Known MIME types list with what is in the database
211 mtypes
= wx
.TheMimeTypesManager
.EnumAllFileTypes()
213 self
.mimelist
.Append(mt
)
215 # Do a lookup of *.wav for a starting position
218 # Grab the selection from the listbox, push that into
219 # the text box at top, select 'MIME', and then look it up.
220 def OnListbox(self
, event
):
221 mimetype
= event
.GetString()
222 self
.ext
.SetValue(mimetype
)
223 self
.how
.SetSelection(1)
226 # Look up a given file extension or MIME type.
227 def OnLookup(self
, event
=None):
228 txt
= self
.ext
.GetValue()
231 if self
.how
.GetSelection() == 1:
232 fileType
= wx
.TheMimeTypesManager
.GetFileTypeFromMimeType(txt
)
235 # Select the entered value in the list
237 if self
.mimelist
.FindString(txt
) != -1:
238 self
.mimelist
.SetSelection(self
.mimelist
.FindString(txt
))
240 # Must be an extension lookup
242 fileType
= wx
.TheMimeTypesManager
.GetFileTypeFromExtension(txt
)
243 msg
= "File extension"
245 # Select the entered value in the list
247 if self
.mimelist
.FindString(str(fileType
.GetMimeType())) != -1:
248 # Using CallAfter to ensure that GUI is ready before trying to
249 # select it (otherwise, it's selected but not visible)
250 wx
.CallAfter(self
.mimelist
.SetSelection
, self
.mimelist
.FindString(str(fileType
.GetMimeType())))
254 wx
.MessageBox(msg
+ " not found.", "Oops!")
256 self
.Update(fileType
)
258 # Populate the wx.FileType fields with actual values.
259 def Update(self
, ft
):
262 info
= ft
.GetIconInfo()
265 bmp
= images
.getNoIconBitmap()
266 self
.icon
.SetBitmap(bmp
)
267 self
.iconsource
.SetValue("")
268 self
.iconoffset
.SetValue("")
270 icon
, file, idx
= info
272 self
.icon
.SetIcon(icon
)
274 bmp
= images
.getNoIconBitmap()
275 self
.icon
.SetBitmap(bmp
)
276 self
.iconsource
.SetValue(file)
277 self
.iconoffset
.SetValue(str(idx
))
280 self
.mimetype
.SetValue(str(ft
.GetMimeType()))
282 self
.mimetypes
.SetValue(str(ft
.GetMimeTypes()))
283 #------- Associated extensions
284 self
.extensions
.SetValue(str(ft
.GetExtensions()))
285 #------- Description of file type
286 self
.description
.SetValue(str(ft
.GetDescription()))
288 #------- Prep a fake command line command
289 extList
= ft
.GetExtensions()
293 if ext
[0] == ".": ext
= ext
[1:]
297 filename
= "SPAM" + "." + ext
298 mime
= ft
.GetMimeType() or ""
300 #------- OPEN command
301 cmd
= ft
.GetOpenCommand(filename
, mime
)
302 self
.opencommand
.SetValue(str(cmd
))
304 #------- PRINT command
305 cmd
= ft
.GetPrintCommand(filename
, mime
)
306 self
.printcommand
.SetValue(str(cmd
))
308 #------- All commands
309 all
= ft
.GetAllCommands(filename
, mime
)
312 self
.allcommands
.SetValue("")
314 verbs
, commands
= all
315 text
= pprint
.pformat(map(None, verbs
, commands
))
316 self
.allcommands
.SetValue(text
)
319 #----------------------------------------------------------------------
321 def runTest(frame
, nb
, log
):
322 win
= MimeTypesDemoPanel(nb
, log
)
325 #----------------------------------------------------------------------
329 The <b>wx.MimeTypesManager</b> class allows the application to retrieve the
330 information about all known MIME types from a system-specific location and the
331 filename extensions to the MIME types and vice versa. After initialization the
332 methods <b>GetFileTypeFromMimeType()</b> and <b>GetFileTypeFromExtension()</b>
333 may be called: they will return a <b>wx.FileType</b> object which may be further
334 queried for file description, icon and other attributes.
336 A global instance of <b>wx.MimeTypesManager</b> is always available as
337 <b>wx.TheMimeTypesManager</b>. It is recommended to use this instance instead
338 of creating your own because gathering MIME information may take quite a long
341 This demo shows how to use wx.TheMimeTypesManager to list all known MIME types
342 and retrieve that information as a wx.FileType from either an extension or
345 For further information please consult the wxWindows documentation for
346 <b>wx.MimeTypesManager</b> and <b>wx.FileType</b>.
350 #----------------------------------------------------------------------
352 if __name__
== '__main__':
355 run
.main(['', os
.path
.basename(sys
.argv
[0])])