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 // create and show the main frame
152 MyFrame
* frame
= new MyFrame
;
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
164 EVT_MENU(SPLIT_VERTICAL
, MyFrame::SplitVertical
)
165 EVT_MENU(SPLIT_HORIZONTAL
, MyFrame::SplitHorizontal
)
166 EVT_MENU(SPLIT_UNSPLIT
, MyFrame::Unsplit
)
167 EVT_MENU(SPLIT_LIVE
, MyFrame::ToggleLive
)
168 EVT_MENU(SPLIT_SETPOSITION
, MyFrame::SetPosition
)
169 EVT_MENU(SPLIT_SETMINSIZE
, MyFrame::SetMinSize
)
170 EVT_MENU(SPLIT_SETGRAVITY
, MyFrame::SetGravity
)
171 EVT_MENU(SPLIT_REPLACE
, MyFrame::Replace
)
173 EVT_MENU(SPLIT_QUIT
, MyFrame::Quit
)
175 EVT_UPDATE_UI(SPLIT_VERTICAL
, MyFrame::UpdateUIVertical
)
176 EVT_UPDATE_UI(SPLIT_HORIZONTAL
, MyFrame::UpdateUIHorizontal
)
177 EVT_UPDATE_UI(SPLIT_UNSPLIT
, MyFrame::UpdateUIUnsplit
)
180 // My frame constructor
182 : wxFrame(NULL
, wxID_ANY
, _T("wxSplitterWindow sample"),
183 wxDefaultPosition
, wxSize(420, 300),
184 wxDEFAULT_FRAME_STYLE
| wxNO_FULL_REPAINT_ON_RESIZE
)
188 #endif // wxUSE_STATUSBAR
191 wxMenu
*splitMenu
= new wxMenu
;
192 splitMenu
->Append(SPLIT_VERTICAL
,
193 _T("Split &Vertically\tCtrl-V"),
194 _T("Split vertically"));
195 splitMenu
->Append(SPLIT_HORIZONTAL
,
196 _T("Split &Horizontally\tCtrl-H"),
197 _T("Split horizontally"));
198 splitMenu
->Append(SPLIT_UNSPLIT
,
199 _T("&Unsplit\tCtrl-U"),
201 splitMenu
->AppendSeparator();
203 splitMenu
->AppendCheckItem(SPLIT_LIVE
,
204 _T("&Live update\tCtrl-L"),
205 _T("Toggle live update mode"));
206 splitMenu
->Append(SPLIT_SETPOSITION
,
207 _T("Set splitter &position\tCtrl-P"),
208 _T("Set the splitter position"));
209 splitMenu
->Append(SPLIT_SETMINSIZE
,
210 _T("Set &min size\tCtrl-M"),
211 _T("Set minimum pane size"));
212 splitMenu
->Append(SPLIT_SETGRAVITY
,
213 _T("Set &gravity\tCtrl-G"),
214 _T("Set gravity of sash"));
215 splitMenu
->AppendSeparator();
217 splitMenu
->Append(SPLIT_REPLACE
,
218 _T("&Replace right window"),
219 _T("Replace right window"));
220 splitMenu
->AppendSeparator();
222 splitMenu
->Append(SPLIT_QUIT
, _T("E&xit\tAlt-X"), _T("Exit"));
224 wxMenuBar
*menuBar
= new wxMenuBar
;
225 menuBar
->Append(splitMenu
, _T("&Splitter"));
229 menuBar
->Check(SPLIT_LIVE
, true);
230 m_splitter
= new MySplitterWindow(this);
232 m_splitter
->SetSashGravity(1.0);
235 m_left
= new MyCanvas(m_splitter
, true);
236 m_left
->SetBackgroundColour(*wxRED
);
237 m_left
->SetScrollbars(20, 20, 5, 5);
238 m_left
->SetCursor(wxCursor(wxCURSOR_MAGNIFIER
));
240 m_right
= new MyCanvas(m_splitter
, false);
241 m_right
->SetBackgroundColour(*wxCYAN
);
242 m_right
->SetScrollbars(20, 20, 5, 5);
243 #else // for testing kbd navigation inside the splitter
244 m_left
= new wxTextCtrl(m_splitter
, wxID_ANY
, _T("first text"));
245 m_right
= new wxTextCtrl(m_splitter
, wxID_ANY
, _T("second text"));
248 // you can also do this to start with a single window
250 m_right
->Show(false);
251 m_splitter
->Initialize(m_left
);
253 // you can also try -100
254 m_splitter
->SplitVertically(m_left
, m_right
, 100);
258 SetStatusText(_T("Min pane size = 0"), 1);
259 #endif // wxUSE_STATUSBAR
261 m_replacewindow
= (wxWindow
*)0;
266 if (m_replacewindow
) {
267 m_replacewindow
->Destroy();
268 m_replacewindow
= (wxWindow
*)0;
272 // menu command handlers
274 void MyFrame::Quit(wxCommandEvent
& WXUNUSED(event
) )
279 void MyFrame::SplitHorizontal(wxCommandEvent
& WXUNUSED(event
) )
281 if ( m_splitter
->IsSplit() )
282 m_splitter
->Unsplit();
285 m_splitter
->SplitHorizontally( m_left
, m_right
);
288 SetStatusText(_T("Splitter split horizontally"), 1);
289 #endif // wxUSE_STATUSBAR
292 void MyFrame::SplitVertical(wxCommandEvent
& WXUNUSED(event
) )
294 if ( m_splitter
->IsSplit() )
295 m_splitter
->Unsplit();
298 m_splitter
->SplitVertically( m_left
, m_right
);
301 SetStatusText(_T("Splitter split vertically"), 1);
302 #endif // wxUSE_STATUSBAR
305 void MyFrame::Unsplit(wxCommandEvent
& WXUNUSED(event
) )
307 if ( m_splitter
->IsSplit() )
308 m_splitter
->Unsplit();
310 SetStatusText(_T("No splitter"));
311 #endif // wxUSE_STATUSBAR
314 void MyFrame::ToggleLive(wxCommandEvent
& event
)
316 long style
= m_splitter
->GetWindowStyleFlag();
317 if ( event
.IsChecked() )
318 style
|= wxSP_LIVE_UPDATE
;
320 style
&= ~wxSP_LIVE_UPDATE
;
322 m_splitter
->SetWindowStyleFlag(style
);
325 void MyFrame::SetPosition(wxCommandEvent
& WXUNUSED(event
) )
328 str
.Printf( wxT("%d"), m_splitter
->GetSashPosition());
329 str
= wxGetTextFromUser(_T("Enter splitter position:"), _T(""), str
, this);
334 if ( !str
.ToLong(&pos
) )
336 wxLogError(_T("The splitter position should be an integer."));
340 m_splitter
->SetSashPosition(pos
);
342 wxLogStatus(this, _T("Splitter position set to %ld"), pos
);
345 void MyFrame::SetMinSize(wxCommandEvent
& WXUNUSED(event
) )
348 str
.Printf( wxT("%d"), m_splitter
->GetMinimumPaneSize());
349 str
= wxGetTextFromUser(_T("Enter minimal size for panes:"), _T(""), str
, this);
353 int minsize
= wxStrtol( str
, (wxChar
**)NULL
, 10 );
354 m_splitter
->SetMinimumPaneSize(minsize
);
356 str
.Printf( wxT("Min pane size = %d"), minsize
);
357 SetStatusText(str
, 1);
358 #endif // wxUSE_STATUSBAR
360 void MyFrame::SetGravity(wxCommandEvent
& WXUNUSED(event
) )
363 str
.Printf( wxT("%g"), m_splitter
->GetSashGravity());
364 str
= wxGetTextFromUser(_T("Enter sash gravity (0,1):"), _T(""), str
, this);
368 double gravity
= wxStrtod( str
, (wxChar
**)NULL
);
369 m_splitter
->SetSashGravity(gravity
);
371 str
.Printf( wxT("Gravity = %g"), gravity
);
372 SetStatusText(str
, 1);
373 #endif // wxUSE_STATUSBAR
376 void MyFrame::Replace(wxCommandEvent
& WXUNUSED(event
) )
378 if (m_replacewindow
== 0) {
379 m_replacewindow
= m_splitter
->GetWindow2();
380 m_splitter
->ReplaceWindow(m_replacewindow
, new wxPanel(m_splitter
, wxID_ANY
));
381 m_replacewindow
->Hide();
383 wxWindow
*empty
= m_splitter
->GetWindow2();
384 m_splitter
->ReplaceWindow(empty
, m_replacewindow
);
385 m_replacewindow
->Show();
391 // Update UI handlers
393 void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent
& event
)
395 event
.Enable( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_HORIZONTAL
) );
398 void MyFrame::UpdateUIVertical(wxUpdateUIEvent
& event
)
400 event
.Enable( ( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_VERTICAL
) ) );
403 void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent
& event
)
405 event
.Enable( m_splitter
->IsSplit() );
408 // ----------------------------------------------------------------------------
410 // ----------------------------------------------------------------------------
412 BEGIN_EVENT_TABLE(MySplitterWindow
, wxSplitterWindow
)
413 EVT_SPLITTER_SASH_POS_CHANGED(wxID_ANY
, MySplitterWindow::OnPositionChanged
)
414 EVT_SPLITTER_SASH_POS_CHANGING(wxID_ANY
, MySplitterWindow::OnPositionChanging
)
416 EVT_SPLITTER_DCLICK(wxID_ANY
, MySplitterWindow::OnDClick
)
418 EVT_SPLITTER_UNSPLIT(wxID_ANY
, MySplitterWindow::OnUnsplitEvent
)
421 MySplitterWindow::MySplitterWindow(wxFrame
*parent
)
422 : wxSplitterWindow(parent
, wxID_ANY
,
423 wxDefaultPosition
, wxDefaultSize
,
424 wxSP_3D
| wxSP_LIVE_UPDATE
|
425 wxCLIP_CHILDREN
/* | wxSP_NO_XP_THEME */ )
430 void MySplitterWindow::OnPositionChanged(wxSplitterEvent
& event
)
432 wxLogStatus(m_frame
, _T("Position has changed, now = %d (or %d)"),
433 event
.GetSashPosition(), GetSashPosition());
438 void MySplitterWindow::OnPositionChanging(wxSplitterEvent
& event
)
440 wxLogStatus(m_frame
, _T("Position is changing, now = %d (or %d)"),
441 event
.GetSashPosition(), GetSashPosition());
446 void MySplitterWindow::OnDClick(wxSplitterEvent
& event
)
449 m_frame
->SetStatusText(_T("Splitter double clicked"), 1);
450 #endif // wxUSE_STATUSBAR
455 void MySplitterWindow::OnUnsplitEvent(wxSplitterEvent
& event
)
458 m_frame
->SetStatusText(_T("Splitter unsplit"), 1);
459 #endif // wxUSE_STATUSBAR
464 // ----------------------------------------------------------------------------
466 // ----------------------------------------------------------------------------
468 MyCanvas::MyCanvas(wxWindow
* parent
, bool mirror
)
469 : wxScrolledWindow(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
470 wxHSCROLL
| wxVSCROLL
| wxNO_FULL_REPAINT_ON_RESIZE
)
475 void MyCanvas::OnDraw(wxDC
& dcOrig
)
477 wxMirrorDC
dc(dcOrig
, m_mirror
);
479 dc
.SetPen(*wxBLACK_PEN
);
480 dc
.DrawLine(0, 0, 100, 200);
482 dc
.SetBackgroundMode(wxTRANSPARENT
);
483 dc
.DrawText(_T("Testing"), 50, 50);
485 dc
.SetPen(*wxRED_PEN
);
486 dc
.SetBrush(*wxGREEN_BRUSH
);
487 dc
.DrawRectangle(120, 120, 100, 80);