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 wxScrolledWindow
*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
->SetScrollbars(20, 20, 5, 5);
280 m_left
->SetCursor(wxCursor(wxCURSOR_MAGNIFIER
));
282 m_right
= new MyCanvas(m_splitter
, false);
283 m_right
->SetBackgroundColour(*wxCYAN
);
284 m_right
->SetScrollbars(20, 20, 5, 5);
285 #else // for testing kbd navigation inside the splitter
286 m_left
= new wxTextCtrl(m_splitter
, wxID_ANY
, wxT("first text"));
287 m_right
= new wxTextCtrl(m_splitter
, wxID_ANY
, wxT("second text"));
290 // you can also do this to start with a single window
292 m_right
->Show(false);
293 m_splitter
->Initialize(m_left
);
295 // you can also try -100
296 m_splitter
->SplitVertically(m_left
, m_right
, 100);
300 SetStatusText(wxT("Min pane size = 0"), 1);
301 #endif // wxUSE_STATUSBAR
303 m_replacewindow
= NULL
;
308 if (m_replacewindow
) {
309 m_replacewindow
->Destroy();
313 // menu command handlers
315 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
320 void MyFrame::OnSplitHorizontal(wxCommandEvent
& WXUNUSED(event
) )
322 if ( m_splitter
->IsSplit() )
323 m_splitter
->Unsplit();
326 m_splitter
->SplitHorizontally( m_left
, m_right
);
327 m_replacewindow
= NULL
;
330 SetStatusText(wxT("Splitter split horizontally"), 1);
331 #endif // wxUSE_STATUSBAR
334 void MyFrame::OnSplitVertical(wxCommandEvent
& WXUNUSED(event
) )
336 if ( m_splitter
->IsSplit() )
337 m_splitter
->Unsplit();
340 m_splitter
->SplitVertically( m_left
, m_right
);
341 m_replacewindow
= NULL
;
344 SetStatusText(wxT("Splitter split vertically"), 1);
345 #endif // wxUSE_STATUSBAR
348 void MyFrame::OnUnsplit(wxCommandEvent
& WXUNUSED(event
) )
350 if ( m_splitter
->IsSplit() )
351 m_splitter
->Unsplit();
353 SetStatusText(wxT("No splitter"));
354 #endif // wxUSE_STATUSBAR
357 void MyFrame::ToggleFlag(int flag
, bool enable
)
359 long style
= m_splitter
->GetWindowStyleFlag();
365 m_splitter
->SetWindowStyleFlag(style
);
367 // we need to move sash to redraw it
368 int pos
= m_splitter
->GetSashPosition();
369 m_splitter
->SetSashPosition(pos
+ 1);
370 m_splitter
->SetSashPosition(pos
);
373 void MyFrame::OnSetPosition(wxCommandEvent
& WXUNUSED(event
) )
376 str
.Printf( wxT("%d"), m_splitter
->GetSashPosition());
378 str
= wxGetTextFromUser(wxT("Enter splitter position:"), wxT(""), str
, this);
384 if ( !str
.ToLong(&pos
) )
386 wxLogError(wxT("The splitter position should be an integer."));
390 m_splitter
->SetSashPosition(pos
);
392 wxLogStatus(this, wxT("Splitter position set to %ld"), pos
);
395 void MyFrame::OnSetMinSize(wxCommandEvent
& WXUNUSED(event
) )
398 str
.Printf( wxT("%d"), m_splitter
->GetMinimumPaneSize());
400 str
= wxGetTextFromUser(wxT("Enter minimal size for panes:"), wxT(""), str
, this);
405 int minsize
= wxStrtol( str
, (wxChar
**)NULL
, 10 );
406 m_splitter
->SetMinimumPaneSize(minsize
);
408 str
.Printf( wxT("Min pane size = %d"), minsize
);
409 SetStatusText(str
, 1);
410 #endif // wxUSE_STATUSBAR
413 void MyFrame::OnSetGravity(wxCommandEvent
& WXUNUSED(event
) )
416 str
.Printf( wxT("%g"), m_splitter
->GetSashGravity());
418 str
= wxGetTextFromUser(wxT("Enter sash gravity (0,1):"), wxT(""), str
, this);
423 double gravity
= wxStrtod( str
, (wxChar
**)NULL
);
424 m_splitter
->SetSashGravity(gravity
);
426 str
.Printf( wxT("Gravity = %g"), gravity
);
427 SetStatusText(str
, 1);
428 #endif // wxUSE_STATUSBAR
431 void MyFrame::OnReplace(wxCommandEvent
& WXUNUSED(event
) )
433 if (m_replacewindow
== NULL
) {
434 m_replacewindow
= m_splitter
->GetWindow2();
435 m_splitter
->ReplaceWindow(m_replacewindow
, new wxPanel(m_splitter
, wxID_ANY
));
436 m_replacewindow
->Hide();
438 wxWindow
*empty
= m_splitter
->GetWindow2();
439 wxASSERT(empty
!= m_replacewindow
);
440 m_splitter
->ReplaceWindow(empty
, m_replacewindow
);
441 m_replacewindow
->Show();
442 m_replacewindow
= NULL
;
447 void MyFrame::OnToggleInvisible(wxCommandEvent
& WXUNUSED(event
) )
449 m_splitter
->SetSashInvisible(!m_splitter
->IsSashInvisible());
450 m_splitter
->SizeWindows();
453 // Update UI handlers
455 void MyFrame::OnUpdateUIHorizontal(wxUpdateUIEvent
& event
)
457 event
.Enable( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_HORIZONTAL
) );
460 void MyFrame::OnUpdateUIVertical(wxUpdateUIEvent
& event
)
462 event
.Enable( ( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_VERTICAL
) ) );
465 void MyFrame::OnUpdateUIUnsplit(wxUpdateUIEvent
& event
)
467 event
.Enable( m_splitter
->IsSplit() );
470 void MyFrame::OnUpdateUIInvisible(wxUpdateUIEvent
& event
)
472 event
.Check( m_splitter
->IsSashInvisible() );
475 // ----------------------------------------------------------------------------
477 // ----------------------------------------------------------------------------
479 BEGIN_EVENT_TABLE(MySplitterWindow
, wxSplitterWindow
)
480 EVT_SPLITTER_SASH_POS_CHANGED(wxID_ANY
, MySplitterWindow::OnPositionChanged
)
481 EVT_SPLITTER_SASH_POS_CHANGING(wxID_ANY
, MySplitterWindow::OnPositionChanging
)
483 EVT_SPLITTER_DCLICK(wxID_ANY
, MySplitterWindow::OnDClick
)
485 EVT_SPLITTER_UNSPLIT(wxID_ANY
, MySplitterWindow::OnUnsplitEvent
)
488 MySplitterWindow::MySplitterWindow(wxFrame
*parent
)
489 : wxSplitterWindow(parent
, wxID_ANY
,
490 wxDefaultPosition
, wxDefaultSize
,
491 wxSP_3D
| wxSP_LIVE_UPDATE
|
492 wxCLIP_CHILDREN
/* | wxSP_NO_XP_THEME */ )
497 void MySplitterWindow::OnPositionChanged(wxSplitterEvent
& event
)
499 wxLogStatus(m_frame
, wxT("Position has changed, now = %d (or %d)"),
500 event
.GetSashPosition(), GetSashPosition());
505 void MySplitterWindow::OnPositionChanging(wxSplitterEvent
& event
)
507 wxLogStatus(m_frame
, wxT("Position is changing, now = %d (or %d)"),
508 event
.GetSashPosition(), GetSashPosition());
513 void MySplitterWindow::OnDClick(wxSplitterEvent
& event
)
516 m_frame
->SetStatusText(wxT("Splitter double clicked"), 1);
517 #endif // wxUSE_STATUSBAR
522 void MySplitterWindow::OnUnsplitEvent(wxSplitterEvent
& event
)
525 m_frame
->SetStatusText(wxT("Splitter unsplit"), 1);
526 #endif // wxUSE_STATUSBAR
531 // ----------------------------------------------------------------------------
533 // ----------------------------------------------------------------------------
535 MyCanvas::MyCanvas(wxWindow
* parent
, bool mirror
)
536 : wxScrolledWindow(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
537 wxHSCROLL
| wxVSCROLL
| wxNO_FULL_REPAINT_ON_RESIZE
)
542 void MyCanvas::OnDraw(wxDC
& dcOrig
)
544 wxMirrorDC
dc(dcOrig
, m_mirror
);
546 dc
.SetPen(*wxBLACK_PEN
);
547 dc
.DrawLine(0, 0, 100, 200);
549 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
550 dc
.DrawText(wxT("Testing"), 50, 50);
552 dc
.SetPen(*wxRED_PEN
);
553 dc
.SetBrush(*wxGREEN_BRUSH
);
554 dc
.DrawRectangle(120, 120, 100, 80);