1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: View classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 // #pragma implementation
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
27 #if !wxUSE_DOC_VIEW_ARCHITECTURE
28 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
35 IMPLEMENT_DYNAMIC_CLASS(DrawingView
, wxView
)
37 // For drawing lines in a canvas
41 BEGIN_EVENT_TABLE(DrawingView
, wxView
)
42 EVT_MENU(DOODLE_CUT
, DrawingView::OnCut
)
45 // What to do when a view is created. Creates actual
46 // windows for displaying the view.
47 bool DrawingView::OnCreate(wxDocument
*doc
, long WXUNUSED(flags
) )
49 frame
= wxGetApp().CreateChildFrame(doc
, this, true);
50 frame
->SetTitle(_T("DrawingView"));
52 canvas
= GetMainFrame()->CreateCanvas(this, frame
);
54 // X seems to require a forced resize
56 frame
->GetSize(&x
, &y
);
57 frame
->SetSize(wxDefaultPosition
.x
, wxDefaultPosition
.y
, x
, y
);
65 // Sneakily gets used for default print/preview
66 // as well as drawing on the screen.
67 void DrawingView::OnDraw(wxDC
*dc
)
69 dc
->SetFont(*wxNORMAL_FONT
);
70 dc
->SetPen(*wxBLACK_PEN
);
72 wxList::compatibility_iterator node
= ((DrawingDocument
*)GetDocument())->GetDoodleSegments().GetFirst();
75 DoodleSegment
*seg
= (DoodleSegment
*)node
->GetData();
77 node
= node
->GetNext();
81 void DrawingView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
86 /* Is the following necessary?
93 wxClientDC dc(canvas);
101 // Clean up windows used for displaying the view.
102 bool DrawingView::OnClose(bool deleteWindow
)
104 if (!GetDocument()->Close())
107 // Clear the canvas in case we're in single-window mode,
108 // and the canvas stays.
109 canvas
->ClearBackground();
110 canvas
->view
= (wxView
*) NULL
;
111 canvas
= (MyCanvas
*) NULL
;
113 wxString
s(wxTheApp
->GetAppName());
117 SetFrame((wxFrame
*)NULL
);
129 void DrawingView::OnCut(wxCommandEvent
& WXUNUSED(event
) )
131 DrawingDocument
*doc
= (DrawingDocument
*)GetDocument();
132 doc
->GetCommandProcessor()->Submit(new DrawingCommand(_T("Cut Last Segment"), DOODLE_CUT
, doc
, (DoodleSegment
*) NULL
));
135 IMPLEMENT_DYNAMIC_CLASS(TextEditView
, wxView
)
137 bool TextEditView::OnCreate(wxDocument
*doc
, long WXUNUSED(flags
) )
139 frame
= wxGetApp().CreateChildFrame(doc
, this, false);
142 frame
->GetClientSize(&width
, &height
);
143 textsw
= new MyTextWindow(this, frame
, wxPoint(0, 0), wxSize(width
, height
), wxTE_MULTILINE
);
144 frame
->SetTitle(_T("TextEditView"));
147 // X seems to require a forced resize
149 frame
->GetSize(&x
, &y
);
150 frame
->SetSize(wxDefaultPosition
.x
, wxDefaultPosition
.y
, x
, y
);
159 // Handled by wxTextWindow
160 void TextEditView::OnDraw(wxDC
*WXUNUSED(dc
) )
164 void TextEditView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
) )
168 bool TextEditView::OnClose(bool deleteWindow
)
170 if (!GetDocument()->Close())
184 * Window implementations
187 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
188 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
191 // Define a constructor for my canvas
192 MyCanvas::MyCanvas(wxView
*v
, wxMDIChildFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, long style
):
193 wxScrolledWindow(frame
, wxID_ANY
, pos
, size
, style
)
198 // Define the repainting behaviour
199 void MyCanvas::OnDraw(wxDC
& dc
)
205 // This implements a tiny doodling program. Drag the mouse using
207 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
212 static DoodleSegment
*currentSegment
= (DoodleSegment
*) NULL
;
217 dc
.SetPen(*wxBLACK_PEN
);
219 wxPoint
pt(event
.GetLogicalPosition(dc
));
221 if (currentSegment
&& event
.LeftUp())
223 if (currentSegment
->lines
.GetCount() == 0)
225 delete currentSegment
;
226 currentSegment
= (DoodleSegment
*) NULL
;
230 // We've got a valid segment on mouse left up, so store it.
231 DrawingDocument
*doc
= (DrawingDocument
*)view
->GetDocument();
233 doc
->GetCommandProcessor()->Submit(new DrawingCommand(_T("Add Segment"), DOODLE_ADD
, doc
, currentSegment
));
235 view
->GetDocument()->Modify(true);
236 currentSegment
= (DoodleSegment
*) NULL
;
240 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
243 currentSegment
= new DoodleSegment
;
245 DoodleLine
*newLine
= new DoodleLine
;
246 newLine
->x1
= (long)xpos
;
247 newLine
->y1
= (long)ypos
;
250 currentSegment
->lines
.Append(newLine
);
252 dc
.DrawLine( (long)xpos
, (long)ypos
, pt
.x
, pt
.y
);
258 // Define a constructor for my text subwindow
259 MyTextWindow::MyTextWindow(wxView
*v
, wxMDIChildFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, long style
):
260 wxTextCtrl(frame
, wxID_ANY
, _T(""), pos
, size
, style
)