1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implements view functionality in OGLEdit
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 // #pragma implementation
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #include <wx/colordlg.h>
29 #if !wxUSE_DOC_VIEW_ARCHITECTURE
30 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in wx_setup.h!
38 IMPLEMENT_DYNAMIC_CLASS(DiagramView
, wxView
)
40 BEGIN_EVENT_TABLE(DiagramView
, wxView
)
41 EVT_MENU(OGLEDIT_CUT
, DiagramView::OnCut
)
42 EVT_MENU(OGLEDIT_CHANGE_BACKGROUND_COLOUR
, DiagramView::OnChangeBackgroundColour
)
43 EVT_MENU(OGLEDIT_EDIT_LABEL
, DiagramView::OnEditLabel
)
46 // What to do when a view is created. Creates actual
47 // windows for displaying the view.
48 bool DiagramView::OnCreate(wxDocument
*doc
, long WXUNUSED(flags
))
50 frame
= GetMainFrame();
51 canvas
= GetMainFrame()->canvas
;
57 // Initialize the edit menu Undo and Redo items
58 doc
->GetCommandProcessor()->SetEditMenu(((MyFrame
*)frame
)->editMenu
);
59 doc
->GetCommandProcessor()->Initialize();
61 wxShapeCanvas
*shapeCanvas
= (wxShapeCanvas
*)canvas
;
62 DiagramDocument
*diagramDoc
= (DiagramDocument
*)doc
;
63 shapeCanvas
->SetDiagram(diagramDoc
->GetDiagram());
64 diagramDoc
->GetDiagram()->SetCanvas(shapeCanvas
);
69 #define CENTER FALSE // Place the drawing to the center of the page
72 // Sneakily gets used for default print/preview
73 // as well as drawing on the screen.
74 void DiagramView::OnDraw(wxDC
*dc
)
77 /* You might use THIS code if you were scaling
78 * graphics of known size to fit on the page.
82 // We need to adjust for the graphic size, a formula will be added
85 // A better way of find the maxium values would be to search through
88 // Let's have at least 10 device units margin
92 // Add the margin to the graphic size
93 maxX
+= (2 * marginX
);
94 maxY
+= (2 * marginY
);
96 // Get the size of the DC in pixels
99 // Calculate a suitable scaling factor
100 float scaleX
= (float) (w
/ maxX
);
101 float scaleY
= (float) (h
/ maxY
);
103 // Use x or y scaling factor, whichever fits on the DC
104 float actualScale
= wxMin (scaleX
, scaleY
);
107 // Calculate the position on the DC for centring the graphic
109 // center the drawing
110 posX
= (float) ((w
- (200 * actualScale
)) / 2.0);
111 posY
= (float) ((h
- (200 * actualScale
)) / 2.0);
113 // Use defined presets
119 // Set the scale and origin
120 dc
->SetUserScale (actualScale
, actualScale
);
121 dc
->SetDeviceOrigin ((long) posX
, (long) posY
);
123 // This part was added to preform the print preview and printing functions
125 dc
->BeginDrawing(); // Allows optimization of drawing code under MS Windows.
126 wxDiagram
*diagram_p
=((DiagramDocument
*)GetDocument())->GetDiagram(); // Get the current diagram
127 if (diagram_p
->GetShapeList())
129 /* wxCursor *old_cursor = NULL; */
130 wxNode
*current
= diagram_p
->GetShapeList()->GetFirst();
132 while (current
) // Loop through the entire list of shapes
134 wxShape
*object
= (wxShape
*)current
->GetData();
135 if (!object
->GetParent())
137 object
->Draw(* dc
); // Draw the shape onto our printing dc
139 current
= current
->GetNext(); // Procede to the next shape in the list
142 dc
->EndDrawing(); // Allows optimization of drawing code under MS Windows.
145 void DiagramView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
151 // Clean up windows used for displaying the view.
152 bool DiagramView::OnClose(bool WXUNUSED(deleteWindow
))
154 if (!GetDocument()->Close())
157 DiagramDocument
*diagramDoc
= (DiagramDocument
*)GetDocument();
158 diagramDoc
->GetDiagram()->SetCanvas(NULL
);
160 canvas
->ClearBackground();
161 canvas
->SetDiagram(NULL
);
165 wxString s
= wxTheApp
->GetAppName();
176 wxShape
*DiagramView::FindSelectedShape(void)
178 DiagramDocument
*doc
= (DiagramDocument
*)GetDocument();
179 wxShape
*theShape
= NULL
;
180 wxNode
*node
= doc
->GetDiagram()->GetShapeList()->GetFirst();
183 wxShape
*eachShape
= (wxShape
*)node
->GetData();
184 if ((eachShape
->GetParent() == NULL
) && eachShape
->Selected())
186 theShape
= eachShape
;
189 else node
= node
->GetNext();
194 void DiagramView::OnCut(wxCommandEvent
& WXUNUSED(event
))
196 DiagramDocument
*doc
= (DiagramDocument
*)GetDocument();
198 wxShape
*theShape
= FindSelectedShape();
200 doc
->GetCommandProcessor()->Submit(new DiagramCommand(_T("Cut"), OGLEDIT_CUT
, doc
, NULL
, 0.0, 0.0, TRUE
, theShape
));
203 void DiagramView::OnChangeBackgroundColour(wxCommandEvent
& WXUNUSED(event
))
205 DiagramDocument
*doc
= (DiagramDocument
*)GetDocument();
207 wxShape
*theShape
= FindSelectedShape();
211 data
.SetChooseFull(TRUE
);
212 data
.SetColour(theShape
->GetBrush()->GetColour());
214 wxColourDialog
*dialog
= new wxColourDialog(frame
, &data
);
215 wxBrush
*theBrush
= NULL
;
216 if (dialog
->ShowModal() == wxID_OK
)
218 wxColourData retData
= dialog
->GetColourData();
219 wxColour col
= retData
.GetColour();
220 theBrush
= wxTheBrushList
->FindOrCreateBrush(col
, wxSOLID
);
225 doc
->GetCommandProcessor()->Submit(new DiagramCommand(_T("Change colour"), OGLEDIT_CHANGE_BACKGROUND_COLOUR
, doc
,
226 theBrush
, theShape
));
230 void DiagramView::OnEditLabel(wxCommandEvent
& WXUNUSED(event
))
232 wxShape
*theShape
= FindSelectedShape();
235 wxString newLabel
= wxGetTextFromUser(_T("Enter new label"), _T("Shape Label"), ((MyEvtHandler
*)theShape
->GetEventHandler())->label
);
236 GetDocument()->GetCommandProcessor()->Submit(new DiagramCommand(_T("Edit label"), OGLEDIT_EDIT_LABEL
, (DiagramDocument
*) GetDocument(), newLabel
, theShape
));
242 * Window implementations
245 BEGIN_EVENT_TABLE(MyCanvas
, wxShapeCanvas
)
246 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
247 EVT_PAINT(MyCanvas::OnPaint
)
250 // Define a constructor for my canvas
251 MyCanvas::MyCanvas(wxView
*v
, wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
,
252 const wxSize
& size
, long style
):
253 wxShapeCanvas(parent
, id
, pos
, size
, style
)
255 SetBackgroundColour(*wxWHITE
);
259 MyCanvas::~MyCanvas(void)
263 void MyCanvas::OnLeftClick(double x
, double y
, int WXUNUSED(keys
))
265 EditorToolPalette
*palette
= wxGetApp().frame
->palette
;
266 wxClassInfo
*info
= NULL
;
267 switch (palette
->currentlySelected
)
271 info
= CLASSINFO(wxRectangleShape
);
276 info
= CLASSINFO(wxRoundedRectangleShape
);
281 info
= CLASSINFO(wxEllipseShape
);
286 info
= CLASSINFO(wxDiamondShape
);
294 view
->GetDocument()->GetCommandProcessor()->Submit(
295 new DiagramCommand( info
->GetClassName(), OGLEDIT_ADD_SHAPE
, (DiagramDocument
*)view
->GetDocument(), info
,
300 void MyCanvas::OnRightClick(double WXUNUSED(x
), double WXUNUSED(y
), int WXUNUSED(keys
))
304 void MyCanvas::OnDragLeft(bool WXUNUSED(draw
), double WXUNUSED(x
), double WXUNUSED(y
), int WXUNUSED(keys
))
308 void MyCanvas::OnBeginDragLeft(double WXUNUSED(x
), double WXUNUSED(y
), int WXUNUSED(keys
))
312 void MyCanvas::OnEndDragLeft(double WXUNUSED(x
), double WXUNUSED(y
), int WXUNUSED(keys
))
316 void MyCanvas::OnDragRight(bool WXUNUSED(draw
), double WXUNUSED(x
), double WXUNUSED(y
), int WXUNUSED(keys
))
320 void MyCanvas::OnBeginDragRight(double WXUNUSED(x
), double WXUNUSED(y
), int WXUNUSED(keys
))
324 void MyCanvas::OnEndDragRight(double WXUNUSED(x
), double WXUNUSED(y
), int WXUNUSED(keys
))
328 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
330 wxShapeCanvas::OnMouseEvent(event
);
333 void MyCanvas::OnPaint(wxPaintEvent
& event
)
336 wxShapeCanvas::OnPaint(event
);