]> git.saurik.com Git - wxWidgets.git/blame - samples/sashtest/sashtest.cpp
use wxDC::GetMultiLineTextExtent() instead of duplicating its code in wxButton::DoGet...
[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
2f6c54eb 9// Licence: wxWindows license
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{
43 // Create the main frame window
44
e77d093d 45 frame = new MyFrame(NULL, wxID_ANY, _T("Sash Demo"), wxPoint(0, 0), wxSize(500, 400),
1f23a1c0
VZ
46 wxDEFAULT_FRAME_STYLE |
47 wxNO_FULL_REPAINT_ON_RESIZE |
48 wxHSCROLL | wxVSCROLL);
a6d70308
JS
49
50 // Give it an icon (this is ignored in MDI mode: uses resources)
51#ifdef __WXMSW__
600683ca 52 frame->SetIcon(wxIcon(_T("sashtest_icn")));
a6d70308 53#endif
a6d70308
JS
54
55 // Make a menubar
56 wxMenu *file_menu = new wxMenu;
57
600683ca
MB
58 file_menu->Append(SASHTEST_NEW_WINDOW, _T("&New window"));
59 file_menu->Append(SASHTEST_TOGGLE_WINDOW, _T("&Toggle window"));
60 file_menu->Append(SASHTEST_QUIT, _T("&Exit"));
a6d70308
JS
61
62 wxMenu *help_menu = new wxMenu;
600683ca 63 help_menu->Append(SASHTEST_ABOUT, _T("&About"));
a6d70308
JS
64
65 wxMenuBar *menu_bar = new wxMenuBar;
66
600683ca
MB
67 menu_bar->Append(file_menu, _T("&File"));
68 menu_bar->Append(help_menu, _T("&Help"));
a6d70308
JS
69
70 // Associate the menu bar with the frame
71 frame->SetMenuBar(menu_bar);
72
8520f137 73#if wxUSE_STATUSBAR
a6d70308 74 frame->CreateStatusBar();
8520f137 75#endif // wxUSE_STATUSBAR
a6d70308 76
e77d093d 77 frame->Show(true);
a6d70308
JS
78
79 SetTopWindow(frame);
80
e77d093d 81 return true;
a6d70308
JS
82}
83
84BEGIN_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)
91END_EVENT_TABLE()
92
93
94// Define my frame constructor
95MyFrame::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);
110 win->SetBackgroundColour(wxColour(255, 0, 0));
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);
122 win->SetBackgroundColour(wxColour(0, 0, 255));
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);
134 win->SetBackgroundColour(wxColour(0, 255, 0));
e77d093d 135 win->SetSashVisible(wxSASH_RIGHT, true);
a6d70308
JS
136 win->SetExtraBorderSize(10);
137
e77d093d 138 wxTextCtrl* textWindow = new wxTextCtrl(win, wxID_ANY, _T(""), wxDefaultPosition, wxDefaultSize,
a6d70308
JS
139 wxTE_MULTILINE|wxSUNKEN_BORDER);
140// wxTE_MULTILINE|wxNO_BORDER);
600683ca 141 textWindow->SetValue(_T("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 158void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
a6d70308 159{
e77d093d 160 Close(true);
a6d70308
JS
161}
162
8fdca65c 163void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
a6d70308 164{
be5a51fb 165 (void)wxMessageBox(_T("wxWidgets 2.0 Sash Demo\nAuthor: Julian Smart (c) 1998"), _T("About Sash Demo"));
a6d70308
JS
166}
167
8fdca65c 168void 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
JS
177 }
178 wxLayoutAlgorithm layout;
179 layout.LayoutMDIFrame(this);
180}
181
182void MyFrame::OnSashDrag(wxSashEvent& event)
183{
184 if (event.GetDragStatus() == wxSASH_STATUS_OUT_OF_RANGE)
185 return;
186
187 switch (event.GetId())
188 {
189 case ID_WINDOW_TOP:
190 {
191 m_topWindow->SetDefaultSize(wxSize(1000, event.GetDragRect().height));
192 break;
193 }
194 case ID_WINDOW_LEFT1:
195 {
196 m_leftWindow1->SetDefaultSize(wxSize(event.GetDragRect().width, 1000));
197 break;
198 }
199 case ID_WINDOW_LEFT2:
200 {
201 m_leftWindow2->SetDefaultSize(wxSize(event.GetDragRect().width, 1000));
202 break;
203 }
204 case ID_WINDOW_BOTTOM:
205 {
206 m_bottomWindow->SetDefaultSize(wxSize(1000, event.GetDragRect().height));
207 break;
208 }
209 }
210 wxLayoutAlgorithm layout;
211 layout.LayoutMDIFrame(this);
212
213 // Leaves bits of itself behind sometimes
214 GetClientWindow()->Refresh();
215}
216
8fdca65c 217void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event))
a6d70308
JS
218{
219 // Make another frame, containing a canvas
600683ca 220 MyChild *subframe = new MyChild(frame, _T("Canvas Frame"),
1f23a1c0
VZ
221 wxPoint(10, 10), wxSize(300, 300),
222 wxDEFAULT_FRAME_STYLE |
223 wxNO_FULL_REPAINT_ON_RESIZE);
a6d70308 224
600683ca 225 subframe->SetTitle(wxString::Format(_T("Canvas Frame %d"), winNumber));
a6d70308
JS
226 winNumber ++;
227
228 // Give it an icon (this is ignored in MDI mode: uses resources)
229#ifdef __WXMSW__
600683ca 230 subframe->SetIcon(wxIcon(_T("sashtest_icn")));
a6d70308
JS
231#endif
232
8520f137 233#if wxUSE_STATUSBAR
a6d70308
JS
234 // Give it a status line
235 subframe->CreateStatusBar();
8520f137 236#endif // wxUSE_STATUSBAR
a6d70308
JS
237
238 // Make a menubar
239 wxMenu *file_menu = new wxMenu;
240
600683ca
MB
241 file_menu->Append(SASHTEST_NEW_WINDOW, _T("&New window"));
242 file_menu->Append(SASHTEST_CHILD_QUIT, _T("&Close child"));
243 file_menu->Append(SASHTEST_QUIT, _T("&Exit"));
a6d70308
JS
244
245 wxMenu *option_menu = new wxMenu;
246
247 // Dummy option
600683ca 248 option_menu->Append(SASHTEST_REFRESH, _T("&Refresh picture"));
a6d70308
JS
249
250 wxMenu *help_menu = new wxMenu;
600683ca 251 help_menu->Append(SASHTEST_ABOUT, _T("&About"));
a6d70308
JS
252
253 wxMenuBar *menu_bar = new wxMenuBar;
254
600683ca
MB
255 menu_bar->Append(file_menu, _T("&File"));
256 menu_bar->Append(option_menu, _T("&Options"));
257 menu_bar->Append(help_menu, _T("&Help"));
a6d70308
JS
258
259 // Associate the menu bar with the frame
260 subframe->SetMenuBar(menu_bar);
261
262 int width, height;
263 subframe->GetClientSize(&width, &height);
264 MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
265 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
266 subframe->canvas = canvas;
267
268 // Give it scrollbars
269 canvas->SetScrollbars(20, 20, 50, 50);
270
e77d093d 271 subframe->Show(true);
a6d70308
JS
272}
273
274BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
2f6c54eb 275 EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
a6d70308
JS
276END_EVENT_TABLE()
277
278// Define a constructor for my canvas
1f23a1c0 279MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
e77d093d 280 : wxScrolledWindow(parent, wxID_ANY, pos, size,
1f23a1c0 281 wxSUNKEN_BORDER | wxNO_FULL_REPAINT_ON_RESIZE)
a6d70308 282{
f6bcfd97 283 SetBackgroundColour(* wxWHITE);
a6d70308
JS
284}
285
286// Define the repainting behaviour
287void MyCanvas::OnDraw(wxDC& dc)
288{
289 dc.SetFont(*wxSWISS_FONT);
290 dc.SetPen(*wxGREEN_PEN);
291 dc.DrawLine(0, 0, 200, 200);
292 dc.DrawLine(200, 0, 0, 200);
293
294 dc.SetBrush(*wxCYAN_BRUSH);
295 dc.SetPen(*wxRED_PEN);
296 dc.DrawRectangle(100, 100, 100, 50);
297 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
298
299 dc.DrawEllipse(250, 250, 100, 50);
9fc3cba7 300#if wxUSE_SPLINES
a6d70308 301 dc.DrawSpline(50, 200, 50, 100, 200, 10);
9fc3cba7 302#endif // wxUSE_SPLINES
a6d70308 303 dc.DrawLine(50, 230, 200, 230);
600683ca 304 dc.DrawText(_T("This is a test string"), 50, 230);
a6d70308
JS
305
306 wxPoint points[3];
307 points[0].x = 200; points[0].y = 300;
308 points[1].x = 100; points[1].y = 400;
309 points[2].x = 300; points[2].y = 400;
310
311 dc.DrawPolygon(3, points);
312}
313
314// This implements a tiny doodling program! Drag the mouse using
315// the left button.
316void MyCanvas::OnEvent(wxMouseEvent& event)
317{
318 wxClientDC dc(this);
319 PrepareDC(dc);
320
321 wxPoint pt(event.GetLogicalPosition(dc));
322
323 if (xpos > -1 && ypos > -1 && event.Dragging())
324 {
325 dc.SetPen(*wxBLACK_PEN);
326 dc.DrawLine(xpos, ypos, pt.x, pt.y);
327 }
328 xpos = pt.x;
329 ypos = pt.y;
330}
331
8fdca65c 332void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event))
a6d70308
JS
333{
334 wxLayoutAlgorithm layout;
335 layout.LayoutMDIFrame(this);
336}
337
338// Note that SASHTEST_NEW_WINDOW and SASHTEST_ABOUT commands get passed
339// to the parent window for processing, so no need to
340// duplicate event handlers here.
341
342BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
343 EVT_MENU(SASHTEST_CHILD_QUIT, MyChild::OnQuit)
344END_EVENT_TABLE()
345
346MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title, const wxPoint& pos, const wxSize& size,
347const long style):
e77d093d 348 wxMDIChildFrame(parent, wxID_ANY, title, pos, size, style)
a6d70308
JS
349{
350 canvas = NULL;
351 my_children.Append(this);
352}
353
354MyChild::~MyChild(void)
355{
356 my_children.DeleteObject(this);
357}
358
359void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
360{
e77d093d 361 Close(true);
a6d70308
JS
362}
363
364void MyChild::OnActivate(wxActivateEvent& event)
365{
366 if (event.GetActive() && canvas)
367 canvas->SetFocus();
368}
369