]>
git.saurik.com Git - wxWidgets.git/blob - samples/docvwmdi/view.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: View classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 // #pragma implementation
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
27 #if !wxUSE_DOC_VIEW_ARCHITECTURE
28 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in wx_setup.h!
35 IMPLEMENT_DYNAMIC_CLASS(DrawingView
, wxView
)
37 // For drawing lines in a canvas
41 BEGIN_EVENT_TABLE(DrawingView
, wxView
)
42 EVT_MENU(DOODLE_CUT
, DrawingView::OnCut
)
45 // What to do when a view is created. Creates actual
46 // windows for displaying the view.
47 bool DrawingView::OnCreate(wxDocument
*doc
, long WXUNUSED(flags
) )
49 frame
= wxGetApp().CreateChildFrame(doc
, this, TRUE
);
50 frame
->SetTitle("DrawingView");
52 canvas
= GetMainFrame()->CreateCanvas(this, frame
);
54 // X seems to require a forced resize
56 frame
->GetSize(&x
, &y
);
57 frame
->SetSize(-1, -1, x
, y
);
64 // Sneakily gets used for default print/preview
65 // as well as drawing on the screen.
66 void DrawingView::OnDraw(wxDC
*dc
)
68 dc
->SetFont(*wxNORMAL_FONT
);
69 dc
->SetPen(*wxBLACK_PEN
);
71 wxNode
*node
= ((DrawingDocument
*)GetDocument())->GetDoodleSegments().First();
74 DoodleSegment
*seg
= (DoodleSegment
*)node
->Data();
80 void DrawingView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
85 /* Is the following necessary?
92 wxClientDC dc(canvas);
100 // Clean up windows used for displaying the view.
101 bool DrawingView::OnClose(bool deleteWindow
)
103 if (!GetDocument()->Close())
106 // Clear the canvas in case we're in single-window mode,
107 // and the canvas stays.
109 canvas
->view
= (wxView
*) NULL
;
110 canvas
= (MyCanvas
*) NULL
;
112 wxString
s(wxTheApp
->GetAppName());
116 SetFrame((wxFrame
*)NULL
);
128 void DrawingView::OnCut(wxCommandEvent
& WXUNUSED(event
) )
130 DrawingDocument
*doc
= (DrawingDocument
*)GetDocument();
131 doc
->GetCommandProcessor()->Submit(new DrawingCommand((const wxString
) "Cut Last Segment", DOODLE_CUT
, doc
, (DoodleSegment
*) NULL
));
134 IMPLEMENT_DYNAMIC_CLASS(TextEditView
, wxView
)
136 bool TextEditView::OnCreate(wxDocument
*doc
, long WXUNUSED(flags
) )
138 frame
= wxGetApp().CreateChildFrame(doc
, this, FALSE
);
141 frame
->GetClientSize(&width
, &height
);
142 textsw
= new MyTextWindow(this, frame
, wxPoint(0, 0), wxSize(width
, height
), wxTE_MULTILINE
);
143 frame
->SetTitle("TextEditView");
146 // X seems to require a forced resize
148 frame
->GetSize(&x
, &y
);
149 frame
->SetSize(-1, -1, x
, y
);
158 // Handled by wxTextWindow
159 void TextEditView::OnDraw(wxDC
*WXUNUSED(dc
) )
163 void TextEditView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
) )
167 bool TextEditView::OnClose(bool deleteWindow
)
169 if (!GetDocument()->Close())
183 * Window implementations
186 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
187 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
190 // Define a constructor for my canvas
191 MyCanvas::MyCanvas(wxView
*v
, wxFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, const long style
):
192 wxScrolledWindow(frame
, -1, pos
, size
, style
)
197 // Define the repainting behaviour
198 void MyCanvas::OnDraw(wxDC
& dc
)
204 // This implements a tiny doodling program. Drag the mouse using
206 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
211 static DoodleSegment
*currentSegment
= (DoodleSegment
*) NULL
;
216 dc
.SetPen(*wxBLACK_PEN
);
218 wxPoint
pt(event
.GetLogicalPosition(dc
));
220 if (currentSegment
&& event
.LeftUp())
222 if (currentSegment
->lines
.Number() == 0)
224 delete currentSegment
;
225 currentSegment
= (DoodleSegment
*) NULL
;
229 // We've got a valid segment on mouse left up, so store it.
230 DrawingDocument
*doc
= (DrawingDocument
*)view
->GetDocument();
232 doc
->GetCommandProcessor()->Submit(new DrawingCommand("Add Segment", DOODLE_ADD
, doc
, currentSegment
));
234 view
->GetDocument()->Modify(TRUE
);
235 currentSegment
= (DoodleSegment
*) NULL
;
239 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
242 currentSegment
= new DoodleSegment
;
244 DoodleLine
*newLine
= new DoodleLine
;
245 newLine
->x1
= (long)xpos
;
246 newLine
->y1
= (long)ypos
;
249 currentSegment
->lines
.Append(newLine
);
251 dc
.DrawLine( (long)xpos
, (long)ypos
, pt
.x
, pt
.y
);
257 // Define a constructor for my text subwindow
258 MyTextWindow::MyTextWindow(wxView
*v
, wxFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, const long style
):
259 wxTextCtrl(frame
, -1, "", pos
, size
, style
)