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
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 class MyApp
: public wxApp
68 virtual bool OnInit();
70 DECLARE_NO_COPY_CLASS(MyApp
)
73 class MyFrame
: public wxFrame
80 void SplitHorizontal(wxCommandEvent
& event
);
81 void SplitVertical(wxCommandEvent
& event
);
82 void Unsplit(wxCommandEvent
& event
);
83 void ToggleLive(wxCommandEvent
& event
);
84 void SetPosition(wxCommandEvent
& event
);
85 void SetMinSize(wxCommandEvent
& event
);
86 void SetGravity(wxCommandEvent
& event
);
88 void Quit(wxCommandEvent
& event
);
90 // Menu command update functions
91 void UpdateUIHorizontal(wxUpdateUIEvent
& event
);
92 void UpdateUIVertical(wxUpdateUIEvent
& event
);
93 void UpdateUIUnsplit(wxUpdateUIEvent
& event
);
96 wxScrolledWindow
*m_left
, *m_right
;
98 wxSplitterWindow
* m_splitter
;
100 DECLARE_EVENT_TABLE()
101 DECLARE_NO_COPY_CLASS(MyFrame
)
104 class MySplitterWindow
: public wxSplitterWindow
107 MySplitterWindow(wxFrame
*parent
);
110 void OnPositionChanged(wxSplitterEvent
& event
);
111 void OnPositionChanging(wxSplitterEvent
& event
);
112 void OnDClick(wxSplitterEvent
& event
);
113 void OnUnsplitEvent(wxSplitterEvent
& event
);
118 DECLARE_EVENT_TABLE()
119 DECLARE_NO_COPY_CLASS(MySplitterWindow
)
122 class MyCanvas
: public wxScrolledWindow
125 MyCanvas(wxWindow
* parent
, bool mirror
);
126 virtual ~MyCanvas(){};
128 virtual void OnDraw(wxDC
& dc
);
133 DECLARE_NO_COPY_CLASS(MyCanvas
)
136 // ============================================================================
138 // ============================================================================
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
148 // create and show the main frame
149 MyFrame
* frame
= new MyFrame
;
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
160 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
161 EVT_MENU(SPLIT_VERTICAL
, MyFrame::SplitVertical
)
162 EVT_MENU(SPLIT_HORIZONTAL
, MyFrame::SplitHorizontal
)
163 EVT_MENU(SPLIT_UNSPLIT
, MyFrame::Unsplit
)
164 EVT_MENU(SPLIT_LIVE
, MyFrame::ToggleLive
)
165 EVT_MENU(SPLIT_SETPOSITION
, MyFrame::SetPosition
)
166 EVT_MENU(SPLIT_SETMINSIZE
, MyFrame::SetMinSize
)
167 EVT_MENU(SPLIT_SETGRAVITY
, MyFrame::SetGravity
)
169 EVT_MENU(SPLIT_QUIT
, MyFrame::Quit
)
171 EVT_UPDATE_UI(SPLIT_VERTICAL
, MyFrame::UpdateUIVertical
)
172 EVT_UPDATE_UI(SPLIT_HORIZONTAL
, MyFrame::UpdateUIHorizontal
)
173 EVT_UPDATE_UI(SPLIT_UNSPLIT
, MyFrame::UpdateUIUnsplit
)
176 // My frame constructor
178 : wxFrame(NULL
, wxID_ANY
, _T("wxSplitterWindow sample"),
179 wxDefaultPosition
, wxSize(420, 300),
180 wxDEFAULT_FRAME_STYLE
| wxNO_FULL_REPAINT_ON_RESIZE
)
184 #endif // wxUSE_STATUSBAR
187 wxMenu
*splitMenu
= new wxMenu
;
188 splitMenu
->Append(SPLIT_VERTICAL
,
189 _T("Split &Vertically\tCtrl-V"),
190 _T("Split vertically"));
191 splitMenu
->Append(SPLIT_HORIZONTAL
,
192 _T("Split &Horizontally\tCtrl-H"),
193 _T("Split horizontally"));
194 splitMenu
->Append(SPLIT_UNSPLIT
,
195 _T("&Unsplit\tCtrl-U"),
197 splitMenu
->AppendSeparator();
199 splitMenu
->AppendCheckItem(SPLIT_LIVE
,
200 _T("&Live update\tCtrl-L"),
201 _T("Toggle live update mode"));
202 splitMenu
->Append(SPLIT_SETPOSITION
,
203 _T("Set splitter &position\tCtrl-P"),
204 _T("Set the splitter position"));
205 splitMenu
->Append(SPLIT_SETMINSIZE
,
206 _T("Set &min size\tCtrl-M"),
207 _T("Set minimum pane size"));
208 splitMenu
->Append(SPLIT_SETGRAVITY
,
209 _T("Set &gravity\tCtrl-G"),
210 _T("Set gravity of sash"));
211 splitMenu
->AppendSeparator();
213 splitMenu
->Append(SPLIT_QUIT
, _T("E&xit\tAlt-X"), _T("Exit"));
215 wxMenuBar
*menuBar
= new wxMenuBar
;
216 menuBar
->Append(splitMenu
, _T("&Splitter"));
220 menuBar
->Check(SPLIT_LIVE
, true);
221 m_splitter
= new MySplitterWindow(this);
223 m_splitter
->SetSashGravity(1.0);
226 m_left
= new MyCanvas(m_splitter
, true);
227 m_left
->SetBackgroundColour(*wxRED
);
228 m_left
->SetScrollbars(20, 20, 5, 5);
229 m_left
->SetCursor(wxCursor(wxCURSOR_MAGNIFIER
));
231 m_right
= new MyCanvas(m_splitter
, false);
232 m_right
->SetBackgroundColour(*wxCYAN
);
233 m_right
->SetScrollbars(20, 20, 5, 5);
234 #else // for testing kbd navigation inside the splitter
235 m_left
= new wxTextCtrl(m_splitter
, wxID_ANY
, _T("first text"));
236 m_right
= new wxTextCtrl(m_splitter
, wxID_ANY
, _T("second text"));
239 // you can also do this to start with a single window
241 m_right
->Show(false);
242 m_splitter
->Initialize(m_left
);
244 // you can also try -100
245 m_splitter
->SplitVertically(m_left
, m_right
, 100);
249 SetStatusText(_T("Min pane size = 0"), 1);
250 #endif // wxUSE_STATUSBAR
253 // menu command handlers
255 void MyFrame::Quit(wxCommandEvent
& WXUNUSED(event
) )
260 void MyFrame::SplitHorizontal(wxCommandEvent
& WXUNUSED(event
) )
262 if ( m_splitter
->IsSplit() )
263 m_splitter
->Unsplit();
266 m_splitter
->SplitHorizontally( m_left
, m_right
);
269 SetStatusText(_T("Splitter split horizontally"), 1);
270 #endif // wxUSE_STATUSBAR
273 void MyFrame::SplitVertical(wxCommandEvent
& WXUNUSED(event
) )
275 if ( m_splitter
->IsSplit() )
276 m_splitter
->Unsplit();
279 m_splitter
->SplitVertically( m_left
, m_right
);
282 SetStatusText(_T("Splitter split vertically"), 1);
283 #endif // wxUSE_STATUSBAR
286 void MyFrame::Unsplit(wxCommandEvent
& WXUNUSED(event
) )
288 if ( m_splitter
->IsSplit() )
289 m_splitter
->Unsplit();
291 SetStatusText(_T("No splitter"));
292 #endif // wxUSE_STATUSBAR
295 void MyFrame::ToggleLive(wxCommandEvent
& event
)
297 long style
= m_splitter
->GetWindowStyleFlag();
298 if ( event
.IsChecked() )
299 style
|= wxSP_LIVE_UPDATE
;
301 style
&= ~wxSP_LIVE_UPDATE
;
303 m_splitter
->SetWindowStyleFlag(style
);
306 void MyFrame::SetPosition(wxCommandEvent
& WXUNUSED(event
) )
309 str
.Printf( wxT("%d"), m_splitter
->GetSashPosition());
310 str
= wxGetTextFromUser(_T("Enter splitter position:"), _T(""), str
, this);
315 if ( !str
.ToLong(&pos
) )
317 wxLogError(_T("The splitter position should be an integer."));
321 m_splitter
->SetSashPosition(pos
);
323 wxLogStatus(this, _T("Splitter position set to %ld"), pos
);
326 void MyFrame::SetMinSize(wxCommandEvent
& WXUNUSED(event
) )
329 str
.Printf( wxT("%d"), m_splitter
->GetMinimumPaneSize());
330 str
= wxGetTextFromUser(_T("Enter minimal size for panes:"), _T(""), str
, this);
334 int minsize
= wxStrtol( str
, (wxChar
**)NULL
, 10 );
335 m_splitter
->SetMinimumPaneSize(minsize
);
337 str
.Printf( wxT("Min pane size = %d"), minsize
);
338 SetStatusText(str
, 1);
339 #endif // wxUSE_STATUSBAR
341 void MyFrame::SetGravity(wxCommandEvent
& WXUNUSED(event
) )
344 str
.Printf( wxT("%g"), m_splitter
->GetSashGravity());
345 str
= wxGetTextFromUser(_T("Enter sash gravity (0,1):"), _T(""), str
, this);
349 double gravity
= wxStrtod( str
, (wxChar
**)NULL
);
350 m_splitter
->SetSashGravity(gravity
);
352 str
.Printf( wxT("Gravity = %g"), gravity
);
353 SetStatusText(str
, 1);
354 #endif // wxUSE_STATUSBAR
357 // Update UI handlers
359 void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent
& event
)
361 event
.Enable( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_HORIZONTAL
) );
364 void MyFrame::UpdateUIVertical(wxUpdateUIEvent
& event
)
366 event
.Enable( ( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_VERTICAL
) ) );
369 void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent
& event
)
371 event
.Enable( m_splitter
->IsSplit() );
374 // ----------------------------------------------------------------------------
376 // ----------------------------------------------------------------------------
378 BEGIN_EVENT_TABLE(MySplitterWindow
, wxSplitterWindow
)
379 EVT_SPLITTER_SASH_POS_CHANGED(wxID_ANY
, MySplitterWindow::OnPositionChanged
)
380 EVT_SPLITTER_SASH_POS_CHANGING(wxID_ANY
, MySplitterWindow::OnPositionChanging
)
382 EVT_SPLITTER_DCLICK(wxID_ANY
, MySplitterWindow::OnDClick
)
384 EVT_SPLITTER_UNSPLIT(wxID_ANY
, MySplitterWindow::OnUnsplitEvent
)
387 MySplitterWindow::MySplitterWindow(wxFrame
*parent
)
388 : wxSplitterWindow(parent
, wxID_ANY
,
389 wxDefaultPosition
, wxDefaultSize
,
390 wxSP_3D
| wxSP_LIVE_UPDATE
|
391 wxCLIP_CHILDREN
/* | wxSP_NO_XP_THEME */ )
396 void MySplitterWindow::OnPositionChanged(wxSplitterEvent
& event
)
398 wxLogStatus(m_frame
, _T("Position has changed, now = %d (or %d)"),
399 event
.GetSashPosition(), GetSashPosition());
404 void MySplitterWindow::OnPositionChanging(wxSplitterEvent
& event
)
406 wxLogStatus(m_frame
, _T("Position is changing, now = %d (or %d)"),
407 event
.GetSashPosition(), GetSashPosition());
412 void MySplitterWindow::OnDClick(wxSplitterEvent
& event
)
415 m_frame
->SetStatusText(_T("Splitter double clicked"), 1);
416 #endif // wxUSE_STATUSBAR
421 void MySplitterWindow::OnUnsplitEvent(wxSplitterEvent
& event
)
424 m_frame
->SetStatusText(_T("Splitter unsplit"), 1);
425 #endif // wxUSE_STATUSBAR
430 // ----------------------------------------------------------------------------
432 // ----------------------------------------------------------------------------
434 MyCanvas::MyCanvas(wxWindow
* parent
, bool mirror
)
435 : wxScrolledWindow(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
436 wxHSCROLL
| wxVSCROLL
| wxNO_FULL_REPAINT_ON_RESIZE
)
441 void MyCanvas::OnDraw(wxDC
& dcOrig
)
443 wxMirrorDC
dc(dcOrig
, m_mirror
);
445 dc
.SetPen(*wxBLACK_PEN
);
446 dc
.DrawLine(0, 0, 100, 200);
448 dc
.SetBackgroundMode(wxTRANSPARENT
);
449 dc
.DrawText(_T("Testing"), 50, 50);
451 dc
.SetPen(*wxRED_PEN
);
452 dc
.SetBrush(*wxGREEN_BRUSH
);
453 dc
.DrawRectangle(120, 120, 100, 80);