]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/lib/filebrowsebutton.py
Module definitions files for build VisualAge C++ V3.0 dlls.
[wxWidgets.git] / utils / wxPython / lib / filebrowsebutton.py
1 from wxPython.wx import *
2 import os
3
4 #----------------------------------------------------------------------
5
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
9
10 __init__ (
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
15 toolTip -- Help text
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
21 )
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
27 '''
28 def __init__ (self, parent, id= -1,
29 pos = wxDefaultPosition, size = wxDefaultSize,
30 style = wxTAB_TRAVERSAL,
31 labelText= "File Entry:",
32 buttonText= "Browse",
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",
36 startDirectory = ".",
37 initialValue = "",
38 fileMask = "*.*",
39 fileMode = wxOPEN,
40 # callback for when value changes (optional)
41 changeCallback= None
42 ):
43 wxPanel.__init__ (self, parent, id, pos, size, style)
44 # store variables
45 self.dialogTitle = dialogTitle
46 self.startDirectory = startDirectory
47 self.fileMask = fileMask
48 self.fileMode = fileMode
49 self.changeCallback = changeCallback
50
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 )
57
58 ID = wxNewId()
59 self.textControl = wxTextCtrl(self, ID)
60 self.textControl.SetToolTipString( toolTip )
61 box.Add( self.textControl, 1, wxLEFT|wxCENTER, 5)
62 if changeCallback:
63 EVT_TEXT(self.textControl, ID, changeCallback)
64
65 ID = wxNewId()
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)
70
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)
74 outsidebox.Fit(self)
75
76 self.SetAutoLayout(true)
77 self.SetSizer( outsidebox )
78 self.Layout()
79 if size.width != -1 or size.height != -1:
80 self.SetSize(size)
81
82
83
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):
89 directory =current
90 elif directory and os.path.isdir( directory[0] ):
91 directory = directory [0]
92 else:
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())
97 dlg.Destroy()
98 self.textControl.SetFocus()
99
100
101 def GetValue (self):
102 ''' Convenient access to text control value '''
103 return self.textControl.GetValue ()
104
105
106 def SetValue (self, value):
107 ''' Convenient setting of text control value '''
108 return self.textControl.SetValue (value)
109
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)
115
116 def GetLabel( self ):
117 ''' Retrieve the label's current text '''
118 return self.label.GetLabel()
119
120 def SetLabel( self, value ):
121 ''' Set the label's current text '''
122 return self.label.SetLabel( value )
123
124
125
126 #----------------------------------------------------------------------
127
128
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 )
142
143 def OnFileNameChanged (self, event):
144 print "Filename changed", event.GetString ()
145
146 def OnCloseMe(self, event):
147 self.Close(true)
148
149 def OnCloseWindow(self, event):
150 self.Destroy()
151
152 class DemoApp(wxApp):
153 def OnInit(self):
154 wxImage_AddHandler(wxJPEGHandler())
155 wxImage_AddHandler(wxPNGHandler())
156 wxImage_AddHandler(wxGIFHandler())
157 frame = DemoFrame(NULL)
158 #frame = RulesPanel(NULL )
159 frame.Show(true)
160 self.SetTopWindow(frame)
161 return true
162
163 def test( ):
164 app = DemoApp(0)
165 app.MainLoop()
166 print 'Creating dialogue'
167 test( )
168
169
170