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"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // ID for the menu commands
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 class MyApp
: public wxApp
68 class MyFrame
: public wxFrame
75 void SplitHorizontal(wxCommandEvent
& event
);
76 void SplitVertical(wxCommandEvent
& event
);
77 void Unsplit(wxCommandEvent
& event
);
78 void ToggleLive(wxCommandEvent
& event
);
79 void SetPosition(wxCommandEvent
& event
);
80 void SetMinSize(wxCommandEvent
& event
);
82 void Quit(wxCommandEvent
& event
);
84 // Menu command update functions
85 void UpdateUIHorizontal(wxUpdateUIEvent
& event
);
86 void UpdateUIVertical(wxUpdateUIEvent
& event
);
87 void UpdateUIUnsplit(wxUpdateUIEvent
& event
);
90 wxScrolledWindow
*m_left
, *m_right
;
92 wxSplitterWindow
* m_splitter
;
97 class MySplitterWindow
: public wxSplitterWindow
100 MySplitterWindow(wxFrame
*parent
);
103 void OnPositionChanged(wxSplitterEvent
& event
);
104 void OnPositionChanging(wxSplitterEvent
& event
);
105 void OnDClick(wxSplitterEvent
& event
);
106 void OnUnsplit(wxSplitterEvent
& event
);
111 DECLARE_EVENT_TABLE()
114 class MyCanvas
: public wxScrolledWindow
117 MyCanvas(wxWindow
* parent
);
120 virtual void OnDraw(wxDC
& dc
);
123 // ============================================================================
125 // ============================================================================
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
135 // create and show the main frame
136 MyFrame
* frame
= new MyFrame
;
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
148 EVT_MENU(SPLIT_VERTICAL
, MyFrame::SplitVertical
)
149 EVT_MENU(SPLIT_HORIZONTAL
, MyFrame::SplitHorizontal
)
150 EVT_MENU(SPLIT_UNSPLIT
, MyFrame::Unsplit
)
151 EVT_MENU(SPLIT_LIVE
, MyFrame::ToggleLive
)
152 EVT_MENU(SPLIT_SETPOSITION
, MyFrame::SetPosition
)
153 EVT_MENU(SPLIT_SETMINSIZE
, MyFrame::SetMinSize
)
155 EVT_MENU(SPLIT_QUIT
, MyFrame::Quit
)
157 EVT_UPDATE_UI(SPLIT_VERTICAL
, MyFrame::UpdateUIVertical
)
158 EVT_UPDATE_UI(SPLIT_HORIZONTAL
, MyFrame::UpdateUIHorizontal
)
159 EVT_UPDATE_UI(SPLIT_UNSPLIT
, MyFrame::UpdateUIUnsplit
)
162 // My frame constructor
164 : wxFrame(NULL
, -1, _T("wxSplitterWindow sample"),
165 wxDefaultPosition
, wxSize(420, 300),
166 wxDEFAULT_FRAME_STYLE
| wxNO_FULL_REPAINT_ON_RESIZE
)
171 wxMenu
*splitMenu
= new wxMenu
;
172 splitMenu
->Append(SPLIT_VERTICAL
,
173 _T("Split &Vertically\tCtrl-V"),
174 _T("Split vertically"));
175 splitMenu
->Append(SPLIT_HORIZONTAL
,
176 _T("Split &Horizontally\tCtrl-H"),
177 _T("Split horizontally"));
178 splitMenu
->Append(SPLIT_UNSPLIT
,
179 _T("&Unsplit\tCtrl-U"),
181 splitMenu
->AppendSeparator();
183 splitMenu
->AppendCheckItem(SPLIT_LIVE
,
184 _T("&Live update\tCtrl-L"),
185 _T("Toggle live update mode"));
186 splitMenu
->Append(SPLIT_SETPOSITION
,
187 _T("Set splitter &position\tCtrl-P"),
188 _T("Set the splitter position"));
189 splitMenu
->Append(SPLIT_SETMINSIZE
,
190 _T("Set &min size\tCtrl-M"),
191 _T("Set minimum pane size"));
192 splitMenu
->AppendSeparator();
194 splitMenu
->Append(SPLIT_QUIT
, _T("E&xit\tAlt-X"), _T("Exit"));
196 wxMenuBar
*menuBar
= new wxMenuBar
;
197 menuBar
->Append(splitMenu
, _T("&Splitter"));
201 menuBar
->Check(SPLIT_LIVE
, TRUE
);
202 m_splitter
= new MySplitterWindow(this);
205 m_left
= new MyCanvas(m_splitter
);
206 m_left
->SetBackgroundColour(*wxRED
);
207 m_left
->SetScrollbars(20, 20, 50, 50);
208 m_left
->SetCursor(wxCursor(wxCURSOR_MAGNIFIER
));
210 m_right
= new MyCanvas(m_splitter
);
211 m_right
->SetBackgroundColour(*wxCYAN
);
212 m_right
->SetScrollbars(20, 20, 50, 50);
213 #else // for testing kbd navigation inside the splitter
214 m_left
= new wxTextCtrl(m_splitter
, -1, _T("first text"));
215 m_right
= new wxTextCtrl(m_splitter
, -1, _T("second text"));
218 // you can also do this to start with a single window
220 m_right
->Show(FALSE
);
221 m_splitter
->Initialize(m_left
);
223 // you can also try -100
224 m_splitter
->SplitVertically(m_left
, m_right
, 100);
227 SetStatusText(_T("Min pane size = 0"), 1);
234 // menu command handlers
236 void MyFrame::Quit(wxCommandEvent
& WXUNUSED(event
) )
241 void MyFrame::SplitHorizontal(wxCommandEvent
& WXUNUSED(event
) )
243 if ( m_splitter
->IsSplit() )
244 m_splitter
->Unsplit();
247 m_splitter
->SplitHorizontally( m_left
, m_right
);
249 SetStatusText(_T("Splitter split horizontally"), 1);
252 void MyFrame::SplitVertical(wxCommandEvent
& WXUNUSED(event
) )
254 if ( m_splitter
->IsSplit() )
255 m_splitter
->Unsplit();
258 m_splitter
->SplitVertically( m_left
, m_right
);
260 SetStatusText(_T("Splitter split vertically"), 1);
263 void MyFrame::Unsplit(wxCommandEvent
& WXUNUSED(event
) )
265 if ( m_splitter
->IsSplit() )
266 m_splitter
->Unsplit();
267 SetStatusText(_T("No splitter"));
270 void MyFrame::ToggleLive(wxCommandEvent
& event
)
272 long style
= m_splitter
->GetWindowStyleFlag();
273 if ( event
.IsChecked() )
274 style
|= wxSP_LIVE_UPDATE
;
276 style
&= ~wxSP_LIVE_UPDATE
;
278 m_splitter
->SetWindowStyleFlag(style
);
281 void MyFrame::SetPosition(wxCommandEvent
& WXUNUSED(event
) )
284 str
.Printf( wxT("%d"), m_splitter
->GetSashPosition());
285 str
= wxGetTextFromUser(_T("Enter splitter position:"), _T(""), str
, this);
290 if ( !str
.ToLong(&pos
) )
292 wxLogError(_T("The splitter position should be an integer."));
296 m_splitter
->SetSashPosition(pos
);
298 wxLogStatus(this, _T("Splitter position set to %ld"), pos
);
301 void MyFrame::SetMinSize(wxCommandEvent
& WXUNUSED(event
) )
304 str
.Printf( wxT("%d"), m_splitter
->GetMinimumPaneSize());
305 str
= wxGetTextFromUser(_T("Enter minimal size for panes:"), _T(""), str
, this);
309 int minsize
= wxStrtol( str
, (wxChar
**)NULL
, 10 );
310 m_splitter
->SetMinimumPaneSize(minsize
);
311 str
.Printf( wxT("Min pane size = %d"), minsize
);
312 SetStatusText(str
, 1);
315 // Update UI handlers
317 void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent
& event
)
319 event
.Enable( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_HORIZONTAL
) );
322 void MyFrame::UpdateUIVertical(wxUpdateUIEvent
& event
)
324 event
.Enable( ( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_VERTICAL
) ) );
327 void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent
& event
)
329 event
.Enable( m_splitter
->IsSplit() );
332 // ----------------------------------------------------------------------------
334 // ----------------------------------------------------------------------------
336 BEGIN_EVENT_TABLE(MySplitterWindow
, wxSplitterWindow
)
337 EVT_SPLITTER_SASH_POS_CHANGED(-1, MySplitterWindow::OnPositionChanged
)
338 EVT_SPLITTER_SASH_POS_CHANGING(-1, MySplitterWindow::OnPositionChanging
)
340 EVT_SPLITTER_DCLICK(-1, MySplitterWindow::OnDClick
)
342 EVT_SPLITTER_UNSPLIT(-1, MySplitterWindow::OnUnsplit
)
345 MySplitterWindow::MySplitterWindow(wxFrame
*parent
)
346 : wxSplitterWindow(parent
, -1,
347 wxDefaultPosition
, wxDefaultSize
,
348 wxSP_3D
| wxSP_LIVE_UPDATE
| wxCLIP_CHILDREN
)
353 void MySplitterWindow::OnPositionChanged(wxSplitterEvent
& event
)
355 wxLogStatus(m_frame
, _T("Position has changed, now = %d (or %d)"),
356 event
.GetSashPosition(), GetSashPosition());
361 void MySplitterWindow::OnPositionChanging(wxSplitterEvent
& event
)
363 wxLogStatus(m_frame
, _T("Position is changing, now = %d (or %d)"),
364 event
.GetSashPosition(), GetSashPosition());
369 void MySplitterWindow::OnDClick(wxSplitterEvent
& event
)
371 m_frame
->SetStatusText(_T("Splitter double clicked"), 1);
376 void MySplitterWindow::OnUnsplit(wxSplitterEvent
& event
)
378 m_frame
->SetStatusText(_T("Splitter unsplit"), 1);
383 // ----------------------------------------------------------------------------
385 // ----------------------------------------------------------------------------
387 MyCanvas::MyCanvas(wxWindow
* parent
)
388 : wxScrolledWindow(parent
, -1)
392 MyCanvas::~MyCanvas()
396 void MyCanvas::OnDraw(wxDC
& dc
)
398 dc
.SetPen(*wxBLACK_PEN
);
399 dc
.DrawLine(0, 0, 100, 100);
401 dc
.SetBackgroundMode(wxTRANSPARENT
);
402 dc
.DrawText(_T("Testing"), 50, 50);
404 dc
.SetPen(*wxRED_PEN
);
405 dc
.SetBrush(*wxGREEN_BRUSH
);
406 dc
.DrawRectangle(120, 120, 100, 80);