1 #----------------------------------------------------------------------
2 # Name: wxPython.lib.filebrowsebutton
3 # Purpose: Composite controls that provide a Browse button next to
4 # either a wxTextCtrl or a wxComboBox. The Browse button
5 # launches a wxFileDialog and loads the result into the
8 # Author: Mike Fletcher
11 # Copyright: (c) 2000 by Total Control Software
12 # Licence: wxWindows license
13 #----------------------------------------------------------------------
14 # 12/02/2003 - Jeff Grimmett (grimmtooth@softhome.net)
16 # o 2.5 Compatability changes
24 #----------------------------------------------------------------------
26 class FileBrowseButton(wx
.Panel
):
28 A control to allow the user to type in a filename or browse with
29 the standard file dialog to select file
31 def __init__ (self
, parent
, id= -1,
32 pos
= wx
.DefaultPosition
,
33 size
= wx
.DefaultSize
,
34 style
= wx
.TAB_TRAVERSAL
,
35 labelText
= "File Entry:",
37 toolTip
= "Type filename or click browse to choose file",
38 # following are the values for a file dialog box
39 dialogTitle
= "Choose a file",
44 # callback for when value changes (optional)
45 changeCallback
= lambda x
:x
,
49 :param labelText: Text for label to left of text field
50 :param buttonText: Text for button which launches the file dialog
51 :param toolTip: Help text
52 :param dialogTitle: Title used in file dialog
53 :param startDirectory: Default directory for file dialog startup
54 :param fileMask: File mask (glob pattern, such as *.*) to use in file dialog
55 :param fileMode: wx.OPEN or wx.SAVE, indicates type of file dialog to use
56 :param changeCallback: Optional callback called for all changes in value of the control
57 :param labelWidth: Width of the label
61 self
.labelText
= labelText
62 self
.buttonText
= buttonText
63 self
.toolTip
= toolTip
64 self
.dialogTitle
= dialogTitle
65 self
.startDirectory
= startDirectory
66 self
.initialValue
= initialValue
67 self
.fileMask
= fileMask
68 self
.fileMode
= fileMode
69 self
.changeCallback
= changeCallback
70 self
.callCallback
= True
71 self
.labelWidth
= labelWidth
74 self
.createDialog(parent
, id, pos
, size
, style
)
75 # Setting a value causes the changeCallback to be called.
76 # In this case that would be before the return of the
77 # constructor. Not good. So a default value on
78 # SetValue is used to disable the callback
79 self
.SetValue( initialValue
, 0)
82 def createDialog( self
, parent
, id, pos
, size
, style
):
83 """Setup the graphic representation of the dialog"""
84 wx
.Panel
.__init
__ (self
, parent
, id, pos
, size
, style
)
85 self
.SetMinSize(size
) # play nice with sizers
87 box
= wx
.BoxSizer(wx
.HORIZONTAL
)
89 self
.label
= self
.createLabel( )
90 box
.Add( self
.label
, 0, wx
.CENTER
)
92 self
.textControl
= self
.createTextControl()
93 box
.Add( self
.textControl
, 1, wx
.LEFT|wx
.CENTER
, 5)
95 self
.browseButton
= self
.createBrowseButton()
96 box
.Add( self
.browseButton
, 0, wx
.LEFT|wx
.CENTER
, 5)
98 # add a border around the whole thing and resize the panel to fit
99 outsidebox
= wx
.BoxSizer(wx
.VERTICAL
)
100 outsidebox
.Add(box
, 1, wx
.EXPAND|wx
.ALL
, 3)
103 self
.SetAutoLayout(True)
104 self
.SetSizer( outsidebox
)
106 if type( size
) == types
.TupleType
:
107 size
= apply( wx
.Size
, size
)
108 self
.SetDimensions(-1, -1, size
.width
, size
.height
, wx
.SIZE_USE_EXISTING
)
110 # if size.width != -1 or size.height != -1:
113 def SetBackgroundColour(self
,color
):
114 wx
.Panel
.SetBackgroundColour(self
,color
)
115 self
.label
.SetBackgroundColour(color
)
117 def createLabel( self
):
118 """Create the label/caption"""
119 label
= wx
.StaticText(self
, -1, self
.labelText
, style
=wx
.ALIGN_RIGHT
)
120 font
= label
.GetFont()
121 w
, h
, d
, e
= self
.GetFullTextExtent(self
.labelText
, font
)
122 if self
.labelWidth
> 0:
123 label
.SetSize((self
.labelWidth
+5, h
))
125 label
.SetSize((w
+5, h
))
128 def createTextControl( self
):
129 """Create the text control"""
130 textControl
= wx
.TextCtrl(self
, -1)
131 textControl
.SetToolTipString( self
.toolTip
)
132 if self
.changeCallback
:
133 textControl
.Bind(wx
.EVT_TEXT
, self
.OnChanged
)
134 textControl
.Bind(wx
.EVT_COMBOBOX
, self
.OnChanged
)
137 def OnChanged(self
, evt
):
138 if self
.callCallback
and self
.changeCallback
:
139 self
.changeCallback(evt
)
141 def createBrowseButton( self
):
142 """Create the browse-button control"""
143 button
=wx
.Button(self
, -1, self
.buttonText
)
144 button
.SetToolTipString( self
.toolTip
)
145 button
.Bind(wx
.EVT_BUTTON
, self
.OnBrowse
)
149 def OnBrowse (self
, event
= None):
150 """ Going to browse for file... """
151 current
= self
.GetValue()
152 directory
= os
.path
.split(current
)
153 if os
.path
.isdir( current
):
156 elif directory
and os
.path
.isdir( directory
[0] ):
157 current
= directory
[1]
158 directory
= directory
[0]
160 directory
= self
.startDirectory
161 dlg
= wx
.FileDialog(self
, self
.dialogTitle
, directory
, current
,
162 self
.fileMask
, self
.fileMode
)
164 if dlg
.ShowModal() == wx
.ID_OK
:
165 self
.SetValue(dlg
.GetPath())
171 retrieve current value of text control
173 return self
.textControl
.GetValue()
175 def SetValue (self
, value
, callBack
=1):
176 """set current value of text control"""
177 save
= self
.callCallback
178 self
.callCallback
= callBack
179 self
.textControl
.SetValue(value
)
180 self
.callCallback
= save
183 def Enable (self
, value
):
184 """ Convenient enabling/disabling of entire control """
185 self
.label
.Enable (value
)
186 self
.textControl
.Enable (value
)
187 return self
.browseButton
.Enable (value
)
189 def GetLabel( self
):
190 """ Retrieve the label's current text """
191 return self
.label
.GetLabel()
193 def SetLabel( self
, value
):
194 """ Set the label's current text """
195 rvalue
= self
.label
.SetLabel( value
)
202 class FileBrowseButtonWithHistory( FileBrowseButton
):
204 with following additions:
205 __init__(..., history=None)
207 history -- optional list of paths for initial history drop-down
208 (must be passed by name, not a positional argument)
209 If history is callable it will must return a list used
210 for the history drop-down
212 changeCallback -- as for FileBrowseButton, but with a work-around
213 for win32 systems which don't appear to create wx.EVT_COMBOBOX
214 events properly. There is a (slight) chance that this work-around
215 will cause some systems to create two events for each Combobox
216 selection. If you discover this condition, please report it!
218 As for a FileBrowseButton.__init__ otherwise.
221 Return reference to the control which implements interfaces
222 required for manipulating the history list. See GetHistoryControl
223 documentation for description of what that interface is.
226 Return current history list
228 SetHistory( value=(), selectionIndex = None )
229 Set current history list, if selectionIndex is not None, select that index
232 def __init__( self
, *arguments
, **namedarguments
):
233 self
.history
= namedarguments
.get( "history" )
235 del namedarguments
["history"]
237 self
.historyCallBack
=None
238 if callable(self
.history
):
239 self
.historyCallBack
=self
.history
241 apply( FileBrowseButton
.__init
__, ( self
,)+arguments
, namedarguments
)
244 def createTextControl( self
):
245 """Create the text control"""
246 textControl
= wx
.ComboBox(self
, -1, style
= wx
.CB_DROPDOWN
)
247 textControl
.SetToolTipString( self
.toolTip
)
248 textControl
.Bind(wx
.EVT_SET_FOCUS
, self
.OnSetFocus
)
249 if self
.changeCallback
:
250 textControl
.Bind(wx
.EVT_TEXT
, self
.OnChanged
)
251 textControl
.Bind(wx
.EVT_COMBOBOX
, self
.OnChanged
)
255 self
.SetHistory( history
, control
=textControl
)
259 def GetHistoryControl( self
):
261 Return a pointer to the control which provides (at least)
262 the following methods for manipulating the history list:
264 Append( item ) -- add item
265 Clear() -- clear all items
266 Delete( index ) -- 0-based index to delete from list
267 SetSelection( index ) -- 0-based index to select in list
269 Semantics of the methods follow those for the wxComboBox control
271 return self
.textControl
274 def SetHistory( self
, value
=(), selectionIndex
= None, control
=None ):
275 """Set the current history list"""
277 control
= self
.GetHistoryControl()
278 if self
.history
== value
:
281 # Clear history values not the selected one.
282 tempValue
=control
.GetValue()
283 # clear previous values
285 control
.SetValue(tempValue
)
286 # walk through, appending new values
288 control
.Append( path
)
289 if selectionIndex
is not None:
290 control
.SetSelection( selectionIndex
)
293 def GetHistory( self
):
294 """Return the current history list"""
295 if self
.historyCallBack
!= None:
296 return self
.historyCallBack()
298 return list( self
.history
)
303 def OnSetFocus(self
, event
):
304 """When the history scroll is selected, update the history"""
305 if self
.historyCallBack
!= None:
306 self
.SetHistory( self
.historyCallBack(), control
=self
.textControl
)
310 if wx
.Platform
== "__WXMSW__":
311 def SetValue (self
, value
, callBack
=1):
312 """ Convenient setting of text control value, works
313 around limitation of wx.ComboBox """
314 save
= self
.callCallback
315 self
.callCallback
= callBack
316 self
.textControl
.SetValue(value
)
317 self
.callCallback
= save
319 # Hack to call an event handler
321 def __init__(self
, string
):
326 # The callback wasn't being called when SetValue was used ??
327 # So added this explicit call to it
328 self
.changeCallback(LocalEvent(value
))
331 class DirBrowseButton(FileBrowseButton
):
332 def __init__(self
, parent
, id = -1,
333 pos
= wx
.DefaultPosition
, size
= wx
.DefaultSize
,
334 style
= wx
.TAB_TRAVERSAL
,
335 labelText
= 'Select a directory:',
336 buttonText
= 'Browse',
337 toolTip
= 'Type directory name or browse to select',
339 startDirectory
= '.',
340 changeCallback
= None,
341 dialogClass
= wx
.DirDialog
,
342 newDirectory
= False):
343 FileBrowseButton
.__init
__(self
, parent
, id, pos
, size
, style
,
344 labelText
, buttonText
, toolTip
,
345 dialogTitle
, startDirectory
,
346 changeCallback
= changeCallback
)
347 self
.dialogClass
= dialogClass
348 self
.newDirectory
= newDirectory
351 def OnBrowse(self
, ev
= None):
354 if not self
.newDirectory
:
355 style |
= wx
.DD_DIR_MUST_EXIST
357 dialog
= self
.dialogClass(self
,
358 message
= self
.dialogTitle
,
359 defaultPath
= self
.startDirectory
,
362 if dialog
.ShowModal() == wx
.ID_OK
:
363 self
.SetValue(dialog
.GetPath())
368 #----------------------------------------------------------------------
371 if __name__
== "__main__":
372 #from skeletonbuilder import rulesfile
373 class SimpleCallback
:
374 def __init__( self
, tag
):
376 def __call__( self
, event
):
377 print self
.tag
, event
.GetString()
378 class DemoFrame( wx
.Frame
):
379 def __init__(self
, parent
):
380 wx
.Frame
.__init
__(self
, parent
, -1, "File entry with browse", size
=(500,260))
381 self
.Bind(wx
.EVT_CLOSE
, self
.OnCloseWindow
)
382 panel
= wx
.Panel (self
,-1)
383 innerbox
= wx
.BoxSizer(wx
.VERTICAL
)
384 control
= FileBrowseButton(
386 initialValue
= "z:\\temp",
388 innerbox
.Add( control
, 0, wx
.EXPAND
)
389 middlecontrol
= FileBrowseButtonWithHistory(
391 labelText
= "With History",
392 initialValue
= "d:\\temp",
393 history
= ["c:\\temp", "c:\\tmp", "r:\\temp","z:\\temp"],
394 changeCallback
= SimpleCallback( "With History" ),
396 innerbox
.Add( middlecontrol
, 0, wx
.EXPAND
)
397 middlecontrol
= FileBrowseButtonWithHistory(
399 labelText
= "History callback",
400 initialValue
= "d:\\temp",
401 history
= self
.historyCallBack
,
402 changeCallback
= SimpleCallback( "History callback" ),
404 innerbox
.Add( middlecontrol
, 0, wx
.EXPAND
)
405 self
.bottomcontrol
= control
= FileBrowseButton(
407 labelText
= "With Callback",
408 style
= wx
.SUNKEN_BORDER|wx
.CLIP_CHILDREN
,
409 changeCallback
= SimpleCallback( "With Callback" ),
411 innerbox
.Add( control
, 0, wx
.EXPAND
)
412 self
.bottommostcontrol
= control
= DirBrowseButton(
414 labelText
= "Simple dir browse button",
415 style
= wx
.SUNKEN_BORDER|wx
.CLIP_CHILDREN
)
416 innerbox
.Add( control
, 0, wx
.EXPAND
)
418 innerbox
.Add( wx
.Button( panel
, ID
,"Change Label", ), 1, wx
.EXPAND
)
419 self
.Bind(wx
.EVT_BUTTON
, self
.OnChangeLabel
, id=ID
)
421 innerbox
.Add( wx
.Button( panel
, ID
,"Change Value", ), 1, wx
.EXPAND
)
422 self
.Bind(wx
.EVT_BUTTON
, self
.OnChangeValue
, id=ID
)
423 panel
.SetAutoLayout(True)
424 panel
.SetSizer( innerbox
)
425 self
.history
={"c:\\temp":1, "c:\\tmp":1, "r:\\temp":1,"z:\\temp":1}
427 def historyCallBack(self
):
428 keys
=self
.history
.keys()
432 def OnFileNameChangedHistory (self
, event
):
433 self
.history
[event
.GetString ()]=1
435 def OnCloseMe(self
, event
):
437 def OnChangeLabel( self
, event
):
438 self
.bottomcontrol
.SetLabel( "Label Updated" )
439 def OnChangeValue( self
, event
):
440 self
.bottomcontrol
.SetValue( "r:\\somewhere\\over\\the\\rainbow.htm" )
442 def OnCloseWindow(self
, event
):
445 class DemoApp(wx
.App
):
447 wx
.InitAllImageHandlers()
448 frame
= DemoFrame(None)
450 self
.SetTopWindow(frame
)
456 print 'Creating dialog'