]>
Commit | Line | Data |
---|---|---|
a6d70308 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: sashtest.cpp | |
3 | // Purpose: Layout/sash sample | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
6aa89a22 | 7 | // Copyright: (c) Julian Smart |
526954c5 | 8 | // Licence: wxWindows licence |
a6d70308 JS |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | // For compilers that support precompilation, includes "wx/wx.h". | |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
18 | #ifndef WX_PRECOMP | |
19 | #include "wx/wx.h" | |
20 | #include "wx/mdi.h" | |
21 | #endif | |
22 | ||
012f2cb2 GD |
23 | #include "wx/toolbar.h" |
24 | #include "wx/laywin.h" | |
a6d70308 JS |
25 | |
26 | #include "sashtest.h" | |
27 | ||
28 | MyFrame *frame = NULL; | |
29 | wxList my_children; | |
30 | ||
31 | IMPLEMENT_APP(MyApp) | |
32 | ||
33 | // For drawing lines in a canvas | |
34 | long xpos = -1; | |
35 | long ypos = -1; | |
36 | ||
37 | int winNumber = 1; | |
38 | ||
39 | // Initialise this in OnInit, not statically | |
40 | bool MyApp::OnInit(void) | |
41 | { | |
45e6e6f8 VZ |
42 | if ( !wxApp::OnInit() ) |
43 | return false; | |
44 | ||
a6d70308 JS |
45 | // Create the main frame window |
46 | ||
9a83f860 | 47 | frame = new MyFrame(NULL, wxID_ANY, wxT("Sash Demo"), wxPoint(0, 0), wxSize(500, 400), |
1f23a1c0 VZ |
48 | wxDEFAULT_FRAME_STYLE | |
49 | wxNO_FULL_REPAINT_ON_RESIZE | | |
50 | wxHSCROLL | wxVSCROLL); | |
a6d70308 JS |
51 | |
52 | // Give it an icon (this is ignored in MDI mode: uses resources) | |
53 | #ifdef __WXMSW__ | |
9a83f860 | 54 | frame->SetIcon(wxIcon(wxT("sashtest_icn"))); |
a6d70308 | 55 | #endif |
a6d70308 JS |
56 | |
57 | // Make a menubar | |
58 | wxMenu *file_menu = new wxMenu; | |
59 | ||
9a83f860 VZ |
60 | file_menu->Append(SASHTEST_NEW_WINDOW, wxT("&New window")); |
61 | file_menu->Append(SASHTEST_TOGGLE_WINDOW, wxT("&Toggle window")); | |
62 | file_menu->Append(SASHTEST_QUIT, wxT("&Exit")); | |
a6d70308 JS |
63 | |
64 | wxMenu *help_menu = new wxMenu; | |
9a83f860 | 65 | help_menu->Append(SASHTEST_ABOUT, wxT("&About")); |
a6d70308 JS |
66 | |
67 | wxMenuBar *menu_bar = new wxMenuBar; | |
68 | ||
9a83f860 VZ |
69 | menu_bar->Append(file_menu, wxT("&File")); |
70 | menu_bar->Append(help_menu, wxT("&Help")); | |
a6d70308 JS |
71 | |
72 | // Associate the menu bar with the frame | |
73 | frame->SetMenuBar(menu_bar); | |
74 | ||
8520f137 | 75 | #if wxUSE_STATUSBAR |
a6d70308 | 76 | frame->CreateStatusBar(); |
8520f137 | 77 | #endif // wxUSE_STATUSBAR |
a6d70308 | 78 | |
e77d093d | 79 | frame->Show(true); |
a6d70308 | 80 | |
e77d093d | 81 | return true; |
a6d70308 JS |
82 | } |
83 | ||
84 | BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame) | |
85 | EVT_MENU(SASHTEST_ABOUT, MyFrame::OnAbout) | |
86 | EVT_MENU(SASHTEST_NEW_WINDOW, MyFrame::OnNewWindow) | |
87 | EVT_SIZE(MyFrame::OnSize) | |
88 | EVT_MENU(SASHTEST_QUIT, MyFrame::OnQuit) | |
89 | EVT_MENU(SASHTEST_TOGGLE_WINDOW, MyFrame::OnToggleWindow) | |
90 | EVT_SASH_DRAGGED_RANGE(ID_WINDOW_TOP, ID_WINDOW_BOTTOM, MyFrame::OnSashDrag) | |
91 | END_EVENT_TABLE() | |
92 | ||
93 | ||
94 | // Define my frame constructor | |
95 | MyFrame::MyFrame(wxWindow *parent, const wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, | |
2f6c54eb | 96 | const long style): |
a6d70308 JS |
97 | wxMDIParentFrame(parent, id, title, pos, size, style) |
98 | { | |
99 | // Create some dummy layout windows | |
100 | ||
101 | // A window like a toolbar | |
1f23a1c0 VZ |
102 | wxSashLayoutWindow* win = |
103 | new wxSashLayoutWindow(this, ID_WINDOW_TOP, | |
104 | wxDefaultPosition, wxSize(200, 30), | |
105 | wxNO_BORDER | wxSW_3D | wxCLIP_CHILDREN); | |
106 | ||
a6d70308 JS |
107 | win->SetDefaultSize(wxSize(1000, 30)); |
108 | win->SetOrientation(wxLAYOUT_HORIZONTAL); | |
109 | win->SetAlignment(wxLAYOUT_TOP); | |
ea92bb67 | 110 | win->SetBackgroundColour(*wxRED); |
e77d093d | 111 | win->SetSashVisible(wxSASH_BOTTOM, true); |
a6d70308 JS |
112 | |
113 | m_topWindow = win; | |
114 | ||
115 | // A window like a statusbar | |
1f23a1c0 VZ |
116 | win = new wxSashLayoutWindow(this, ID_WINDOW_BOTTOM, |
117 | wxDefaultPosition, wxSize(200, 30), | |
118 | wxNO_BORDER | wxSW_3D | wxCLIP_CHILDREN); | |
a6d70308 JS |
119 | win->SetDefaultSize(wxSize(1000, 30)); |
120 | win->SetOrientation(wxLAYOUT_HORIZONTAL); | |
121 | win->SetAlignment(wxLAYOUT_BOTTOM); | |
ea92bb67 | 122 | win->SetBackgroundColour(*wxBLUE); |
e77d093d | 123 | win->SetSashVisible(wxSASH_TOP, true); |
a6d70308 JS |
124 | |
125 | m_bottomWindow = win; | |
126 | ||
127 | // A window to the left of the client window | |
1f23a1c0 VZ |
128 | win = new wxSashLayoutWindow(this, ID_WINDOW_LEFT1, |
129 | wxDefaultPosition, wxSize(200, 30), | |
130 | wxNO_BORDER | wxSW_3D | wxCLIP_CHILDREN); | |
a6d70308 JS |
131 | win->SetDefaultSize(wxSize(120, 1000)); |
132 | win->SetOrientation(wxLAYOUT_VERTICAL); | |
133 | win->SetAlignment(wxLAYOUT_LEFT); | |
ea92bb67 | 134 | win->SetBackgroundColour(*wxGREEN); |
e77d093d | 135 | win->SetSashVisible(wxSASH_RIGHT, true); |
a6d70308 JS |
136 | win->SetExtraBorderSize(10); |
137 | ||
5f4d35b8 | 138 | wxTextCtrl* textWindow = new wxTextCtrl(win, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, |
a6d70308 JS |
139 | wxTE_MULTILINE|wxSUNKEN_BORDER); |
140 | // wxTE_MULTILINE|wxNO_BORDER); | |
9a83f860 | 141 | textWindow->SetValue(wxT("A help window")); |
a6d70308 JS |
142 | |
143 | m_leftWindow1 = win; | |
144 | ||
145 | // Another window to the left of the client window | |
1f23a1c0 VZ |
146 | win = new wxSashLayoutWindow(this, ID_WINDOW_LEFT2, |
147 | wxDefaultPosition, wxSize(200, 30), | |
6138e469 | 148 | wxNO_BORDER | wxSW_3D | wxCLIP_CHILDREN); |
a6d70308 JS |
149 | win->SetDefaultSize(wxSize(120, 1000)); |
150 | win->SetOrientation(wxLAYOUT_VERTICAL); | |
151 | win->SetAlignment(wxLAYOUT_LEFT); | |
152 | win->SetBackgroundColour(wxColour(0, 255, 255)); | |
e77d093d | 153 | win->SetSashVisible(wxSASH_RIGHT, true); |
a6d70308 JS |
154 | |
155 | m_leftWindow2 = win; | |
156 | } | |
157 | ||
8fdca65c | 158 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
a6d70308 | 159 | { |
e77d093d | 160 | Close(true); |
a6d70308 JS |
161 | } |
162 | ||
8fdca65c | 163 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
a6d70308 | 164 | { |
9a83f860 | 165 | (void)wxMessageBox(wxT("wxWidgets 2.0 Sash Demo\nAuthor: Julian Smart (c) 1998"), wxT("About Sash Demo")); |
a6d70308 JS |
166 | } |
167 | ||
8fdca65c | 168 | void MyFrame::OnToggleWindow(wxCommandEvent& WXUNUSED(event)) |
a6d70308 JS |
169 | { |
170 | if (m_leftWindow1->IsShown()) | |
171 | { | |
e77d093d | 172 | m_leftWindow1->Show(false); |
a6d70308 JS |
173 | } |
174 | else | |
175 | { | |
e77d093d | 176 | m_leftWindow1->Show(true); |
a6d70308 | 177 | } |
5f4d35b8 | 178 | #if wxUSE_MDI_ARCHITECTURE |
a6d70308 JS |
179 | wxLayoutAlgorithm layout; |
180 | layout.LayoutMDIFrame(this); | |
5f4d35b8 | 181 | #endif // wxUSE_MDI_ARCHITECTURE |
a6d70308 JS |
182 | } |
183 | ||
184 | void MyFrame::OnSashDrag(wxSashEvent& event) | |
185 | { | |
186 | if (event.GetDragStatus() == wxSASH_STATUS_OUT_OF_RANGE) | |
187 | return; | |
188 | ||
189 | switch (event.GetId()) | |
190 | { | |
191 | case ID_WINDOW_TOP: | |
192 | { | |
193 | m_topWindow->SetDefaultSize(wxSize(1000, event.GetDragRect().height)); | |
194 | break; | |
195 | } | |
196 | case ID_WINDOW_LEFT1: | |
197 | { | |
198 | m_leftWindow1->SetDefaultSize(wxSize(event.GetDragRect().width, 1000)); | |
199 | break; | |
200 | } | |
201 | case ID_WINDOW_LEFT2: | |
202 | { | |
203 | m_leftWindow2->SetDefaultSize(wxSize(event.GetDragRect().width, 1000)); | |
204 | break; | |
205 | } | |
206 | case ID_WINDOW_BOTTOM: | |
207 | { | |
208 | m_bottomWindow->SetDefaultSize(wxSize(1000, event.GetDragRect().height)); | |
209 | break; | |
210 | } | |
211 | } | |
5f4d35b8 WS |
212 | |
213 | #if wxUSE_MDI_ARCHITECTURE | |
a6d70308 JS |
214 | wxLayoutAlgorithm layout; |
215 | layout.LayoutMDIFrame(this); | |
5f4d35b8 | 216 | #endif // wxUSE_MDI_ARCHITECTURE |
a6d70308 JS |
217 | |
218 | // Leaves bits of itself behind sometimes | |
219 | GetClientWindow()->Refresh(); | |
220 | } | |
221 | ||
8fdca65c | 222 | void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event)) |
a6d70308 JS |
223 | { |
224 | // Make another frame, containing a canvas | |
9a83f860 | 225 | MyChild *subframe = new MyChild(frame, wxT("Canvas Frame"), |
1f23a1c0 VZ |
226 | wxPoint(10, 10), wxSize(300, 300), |
227 | wxDEFAULT_FRAME_STYLE | | |
228 | wxNO_FULL_REPAINT_ON_RESIZE); | |
a6d70308 | 229 | |
9a83f860 | 230 | subframe->SetTitle(wxString::Format(wxT("Canvas Frame %d"), winNumber)); |
a6d70308 JS |
231 | winNumber ++; |
232 | ||
233 | // Give it an icon (this is ignored in MDI mode: uses resources) | |
234 | #ifdef __WXMSW__ | |
9a83f860 | 235 | subframe->SetIcon(wxIcon(wxT("sashtest_icn"))); |
a6d70308 JS |
236 | #endif |
237 | ||
8520f137 | 238 | #if wxUSE_STATUSBAR |
a6d70308 JS |
239 | // Give it a status line |
240 | subframe->CreateStatusBar(); | |
8520f137 | 241 | #endif // wxUSE_STATUSBAR |
a6d70308 JS |
242 | |
243 | // Make a menubar | |
244 | wxMenu *file_menu = new wxMenu; | |
245 | ||
9a83f860 VZ |
246 | file_menu->Append(SASHTEST_NEW_WINDOW, wxT("&New window")); |
247 | file_menu->Append(SASHTEST_CHILD_QUIT, wxT("&Close child")); | |
248 | file_menu->Append(SASHTEST_QUIT, wxT("&Exit")); | |
a6d70308 JS |
249 | |
250 | wxMenu *option_menu = new wxMenu; | |
251 | ||
252 | // Dummy option | |
9a83f860 | 253 | option_menu->Append(SASHTEST_REFRESH, wxT("&Refresh picture")); |
a6d70308 JS |
254 | |
255 | wxMenu *help_menu = new wxMenu; | |
9a83f860 | 256 | help_menu->Append(SASHTEST_ABOUT, wxT("&About")); |
a6d70308 JS |
257 | |
258 | wxMenuBar *menu_bar = new wxMenuBar; | |
259 | ||
9a83f860 VZ |
260 | menu_bar->Append(file_menu, wxT("&File")); |
261 | menu_bar->Append(option_menu, wxT("&Options")); | |
262 | menu_bar->Append(help_menu, wxT("&Help")); | |
a6d70308 JS |
263 | |
264 | // Associate the menu bar with the frame | |
265 | subframe->SetMenuBar(menu_bar); | |
266 | ||
267 | int width, height; | |
268 | subframe->GetClientSize(&width, &height); | |
269 | MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height)); | |
270 | canvas->SetCursor(wxCursor(wxCURSOR_PENCIL)); | |
271 | subframe->canvas = canvas; | |
272 | ||
273 | // Give it scrollbars | |
274 | canvas->SetScrollbars(20, 20, 50, 50); | |
275 | ||
e77d093d | 276 | subframe->Show(true); |
a6d70308 JS |
277 | } |
278 | ||
279 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) | |
2f6c54eb | 280 | EVT_MOUSE_EVENTS(MyCanvas::OnEvent) |
a6d70308 JS |
281 | END_EVENT_TABLE() |
282 | ||
283 | // Define a constructor for my canvas | |
1f23a1c0 | 284 | MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size) |
e77d093d | 285 | : wxScrolledWindow(parent, wxID_ANY, pos, size, |
1f23a1c0 | 286 | wxSUNKEN_BORDER | wxNO_FULL_REPAINT_ON_RESIZE) |
a6d70308 | 287 | { |
f6bcfd97 | 288 | SetBackgroundColour(* wxWHITE); |
a6d70308 JS |
289 | } |
290 | ||
291 | // Define the repainting behaviour | |
292 | void MyCanvas::OnDraw(wxDC& dc) | |
293 | { | |
5f4d35b8 WS |
294 | dc.SetFont(*wxSWISS_FONT); |
295 | dc.SetPen(*wxGREEN_PEN); | |
296 | dc.DrawLine(0, 0, 200, 200); | |
297 | dc.DrawLine(200, 0, 0, 200); | |
a6d70308 | 298 | |
5f4d35b8 WS |
299 | dc.SetBrush(*wxCYAN_BRUSH); |
300 | dc.SetPen(*wxRED_PEN); | |
301 | dc.DrawRectangle(100, 100, 100, 50); | |
302 | dc.DrawRoundedRectangle(150, 150, 100, 50, 20); | |
a6d70308 | 303 | |
5f4d35b8 | 304 | dc.DrawEllipse(250, 250, 100, 50); |
9fc3cba7 | 305 | #if wxUSE_SPLINES |
5f4d35b8 | 306 | dc.DrawSpline(50, 200, 50, 100, 200, 10); |
9fc3cba7 | 307 | #endif // wxUSE_SPLINES |
5f4d35b8 | 308 | dc.DrawLine(50, 230, 200, 230); |
9a83f860 | 309 | dc.DrawText(wxT("This is a test string"), 50, 230); |
5f4d35b8 WS |
310 | |
311 | wxPoint points[3]; | |
312 | points[0].x = 200; points[0].y = 300; | |
313 | points[1].x = 100; points[1].y = 400; | |
314 | points[2].x = 300; points[2].y = 400; | |
315 | ||
316 | dc.DrawPolygon(3, points); | |
a6d70308 JS |
317 | } |
318 | ||
319 | // This implements a tiny doodling program! Drag the mouse using | |
320 | // the left button. | |
321 | void MyCanvas::OnEvent(wxMouseEvent& event) | |
322 | { | |
323 | wxClientDC dc(this); | |
324 | PrepareDC(dc); | |
325 | ||
326 | wxPoint pt(event.GetLogicalPosition(dc)); | |
327 | ||
328 | if (xpos > -1 && ypos > -1 && event.Dragging()) | |
329 | { | |
330 | dc.SetPen(*wxBLACK_PEN); | |
331 | dc.DrawLine(xpos, ypos, pt.x, pt.y); | |
332 | } | |
333 | xpos = pt.x; | |
334 | ypos = pt.y; | |
335 | } | |
336 | ||
8fdca65c | 337 | void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event)) |
a6d70308 | 338 | { |
5f4d35b8 | 339 | #if wxUSE_MDI_ARCHITECTURE |
a6d70308 JS |
340 | wxLayoutAlgorithm layout; |
341 | layout.LayoutMDIFrame(this); | |
5f4d35b8 | 342 | #endif // wxUSE_MDI_ARCHITECTURE |
a6d70308 JS |
343 | } |
344 | ||
345 | // Note that SASHTEST_NEW_WINDOW and SASHTEST_ABOUT commands get passed | |
346 | // to the parent window for processing, so no need to | |
347 | // duplicate event handlers here. | |
348 | ||
349 | BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame) | |
350 | EVT_MENU(SASHTEST_CHILD_QUIT, MyChild::OnQuit) | |
351 | END_EVENT_TABLE() | |
352 | ||
353 | MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title, const wxPoint& pos, const wxSize& size, | |
354 | const long style): | |
e77d093d | 355 | wxMDIChildFrame(parent, wxID_ANY, title, pos, size, style) |
a6d70308 JS |
356 | { |
357 | canvas = NULL; | |
358 | my_children.Append(this); | |
359 | } | |
360 | ||
361 | MyChild::~MyChild(void) | |
362 | { | |
363 | my_children.DeleteObject(this); | |
364 | } | |
365 | ||
366 | void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
367 | { | |
e77d093d | 368 | Close(true); |
a6d70308 JS |
369 | } |
370 | ||
371 | void MyChild::OnActivate(wxActivateEvent& event) | |
372 | { | |
373 | if (event.GetActive() && canvas) | |
374 | canvas->SetFocus(); | |
375 | } |