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
34 static float xpos
= -1;
35 static float ypos
= -1;
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 m_frame
= wxGetApp().CreateChildFrame(doc
, this, true);
46 m_frame
->SetTitle(wxT("DrawingView"));
48 m_canvas
= GetMainFrame()->CreateCanvas(this, m_frame
);
50 // X seems to require a forced resize
52 m_frame
->GetSize(&x
, &y
);
53 m_frame
->SetSize(wxDefaultCoord
, wxDefaultCoord
, x
, y
);
61 DrawingDocument
* DrawingView::GetDocument()
63 return wxStaticCast(wxView::GetDocument(), DrawingDocument
);
66 // Sneakily gets used for default print/preview
67 // as well as drawing on the screen.
68 void DrawingView::OnDraw(wxDC
*dc
)
70 dc
->SetFont(*wxNORMAL_FONT
);
71 dc
->SetPen(*wxBLACK_PEN
);
73 wxList::compatibility_iterator node
= GetDocument()->GetDoodleSegments().GetFirst();
76 DoodleSegment
* seg
= (DoodleSegment
*)node
->GetData();
78 node
= node
->GetNext();
82 void DrawingView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
87 /* Is the following necessary?
94 wxClientDC dc(canvas);
102 // Clean up windows used for displaying the view.
103 bool DrawingView::OnClose(bool deleteWindow
)
105 if (!GetDocument()->Close())
108 // Clear the canvas in case we're in single-window mode,
109 // and the canvas stays.
110 m_canvas
->ClearBackground();
111 m_canvas
->m_view
= NULL
;
114 wxString
s(wxTheApp
->GetAppDisplayName());
116 m_frame
->SetTitle(s
);
130 void DrawingView::OnCut(wxCommandEvent
& WXUNUSED(event
) )
132 DrawingDocument
* doc
= GetDocument();
133 doc
->GetCommandProcessor()->Submit(new DrawingCommand(wxT("Cut Last Segment"), DOODLE_CUT
, doc
, NULL
));
136 IMPLEMENT_DYNAMIC_CLASS(TextEditView
, wxView
)
138 BEGIN_EVENT_TABLE(TextEditView
, wxView
)
139 EVT_MENU(wxID_COPY
, TextEditView::OnCopy
)
140 EVT_MENU(wxID_PASTE
, TextEditView::OnPaste
)
141 EVT_MENU(wxID_SELECTALL
, TextEditView::OnSelectAll
)
144 bool TextEditView::OnCreate(wxDocument
* doc
, long WXUNUSED(flags
) )
146 m_frame
= wxGetApp().CreateChildFrame(doc
, this, false);
148 wxSize size
= m_frame
->GetClientSize();
149 m_textsw
= new MyTextWindow(this, m_frame
, wxPoint(0, 0), size
, wxTE_MULTILINE
);
150 m_frame
->SetTitle(wxT("TextEditView"));
153 // X seems to require a forced resize
155 m_frame
->GetSize(&x
, &y
);
156 m_frame
->SetSize(wxDefaultCoord
, wxDefaultCoord
, x
, y
);
165 // Handled by wxTextWindow
166 void TextEditView::OnDraw(wxDC
*WXUNUSED(dc
) )
170 void TextEditView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
) )
174 bool TextEditView::OnClose(bool deleteWindow
)
176 if (!GetDocument()->Close())
189 bool TextEditView::ProcessEvent(wxEvent
& event
)
191 bool processed
= false;
192 if (!processed
) switch (event
.GetId())
197 processed
= m_textsw
->ProcessEvent(event
);
200 if (!processed
) processed
= wxView::ProcessEvent(event
);
205 * Window implementations
208 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
209 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
212 // Define a constructor for my canvas
213 MyCanvas::MyCanvas(DrawingView
* view
, wxMDIChildFrame
* frame
, const wxPoint
& pos
, const wxSize
& size
, long style
):
214 wxScrolledWindow(frame
, wxID_ANY
, pos
, size
, style
)
219 // Define the repainting behaviour
220 void MyCanvas::OnDraw(wxDC
& dc
)
223 m_view
->OnDraw(& dc
);
226 // This implements a tiny doodling program. Drag the mouse using
228 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
233 static DoodleSegment
* currentSegment
= NULL
;
238 dc
.SetPen(*wxBLACK_PEN
);
240 wxPoint
pt(event
.GetLogicalPosition(dc
));
242 if (currentSegment
&& event
.LeftUp())
244 if (currentSegment
->m_lines
.GetCount() == 0)
246 delete currentSegment
;
247 currentSegment
= NULL
;
251 // We've got a valid segment on mouse left up, so store it.
252 DrawingDocument
* doc
= m_view
->GetDocument();
254 doc
->GetCommandProcessor()->Submit(new DrawingCommand(wxT("Add Segment"), DOODLE_ADD
, doc
, currentSegment
));
256 m_view
->GetDocument()->Modify(true);
257 currentSegment
= NULL
;
261 if ( (xpos
> -1) && (ypos
> -1) && event
.Dragging())
264 currentSegment
= new DoodleSegment
;
266 DoodleLine
*newLine
= new DoodleLine
;
267 newLine
->x1
= (long)xpos
;
268 newLine
->y1
= (long)ypos
;
271 currentSegment
->m_lines
.Append(newLine
);
273 dc
.DrawLine( (long)xpos
, (long)ypos
, pt
.x
, pt
.y
);
279 // Define a constructor for my text subwindow
280 MyTextWindow::MyTextWindow(wxView
* view
, wxMDIChildFrame
* frame
, const wxPoint
& pos
, const wxSize
& size
, long style
):
281 wxTextCtrl(frame
, wxID_ANY
, wxEmptyString
, pos
, size
, style
)