Allow specification of border spacing
[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 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
74 m_sheetOuterBorder = 0;
75 #endif
76 topSizer->Add(m_innerSizer, 1, wxGROW|wxALL, m_sheetOuterBorder);
77
78 m_bookCtrl = CreateBookCtrl();
79 AddBookCtrl(m_innerSizer);
80
81 return true;
82 }
83
84 void wxPropertySheetDialog::Init()
85 {
86 m_sheetStyle = wxPROPSHEET_DEFAULT;
87 m_innerSizer = NULL;
88 m_bookCtrl = NULL;
89 m_sheetOuterBorder = 2;
90 m_sheetInnerBorder = 5;
91 }
92
93 // Layout the dialog, to be called after pages have been created
94 void wxPropertySheetDialog::LayoutDialog(int centreFlags)
95 {
96 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
97 GetSizer()->Fit(this);
98 GetSizer()->SetSizeHints(this);
99 if (centreFlags)
100 Centre(centreFlags);
101 #else
102 wxUnusedVar(centreFlags);
103 #endif
104 #if defined(__SMARTPHONE__)
105 if (m_bookCtrl)
106 m_bookCtrl->SetFocus();
107 #endif
108 }
109
110 // Creates the buttons, if any
111 void wxPropertySheetDialog::CreateButtons(int flags)
112 {
113 #ifdef __POCKETPC__
114 // keep system option status
115 const wxChar *optionName = wxT("wince.dialog.real-ok-cancel");
116 const int status = wxSystemOptions::GetOptionInt(optionName);
117 wxSystemOptions::SetOption(optionName,0);
118 #endif
119
120 wxSizer *buttonSizer = CreateButtonSizer( flags & ButtonSizerFlags );
121 if(buttonSizer->GetChildren().GetCount() > 0 )
122 {
123 m_innerSizer->Add( buttonSizer, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxTOP|wxBOTTOM|wxLEFT|wxRIGHT, 2);
124 m_innerSizer->AddSpacer(2);
125 }
126 else
127 {
128 delete buttonSizer;
129 }
130
131 #ifdef __POCKETPC__
132 // restore system option
133 wxSystemOptions::SetOption(optionName,status);
134 #endif
135 }
136
137 // Creates the book control
138 wxBookCtrlBase* wxPropertySheetDialog::CreateBookCtrl()
139 {
140 int style = wxCLIP_CHILDREN;
141 #if defined(__POCKETPC__) && wxUSE_NOTEBOOK
142 style |= wxBK_BOTTOM|wxNB_FLAT;
143 #else
144 style |= wxBK_DEFAULT;
145 #endif
146
147 wxBookCtrlBase* bookCtrl = NULL;
148
149 #if wxUSE_NOTEBOOK
150 if (GetSheetStyle() & wxPROPSHEET_NOTEBOOK)
151 bookCtrl = new wxNotebook(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, style );
152 #endif
153 #if wxUSE_CHOICEBOOK
154 if (GetSheetStyle() & wxPROPSHEET_CHOICEBOOK)
155 bookCtrl = new wxChoicebook(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, style );
156 #endif
157 #if wxUSE_TOOLBOOK
158 #if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
159 if (GetSheetStyle() & wxPROPSHEET_BUTTONTOOLBOOK)
160 bookCtrl = new wxToolbook(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, style|wxBK_BUTTONBAR );
161 else
162 #endif
163 if ((GetSheetStyle() & wxPROPSHEET_TOOLBOOK) || (GetSheetStyle() & wxPROPSHEET_BUTTONTOOLBOOK))
164 bookCtrl = new wxToolbook(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, style );
165 #endif
166 #if wxUSE_LISTBOOK
167 if (GetSheetStyle() & wxPROPSHEET_LISTBOOK)
168 bookCtrl = new wxListbook(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, style );
169 #endif
170 if (!bookCtrl)
171 bookCtrl = new wxBookCtrl(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, style );
172
173 if (GetSheetStyle() & wxPROPSHEET_SHRINKTOFIT)
174 bookCtrl->SetFitToCurrentPage(true);
175
176 return bookCtrl;
177 }
178
179 // Adds the book control to the inner sizer.
180 void wxPropertySheetDialog::AddBookCtrl(wxSizer* sizer)
181 {
182 #if defined(__POCKETPC__) && wxUSE_NOTEBOOK
183 // The book control has to be sized larger than the dialog because of a border bug
184 // in WinCE
185 int borderSize = -2;
186 sizer->Add( m_bookCtrl, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxLEFT|wxTOP|wxRIGHT, borderSize );
187 #else
188 sizer->Add( m_bookCtrl, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, m_sheetInnerBorder );
189 #endif
190 }
191
192 void wxPropertySheetDialog::OnActivate(wxActivateEvent& event)
193 {
194 #if defined(__SMARTPHONE__)
195 // Attempt to focus the choice control: not yet working, but might
196 // be a step in the right direction. OnActivate overrides the default
197 // handler in toplevel.cpp that sets the focus for the first child of
198 // of the dialog (the choicebook).
199 if (event.GetActive())
200 {
201 wxChoicebook* choiceBook = wxDynamicCast(GetBookCtrl(), wxChoicebook);
202 if (choiceBook)
203 choiceBook->SetFocus();
204 }
205 else
206 #endif
207 event.Skip();
208 }
209
210 // Resize dialog if necessary
211 void wxPropertySheetDialog::OnIdle(wxIdleEvent& event)
212 {
213 event.Skip();
214
215 if ((GetSheetStyle() & wxPROPSHEET_SHRINKTOFIT) && GetBookCtrl())
216 {
217 int sel = GetBookCtrl()->GetSelection();
218 if (sel != -1 && sel != m_selectedPage)
219 {
220 GetBookCtrl()->InvalidateBestSize();
221 InvalidateBestSize();
222 SetSizeHints(-1, -1, -1, -1);
223
224 m_selectedPage = sel;
225 LayoutDialog(0);
226 }
227 }
228 }
229
230 #endif // wxUSE_BOOKCTRL
231