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 if (!singleWindowMode
)
48 m_frame
= wxGetApp().CreateChildFrame(doc
, this, true);
49 m_frame
->SetTitle(wxT("DrawingView"));
51 m_canvas
= GetMainFrame()->CreateCanvas(this, m_frame
);
53 // X seems to require a forced resize
55 m_frame
->GetSize(&x
, &y
);
56 m_frame
->SetSize(wxDefaultCoord
, wxDefaultCoord
, x
, y
);
63 m_frame
= GetMainFrame();
64 m_canvas
= GetMainFrame()->m_canvas
;
65 m_canvas
->m_view
= this;
67 // Associate the appropriate frame with this view.
70 // Make sure the document manager knows that this is the
74 // Initialize the edit menu Undo and Redo items
75 doc
->GetCommandProcessor()->SetEditMenu(((MyFrame
*)m_frame
)->m_editMenu
);
76 doc
->GetCommandProcessor()->Initialize();
82 // Sneakily gets used for default print/preview
83 // as well as drawing on the screen.
84 void DrawingView::OnDraw(wxDC
*dc
)
86 dc
->SetFont(*wxNORMAL_FONT
);
87 dc
->SetPen(*wxBLACK_PEN
);
89 wxList::compatibility_iterator node
= GetDocument()->GetDoodleSegments().GetFirst();
92 DoodleSegment
*seg
= (DoodleSegment
*)node
->GetData();
94 node
= node
->GetNext();
98 DrawingDocument
* DrawingView::GetDocument()
100 return wxStaticCast(wxView::GetDocument(), DrawingDocument
);
103 void DrawingView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
108 /* Is the following necessary?
115 wxClientDC dc(canvas);
123 // Clean up windows used for displaying the view.
124 bool DrawingView::OnClose(bool deleteWindow
)
126 if (!GetDocument()->Close())
129 // Clear the canvas in case we're in single-window mode,
130 // and the canvas stays.
131 m_canvas
->ClearBackground();
132 m_canvas
->m_view
= NULL
;
135 wxString
s(wxTheApp
->GetAppDisplayName());
137 m_frame
->SetTitle(s
);
143 if (deleteWindow
&& !singleWindowMode
)
151 void DrawingView::OnCut(wxCommandEvent
& WXUNUSED(event
) )
153 DrawingDocument
* doc
= GetDocument();
154 doc
->GetCommandProcessor()->Submit(new DrawingCommand(wxT("Cut Last Segment"), DOODLE_CUT
, doc
, NULL
));
157 IMPLEMENT_DYNAMIC_CLASS(TextEditView
, wxView
)
159 BEGIN_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
)
165 bool TextEditView::OnCreate(wxDocument
*doc
, long WXUNUSED(flags
) )
167 m_frame
= wxGetApp().CreateChildFrame(doc
, this, false);
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"));
174 // X seems to require a forced resize
176 frame
->GetSize(&x
, &y
);
177 frame
->SetSize(wxDefaultCoord
, wxDefaultCoord
, x
, y
);
186 // Handled by wxTextWindow
187 void TextEditView::OnDraw(wxDC
*WXUNUSED(dc
) )
191 void TextEditView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
) )
195 bool TextEditView::OnClose(bool deleteWindow
)
197 if (!GetDocument()->Close())
211 * Window implementations
214 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
215 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
218 // Define a constructor for my canvas
219 MyCanvas::MyCanvas(DrawingView
* view
, wxFrame
* frame
, const wxPoint
& pos
, const wxSize
& size
, const long style
):
220 wxScrolledWindow(frame
, wxID_ANY
, pos
, size
, style
)
225 // Define the repainting behaviour
226 void MyCanvas::OnDraw(wxDC
& dc
)
229 m_view
->OnDraw(& dc
);
232 // This implements a tiny doodling program. Drag the mouse using
234 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
239 static DoodleSegment
*currentSegment
= NULL
;
244 dc
.SetPen(*wxBLACK_PEN
);
246 wxPoint
pt(event
.GetLogicalPosition(dc
));
248 if (currentSegment
&& event
.LeftUp())
250 if (currentSegment
->m_lines
.GetCount() == 0)
252 delete currentSegment
;
253 currentSegment
= NULL
;
257 // We've got a valid segment on mouse left up, so store it.
258 DrawingDocument
* doc
= m_view
->GetDocument();
260 doc
->GetCommandProcessor()->Submit(new DrawingCommand(wxT("Add Segment"), DOODLE_ADD
, doc
, currentSegment
));
262 m_view
->GetDocument()->Modify(true);
263 currentSegment
= NULL
;
267 if ( (xpos
> -1) && (ypos
> -1) && event
.Dragging())
270 currentSegment
= new DoodleSegment
;
272 DoodleLine
*newLine
= new DoodleLine
;
273 newLine
->x1
= (long)xpos
;
274 newLine
->y1
= (long)ypos
;
277 currentSegment
->m_lines
.Append(newLine
);
279 dc
.DrawLine( (long)xpos
, (long)ypos
, pt
.x
, pt
.y
);
285 // Define a constructor for my text subwindow
286 MyTextWindow::MyTextWindow(wxView
* view
, wxFrame
* frame
, const wxPoint
& pos
, const wxSize
& size
, const long style
):
287 wxTextCtrl(frame
, wxID_ANY
, wxEmptyString
, pos
, size
, style
)