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