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 m_frame
->GetSize(&x
, &y
);
177 m_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())
210 * Window implementations
213 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
214 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
217 // Define a constructor for my canvas
218 MyCanvas::MyCanvas(DrawingView
* view
, wxFrame
* frame
, const wxPoint
& pos
, const wxSize
& size
, const long style
):
219 wxScrolledWindow(frame
, wxID_ANY
, pos
, size
, style
)
224 // Define the repainting behaviour
225 void MyCanvas::OnDraw(wxDC
& dc
)
228 m_view
->OnDraw(& dc
);
231 // This implements a tiny doodling program. Drag the mouse using
233 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
238 static DoodleSegment
*currentSegment
= NULL
;
243 dc
.SetPen(*wxBLACK_PEN
);
245 wxPoint
pt(event
.GetLogicalPosition(dc
));
247 if (currentSegment
&& event
.LeftUp())
249 if (currentSegment
->m_lines
.GetCount() == 0)
251 delete currentSegment
;
252 currentSegment
= NULL
;
256 // We've got a valid segment on mouse left up, so store it.
257 DrawingDocument
* doc
= m_view
->GetDocument();
259 doc
->GetCommandProcessor()->Submit(new DrawingCommand(wxT("Add Segment"), DOODLE_ADD
, doc
, currentSegment
));
261 m_view
->GetDocument()->Modify(true);
262 currentSegment
= NULL
;
266 if ( (xpos
> -1) && (ypos
> -1) && event
.Dragging())
269 currentSegment
= new DoodleSegment
;
271 DoodleLine
*newLine
= new DoodleLine
;
272 newLine
->x1
= (long)xpos
;
273 newLine
->y1
= (long)ypos
;
276 currentSegment
->m_lines
.Append(newLine
);
278 dc
.DrawLine( (long)xpos
, (long)ypos
, pt
.x
, pt
.y
);
284 // Define a constructor for my text subwindow
285 MyTextWindow::MyTextWindow(wxView
* view
, wxFrame
* frame
, const wxPoint
& pos
, const wxSize
& size
, const long style
):
286 wxTextCtrl(frame
, wxID_ANY
, wxEmptyString
, pos
, size
, style
)