1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: View classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #if !wxUSE_DOC_VIEW_ARCHITECTURE
24 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
31 IMPLEMENT_DYNAMIC_CLASS(DrawingView
, wxView
)
33 // For drawing lines in a canvas
37 BEGIN_EVENT_TABLE(DrawingView
, wxView
)
38 EVT_MENU(DOODLE_CUT
, DrawingView::OnCut
)
41 // What to do when a view is created. Creates actual
42 // windows for displaying the view.
43 bool DrawingView::OnCreate(wxDocument
*doc
, long WXUNUSED(flags
) )
45 frame
= wxGetApp().CreateChildFrame(doc
, this, true);
46 frame
->SetTitle(_T("DrawingView"));
48 canvas
= GetMainFrame()->CreateCanvas(this, frame
);
50 // X seems to require a forced resize
52 frame
->GetSize(&x
, &y
);
53 frame
->SetSize(wxDefaultCoord
, wxDefaultCoord
, x
, y
);
61 // Sneakily gets used for default print/preview
62 // as well as drawing on the screen.
63 void DrawingView::OnDraw(wxDC
*dc
)
65 dc
->SetFont(*wxNORMAL_FONT
);
66 dc
->SetPen(*wxBLACK_PEN
);
68 wxList::compatibility_iterator node
= ((DrawingDocument
*)GetDocument())->GetDoodleSegments().GetFirst();
71 DoodleSegment
*seg
= (DoodleSegment
*)node
->GetData();
73 node
= node
->GetNext();
77 void DrawingView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
82 /* Is the following necessary?
89 wxClientDC dc(canvas);
97 // Clean up windows used for displaying the view.
98 bool DrawingView::OnClose(bool deleteWindow
)
100 if (!GetDocument()->Close())
103 // Clear the canvas in case we're in single-window mode,
104 // and the canvas stays.
105 canvas
->ClearBackground();
106 canvas
->view
= (wxView
*) NULL
;
107 canvas
= (MyCanvas
*) NULL
;
109 wxString
s(wxTheApp
->GetAppDisplayName());
113 SetFrame((wxFrame
*)NULL
);
125 void DrawingView::OnCut(wxCommandEvent
& WXUNUSED(event
) )
127 DrawingDocument
*doc
= (DrawingDocument
*)GetDocument();
128 doc
->GetCommandProcessor()->Submit(new DrawingCommand(_T("Cut Last Segment"), DOODLE_CUT
, doc
, (DoodleSegment
*) NULL
));
131 IMPLEMENT_DYNAMIC_CLASS(TextEditView
, wxView
)
133 bool TextEditView::OnCreate(wxDocument
*doc
, long WXUNUSED(flags
) )
135 frame
= wxGetApp().CreateChildFrame(doc
, this, false);
138 frame
->GetClientSize(&width
, &height
);
139 textsw
= new MyTextWindow(this, frame
, wxPoint(0, 0), wxSize(width
, height
), wxTE_MULTILINE
);
140 frame
->SetTitle(_T("TextEditView"));
143 // X seems to require a forced resize
145 frame
->GetSize(&x
, &y
);
146 frame
->SetSize(wxDefaultCoord
, wxDefaultCoord
, x
, y
);
155 // Handled by wxTextWindow
156 void TextEditView::OnDraw(wxDC
*WXUNUSED(dc
) )
160 void TextEditView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
) )
164 bool TextEditView::OnClose(bool deleteWindow
)
166 if (!GetDocument()->Close())
180 * Window implementations
183 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
184 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
187 // Define a constructor for my canvas
188 MyCanvas::MyCanvas(wxView
*v
, wxMDIChildFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, long style
):
189 wxScrolledWindow(frame
, wxID_ANY
, pos
, size
, style
)
194 // Define the repainting behaviour
195 void MyCanvas::OnDraw(wxDC
& dc
)
201 // This implements a tiny doodling program. Drag the mouse using
203 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
208 static DoodleSegment
*currentSegment
= (DoodleSegment
*) NULL
;
213 dc
.SetPen(*wxBLACK_PEN
);
215 wxPoint
pt(event
.GetLogicalPosition(dc
));
217 if (currentSegment
&& event
.LeftUp())
219 if (currentSegment
->lines
.GetCount() == 0)
221 delete currentSegment
;
222 currentSegment
= (DoodleSegment
*) NULL
;
226 // We've got a valid segment on mouse left up, so store it.
227 DrawingDocument
*doc
= (DrawingDocument
*)view
->GetDocument();
229 doc
->GetCommandProcessor()->Submit(new DrawingCommand(_T("Add Segment"), DOODLE_ADD
, doc
, currentSegment
));
231 view
->GetDocument()->Modify(true);
232 currentSegment
= (DoodleSegment
*) NULL
;
236 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
239 currentSegment
= new DoodleSegment
;
241 DoodleLine
*newLine
= new DoodleLine
;
242 newLine
->x1
= (long)xpos
;
243 newLine
->y1
= (long)ypos
;
246 currentSegment
->lines
.Append(newLine
);
248 dc
.DrawLine( (long)xpos
, (long)ypos
, pt
.x
, pt
.y
);
254 // Define a constructor for my text subwindow
255 MyTextWindow::MyTextWindow(wxView
*v
, wxMDIChildFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, long style
):
256 wxTextCtrl(frame
, wxID_ANY
, _T(""), pos
, size
, style
)