1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSplitterWindow sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
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"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 // ID for the menu commands
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 class MyApp
: public wxApp
67 class MyFrame
: public wxFrame
74 void SplitHorizontal(wxCommandEvent
& event
);
75 void SplitVertical(wxCommandEvent
& event
);
76 void Unsplit(wxCommandEvent
& event
);
77 void ToggleLive(wxCommandEvent
& event
);
78 void SetPosition(wxCommandEvent
& event
);
79 void SetMinSize(wxCommandEvent
& event
);
81 void Quit(wxCommandEvent
& event
);
83 // Menu command update functions
84 void UpdateUIHorizontal(wxUpdateUIEvent
& event
);
85 void UpdateUIVertical(wxUpdateUIEvent
& event
);
86 void UpdateUIUnsplit(wxUpdateUIEvent
& event
);
89 wxScrolledWindow
*m_left
, *m_right
;
91 wxSplitterWindow
* m_splitter
;
96 class MySplitterWindow
: public wxSplitterWindow
99 MySplitterWindow(wxFrame
*parent
);
102 void OnPositionChanged(wxSplitterEvent
& event
);
103 void OnPositionChanging(wxSplitterEvent
& event
);
104 void OnDClick(wxSplitterEvent
& event
);
105 void OnUnsplit(wxSplitterEvent
& event
);
110 DECLARE_EVENT_TABLE()
113 class MyCanvas
: public wxScrolledWindow
116 MyCanvas(wxWindow
* parent
);
119 virtual void OnDraw(wxDC
& dc
);
122 // ============================================================================
124 // ============================================================================
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
134 // create and show the main frame
135 MyFrame
* frame
= new MyFrame
;
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
147 EVT_MENU(SPLIT_VERTICAL
, MyFrame::SplitVertical
)
148 EVT_MENU(SPLIT_HORIZONTAL
, MyFrame::SplitHorizontal
)
149 EVT_MENU(SPLIT_UNSPLIT
, MyFrame::Unsplit
)
150 EVT_MENU(SPLIT_LIVE
, MyFrame::ToggleLive
)
151 EVT_MENU(SPLIT_SETPOSITION
, MyFrame::SetPosition
)
152 EVT_MENU(SPLIT_SETMINSIZE
, MyFrame::SetMinSize
)
154 EVT_MENU(SPLIT_QUIT
, MyFrame::Quit
)
156 EVT_UPDATE_UI(SPLIT_VERTICAL
, MyFrame::UpdateUIVertical
)
157 EVT_UPDATE_UI(SPLIT_HORIZONTAL
, MyFrame::UpdateUIHorizontal
)
158 EVT_UPDATE_UI(SPLIT_UNSPLIT
, MyFrame::UpdateUIUnsplit
)
161 // My frame constructor
163 : wxFrame(NULL
, -1, _T("wxSplitterWindow sample"),
164 wxDefaultPosition
, wxSize(420, 300),
165 wxDEFAULT_FRAME_STYLE
| wxNO_FULL_REPAINT_ON_RESIZE
)
170 wxMenu
*splitMenu
= new wxMenu
;
171 splitMenu
->Append(SPLIT_VERTICAL
,
172 _T("Split &Vertically\tCtrl-V"),
173 _T("Split vertically"));
174 splitMenu
->Append(SPLIT_HORIZONTAL
,
175 _T("Split &Horizontally\tCtrl-H"),
176 _T("Split horizontally"));
177 splitMenu
->Append(SPLIT_UNSPLIT
,
178 _T("&Unsplit\tCtrl-U"),
180 splitMenu
->AppendSeparator();
182 splitMenu
->AppendCheckItem(SPLIT_LIVE
,
183 _T("&Live update\tCtrl-L"),
184 _T("Toggle live update mode"));
185 splitMenu
->Append(SPLIT_SETPOSITION
,
186 _T("Set splitter &position\tCtrl-P"),
187 _T("Set the splitter position"));
188 splitMenu
->Append(SPLIT_SETMINSIZE
,
189 _T("Set &min size\tCtrl-M"),
190 _T("Set minimum pane size"));
191 splitMenu
->AppendSeparator();
193 splitMenu
->Append(SPLIT_QUIT
, _T("E&xit\tAlt-X"), _T("Exit"));
195 wxMenuBar
*menuBar
= new wxMenuBar
;
196 menuBar
->Append(splitMenu
, _T("&Splitter"));
200 menuBar
->Check(SPLIT_LIVE
, TRUE
);
201 m_splitter
= new MySplitterWindow(this);
204 m_left
= new MyCanvas(m_splitter
);
205 m_left
->SetBackgroundColour(*wxRED
);
206 m_left
->SetScrollbars(20, 20, 50, 50);
207 m_left
->SetCursor(wxCursor(wxCURSOR_MAGNIFIER
));
209 m_right
= new MyCanvas(m_splitter
);
210 m_right
->SetBackgroundColour(*wxCYAN
);
211 m_right
->SetScrollbars(20, 20, 50, 50);
212 #else // for testing kbd navigation inside the splitter
213 m_left
= new wxTextCtrl(m_splitter
, -1, _T("first text"));
214 m_right
= new wxTextCtrl(m_splitter
, -1, _T("second text"));
217 // you can also do this to start with a single window
219 m_right
->Show(FALSE
);
220 m_splitter
->Initialize(m_left
);
222 // you can also try -100
223 m_splitter
->SplitVertically(m_left
, m_right
, 100);
226 SetStatusText(_T("Min pane size = 0"), 1);
233 // menu command handlers
235 void MyFrame::Quit(wxCommandEvent
& WXUNUSED(event
) )
240 void MyFrame::SplitHorizontal(wxCommandEvent
& WXUNUSED(event
) )
242 if ( m_splitter
->IsSplit() )
243 m_splitter
->Unsplit();
246 m_splitter
->SplitHorizontally( m_left
, m_right
);
248 SetStatusText(_T("Splitter split horizontally"), 1);
251 void MyFrame::SplitVertical(wxCommandEvent
& WXUNUSED(event
) )
253 if ( m_splitter
->IsSplit() )
254 m_splitter
->Unsplit();
257 m_splitter
->SplitVertically( m_left
, m_right
);
259 SetStatusText(_T("Splitter split vertically"), 1);
262 void MyFrame::Unsplit(wxCommandEvent
& WXUNUSED(event
) )
264 if ( m_splitter
->IsSplit() )
265 m_splitter
->Unsplit();
266 SetStatusText(_T("No splitter"));
269 void MyFrame::ToggleLive(wxCommandEvent
& event
)
271 long style
= m_splitter
->GetWindowStyleFlag();
272 if ( event
.IsChecked() )
273 style
|= wxSP_LIVE_UPDATE
;
275 style
&= ~wxSP_LIVE_UPDATE
;
277 m_splitter
->SetWindowStyleFlag(style
);
280 void MyFrame::SetPosition(wxCommandEvent
& WXUNUSED(event
) )
283 str
.Printf( wxT("%d"), m_splitter
->GetSashPosition());
284 str
= wxGetTextFromUser(_T("Enter splitter position:"), _T(""), str
, this);
289 if ( !str
.ToLong(&pos
) )
291 wxLogError(_T("The splitter position should be an integer."));
295 m_splitter
->SetSashPosition(pos
);
297 wxLogStatus(this, _T("Splitter position set to %ld"), pos
);
300 void MyFrame::SetMinSize(wxCommandEvent
& WXUNUSED(event
) )
303 str
.Printf( wxT("%d"), m_splitter
->GetMinimumPaneSize());
304 str
= wxGetTextFromUser(_T("Enter minimal size for panes:"), _T(""), str
, this);
308 int minsize
= wxStrtol( str
, (wxChar
**)NULL
, 10 );
309 m_splitter
->SetMinimumPaneSize(minsize
);
310 str
.Printf( wxT("Min pane size = %d"), minsize
);
311 SetStatusText(str
, 1);
314 // Update UI handlers
316 void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent
& event
)
318 event
.Enable( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_HORIZONTAL
) );
321 void MyFrame::UpdateUIVertical(wxUpdateUIEvent
& event
)
323 event
.Enable( ( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_VERTICAL
) ) );
326 void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent
& event
)
328 event
.Enable( m_splitter
->IsSplit() );
331 // ----------------------------------------------------------------------------
333 // ----------------------------------------------------------------------------
335 BEGIN_EVENT_TABLE(MySplitterWindow
, wxSplitterWindow
)
336 EVT_SPLITTER_SASH_POS_CHANGED(-1, MySplitterWindow::OnPositionChanged
)
337 EVT_SPLITTER_SASH_POS_CHANGING(-1, MySplitterWindow::OnPositionChanging
)
339 EVT_SPLITTER_DCLICK(-1, MySplitterWindow::OnDClick
)
341 EVT_SPLITTER_UNSPLIT(-1, MySplitterWindow::OnUnsplit
)
344 MySplitterWindow::MySplitterWindow(wxFrame
*parent
)
345 : wxSplitterWindow(parent
, -1,
346 wxDefaultPosition
, wxDefaultSize
,
347 wxSP_3D
| wxSP_LIVE_UPDATE
| wxCLIP_CHILDREN
)
352 void MySplitterWindow::OnPositionChanged(wxSplitterEvent
& event
)
354 wxLogStatus(m_frame
, _T("Position has changed, now = %d (or %d)"),
355 event
.GetSashPosition(), GetSashPosition());
360 void MySplitterWindow::OnPositionChanging(wxSplitterEvent
& event
)
362 wxLogStatus(m_frame
, _T("Position is changing, now = %d (or %d)"),
363 event
.GetSashPosition(), GetSashPosition());
368 void MySplitterWindow::OnDClick(wxSplitterEvent
& event
)
370 m_frame
->SetStatusText(_T("Splitter double clicked"), 1);
375 void MySplitterWindow::OnUnsplit(wxSplitterEvent
& event
)
377 m_frame
->SetStatusText(_T("Splitter unsplit"), 1);
382 // ----------------------------------------------------------------------------
384 // ----------------------------------------------------------------------------
386 MyCanvas::MyCanvas(wxWindow
* parent
)
387 : wxScrolledWindow(parent
, -1)
391 MyCanvas::~MyCanvas()
395 void MyCanvas::OnDraw(wxDC
& dc
)
397 dc
.SetPen(*wxBLACK_PEN
);
398 dc
.DrawLine(0, 0, 100, 100);
400 dc
.SetBackgroundMode(wxTRANSPARENT
);
401 dc
.DrawText(_T("Testing"), 50, 50);
403 dc
.SetPen(*wxRED_PEN
);
404 dc
.SetBrush(*wxGREEN_BRUSH
);
405 dc
.DrawRectangle(120, 120, 100, 80);