]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/MimeTypesManager.py
Set the DC font before drawing the label
[wxWidgets.git] / wxPython / demo / MimeTypesManager.py
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 #
14
15
16 import pprint
17 import wx
18 import images
19
20 #----------------------------------------------------------------------------
21
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)
29 self.Enable(True)
30
31 self.SetSizer(sizer)
32 sizer.Fit(self)
33
34 def Enable(self, value):
35 self.ctl.Enable(value)
36
37 def SetValue(self, value):
38 self.ctl.SetValue(value)
39
40 def GetValue(self):
41 return(self.ctl.GetValue())
42
43
44 class MimeTypesDemoPanel(wx.Panel):
45 def __init__(self, parent, log):
46
47 self.log = log
48
49 wx.Panel.__init__(self, parent, -1)
50
51 # Contains everything
52 tsizer = wx.BoxSizer(wx.VERTICAL)
53
54 # Contains upper controls
55 usizer = wx.BoxSizer(wx.HORIZONTAL)
56
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)
61
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
66 )
67 usizer.Add(self.how, 0, wx.ALL | wx.ALIGN_TOP, 4)
68 self.how.SetSelection(0)
69
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)
74
75 # StaticBox with larger label than usual
76 lbox = wx.StaticBox(self, -1, 'wx.FileType')
77 lbox.SetFont(
78 wx.Font(
79 self.GetFont().GetPointSize() * 2,
80 self.GetFont().GetFamily(),
81 self.GetFont().GetStyle(),
82 wx.BOLD
83 ))
84
85 lsizer = wx.StaticBoxSizer(lbox, wx.HORIZONTAL)
86
87 # Contains the wx.FileType info
88 llsizer = wx.GridBagSizer(2, 2)
89 llsizer.AddGrowableCol(2)
90
91 # This will be used for all of the labels that follow (bold label)
92 bfont = wx.Font(
93 self.GetFont().GetPointSize(),
94 self.GetFont().GetFamily(),
95 self.GetFont().GetStyle(),
96 wx.BOLD
97 )
98
99 #------- Icon info
100
101 t = wx.StaticText(self, -1, 'GetIconInfo: ', style = wx.ALIGN_RIGHT )
102 t.SetFont(bfont)
103 llsizer.Add(t, (0, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2)
104
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)
107
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)
110
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)
113
114 #------- MIME Type
115
116 t = wx.StaticText(self, -1, 'GetMimeType: ', style = wx.ALIGN_RIGHT )
117 t.SetFont(bfont)
118 llsizer.Add(t, (1, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2)
119
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)
122
123 #------- MIME Types
124
125 t = wx.StaticText(self, -1, 'GetMimeTypes: ', style = wx.ALIGN_RIGHT )
126 t.SetFont(bfont)
127 llsizer.Add(t, (2, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2)
128
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)
131
132 #------- Extensions
133
134 t = wx.StaticText(self, -1, 'GetExtensions: ', style = wx.ALIGN_RIGHT )
135 t.SetFont(bfont)
136 llsizer.Add(t, (3, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2)
137
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)
140
141 #------- Description
142
143 t = wx.StaticText(self, -1, 'GetDescription: ', style = wx.ALIGN_RIGHT )
144 t.SetFont(bfont)
145 llsizer.Add(t, (4, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2)
146
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)
149
150 #------- Open command
151
152 t = wx.StaticText(self, -1, 'GetOpenCommand: ', style = wx.ALIGN_RIGHT )
153 t.SetFont(bfont)
154 llsizer.Add(t, (5, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2)
155
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)
158
159 #------- Print command
160
161 t = wx.StaticText(self, -1, 'GetPrintCommand: ', style = wx.ALIGN_RIGHT )
162 t.SetFont(bfont)
163 llsizer.Add(t, (6, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2)
164
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)
167
168 #------- All commands
169
170 t = wx.StaticText(self, -1, 'GetAllCommands: ', style = wx.ALIGN_RIGHT )
171 t.SetFont(bfont)
172 llsizer.Add(t, (7, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2)
173
174 self.allcommands = wx.TextCtrl(self, -1, value="", style = wx.TE_READONLY | wx.TE_DONTWRAP | wx.TE_MULTILINE )
175
176 # Set the default height to be smaller than normal (for
177 # multi-line) so the sizer can then expand it to whatever
178 # space is available
179 self.allcommands.SetSize((-1, 20))
180
181 llsizer.Add(self.allcommands, (7, 1), (1, 3), wx.ALL | wx.GROW | wx.ALIGN_CENTER, 2)
182
183 # Tell the sizer to expand this row as needed
184 llsizer.AddGrowableRow(7)
185
186 #----------------------------------------------------------------------------
187
188 lrsizer = wx.StaticBoxSizer(wx.StaticBox(self, -1, 'Known MIME types'), wx.HORIZONTAL)
189
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)
194
195 #----------------------------------------------------------------------------
196
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)
199
200 #----------------------------------------------------------------------------
201
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)
204
205 #----------------------------------------------------------------------------
206
207 self.SetSizer(tsizer)
208 tsizer.Fit(self)
209
210 # Populate the Known MIME types list with what is in the database
211 mtypes = wx.TheMimeTypesManager.EnumAllFileTypes()
212 for mt in mtypes:
213 self.mimelist.Append(mt)
214
215 # Do a lookup of *.wav for a starting position
216 self.OnLookup()
217
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)
224 self.OnLookup()
225
226 # Look up a given file extension or MIME type.
227 def OnLookup(self, event=None):
228 txt = self.ext.GetValue()
229
230 # For MIME lookups
231 if self.how.GetSelection() == 1:
232 fileType = wx.TheMimeTypesManager.GetFileTypeFromMimeType(txt)
233 msg = "Mime type"
234
235 # Select the entered value in the list
236 if fileType:
237 if self.mimelist.FindString(txt) != -1:
238 self.mimelist.SetSelection(self.mimelist.FindString(txt))
239
240 # Must be an extension lookup
241 else:
242 fileType = wx.TheMimeTypesManager.GetFileTypeFromExtension(txt)
243 msg = "File extension"
244
245 # Select the entered value in the list
246 if fileType:
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())))
251
252
253 if fileType is None:
254 wx.MessageBox(msg + " not found.", "Oops!")
255 else:
256 self.Update(fileType)
257
258 # Populate the wx.FileType fields with actual values.
259 def Update(self, ft):
260
261 #------- Icon info
262 info = ft.GetIconInfo()
263
264 if info is None:
265 bmp = images.getNoIconBitmap()
266 self.icon.SetBitmap(bmp)
267 self.iconsource.SetValue("")
268 self.iconoffset.SetValue("")
269 else:
270 icon, file, idx = info
271 if icon.Ok():
272 self.icon.SetIcon(icon)
273 else:
274 bmp = images.getNoIconBitmap()
275 self.icon.SetBitmap(bmp)
276 self.iconsource.SetValue(file)
277 self.iconoffset.SetValue(str(idx))
278
279 #------- MIME type
280 self.mimetype.SetValue(str(ft.GetMimeType()))
281 #------- MIME types
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()))
287
288 #------- Prep a fake command line command
289 extList = ft.GetExtensions()
290
291 if extList:
292 ext = extList[0]
293 if ext[0] == ".": ext = ext[1:]
294 else:
295 ext = ""
296
297 filename = "SPAM" + "." + ext
298 mime = ft.GetMimeType() or ""
299
300 #------- OPEN command
301 cmd = ft.GetOpenCommand(filename, mime)
302 self.opencommand.SetValue(str(cmd))
303
304 #------- PRINT command
305 cmd = ft.GetPrintCommand(filename, mime)
306 self.printcommand.SetValue(str(cmd))
307
308 #------- All commands
309 all = ft.GetAllCommands(filename, mime)
310
311 if all is None:
312 self.allcommands.SetValue("")
313 else:
314 verbs, commands = all
315 text = pprint.pformat(map(None, verbs, commands))
316 self.allcommands.SetValue(text)
317
318
319 #----------------------------------------------------------------------
320
321 def runTest(frame, nb, log):
322 win = MimeTypesDemoPanel(nb, log)
323 return win
324
325 #----------------------------------------------------------------------
326
327 overview = """\
328
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.
335
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
339 on Unix systems.
340
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
343 MIME type.
344
345 For further information please consult the wxWindows documentation for
346 <b>wx.MimeTypesManager</b> and <b>wx.FileType</b>.
347
348 """
349
350 #----------------------------------------------------------------------
351
352 if __name__ == '__main__':
353 import sys,os
354 import run
355 run.main(['', os.path.basename(sys.argv[0])])