]> git.saurik.com Git - wxWidgets.git/blob - src/generic/filepickerg.cpp
Make wxOwnerDrawnComboBox::DoGetBestSize() twice as fast.
[wxWidgets.git] / src / generic / filepickerg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/filepickerg.cpp
3 // Purpose: wxGenericFileDirButton class implementation
4 // Author: Francesco Montorsi
5 // Modified by:
6 // Created: 15/04/2006
7 // Copyright: (c) Francesco Montorsi
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
27
28 #include "wx/filename.h"
29 #include "wx/filepicker.h"
30
31 #include "wx/scopedptr.h"
32
33
34 // ============================================================================
35 // implementation
36 // ============================================================================
37
38 IMPLEMENT_DYNAMIC_CLASS(wxGenericFileButton, wxButton)
39 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirButton, wxButton)
40
41 // ----------------------------------------------------------------------------
42 // wxGenericFileButton
43 // ----------------------------------------------------------------------------
44
45 bool wxGenericFileDirButton::Create(wxWindow *parent,
46 wxWindowID id,
47 const wxString& label,
48 const wxString& path,
49 const wxString& message,
50 const wxString& wildcard,
51 const wxPoint& pos,
52 const wxSize& size,
53 long style,
54 const wxValidator& validator,
55 const wxString& name)
56 {
57 m_pickerStyle = style;
58
59 // If the special wxPB_SMALL flag is used, ignore the provided label and
60 // use the shortest possible label and the smallest possible button fitting
61 // it.
62 long styleButton = 0;
63 wxString labelButton;
64 if ( m_pickerStyle & wxPB_SMALL )
65 {
66 labelButton = _("...");
67 styleButton = wxBU_EXACTFIT;
68 }
69 else
70 {
71 labelButton = label;
72 }
73
74 // create this button
75 if ( !wxButton::Create(parent, id, labelButton,
76 pos, size, styleButton, validator, name) )
77 {
78 wxFAIL_MSG( wxT("wxGenericFileButton creation failed") );
79 return false;
80 }
81
82 // and handle user clicks on it
83 Connect(GetId(), wxEVT_BUTTON,
84 wxCommandEventHandler(wxGenericFileDirButton::OnButtonClick),
85 NULL, this);
86
87 // create the dialog associated with this button
88 m_path = path;
89 m_message = message;
90 m_wildcard = wildcard;
91
92 return true;
93 }
94
95 void wxGenericFileDirButton::OnButtonClick(wxCommandEvent& WXUNUSED(ev))
96 {
97 wxScopedPtr<wxDialog> p(CreateDialog());
98 if (p->ShowModal() == wxID_OK)
99 {
100 // save updated path in m_path
101 UpdatePathFromDialog(p.get());
102
103 // fire an event
104 wxFileDirPickerEvent event(GetEventType(), this, GetId(), m_path);
105 GetEventHandler()->ProcessEvent(event);
106 }
107 }
108
109 void wxGenericFileDirButton::SetInitialDirectory(const wxString& dir)
110 {
111 m_initialDir = dir;
112 }
113
114 // ----------------------------------------------------------------------------
115 // wxGenericFileButton
116 // ----------------------------------------------------------------------------
117
118 wxDialog *wxGenericFileButton::CreateDialog()
119 {
120 // Determine the initial directory for the dialog: it comes either from the
121 // default path, if it has it, or from the separately specified initial
122 // directory that can be set even if the path is e.g. empty.
123 wxFileName fn(m_path);
124 wxString initialDir = fn.GetPath();
125 if ( initialDir.empty() )
126 initialDir = m_initialDir;
127
128 return new wxFileDialog
129 (
130 GetDialogParent(),
131 m_message,
132 initialDir,
133 fn.GetFullName(),
134 m_wildcard,
135 GetDialogStyle()
136 );
137 }
138
139 // ----------------------------------------------------------------------------
140 // wxGenericDirButton
141 // ----------------------------------------------------------------------------
142
143 wxDialog *wxGenericDirButton::CreateDialog()
144 {
145 wxDirDialog* const dialog = new wxDirDialog
146 (
147 GetDialogParent(),
148 m_message,
149 m_path.empty() ? m_initialDir : m_path,
150 GetDialogStyle()
151 );
152 return dialog;
153 }
154
155 #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL