1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSplitterWindow sample
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
32 #include "wx/scrolwin.h"
35 #include "wx/textdlg.h" // for wxGetTextFromUser
38 #include "wx/splitter.h"
39 #include "wx/dcmirror.h"
41 #ifndef wxHAS_IMAGES_IN_RESOURCES
42 #include "../sample.xpm"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // ID for the menu commands
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 class MyApp
: public wxApp
75 virtual bool OnInit();
77 wxDECLARE_NO_COPY_CLASS(MyApp
);
80 class MyFrame
: public wxFrame
86 void ToggleFlag(int flag
, bool enable
);
89 void OnSplitHorizontal(wxCommandEvent
& event
);
90 void OnSplitVertical(wxCommandEvent
& event
);
91 void OnUnsplit(wxCommandEvent
& event
);
92 void OnToggleLive(wxCommandEvent
& event
)
93 { ToggleFlag(wxSP_LIVE_UPDATE
, event
.IsChecked()); }
94 void OnToggleBorder(wxCommandEvent
& event
)
95 { ToggleFlag(wxSP_BORDER
, event
.IsChecked()); }
96 void OnToggle3DSash(wxCommandEvent
& event
)
97 { ToggleFlag(wxSP_3DSASH
, event
.IsChecked()); }
98 void OnSetPosition(wxCommandEvent
& event
);
99 void OnSetMinSize(wxCommandEvent
& event
);
100 void OnSetGravity(wxCommandEvent
& event
);
101 void OnReplace(wxCommandEvent
&event
);
102 void OnToggleInvisible(wxCommandEvent
&event
);
104 void OnQuit(wxCommandEvent
& event
);
106 // Menu command update functions
107 void OnUpdateUIHorizontal(wxUpdateUIEvent
& event
);
108 void OnUpdateUIVertical(wxUpdateUIEvent
& event
);
109 void OnUpdateUIUnsplit(wxUpdateUIEvent
& event
);
110 void OnUpdateUIInvisible(wxUpdateUIEvent
& event
);
113 wxWindow
*m_left
, *m_right
;
115 wxSplitterWindow
* m_splitter
;
116 wxWindow
*m_replacewindow
;
118 DECLARE_EVENT_TABLE()
119 wxDECLARE_NO_COPY_CLASS(MyFrame
);
122 class MySplitterWindow
: public wxSplitterWindow
125 MySplitterWindow(wxFrame
*parent
);
128 void OnPositionChanged(wxSplitterEvent
& event
);
129 void OnPositionChanging(wxSplitterEvent
& event
);
130 void OnDClick(wxSplitterEvent
& event
);
131 void OnUnsplitEvent(wxSplitterEvent
& event
);
136 DECLARE_EVENT_TABLE()
137 wxDECLARE_NO_COPY_CLASS(MySplitterWindow
);
140 class MyCanvas
: public wxScrolledWindow
143 MyCanvas(wxWindow
* parent
, bool mirror
);
144 virtual ~MyCanvas(){};
146 virtual void OnDraw(wxDC
& dc
);
151 wxDECLARE_NO_COPY_CLASS(MyCanvas
);
154 // ============================================================================
156 // ============================================================================
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
166 if ( !wxApp::OnInit() )
169 // create and show the main frame
170 MyFrame
* frame
= new MyFrame
;
177 // ----------------------------------------------------------------------------
179 // ----------------------------------------------------------------------------
181 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
182 EVT_MENU(SPLIT_VERTICAL
, MyFrame::OnSplitVertical
)
183 EVT_MENU(SPLIT_HORIZONTAL
, MyFrame::OnSplitHorizontal
)
184 EVT_MENU(SPLIT_UNSPLIT
, MyFrame::OnUnsplit
)
185 EVT_MENU(SPLIT_LIVE
, MyFrame::OnToggleLive
)
186 EVT_MENU(SPLIT_BORDER
, MyFrame::OnToggleBorder
)
187 EVT_MENU(SPLIT_3DSASH
, MyFrame::OnToggle3DSash
)
188 EVT_MENU(SPLIT_SETPOSITION
, MyFrame::OnSetPosition
)
189 EVT_MENU(SPLIT_SETMINSIZE
, MyFrame::OnSetMinSize
)
190 EVT_MENU(SPLIT_SETGRAVITY
, MyFrame::OnSetGravity
)
191 EVT_MENU(SPLIT_REPLACE
, MyFrame::OnReplace
)
192 EVT_MENU(SPLIT_INVISIBLE
, MyFrame::OnToggleInvisible
)
194 EVT_MENU(SPLIT_QUIT
, MyFrame::OnQuit
)
196 EVT_UPDATE_UI(SPLIT_VERTICAL
, MyFrame::OnUpdateUIVertical
)
197 EVT_UPDATE_UI(SPLIT_HORIZONTAL
, MyFrame::OnUpdateUIHorizontal
)
198 EVT_UPDATE_UI(SPLIT_UNSPLIT
, MyFrame::OnUpdateUIUnsplit
)
199 EVT_UPDATE_UI(SPLIT_INVISIBLE
, MyFrame::OnUpdateUIInvisible
)
202 // My frame constructor
204 : wxFrame(NULL
, wxID_ANY
, wxT("wxSplitterWindow sample"),
205 wxDefaultPosition
, wxSize(420, 300),
206 wxDEFAULT_FRAME_STYLE
| wxNO_FULL_REPAINT_ON_RESIZE
)
208 SetIcon(wxICON(sample
));
212 #endif // wxUSE_STATUSBAR
215 wxMenu
*splitMenu
= new wxMenu
;
216 splitMenu
->Append(SPLIT_VERTICAL
,
217 wxT("Split &Vertically\tCtrl-V"),
218 wxT("Split vertically"));
219 splitMenu
->Append(SPLIT_HORIZONTAL
,
220 wxT("Split &Horizontally\tCtrl-H"),
221 wxT("Split horizontally"));
222 splitMenu
->Append(SPLIT_UNSPLIT
,
223 wxT("&Unsplit\tCtrl-U"),
225 splitMenu
->AppendCheckItem(SPLIT_INVISIBLE
,
226 wxT("Toggle sash &invisibility\tCtrl-I"),
227 wxT("Toggle sash invisibility"));
228 splitMenu
->AppendSeparator();
230 splitMenu
->AppendCheckItem(SPLIT_LIVE
,
231 wxT("&Live update\tCtrl-L"),
232 wxT("Toggle live update mode"));
233 splitMenu
->AppendCheckItem(SPLIT_BORDER
,
235 wxT("Toggle wxSP_BORDER flag"));
236 splitMenu
->Check(SPLIT_BORDER
, true);
237 splitMenu
->AppendCheckItem(SPLIT_3DSASH
,
239 wxT("Toggle wxSP_3DSASH flag"));
240 splitMenu
->Check(SPLIT_3DSASH
, true);
241 splitMenu
->Append(SPLIT_SETPOSITION
,
242 wxT("Set splitter &position\tCtrl-P"),
243 wxT("Set the splitter position"));
244 splitMenu
->Append(SPLIT_SETMINSIZE
,
245 wxT("Set &min size\tCtrl-M"),
246 wxT("Set minimum pane size"));
247 splitMenu
->Append(SPLIT_SETGRAVITY
,
248 wxT("Set &gravity\tCtrl-G"),
249 wxT("Set gravity of sash"));
250 splitMenu
->AppendSeparator();
252 splitMenu
->Append(SPLIT_REPLACE
,
253 wxT("&Replace right window"),
254 wxT("Replace right window"));
255 splitMenu
->AppendSeparator();
257 splitMenu
->Append(SPLIT_QUIT
, wxT("E&xit\tAlt-X"), wxT("Exit"));
259 wxMenuBar
*menuBar
= new wxMenuBar
;
260 menuBar
->Append(splitMenu
, wxT("&Splitter"));
264 menuBar
->Check(SPLIT_LIVE
, true);
265 m_splitter
= new MySplitterWindow(this);
267 // If you use non-zero gravity you must initialize the splitter with its
268 // correct initial size, otherwise it will change the sash position by a
269 // huge amount when it's resized from its initial default size to its real
270 // size when the frame lays it out. This wouldn't be necessary if default
271 // zero gravity were used (although it would do no harm neither).
272 m_splitter
->SetSize(GetClientSize());
273 m_splitter
->SetSashGravity(1.0);
276 m_left
= new MyCanvas(m_splitter
, true);
277 m_left
->SetBackgroundColour(*wxRED
);
278 m_left
->SetCursor(wxCursor(wxCURSOR_MAGNIFIER
));
280 m_right
= new MyCanvas(m_splitter
, false);
281 m_right
->SetBackgroundColour(*wxCYAN
);
282 #else // for testing kbd navigation inside the splitter
283 m_left
= new wxTextCtrl(m_splitter
, wxID_ANY
, wxT("first text"));
284 m_right
= new wxTextCtrl(m_splitter
, wxID_ANY
, wxT("second text"));
287 // you can also do this to start with a single window
289 m_right
->Show(false);
290 m_splitter
->Initialize(m_left
);
292 // you can also try -100
293 m_splitter
->SplitVertically(m_left
, m_right
, 100);
297 SetStatusText(wxT("Min pane size = 0"), 1);
298 #endif // wxUSE_STATUSBAR
300 m_replacewindow
= NULL
;
305 if (m_replacewindow
) {
306 m_replacewindow
->Destroy();
310 // menu command handlers
312 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
317 void MyFrame::OnSplitHorizontal(wxCommandEvent
& WXUNUSED(event
) )
319 if ( m_splitter
->IsSplit() )
320 m_splitter
->Unsplit();
323 m_splitter
->SplitHorizontally( m_left
, m_right
);
324 m_replacewindow
= NULL
;
327 SetStatusText(wxT("Splitter split horizontally"), 1);
328 #endif // wxUSE_STATUSBAR
331 void MyFrame::OnSplitVertical(wxCommandEvent
& WXUNUSED(event
) )
333 if ( m_splitter
->IsSplit() )
334 m_splitter
->Unsplit();
337 m_splitter
->SplitVertically( m_left
, m_right
);
338 m_replacewindow
= NULL
;
341 SetStatusText(wxT("Splitter split vertically"), 1);
342 #endif // wxUSE_STATUSBAR
345 void MyFrame::OnUnsplit(wxCommandEvent
& WXUNUSED(event
) )
347 if ( m_splitter
->IsSplit() )
348 m_splitter
->Unsplit();
350 SetStatusText(wxT("No splitter"));
351 #endif // wxUSE_STATUSBAR
354 void MyFrame::ToggleFlag(int flag
, bool enable
)
356 long style
= m_splitter
->GetWindowStyleFlag();
362 m_splitter
->SetWindowStyleFlag(style
);
364 // we need to move sash to redraw it
365 int pos
= m_splitter
->GetSashPosition();
366 m_splitter
->SetSashPosition(pos
+ 1);
367 m_splitter
->SetSashPosition(pos
);
370 void MyFrame::OnSetPosition(wxCommandEvent
& WXUNUSED(event
) )
373 str
.Printf( wxT("%d"), m_splitter
->GetSashPosition());
375 str
= wxGetTextFromUser(wxT("Enter splitter position:"), wxT(""), str
, this);
381 if ( !str
.ToLong(&pos
) )
383 wxLogError(wxT("The splitter position should be an integer."));
387 m_splitter
->SetSashPosition(pos
);
389 wxLogStatus(this, wxT("Splitter position set to %ld"), pos
);
392 void MyFrame::OnSetMinSize(wxCommandEvent
& WXUNUSED(event
) )
395 str
.Printf( wxT("%d"), m_splitter
->GetMinimumPaneSize());
397 str
= wxGetTextFromUser(wxT("Enter minimal size for panes:"), wxT(""), str
, this);
402 int minsize
= wxStrtol( str
, (wxChar
**)NULL
, 10 );
403 m_splitter
->SetMinimumPaneSize(minsize
);
405 str
.Printf( wxT("Min pane size = %d"), minsize
);
406 SetStatusText(str
, 1);
407 #endif // wxUSE_STATUSBAR
410 void MyFrame::OnSetGravity(wxCommandEvent
& WXUNUSED(event
) )
413 str
.Printf( wxT("%g"), m_splitter
->GetSashGravity());
415 str
= wxGetTextFromUser(wxT("Enter sash gravity (0,1):"), wxT(""), str
, this);
420 double gravity
= wxStrtod( str
, (wxChar
**)NULL
);
421 m_splitter
->SetSashGravity(gravity
);
423 str
.Printf( wxT("Gravity = %g"), gravity
);
424 SetStatusText(str
, 1);
425 #endif // wxUSE_STATUSBAR
428 void MyFrame::OnReplace(wxCommandEvent
& WXUNUSED(event
) )
430 if (m_replacewindow
== NULL
) {
431 m_replacewindow
= m_splitter
->GetWindow2();
432 m_splitter
->ReplaceWindow(m_replacewindow
, new wxPanel(m_splitter
, wxID_ANY
));
433 m_replacewindow
->Hide();
435 wxWindow
*empty
= m_splitter
->GetWindow2();
436 wxASSERT(empty
!= m_replacewindow
);
437 m_splitter
->ReplaceWindow(empty
, m_replacewindow
);
438 m_replacewindow
->Show();
439 m_replacewindow
= NULL
;
444 void MyFrame::OnToggleInvisible(wxCommandEvent
& WXUNUSED(event
) )
446 m_splitter
->SetSashInvisible(!m_splitter
->IsSashInvisible());
447 m_splitter
->SizeWindows();
450 // Update UI handlers
452 void MyFrame::OnUpdateUIHorizontal(wxUpdateUIEvent
& event
)
454 event
.Enable( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_HORIZONTAL
) );
457 void MyFrame::OnUpdateUIVertical(wxUpdateUIEvent
& event
)
459 event
.Enable( ( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_VERTICAL
) ) );
462 void MyFrame::OnUpdateUIUnsplit(wxUpdateUIEvent
& event
)
464 event
.Enable( m_splitter
->IsSplit() );
467 void MyFrame::OnUpdateUIInvisible(wxUpdateUIEvent
& event
)
469 event
.Check( m_splitter
->IsSashInvisible() );
472 // ----------------------------------------------------------------------------
474 // ----------------------------------------------------------------------------
476 BEGIN_EVENT_TABLE(MySplitterWindow
, wxSplitterWindow
)
477 EVT_SPLITTER_SASH_POS_CHANGED(wxID_ANY
, MySplitterWindow::OnPositionChanged
)
478 EVT_SPLITTER_SASH_POS_CHANGING(wxID_ANY
, MySplitterWindow::OnPositionChanging
)
480 EVT_SPLITTER_DCLICK(wxID_ANY
, MySplitterWindow::OnDClick
)
482 EVT_SPLITTER_UNSPLIT(wxID_ANY
, MySplitterWindow::OnUnsplitEvent
)
485 MySplitterWindow::MySplitterWindow(wxFrame
*parent
)
486 : wxSplitterWindow(parent
, wxID_ANY
,
487 wxDefaultPosition
, wxDefaultSize
,
488 wxSP_3D
| wxSP_LIVE_UPDATE
|
489 wxCLIP_CHILDREN
/* | wxSP_NO_XP_THEME */ )
494 void MySplitterWindow::OnPositionChanged(wxSplitterEvent
& event
)
496 wxLogStatus(m_frame
, wxT("Position has changed, now = %d (or %d)"),
497 event
.GetSashPosition(), GetSashPosition());
502 void MySplitterWindow::OnPositionChanging(wxSplitterEvent
& event
)
504 wxLogStatus(m_frame
, wxT("Position is changing, now = %d (or %d)"),
505 event
.GetSashPosition(), GetSashPosition());
510 void MySplitterWindow::OnDClick(wxSplitterEvent
& event
)
513 m_frame
->SetStatusText(wxT("Splitter double clicked"), 1);
514 #endif // wxUSE_STATUSBAR
519 void MySplitterWindow::OnUnsplitEvent(wxSplitterEvent
& event
)
522 m_frame
->SetStatusText(wxT("Splitter unsplit"), 1);
523 #endif // wxUSE_STATUSBAR
528 // ----------------------------------------------------------------------------
530 // ----------------------------------------------------------------------------
532 MyCanvas::MyCanvas(wxWindow
* parent
, bool mirror
)
533 : wxScrolledWindow(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
534 wxHSCROLL
| wxVSCROLL
| wxNO_FULL_REPAINT_ON_RESIZE
)
537 SetScrollbars(20, 20, 5, 5);
540 void MyCanvas::OnDraw(wxDC
& dcOrig
)
542 wxMirrorDC
dc(dcOrig
, m_mirror
);
544 dc
.SetPen(*wxBLACK_PEN
);
545 dc
.DrawLine(0, 0, 100, 200);
547 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
548 dc
.DrawText(wxT("Testing"), 50, 50);
550 dc
.SetPen(*wxRED_PEN
);
551 dc
.SetBrush(*wxGREEN_BRUSH
);
552 dc
.DrawRectangle(120, 120, 100, 80);