1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSplitterWindow sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
33 #include "wx/scrolwin.h"
36 #include "wx/textdlg.h" // for wxGetTextFromUser
39 #include "wx/splitter.h"
40 #include "wx/dcmirror.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // ID for the menu commands
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 class MyApp
: public wxApp
71 virtual bool OnInit();
73 DECLARE_NO_COPY_CLASS(MyApp
)
76 class MyFrame
: public wxFrame
83 void SplitHorizontal(wxCommandEvent
& event
);
84 void SplitVertical(wxCommandEvent
& event
);
85 void Unsplit(wxCommandEvent
& event
);
86 void ToggleFlag(int flag
, bool enable
);
87 void ToggleLive(wxCommandEvent
& event
)
88 { ToggleFlag(wxSP_LIVE_UPDATE
, event
.IsChecked()); }
89 void ToggleBorder(wxCommandEvent
& event
)
90 { ToggleFlag(wxSP_BORDER
, event
.IsChecked()); }
91 void Toggle3DSash(wxCommandEvent
& event
)
92 { ToggleFlag(wxSP_3DSASH
, event
.IsChecked()); }
93 void SetPosition(wxCommandEvent
& event
);
94 void SetMinSize(wxCommandEvent
& event
);
95 void SetGravity(wxCommandEvent
& event
);
96 void Replace(wxCommandEvent
&event
);
98 void Quit(wxCommandEvent
& event
);
100 // Menu command update functions
101 void UpdateUIHorizontal(wxUpdateUIEvent
& event
);
102 void UpdateUIVertical(wxUpdateUIEvent
& event
);
103 void UpdateUIUnsplit(wxUpdateUIEvent
& event
);
106 wxScrolledWindow
*m_left
, *m_right
;
108 wxSplitterWindow
* m_splitter
;
109 wxWindow
*m_replacewindow
;
111 DECLARE_EVENT_TABLE()
112 DECLARE_NO_COPY_CLASS(MyFrame
)
115 class MySplitterWindow
: public wxSplitterWindow
118 MySplitterWindow(wxFrame
*parent
);
121 void OnPositionChanged(wxSplitterEvent
& event
);
122 void OnPositionChanging(wxSplitterEvent
& event
);
123 void OnDClick(wxSplitterEvent
& event
);
124 void OnUnsplitEvent(wxSplitterEvent
& event
);
129 DECLARE_EVENT_TABLE()
130 DECLARE_NO_COPY_CLASS(MySplitterWindow
)
133 class MyCanvas
: public wxScrolledWindow
136 MyCanvas(wxWindow
* parent
, bool mirror
);
137 virtual ~MyCanvas(){};
139 virtual void OnDraw(wxDC
& dc
);
144 DECLARE_NO_COPY_CLASS(MyCanvas
)
147 // ============================================================================
149 // ============================================================================
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
159 if ( !wxApp::OnInit() )
162 // create and show the main frame
163 MyFrame
* frame
= new MyFrame
;
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
174 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
175 EVT_MENU(SPLIT_VERTICAL
, MyFrame::SplitVertical
)
176 EVT_MENU(SPLIT_HORIZONTAL
, MyFrame::SplitHorizontal
)
177 EVT_MENU(SPLIT_UNSPLIT
, MyFrame::Unsplit
)
178 EVT_MENU(SPLIT_LIVE
, MyFrame::ToggleLive
)
179 EVT_MENU(SPLIT_BORDER
, MyFrame::ToggleBorder
)
180 EVT_MENU(SPLIT_3DSASH
, MyFrame::Toggle3DSash
)
181 EVT_MENU(SPLIT_SETPOSITION
, MyFrame::SetPosition
)
182 EVT_MENU(SPLIT_SETMINSIZE
, MyFrame::SetMinSize
)
183 EVT_MENU(SPLIT_SETGRAVITY
, MyFrame::SetGravity
)
184 EVT_MENU(SPLIT_REPLACE
, MyFrame::Replace
)
186 EVT_MENU(SPLIT_QUIT
, MyFrame::Quit
)
188 EVT_UPDATE_UI(SPLIT_VERTICAL
, MyFrame::UpdateUIVertical
)
189 EVT_UPDATE_UI(SPLIT_HORIZONTAL
, MyFrame::UpdateUIHorizontal
)
190 EVT_UPDATE_UI(SPLIT_UNSPLIT
, MyFrame::UpdateUIUnsplit
)
193 // My frame constructor
195 : wxFrame(NULL
, wxID_ANY
, _T("wxSplitterWindow sample"),
196 wxDefaultPosition
, wxSize(420, 300),
197 wxDEFAULT_FRAME_STYLE
| wxNO_FULL_REPAINT_ON_RESIZE
)
201 #endif // wxUSE_STATUSBAR
204 wxMenu
*splitMenu
= new wxMenu
;
205 splitMenu
->Append(SPLIT_VERTICAL
,
206 _T("Split &Vertically\tCtrl-V"),
207 _T("Split vertically"));
208 splitMenu
->Append(SPLIT_HORIZONTAL
,
209 _T("Split &Horizontally\tCtrl-H"),
210 _T("Split horizontally"));
211 splitMenu
->Append(SPLIT_UNSPLIT
,
212 _T("&Unsplit\tCtrl-U"),
214 splitMenu
->AppendSeparator();
216 splitMenu
->AppendCheckItem(SPLIT_LIVE
,
217 _T("&Live update\tCtrl-L"),
218 _T("Toggle live update mode"));
219 splitMenu
->AppendCheckItem(SPLIT_BORDER
,
221 _T("Toggle wxSP_BORDER flag"));
222 splitMenu
->Check(SPLIT_BORDER
, true);
223 splitMenu
->AppendCheckItem(SPLIT_3DSASH
,
225 _T("Toggle wxSP_3DSASH flag"));
226 splitMenu
->Check(SPLIT_3DSASH
, true);
227 splitMenu
->Append(SPLIT_SETPOSITION
,
228 _T("Set splitter &position\tCtrl-P"),
229 _T("Set the splitter position"));
230 splitMenu
->Append(SPLIT_SETMINSIZE
,
231 _T("Set &min size\tCtrl-M"),
232 _T("Set minimum pane size"));
233 splitMenu
->Append(SPLIT_SETGRAVITY
,
234 _T("Set &gravity\tCtrl-G"),
235 _T("Set gravity of sash"));
236 splitMenu
->AppendSeparator();
238 splitMenu
->Append(SPLIT_REPLACE
,
239 _T("&Replace right window"),
240 _T("Replace right window"));
241 splitMenu
->AppendSeparator();
243 splitMenu
->Append(SPLIT_QUIT
, _T("E&xit\tAlt-X"), _T("Exit"));
245 wxMenuBar
*menuBar
= new wxMenuBar
;
246 menuBar
->Append(splitMenu
, _T("&Splitter"));
250 menuBar
->Check(SPLIT_LIVE
, true);
251 m_splitter
= new MySplitterWindow(this);
253 m_splitter
->SetSashGravity(1.0);
256 m_left
= new MyCanvas(m_splitter
, true);
257 m_left
->SetBackgroundColour(*wxRED
);
258 m_left
->SetScrollbars(20, 20, 5, 5);
259 m_left
->SetCursor(wxCursor(wxCURSOR_MAGNIFIER
));
261 m_right
= new MyCanvas(m_splitter
, false);
262 m_right
->SetBackgroundColour(*wxCYAN
);
263 m_right
->SetScrollbars(20, 20, 5, 5);
264 #else // for testing kbd navigation inside the splitter
265 m_left
= new wxTextCtrl(m_splitter
, wxID_ANY
, _T("first text"));
266 m_right
= new wxTextCtrl(m_splitter
, wxID_ANY
, _T("second text"));
269 // you can also do this to start with a single window
271 m_right
->Show(false);
272 m_splitter
->Initialize(m_left
);
274 // you can also try -100
275 m_splitter
->SplitVertically(m_left
, m_right
, 100);
279 SetStatusText(_T("Min pane size = 0"), 1);
280 #endif // wxUSE_STATUSBAR
282 m_replacewindow
= (wxWindow
*)0;
287 if (m_replacewindow
) {
288 m_replacewindow
->Destroy();
289 m_replacewindow
= (wxWindow
*)0;
293 // menu command handlers
295 void MyFrame::Quit(wxCommandEvent
& WXUNUSED(event
) )
300 void MyFrame::SplitHorizontal(wxCommandEvent
& WXUNUSED(event
) )
302 if ( m_splitter
->IsSplit() )
303 m_splitter
->Unsplit();
306 m_splitter
->SplitHorizontally( m_left
, m_right
);
309 SetStatusText(_T("Splitter split horizontally"), 1);
310 #endif // wxUSE_STATUSBAR
313 void MyFrame::SplitVertical(wxCommandEvent
& WXUNUSED(event
) )
315 if ( m_splitter
->IsSplit() )
316 m_splitter
->Unsplit();
319 m_splitter
->SplitVertically( m_left
, m_right
);
322 SetStatusText(_T("Splitter split vertically"), 1);
323 #endif // wxUSE_STATUSBAR
326 void MyFrame::Unsplit(wxCommandEvent
& WXUNUSED(event
) )
328 if ( m_splitter
->IsSplit() )
329 m_splitter
->Unsplit();
331 SetStatusText(_T("No splitter"));
332 #endif // wxUSE_STATUSBAR
335 void MyFrame::ToggleFlag(int flag
, bool enable
)
337 long style
= m_splitter
->GetWindowStyleFlag();
343 m_splitter
->SetWindowStyleFlag(style
);
345 // we need to move sash to redraw it
346 int pos
= m_splitter
->GetSashPosition();
347 m_splitter
->SetSashPosition(pos
+ 1);
348 m_splitter
->SetSashPosition(pos
);
351 void MyFrame::SetPosition(wxCommandEvent
& WXUNUSED(event
) )
354 str
.Printf( wxT("%d"), m_splitter
->GetSashPosition());
356 str
= wxGetTextFromUser(_T("Enter splitter position:"), _T(""), str
, this);
362 if ( !str
.ToLong(&pos
) )
364 wxLogError(_T("The splitter position should be an integer."));
368 m_splitter
->SetSashPosition(pos
);
370 wxLogStatus(this, _T("Splitter position set to %ld"), pos
);
373 void MyFrame::SetMinSize(wxCommandEvent
& WXUNUSED(event
) )
376 str
.Printf( wxT("%d"), m_splitter
->GetMinimumPaneSize());
378 str
= wxGetTextFromUser(_T("Enter minimal size for panes:"), _T(""), str
, this);
383 int minsize
= wxStrtol( str
, (wxChar
**)NULL
, 10 );
384 m_splitter
->SetMinimumPaneSize(minsize
);
386 str
.Printf( wxT("Min pane size = %d"), minsize
);
387 SetStatusText(str
, 1);
388 #endif // wxUSE_STATUSBAR
390 void MyFrame::SetGravity(wxCommandEvent
& WXUNUSED(event
) )
393 str
.Printf( wxT("%g"), m_splitter
->GetSashGravity());
395 str
= wxGetTextFromUser(_T("Enter sash gravity (0,1):"), _T(""), str
, this);
400 double gravity
= wxStrtod( str
, (wxChar
**)NULL
);
401 m_splitter
->SetSashGravity(gravity
);
403 str
.Printf( wxT("Gravity = %g"), gravity
);
404 SetStatusText(str
, 1);
405 #endif // wxUSE_STATUSBAR
408 void MyFrame::Replace(wxCommandEvent
& WXUNUSED(event
) )
410 if (m_replacewindow
== 0) {
411 m_replacewindow
= m_splitter
->GetWindow2();
412 m_splitter
->ReplaceWindow(m_replacewindow
, new wxPanel(m_splitter
, wxID_ANY
));
413 m_replacewindow
->Hide();
415 wxWindow
*empty
= m_splitter
->GetWindow2();
416 m_splitter
->ReplaceWindow(empty
, m_replacewindow
);
417 m_replacewindow
->Show();
423 // Update UI handlers
425 void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent
& event
)
427 event
.Enable( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_HORIZONTAL
) );
430 void MyFrame::UpdateUIVertical(wxUpdateUIEvent
& event
)
432 event
.Enable( ( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_VERTICAL
) ) );
435 void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent
& event
)
437 event
.Enable( m_splitter
->IsSplit() );
440 // ----------------------------------------------------------------------------
442 // ----------------------------------------------------------------------------
444 BEGIN_EVENT_TABLE(MySplitterWindow
, wxSplitterWindow
)
445 EVT_SPLITTER_SASH_POS_CHANGED(wxID_ANY
, MySplitterWindow::OnPositionChanged
)
446 EVT_SPLITTER_SASH_POS_CHANGING(wxID_ANY
, MySplitterWindow::OnPositionChanging
)
448 EVT_SPLITTER_DCLICK(wxID_ANY
, MySplitterWindow::OnDClick
)
450 EVT_SPLITTER_UNSPLIT(wxID_ANY
, MySplitterWindow::OnUnsplitEvent
)
453 MySplitterWindow::MySplitterWindow(wxFrame
*parent
)
454 : wxSplitterWindow(parent
, wxID_ANY
,
455 wxDefaultPosition
, wxDefaultSize
,
456 wxSP_3D
| wxSP_LIVE_UPDATE
|
457 wxCLIP_CHILDREN
/* | wxSP_NO_XP_THEME */ )
462 void MySplitterWindow::OnPositionChanged(wxSplitterEvent
& event
)
464 wxLogStatus(m_frame
, _T("Position has changed, now = %d (or %d)"),
465 event
.GetSashPosition(), GetSashPosition());
470 void MySplitterWindow::OnPositionChanging(wxSplitterEvent
& event
)
472 wxLogStatus(m_frame
, _T("Position is changing, now = %d (or %d)"),
473 event
.GetSashPosition(), GetSashPosition());
478 void MySplitterWindow::OnDClick(wxSplitterEvent
& event
)
481 m_frame
->SetStatusText(_T("Splitter double clicked"), 1);
482 #endif // wxUSE_STATUSBAR
487 void MySplitterWindow::OnUnsplitEvent(wxSplitterEvent
& event
)
490 m_frame
->SetStatusText(_T("Splitter unsplit"), 1);
491 #endif // wxUSE_STATUSBAR
496 // ----------------------------------------------------------------------------
498 // ----------------------------------------------------------------------------
500 MyCanvas::MyCanvas(wxWindow
* parent
, bool mirror
)
501 : wxScrolledWindow(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
502 wxHSCROLL
| wxVSCROLL
| wxNO_FULL_REPAINT_ON_RESIZE
)
507 void MyCanvas::OnDraw(wxDC
& dcOrig
)
509 wxMirrorDC
dc(dcOrig
, m_mirror
);
511 dc
.SetPen(*wxBLACK_PEN
);
512 dc
.DrawLine(0, 0, 100, 200);
514 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
515 dc
.DrawText(_T("Testing"), 50, 50);
517 dc
.SetPen(*wxRED_PEN
);
518 dc
.SetBrush(*wxGREEN_BRUSH
);
519 dc
.DrawRectangle(120, 120, 100, 80);