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