]> git.saurik.com Git - wxWidgets.git/blame - samples/docvwmdi/view.cpp
don't give errors if SetLayeredWindowAttributes() is not available (as is the case...
[wxWidgets.git] / samples / docvwmdi / view.cpp
CommitLineData
2108f33a
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: view.cpp
3// Purpose: View classes
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
2108f33a
JS
10/////////////////////////////////////////////////////////////////////////////
11
2108f33a
JS
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#endif
22
e4b19d9b 23#if !wxUSE_DOC_VIEW_ARCHITECTURE
ad813b00 24#error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
2108f33a
JS
25#endif
26
27#include "docview.h"
28#include "doc.h"
29#include "view.h"
30
31IMPLEMENT_DYNAMIC_CLASS(DrawingView, wxView)
32
33// For drawing lines in a canvas
6bdf5153
VZ
34static float xpos = -1;
35static float ypos = -1;
2108f33a
JS
36
37BEGIN_EVENT_TABLE(DrawingView, wxView)
38 EVT_MENU(DOODLE_CUT, DrawingView::OnCut)
39END_EVENT_TABLE()
40
41// What to do when a view is created. Creates actual
42// windows for displaying the view.
43bool DrawingView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
44{
6bdf5153
VZ
45 m_frame = wxGetApp().CreateChildFrame(doc, this, true);
46 m_frame->SetTitle(wxT("DrawingView"));
2108f33a 47
6bdf5153 48 m_canvas = GetMainFrame()->CreateCanvas(this, m_frame);
2108f33a
JS
49#ifdef __X__
50 // X seems to require a forced resize
51 int x, y;
6bdf5153
VZ
52 m_frame->GetSize(&x, &y);
53 m_frame->SetSize(wxDefaultCoord, wxDefaultCoord, x, y);
2108f33a 54#endif
6bdf5153 55 m_frame->Show(true);
691d944f 56 Activate(true);
2108f33a 57
691d944f 58 return true;
2108f33a
JS
59}
60
6bdf5153
VZ
61DrawingDocument* DrawingView::GetDocument()
62{
63 return wxStaticCast(wxView::GetDocument(), DrawingDocument);
64}
65
2108f33a
JS
66// Sneakily gets used for default print/preview
67// as well as drawing on the screen.
68void DrawingView::OnDraw(wxDC *dc)
69{
6bdf5153
VZ
70 dc->SetFont(*wxNORMAL_FONT);
71 dc->SetPen(*wxBLACK_PEN);
72
73 wxList::compatibility_iterator node = GetDocument()->GetDoodleSegments().GetFirst();
74 while (node)
75 {
76 DoodleSegment* seg = (DoodleSegment*)node->GetData();
77 seg->Draw(dc);
78 node = node->GetNext();
79 }
2108f33a
JS
80}
81
82void DrawingView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint))
83{
6bdf5153
VZ
84 if (m_canvas)
85 m_canvas->Refresh();
2108f33a
JS
86
87/* Is the following necessary?
88#ifdef __WXMSW__
6bdf5153
VZ
89 if (canvas)
90 canvas->Refresh();
2108f33a 91#else
6bdf5153 92 if (canvas)
2108f33a 93 {
6bdf5153
VZ
94 wxClientDC dc(canvas);
95 dc.Clear();
96 OnDraw(& dc);
2108f33a
JS
97 }
98#endif
99*/
100}
101
102// Clean up windows used for displaying the view.
103bool DrawingView::OnClose(bool deleteWindow)
104{
6bdf5153
VZ
105 if (!GetDocument()->Close())
106 return false;
2108f33a 107
6bdf5153
VZ
108 // Clear the canvas in case we're in single-window mode,
109 // and the canvas stays.
110 m_canvas->ClearBackground();
111 m_canvas->m_view = NULL;
112 m_canvas = NULL;
2108f33a 113
6bdf5153
VZ
114 wxString s(wxTheApp->GetAppDisplayName());
115 if (m_frame)
116 m_frame->SetTitle(s);
2108f33a 117
6bdf5153 118 SetFrame(NULL);
2108f33a 119
6bdf5153 120 Activate(false);
1d14c005 121
6bdf5153
VZ
122 if (deleteWindow)
123 {
124 delete m_frame;
125 return true;
126 }
691d944f 127 return true;
2108f33a
JS
128}
129
130void DrawingView::OnCut(wxCommandEvent& WXUNUSED(event) )
131{
6bdf5153
VZ
132 DrawingDocument* doc = GetDocument();
133 doc->GetCommandProcessor()->Submit(new DrawingCommand(wxT("Cut Last Segment"), DOODLE_CUT, doc, NULL));
2108f33a
JS
134}
135
136IMPLEMENT_DYNAMIC_CLASS(TextEditView, wxView)
137
4e553af1
VZ
138BEGIN_EVENT_TABLE(TextEditView, wxView)
139 EVT_MENU(wxID_COPY, TextEditView::OnCopy)
140 EVT_MENU(wxID_PASTE, TextEditView::OnPaste)
141 EVT_MENU(wxID_SELECTALL, TextEditView::OnSelectAll)
142END_EVENT_TABLE()
143
6bdf5153 144bool TextEditView::OnCreate(wxDocument* doc, long WXUNUSED(flags) )
2108f33a 145{
6bdf5153 146 m_frame = wxGetApp().CreateChildFrame(doc, this, false);
2108f33a 147
6bdf5153
VZ
148 wxSize size = m_frame->GetClientSize();
149 m_textsw = new MyTextWindow(this, m_frame, wxPoint(0, 0), size, wxTE_MULTILINE);
150 m_frame->SetTitle(wxT("TextEditView"));
2108f33a
JS
151
152#ifdef __X__
6bdf5153
VZ
153 // X seems to require a forced resize
154 int x, y;
155 m_frame->GetSize(&x, &y);
156 m_frame->SetSize(wxDefaultCoord, wxDefaultCoord, x, y);
2108f33a
JS
157#endif
158
6bdf5153
VZ
159 m_frame->Show(true);
160 Activate(true);
1d14c005 161
6bdf5153 162 return true;
2108f33a
JS
163}
164
165// Handled by wxTextWindow
166void TextEditView::OnDraw(wxDC *WXUNUSED(dc) )
167{
168}
169
170void TextEditView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
171{
172}
173
174bool TextEditView::OnClose(bool deleteWindow)
175{
6bdf5153
VZ
176 if (!GetDocument()->Close())
177 return false;
1d14c005 178
6bdf5153 179 Activate(false);
2108f33a 180
6bdf5153
VZ
181 if (deleteWindow)
182 {
183 delete m_frame;
184 return true;
185 }
691d944f 186 return true;
6bdf5153
VZ
187}
188
189bool TextEditView::ProcessEvent(wxEvent& event)
190{
191 bool processed = false;
192 if (!processed) switch (event.GetId())
193 {
194 case wxID_COPY:
195 case wxID_PASTE:
196 case wxID_SELECTALL:
197 processed = m_textsw->ProcessEvent(event);
198 break;
199 }
200 if (!processed) processed = wxView::ProcessEvent(event);
201 return processed;
2108f33a
JS
202}
203
204/*
205 * Window implementations
206 */
207
208BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
209 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
210END_EVENT_TABLE()
211
212// Define a constructor for my canvas
6bdf5153 213MyCanvas::MyCanvas(DrawingView* view, wxMDIChildFrame* frame, const wxPoint& pos, const wxSize& size, long style):
691d944f 214 wxScrolledWindow(frame, wxID_ANY, pos, size, style)
2108f33a 215{
6bdf5153 216 m_view = view;
2108f33a
JS
217}
218
219// Define the repainting behaviour
220void MyCanvas::OnDraw(wxDC& dc)
221{
6bdf5153
VZ
222 if (m_view)
223 m_view->OnDraw(& dc);
2108f33a
JS
224}
225
226// This implements a tiny doodling program. Drag the mouse using
227// the left button.
228void MyCanvas::OnMouseEvent(wxMouseEvent& event)
229{
6bdf5153
VZ
230 if (!m_view)
231 return;
1d14c005 232
6bdf5153 233 static DoodleSegment* currentSegment = NULL;
2108f33a 234
6bdf5153
VZ
235 wxClientDC dc(this);
236 PrepareDC(dc);
2108f33a 237
6bdf5153 238 dc.SetPen(*wxBLACK_PEN);
2108f33a 239
6bdf5153 240 wxPoint pt(event.GetLogicalPosition(dc));
2108f33a 241
6bdf5153 242 if (currentSegment && event.LeftUp())
2108f33a 243 {
6bdf5153
VZ
244 if (currentSegment->m_lines.GetCount() == 0)
245 {
246 delete currentSegment;
247 currentSegment = NULL;
248 }
249 else
250 {
251 // We've got a valid segment on mouse left up, so store it.
252 DrawingDocument* doc = m_view->GetDocument();
253
254 doc->GetCommandProcessor()->Submit(new DrawingCommand(wxT("Add Segment"), DOODLE_ADD, doc, currentSegment));
255
256 m_view->GetDocument()->Modify(true);
257 currentSegment = NULL;
258 }
2108f33a 259 }
6bdf5153
VZ
260
261 if ( (xpos > -1) && (ypos > -1) && event.Dragging())
2108f33a 262 {
6bdf5153
VZ
263 if (!currentSegment)
264 currentSegment = new DoodleSegment;
2108f33a 265
6bdf5153
VZ
266 DoodleLine *newLine = new DoodleLine;
267 newLine->x1 = (long)xpos;
268 newLine->y1 = (long)ypos;
269 newLine->x2 = pt.x;
270 newLine->y2 = pt.y;
271 currentSegment->m_lines.Append(newLine);
2108f33a 272
6bdf5153 273 dc.DrawLine( (long)xpos, (long)ypos, pt.x, pt.y);
2108f33a 274 }
6bdf5153
VZ
275 xpos = pt.x;
276 ypos = pt.y;
2108f33a
JS
277}
278
279// Define a constructor for my text subwindow
6bdf5153
VZ
280MyTextWindow::MyTextWindow(wxView* view, wxMDIChildFrame* frame, const wxPoint& pos, const wxSize& size, long style):
281 wxTextCtrl(frame, wxID_ANY, wxEmptyString, pos, size, style)
2108f33a 282{
6bdf5153 283 m_view = view;
2108f33a
JS
284}
285