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 // Sneakily gets used for default print/preview
70 // as well as drawing on the screen.
71 void DiagramView::OnDraw(wxDC
*dc
)
75 void DiagramView::OnUpdate(wxView
*sender
, wxObject
*hint
)
81 // Clean up windows used for displaying the view.
82 bool DiagramView::OnClose(bool deleteWindow
)
84 if (!GetDocument()->Close())
87 DiagramDocument
*diagramDoc
= (DiagramDocument
*)GetDocument();
88 diagramDoc
->GetDiagram()->SetCanvas(NULL
);
91 canvas
->SetDiagram(NULL
);
95 wxString s
= wxTheApp
->GetAppName();
106 wxShape
*DiagramView::FindSelectedShape(void)
108 DiagramDocument
*doc
= (DiagramDocument
*)GetDocument();
109 wxShape
*theShape
= NULL
;
110 wxNode
*node
= doc
->GetDiagram()->GetShapeList()->First();
113 wxShape
*eachShape
= (wxShape
*)node
->Data();
114 if ((eachShape
->GetParent() == NULL
) && eachShape
->Selected())
116 theShape
= eachShape
;
119 else node
= node
->Next();
124 void DiagramView::OnCut(wxCommandEvent
& event
)
126 DiagramDocument
*doc
= (DiagramDocument
*)GetDocument();
128 wxShape
*theShape
= FindSelectedShape();
130 doc
->GetCommandProcessor()->Submit(new DiagramCommand("Cut", OGLEDIT_CUT
, doc
, NULL
, 0.0, 0.0, TRUE
, theShape
));
133 void DiagramView::OnChangeBackgroundColour(wxCommandEvent
& event
)
135 DiagramDocument
*doc
= (DiagramDocument
*)GetDocument();
137 wxShape
*theShape
= FindSelectedShape();
141 data
.SetChooseFull(TRUE
);
142 data
.SetColour(theShape
->GetBrush()->GetColour());
144 wxColourDialog
*dialog
= new wxColourDialog(frame
, &data
);
145 wxBrush
*theBrush
= NULL
;
146 if (dialog
->ShowModal() == wxID_OK
)
148 wxColourData retData
= dialog
->GetColourData();
149 wxColour col
= retData
.GetColour();
150 theBrush
= wxTheBrushList
->FindOrCreateBrush(col
, wxSOLID
);
155 doc
->GetCommandProcessor()->Submit(new DiagramCommand("Change colour", OGLEDIT_CHANGE_BACKGROUND_COLOUR
, doc
,
156 theBrush
, theShape
));
160 void DiagramView::OnEditLabel(wxCommandEvent
& event
)
162 wxShape
*theShape
= FindSelectedShape();
165 wxString newLabel
= wxGetTextFromUser("Enter new label", "Shape Label", ((MyEvtHandler
*)theShape
->GetEventHandler())->label
);
166 GetDocument()->GetCommandProcessor()->Submit(new DiagramCommand("Edit label", OGLEDIT_EDIT_LABEL
, (DiagramDocument
*) GetDocument(), newLabel
, theShape
));
171 * Window implementations
174 BEGIN_EVENT_TABLE(MyCanvas
, wxShapeCanvas
)
175 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
176 EVT_PAINT(MyCanvas::OnPaint
)
179 // Define a constructor for my canvas
180 MyCanvas::MyCanvas(wxView
*v
, wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
,
181 const wxSize
& size
, long style
):
182 wxShapeCanvas(parent
, id
, pos
, size
, style
)
187 MyCanvas::~MyCanvas(void)
191 void MyCanvas::OnLeftClick(double x
, double y
, int keys
)
193 EditorToolPalette
*palette
= wxGetApp().frame
->palette
;
194 wxClassInfo
*info
= NULL
;
195 switch (palette
->currentlySelected
)
199 info
= CLASSINFO(wxRectangleShape
);
204 info
= CLASSINFO(wxRoundedRectangleShape
);
209 info
= CLASSINFO(wxEllipseShape
);
214 info
= CLASSINFO(wxDiamondShape
);
222 view
->GetDocument()->GetCommandProcessor()->Submit(new DiagramCommand(info
->GetClassName(), OGLEDIT_ADD_SHAPE
, (DiagramDocument
*)view
->GetDocument(), info
,
227 void MyCanvas::OnRightClick(double x
, double y
, int keys
)
231 void MyCanvas::OnDragLeft(bool draw
, double x
, double y
, int keys
)
235 void MyCanvas::OnBeginDragLeft(double x
, double y
, int keys
)
239 void MyCanvas::OnEndDragLeft(double x
, double y
, int keys
)
243 void MyCanvas::OnDragRight(bool draw
, double x
, double y
, int keys
)
247 void MyCanvas::OnBeginDragRight(double x
, double y
, int keys
)
251 void MyCanvas::OnEndDragRight(double x
, double y
, int keys
)
255 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
257 wxShapeCanvas::OnMouseEvent(event
);
260 void MyCanvas::OnPaint(wxPaintEvent
& event
)
263 wxShapeCanvas::OnPaint(event
);