1 /////////////////////////////////////////////////////////////////////////////
2 // Name: contrib/samples/ogl/ogledit/view.cpp
3 // Purpose: Implements view functionality in OGLEdit
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
23 #include "wx/colordlg.h"
25 #if !wxUSE_DOC_VIEW_ARCHITECTURE
26 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in wx_setup.h!
34 IMPLEMENT_DYNAMIC_CLASS(DiagramView
, wxView
)
36 BEGIN_EVENT_TABLE(DiagramView
, wxView
)
37 EVT_MENU(wxID_CUT
, DiagramView::OnCut
)
38 EVT_MENU(OGLEDIT_CHANGE_BACKGROUND_COLOUR
, DiagramView::OnChangeBackgroundColour
)
39 EVT_MENU(OGLEDIT_EDIT_LABEL
, DiagramView::OnEditLabel
)
42 // What to do when a view is created. Creates actual
43 // windows for displaying the view.
44 bool DiagramView::OnCreate(wxDocument
*doc
, long WXUNUSED(flags
))
46 frame
= GetMainFrame();
47 canvas
= GetMainFrame()->canvas
;
53 // Initialize the edit menu Undo and Redo items
54 doc
->GetCommandProcessor()->SetEditMenu(((MyFrame
*)frame
)->editMenu
);
55 doc
->GetCommandProcessor()->Initialize();
57 wxShapeCanvas
*shapeCanvas
= (wxShapeCanvas
*)canvas
;
58 DiagramDocument
*diagramDoc
= (DiagramDocument
*)doc
;
59 shapeCanvas
->SetDiagram(diagramDoc
->GetDiagram());
60 diagramDoc
->GetDiagram()->SetCanvas(shapeCanvas
);
65 #define CENTER false // Place the drawing to the center of the page
68 // Sneakily gets used for default print/preview
69 // as well as drawing on the screen.
70 void DiagramView::OnDraw(wxDC
*dc
)
73 /* You might use THIS code if you were scaling
74 * graphics of known size to fit on the page.
78 // We need to adjust for the graphic size, a formula will be added
81 // A better way of find the maxium values would be to search through
84 // Let's have at least 10 device units margin
88 // Add the margin to the graphic size
89 maxX
+= (2 * marginX
);
90 maxY
+= (2 * marginY
);
92 // Get the size of the DC in pixels
95 // Calculate a suitable scaling factor
96 float scaleX
= (float) (w
/ maxX
);
97 float scaleY
= (float) (h
/ maxY
);
99 // Use x or y scaling factor, whichever fits on the DC
100 float actualScale
= wxMin (scaleX
, scaleY
);
103 // Calculate the position on the DC for centring the graphic
105 // center the drawing
106 posX
= (float) ((w
- (200 * actualScale
)) / 2.0);
107 posY
= (float) ((h
- (200 * actualScale
)) / 2.0);
109 // Use defined presets
115 // Set the scale and origin
116 dc
->SetUserScale (actualScale
, actualScale
);
117 dc
->SetDeviceOrigin ((long) posX
, (long) posY
);
119 // This part was added to preform the print preview and printing functions
121 wxDiagram
*diagram_p
=((DiagramDocument
*)GetDocument())->GetDiagram(); // Get the current diagram
122 if (diagram_p
->GetShapeList())
124 /* wxCursor *old_cursor = NULL; */
125 wxObjectList::compatibility_iterator current
= diagram_p
->GetShapeList()->GetFirst();
127 while (current
) // Loop through the entire list of shapes
129 wxShape
*object
= (wxShape
*)current
->GetData();
130 if (!object
->GetParent())
132 object
->Draw(* dc
); // Draw the shape onto our printing dc
134 current
= current
->GetNext(); // Procede to the next shape in the list
139 void DiagramView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
145 // Clean up windows used for displaying the view.
146 bool DiagramView::OnClose(bool WXUNUSED(deleteWindow
))
148 if (!GetDocument()->Close())
151 DiagramDocument
*diagramDoc
= (DiagramDocument
*)GetDocument();
152 diagramDoc
->GetDiagram()->SetCanvas(NULL
);
154 canvas
->ClearBackground();
155 canvas
->SetDiagram(NULL
);
159 wxString s
= wxTheApp
->GetAppName();
170 wxShape
*DiagramView::FindSelectedShape(void)
172 DiagramDocument
*doc
= (DiagramDocument
*)GetDocument();
173 wxObjectList::compatibility_iterator node
= doc
->GetDiagram()->GetShapeList()->GetFirst();
176 wxShape
*eachShape
= (wxShape
*)node
->GetData();
177 if ((eachShape
->GetParent() == NULL
) && eachShape
->Selected())
181 else node
= node
->GetNext();
186 void DiagramView::OnCut(wxCommandEvent
& WXUNUSED(event
))
188 DiagramDocument
*doc
= (DiagramDocument
*)GetDocument();
190 wxShape
*theShape
= FindSelectedShape();
192 doc
->GetCommandProcessor()->Submit(new DiagramCommand(_T("Cut"), wxID_CUT
, doc
, NULL
, 0.0, 0.0, true, theShape
));
195 void DiagramView::OnChangeBackgroundColour(wxCommandEvent
& WXUNUSED(event
))
197 DiagramDocument
*doc
= (DiagramDocument
*)GetDocument();
199 wxShape
*theShape
= FindSelectedShape();
203 data
.SetChooseFull(true);
204 data
.SetColour(theShape
->GetBrush()->GetColour());
206 wxColourDialog
*dialog
= new wxColourDialog(frame
, &data
);
207 wxBrush
*theBrush
= NULL
;
208 if (dialog
->ShowModal() == wxID_OK
)
210 wxColourData retData
= dialog
->GetColourData();
211 wxColour col
= retData
.GetColour();
212 theBrush
= wxTheBrushList
->FindOrCreateBrush(col
, wxSOLID
);
217 doc
->GetCommandProcessor()->Submit(new DiagramCommand(_T("Change colour"), OGLEDIT_CHANGE_BACKGROUND_COLOUR
, doc
,
218 theBrush
, theShape
));
222 void DiagramView::OnEditLabel(wxCommandEvent
& WXUNUSED(event
))
224 wxShape
*theShape
= FindSelectedShape();
227 wxString newLabel
= wxGetTextFromUser(_T("Enter new label"), _T("Shape Label"), ((MyEvtHandler
*)theShape
->GetEventHandler())->label
);
228 GetDocument()->GetCommandProcessor()->Submit(new DiagramCommand(_T("Edit label"), OGLEDIT_EDIT_LABEL
, (DiagramDocument
*) GetDocument(), newLabel
, theShape
));
234 * Window implementations
237 BEGIN_EVENT_TABLE(MyCanvas
, wxShapeCanvas
)
238 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
239 EVT_PAINT(MyCanvas::OnPaint
)
242 // Define a constructor for my canvas
243 MyCanvas::MyCanvas(wxView
*v
, wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
,
244 const wxSize
& size
, long style
):
245 wxShapeCanvas(parent
, id
, pos
, size
, style
)
247 SetBackgroundColour(*wxWHITE
);
251 MyCanvas::~MyCanvas(void)
255 void MyCanvas::OnLeftClick(double x
, double y
, int WXUNUSED(keys
))
257 EditorToolPalette
*palette
= wxGetApp().frame
->palette
;
258 wxClassInfo
*info
= NULL
;
259 switch (palette
->currentlySelected
)
263 info
= CLASSINFO(wxRectangleShape
);
268 info
= CLASSINFO(wxRoundedRectangleShape
);
273 info
= CLASSINFO(wxEllipseShape
);
278 info
= CLASSINFO(wxDiamondShape
);
286 view
->GetDocument()->GetCommandProcessor()->Submit(
287 new DiagramCommand( info
->GetClassName(), OGLEDIT_ADD_SHAPE
, (DiagramDocument
*)view
->GetDocument(), info
,
292 void MyCanvas::OnRightClick(double WXUNUSED(x
), double WXUNUSED(y
), int WXUNUSED(keys
))
296 void MyCanvas::OnDragLeft(bool WXUNUSED(draw
), double WXUNUSED(x
), double WXUNUSED(y
), int WXUNUSED(keys
))
300 void MyCanvas::OnBeginDragLeft(double WXUNUSED(x
), double WXUNUSED(y
), int WXUNUSED(keys
))
304 void MyCanvas::OnEndDragLeft(double WXUNUSED(x
), double WXUNUSED(y
), int WXUNUSED(keys
))
308 void MyCanvas::OnDragRight(bool WXUNUSED(draw
), double WXUNUSED(x
), double WXUNUSED(y
), int WXUNUSED(keys
))
312 void MyCanvas::OnBeginDragRight(double WXUNUSED(x
), double WXUNUSED(y
), int WXUNUSED(keys
))
316 void MyCanvas::OnEndDragRight(double WXUNUSED(x
), double WXUNUSED(y
), int WXUNUSED(keys
))
320 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
322 wxShapeCanvas::OnMouseEvent(event
);
325 void MyCanvas::OnPaint(wxPaintEvent
& event
)
328 wxShapeCanvas::OnPaint(event
);