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