]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/MimeTypesManager.py
reformat the overview string
[wxWidgets.git] / wxPython / demo / MimeTypesManager.py
CommitLineData
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
16import pprint
17import wx
18import images
b37c7e1d 19
486afba9
RD
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.
23convert = str
24if 'unicode' in wx.PlatformInfo:
25 convert = unicode
26
b37c7e1d
RD
27#----------------------------------------------------------------------------
28
cc6eca35
RD
29class MimeTypesDemoPanel(wx.Panel):
30 def __init__(self, parent, log):
31
32 self.log = log
33
34 wx.Panel.__init__(self, parent, -1)
35
4a340a90
RD
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
cc6eca35
RD
44 # Contains everything
45 tsizer = wx.BoxSizer(wx.VERTICAL)
46
47 # Contains upper controls
48 usizer = wx.BoxSizer(wx.HORIZONTAL)
49
4a340a90
RD
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)
102e2b26 53 usizer.Add(t, 0, wx.ALL | wx.ALIGN_CENTER | wx.ALIGN_CENTER_VERTICAL, 2)
4a340a90
RD
54
55 self.ext = wx.TextCtrl(self, -1, value="wav", style = wx.TE_PROCESS_ENTER )
3c29a62a 56 usizer.Add(self.ext, 0, wx.ALL | wx.ALIGN_TOP, 4)
4a340a90 57 self.Bind(wx.EVT_TEXT_ENTER, self.OnLookup, self.ext)
cc6eca35
RD
58
59 # Select how to look it up
4a340a90
RD
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)
cc6eca35
RD
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!")
4a340a90 69 usizer.Add(self.go, 0, wx.ALL | wx.ALIGN_CENTER | wx.ALIGN_CENTER_VERTICAL, 4)
cc6eca35
RD
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
cc6eca35
RD
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())
3c29a62a 95 llsizer.Add(self.icon, (0, 1), (1, 1), wx.ALL | wx.ALIGN_CENTER, 2)
cc6eca35 96
102e2b26
RD
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)
cc6eca35 99
102e2b26 100 self.iconoffset = wx.TextCtrl(self, -1, value="", size=(25,-1), style = wx.TE_READONLY )
3c29a62a 101 llsizer.Add(self.iconoffset, (0, 3), (1, 1), wx.ALL | wx.ALIGN_CENTER_VERTICAL, 2)
cc6eca35
RD
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
b37c7e1d 131
cc6eca35
RD
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)
b37c7e1d 135
cc6eca35
RD
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)
b37c7e1d 138
cc6eca35 139 #------- Open command
b37c7e1d 140
cc6eca35
RD
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
4a340a90 177 lrsizer = wx.BoxSizer(wx.VERTICAL)
cc6eca35
RD
178
179 #------- List box with known MIME types
4a340a90
RD
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
cc6eca35 185 self.mimelist = wx.ListBox(self, -1, choices=[], style = wx.LB_SINGLE | wx.LB_SORT)
3c29a62a 186 lrsizer.Add(self.mimelist, 1, wx.ALL | wx.EXPAND | wx.ALIGN_CENTER | wx.FIXED_MINSIZE, 4)
cc6eca35
RD
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)
b37c7e1d 193
cc6eca35 194 #----------------------------------------------------------------------------
b37c7e1d 195
cc6eca35
RD
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)
b37c7e1d 198
cc6eca35 199 #----------------------------------------------------------------------------
b37c7e1d 200
cc6eca35
RD
201 self.SetSizer(tsizer)
202 tsizer.Fit(self)
203
204 # Populate the Known MIME types list with what is in the database
45cf74cc
RD
205 try:
206 mtypes = wx.TheMimeTypesManager.EnumAllFileTypes()
207 except wx.PyAssertionError:
208 mtypes = []
486afba9
RD
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 = []
cc6eca35 215 for mt in mtypes:
486afba9
RD
216 if mt not in mimes:
217 self.mimelist.Append(mt)
218 mimes.append(mt)
b37c7e1d 219
cc6eca35
RD
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.
b37c7e1d
RD
225 def OnListbox(self, event):
226 mimetype = event.GetString()
cc6eca35 227 self.ext.SetValue(mimetype)
4a340a90 228 self.useMime.SetValue(1)
b37c7e1d
RD
229 self.OnLookup()
230
cc6eca35 231 # Look up a given file extension or MIME type.
b37c7e1d 232 def OnLookup(self, event=None):
cc6eca35
RD
233 txt = self.ext.GetValue()
234
235 # For MIME lookups
4a340a90 236 if self.useMime.GetValue() == 1:
cc6eca35 237 fileType = wx.TheMimeTypesManager.GetFileTypeFromMimeType(txt)
b37c7e1d 238 msg = "Mime type"
cc6eca35
RD
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
b37c7e1d 246 else:
cc6eca35 247 fileType = wx.TheMimeTypesManager.GetFileTypeFromExtension(txt)
b37c7e1d 248 msg = "File extension"
cc6eca35
RD
249
250 # Select the entered value in the list
251 if fileType:
486afba9 252 if self.mimelist.FindString(convert(fileType.GetMimeType())) != -1:
cc6eca35
RD
253 # Using CallAfter to ensure that GUI is ready before trying to
254 # select it (otherwise, it's selected but not visible)
486afba9 255 wx.CallAfter(self.mimelist.SetSelection, self.mimelist.FindString(convert(fileType.GetMimeType())))
cc6eca35
RD
256
257
b37c7e1d 258 if fileType is None:
cc6eca35 259 wx.MessageBox(msg + " not found.", "Oops!")
b37c7e1d
RD
260 else:
261 self.Update(fileType)
262
cc6eca35 263 # Populate the wx.FileType fields with actual values.
b37c7e1d 264 def Update(self, ft):
cc6eca35
RD
265
266 #------- Icon info
b37c7e1d 267 info = ft.GetIconInfo()
cc6eca35 268
b37c7e1d 269 if info is None:
cc6eca35
RD
270 bmp = images.getNoIconBitmap()
271 self.icon.SetBitmap(bmp)
272 self.iconsource.SetValue("")
273 self.iconoffset.SetValue("")
b37c7e1d
RD
274 else:
275 icon, file, idx = info
cc6eca35
RD
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)
486afba9 282 self.iconoffset.SetValue(convert(idx))
cc6eca35
RD
283
284 #------- MIME type
486afba9 285 self.mimetype.SetValue(convert(ft.GetMimeType()))
cc6eca35 286 #------- MIME types
486afba9 287 self.mimetypes.SetValue(convert(ft.GetMimeTypes()))
cc6eca35 288 #------- Associated extensions
486afba9 289 self.extensions.SetValue(convert(ft.GetExtensions()))
cc6eca35 290 #------- Description of file type
486afba9 291 self.description.SetValue(convert(ft.GetDescription()))
cc6eca35
RD
292
293 #------- Prep a fake command line command
0d3859bf 294 extList = ft.GetExtensions()
cc6eca35 295
0d3859bf
RD
296 if extList:
297 ext = extList[0]
486afba9 298 if len(ext) > 0 and ext[0] == ".": ext = ext[1:]
0d3859bf
RD
299 else:
300 ext = ""
cc6eca35 301
4c3b4ed0 302 filename = "SPAM" + "." + ext
b37c7e1d 303 mime = ft.GetMimeType() or ""
cc6eca35
RD
304
305 #------- OPEN command
ee707cb7 306 cmd = ft.GetOpenCommand(filename, mime)
486afba9 307 self.opencommand.SetValue(convert(cmd))
b37c7e1d 308
cc6eca35 309 #------- PRINT command
ee707cb7 310 cmd = ft.GetPrintCommand(filename, mime)
486afba9 311 self.printcommand.SetValue(convert(cmd))
b37c7e1d 312
cc6eca35 313 #------- All commands
ee707cb7 314 all = ft.GetAllCommands(filename, mime)
cc6eca35 315
b37c7e1d 316 if all is None:
cc6eca35 317 self.allcommands.SetValue("")
b37c7e1d
RD
318 else:
319 verbs, commands = all
320 text = pprint.pformat(map(None, verbs, commands))
cc6eca35
RD
321 self.allcommands.SetValue(text)
322
b37c7e1d
RD
323
324#----------------------------------------------------------------------
325
326def runTest(frame, nb, log):
cc6eca35 327 win = MimeTypesDemoPanel(nb, log)
b37c7e1d
RD
328 return win
329
b37c7e1d
RD
330#----------------------------------------------------------------------
331
b37c7e1d 332overview = """\
b37c7e1d 333
cc6eca35
RD
334The <b>wx.MimeTypesManager</b> class allows the application to retrieve the
335information about all known MIME types from a system-specific location and the
336filename extensions to the MIME types and vice versa. After initialization the
337methods <b>GetFileTypeFromMimeType()</b> and <b>GetFileTypeFromExtension()</b>
338may be called: they will return a <b>wx.FileType</b> object which may be further
339queried for file description, icon and other attributes.
b37c7e1d 340
cc6eca35
RD
341A global instance of <b>wx.MimeTypesManager</b> is always available as
342<b>wx.TheMimeTypesManager</b>. It is recommended to use this instance instead
343of creating your own because gathering MIME information may take quite a long
344on Unix systems.
b37c7e1d 345
cc6eca35
RD
346This demo shows how to use wx.TheMimeTypesManager to list all known MIME types
347and retrieve that information as a wx.FileType from either an extension or
348MIME type.
1fded56b 349
cc6eca35
RD
350For further information please consult the wxWindows documentation for
351<b>wx.MimeTypesManager</b> and <b>wx.FileType</b>.
1fded56b 352
cc6eca35 353"""
1fded56b 354
cc6eca35 355#----------------------------------------------------------------------
1fded56b
RD
356
357if __name__ == '__main__':
358 import sys,os
359 import run
8eca4fef 360 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])