]> git.saurik.com Git - wxWidgets.git/blame - samples/docvwmdi/view.cpp
mac cleanup, cgcolor changes
[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
34float xpos = -1;
35float ypos = -1;
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{
691d944f 45 frame = wxGetApp().CreateChildFrame(doc, this, true);
8325937e 46 frame->SetTitle(_T("DrawingView"));
2108f33a
JS
47
48 canvas = GetMainFrame()->CreateCanvas(this, frame);
49#ifdef __X__
50 // X seems to require a forced resize
51 int x, y;
52 frame->GetSize(&x, &y);
422d0ff0 53 frame->SetSize(wxDefaultCoord, wxDefaultCoord, x, y);
2108f33a 54#endif
691d944f
WS
55 frame->Show(true);
56 Activate(true);
2108f33a 57
691d944f 58 return true;
2108f33a
JS
59}
60
61// Sneakily gets used for default print/preview
62// as well as drawing on the screen.
63void DrawingView::OnDraw(wxDC *dc)
64{
65 dc->SetFont(*wxNORMAL_FONT);
66 dc->SetPen(*wxBLACK_PEN);
67
bd5206dd 68 wxList::compatibility_iterator node = ((DrawingDocument *)GetDocument())->GetDoodleSegments().GetFirst();
2108f33a
JS
69 while (node)
70 {
b1d4dd7a 71 DoodleSegment *seg = (DoodleSegment *)node->GetData();
2108f33a 72 seg->Draw(dc);
b1d4dd7a 73 node = node->GetNext();
2108f33a
JS
74 }
75}
76
77void DrawingView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint))
78{
79 if (canvas)
80 canvas->Refresh();
81
82/* Is the following necessary?
83#ifdef __WXMSW__
84 if (canvas)
85 canvas->Refresh();
86#else
87 if (canvas)
88 {
89 wxClientDC dc(canvas);
90 dc.Clear();
91 OnDraw(& dc);
92 }
93#endif
94*/
95}
96
97// Clean up windows used for displaying the view.
98bool DrawingView::OnClose(bool deleteWindow)
99{
100 if (!GetDocument()->Close())
691d944f 101 return false;
2108f33a
JS
102
103 // Clear the canvas in case we're in single-window mode,
104 // and the canvas stays.
1d14c005 105 canvas->ClearBackground();
c67daf87
UR
106 canvas->view = (wxView *) NULL;
107 canvas = (MyCanvas *) NULL;
2108f33a 108
9cf3d218 109 wxString s(wxTheApp->GetAppDisplayName());
2108f33a
JS
110 if (frame)
111 frame->SetTitle(s);
112
9746a2ba 113 SetFrame((wxFrame*)NULL);
2108f33a 114
691d944f 115 Activate(false);
1d14c005 116
2108f33a
JS
117 if (deleteWindow)
118 {
119 delete frame;
691d944f 120 return true;
2108f33a 121 }
691d944f 122 return true;
2108f33a
JS
123}
124
125void DrawingView::OnCut(wxCommandEvent& WXUNUSED(event) )
126{
127 DrawingDocument *doc = (DrawingDocument *)GetDocument();
8325937e 128 doc->GetCommandProcessor()->Submit(new DrawingCommand(_T("Cut Last Segment"), DOODLE_CUT, doc, (DoodleSegment *) NULL));
2108f33a
JS
129}
130
131IMPLEMENT_DYNAMIC_CLASS(TextEditView, wxView)
132
133bool TextEditView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
134{
691d944f 135 frame = wxGetApp().CreateChildFrame(doc, this, false);
2108f33a
JS
136
137 int width, height;
138 frame->GetClientSize(&width, &height);
139 textsw = new MyTextWindow(this, frame, wxPoint(0, 0), wxSize(width, height), wxTE_MULTILINE);
8325937e 140 frame->SetTitle(_T("TextEditView"));
2108f33a
JS
141
142#ifdef __X__
143 // X seems to require a forced resize
144 int x, y;
145 frame->GetSize(&x, &y);
422d0ff0 146 frame->SetSize(wxDefaultCoord, wxDefaultCoord, x, y);
2108f33a
JS
147#endif
148
691d944f
WS
149 frame->Show(true);
150 Activate(true);
1d14c005 151
691d944f 152 return true;
2108f33a
JS
153}
154
155// Handled by wxTextWindow
156void TextEditView::OnDraw(wxDC *WXUNUSED(dc) )
157{
158}
159
160void TextEditView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
161{
162}
163
164bool TextEditView::OnClose(bool deleteWindow)
165{
166 if (!GetDocument()->Close())
691d944f 167 return false;
1d14c005 168
691d944f 169 Activate(false);
2108f33a
JS
170
171 if (deleteWindow)
172 {
173 delete frame;
691d944f 174 return true;
2108f33a 175 }
691d944f 176 return true;
2108f33a
JS
177}
178
179/*
180 * Window implementations
181 */
182
183BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
184 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
185END_EVENT_TABLE()
186
187// Define a constructor for my canvas
b9f933ab 188MyCanvas::MyCanvas(wxView *v, wxMDIChildFrame *frame, const wxPoint& pos, const wxSize& size, long style):
691d944f 189 wxScrolledWindow(frame, wxID_ANY, pos, size, style)
2108f33a
JS
190{
191 view = v;
192}
193
194// Define the repainting behaviour
195void MyCanvas::OnDraw(wxDC& dc)
196{
197 if (view)
198 view->OnDraw(& dc);
199}
200
201// This implements a tiny doodling program. Drag the mouse using
202// the left button.
203void MyCanvas::OnMouseEvent(wxMouseEvent& event)
204{
205 if (!view)
206 return;
1d14c005 207
c67daf87 208 static DoodleSegment *currentSegment = (DoodleSegment *) NULL;
2108f33a
JS
209
210 wxClientDC dc(this);
211 PrepareDC(dc);
212
213 dc.SetPen(*wxBLACK_PEN);
214
215 wxPoint pt(event.GetLogicalPosition(dc));
216
217 if (currentSegment && event.LeftUp())
218 {
b1d4dd7a 219 if (currentSegment->lines.GetCount() == 0)
2108f33a
JS
220 {
221 delete currentSegment;
c67daf87 222 currentSegment = (DoodleSegment *) NULL;
2108f33a
JS
223 }
224 else
225 {
226 // We've got a valid segment on mouse left up, so store it.
227 DrawingDocument *doc = (DrawingDocument *)view->GetDocument();
228
8325937e 229 doc->GetCommandProcessor()->Submit(new DrawingCommand(_T("Add Segment"), DOODLE_ADD, doc, currentSegment));
2108f33a 230
691d944f 231 view->GetDocument()->Modify(true);
c67daf87 232 currentSegment = (DoodleSegment *) NULL;
2108f33a
JS
233 }
234 }
1d14c005 235
2108f33a
JS
236 if (xpos > -1 && ypos > -1 && event.Dragging())
237 {
238 if (!currentSegment)
239 currentSegment = new DoodleSegment;
240
241 DoodleLine *newLine = new DoodleLine;
1d14c005 242 newLine->x1 = (long)xpos;
2108f33a 243 newLine->y1 = (long)ypos;
1d14c005 244 newLine->x2 = pt.x;
2108f33a
JS
245 newLine->y2 = pt.y;
246 currentSegment->lines.Append(newLine);
1d14c005 247
2108f33a
JS
248 dc.DrawLine( (long)xpos, (long)ypos, pt.x, pt.y);
249 }
250 xpos = pt.x;
251 ypos = pt.y;
252}
253
254// Define a constructor for my text subwindow
b9f933ab 255MyTextWindow::MyTextWindow(wxView *v, wxMDIChildFrame *frame, const wxPoint& pos, const wxSize& size, long style):
691d944f 256 wxTextCtrl(frame, wxID_ANY, _T(""), pos, size, style)
2108f33a
JS
257{
258 view = v;
259}
260
261