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 if (!singleWindowMode
)
48 frame
= wxGetApp().CreateChildFrame(doc
, this, true);
49 frame
->SetTitle(_T("DrawingView"));
51 canvas
= GetMainFrame()->CreateCanvas(this, frame
);
53 // X seems to require a forced resize
55 frame
->GetSize(&x
, &y
);
56 frame
->SetSize(wxDefaultCoord
, wxDefaultCoord
, x
, y
);
63 frame
= GetMainFrame();
64 canvas
= GetMainFrame()->canvas
;
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
*)frame
)->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
= ((DrawingDocument
*)GetDocument())->GetDoodleSegments().GetFirst();
92 DoodleSegment
*seg
= (DoodleSegment
*)node
->GetData();
94 node
= node
->GetNext();
98 void DrawingView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
103 /* Is the following necessary?
110 wxClientDC dc(canvas);
118 // Clean up windows used for displaying the view.
119 bool DrawingView::OnClose(bool deleteWindow
)
121 if (!GetDocument()->Close())
124 // Clear the canvas in case we're in single-window mode,
125 // and the canvas stays.
126 canvas
->ClearBackground();
127 canvas
->view
= (wxView
*) NULL
;
128 canvas
= (MyCanvas
*) NULL
;
130 wxString
s(wxTheApp
->GetAppName());
134 SetFrame((wxFrame
*) NULL
);
138 if (deleteWindow
&& !singleWindowMode
)
146 void DrawingView::OnCut(wxCommandEvent
& WXUNUSED(event
) )
148 DrawingDocument
*doc
= (DrawingDocument
*)GetDocument();
149 doc
->GetCommandProcessor()->Submit(new DrawingCommand(_T("Cut Last Segment"), DOODLE_CUT
, doc
, (DoodleSegment
*) NULL
));
152 IMPLEMENT_DYNAMIC_CLASS(TextEditView
, wxView
)
154 bool TextEditView::OnCreate(wxDocument
*doc
, long WXUNUSED(flags
) )
156 frame
= wxGetApp().CreateChildFrame(doc
, this, false);
159 frame
->GetClientSize(&width
, &height
);
160 textsw
= new MyTextWindow(this, frame
, wxPoint(0, 0), wxSize(width
, height
), wxTE_MULTILINE
);
161 frame
->SetTitle(_T("TextEditView"));
164 // X seems to require a forced resize
166 frame
->GetSize(&x
, &y
);
167 frame
->SetSize(wxDefaultCoord
, wxDefaultCoord
, x
, y
);
176 // Handled by wxTextWindow
177 void TextEditView::OnDraw(wxDC
*WXUNUSED(dc
) )
181 void TextEditView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
) )
185 bool TextEditView::OnClose(bool deleteWindow
)
187 if (!GetDocument()->Close())
201 * Window implementations
204 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
205 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
208 // Define a constructor for my canvas
209 MyCanvas::MyCanvas(wxView
*v
, wxFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, const long style
):
210 wxScrolledWindow(frame
, wxID_ANY
, pos
, size
, style
)
215 // Define the repainting behaviour
216 void MyCanvas::OnDraw(wxDC
& dc
)
222 // This implements a tiny doodling program. Drag the mouse using
224 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
229 static DoodleSegment
*currentSegment
= (DoodleSegment
*) NULL
;
234 dc
.SetPen(*wxBLACK_PEN
);
236 wxPoint
pt(event
.GetLogicalPosition(dc
));
238 if (currentSegment
&& event
.LeftUp())
240 if (currentSegment
->lines
.GetCount() == 0)
242 delete currentSegment
;
243 currentSegment
= (DoodleSegment
*) NULL
;
247 // We've got a valid segment on mouse left up, so store it.
248 DrawingDocument
*doc
= (DrawingDocument
*)view
->GetDocument();
250 doc
->GetCommandProcessor()->Submit(new DrawingCommand(_T("Add Segment"), DOODLE_ADD
, doc
, currentSegment
));
252 view
->GetDocument()->Modify(true);
253 currentSegment
= (DoodleSegment
*) NULL
;
257 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
260 currentSegment
= new DoodleSegment
;
262 DoodleLine
*newLine
= new DoodleLine
;
263 newLine
->x1
= (long)xpos
;
264 newLine
->y1
= (long)ypos
;
267 currentSegment
->lines
.Append(newLine
);
269 dc
.DrawLine( (long)xpos
, (long)ypos
, pt
.x
, pt
.y
);
275 // Define a constructor for my text subwindow
276 MyTextWindow::MyTextWindow(wxView
*v
, wxFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, const long style
):
277 wxTextCtrl(frame
, wxID_ANY
, _T(""), pos
, size
, style
)