]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/lib/filebrowsebutton.py
Unicode compilation fix
[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
111 #----------------------------------------------------------------------
112
113
114 if __name__ == "__main__":
115 #from skeletonbuilder import rulesfile
116 class DemoFrame( wxFrame ):
117 def __init__(self, parent):
118 wxFrame.__init__(self, parent, 2400, "File entry with browse", size=(500,100) )
119 panel = wxPanel (self,-1)
120 innerbox = wxBoxSizer(wxVERTICAL)
121 control = FileBrowseButton(panel)
122 innerbox.Add( control, 0, wxEXPAND )
123 control = FileBrowseButton(panel, labelText = "With Callback", style = wxSUNKEN_BORDER ,changeCallback= self.OnFileNameChanged)
124 innerbox.Add( control, 0, wxEXPAND|wxALIGN_BOTTOM )
125 panel.SetAutoLayout(true)
126 panel.SetSizer( innerbox )
127
128 def OnFileNameChanged (self, event):
129 print "Filename changed", event.GetString ()
130
131 def OnCloseMe(self, event):
132 self.Close(true)
133
134 def OnCloseWindow(self, event):
135 self.Destroy()
136
137 class DemoApp(wxApp):
138 def OnInit(self):
139 wxImage_AddHandler(wxJPEGHandler())
140 wxImage_AddHandler(wxPNGHandler())
141 wxImage_AddHandler(wxGIFHandler())
142 frame = DemoFrame(NULL)
143 #frame = RulesPanel(NULL )
144 frame.Show(true)
145 self.SetTopWindow(frame)
146 return true
147
148 def test( ):
149 app = DemoApp(0)
150 app.MainLoop()
151 print 'Creating dialogue'
152 test( )
153
154
155