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
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 class MyApp
: public wxApp
57 class MyFrame
: public wxFrame
64 void SplitHorizontal(wxCommandEvent
& event
);
65 void SplitVertical(wxCommandEvent
& event
);
66 void Unsplit(wxCommandEvent
& event
);
67 void SetMinSize(wxCommandEvent
& event
);
68 void Quit(wxCommandEvent
& event
);
70 // Menu command update functions
71 void UpdateUIHorizontal(wxUpdateUIEvent
& event
);
72 void UpdateUIVertical(wxUpdateUIEvent
& event
);
73 void UpdateUIUnsplit(wxUpdateUIEvent
& event
);
76 wxScrolledWindow
*m_left
, *m_right
;
78 wxSplitterWindow
* m_splitter
;
83 class MySplitterWindow
: public wxSplitterWindow
86 MySplitterWindow(wxFrame
*parent
);
89 void OnPositionChanged(wxSplitterEvent
& event
);
90 void OnPositionChanging(wxSplitterEvent
& event
);
91 void OnDClick(wxSplitterEvent
& event
);
92 void OnUnsplit(wxSplitterEvent
& event
);
100 class MyCanvas
: public wxScrolledWindow
103 MyCanvas(wxWindow
* parent
);
106 virtual void OnDraw(wxDC
& dc
);
109 // ============================================================================
111 // ============================================================================
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
121 // create and show the main frame
122 MyFrame
* frame
= new MyFrame
;
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
134 EVT_MENU(SPLIT_VERTICAL
, MyFrame::SplitVertical
)
135 EVT_MENU(SPLIT_HORIZONTAL
, MyFrame::SplitHorizontal
)
136 EVT_MENU(SPLIT_UNSPLIT
, MyFrame::Unsplit
)
137 EVT_MENU(SPLIT_QUIT
, MyFrame::Quit
)
138 EVT_MENU(SPLIT_SETMINSIZE
, MyFrame::SetMinSize
)
140 EVT_UPDATE_UI(SPLIT_VERTICAL
, MyFrame::UpdateUIVertical
)
141 EVT_UPDATE_UI(SPLIT_HORIZONTAL
, MyFrame::UpdateUIHorizontal
)
142 EVT_UPDATE_UI(SPLIT_UNSPLIT
, MyFrame::UpdateUIUnsplit
)
145 // My frame constructor
147 : wxFrame(NULL
, -1, _T("wxSplitterWindow sample"),
148 wxDefaultPosition
, wxSize(420, 300),
149 wxDEFAULT_FRAME_STYLE
| wxNO_FULL_REPAINT_ON_RESIZE
)
154 wxMenu
*fileMenu
= new wxMenu
;
155 fileMenu
->Append(SPLIT_VERTICAL
, _T("Split &Vertically\tCtrl-V"), _T("Split vertically"));
156 fileMenu
->Append(SPLIT_HORIZONTAL
, _T("Split &Horizontally\tCtrl-H"), _T("Split horizontally"));
157 fileMenu
->Append(SPLIT_UNSPLIT
, _T("&Unsplit\tCtrl-U"), _T("Unsplit"));
158 fileMenu
->AppendSeparator();
159 fileMenu
->Append(SPLIT_SETMINSIZE
, _T("Set &min size"), _T("Set minimum pane size"));
160 fileMenu
->AppendSeparator();
161 fileMenu
->Append(SPLIT_QUIT
, _T("E&xit\tAlt-X"), _T("Exit"));
163 wxMenuBar
*menuBar
= new wxMenuBar
;
164 menuBar
->Append(fileMenu
, _T("&File"));
168 m_splitter
= new MySplitterWindow(this);
171 m_left
= new MyCanvas(m_splitter
);
172 m_left
->SetBackgroundColour(*wxRED
);
173 m_left
->SetScrollbars(20, 20, 50, 50);
174 m_left
->SetCursor(wxCursor(wxCURSOR_MAGNIFIER
));
176 m_right
= new MyCanvas(m_splitter
);
177 m_right
->SetBackgroundColour(*wxCYAN
);
178 m_right
->SetScrollbars(20, 20, 50, 50);
179 #else // for testing kbd navigation inside the splitter
180 m_left
= new wxTextCtrl(m_splitter
, -1, _T("first text"));
181 m_right
= new wxTextCtrl(m_splitter
, -1, _T("second text"));
184 // you can also do this to start with a single window
186 m_right
->Show(FALSE
);
187 m_splitter
->Initialize(m_left
);
189 m_splitter
->SplitVertically(m_left
, m_right
, 100);
192 SetStatusText(_T("Min pane size = 0"), 1);
199 // menu command handlers
201 void MyFrame::Quit(wxCommandEvent
& WXUNUSED(event
) )
206 void MyFrame::SplitHorizontal(wxCommandEvent
& WXUNUSED(event
) )
208 if ( m_splitter
->IsSplit() )
209 m_splitter
->Unsplit();
212 m_splitter
->SplitHorizontally( m_left
, m_right
);
215 void MyFrame::SplitVertical(wxCommandEvent
& WXUNUSED(event
) )
217 if ( m_splitter
->IsSplit() )
218 m_splitter
->Unsplit();
221 m_splitter
->SplitVertically( m_left
, m_right
);
224 void MyFrame::Unsplit(wxCommandEvent
& WXUNUSED(event
) )
226 if ( m_splitter
->IsSplit() )
227 m_splitter
->Unsplit();
228 SetStatusText(_T("No splitter"));
231 void MyFrame::SetMinSize(wxCommandEvent
& WXUNUSED(event
) )
234 str
.Printf( _T(_T("%d")), m_splitter
->GetMinimumPaneSize());
235 str
= wxGetTextFromUser(_T("Enter minimal size for panes:"), _T(""), str
, this);
239 int minsize
= wxStrtol( str
, (wxChar
**)NULL
, 10 );
240 m_splitter
->SetMinimumPaneSize(minsize
);
241 str
.Printf( _T(_T("Min pane size = %d")), minsize
);
242 SetStatusText(str
, 1);
245 // Update UI handlers
247 void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent
& event
)
249 event
.Enable( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_HORIZONTAL
) );
252 void MyFrame::UpdateUIVertical(wxUpdateUIEvent
& event
)
254 event
.Enable( ( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_VERTICAL
) ) );
257 void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent
& event
)
259 event
.Enable( m_splitter
->IsSplit() );
262 // ----------------------------------------------------------------------------
264 // ----------------------------------------------------------------------------
266 BEGIN_EVENT_TABLE(MySplitterWindow
, wxSplitterWindow
)
267 EVT_SPLITTER_SASH_POS_CHANGED(-1, MySplitterWindow::OnPositionChanged
)
268 EVT_SPLITTER_SASH_POS_CHANGING(-1, MySplitterWindow::OnPositionChanging
)
270 EVT_SPLITTER_DCLICK(-1, MySplitterWindow::OnDClick
)
272 EVT_SPLITTER_UNSPLIT(-1, MySplitterWindow::OnUnsplit
)
275 MySplitterWindow::MySplitterWindow(wxFrame
*parent
)
276 : wxSplitterWindow(parent
, -1,
277 wxDefaultPosition
, wxDefaultSize
,
278 wxSP_3D
| wxSP_LIVE_UPDATE
| wxCLIP_CHILDREN
)
283 void MySplitterWindow::OnPositionChanged(wxSplitterEvent
& event
)
285 wxLogStatus(m_frame
, _T("Position has changed, now = %d (or %d)"),
286 event
.GetSashPosition(), GetSashPosition());
291 void MySplitterWindow::OnPositionChanging(wxSplitterEvent
& event
)
293 wxLogStatus(m_frame
, _T("Position is changing, now = %d (or %d)"),
294 event
.GetSashPosition(), GetSashPosition());
299 void MySplitterWindow::OnDClick(wxSplitterEvent
& event
)
301 m_frame
->SetStatusText(_T("Splitter double clicked"), 1);
306 void MySplitterWindow::OnUnsplit(wxSplitterEvent
& event
)
308 m_frame
->SetStatusText(_T("Splitter unsplit"), 1);
313 // ----------------------------------------------------------------------------
315 // ----------------------------------------------------------------------------
317 MyCanvas::MyCanvas(wxWindow
* parent
)
318 : wxScrolledWindow(parent
, -1)
322 MyCanvas::~MyCanvas()
326 void MyCanvas::OnDraw(wxDC
& dc
)
328 dc
.SetPen(*wxBLACK_PEN
);
329 dc
.DrawLine(0, 0, 100, 100);
331 dc
.SetBackgroundMode(wxTRANSPARENT
);
332 dc
.DrawText(_T("Testing"), 50, 50);
334 dc
.SetPen(*wxRED_PEN
);
335 dc
.SetBrush(*wxGREEN_BRUSH
);
336 dc
.DrawRectangle(120, 120, 100, 80);