]>
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 #---------------------------------------------------------------------- 
  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. 
  24 if 'unicode' in wx
.PlatformInfo
: 
  27 #---------------------------------------------------------------------------- 
  29 class MimeTypesDemoPanel(wx
.Panel
): 
  30     def __init__(self
, parent
, log
): 
  34         wx
.Panel
.__init
__(self
, parent
, -1) 
  36         # This will be used for all of the labels that follow (bold label) 
  38                     self
.GetFont().GetPointSize(),  
  39                     self
.GetFont().GetFamily(), 
  40                     self
.GetFont().GetStyle(), 
  45         tsizer 
= wx
.BoxSizer(wx
.VERTICAL
) 
  47         # Contains upper controls 
  48         usizer 
= wx
.BoxSizer(wx
.HORIZONTAL
) 
  50         # Text control for ext / type entry plus label. 
  51         t 
= wx
.StaticText(self
, -1, 'Extension / MIME type: ', style 
= wx
.ALIGN_RIGHT 
) 
  53         usizer
.Add(t
, 0, wx
.ALL | wx
.ALIGN_CENTER | wx
.ALIGN_CENTER_VERTICAL
, 2) 
  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
) 
  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") 
  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) 
  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
) 
  72         # StaticBox with larger label than usual 
  73         lbox 
= wx
.StaticBox(self
, -1, 'wx.FileType') 
  76                 self
.GetFont().GetPointSize() * 2,  
  77                 self
.GetFont().GetFamily(), 
  78                 self
.GetFont().GetStyle(), 
  82         lsizer 
= wx
.StaticBoxSizer(lbox
, wx
.HORIZONTAL
) 
  84         # Contains the wx.FileType info 
  85         llsizer 
= wx
.GridBagSizer(2, 2) 
  86         llsizer
.AddGrowableCol(2) 
  90         t 
= wx
.StaticText(self
, -1, 'GetIconInfo: ', style 
= wx
.ALIGN_RIGHT 
) 
  92         llsizer
.Add(t
, (0, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2) 
  94         self
.icon 
= wx
.StaticBitmap(self
, -1, images
.getNoIconBitmap()) 
  95         llsizer
.Add(self
.icon
, (0, 1), (1, 1), wx
.ALL | wx
.ALIGN_CENTER
, 2) 
  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) 
 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) 
 105         t 
= wx
.StaticText(self
, -1, 'GetMimeType: ', style 
= wx
.ALIGN_RIGHT 
) 
 107         llsizer
.Add(t
, (1, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2) 
 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) 
 114         t 
= wx
.StaticText(self
, -1, 'GetMimeTypes: ', style 
= wx
.ALIGN_RIGHT 
) 
 116         llsizer
.Add(t
, (2, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2) 
 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) 
 123         t 
= wx
.StaticText(self
, -1, 'GetExtensions: ', style 
= wx
.ALIGN_RIGHT 
) 
 125         llsizer
.Add(t
, (3, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2) 
 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) 
 132         t 
= wx
.StaticText(self
, -1, 'GetDescription: ', style 
= wx
.ALIGN_RIGHT 
) 
 134         llsizer
.Add(t
, (4, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2) 
 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) 
 139         #------- Open command 
 141         t 
= wx
.StaticText(self
, -1, 'GetOpenCommand: ', style 
= wx
.ALIGN_RIGHT 
) 
 143         llsizer
.Add(t
, (5, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2) 
 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) 
 148         #------- Print command 
 150         t 
= wx
.StaticText(self
, -1, 'GetPrintCommand: ', style 
= wx
.ALIGN_RIGHT 
) 
 152         llsizer
.Add(t
, (6, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2) 
 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) 
 157         #------- All commands 
 159         t 
= wx
.StaticText(self
, -1, 'GetAllCommands: ', style 
= wx
.ALIGN_RIGHT 
) 
 161         llsizer
.Add(t
, (7, 0), (1, 1), wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 2) 
 163         self
.allcommands 
= wx
.TextCtrl(self
, -1, value
="", style 
= wx
.TE_READONLY | wx
.TE_DONTWRAP | wx
.TE_MULTILINE 
) 
 165         # Set the default height to be smaller than normal (for 
 166         # multi-line) so the sizer can then expand it to whatever 
 168         self
.allcommands
.SetSize((-1, 20)) 
 170         llsizer
.Add(self
.allcommands
, (7, 1), (1, 3), wx
.ALL | wx
.GROW | wx
.ALIGN_CENTER
, 2) 
 172         # Tell the sizer to expand this row as needed 
 173         llsizer
.AddGrowableRow(7) 
 175         #---------------------------------------------------------------------------- 
 177         lrsizer 
= wx
.BoxSizer(wx
.VERTICAL
) 
 179         #------- List box with known MIME types 
 181         t 
= wx
.StaticText(self
, -1, 'Known MIME types') 
 183         lrsizer
.Add(t
, 0, wx
.ALL | wx
.EXPAND | wx
.ALIGN_CENTER
, 4) 
 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
) 
 189         #---------------------------------------------------------------------------- 
 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) 
 194         #---------------------------------------------------------------------------- 
 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) 
 199         #---------------------------------------------------------------------------- 
 201         self
.SetSizer(tsizer
) 
 204         # Populate the Known MIME types list with what is in the database 
 206             mtypes 
= wx
.TheMimeTypesManager
.EnumAllFileTypes() 
 207         except wx
.PyAssertionError
: 
 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. 
 217                 self
.mimelist
.Append(mt
) 
 220         # Do a lookup of *.wav for a starting position 
 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) 
 231     # Look up a given file extension or MIME type. 
 232     def OnLookup(self
, event
=None): 
 233         txt 
= self
.ext
.GetValue() 
 236         if self
.useMime
.GetValue() == 1: 
 237             fileType 
= wx
.TheMimeTypesManager
.GetFileTypeFromMimeType(txt
) 
 240             # Select the entered value in the list 
 242                 if self
.mimelist
.FindString(txt
) != -1: 
 243                     self
.mimelist
.SetSelection(self
.mimelist
.FindString(txt
)) 
 245         # Must be an extension lookup 
 247             fileType 
= wx
.TheMimeTypesManager
.GetFileTypeFromExtension(txt
) 
 248             msg 
= "File extension" 
 250             # Select the entered value in the list 
 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()))) 
 259             wx
.MessageBox(msg 
+ " not found.", "Oops!") 
 261             self
.Update(fileType
) 
 263     # Populate the wx.FileType fields with actual values. 
 264     def Update(self
, ft
): 
 267         info 
= ft
.GetIconInfo() 
 270             bmp 
= images
.getNoIconBitmap() 
 271             self
.icon
.SetBitmap(bmp
) 
 272             self
.iconsource
.SetValue("") 
 273             self
.iconoffset
.SetValue("") 
 275             icon
, file, idx 
= info
 
 277                 self
.icon
.SetIcon(icon
) 
 279                 bmp 
= images
.getNoIconBitmap() 
 280                 self
.icon
.SetBitmap(bmp
)                 
 281             self
.iconsource
.SetValue(file) 
 282             self
.iconoffset
.SetValue(convert(idx
)) 
 285         self
.mimetype
.SetValue(convert(ft
.GetMimeType())) 
 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())) 
 293         #------- Prep a fake command line command 
 294         extList 
= ft
.GetExtensions() 
 298             if len(ext
) > 0 and ext
[0] == ".": ext 
= ext
[1:] 
 302         filename 
= "SPAM" + "." + ext
 
 303         mime 
= ft
.GetMimeType() or "" 
 305         #------- OPEN command 
 306         cmd 
= ft
.GetOpenCommand(filename
, mime
) 
 307         self
.opencommand
.SetValue(convert(cmd
)) 
 309         #------- PRINT command 
 310         cmd 
= ft
.GetPrintCommand(filename
, mime
) 
 311         self
.printcommand
.SetValue(convert(cmd
)) 
 313         #------- All commands 
 314         all 
= ft
.GetAllCommands(filename
, mime
) 
 317             self
.allcommands
.SetValue("") 
 319             verbs
, commands 
= all
 
 320             text 
= pprint
.pformat(map(None, verbs
, commands
)) 
 321             self
.allcommands
.SetValue(text
) 
 324 #---------------------------------------------------------------------- 
 326 def runTest(frame
, nb
, log
): 
 327     win 
= MimeTypesDemoPanel(nb
, log
) 
 330 #---------------------------------------------------------------------- 
 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. 
 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  
 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 
 350 For further information please consult the wxWindows documentation for 
 351 <b>wx.MimeTypesManager</b> and <b>wx.FileType</b>. 
 355 #---------------------------------------------------------------------- 
 357 if __name__ 
== '__main__': 
 360     run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])