1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSplitterWindow sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
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 #ifndef wxHAS_IMAGES_IN_RESOURCES
43 #include "../sample.xpm"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // ID for the menu commands
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 class MyApp
: public wxApp
76 virtual bool OnInit();
78 wxDECLARE_NO_COPY_CLASS(MyApp
);
81 class MyFrame
: public wxFrame
87 void ToggleFlag(int flag
, bool enable
);
90 void OnSplitHorizontal(wxCommandEvent
& event
);
91 void OnSplitVertical(wxCommandEvent
& event
);
92 void OnUnsplit(wxCommandEvent
& event
);
93 void OnToggleLive(wxCommandEvent
& event
)
94 { ToggleFlag(wxSP_LIVE_UPDATE
, event
.IsChecked()); }
95 void OnToggleBorder(wxCommandEvent
& event
)
96 { ToggleFlag(wxSP_BORDER
, event
.IsChecked()); }
97 void OnToggle3DSash(wxCommandEvent
& event
)
98 { ToggleFlag(wxSP_3DSASH
, event
.IsChecked()); }
99 void OnSetPosition(wxCommandEvent
& event
);
100 void OnSetMinSize(wxCommandEvent
& event
);
101 void OnSetGravity(wxCommandEvent
& event
);
102 void OnReplace(wxCommandEvent
&event
);
103 void OnToggleInvisible(wxCommandEvent
&event
);
105 void OnQuit(wxCommandEvent
& event
);
107 // Menu command update functions
108 void OnUpdateUIHorizontal(wxUpdateUIEvent
& event
);
109 void OnUpdateUIVertical(wxUpdateUIEvent
& event
);
110 void OnUpdateUIUnsplit(wxUpdateUIEvent
& event
);
111 void OnUpdateUIInvisible(wxUpdateUIEvent
& event
);
114 wxWindow
*m_left
, *m_right
;
116 wxSplitterWindow
* m_splitter
;
117 wxWindow
*m_replacewindow
;
119 DECLARE_EVENT_TABLE()
120 wxDECLARE_NO_COPY_CLASS(MyFrame
);
123 class MySplitterWindow
: public wxSplitterWindow
126 MySplitterWindow(wxFrame
*parent
);
129 void OnPositionChanged(wxSplitterEvent
& event
);
130 void OnPositionChanging(wxSplitterEvent
& event
);
131 void OnDClick(wxSplitterEvent
& event
);
132 void OnUnsplitEvent(wxSplitterEvent
& event
);
137 DECLARE_EVENT_TABLE()
138 wxDECLARE_NO_COPY_CLASS(MySplitterWindow
);
141 class MyCanvas
: public wxScrolledWindow
144 MyCanvas(wxWindow
* parent
, bool mirror
);
145 virtual ~MyCanvas(){};
147 virtual void OnDraw(wxDC
& dc
);
152 wxDECLARE_NO_COPY_CLASS(MyCanvas
);
155 // ============================================================================
157 // ============================================================================
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
167 if ( !wxApp::OnInit() )
170 // create and show the main frame
171 MyFrame
* frame
= new MyFrame
;
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
182 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
183 EVT_MENU(SPLIT_VERTICAL
, MyFrame::OnSplitVertical
)
184 EVT_MENU(SPLIT_HORIZONTAL
, MyFrame::OnSplitHorizontal
)
185 EVT_MENU(SPLIT_UNSPLIT
, MyFrame::OnUnsplit
)
186 EVT_MENU(SPLIT_LIVE
, MyFrame::OnToggleLive
)
187 EVT_MENU(SPLIT_BORDER
, MyFrame::OnToggleBorder
)
188 EVT_MENU(SPLIT_3DSASH
, MyFrame::OnToggle3DSash
)
189 EVT_MENU(SPLIT_SETPOSITION
, MyFrame::OnSetPosition
)
190 EVT_MENU(SPLIT_SETMINSIZE
, MyFrame::OnSetMinSize
)
191 EVT_MENU(SPLIT_SETGRAVITY
, MyFrame::OnSetGravity
)
192 EVT_MENU(SPLIT_REPLACE
, MyFrame::OnReplace
)
193 EVT_MENU(SPLIT_INVISIBLE
, MyFrame::OnToggleInvisible
)
195 EVT_MENU(SPLIT_QUIT
, MyFrame::OnQuit
)
197 EVT_UPDATE_UI(SPLIT_VERTICAL
, MyFrame::OnUpdateUIVertical
)
198 EVT_UPDATE_UI(SPLIT_HORIZONTAL
, MyFrame::OnUpdateUIHorizontal
)
199 EVT_UPDATE_UI(SPLIT_UNSPLIT
, MyFrame::OnUpdateUIUnsplit
)
200 EVT_UPDATE_UI(SPLIT_INVISIBLE
, MyFrame::OnUpdateUIInvisible
)
203 // My frame constructor
205 : wxFrame(NULL
, wxID_ANY
, wxT("wxSplitterWindow sample"),
206 wxDefaultPosition
, wxSize(420, 300),
207 wxDEFAULT_FRAME_STYLE
| wxNO_FULL_REPAINT_ON_RESIZE
)
209 SetIcon(wxICON(sample
));
213 #endif // wxUSE_STATUSBAR
216 wxMenu
*splitMenu
= new wxMenu
;
217 splitMenu
->Append(SPLIT_VERTICAL
,
218 wxT("Split &Vertically\tCtrl-V"),
219 wxT("Split vertically"));
220 splitMenu
->Append(SPLIT_HORIZONTAL
,
221 wxT("Split &Horizontally\tCtrl-H"),
222 wxT("Split horizontally"));
223 splitMenu
->Append(SPLIT_UNSPLIT
,
224 wxT("&Unsplit\tCtrl-U"),
226 splitMenu
->AppendCheckItem(SPLIT_INVISIBLE
,
227 wxT("Toggle sash &invisibility\tCtrl-I"),
228 wxT("Toggle sash invisibility"));
229 splitMenu
->AppendSeparator();
231 splitMenu
->AppendCheckItem(SPLIT_LIVE
,
232 wxT("&Live update\tCtrl-L"),
233 wxT("Toggle live update mode"));
234 splitMenu
->AppendCheckItem(SPLIT_BORDER
,
236 wxT("Toggle wxSP_BORDER flag"));
237 splitMenu
->Check(SPLIT_BORDER
, true);
238 splitMenu
->AppendCheckItem(SPLIT_3DSASH
,
240 wxT("Toggle wxSP_3DSASH flag"));
241 splitMenu
->Check(SPLIT_3DSASH
, true);
242 splitMenu
->Append(SPLIT_SETPOSITION
,
243 wxT("Set splitter &position\tCtrl-P"),
244 wxT("Set the splitter position"));
245 splitMenu
->Append(SPLIT_SETMINSIZE
,
246 wxT("Set &min size\tCtrl-M"),
247 wxT("Set minimum pane size"));
248 splitMenu
->Append(SPLIT_SETGRAVITY
,
249 wxT("Set &gravity\tCtrl-G"),
250 wxT("Set gravity of sash"));
251 splitMenu
->AppendSeparator();
253 splitMenu
->Append(SPLIT_REPLACE
,
254 wxT("&Replace right window"),
255 wxT("Replace right window"));
256 splitMenu
->AppendSeparator();
258 splitMenu
->Append(SPLIT_QUIT
, wxT("E&xit\tAlt-X"), wxT("Exit"));
260 wxMenuBar
*menuBar
= new wxMenuBar
;
261 menuBar
->Append(splitMenu
, wxT("&Splitter"));
265 menuBar
->Check(SPLIT_LIVE
, true);
266 m_splitter
= new MySplitterWindow(this);
268 // If you use non-zero gravity you must initialize the splitter with its
269 // correct initial size, otherwise it will change the sash position by a
270 // huge amount when it's resized from its initial default size to its real
271 // size when the frame lays it out. This wouldn't be necessary if default
272 // zero gravity were used (although it would do no harm neither).
273 m_splitter
->SetSize(GetClientSize());
274 m_splitter
->SetSashGravity(1.0);
277 m_left
= new MyCanvas(m_splitter
, true);
278 m_left
->SetBackgroundColour(*wxRED
);
279 m_left
->SetCursor(wxCursor(wxCURSOR_MAGNIFIER
));
281 m_right
= new MyCanvas(m_splitter
, false);
282 m_right
->SetBackgroundColour(*wxCYAN
);
283 #else // for testing kbd navigation inside the splitter
284 m_left
= new wxTextCtrl(m_splitter
, wxID_ANY
, wxT("first text"));
285 m_right
= new wxTextCtrl(m_splitter
, wxID_ANY
, wxT("second text"));
288 // you can also do this to start with a single window
290 m_right
->Show(false);
291 m_splitter
->Initialize(m_left
);
293 // you can also try -100
294 m_splitter
->SplitVertically(m_left
, m_right
, 100);
298 SetStatusText(wxT("Min pane size = 0"), 1);
299 #endif // wxUSE_STATUSBAR
301 m_replacewindow
= NULL
;
306 if (m_replacewindow
) {
307 m_replacewindow
->Destroy();
311 // menu command handlers
313 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
318 void MyFrame::OnSplitHorizontal(wxCommandEvent
& WXUNUSED(event
) )
320 if ( m_splitter
->IsSplit() )
321 m_splitter
->Unsplit();
324 m_splitter
->SplitHorizontally( m_left
, m_right
);
325 m_replacewindow
= NULL
;
328 SetStatusText(wxT("Splitter split horizontally"), 1);
329 #endif // wxUSE_STATUSBAR
332 void MyFrame::OnSplitVertical(wxCommandEvent
& WXUNUSED(event
) )
334 if ( m_splitter
->IsSplit() )
335 m_splitter
->Unsplit();
338 m_splitter
->SplitVertically( m_left
, m_right
);
339 m_replacewindow
= NULL
;
342 SetStatusText(wxT("Splitter split vertically"), 1);
343 #endif // wxUSE_STATUSBAR
346 void MyFrame::OnUnsplit(wxCommandEvent
& WXUNUSED(event
) )
348 if ( m_splitter
->IsSplit() )
349 m_splitter
->Unsplit();
351 SetStatusText(wxT("No splitter"));
352 #endif // wxUSE_STATUSBAR
355 void MyFrame::ToggleFlag(int flag
, bool enable
)
357 long style
= m_splitter
->GetWindowStyleFlag();
363 m_splitter
->SetWindowStyleFlag(style
);
365 // we need to move sash to redraw it
366 int pos
= m_splitter
->GetSashPosition();
367 m_splitter
->SetSashPosition(pos
+ 1);
368 m_splitter
->SetSashPosition(pos
);
371 void MyFrame::OnSetPosition(wxCommandEvent
& WXUNUSED(event
) )
374 str
.Printf( wxT("%d"), m_splitter
->GetSashPosition());
376 str
= wxGetTextFromUser(wxT("Enter splitter position:"), wxT(""), str
, this);
382 if ( !str
.ToLong(&pos
) )
384 wxLogError(wxT("The splitter position should be an integer."));
388 m_splitter
->SetSashPosition(pos
);
390 wxLogStatus(this, wxT("Splitter position set to %ld"), pos
);
393 void MyFrame::OnSetMinSize(wxCommandEvent
& WXUNUSED(event
) )
396 str
.Printf( wxT("%d"), m_splitter
->GetMinimumPaneSize());
398 str
= wxGetTextFromUser(wxT("Enter minimal size for panes:"), wxT(""), str
, this);
403 int minsize
= wxStrtol( str
, (wxChar
**)NULL
, 10 );
404 m_splitter
->SetMinimumPaneSize(minsize
);
406 str
.Printf( wxT("Min pane size = %d"), minsize
);
407 SetStatusText(str
, 1);
408 #endif // wxUSE_STATUSBAR
411 void MyFrame::OnSetGravity(wxCommandEvent
& WXUNUSED(event
) )
414 str
.Printf( wxT("%g"), m_splitter
->GetSashGravity());
416 str
= wxGetTextFromUser(wxT("Enter sash gravity (0,1):"), wxT(""), str
, this);
421 double gravity
= wxStrtod( str
, (wxChar
**)NULL
);
422 m_splitter
->SetSashGravity(gravity
);
424 str
.Printf( wxT("Gravity = %g"), gravity
);
425 SetStatusText(str
, 1);
426 #endif // wxUSE_STATUSBAR
429 void MyFrame::OnReplace(wxCommandEvent
& WXUNUSED(event
) )
431 if (m_replacewindow
== NULL
) {
432 m_replacewindow
= m_splitter
->GetWindow2();
433 m_splitter
->ReplaceWindow(m_replacewindow
, new wxPanel(m_splitter
, wxID_ANY
));
434 m_replacewindow
->Hide();
436 wxWindow
*empty
= m_splitter
->GetWindow2();
437 wxASSERT(empty
!= m_replacewindow
);
438 m_splitter
->ReplaceWindow(empty
, m_replacewindow
);
439 m_replacewindow
->Show();
440 m_replacewindow
= NULL
;
445 void MyFrame::OnToggleInvisible(wxCommandEvent
& WXUNUSED(event
) )
447 m_splitter
->SetSashInvisible(!m_splitter
->IsSashInvisible());
448 m_splitter
->SizeWindows();
451 // Update UI handlers
453 void MyFrame::OnUpdateUIHorizontal(wxUpdateUIEvent
& event
)
455 event
.Enable( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_HORIZONTAL
) );
458 void MyFrame::OnUpdateUIVertical(wxUpdateUIEvent
& event
)
460 event
.Enable( ( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_VERTICAL
) ) );
463 void MyFrame::OnUpdateUIUnsplit(wxUpdateUIEvent
& event
)
465 event
.Enable( m_splitter
->IsSplit() );
468 void MyFrame::OnUpdateUIInvisible(wxUpdateUIEvent
& event
)
470 event
.Check( m_splitter
->IsSashInvisible() );
473 // ----------------------------------------------------------------------------
475 // ----------------------------------------------------------------------------
477 BEGIN_EVENT_TABLE(MySplitterWindow
, wxSplitterWindow
)
478 EVT_SPLITTER_SASH_POS_CHANGED(wxID_ANY
, MySplitterWindow::OnPositionChanged
)
479 EVT_SPLITTER_SASH_POS_CHANGING(wxID_ANY
, MySplitterWindow::OnPositionChanging
)
481 EVT_SPLITTER_DCLICK(wxID_ANY
, MySplitterWindow::OnDClick
)
483 EVT_SPLITTER_UNSPLIT(wxID_ANY
, MySplitterWindow::OnUnsplitEvent
)
486 MySplitterWindow::MySplitterWindow(wxFrame
*parent
)
487 : wxSplitterWindow(parent
, wxID_ANY
,
488 wxDefaultPosition
, wxDefaultSize
,
489 wxSP_3D
| wxSP_LIVE_UPDATE
|
490 wxCLIP_CHILDREN
/* | wxSP_NO_XP_THEME */ )
495 void MySplitterWindow::OnPositionChanged(wxSplitterEvent
& event
)
497 wxLogStatus(m_frame
, wxT("Position has changed, now = %d (or %d)"),
498 event
.GetSashPosition(), GetSashPosition());
503 void MySplitterWindow::OnPositionChanging(wxSplitterEvent
& event
)
505 wxLogStatus(m_frame
, wxT("Position is changing, now = %d (or %d)"),
506 event
.GetSashPosition(), GetSashPosition());
511 void MySplitterWindow::OnDClick(wxSplitterEvent
& event
)
514 m_frame
->SetStatusText(wxT("Splitter double clicked"), 1);
515 #endif // wxUSE_STATUSBAR
520 void MySplitterWindow::OnUnsplitEvent(wxSplitterEvent
& event
)
523 m_frame
->SetStatusText(wxT("Splitter unsplit"), 1);
524 #endif // wxUSE_STATUSBAR
529 // ----------------------------------------------------------------------------
531 // ----------------------------------------------------------------------------
533 MyCanvas::MyCanvas(wxWindow
* parent
, bool mirror
)
534 : wxScrolledWindow(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
535 wxHSCROLL
| wxVSCROLL
| wxNO_FULL_REPAINT_ON_RESIZE
)
538 SetScrollbars(20, 20, 5, 5);
541 void MyCanvas::OnDraw(wxDC
& dcOrig
)
543 wxMirrorDC
dc(dcOrig
, m_mirror
);
545 dc
.SetPen(*wxBLACK_PEN
);
546 dc
.DrawLine(0, 0, 100, 200);
548 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
549 dc
.DrawText(wxT("Testing"), 50, 50);
551 dc
.SetPen(*wxRED_PEN
);
552 dc
.SetBrush(*wxGREEN_BRUSH
);
553 dc
.DrawRectangle(120, 120, 100, 80);