Steps towards implementing native-style, non-top-level toolbars on Mac
[wxWidgets.git] / src / generic / propdlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/propdlg.cpp
3 // Purpose: wxPropertySheetDialog
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 2005-03-12
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #include "wx/defs.h"
20
21 #if wxUSE_BOOKCTRL
22
23 #ifndef WX_PRECOMP
24 #include "wx/button.h"
25 #include "wx/sizer.h"
26 #include "wx/intl.h"
27 #include "wx/log.h"
28 #include "wx/msgdlg.h"
29 #endif
30
31 #include "wx/bookctrl.h"
32
33 #if wxUSE_NOTEBOOK
34 #include "wx/notebook.h"
35 #endif
36 #if wxUSE_CHOICEBOOK
37 #include "wx/choicebk.h"
38 #endif
39 #if wxUSE_TOOLBOOK
40 #include "wx/toolbook.h"
41 #endif
42 #if wxUSE_LISTBOOK
43 #include "wx/listbook.h"
44 #endif
45
46 #include "wx/generic/propdlg.h"
47 #include "wx/sysopt.h"
48
49 //-----------------------------------------------------------------------------
50 // wxPropertySheetDialog
51 //-----------------------------------------------------------------------------
52
53 IMPLEMENT_DYNAMIC_CLASS(wxPropertySheetDialog, wxDialog)
54
55 BEGIN_EVENT_TABLE(wxPropertySheetDialog, wxDialog)
56 EVT_ACTIVATE(wxPropertySheetDialog::OnActivate)
57 EVT_IDLE(wxPropertySheetDialog::OnIdle)
58 END_EVENT_TABLE()
59
60 bool wxPropertySheetDialog::Create(wxWindow* parent, wxWindowID id, const wxString& title,
61 const wxPoint& pos, const wxSize& sz, long style,
62 const wxString& name)
63 {
64 if (!wxDialog::Create(parent, id, title, pos, sz, style|wxCLIP_CHILDREN, name))
65 return false;
66
67 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
68 SetSizer(topSizer);
69
70 // This gives more space around the edges
71 m_innerSizer = new wxBoxSizer( wxVERTICAL );
72
73 int extraSpace = 2;
74 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
75 extraSpace=0;
76 #endif
77 topSizer->Add(m_innerSizer, 1, wxGROW|wxALL, extraSpace);
78
79 m_bookCtrl = CreateBookCtrl();
80 AddBookCtrl(m_innerSizer);
81
82 return true;
83 }
84
85 void wxPropertySheetDialog::Init()
86 {
87 m_sheetStyle = wxPROPSHEET_DEFAULT;
88 m_innerSizer = NULL;
89 m_bookCtrl = NULL;
90 }
91
92 // Layout the dialog, to be called after pages have been created
93 void wxPropertySheetDialog::LayoutDialog(int centreFlags)
94 {
95 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
96 GetSizer()->Fit(this);
97 GetSizer()->SetSizeHints(this);
98 if (centreFlags)
99 Centre(centreFlags);
100 #else
101 wxUnusedVar(centreFlags);
102 #endif
103 #if defined(__SMARTPHONE__)
104 if (m_bookCtrl)
105 m_bookCtrl->SetFocus();
106 #endif
107 }
108
109 // Creates the buttons, if any
110 void wxPropertySheetDialog::CreateButtons(int flags)
111 {
112 #ifdef __POCKETPC__
113 // keep system option status
114 const wxChar *optionName = wxT("wince.dialog.real-ok-cancel");
115 const int status = wxSystemOptions::GetOptionInt(optionName);
116 wxSystemOptions::SetOption(optionName,0);
117 #endif
118
119 wxSizer *buttonSizer = CreateButtonSizer( flags & ButtonSizerFlags );
120 if(buttonSizer->GetChildren().GetCount() > 0 )
121 {
122 m_innerSizer->Add( buttonSizer, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxTOP|wxBOTTOM|wxLEFT|wxRIGHT, 2);
123 m_innerSizer->AddSpacer(2);
124 }
125 else
126 {
127 delete buttonSizer;
128 }
129
130 #ifdef __POCKETPC__
131 // restore system option
132 wxSystemOptions::SetOption(optionName,status);
133 #endif
134 }
135
136 // Creates the book control
137 wxBookCtrlBase* wxPropertySheetDialog::CreateBookCtrl()
138 {
139 int style = wxCLIP_CHILDREN;
140 #if defined(__POCKETPC__) && wxUSE_NOTEBOOK
141 style |= wxBK_BOTTOM|wxNB_FLAT;
142 #else
143 style |= wxBK_DEFAULT;
144 #endif
145
146 wxBookCtrlBase* bookCtrl = NULL;
147
148 #if wxUSE_NOTEBOOK
149 if (GetSheetStyle() & wxPROPSHEET_NOTEBOOK)
150 bookCtrl = new wxNotebook(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, style );
151 #endif
152 #if wxUSE_CHOICEBOOK
153 if (GetSheetStyle() & wxPROPSHEET_CHOICEBOOK)
154 bookCtrl = new wxChoicebook(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, style );
155 #endif
156 #if wxUSE_TOOLBOOK
157 #if wxUSE_TOOLBAR && wxUSE_BMPBUTTON
158 if (GetSheetStyle() & wxPROPSHEET_BUTTONTOOLBOOK)
159 bookCtrl = new wxToolbook(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, style|wxBK_BUTTONBAR );
160 else
161 #endif
162 if ((GetSheetStyle() & wxPROPSHEET_TOOLBOOK) || (GetSheetStyle() & wxPROPSHEET_BUTTONTOOLBOOK))
163 bookCtrl = new wxToolbook(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, style );
164 #endif
165 #if wxUSE_LISTBOOK
166 if (GetSheetStyle() & wxPROPSHEET_LISTBOOK)
167 bookCtrl = new wxListbook(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, style );
168 #endif
169 if (!bookCtrl)
170 bookCtrl = new wxBookCtrl(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, style );
171
172 if (GetSheetStyle() & wxPROPSHEET_SHRINKTOFIT)
173 bookCtrl->SetFitToCurrentPage(true);
174
175 return bookCtrl;
176 }
177
178 // Adds the book control to the inner sizer.
179 void wxPropertySheetDialog::AddBookCtrl(wxSizer* sizer)
180 {
181 #if defined(__POCKETPC__) && wxUSE_NOTEBOOK
182 // The book control has to be sized larger than the dialog because of a border bug
183 // in WinCE
184 int borderSize = -2;
185 sizer->Add( m_bookCtrl, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxLEFT|wxTOP|wxRIGHT, borderSize );
186 #else
187 sizer->Add( m_bookCtrl, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
188 #endif
189 }
190
191 void wxPropertySheetDialog::OnActivate(wxActivateEvent& event)
192 {
193 #if defined(__SMARTPHONE__)
194 // Attempt to focus the choice control: not yet working, but might
195 // be a step in the right direction. OnActivate overrides the default
196 // handler in toplevel.cpp that sets the focus for the first child of
197 // of the dialog (the choicebook).
198 if (event.GetActive())
199 {
200 wxChoicebook* choiceBook = wxDynamicCast(GetBookCtrl(), wxChoicebook);
201 if (choiceBook)
202 choiceBook->SetFocus();
203 }
204 else
205 #endif
206 event.Skip();
207 }
208
209 // Resize dialog if necessary
210 void wxPropertySheetDialog::OnIdle(wxIdleEvent& event)
211 {
212 event.Skip();
213
214 if ((GetSheetStyle() & wxPROPSHEET_SHRINKTOFIT) && GetBookCtrl())
215 {
216 int sel = GetBookCtrl()->GetSelection();
217 if (sel != -1 && sel != m_selectedPage)
218 {
219 GetBookCtrl()->InvalidateBestSize();
220 InvalidateBestSize();
221 SetSizeHints(-1, -1, -1, -1);
222
223 m_selectedPage = sel;
224 LayoutDialog(0);
225 }
226 }
227 }
228
229 #endif // wxUSE_BOOKCTRL
230