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
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 class MyApp
: public wxApp
71 virtual bool OnInit();
73 DECLARE_NO_COPY_CLASS(MyApp
)
76 class MyFrame
: public wxFrame
82 void ToggleFlag(int flag
, bool enable
);
85 void OnSplitHorizontal(wxCommandEvent
& event
);
86 void OnSplitVertical(wxCommandEvent
& event
);
87 void OnUnsplit(wxCommandEvent
& event
);
88 void OnToggleLive(wxCommandEvent
& event
)
89 { ToggleFlag(wxSP_LIVE_UPDATE
, event
.IsChecked()); }
90 void OnToggleBorder(wxCommandEvent
& event
)
91 { ToggleFlag(wxSP_BORDER
, event
.IsChecked()); }
92 void OnToggle3DSash(wxCommandEvent
& event
)
93 { ToggleFlag(wxSP_3DSASH
, event
.IsChecked()); }
94 void OnSetPosition(wxCommandEvent
& event
);
95 void OnSetMinSize(wxCommandEvent
& event
);
96 void OnSetGravity(wxCommandEvent
& event
);
97 void OnReplace(wxCommandEvent
&event
);
99 void OnQuit(wxCommandEvent
& event
);
101 // Menu command update functions
102 void OnUpdateUIHorizontal(wxUpdateUIEvent
& event
);
103 void OnUpdateUIVertical(wxUpdateUIEvent
& event
);
104 void OnUpdateUIUnsplit(wxUpdateUIEvent
& event
);
107 wxScrolledWindow
*m_left
, *m_right
;
109 wxSplitterWindow
* m_splitter
;
110 wxWindow
*m_replacewindow
;
112 DECLARE_EVENT_TABLE()
113 DECLARE_NO_COPY_CLASS(MyFrame
)
116 class MySplitterWindow
: public wxSplitterWindow
119 MySplitterWindow(wxFrame
*parent
);
122 void OnPositionChanged(wxSplitterEvent
& event
);
123 void OnPositionChanging(wxSplitterEvent
& event
);
124 void OnDClick(wxSplitterEvent
& event
);
125 void OnUnsplitEvent(wxSplitterEvent
& event
);
130 DECLARE_EVENT_TABLE()
131 DECLARE_NO_COPY_CLASS(MySplitterWindow
)
134 class MyCanvas
: public wxScrolledWindow
137 MyCanvas(wxWindow
* parent
, bool mirror
);
138 virtual ~MyCanvas(){};
140 virtual void OnDraw(wxDC
& dc
);
145 DECLARE_NO_COPY_CLASS(MyCanvas
)
148 // ============================================================================
150 // ============================================================================
152 // ----------------------------------------------------------------------------
154 // ----------------------------------------------------------------------------
160 if ( !wxApp::OnInit() )
163 // create and show the main frame
164 MyFrame
* frame
= new MyFrame
;
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
175 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
176 EVT_MENU(SPLIT_VERTICAL
, MyFrame::OnSplitVertical
)
177 EVT_MENU(SPLIT_HORIZONTAL
, MyFrame::OnSplitHorizontal
)
178 EVT_MENU(SPLIT_UNSPLIT
, MyFrame::OnUnsplit
)
179 EVT_MENU(SPLIT_LIVE
, MyFrame::OnToggleLive
)
180 EVT_MENU(SPLIT_BORDER
, MyFrame::OnToggleBorder
)
181 EVT_MENU(SPLIT_3DSASH
, MyFrame::OnToggle3DSash
)
182 EVT_MENU(SPLIT_SETPOSITION
, MyFrame::OnSetPosition
)
183 EVT_MENU(SPLIT_SETMINSIZE
, MyFrame::OnSetMinSize
)
184 EVT_MENU(SPLIT_SETGRAVITY
, MyFrame::OnSetGravity
)
185 EVT_MENU(SPLIT_REPLACE
, MyFrame::OnReplace
)
187 EVT_MENU(SPLIT_QUIT
, MyFrame::OnQuit
)
189 EVT_UPDATE_UI(SPLIT_VERTICAL
, MyFrame::OnUpdateUIVertical
)
190 EVT_UPDATE_UI(SPLIT_HORIZONTAL
, MyFrame::OnUpdateUIHorizontal
)
191 EVT_UPDATE_UI(SPLIT_UNSPLIT
, MyFrame::OnUpdateUIUnsplit
)
194 // My frame constructor
196 : wxFrame(NULL
, wxID_ANY
, _T("wxSplitterWindow sample"),
197 wxDefaultPosition
, wxSize(420, 300),
198 wxDEFAULT_FRAME_STYLE
| wxNO_FULL_REPAINT_ON_RESIZE
)
202 #endif // wxUSE_STATUSBAR
205 wxMenu
*splitMenu
= new wxMenu
;
206 splitMenu
->Append(SPLIT_VERTICAL
,
207 _T("Split &Vertically\tCtrl-V"),
208 _T("Split vertically"));
209 splitMenu
->Append(SPLIT_HORIZONTAL
,
210 _T("Split &Horizontally\tCtrl-H"),
211 _T("Split horizontally"));
212 splitMenu
->Append(SPLIT_UNSPLIT
,
213 _T("&Unsplit\tCtrl-U"),
215 splitMenu
->AppendSeparator();
217 splitMenu
->AppendCheckItem(SPLIT_LIVE
,
218 _T("&Live update\tCtrl-L"),
219 _T("Toggle live update mode"));
220 splitMenu
->AppendCheckItem(SPLIT_BORDER
,
222 _T("Toggle wxSP_BORDER flag"));
223 splitMenu
->Check(SPLIT_BORDER
, true);
224 splitMenu
->AppendCheckItem(SPLIT_3DSASH
,
226 _T("Toggle wxSP_3DSASH flag"));
227 splitMenu
->Check(SPLIT_3DSASH
, true);
228 splitMenu
->Append(SPLIT_SETPOSITION
,
229 _T("Set splitter &position\tCtrl-P"),
230 _T("Set the splitter position"));
231 splitMenu
->Append(SPLIT_SETMINSIZE
,
232 _T("Set &min size\tCtrl-M"),
233 _T("Set minimum pane size"));
234 splitMenu
->Append(SPLIT_SETGRAVITY
,
235 _T("Set &gravity\tCtrl-G"),
236 _T("Set gravity of sash"));
237 splitMenu
->AppendSeparator();
239 splitMenu
->Append(SPLIT_REPLACE
,
240 _T("&Replace right window"),
241 _T("Replace right window"));
242 splitMenu
->AppendSeparator();
244 splitMenu
->Append(SPLIT_QUIT
, _T("E&xit\tAlt-X"), _T("Exit"));
246 wxMenuBar
*menuBar
= new wxMenuBar
;
247 menuBar
->Append(splitMenu
, _T("&Splitter"));
251 menuBar
->Check(SPLIT_LIVE
, true);
252 m_splitter
= new MySplitterWindow(this);
254 m_splitter
->SetSashGravity(1.0);
257 m_left
= new MyCanvas(m_splitter
, true);
258 m_left
->SetBackgroundColour(*wxRED
);
259 m_left
->SetScrollbars(20, 20, 5, 5);
260 m_left
->SetCursor(wxCursor(wxCURSOR_MAGNIFIER
));
262 m_right
= new MyCanvas(m_splitter
, false);
263 m_right
->SetBackgroundColour(*wxCYAN
);
264 m_right
->SetScrollbars(20, 20, 5, 5);
265 #else // for testing kbd navigation inside the splitter
266 m_left
= new wxTextCtrl(m_splitter
, wxID_ANY
, _T("first text"));
267 m_right
= new wxTextCtrl(m_splitter
, wxID_ANY
, _T("second text"));
270 // you can also do this to start with a single window
272 m_right
->Show(false);
273 m_splitter
->Initialize(m_left
);
275 // you can also try -100
276 m_splitter
->SplitVertically(m_left
, m_right
, 100);
280 SetStatusText(_T("Min pane size = 0"), 1);
281 #endif // wxUSE_STATUSBAR
283 m_replacewindow
= (wxWindow
*)0;
288 if (m_replacewindow
) {
289 m_replacewindow
->Destroy();
290 m_replacewindow
= (wxWindow
*)0;
294 // menu command handlers
296 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
301 void MyFrame::OnSplitHorizontal(wxCommandEvent
& WXUNUSED(event
) )
303 if ( m_splitter
->IsSplit() )
304 m_splitter
->Unsplit();
307 m_splitter
->SplitHorizontally( m_left
, m_right
);
310 SetStatusText(_T("Splitter split horizontally"), 1);
311 #endif // wxUSE_STATUSBAR
314 void MyFrame::OnSplitVertical(wxCommandEvent
& WXUNUSED(event
) )
316 if ( m_splitter
->IsSplit() )
317 m_splitter
->Unsplit();
320 m_splitter
->SplitVertically( m_left
, m_right
);
323 SetStatusText(_T("Splitter split vertically"), 1);
324 #endif // wxUSE_STATUSBAR
327 void MyFrame::OnUnsplit(wxCommandEvent
& WXUNUSED(event
) )
329 if ( m_splitter
->IsSplit() )
330 m_splitter
->Unsplit();
332 SetStatusText(_T("No splitter"));
333 #endif // wxUSE_STATUSBAR
336 void MyFrame::ToggleFlag(int flag
, bool enable
)
338 long style
= m_splitter
->GetWindowStyleFlag();
344 m_splitter
->SetWindowStyleFlag(style
);
346 // we need to move sash to redraw it
347 int pos
= m_splitter
->GetSashPosition();
348 m_splitter
->SetSashPosition(pos
+ 1);
349 m_splitter
->SetSashPosition(pos
);
352 void MyFrame::OnSetPosition(wxCommandEvent
& WXUNUSED(event
) )
355 str
.Printf( wxT("%d"), m_splitter
->GetSashPosition());
357 str
= wxGetTextFromUser(_T("Enter splitter position:"), _T(""), str
, this);
363 if ( !str
.ToLong(&pos
) )
365 wxLogError(_T("The splitter position should be an integer."));
369 m_splitter
->SetSashPosition(pos
);
371 wxLogStatus(this, _T("Splitter position set to %ld"), pos
);
374 void MyFrame::OnSetMinSize(wxCommandEvent
& WXUNUSED(event
) )
377 str
.Printf( wxT("%d"), m_splitter
->GetMinimumPaneSize());
379 str
= wxGetTextFromUser(_T("Enter minimal size for panes:"), _T(""), str
, this);
384 int minsize
= wxStrtol( str
, (wxChar
**)NULL
, 10 );
385 m_splitter
->SetMinimumPaneSize(minsize
);
387 str
.Printf( wxT("Min pane size = %d"), minsize
);
388 SetStatusText(str
, 1);
389 #endif // wxUSE_STATUSBAR
392 void MyFrame::OnSetGravity(wxCommandEvent
& WXUNUSED(event
) )
395 str
.Printf( wxT("%g"), m_splitter
->GetSashGravity());
397 str
= wxGetTextFromUser(_T("Enter sash gravity (0,1):"), _T(""), str
, this);
402 double gravity
= wxStrtod( str
, (wxChar
**)NULL
);
403 m_splitter
->SetSashGravity(gravity
);
405 str
.Printf( wxT("Gravity = %g"), gravity
);
406 SetStatusText(str
, 1);
407 #endif // wxUSE_STATUSBAR
410 void MyFrame::OnReplace(wxCommandEvent
& WXUNUSED(event
) )
412 if (m_replacewindow
== 0) {
413 m_replacewindow
= m_splitter
->GetWindow2();
414 m_splitter
->ReplaceWindow(m_replacewindow
, new wxPanel(m_splitter
, wxID_ANY
));
415 m_replacewindow
->Hide();
417 wxWindow
*empty
= m_splitter
->GetWindow2();
418 m_splitter
->ReplaceWindow(empty
, m_replacewindow
);
419 m_replacewindow
->Show();
425 // Update UI handlers
427 void MyFrame::OnUpdateUIHorizontal(wxUpdateUIEvent
& event
)
429 event
.Enable( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_HORIZONTAL
) );
432 void MyFrame::OnUpdateUIVertical(wxUpdateUIEvent
& event
)
434 event
.Enable( ( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_VERTICAL
) ) );
437 void MyFrame::OnUpdateUIUnsplit(wxUpdateUIEvent
& event
)
439 event
.Enable( m_splitter
->IsSplit() );
442 // ----------------------------------------------------------------------------
444 // ----------------------------------------------------------------------------
446 BEGIN_EVENT_TABLE(MySplitterWindow
, wxSplitterWindow
)
447 EVT_SPLITTER_SASH_POS_CHANGED(wxID_ANY
, MySplitterWindow::OnPositionChanged
)
448 EVT_SPLITTER_SASH_POS_CHANGING(wxID_ANY
, MySplitterWindow::OnPositionChanging
)
450 EVT_SPLITTER_DCLICK(wxID_ANY
, MySplitterWindow::OnDClick
)
452 EVT_SPLITTER_UNSPLIT(wxID_ANY
, MySplitterWindow::OnUnsplitEvent
)
455 MySplitterWindow::MySplitterWindow(wxFrame
*parent
)
456 : wxSplitterWindow(parent
, wxID_ANY
,
457 wxDefaultPosition
, wxDefaultSize
,
458 wxSP_3D
| wxSP_LIVE_UPDATE
|
459 wxCLIP_CHILDREN
/* | wxSP_NO_XP_THEME */ )
464 void MySplitterWindow::OnPositionChanged(wxSplitterEvent
& event
)
466 wxLogStatus(m_frame
, _T("Position has changed, now = %d (or %d)"),
467 event
.GetSashPosition(), GetSashPosition());
472 void MySplitterWindow::OnPositionChanging(wxSplitterEvent
& event
)
474 wxLogStatus(m_frame
, _T("Position is changing, now = %d (or %d)"),
475 event
.GetSashPosition(), GetSashPosition());
480 void MySplitterWindow::OnDClick(wxSplitterEvent
& event
)
483 m_frame
->SetStatusText(_T("Splitter double clicked"), 1);
484 #endif // wxUSE_STATUSBAR
489 void MySplitterWindow::OnUnsplitEvent(wxSplitterEvent
& event
)
492 m_frame
->SetStatusText(_T("Splitter unsplit"), 1);
493 #endif // wxUSE_STATUSBAR
498 // ----------------------------------------------------------------------------
500 // ----------------------------------------------------------------------------
502 MyCanvas::MyCanvas(wxWindow
* parent
, bool mirror
)
503 : wxScrolledWindow(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
504 wxHSCROLL
| wxVSCROLL
| wxNO_FULL_REPAINT_ON_RESIZE
)
509 void MyCanvas::OnDraw(wxDC
& dcOrig
)
511 wxMirrorDC
dc(dcOrig
, m_mirror
);
513 dc
.SetPen(*wxBLACK_PEN
);
514 dc
.DrawLine(0, 0, 100, 200);
516 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
517 dc
.DrawText(_T("Testing"), 50, 50);
519 dc
.SetPen(*wxRED_PEN
);
520 dc
.SetBrush(*wxGREEN_BRUSH
);
521 dc
.DrawRectangle(120, 120, 100, 80);