]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: m_splitter.cpp | |
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 | |
9 | // Licence: wxWindows license | |
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: | |
32 | bool OnInit(); | |
33 | }; | |
34 | ||
35 | class MySplitterWindow : public wxSplitterWindow | |
36 | { | |
37 | public: | |
38 | MySplitterWindow(wxFrame *parent, wxWindowID id) : wxSplitterWindow(parent, id) | |
39 | { | |
40 | m_frame = parent; | |
41 | } | |
42 | ||
43 | virtual bool OnSashPositionChange(int newSashPosition) | |
44 | { | |
45 | if ( !wxSplitterWindow::OnSashPositionChange(newSashPosition) ) | |
46 | return FALSE; | |
47 | ||
48 | wxString str; | |
49 | str.Printf("Sash position = %d", newSashPosition); | |
50 | m_frame->SetStatusText(str); | |
51 | ||
52 | return TRUE; | |
53 | } | |
54 | ||
55 | private: | |
56 | wxFrame *m_frame; | |
57 | }; | |
58 | ||
59 | class MyFrame: public wxFrame | |
60 | { | |
61 | public: | |
62 | MyFrame(wxFrame* frame, const wxString& title, const wxPoint& pos, const wxSize& size); | |
63 | virtual ~MyFrame(); | |
64 | ||
65 | // Menu commands | |
66 | void SplitHorizontal(wxCommandEvent& event); | |
67 | void SplitVertical(wxCommandEvent& event); | |
68 | void Unsplit(wxCommandEvent& event); | |
69 | void SetMinSize(wxCommandEvent& event); | |
70 | void Quit(wxCommandEvent& event); | |
71 | ||
72 | // Menu command update functions | |
73 | void UpdateUIHorizontal(wxUpdateUIEvent& event); | |
74 | void UpdateUIVertical(wxUpdateUIEvent& event); | |
75 | void UpdateUIUnsplit(wxUpdateUIEvent& event); | |
76 | ||
77 | private: | |
78 | void UpdatePosition(); | |
79 | ||
80 | wxMenu* fileMenu; | |
81 | wxMenuBar* menuBar; | |
82 | MyCanvas* m_leftCanvas; | |
83 | MyCanvas* m_rightCanvas; | |
84 | MySplitterWindow* m_splitter; | |
85 | ||
86 | DECLARE_EVENT_TABLE() | |
87 | }; | |
88 | ||
89 | class MyCanvas: public wxScrolledWindow | |
90 | { | |
91 | public: | |
92 | MyCanvas(wxWindow* parent, wxWindowID id, int x, int y, int w, int h); | |
93 | virtual ~MyCanvas(); | |
94 | ||
95 | virtual void OnDraw(wxDC& dc); | |
96 | ||
97 | DECLARE_EVENT_TABLE() | |
98 | }; | |
99 | ||
100 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) | |
101 | END_EVENT_TABLE() | |
102 | ||
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 | }; | |
112 | ||
113 | // Window ids | |
114 | #define SPLITTER_WINDOW 100 | |
115 | #define SPLITTER_FRAME 101 | |
116 | #define CANVAS1 102 | |
117 | #define CANVAS2 103 | |
118 | ||
119 | IMPLEMENT_APP(MyApp) | |
120 | ||
121 | bool MyApp::OnInit(void) | |
122 | { | |
123 | MyFrame* frame = new MyFrame((wxFrame *) NULL, "wxSplitterWindow Example", | |
124 | wxPoint(50, 50), wxSize(420, 300)); | |
125 | ||
126 | // Show the frame | |
127 | frame->Show(TRUE); | |
128 | ||
129 | SetTopWindow(frame); | |
130 | ||
131 | return TRUE; | |
132 | } | |
133 | ||
134 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
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) | |
144 | END_EVENT_TABLE() | |
145 | ||
146 | // My frame constructor | |
147 | MyFrame::MyFrame(wxFrame* frame, const wxString& title, const wxPoint& pos, const wxSize& size): | |
148 | wxFrame(frame, SPLITTER_FRAME, title, pos, size) | |
149 | { | |
150 | CreateStatusBar(2); | |
151 | ||
152 | // Make a menubar | |
153 | fileMenu = new wxMenu; | |
154 | fileMenu->Append(SPLIT_VERTICAL, "Split &Vertically", "Split vertically"); | |
155 | fileMenu->Append(SPLIT_HORIZONTAL, "Split &Horizontally", "Split horizontally"); | |
156 | fileMenu->Append(SPLIT_UNSPLIT, "&Unsplit", "Unsplit"); | |
157 | fileMenu->AppendSeparator(); | |
158 | fileMenu->Append(SPLIT_SETMINSIZE, "Set &min size", "Set minimum pane size"); | |
159 | fileMenu->AppendSeparator(); | |
160 | fileMenu->Append(SPLIT_QUIT, "E&xit", "Exit"); | |
161 | ||
162 | menuBar = new wxMenuBar; | |
163 | menuBar->Append(fileMenu, "&File"); | |
164 | ||
165 | SetMenuBar(menuBar); | |
166 | ||
167 | m_splitter = new MySplitterWindow(this, SPLITTER_WINDOW); | |
168 | ||
169 | m_leftCanvas = new MyCanvas(m_splitter, CANVAS1, 0, 0, 400, 400); | |
170 | m_leftCanvas->SetBackgroundColour(*wxRED); | |
171 | m_leftCanvas->SetScrollbars(20, 20, 50, 50); | |
172 | ||
173 | m_rightCanvas = new MyCanvas(m_splitter, CANVAS2, 0, 0, 400, 400); | |
174 | m_rightCanvas->SetBackgroundColour(*wxCYAN); | |
175 | m_rightCanvas->SetScrollbars(20, 20, 50, 50); | |
176 | m_rightCanvas->Show(FALSE); | |
177 | ||
178 | m_splitter->Initialize(m_leftCanvas); | |
179 | SetStatusText("Min pane size = 0", 1); | |
180 | } | |
181 | ||
182 | MyFrame::~MyFrame() | |
183 | { | |
184 | } | |
185 | ||
186 | void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) ) | |
187 | { | |
188 | Close(TRUE); | |
189 | } | |
190 | ||
191 | void MyFrame::SplitHorizontal(wxCommandEvent& WXUNUSED(event) ) | |
192 | { | |
193 | if ( m_splitter->IsSplit() ) | |
194 | m_splitter->Unsplit(); | |
195 | m_leftCanvas->Show(TRUE); | |
196 | m_rightCanvas->Show(TRUE); | |
197 | m_splitter->SplitHorizontally( m_leftCanvas, m_rightCanvas ); | |
198 | UpdatePosition(); | |
199 | } | |
200 | ||
201 | void MyFrame::SplitVertical(wxCommandEvent& WXUNUSED(event) ) | |
202 | { | |
203 | if ( m_splitter->IsSplit() ) | |
204 | m_splitter->Unsplit(); | |
205 | m_leftCanvas->Show(TRUE); | |
206 | m_rightCanvas->Show(TRUE); | |
207 | m_splitter->SplitVertically( m_leftCanvas, m_rightCanvas ); | |
208 | UpdatePosition(); | |
209 | } | |
210 | ||
211 | void MyFrame::Unsplit(wxCommandEvent& WXUNUSED(event) ) | |
212 | { | |
213 | if ( m_splitter->IsSplit() ) | |
214 | m_splitter->Unsplit(); | |
215 | SetStatusText("No splitter"); | |
216 | } | |
217 | ||
218 | void MyFrame::SetMinSize(wxCommandEvent& WXUNUSED(event) ) | |
219 | { | |
220 | wxString str; | |
221 | str.Printf("%d", m_splitter->GetMinimumPaneSize()); | |
222 | str = wxGetTextFromUser("Enter minimal size for panes:", "", str, this); | |
223 | if ( str.IsEmpty() ) | |
224 | return; | |
225 | ||
226 | int minsize = atoi(str); | |
227 | m_splitter->SetMinimumPaneSize(minsize); | |
228 | str.Printf("Min pane size = %d", minsize); | |
229 | SetStatusText(str, 1); | |
230 | } | |
231 | ||
232 | void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent& event) | |
233 | { | |
234 | event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_HORIZONTAL) ) ); | |
235 | } | |
236 | ||
237 | void MyFrame::UpdateUIVertical(wxUpdateUIEvent& event) | |
238 | { | |
239 | event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_VERTICAL) ) ); | |
240 | } | |
241 | ||
242 | void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent& event) | |
243 | { | |
244 | event.Enable( m_splitter->IsSplit() ); | |
245 | } | |
246 | ||
247 | void MyFrame::UpdatePosition() | |
248 | { | |
249 | wxString str; | |
250 | str.Printf("Sash position = %d", m_splitter->GetSashPosition()); | |
251 | SetStatusText(str); | |
252 | } | |
253 | ||
254 | MyCanvas::MyCanvas(wxWindow* parent, wxWindowID id, int x, int y, int w, int h) : | |
255 | wxScrolledWindow(parent, id, wxPoint(x, y), wxSize(w, h)) | |
256 | { | |
257 | } | |
258 | ||
259 | MyCanvas::~MyCanvas() | |
260 | { | |
261 | } | |
262 | ||
263 | void MyCanvas::OnDraw(wxDC& dc) | |
264 | { | |
265 | dc.SetPen(*wxBLACK_PEN); | |
266 | dc.DrawLine(0, 0, 100, 100); | |
267 | ||
268 | dc.SetBackgroundMode(wxTRANSPARENT); | |
269 | dc.DrawText("Testing", 50, 50); | |
270 | ||
271 | dc.SetPen(*wxRED_PEN); | |
272 | dc.SetBrush(*wxGREEN_BRUSH); | |
273 | dc.DrawRectangle(120, 120, 100, 80); | |
274 | } |