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