]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/lib/filebrowsebutton.py
1 from wxPython
.wx
import *
4 #----------------------------------------------------------------------
6 class FileBrowseButton(wxPanel
):
7 ''' A control to allow the user to type in a filename
8 or browse with the standard file dialog to select file
11 parent, id, pos, size -- passed directly to wxPanel initialisation
12 style = wxTAB_TRAVERSAL -- passed directly to wxPanel initialisation
13 labelText -- Text for label to left of text field
14 buttonText -- Text for button which launches the file dialog
16 dialogTitle -- Title used in file dialog
17 startDirectory -- Default directory for file dialog startup
18 fileMask -- File mask (glob pattern, such as *.*) to use in file dialog
19 fileMode -- wxOPEN or wxSAVE, indicates type of file dialog to use
20 changeCallback -- callback receives all changes in value of control
22 GetValue() -- retrieve current value of text control
23 SetValue(string) -- set current value of text control
24 label -- pointer to internal label widget
25 textControl -- pointer to internal text control
26 browseButton -- pointer to button
28 def __init__ (self
, parent
, id= -1,
29 pos
= wxDefaultPosition
, size
= wxDefaultSize
,
30 style
= wxTAB_TRAVERSAL
,
31 labelText
= "File Entry:",
33 toolTip
= "Type filename or browse computer to choose file",
34 # following are the values for a file dialog box
35 dialogTitle
= "Choose a file",
40 # callback for when value changes (optional)
43 wxPanel
.__init
__ (self
, parent
, id, pos
, size
, style
)
45 self
.dialogTitle
= dialogTitle
46 self
.startDirectory
= startDirectory
47 self
.fileMask
= fileMask
48 self
.fileMode
= fileMode
49 self
.changeCallback
= changeCallback
51 box
= wxBoxSizer(wxHORIZONTAL
)
52 self
.label
= wxStaticText(self
, -1, labelText
, style
=wxALIGN_RIGHT
)
53 font
= self
.label
.GetFont()
54 w
, h
, d
, e
= self
.GetFullTextExtent(labelText
, font
)
55 self
.label
.SetSize(wxSize(w
+5, h
))
56 box
.Add( self
.label
, 0, wxCENTER
)
59 self
.textControl
= wxTextCtrl(self
, ID
)
60 self
.textControl
.SetToolTipString( toolTip
)
61 box
.Add( self
.textControl
, 1, wxLEFT|wxCENTER
, 5)
63 EVT_TEXT(self
.textControl
, ID
, changeCallback
)
66 self
.browseButton
= button
=wxButton(self
, ID
, buttonText
)
67 box
.Add( button
, 0, wxCENTER
)
68 button
.SetToolTipString( toolTip
)
69 EVT_BUTTON(button
, ID
, self
.OnBrowse
)
71 # add a border around the whole thing and resize the panel to fit
72 outsidebox
= wxBoxSizer(wxVERTICAL
)
73 outsidebox
.Add(box
, 1, wxEXPAND|wxALL
, 3)
76 self
.SetAutoLayout(true
)
77 self
.SetSizer( outsidebox
)
79 if size
.width
!= -1 or size
.height
!= -1:
84 def OnBrowse (self
, event
= None):
85 ''' Going to browse for file... '''
86 current
= self
.GetValue ()
87 directory
= os
.path
.split(current
)
88 if os
.path
.isdir( current
):
90 elif directory
and os
.path
.isdir( directory
[0] ):
91 directory
= directory
[0]
93 directory
= self
.startDirectory
94 dlg
= wxFileDialog(self
, self
.dialogTitle
, directory
, current
, self
.fileMask
, self
.fileMode
)
95 if dlg
.ShowModal() == wxID_OK
:
96 self
.SetValue (dlg
.GetPath())
98 self
.textControl
.SetFocus()
102 ''' Convenient access to text control value '''
103 return self
.textControl
.GetValue ()
106 def SetValue (self
, value
):
107 ''' Convenient setting of text control value '''
108 return self
.textControl
.SetValue (value
)
110 def Enable (self
, value
):
111 ''' Convenient enabling/disabling of entire control '''
112 self
.label
.Enable (value
)
113 self
.textControl
.Enable (value
)
114 return self
.browseButton
.Enable (value
)
116 def GetLabel( self
):
117 ''' Retrieve the label's current text '''
118 return self
.label
.GetLabel()
120 def SetLabel( self
, value
):
121 ''' Set the label's current text '''
122 return self
.label
.SetLabel( value
)
126 #----------------------------------------------------------------------
129 if __name__
== "__main__":
130 #from skeletonbuilder import rulesfile
131 class DemoFrame( wxFrame
):
132 def __init__(self
, parent
):
133 wxFrame
.__init
__(self
, parent
, 2400, "File entry with browse", size
=(500,100) )
134 panel
= wxPanel (self
,-1)
135 innerbox
= wxBoxSizer(wxVERTICAL
)
136 control
= FileBrowseButton(panel
)
137 innerbox
.Add( control
, 0, wxEXPAND
)
138 control
= FileBrowseButton(panel
, labelText
= "With Callback", style
= wxSUNKEN_BORDER
,changeCallback
= self
.OnFileNameChanged
)
139 innerbox
.Add( control
, 0, wxEXPAND|wxALIGN_BOTTOM
)
140 panel
.SetAutoLayout(true
)
141 panel
.SetSizer( innerbox
)
143 def OnFileNameChanged (self
, event
):
144 print "Filename changed", event
.GetString ()
146 def OnCloseMe(self
, event
):
149 def OnCloseWindow(self
, event
):
152 class DemoApp(wxApp
):
154 wxImage_AddHandler(wxJPEGHandler())
155 wxImage_AddHandler(wxPNGHandler())
156 wxImage_AddHandler(wxGIFHandler())
157 frame
= DemoFrame(NULL
)
158 #frame = RulesPanel(NULL )
160 self
.SetTopWindow(frame
)
166 print 'Creating dialogue'