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 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
108 if (CENTER
== TRUE
) // center the drawing
110 posX
= (float) ((w
- (200 * actualScale
)) / 2.0);
111 posY
= (float) ((h
- (200 * actualScale
)) / 2.0);
113 else // Use defined presets
120 // Set the scale and origin
121 dc
->SetUserScale (actualScale
, actualScale
);
122 dc
->SetDeviceOrigin ((long) posX
, (long) posY
);
124 // This part was added to preform the print preview and printing functions
126 dc
->BeginDrawing(); // Allows optimization of drawing code under MS Windows.
127 wxDiagram
*diagram_p
=((DiagramDocument
*)GetDocument())->GetDiagram(); // Get the current diagram
128 if (diagram_p
->GetShapeList())
130 wxCursor
*old_cursor
= NULL
;
131 wxNode
*current
= diagram_p
->GetShapeList()->First();
133 while (current
) // Loop through the entire list of shapes
135 wxShape
*object
= (wxShape
*)current
->Data();
136 if (!object
->GetParent())
138 object
->Draw(* dc
); // Draw the shape onto our printing dc
140 current
= current
->Next(); // Procede to the next shape in the list
143 dc
->EndDrawing(); // Allows optimization of drawing code under MS Windows.
146 void DiagramView::OnUpdate(wxView
*sender
, wxObject
*hint
)
152 // Clean up windows used for displaying the view.
153 bool DiagramView::OnClose(bool deleteWindow
)
155 if (!GetDocument()->Close())
158 DiagramDocument
*diagramDoc
= (DiagramDocument
*)GetDocument();
159 diagramDoc
->GetDiagram()->SetCanvas(NULL
);
162 canvas
->SetDiagram(NULL
);
166 wxString s
= wxTheApp
->GetAppName();
177 wxShape
*DiagramView::FindSelectedShape(void)
179 DiagramDocument
*doc
= (DiagramDocument
*)GetDocument();
180 wxShape
*theShape
= NULL
;
181 wxNode
*node
= doc
->GetDiagram()->GetShapeList()->First();
184 wxShape
*eachShape
= (wxShape
*)node
->Data();
185 if ((eachShape
->GetParent() == NULL
) && eachShape
->Selected())
187 theShape
= eachShape
;
190 else node
= node
->Next();
195 void DiagramView::OnCut(wxCommandEvent
& event
)
197 DiagramDocument
*doc
= (DiagramDocument
*)GetDocument();
199 wxShape
*theShape
= FindSelectedShape();
201 doc
->GetCommandProcessor()->Submit(new DiagramCommand("Cut", OGLEDIT_CUT
, doc
, NULL
, 0.0, 0.0, TRUE
, theShape
));
204 void DiagramView::OnChangeBackgroundColour(wxCommandEvent
& event
)
206 DiagramDocument
*doc
= (DiagramDocument
*)GetDocument();
208 wxShape
*theShape
= FindSelectedShape();
212 data
.SetChooseFull(TRUE
);
213 data
.SetColour(theShape
->GetBrush()->GetColour());
215 wxColourDialog
*dialog
= new wxColourDialog(frame
, &data
);
216 wxBrush
*theBrush
= NULL
;
217 if (dialog
->ShowModal() == wxID_OK
)
219 wxColourData retData
= dialog
->GetColourData();
220 wxColour col
= retData
.GetColour();
221 theBrush
= wxTheBrushList
->FindOrCreateBrush(col
, wxSOLID
);
226 doc
->GetCommandProcessor()->Submit(new DiagramCommand("Change colour", OGLEDIT_CHANGE_BACKGROUND_COLOUR
, doc
,
227 theBrush
, theShape
));
231 void DiagramView::OnEditLabel(wxCommandEvent
& event
)
233 wxShape
*theShape
= FindSelectedShape();
236 wxString newLabel
= wxGetTextFromUser("Enter new label", "Shape Label", ((MyEvtHandler
*)theShape
->GetEventHandler())->label
);
237 GetDocument()->GetCommandProcessor()->Submit(new DiagramCommand("Edit label", OGLEDIT_EDIT_LABEL
, (DiagramDocument
*) GetDocument(), newLabel
, theShape
));
243 * Window implementations
246 BEGIN_EVENT_TABLE(MyCanvas
, wxShapeCanvas
)
247 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
248 EVT_PAINT(MyCanvas::OnPaint
)
251 // Define a constructor for my canvas
252 MyCanvas::MyCanvas(wxView
*v
, wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
,
253 const wxSize
& size
, long style
):
254 wxShapeCanvas(parent
, id
, pos
, size
, style
)
256 SetBackgroundColour(*wxWHITE
);
260 MyCanvas::~MyCanvas(void)
264 void MyCanvas::OnLeftClick(double x
, double y
, int keys
)
266 EditorToolPalette
*palette
= wxGetApp().frame
->palette
;
267 wxClassInfo
*info
= NULL
;
268 switch (palette
->currentlySelected
)
272 info
= CLASSINFO(wxRectangleShape
);
277 info
= CLASSINFO(wxRoundedRectangleShape
);
282 info
= CLASSINFO(wxEllipseShape
);
287 info
= CLASSINFO(wxDiamondShape
);
295 view
->GetDocument()->GetCommandProcessor()->Submit(new DiagramCommand(info
->GetClassName(), OGLEDIT_ADD_SHAPE
, (DiagramDocument
*)view
->GetDocument(), info
,
300 void MyCanvas::OnRightClick(double x
, double y
, int keys
)
304 void MyCanvas::OnDragLeft(bool draw
, double x
, double y
, int keys
)
308 void MyCanvas::OnBeginDragLeft(double x
, double y
, int keys
)
312 void MyCanvas::OnEndDragLeft(double x
, double y
, int keys
)
316 void MyCanvas::OnDragRight(bool draw
, double x
, double y
, int keys
)
320 void MyCanvas::OnBeginDragRight(double x
, double y
, int keys
)
324 void MyCanvas::OnEndDragRight(double x
, double y
, int keys
)
328 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
330 wxShapeCanvas::OnMouseEvent(event
);
333 void MyCanvas::OnPaint(wxPaintEvent
& event
)
336 wxShapeCanvas::OnPaint(event
);