]>
git.saurik.com Git - wxWidgets.git/blob - samples/docview/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 !USE_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 if (!singleWindowMode
)
52 frame
= wxGetApp().CreateChildFrame(doc
, this, TRUE
);
53 frame
->SetTitle("DrawingView");
55 canvas
= GetMainFrame()->CreateCanvas(this, frame
);
57 // X seems to require a forced resize
59 frame
->GetSize(&x
, &y
);
67 frame
= GetMainFrame();
68 canvas
= GetMainFrame()->canvas
;
71 // Associate the appropriate frame with this view.
74 // Make sure the document manager knows that this is the
78 // Initialize the edit menu Undo and Redo items
79 doc
->GetCommandProcessor()->SetEditMenu(((MyFrame
*)frame
)->editMenu
);
80 doc
->GetCommandProcessor()->Initialize();
86 // Sneakily gets used for default print/preview
87 // as well as drawing on the screen.
88 void DrawingView::OnDraw(wxDC
*dc
)
90 dc
->SetFont(*wxNORMAL_FONT
);
91 dc
->SetPen(*wxBLACK_PEN
);
93 wxNode
*node
= ((DrawingDocument
*)GetDocument())->GetDoodleSegments().First();
96 DoodleSegment
*seg
= (DoodleSegment
*)node
->Data();
102 void DrawingView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
107 /* Is the following necessary?
114 wxClientDC dc(canvas);
122 // Clean up windows used for displaying the view.
123 bool DrawingView::OnClose(bool deleteWindow
)
125 if (!GetDocument()->Close())
128 // Clear the canvas in case we're in single-window mode,
129 // and the canvas stays.
131 canvas
->view
= (wxView
*) NULL
;
132 canvas
= (MyCanvas
*) NULL
;
134 wxString
s(wxTheApp
->GetAppName());
138 SetFrame((wxFrame
*) NULL
);
142 if (deleteWindow
&& !singleWindowMode
)
150 void DrawingView::OnCut(wxCommandEvent
& WXUNUSED(event
) )
152 DrawingDocument
*doc
= (DrawingDocument
*)GetDocument();
153 doc
->GetCommandProcessor()->Submit(new DrawingCommand((char *) "Cut Last Segment", DOODLE_CUT
, doc
, (DoodleSegment
*) NULL
));
156 IMPLEMENT_DYNAMIC_CLASS(TextEditView
, wxView
)
158 bool TextEditView::OnCreate(wxDocument
*doc
, long WXUNUSED(flags
) )
160 frame
= wxGetApp().CreateChildFrame(doc
, this, FALSE
);
163 frame
->GetClientSize(&width
, &height
);
164 textsw
= new MyTextWindow(this, frame
, wxPoint(0, 0), wxSize(width
, height
), wxTE_MULTILINE
);
165 frame
->SetTitle("TextEditView");
168 // X seems to require a forced resize
170 frame
->GetSize(&x
, &y
);
171 frame
->SetSize(x
, y
);
180 // Handled by wxTextWindow
181 void TextEditView::OnDraw(wxDC
*WXUNUSED(dc
) )
185 void TextEditView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
) )
189 bool TextEditView::OnClose(bool deleteWindow
)
191 if (!GetDocument()->Close())
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(wxView
*v
, wxFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, const long style
):
214 wxScrolledWindow(frame
, -1, pos
, size
, style
)
219 // Define the repainting behaviour
220 void MyCanvas::OnDraw(wxDC
& dc
)
226 // This implements a tiny doodling program. Drag the mouse using
228 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
233 static DoodleSegment
*currentSegment
= (DoodleSegment
*) NULL
;
238 dc
.SetPen(*wxBLACK_PEN
);
240 wxPoint
pt(event
.GetLogicalPosition(dc
));
242 if (currentSegment
&& event
.LeftUp())
244 if (currentSegment
->lines
.Number() == 0)
246 delete currentSegment
;
247 currentSegment
= (DoodleSegment
*) NULL
;
251 // We've got a valid segment on mouse left up, so store it.
252 DrawingDocument
*doc
= (DrawingDocument
*)view
->GetDocument();
254 doc
->GetCommandProcessor()->Submit(new DrawingCommand("Add Segment", DOODLE_ADD
, doc
, currentSegment
));
256 view
->GetDocument()->Modify(TRUE
);
257 currentSegment
= (DoodleSegment
*) 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
->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
*v
, wxFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, const long style
):
281 wxTextCtrl(frame
, -1, "", pos
, size
, style
)