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 // you can also try -100
190 m_splitter
->SplitVertically(m_left
, m_right
, 100);
193 SetStatusText(_T("Min pane size = 0"), 1);
200 // menu command handlers
202 void MyFrame::Quit(wxCommandEvent
& WXUNUSED(event
) )
207 void MyFrame::SplitHorizontal(wxCommandEvent
& WXUNUSED(event
) )
209 if ( m_splitter
->IsSplit() )
210 m_splitter
->Unsplit();
213 m_splitter
->SplitHorizontally( m_left
, m_right
);
215 SetStatusText(_T("Splitter split horizontally"), 1);
218 void MyFrame::SplitVertical(wxCommandEvent
& WXUNUSED(event
) )
220 if ( m_splitter
->IsSplit() )
221 m_splitter
->Unsplit();
224 m_splitter
->SplitVertically( m_left
, m_right
);
226 SetStatusText(_T("Splitter split vertically"), 1);
229 void MyFrame::Unsplit(wxCommandEvent
& WXUNUSED(event
) )
231 if ( m_splitter
->IsSplit() )
232 m_splitter
->Unsplit();
233 SetStatusText(_T("No splitter"));
236 void MyFrame::SetMinSize(wxCommandEvent
& WXUNUSED(event
) )
239 str
.Printf( _T(_T("%d")), m_splitter
->GetMinimumPaneSize());
240 str
= wxGetTextFromUser(_T("Enter minimal size for panes:"), _T(""), str
, this);
244 int minsize
= wxStrtol( str
, (wxChar
**)NULL
, 10 );
245 m_splitter
->SetMinimumPaneSize(minsize
);
246 str
.Printf( _T(_T("Min pane size = %d")), minsize
);
247 SetStatusText(str
, 1);
250 // Update UI handlers
252 void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent
& event
)
254 event
.Enable( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_HORIZONTAL
) );
257 void MyFrame::UpdateUIVertical(wxUpdateUIEvent
& event
)
259 event
.Enable( ( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_VERTICAL
) ) );
262 void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent
& event
)
264 event
.Enable( m_splitter
->IsSplit() );
267 // ----------------------------------------------------------------------------
269 // ----------------------------------------------------------------------------
271 BEGIN_EVENT_TABLE(MySplitterWindow
, wxSplitterWindow
)
272 EVT_SPLITTER_SASH_POS_CHANGED(-1, MySplitterWindow::OnPositionChanged
)
273 EVT_SPLITTER_SASH_POS_CHANGING(-1, MySplitterWindow::OnPositionChanging
)
275 EVT_SPLITTER_DCLICK(-1, MySplitterWindow::OnDClick
)
277 EVT_SPLITTER_UNSPLIT(-1, MySplitterWindow::OnUnsplit
)
280 MySplitterWindow::MySplitterWindow(wxFrame
*parent
)
281 : wxSplitterWindow(parent
, -1,
282 wxDefaultPosition
, wxDefaultSize
,
283 wxSP_3D
| wxSP_LIVE_UPDATE
| wxCLIP_CHILDREN
)
288 void MySplitterWindow::OnPositionChanged(wxSplitterEvent
& event
)
290 wxLogStatus(m_frame
, _T("Position has changed, now = %d (or %d)"),
291 event
.GetSashPosition(), GetSashPosition());
296 void MySplitterWindow::OnPositionChanging(wxSplitterEvent
& event
)
298 wxLogStatus(m_frame
, _T("Position is changing, now = %d (or %d)"),
299 event
.GetSashPosition(), GetSashPosition());
304 void MySplitterWindow::OnDClick(wxSplitterEvent
& event
)
306 m_frame
->SetStatusText(_T("Splitter double clicked"), 1);
311 void MySplitterWindow::OnUnsplit(wxSplitterEvent
& event
)
313 m_frame
->SetStatusText(_T("Splitter unsplit"), 1);
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
322 MyCanvas::MyCanvas(wxWindow
* parent
)
323 : wxScrolledWindow(parent
, -1)
327 MyCanvas::~MyCanvas()
331 void MyCanvas::OnDraw(wxDC
& dc
)
333 dc
.SetPen(*wxBLACK_PEN
);
334 dc
.DrawLine(0, 0, 100, 100);
336 dc
.SetBackgroundMode(wxTRANSPARENT
);
337 dc
.DrawText(_T("Testing"), 50, 50);
339 dc
.SetPen(*wxRED_PEN
);
340 dc
.SetBrush(*wxGREEN_BRUSH
);
341 dc
.DrawRectangle(120, 120, 100, 80);