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
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 class MyApp
: public wxApp
69 virtual bool OnInit();
71 DECLARE_NO_COPY_CLASS(MyApp
)
74 class MyFrame
: public wxFrame
81 void SplitHorizontal(wxCommandEvent
& event
);
82 void SplitVertical(wxCommandEvent
& event
);
83 void Unsplit(wxCommandEvent
& event
);
84 void ToggleLive(wxCommandEvent
& event
);
85 void SetPosition(wxCommandEvent
& event
);
86 void SetMinSize(wxCommandEvent
& event
);
87 void SetGravity(wxCommandEvent
& event
);
88 void Replace(wxCommandEvent
&event
);
90 void Quit(wxCommandEvent
& event
);
92 // Menu command update functions
93 void UpdateUIHorizontal(wxUpdateUIEvent
& event
);
94 void UpdateUIVertical(wxUpdateUIEvent
& event
);
95 void UpdateUIUnsplit(wxUpdateUIEvent
& event
);
98 wxScrolledWindow
*m_left
, *m_right
;
100 wxSplitterWindow
* m_splitter
;
101 wxWindow
*m_replacewindow
;
103 DECLARE_EVENT_TABLE()
104 DECLARE_NO_COPY_CLASS(MyFrame
)
107 class MySplitterWindow
: public wxSplitterWindow
110 MySplitterWindow(wxFrame
*parent
);
113 void OnPositionChanged(wxSplitterEvent
& event
);
114 void OnPositionChanging(wxSplitterEvent
& event
);
115 void OnDClick(wxSplitterEvent
& event
);
116 void OnUnsplitEvent(wxSplitterEvent
& event
);
121 DECLARE_EVENT_TABLE()
122 DECLARE_NO_COPY_CLASS(MySplitterWindow
)
125 class MyCanvas
: public wxScrolledWindow
128 MyCanvas(wxWindow
* parent
, bool mirror
);
129 virtual ~MyCanvas(){};
131 virtual void OnDraw(wxDC
& dc
);
136 DECLARE_NO_COPY_CLASS(MyCanvas
)
139 // ============================================================================
141 // ============================================================================
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
151 if ( !wxApp::OnInit() )
154 // create and show the main frame
155 MyFrame
* frame
= new MyFrame
;
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
166 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
167 EVT_MENU(SPLIT_VERTICAL
, MyFrame::SplitVertical
)
168 EVT_MENU(SPLIT_HORIZONTAL
, MyFrame::SplitHorizontal
)
169 EVT_MENU(SPLIT_UNSPLIT
, MyFrame::Unsplit
)
170 EVT_MENU(SPLIT_LIVE
, MyFrame::ToggleLive
)
171 EVT_MENU(SPLIT_SETPOSITION
, MyFrame::SetPosition
)
172 EVT_MENU(SPLIT_SETMINSIZE
, MyFrame::SetMinSize
)
173 EVT_MENU(SPLIT_SETGRAVITY
, MyFrame::SetGravity
)
174 EVT_MENU(SPLIT_REPLACE
, MyFrame::Replace
)
176 EVT_MENU(SPLIT_QUIT
, MyFrame::Quit
)
178 EVT_UPDATE_UI(SPLIT_VERTICAL
, MyFrame::UpdateUIVertical
)
179 EVT_UPDATE_UI(SPLIT_HORIZONTAL
, MyFrame::UpdateUIHorizontal
)
180 EVT_UPDATE_UI(SPLIT_UNSPLIT
, MyFrame::UpdateUIUnsplit
)
183 // My frame constructor
185 : wxFrame(NULL
, wxID_ANY
, _T("wxSplitterWindow sample"),
186 wxDefaultPosition
, wxSize(420, 300),
187 wxDEFAULT_FRAME_STYLE
| wxNO_FULL_REPAINT_ON_RESIZE
)
191 #endif // wxUSE_STATUSBAR
194 wxMenu
*splitMenu
= new wxMenu
;
195 splitMenu
->Append(SPLIT_VERTICAL
,
196 _T("Split &Vertically\tCtrl-V"),
197 _T("Split vertically"));
198 splitMenu
->Append(SPLIT_HORIZONTAL
,
199 _T("Split &Horizontally\tCtrl-H"),
200 _T("Split horizontally"));
201 splitMenu
->Append(SPLIT_UNSPLIT
,
202 _T("&Unsplit\tCtrl-U"),
204 splitMenu
->AppendSeparator();
206 splitMenu
->AppendCheckItem(SPLIT_LIVE
,
207 _T("&Live update\tCtrl-L"),
208 _T("Toggle live update mode"));
209 splitMenu
->Append(SPLIT_SETPOSITION
,
210 _T("Set splitter &position\tCtrl-P"),
211 _T("Set the splitter position"));
212 splitMenu
->Append(SPLIT_SETMINSIZE
,
213 _T("Set &min size\tCtrl-M"),
214 _T("Set minimum pane size"));
215 splitMenu
->Append(SPLIT_SETGRAVITY
,
216 _T("Set &gravity\tCtrl-G"),
217 _T("Set gravity of sash"));
218 splitMenu
->AppendSeparator();
220 splitMenu
->Append(SPLIT_REPLACE
,
221 _T("&Replace right window"),
222 _T("Replace right window"));
223 splitMenu
->AppendSeparator();
225 splitMenu
->Append(SPLIT_QUIT
, _T("E&xit\tAlt-X"), _T("Exit"));
227 wxMenuBar
*menuBar
= new wxMenuBar
;
228 menuBar
->Append(splitMenu
, _T("&Splitter"));
232 menuBar
->Check(SPLIT_LIVE
, true);
233 m_splitter
= new MySplitterWindow(this);
235 m_splitter
->SetSashGravity(1.0);
238 m_left
= new MyCanvas(m_splitter
, true);
239 m_left
->SetBackgroundColour(*wxRED
);
240 m_left
->SetScrollbars(20, 20, 5, 5);
241 m_left
->SetCursor(wxCursor(wxCURSOR_MAGNIFIER
));
243 m_right
= new MyCanvas(m_splitter
, false);
244 m_right
->SetBackgroundColour(*wxCYAN
);
245 m_right
->SetScrollbars(20, 20, 5, 5);
246 #else // for testing kbd navigation inside the splitter
247 m_left
= new wxTextCtrl(m_splitter
, wxID_ANY
, _T("first text"));
248 m_right
= new wxTextCtrl(m_splitter
, wxID_ANY
, _T("second text"));
251 // you can also do this to start with a single window
253 m_right
->Show(false);
254 m_splitter
->Initialize(m_left
);
256 // you can also try -100
257 m_splitter
->SplitVertically(m_left
, m_right
, 100);
261 SetStatusText(_T("Min pane size = 0"), 1);
262 #endif // wxUSE_STATUSBAR
264 m_replacewindow
= (wxWindow
*)0;
269 if (m_replacewindow
) {
270 m_replacewindow
->Destroy();
271 m_replacewindow
= (wxWindow
*)0;
275 // menu command handlers
277 void MyFrame::Quit(wxCommandEvent
& WXUNUSED(event
) )
282 void MyFrame::SplitHorizontal(wxCommandEvent
& WXUNUSED(event
) )
284 if ( m_splitter
->IsSplit() )
285 m_splitter
->Unsplit();
288 m_splitter
->SplitHorizontally( m_left
, m_right
);
291 SetStatusText(_T("Splitter split horizontally"), 1);
292 #endif // wxUSE_STATUSBAR
295 void MyFrame::SplitVertical(wxCommandEvent
& WXUNUSED(event
) )
297 if ( m_splitter
->IsSplit() )
298 m_splitter
->Unsplit();
301 m_splitter
->SplitVertically( m_left
, m_right
);
304 SetStatusText(_T("Splitter split vertically"), 1);
305 #endif // wxUSE_STATUSBAR
308 void MyFrame::Unsplit(wxCommandEvent
& WXUNUSED(event
) )
310 if ( m_splitter
->IsSplit() )
311 m_splitter
->Unsplit();
313 SetStatusText(_T("No splitter"));
314 #endif // wxUSE_STATUSBAR
317 void MyFrame::ToggleLive(wxCommandEvent
& event
)
319 long style
= m_splitter
->GetWindowStyleFlag();
320 if ( event
.IsChecked() )
321 style
|= wxSP_LIVE_UPDATE
;
323 style
&= ~wxSP_LIVE_UPDATE
;
325 m_splitter
->SetWindowStyleFlag(style
);
328 void MyFrame::SetPosition(wxCommandEvent
& WXUNUSED(event
) )
331 str
.Printf( wxT("%d"), m_splitter
->GetSashPosition());
332 str
= wxGetTextFromUser(_T("Enter splitter position:"), _T(""), str
, this);
337 if ( !str
.ToLong(&pos
) )
339 wxLogError(_T("The splitter position should be an integer."));
343 m_splitter
->SetSashPosition(pos
);
345 wxLogStatus(this, _T("Splitter position set to %ld"), pos
);
348 void MyFrame::SetMinSize(wxCommandEvent
& WXUNUSED(event
) )
351 str
.Printf( wxT("%d"), m_splitter
->GetMinimumPaneSize());
352 str
= wxGetTextFromUser(_T("Enter minimal size for panes:"), _T(""), str
, this);
356 int minsize
= wxStrtol( str
, (wxChar
**)NULL
, 10 );
357 m_splitter
->SetMinimumPaneSize(minsize
);
359 str
.Printf( wxT("Min pane size = %d"), minsize
);
360 SetStatusText(str
, 1);
361 #endif // wxUSE_STATUSBAR
363 void MyFrame::SetGravity(wxCommandEvent
& WXUNUSED(event
) )
366 str
.Printf( wxT("%g"), m_splitter
->GetSashGravity());
367 str
= wxGetTextFromUser(_T("Enter sash gravity (0,1):"), _T(""), str
, this);
371 double gravity
= wxStrtod( str
, (wxChar
**)NULL
);
372 m_splitter
->SetSashGravity(gravity
);
374 str
.Printf( wxT("Gravity = %g"), gravity
);
375 SetStatusText(str
, 1);
376 #endif // wxUSE_STATUSBAR
379 void MyFrame::Replace(wxCommandEvent
& WXUNUSED(event
) )
381 if (m_replacewindow
== 0) {
382 m_replacewindow
= m_splitter
->GetWindow2();
383 m_splitter
->ReplaceWindow(m_replacewindow
, new wxPanel(m_splitter
, wxID_ANY
));
384 m_replacewindow
->Hide();
386 wxWindow
*empty
= m_splitter
->GetWindow2();
387 m_splitter
->ReplaceWindow(empty
, m_replacewindow
);
388 m_replacewindow
->Show();
394 // Update UI handlers
396 void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent
& event
)
398 event
.Enable( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_HORIZONTAL
) );
401 void MyFrame::UpdateUIVertical(wxUpdateUIEvent
& event
)
403 event
.Enable( ( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_VERTICAL
) ) );
406 void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent
& event
)
408 event
.Enable( m_splitter
->IsSplit() );
411 // ----------------------------------------------------------------------------
413 // ----------------------------------------------------------------------------
415 BEGIN_EVENT_TABLE(MySplitterWindow
, wxSplitterWindow
)
416 EVT_SPLITTER_SASH_POS_CHANGED(wxID_ANY
, MySplitterWindow::OnPositionChanged
)
417 EVT_SPLITTER_SASH_POS_CHANGING(wxID_ANY
, MySplitterWindow::OnPositionChanging
)
419 EVT_SPLITTER_DCLICK(wxID_ANY
, MySplitterWindow::OnDClick
)
421 EVT_SPLITTER_UNSPLIT(wxID_ANY
, MySplitterWindow::OnUnsplitEvent
)
424 MySplitterWindow::MySplitterWindow(wxFrame
*parent
)
425 : wxSplitterWindow(parent
, wxID_ANY
,
426 wxDefaultPosition
, wxDefaultSize
,
427 wxSP_3D
| wxSP_LIVE_UPDATE
|
428 wxCLIP_CHILDREN
/* | wxSP_NO_XP_THEME */ )
433 void MySplitterWindow::OnPositionChanged(wxSplitterEvent
& event
)
435 wxLogStatus(m_frame
, _T("Position has changed, now = %d (or %d)"),
436 event
.GetSashPosition(), GetSashPosition());
441 void MySplitterWindow::OnPositionChanging(wxSplitterEvent
& event
)
443 wxLogStatus(m_frame
, _T("Position is changing, now = %d (or %d)"),
444 event
.GetSashPosition(), GetSashPosition());
449 void MySplitterWindow::OnDClick(wxSplitterEvent
& event
)
452 m_frame
->SetStatusText(_T("Splitter double clicked"), 1);
453 #endif // wxUSE_STATUSBAR
458 void MySplitterWindow::OnUnsplitEvent(wxSplitterEvent
& event
)
461 m_frame
->SetStatusText(_T("Splitter unsplit"), 1);
462 #endif // wxUSE_STATUSBAR
467 // ----------------------------------------------------------------------------
469 // ----------------------------------------------------------------------------
471 MyCanvas::MyCanvas(wxWindow
* parent
, bool mirror
)
472 : wxScrolledWindow(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
473 wxHSCROLL
| wxVSCROLL
| wxNO_FULL_REPAINT_ON_RESIZE
)
478 void MyCanvas::OnDraw(wxDC
& dcOrig
)
480 wxMirrorDC
dc(dcOrig
, m_mirror
);
482 dc
.SetPen(*wxBLACK_PEN
);
483 dc
.DrawLine(0, 0, 100, 200);
485 dc
.SetBackgroundMode(wxTRANSPARENT
);
486 dc
.DrawText(_T("Testing"), 50, 50);
488 dc
.SetPen(*wxRED_PEN
);
489 dc
.SetBrush(*wxGREEN_BRUSH
);
490 dc
.DrawRectangle(120, 120, 100, 80);