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