]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
6718d773 | 2 | // Name: splitter.cpp |
c801d85f KB |
3 | // Purpose: wxSplitterWindow sample |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
0d559d69 | 9 | // Licence: wxWindows license |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // For compilers that support precompilation, includes "wx/wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/wx.h" | |
21 | #endif | |
22 | ||
23 | #include "wx/splitter.h" | |
24 | ||
25 | class MyApp; | |
26 | class MyFrame; | |
27 | class MyCanvas; | |
28 | ||
29 | class MyApp: public wxApp | |
30 | { | |
31 | public: | |
0d559d69 | 32 | bool OnInit(); |
c801d85f KB |
33 | }; |
34 | ||
0d559d69 | 35 | class MySplitterWindow : public wxSplitterWindow |
c801d85f KB |
36 | { |
37 | public: | |
4a0b46a7 | 38 | MySplitterWindow(wxFrame *parent, wxWindowID id) |
fe31f91c VZ |
39 | : wxSplitterWindow(parent, id, wxDefaultPosition, wxDefaultSize, |
40 | wxSP_3D | wxSP_LIVE_UPDATE | wxCLIP_CHILDREN) | |
0d559d69 VZ |
41 | { |
42 | m_frame = parent; | |
43 | } | |
44 | ||
45 | virtual bool OnSashPositionChange(int newSashPosition) | |
46 | { | |
47 | if ( !wxSplitterWindow::OnSashPositionChange(newSashPosition) ) | |
48 | return FALSE; | |
4a0b46a7 | 49 | |
0d559d69 | 50 | wxString str; |
1e133b7d | 51 | str.Printf( _T("Sash position = %d"), newSashPosition); |
0d559d69 VZ |
52 | m_frame->SetStatusText(str); |
53 | ||
54 | return TRUE; | |
55 | } | |
4a0b46a7 | 56 | |
0d559d69 VZ |
57 | private: |
58 | wxFrame *m_frame; | |
59 | }; | |
c801d85f | 60 | |
0d559d69 VZ |
61 | class MyFrame: public wxFrame |
62 | { | |
63 | public: | |
64 | MyFrame(wxFrame* frame, const wxString& title, const wxPoint& pos, const wxSize& size); | |
65 | virtual ~MyFrame(); | |
c801d85f | 66 | |
0d559d69 VZ |
67 | // Menu commands |
68 | void SplitHorizontal(wxCommandEvent& event); | |
69 | void SplitVertical(wxCommandEvent& event); | |
70 | void Unsplit(wxCommandEvent& event); | |
71 | void SetMinSize(wxCommandEvent& event); | |
72 | void Quit(wxCommandEvent& event); | |
c801d85f | 73 | |
0d559d69 VZ |
74 | // Menu command update functions |
75 | void UpdateUIHorizontal(wxUpdateUIEvent& event); | |
76 | void UpdateUIVertical(wxUpdateUIEvent& event); | |
77 | void UpdateUIUnsplit(wxUpdateUIEvent& event); | |
c801d85f KB |
78 | |
79 | private: | |
0d559d69 VZ |
80 | void UpdatePosition(); |
81 | ||
80372768 | 82 | wxScrolledWindow *m_left, *m_right; |
6b55490a | 83 | |
0d559d69 | 84 | MySplitterWindow* m_splitter; |
c801d85f | 85 | |
6b55490a | 86 | DECLARE_EVENT_TABLE() |
c801d85f KB |
87 | }; |
88 | ||
89 | class MyCanvas: public wxScrolledWindow | |
90 | { | |
91 | public: | |
33611ebb RR |
92 | MyCanvas(wxWindow* parent, wxWindowID id = -1, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, const wxString& name = ""); |
93 | virtual ~MyCanvas(); | |
c801d85f | 94 | |
0d559d69 | 95 | virtual void OnDraw(wxDC& dc); |
c801d85f KB |
96 | |
97 | DECLARE_EVENT_TABLE() | |
98 | }; | |
99 | ||
100 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) | |
101 | END_EVENT_TABLE() | |
102 | ||
0d559d69 VZ |
103 | // ID for the menu commands |
104 | enum | |
105 | { | |
106 | SPLIT_QUIT, | |
107 | SPLIT_HORIZONTAL, | |
108 | SPLIT_VERTICAL, | |
109 | SPLIT_UNSPLIT, | |
110 | SPLIT_SETMINSIZE | |
111 | }; | |
c801d85f | 112 | |
0d57be45 JS |
113 | // Window ids |
114 | #define SPLITTER_WINDOW 100 | |
115 | #define SPLITTER_FRAME 101 | |
116 | #define CANVAS1 102 | |
117 | #define CANVAS2 103 | |
118 | ||
c801d85f KB |
119 | IMPLEMENT_APP(MyApp) |
120 | ||
121 | bool MyApp::OnInit(void) | |
122 | { | |
0d559d69 VZ |
123 | MyFrame* frame = new MyFrame((wxFrame *) NULL, "wxSplitterWindow Example", |
124 | wxPoint(50, 50), wxSize(420, 300)); | |
c801d85f | 125 | |
0d559d69 VZ |
126 | // Show the frame |
127 | frame->Show(TRUE); | |
4a0b46a7 | 128 | |
0d559d69 | 129 | SetTopWindow(frame); |
c801d85f | 130 | |
0d559d69 | 131 | return TRUE; |
c801d85f KB |
132 | } |
133 | ||
134 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
0d559d69 VZ |
135 | EVT_MENU(SPLIT_VERTICAL, MyFrame::SplitVertical) |
136 | EVT_MENU(SPLIT_HORIZONTAL, MyFrame::SplitHorizontal) | |
137 | EVT_MENU(SPLIT_UNSPLIT, MyFrame::Unsplit) | |
138 | EVT_MENU(SPLIT_QUIT, MyFrame::Quit) | |
139 | EVT_MENU(SPLIT_SETMINSIZE, MyFrame::SetMinSize) | |
140 | ||
141 | EVT_UPDATE_UI(SPLIT_VERTICAL, MyFrame::UpdateUIVertical) | |
142 | EVT_UPDATE_UI(SPLIT_HORIZONTAL, MyFrame::UpdateUIHorizontal) | |
143 | EVT_UPDATE_UI(SPLIT_UNSPLIT, MyFrame::UpdateUIUnsplit) | |
c801d85f KB |
144 | END_EVENT_TABLE() |
145 | ||
146 | // My frame constructor | |
fe31f91c VZ |
147 | MyFrame::MyFrame(wxFrame* frame, const wxString& title, |
148 | const wxPoint& pos, const wxSize& size) | |
149 | : wxFrame(frame, SPLITTER_FRAME, title, pos, size, | |
150 | wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE) | |
c801d85f | 151 | { |
0d559d69 | 152 | CreateStatusBar(2); |
b7346a70 | 153 | |
0d559d69 | 154 | // Make a menubar |
6b55490a | 155 | wxMenu *fileMenu = new wxMenu; |
8b8bff20 VZ |
156 | fileMenu->Append(SPLIT_VERTICAL, "Split &Vertically\tCtrl-V", "Split vertically"); |
157 | fileMenu->Append(SPLIT_HORIZONTAL, "Split &Horizontally\tCtrl-H", "Split horizontally"); | |
158 | fileMenu->Append(SPLIT_UNSPLIT, "&Unsplit\tCtrl-U", "Unsplit"); | |
0d559d69 VZ |
159 | fileMenu->AppendSeparator(); |
160 | fileMenu->Append(SPLIT_SETMINSIZE, "Set &min size", "Set minimum pane size"); | |
161 | fileMenu->AppendSeparator(); | |
8b8bff20 | 162 | fileMenu->Append(SPLIT_QUIT, "E&xit\tAlt-X", "Exit"); |
c801d85f | 163 | |
6b55490a | 164 | wxMenuBar *menuBar = new wxMenuBar; |
0d559d69 | 165 | menuBar->Append(fileMenu, "&File"); |
c801d85f | 166 | |
0d559d69 | 167 | SetMenuBar(menuBar); |
c801d85f | 168 | |
0d559d69 | 169 | m_splitter = new MySplitterWindow(this, SPLITTER_WINDOW); |
4a0b46a7 | 170 | |
6b55490a VZ |
171 | #if 1 |
172 | m_left = new MyCanvas(m_splitter, CANVAS1, wxPoint(0, 0), wxSize(400, 400), "Test1" ); | |
173 | m_left->SetBackgroundColour(*wxRED); | |
174 | m_left->SetScrollbars(20, 20, 50, 50); | |
175 | m_left->SetCursor(wxCursor(wxCURSOR_MAGNIFIER)); | |
176 | ||
177 | m_right = new MyCanvas(m_splitter, CANVAS2, wxPoint(0, 0), wxSize(400, 400), "Test2" ); | |
178 | m_right->SetBackgroundColour(*wxCYAN); | |
179 | m_right->SetScrollbars(20, 20, 50, 50); | |
180 | #else // for testing kbd navigation inside the splitter | |
181 | m_left = new wxTextCtrl(m_splitter, -1, "first text"); | |
182 | m_right = new wxTextCtrl(m_splitter, -1, "second text"); | |
183 | #endif | |
c801d85f | 184 | |
4a0b46a7 VZ |
185 | // you can also do this to start with a single window |
186 | #if 0 | |
6b55490a VZ |
187 | m_right->Show(FALSE); |
188 | m_splitter->Initialize(m_left); | |
4a0b46a7 | 189 | #else |
6b55490a | 190 | m_splitter->SplitVertically(m_left, m_right, 100); |
4a0b46a7 VZ |
191 | #endif |
192 | ||
0d559d69 | 193 | SetStatusText("Min pane size = 0", 1); |
c801d85f KB |
194 | } |
195 | ||
196 | MyFrame::~MyFrame() | |
197 | { | |
198 | } | |
199 | ||
e3e65dac | 200 | void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) ) |
c801d85f | 201 | { |
0d559d69 | 202 | Close(TRUE); |
c801d85f KB |
203 | } |
204 | ||
e3e65dac | 205 | void MyFrame::SplitHorizontal(wxCommandEvent& WXUNUSED(event) ) |
c801d85f | 206 | { |
0d559d69 VZ |
207 | if ( m_splitter->IsSplit() ) |
208 | m_splitter->Unsplit(); | |
6b55490a VZ |
209 | m_left->Show(TRUE); |
210 | m_right->Show(TRUE); | |
211 | m_splitter->SplitHorizontally( m_left, m_right ); | |
0d559d69 | 212 | UpdatePosition(); |
c801d85f KB |
213 | } |
214 | ||
e3e65dac | 215 | void MyFrame::SplitVertical(wxCommandEvent& WXUNUSED(event) ) |
c801d85f | 216 | { |
0d559d69 VZ |
217 | if ( m_splitter->IsSplit() ) |
218 | m_splitter->Unsplit(); | |
6b55490a VZ |
219 | m_left->Show(TRUE); |
220 | m_right->Show(TRUE); | |
221 | m_splitter->SplitVertically( m_left, m_right ); | |
0d559d69 | 222 | UpdatePosition(); |
c801d85f KB |
223 | } |
224 | ||
e3e65dac | 225 | void MyFrame::Unsplit(wxCommandEvent& WXUNUSED(event) ) |
c801d85f | 226 | { |
0d559d69 VZ |
227 | if ( m_splitter->IsSplit() ) |
228 | m_splitter->Unsplit(); | |
229 | SetStatusText("No splitter"); | |
230 | } | |
231 | ||
232 | void MyFrame::SetMinSize(wxCommandEvent& WXUNUSED(event) ) | |
233 | { | |
234 | wxString str; | |
1e133b7d | 235 | str.Printf( _T("%d"), m_splitter->GetMinimumPaneSize()); |
0d559d69 VZ |
236 | str = wxGetTextFromUser("Enter minimal size for panes:", "", str, this); |
237 | if ( str.IsEmpty() ) | |
238 | return; | |
239 | ||
1b3667ab | 240 | int minsize = wxStrtol( str, (wxChar**)NULL, 10 ); |
0d559d69 | 241 | m_splitter->SetMinimumPaneSize(minsize); |
1e133b7d | 242 | str.Printf( _T("Min pane size = %d"), minsize); |
0d559d69 | 243 | SetStatusText(str, 1); |
c801d85f KB |
244 | } |
245 | ||
246 | void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent& event) | |
247 | { | |
0d559d69 | 248 | event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_HORIZONTAL) ) ); |
c801d85f KB |
249 | } |
250 | ||
251 | void MyFrame::UpdateUIVertical(wxUpdateUIEvent& event) | |
252 | { | |
0d559d69 | 253 | event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_VERTICAL) ) ); |
c801d85f KB |
254 | } |
255 | ||
256 | void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent& event) | |
257 | { | |
0d559d69 | 258 | event.Enable( m_splitter->IsSplit() ); |
c801d85f KB |
259 | } |
260 | ||
0d559d69 | 261 | void MyFrame::UpdatePosition() |
c801d85f | 262 | { |
0d559d69 | 263 | wxString str; |
1e133b7d | 264 | str.Printf( _("Sash position = %d"), m_splitter->GetSashPosition()); |
0d559d69 | 265 | SetStatusText(str); |
c801d85f KB |
266 | } |
267 | ||
f6bcfd97 | 268 | MyCanvas::MyCanvas(wxWindow* parent, wxWindowID id, const wxPoint& point, const wxSize& size, const wxString &name ) : |
33611ebb | 269 | wxScrolledWindow(parent, id, point, size, 0, name ) |
c801d85f KB |
270 | { |
271 | } | |
272 | ||
273 | MyCanvas::~MyCanvas() | |
274 | { | |
275 | } | |
276 | ||
277 | void MyCanvas::OnDraw(wxDC& dc) | |
278 | { | |
0d559d69 VZ |
279 | dc.SetPen(*wxBLACK_PEN); |
280 | dc.DrawLine(0, 0, 100, 100); | |
c801d85f | 281 | |
0d559d69 VZ |
282 | dc.SetBackgroundMode(wxTRANSPARENT); |
283 | dc.DrawText("Testing", 50, 50); | |
b7346a70 | 284 | |
0d559d69 VZ |
285 | dc.SetPen(*wxRED_PEN); |
286 | dc.SetBrush(*wxGREEN_BRUSH); | |
287 | dc.DrawRectangle(120, 120, 100, 80); | |
c801d85f | 288 | } |