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