]> git.saurik.com Git - wxWidgets.git/blame - samples/docview/view.cpp
[ 1936700 ] wxCAL_SHOW_WEEK_NUMBERS, slightly modified
[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;
176 frame->GetSize(&x, &y);
422d0ff0 177 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 {
6bdf5153 204 wxDELETE(m_frame)
f2aea0d1 205 return true;
f6bcfd97 206 }
f2aea0d1 207 return true;
457814b5
JS
208}
209
210/*
f6bcfd97
BP
211* Window implementations
212*/
457814b5
JS
213
214BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
215 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
216END_EVENT_TABLE()
217
218// Define a constructor for my canvas
6bdf5153 219MyCanvas::MyCanvas(DrawingView* view, wxFrame* frame, const wxPoint& pos, const wxSize& size, const long style):
f2aea0d1 220 wxScrolledWindow(frame, wxID_ANY, pos, size, style)
457814b5 221{
6bdf5153 222 m_view = view;
457814b5
JS
223}
224
225// Define the repainting behaviour
226void MyCanvas::OnDraw(wxDC& dc)
227{
6bdf5153
VZ
228 if (m_view)
229 m_view->OnDraw(& dc);
457814b5
JS
230}
231
232// This implements a tiny doodling program. Drag the mouse using
233// the left button.
234void MyCanvas::OnMouseEvent(wxMouseEvent& event)
235{
6bdf5153 236 if (!m_view)
f6bcfd97 237 return;
6bdf5153
VZ
238
239 static DoodleSegment *currentSegment = NULL;
240
f6bcfd97
BP
241 wxClientDC dc(this);
242 PrepareDC(dc);
6bdf5153 243
f6bcfd97 244 dc.SetPen(*wxBLACK_PEN);
6bdf5153 245
f6bcfd97 246 wxPoint pt(event.GetLogicalPosition(dc));
6bdf5153 247
f6bcfd97 248 if (currentSegment && event.LeftUp())
457814b5 249 {
6bdf5153 250 if (currentSegment->m_lines.GetCount() == 0)
f6bcfd97
BP
251 {
252 delete currentSegment;
6bdf5153 253 currentSegment = NULL;
f6bcfd97
BP
254 }
255 else
256 {
257 // We've got a valid segment on mouse left up, so store it.
6bdf5153
VZ
258 DrawingDocument* doc = m_view->GetDocument();
259
260 doc->GetCommandProcessor()->Submit(new DrawingCommand(wxT("Add Segment"), DOODLE_ADD, doc, currentSegment));
261
262 m_view->GetDocument()->Modify(true);
263 currentSegment = NULL;
f6bcfd97 264 }
457814b5 265 }
6bdf5153
VZ
266
267 if ( (xpos > -1) && (ypos > -1) && event.Dragging())
457814b5 268 {
f6bcfd97
BP
269 if (!currentSegment)
270 currentSegment = new DoodleSegment;
6bdf5153 271
f6bcfd97 272 DoodleLine *newLine = new DoodleLine;
6bdf5153 273 newLine->x1 = (long)xpos;
f6bcfd97 274 newLine->y1 = (long)ypos;
6bdf5153 275 newLine->x2 = pt.x;
f6bcfd97 276 newLine->y2 = pt.y;
6bdf5153
VZ
277 currentSegment->m_lines.Append(newLine);
278
f6bcfd97 279 dc.DrawLine( (long)xpos, (long)ypos, pt.x, pt.y);
457814b5 280 }
f6bcfd97
BP
281 xpos = pt.x;
282 ypos = pt.y;
457814b5
JS
283}
284
285// Define a constructor for my text subwindow
6bdf5153
VZ
286MyTextWindow::MyTextWindow(wxView* view, wxFrame* frame, const wxPoint& pos, const wxSize& size, const long style):
287 wxTextCtrl(frame, wxID_ANY, wxEmptyString, pos, size, style)
457814b5 288{
6bdf5153 289 m_view = view;
457814b5
JS
290}
291
292