1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSplitterWindow sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
31 #include "wx/splitter.h"
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 // ID for the menu commands
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 class MyApp
: public wxApp
58 class MyFrame
: public wxFrame
65 void SplitHorizontal(wxCommandEvent
& event
);
66 void SplitVertical(wxCommandEvent
& event
);
67 void Unsplit(wxCommandEvent
& event
);
68 void ToggleLive(wxCommandEvent
& event
);
69 void SetMinSize(wxCommandEvent
& event
);
71 void Quit(wxCommandEvent
& event
);
73 // Menu command update functions
74 void UpdateUIHorizontal(wxUpdateUIEvent
& event
);
75 void UpdateUIVertical(wxUpdateUIEvent
& event
);
76 void UpdateUIUnsplit(wxUpdateUIEvent
& event
);
79 wxScrolledWindow
*m_left
, *m_right
;
81 wxSplitterWindow
* m_splitter
;
86 class MySplitterWindow
: public wxSplitterWindow
89 MySplitterWindow(wxFrame
*parent
);
92 void OnPositionChanged(wxSplitterEvent
& event
);
93 void OnPositionChanging(wxSplitterEvent
& event
);
94 void OnDClick(wxSplitterEvent
& event
);
95 void OnUnsplit(wxSplitterEvent
& event
);
100 DECLARE_EVENT_TABLE()
103 class MyCanvas
: public wxScrolledWindow
106 MyCanvas(wxWindow
* parent
);
109 virtual void OnDraw(wxDC
& dc
);
112 // ============================================================================
114 // ============================================================================
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
124 // create and show the main frame
125 MyFrame
* frame
= new MyFrame
;
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
137 EVT_MENU(SPLIT_VERTICAL
, MyFrame::SplitVertical
)
138 EVT_MENU(SPLIT_HORIZONTAL
, MyFrame::SplitHorizontal
)
139 EVT_MENU(SPLIT_UNSPLIT
, MyFrame::Unsplit
)
140 EVT_MENU(SPLIT_LIVE
, MyFrame::ToggleLive
)
141 EVT_MENU(SPLIT_SETMINSIZE
, MyFrame::SetMinSize
)
143 EVT_MENU(SPLIT_QUIT
, MyFrame::Quit
)
145 EVT_UPDATE_UI(SPLIT_VERTICAL
, MyFrame::UpdateUIVertical
)
146 EVT_UPDATE_UI(SPLIT_HORIZONTAL
, MyFrame::UpdateUIHorizontal
)
147 EVT_UPDATE_UI(SPLIT_UNSPLIT
, MyFrame::UpdateUIUnsplit
)
150 // My frame constructor
152 : wxFrame(NULL
, -1, _T("wxSplitterWindow sample"),
153 wxDefaultPosition
, wxSize(420, 300),
154 wxDEFAULT_FRAME_STYLE
| wxNO_FULL_REPAINT_ON_RESIZE
)
159 wxMenu
*fileMenu
= new wxMenu
;
160 fileMenu
->Append(SPLIT_VERTICAL
, _T("Split &Vertically\tCtrl-V"), _T("Split vertically"));
161 fileMenu
->Append(SPLIT_HORIZONTAL
, _T("Split &Horizontally\tCtrl-H"), _T("Split horizontally"));
162 fileMenu
->Append(SPLIT_UNSPLIT
, _T("&Unsplit\tCtrl-U"), _T("Unsplit"));
163 fileMenu
->AppendSeparator();
164 fileMenu
->Append(SPLIT_LIVE
, _T("&Live update"), _T("Toggle live update mode"), TRUE
);
165 fileMenu
->Append(SPLIT_SETMINSIZE
, _T("Set &min size"), _T("Set minimum pane size"));
166 fileMenu
->AppendSeparator();
167 fileMenu
->Append(SPLIT_QUIT
, _T("E&xit\tAlt-X"), _T("Exit"));
169 wxMenuBar
*menuBar
= new wxMenuBar
;
170 menuBar
->Append(fileMenu
, _T("&File"));
174 menuBar
->Check(SPLIT_LIVE
, TRUE
);
175 m_splitter
= new MySplitterWindow(this);
178 m_left
= new MyCanvas(m_splitter
);
179 m_left
->SetBackgroundColour(*wxRED
);
180 m_left
->SetScrollbars(20, 20, 50, 50);
181 m_left
->SetCursor(wxCursor(wxCURSOR_MAGNIFIER
));
183 m_right
= new MyCanvas(m_splitter
);
184 m_right
->SetBackgroundColour(*wxCYAN
);
185 m_right
->SetScrollbars(20, 20, 50, 50);
186 #else // for testing kbd navigation inside the splitter
187 m_left
= new wxTextCtrl(m_splitter
, -1, _T("first text"));
188 m_right
= new wxTextCtrl(m_splitter
, -1, _T("second text"));
191 // you can also do this to start with a single window
193 m_right
->Show(FALSE
);
194 m_splitter
->Initialize(m_left
);
196 // you can also try -100
197 m_splitter
->SplitVertically(m_left
, m_right
, 100);
200 SetStatusText(_T("Min pane size = 0"), 1);
207 // menu command handlers
209 void MyFrame::Quit(wxCommandEvent
& WXUNUSED(event
) )
214 void MyFrame::SplitHorizontal(wxCommandEvent
& WXUNUSED(event
) )
216 if ( m_splitter
->IsSplit() )
217 m_splitter
->Unsplit();
220 m_splitter
->SplitHorizontally( m_left
, m_right
);
222 SetStatusText(_T("Splitter split horizontally"), 1);
225 void MyFrame::SplitVertical(wxCommandEvent
& WXUNUSED(event
) )
227 if ( m_splitter
->IsSplit() )
228 m_splitter
->Unsplit();
231 m_splitter
->SplitVertically( m_left
, m_right
);
233 SetStatusText(_T("Splitter split vertically"), 1);
236 void MyFrame::Unsplit(wxCommandEvent
& WXUNUSED(event
) )
238 if ( m_splitter
->IsSplit() )
239 m_splitter
->Unsplit();
240 SetStatusText(_T("No splitter"));
243 void MyFrame::ToggleLive(wxCommandEvent
& event
)
245 long style
= m_splitter
->GetWindowStyleFlag();
246 if ( event
.IsChecked() )
247 style
|= wxSP_LIVE_UPDATE
;
249 style
&= ~wxSP_LIVE_UPDATE
;
251 m_splitter
->SetWindowStyleFlag(style
);
254 void MyFrame::SetMinSize(wxCommandEvent
& WXUNUSED(event
) )
257 str
.Printf( wxT("%d"), m_splitter
->GetMinimumPaneSize());
258 str
= wxGetTextFromUser(_T("Enter minimal size for panes:"), _T(""), str
, this);
262 int minsize
= wxStrtol( str
, (wxChar
**)NULL
, 10 );
263 m_splitter
->SetMinimumPaneSize(minsize
);
264 str
.Printf( wxT("Min pane size = %d"), minsize
);
265 SetStatusText(str
, 1);
268 // Update UI handlers
270 void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent
& event
)
272 event
.Enable( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_HORIZONTAL
) );
275 void MyFrame::UpdateUIVertical(wxUpdateUIEvent
& event
)
277 event
.Enable( ( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_VERTICAL
) ) );
280 void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent
& event
)
282 event
.Enable( m_splitter
->IsSplit() );
285 // ----------------------------------------------------------------------------
287 // ----------------------------------------------------------------------------
289 BEGIN_EVENT_TABLE(MySplitterWindow
, wxSplitterWindow
)
290 EVT_SPLITTER_SASH_POS_CHANGED(-1, MySplitterWindow::OnPositionChanged
)
291 EVT_SPLITTER_SASH_POS_CHANGING(-1, MySplitterWindow::OnPositionChanging
)
293 EVT_SPLITTER_DCLICK(-1, MySplitterWindow::OnDClick
)
295 EVT_SPLITTER_UNSPLIT(-1, MySplitterWindow::OnUnsplit
)
298 MySplitterWindow::MySplitterWindow(wxFrame
*parent
)
299 : wxSplitterWindow(parent
, -1,
300 wxDefaultPosition
, wxDefaultSize
,
301 wxSP_3D
| wxSP_LIVE_UPDATE
| wxCLIP_CHILDREN
)
306 void MySplitterWindow::OnPositionChanged(wxSplitterEvent
& event
)
308 wxLogStatus(m_frame
, _T("Position has changed, now = %d (or %d)"),
309 event
.GetSashPosition(), GetSashPosition());
314 void MySplitterWindow::OnPositionChanging(wxSplitterEvent
& event
)
316 wxLogStatus(m_frame
, _T("Position is changing, now = %d (or %d)"),
317 event
.GetSashPosition(), GetSashPosition());
322 void MySplitterWindow::OnDClick(wxSplitterEvent
& event
)
324 m_frame
->SetStatusText(_T("Splitter double clicked"), 1);
329 void MySplitterWindow::OnUnsplit(wxSplitterEvent
& event
)
331 m_frame
->SetStatusText(_T("Splitter unsplit"), 1);
336 // ----------------------------------------------------------------------------
338 // ----------------------------------------------------------------------------
340 MyCanvas::MyCanvas(wxWindow
* parent
)
341 : wxScrolledWindow(parent
, -1)
345 MyCanvas::~MyCanvas()
349 void MyCanvas::OnDraw(wxDC
& dc
)
351 dc
.SetPen(*wxBLACK_PEN
);
352 dc
.DrawLine(0, 0, 100, 100);
354 dc
.SetBackgroundMode(wxTRANSPARENT
);
355 dc
.DrawText(_T("Testing"), 50, 50);
357 dc
.SetPen(*wxRED_PEN
);
358 dc
.SetBrush(*wxGREEN_BRUSH
);
359 dc
.DrawRectangle(120, 120, 100, 80);