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 OnUnsplitEvent(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
, wxID_ANY
, _T("wxSplitterWindow sample"),
176 wxDefaultPosition
, wxSize(420, 300),
177 wxDEFAULT_FRAME_STYLE
| wxNO_FULL_REPAINT_ON_RESIZE
)
181 #endif // wxUSE_STATUSBAR
184 wxMenu
*splitMenu
= new wxMenu
;
185 splitMenu
->Append(SPLIT_VERTICAL
,
186 _T("Split &Vertically\tCtrl-V"),
187 _T("Split vertically"));
188 splitMenu
->Append(SPLIT_HORIZONTAL
,
189 _T("Split &Horizontally\tCtrl-H"),
190 _T("Split horizontally"));
191 splitMenu
->Append(SPLIT_UNSPLIT
,
192 _T("&Unsplit\tCtrl-U"),
194 splitMenu
->AppendSeparator();
196 splitMenu
->AppendCheckItem(SPLIT_LIVE
,
197 _T("&Live update\tCtrl-L"),
198 _T("Toggle live update mode"));
199 splitMenu
->Append(SPLIT_SETPOSITION
,
200 _T("Set splitter &position\tCtrl-P"),
201 _T("Set the splitter position"));
202 splitMenu
->Append(SPLIT_SETMINSIZE
,
203 _T("Set &min size\tCtrl-M"),
204 _T("Set minimum pane size"));
205 splitMenu
->AppendSeparator();
207 splitMenu
->Append(SPLIT_QUIT
, _T("E&xit\tAlt-X"), _T("Exit"));
209 wxMenuBar
*menuBar
= new wxMenuBar
;
210 menuBar
->Append(splitMenu
, _T("&Splitter"));
214 menuBar
->Check(SPLIT_LIVE
, true);
215 m_splitter
= new MySplitterWindow(this);
218 m_left
= new MyCanvas(m_splitter
, true);
219 m_left
->SetBackgroundColour(*wxRED
);
220 m_left
->SetScrollbars(20, 20, 5, 5);
221 m_left
->SetCursor(wxCursor(wxCURSOR_MAGNIFIER
));
223 m_right
= new MyCanvas(m_splitter
, false);
224 m_right
->SetBackgroundColour(*wxCYAN
);
225 m_right
->SetScrollbars(20, 20, 5, 5);
226 #else // for testing kbd navigation inside the splitter
227 m_left
= new wxTextCtrl(m_splitter
, wxID_ANY
, _T("first text"));
228 m_right
= new wxTextCtrl(m_splitter
, wxID_ANY
, _T("second text"));
231 // you can also do this to start with a single window
233 m_right
->Show(false);
234 m_splitter
->Initialize(m_left
);
236 // you can also try -100
237 m_splitter
->SplitVertically(m_left
, m_right
, 100);
241 SetStatusText(_T("Min pane size = 0"), 1);
242 #endif // wxUSE_STATUSBAR
249 // menu command handlers
251 void MyFrame::Quit(wxCommandEvent
& WXUNUSED(event
) )
256 void MyFrame::SplitHorizontal(wxCommandEvent
& WXUNUSED(event
) )
258 if ( m_splitter
->IsSplit() )
259 m_splitter
->Unsplit();
262 m_splitter
->SplitHorizontally( m_left
, m_right
);
265 SetStatusText(_T("Splitter split horizontally"), 1);
266 #endif // wxUSE_STATUSBAR
269 void MyFrame::SplitVertical(wxCommandEvent
& WXUNUSED(event
) )
271 if ( m_splitter
->IsSplit() )
272 m_splitter
->Unsplit();
275 m_splitter
->SplitVertically( m_left
, m_right
);
278 SetStatusText(_T("Splitter split vertically"), 1);
279 #endif // wxUSE_STATUSBAR
282 void MyFrame::Unsplit(wxCommandEvent
& WXUNUSED(event
) )
284 if ( m_splitter
->IsSplit() )
285 m_splitter
->Unsplit();
287 SetStatusText(_T("No splitter"));
288 #endif // wxUSE_STATUSBAR
291 void MyFrame::ToggleLive(wxCommandEvent
& event
)
293 long style
= m_splitter
->GetWindowStyleFlag();
294 if ( event
.IsChecked() )
295 style
|= wxSP_LIVE_UPDATE
;
297 style
&= ~wxSP_LIVE_UPDATE
;
299 m_splitter
->SetWindowStyleFlag(style
);
302 void MyFrame::SetPosition(wxCommandEvent
& WXUNUSED(event
) )
305 str
.Printf( wxT("%d"), m_splitter
->GetSashPosition());
306 str
= wxGetTextFromUser(_T("Enter splitter position:"), _T(""), str
, this);
311 if ( !str
.ToLong(&pos
) )
313 wxLogError(_T("The splitter position should be an integer."));
317 m_splitter
->SetSashPosition(pos
);
319 wxLogStatus(this, _T("Splitter position set to %ld"), pos
);
322 void MyFrame::SetMinSize(wxCommandEvent
& WXUNUSED(event
) )
325 str
.Printf( wxT("%d"), m_splitter
->GetMinimumPaneSize());
326 str
= wxGetTextFromUser(_T("Enter minimal size for panes:"), _T(""), str
, this);
330 int minsize
= wxStrtol( str
, (wxChar
**)NULL
, 10 );
331 m_splitter
->SetMinimumPaneSize(minsize
);
333 str
.Printf( wxT("Min pane size = %d"), minsize
);
334 SetStatusText(str
, 1);
335 #endif // wxUSE_STATUSBAR
338 // Update UI handlers
340 void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent
& event
)
342 event
.Enable( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_HORIZONTAL
) );
345 void MyFrame::UpdateUIVertical(wxUpdateUIEvent
& event
)
347 event
.Enable( ( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_VERTICAL
) ) );
350 void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent
& event
)
352 event
.Enable( m_splitter
->IsSplit() );
355 // ----------------------------------------------------------------------------
357 // ----------------------------------------------------------------------------
359 BEGIN_EVENT_TABLE(MySplitterWindow
, wxSplitterWindow
)
360 EVT_SPLITTER_SASH_POS_CHANGED(wxID_ANY
, MySplitterWindow::OnPositionChanged
)
361 EVT_SPLITTER_SASH_POS_CHANGING(wxID_ANY
, MySplitterWindow::OnPositionChanging
)
363 EVT_SPLITTER_DCLICK(wxID_ANY
, MySplitterWindow::OnDClick
)
365 EVT_SPLITTER_UNSPLIT(wxID_ANY
, MySplitterWindow::OnUnsplitEvent
)
368 MySplitterWindow::MySplitterWindow(wxFrame
*parent
)
369 : wxSplitterWindow(parent
, wxID_ANY
,
370 wxDefaultPosition
, wxDefaultSize
,
371 wxSP_3D
| wxSP_LIVE_UPDATE
|
372 wxCLIP_CHILDREN
/* | wxSP_NO_XP_THEME */ )
377 void MySplitterWindow::OnPositionChanged(wxSplitterEvent
& event
)
379 wxLogStatus(m_frame
, _T("Position has changed, now = %d (or %d)"),
380 event
.GetSashPosition(), GetSashPosition());
385 void MySplitterWindow::OnPositionChanging(wxSplitterEvent
& event
)
387 wxLogStatus(m_frame
, _T("Position is changing, now = %d (or %d)"),
388 event
.GetSashPosition(), GetSashPosition());
393 void MySplitterWindow::OnDClick(wxSplitterEvent
& event
)
396 m_frame
->SetStatusText(_T("Splitter double clicked"), 1);
397 #endif // wxUSE_STATUSBAR
402 void MySplitterWindow::OnUnsplitEvent(wxSplitterEvent
& event
)
405 m_frame
->SetStatusText(_T("Splitter unsplit"), 1);
406 #endif // wxUSE_STATUSBAR
411 // ----------------------------------------------------------------------------
413 // ----------------------------------------------------------------------------
415 MyCanvas::MyCanvas(wxWindow
* parent
, bool mirror
)
416 : wxScrolledWindow(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
417 wxHSCROLL
| wxVSCROLL
| wxNO_FULL_REPAINT_ON_RESIZE
)
422 MyCanvas::~MyCanvas()
426 void MyCanvas::OnDraw(wxDC
& dcOrig
)
428 wxMirrorDC
dc(dcOrig
, m_mirror
);
430 dc
.SetPen(*wxBLACK_PEN
);
431 dc
.DrawLine(0, 0, 100, 200);
433 dc
.SetBackgroundMode(wxTRANSPARENT
);
434 dc
.DrawText(_T("Testing"), 50, 50);
436 dc
.SetPen(*wxRED_PEN
);
437 dc
.SetBrush(*wxGREEN_BRUSH
);
438 dc
.DrawRectangle(120, 120, 100, 80);