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