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
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 class MyApp
: public wxApp
67 virtual bool OnInit();
69 DECLARE_NO_COPY_CLASS(MyApp
)
72 class MyFrame
: public wxFrame
79 void SplitHorizontal(wxCommandEvent
& event
);
80 void SplitVertical(wxCommandEvent
& event
);
81 void Unsplit(wxCommandEvent
& event
);
82 void ToggleLive(wxCommandEvent
& event
);
83 void SetPosition(wxCommandEvent
& event
);
84 void SetMinSize(wxCommandEvent
& event
);
86 void Quit(wxCommandEvent
& event
);
88 // Menu command update functions
89 void UpdateUIHorizontal(wxUpdateUIEvent
& event
);
90 void UpdateUIVertical(wxUpdateUIEvent
& event
);
91 void UpdateUIUnsplit(wxUpdateUIEvent
& event
);
94 wxScrolledWindow
*m_left
, *m_right
;
96 wxSplitterWindow
* m_splitter
;
99 DECLARE_NO_COPY_CLASS(MyFrame
)
102 class MySplitterWindow
: public wxSplitterWindow
105 MySplitterWindow(wxFrame
*parent
);
108 void OnPositionChanged(wxSplitterEvent
& event
);
109 void OnPositionChanging(wxSplitterEvent
& event
);
110 void OnDClick(wxSplitterEvent
& event
);
111 void OnUnsplit(wxSplitterEvent
& event
);
116 DECLARE_EVENT_TABLE()
117 DECLARE_NO_COPY_CLASS(MySplitterWindow
)
120 class MyCanvas
: public wxScrolledWindow
123 MyCanvas(wxWindow
* parent
, bool mirror
);
126 virtual void OnDraw(wxDC
& dc
);
131 DECLARE_NO_COPY_CLASS(MyCanvas
)
134 // ============================================================================
136 // ============================================================================
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
146 // create and show the main frame
147 MyFrame
* frame
= new MyFrame
;
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
158 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
159 EVT_MENU(SPLIT_VERTICAL
, MyFrame::SplitVertical
)
160 EVT_MENU(SPLIT_HORIZONTAL
, MyFrame::SplitHorizontal
)
161 EVT_MENU(SPLIT_UNSPLIT
, MyFrame::Unsplit
)
162 EVT_MENU(SPLIT_LIVE
, MyFrame::ToggleLive
)
163 EVT_MENU(SPLIT_SETPOSITION
, MyFrame::SetPosition
)
164 EVT_MENU(SPLIT_SETMINSIZE
, MyFrame::SetMinSize
)
166 EVT_MENU(SPLIT_QUIT
, MyFrame::Quit
)
168 EVT_UPDATE_UI(SPLIT_VERTICAL
, MyFrame::UpdateUIVertical
)
169 EVT_UPDATE_UI(SPLIT_HORIZONTAL
, MyFrame::UpdateUIHorizontal
)
170 EVT_UPDATE_UI(SPLIT_UNSPLIT
, MyFrame::UpdateUIUnsplit
)
173 // My frame constructor
175 : wxFrame(NULL
, -1, _T("wxSplitterWindow sample"),
176 wxDefaultPosition
, wxSize(420, 300),
177 wxDEFAULT_FRAME_STYLE
| wxNO_FULL_REPAINT_ON_RESIZE
)
182 wxMenu
*splitMenu
= new wxMenu
;
183 splitMenu
->Append(SPLIT_VERTICAL
,
184 _T("Split &Vertically\tCtrl-V"),
185 _T("Split vertically"));
186 splitMenu
->Append(SPLIT_HORIZONTAL
,
187 _T("Split &Horizontally\tCtrl-H"),
188 _T("Split horizontally"));
189 splitMenu
->Append(SPLIT_UNSPLIT
,
190 _T("&Unsplit\tCtrl-U"),
192 splitMenu
->AppendSeparator();
194 splitMenu
->AppendCheckItem(SPLIT_LIVE
,
195 _T("&Live update\tCtrl-L"),
196 _T("Toggle live update mode"));
197 splitMenu
->Append(SPLIT_SETPOSITION
,
198 _T("Set splitter &position\tCtrl-P"),
199 _T("Set the splitter position"));
200 splitMenu
->Append(SPLIT_SETMINSIZE
,
201 _T("Set &min size\tCtrl-M"),
202 _T("Set minimum pane size"));
203 splitMenu
->AppendSeparator();
205 splitMenu
->Append(SPLIT_QUIT
, _T("E&xit\tAlt-X"), _T("Exit"));
207 wxMenuBar
*menuBar
= new wxMenuBar
;
208 menuBar
->Append(splitMenu
, _T("&Splitter"));
212 menuBar
->Check(SPLIT_LIVE
, TRUE
);
213 m_splitter
= new MySplitterWindow(this);
216 m_left
= new MyCanvas(m_splitter
, true);
217 m_left
->SetBackgroundColour(*wxRED
);
218 m_left
->SetScrollbars(20, 20, 5, 5);
219 m_left
->SetCursor(wxCursor(wxCURSOR_MAGNIFIER
));
221 m_right
= new MyCanvas(m_splitter
, false);
222 m_right
->SetBackgroundColour(*wxCYAN
);
223 m_right
->SetScrollbars(20, 20, 5, 5);
224 #else // for testing kbd navigation inside the splitter
225 m_left
= new wxTextCtrl(m_splitter
, -1, _T("first text"));
226 m_right
= new wxTextCtrl(m_splitter
, -1, _T("second text"));
229 // you can also do this to start with a single window
231 m_right
->Show(FALSE
);
232 m_splitter
->Initialize(m_left
);
234 // you can also try -100
235 m_splitter
->SplitVertically(m_left
, m_right
, 100);
238 SetStatusText(_T("Min pane size = 0"), 1);
245 // menu command handlers
247 void MyFrame::Quit(wxCommandEvent
& WXUNUSED(event
) )
252 void MyFrame::SplitHorizontal(wxCommandEvent
& WXUNUSED(event
) )
254 if ( m_splitter
->IsSplit() )
255 m_splitter
->Unsplit();
258 m_splitter
->SplitHorizontally( m_left
, m_right
);
260 SetStatusText(_T("Splitter split horizontally"), 1);
263 void MyFrame::SplitVertical(wxCommandEvent
& WXUNUSED(event
) )
265 if ( m_splitter
->IsSplit() )
266 m_splitter
->Unsplit();
269 m_splitter
->SplitVertically( m_left
, m_right
);
271 SetStatusText(_T("Splitter split vertically"), 1);
274 void MyFrame::Unsplit(wxCommandEvent
& WXUNUSED(event
) )
276 if ( m_splitter
->IsSplit() )
277 m_splitter
->Unsplit();
278 SetStatusText(_T("No splitter"));
281 void MyFrame::ToggleLive(wxCommandEvent
& event
)
283 long style
= m_splitter
->GetWindowStyleFlag();
284 if ( event
.IsChecked() )
285 style
|= wxSP_LIVE_UPDATE
;
287 style
&= ~wxSP_LIVE_UPDATE
;
289 m_splitter
->SetWindowStyleFlag(style
);
292 void MyFrame::SetPosition(wxCommandEvent
& WXUNUSED(event
) )
295 str
.Printf( wxT("%d"), m_splitter
->GetSashPosition());
296 str
= wxGetTextFromUser(_T("Enter splitter position:"), _T(""), str
, this);
301 if ( !str
.ToLong(&pos
) )
303 wxLogError(_T("The splitter position should be an integer."));
307 m_splitter
->SetSashPosition(pos
);
309 wxLogStatus(this, _T("Splitter position set to %ld"), pos
);
312 void MyFrame::SetMinSize(wxCommandEvent
& WXUNUSED(event
) )
315 str
.Printf( wxT("%d"), m_splitter
->GetMinimumPaneSize());
316 str
= wxGetTextFromUser(_T("Enter minimal size for panes:"), _T(""), str
, this);
320 int minsize
= wxStrtol( str
, (wxChar
**)NULL
, 10 );
321 m_splitter
->SetMinimumPaneSize(minsize
);
322 str
.Printf( wxT("Min pane size = %d"), minsize
);
323 SetStatusText(str
, 1);
326 // Update UI handlers
328 void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent
& event
)
330 event
.Enable( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_HORIZONTAL
) );
333 void MyFrame::UpdateUIVertical(wxUpdateUIEvent
& event
)
335 event
.Enable( ( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_VERTICAL
) ) );
338 void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent
& event
)
340 event
.Enable( m_splitter
->IsSplit() );
343 // ----------------------------------------------------------------------------
345 // ----------------------------------------------------------------------------
347 BEGIN_EVENT_TABLE(MySplitterWindow
, wxSplitterWindow
)
348 EVT_SPLITTER_SASH_POS_CHANGED(-1, MySplitterWindow::OnPositionChanged
)
349 EVT_SPLITTER_SASH_POS_CHANGING(-1, MySplitterWindow::OnPositionChanging
)
351 EVT_SPLITTER_DCLICK(-1, MySplitterWindow::OnDClick
)
353 EVT_SPLITTER_UNSPLIT(-1, MySplitterWindow::OnUnsplit
)
356 MySplitterWindow::MySplitterWindow(wxFrame
*parent
)
357 : wxSplitterWindow(parent
, -1,
358 wxDefaultPosition
, wxDefaultSize
,
359 0x700| wxSP_LIVE_UPDATE
| wxCLIP_CHILDREN
)
364 void MySplitterWindow::OnPositionChanged(wxSplitterEvent
& event
)
366 wxLogStatus(m_frame
, _T("Position has changed, now = %d (or %d)"),
367 event
.GetSashPosition(), GetSashPosition());
372 void MySplitterWindow::OnPositionChanging(wxSplitterEvent
& event
)
374 wxLogStatus(m_frame
, _T("Position is changing, now = %d (or %d)"),
375 event
.GetSashPosition(), GetSashPosition());
380 void MySplitterWindow::OnDClick(wxSplitterEvent
& event
)
382 m_frame
->SetStatusText(_T("Splitter double clicked"), 1);
387 void MySplitterWindow::OnUnsplit(wxSplitterEvent
& event
)
389 m_frame
->SetStatusText(_T("Splitter unsplit"), 1);
394 // ----------------------------------------------------------------------------
396 // ----------------------------------------------------------------------------
398 MyCanvas::MyCanvas(wxWindow
* parent
, bool mirror
)
399 : wxScrolledWindow(parent
, -1, wxDefaultPosition
, wxDefaultSize
,
400 wxHSCROLL
| wxVSCROLL
| wxNO_FULL_REPAINT_ON_RESIZE
)
405 MyCanvas::~MyCanvas()
409 void MyCanvas::OnDraw(wxDC
& dcOrig
)
411 wxMirrorDC
dc(dcOrig
, m_mirror
);
413 dc
.SetPen(*wxBLACK_PEN
);
414 dc
.DrawLine(0, 0, 100, 200);
416 dc
.SetBackgroundMode(wxTRANSPARENT
);
417 dc
.DrawText(_T("Testing"), 50, 50);
419 dc
.SetPen(*wxRED_PEN
);
420 dc
.SetBrush(*wxGREEN_BRUSH
);
421 dc
.DrawRectangle(120, 120, 100, 80);