1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implements document 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 #if !wxUSE_DOC_VIEW_ARCHITECTURE
28 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in wx_setup.h!
35 #if wxUSE_STD_IOSTREAM
39 IMPLEMENT_DYNAMIC_CLASS(DiagramDocument
, wxDocument
)
41 DiagramDocument::DiagramDocument(void)
45 DiagramDocument::~DiagramDocument(void)
49 bool DiagramDocument::OnCloseDocument(void)
51 diagram
.DeleteAllShapes();
55 #if wxUSE_STD_IOSTREAM
56 wxSTD ostream
& DiagramDocument::SaveObject(wxSTD ostream
& stream
)
58 wxDocument::SaveObject(stream
);
61 (void) wxGetTempFileName("diag", buf
);
63 diagram
.SaveFile(buf
);
64 wxTransferFileToStream(buf
, stream
);
71 wxSTD istream
& DiagramDocument::LoadObject(wxSTD istream
& stream
)
73 wxDocument::LoadObject(stream
);
76 (void) wxGetTempFileName("diag", buf
);
78 wxTransferStreamToFile(stream
, buf
);
80 diagram
.DeleteAllShapes();
81 diagram
.LoadFile(buf
);
88 wxOutputStream
& DiagramDocument::SaveObject(wxOutputStream
& stream
)
92 wxDocument::SaveObject(stream
);
94 (void) wxGetTempFileName(_T("diag"), buf
);
96 diagram
.SaveFile(buf
);
98 wxTransferFileToStream(buf
, stream
);
107 wxInputStream
& DiagramDocument::LoadObject(wxInputStream
& stream
)
110 wxDocument::LoadObject(stream
);
113 (void) wxGetTempFileName(_T("diag"), buf
);
115 wxTransferStreamToFile(stream
, buf
);
117 diagram
.DeleteAllShapes();
118 diagram
.LoadFile(buf
);
128 * Implementation of drawing command
131 DiagramCommand::DiagramCommand(const wxString
& name
, int command
, DiagramDocument
*ddoc
, wxClassInfo
*info
, double xx
, double yy
,
132 bool sel
, wxShape
*theShape
, wxShape
*fs
, wxShape
*ts
):
133 wxCommand(true, name
)
149 DiagramCommand::DiagramCommand(const wxString
& name
, int command
, DiagramDocument
*ddoc
, wxBrush
*backgroundColour
, wxShape
*theShape
):
150 wxCommand(true, name
)
162 shapeBrush
= backgroundColour
;
166 DiagramCommand::DiagramCommand(const wxString
& name
, int command
, DiagramDocument
*ddoc
, const wxString
& lab
, wxShape
*theShape
):
167 wxCommand(true, name
)
184 DiagramCommand::~DiagramCommand(void)
186 if (shape
&& deleteShape
)
188 shape
->SetCanvas(NULL
);
193 bool DiagramCommand::Do(void)
203 shape
->Select(false);
205 // Generate commands to explicitly remove each connected line.
208 doc
->GetDiagram()->RemoveShape(shape
);
209 if (shape
->IsKindOf(CLASSINFO(wxLineShape
)))
211 wxLineShape
*lineShape
= (wxLineShape
*)shape
;
212 fromShape
= lineShape
->GetFrom();
213 toShape
= lineShape
->GetTo();
218 doc
->UpdateAllViews();
223 case OGLEDIT_ADD_SHAPE
:
227 theShape
= shape
; // Saved from undoing the shape
230 theShape
= (wxShape
*)shapeInfo
->CreateObject();
231 theShape
->AssignNewIds();
232 theShape
->SetEventHandler(new MyEvtHandler(theShape
, theShape
, wxEmptyString
));
233 theShape
->SetCentreResize(false);
234 theShape
->SetPen(wxBLACK_PEN
);
235 theShape
->SetBrush(wxCYAN_BRUSH
);
237 theShape
->SetSize(60, 60);
239 doc
->GetDiagram()->AddShape(theShape
);
240 theShape
->Show(true);
242 wxClientDC
dc(theShape
->GetCanvas());
243 theShape
->GetCanvas()->PrepareDC(dc
);
245 theShape
->Move(dc
, x
, y
);
251 doc
->UpdateAllViews();
254 case OGLEDIT_ADD_LINE
:
258 theShape
= shape
; // Saved from undoing the line
261 theShape
= (wxShape
*)shapeInfo
->CreateObject();
262 theShape
->AssignNewIds();
263 theShape
->SetEventHandler(new MyEvtHandler(theShape
, theShape
, wxEmptyString
));
264 theShape
->SetPen(wxBLACK_PEN
);
265 theShape
->SetBrush(wxRED_BRUSH
);
267 wxLineShape
*lineShape
= (wxLineShape
*)theShape
;
269 // Yes, you can have more than 2 control points, in which case
270 // it becomes a multi-segment line.
271 lineShape
->MakeLineControlPoints(2);
272 lineShape
->AddArrow(ARROW_ARROW
, ARROW_POSITION_END
, 10.0, 0.0, _T("Normal arrowhead"));
275 doc
->GetDiagram()->AddShape(theShape
);
277 fromShape
->AddLine((wxLineShape
*)theShape
, toShape
);
279 theShape
->Show(true);
281 wxClientDC
dc(theShape
->GetCanvas());
282 theShape
->GetCanvas()->PrepareDC(dc
);
284 // It won't get drawn properly unless you move both
286 fromShape
->Move(dc
, fromShape
->GetX(), fromShape
->GetY());
287 toShape
->Move(dc
, toShape
->GetX(), toShape
->GetY());
293 doc
->UpdateAllViews();
296 case OGLEDIT_CHANGE_BACKGROUND_COLOUR
:
300 wxClientDC
dc(shape
->GetCanvas());
301 shape
->GetCanvas()->PrepareDC(dc
);
303 wxBrush
*oldBrush
= shape
->GetBrush();
304 shape
->SetBrush(shapeBrush
);
305 shapeBrush
= oldBrush
;
309 doc
->UpdateAllViews();
314 case OGLEDIT_EDIT_LABEL
:
318 MyEvtHandler
*myHandler
= (MyEvtHandler
*)shape
->GetEventHandler();
319 wxString
oldLabel(myHandler
->label
);
320 myHandler
->label
= shapeLabel
;
321 shapeLabel
= oldLabel
;
323 wxClientDC
dc(shape
->GetCanvas());
324 shape
->GetCanvas()->PrepareDC(dc
);
326 shape
->FormatText(dc
, /* (char*) (const char*) */ myHandler
->label
);
330 doc
->UpdateAllViews();
339 bool DiagramCommand::Undo(void)
347 doc
->GetDiagram()->AddShape(shape
);
350 if (shape
->IsKindOf(CLASSINFO(wxLineShape
)))
352 wxLineShape
*lineShape
= (wxLineShape
*)shape
;
354 fromShape
->AddLine(lineShape
, toShape
);
362 doc
->UpdateAllViews();
365 case OGLEDIT_ADD_SHAPE
:
366 case OGLEDIT_ADD_LINE
:
370 wxClientDC
dc(shape
->GetCanvas());
371 shape
->GetCanvas()->PrepareDC(dc
);
373 shape
->Select(false, &dc
);
374 doc
->GetDiagram()->RemoveShape(shape
);
379 doc
->UpdateAllViews();
382 case OGLEDIT_CHANGE_BACKGROUND_COLOUR
:
386 wxClientDC
dc(shape
->GetCanvas());
387 shape
->GetCanvas()->PrepareDC(dc
);
389 wxBrush
*oldBrush
= shape
->GetBrush();
390 shape
->SetBrush(shapeBrush
);
391 shapeBrush
= oldBrush
;
395 doc
->UpdateAllViews();
399 case OGLEDIT_EDIT_LABEL
:
403 MyEvtHandler
*myHandler
= (MyEvtHandler
*)shape
->GetEventHandler();
404 wxString
oldLabel(myHandler
->label
);
405 myHandler
->label
= shapeLabel
;
406 shapeLabel
= oldLabel
;
408 wxClientDC
dc(shape
->GetCanvas());
409 shape
->GetCanvas()->PrepareDC(dc
);
411 shape
->FormatText(dc
, /* (char*) (const char*) */ myHandler
->label
);
415 doc
->UpdateAllViews();
424 // Remove each individual line connected to a shape by sending a command.
425 void DiagramCommand::RemoveLines(wxShape
*shape
)
427 wxNode
*node
= shape
->GetLines().GetFirst();
430 wxLineShape
*line
= (wxLineShape
*)node
->GetData();
431 doc
->GetCommandProcessor()->Submit(new DiagramCommand(_T("Cut"), OGLEDIT_CUT
, doc
, NULL
, 0.0, 0.0, line
->Selected(), line
));
433 node
= shape
->GetLines().GetFirst();
438 * MyEvtHandler: an event handler class for all shapes
441 void MyEvtHandler::OnLeftClick(double WXUNUSED(x
), double WXUNUSED(y
), int keys
, int WXUNUSED(attachment
))
443 wxClientDC
dc(GetShape()->GetCanvas());
444 GetShape()->GetCanvas()->PrepareDC(dc
);
448 // Selection is a concept the library knows about
449 if (GetShape()->Selected())
451 GetShape()->Select(false, &dc
);
452 GetShape()->GetCanvas()->Redraw(dc
); // Redraw because bits of objects will be are missing
456 // Ensure no other shape is selected, to simplify Undo/Redo code
458 wxNode
*node
= GetShape()->GetCanvas()->GetDiagram()->GetShapeList()->GetFirst();
461 wxShape
*eachShape
= (wxShape
*)node
->GetData();
462 if (eachShape
->GetParent() == NULL
)
464 if (eachShape
->Selected())
466 eachShape
->Select(false, &dc
);
470 node
= node
->GetNext();
472 GetShape()->Select(true, &dc
);
474 GetShape()->GetCanvas()->Redraw(dc
);
477 else if (keys
& KEY_CTRL
)
479 // Do something for CONTROL
484 wxGetApp().frame
->SetStatusText(label
);
485 #endif // wxUSE_STATUSBAR
490 * Implement connection of two shapes by right-dragging between them.
493 void MyEvtHandler::OnBeginDragRight(double x
, double y
, int WXUNUSED(keys
), int attachment
)
495 // Force attachment to be zero for now. Eventually we can deal with
496 // the actual attachment point, e.g. a rectangle side if attachment mode is on.
499 wxClientDC
dc(GetShape()->GetCanvas());
500 GetShape()->GetCanvas()->PrepareDC(dc
);
502 wxPen
dottedPen(wxColour(0, 0, 0), 1, wxDOT
);
503 dc
.SetLogicalFunction(OGLRBLF
);
504 dc
.SetPen(dottedPen
);
506 GetShape()->GetAttachmentPosition(attachment
, &xp
, &yp
);
507 dc
.DrawLine((long) xp
, (long) yp
, (long) x
, (long) y
);
508 GetShape()->GetCanvas()->CaptureMouse();
511 void MyEvtHandler::OnDragRight(bool WXUNUSED(draw
), double x
, double y
, int WXUNUSED(keys
), int attachment
)
513 // Force attachment to be zero for now
516 wxClientDC
dc(GetShape()->GetCanvas());
517 GetShape()->GetCanvas()->PrepareDC(dc
);
519 wxPen
dottedPen(wxColour(0, 0, 0), 1, wxDOT
);
520 dc
.SetLogicalFunction(OGLRBLF
);
521 dc
.SetPen(dottedPen
);
523 GetShape()->GetAttachmentPosition(attachment
, &xp
, &yp
);
524 dc
.DrawLine((long) xp
, (long) yp
, (long) x
, (long) y
);
527 void MyEvtHandler::OnEndDragRight(double x
, double y
, int WXUNUSED(keys
), int WXUNUSED(attachment
))
529 GetShape()->GetCanvas()->ReleaseMouse();
530 MyCanvas
*canvas
= (MyCanvas
*)GetShape()->GetCanvas();
532 // Check if we're on an object
534 wxShape
*otherShape
= canvas
->FindFirstSensitiveShape(x
, y
, &new_attachment
, OP_DRAG_RIGHT
);
536 if (otherShape
&& !otherShape
->IsKindOf(CLASSINFO(wxLineShape
)))
538 canvas
->view
->GetDocument()->GetCommandProcessor()->Submit(
539 new DiagramCommand(_T("wxLineShape"), OGLEDIT_ADD_LINE
, (DiagramDocument
*)canvas
->view
->GetDocument(), CLASSINFO(wxLineShape
),
540 0.0, 0.0, false, NULL
, GetShape(), otherShape
));
544 void MyEvtHandler::OnEndSize(double WXUNUSED(x
), double WXUNUSED(y
))
546 wxClientDC
dc(GetShape()->GetCanvas());
547 GetShape()->GetCanvas()->PrepareDC(dc
);
549 GetShape()->FormatText(dc
, /* (char*) (const char*) */ label
);
558 bool MyDiagram::OnShapeSave(wxExprDatabase
& db
, wxShape
& shape
, wxExpr
& expr
)
560 wxDiagram::OnShapeSave(db
, shape
, expr
);
561 MyEvtHandler
*handler
= (MyEvtHandler
*)shape
.GetEventHandler();
562 expr
.AddAttributeValueString(_T("label"), handler
->label
);
566 bool MyDiagram::OnShapeLoad(wxExprDatabase
& db
, wxShape
& shape
, wxExpr
& expr
)
568 wxDiagram::OnShapeLoad(db
, shape
, expr
);
569 wxChar
*label
= NULL
;
570 expr
.AssignAttributeValue(_T("label"), &label
);
571 MyEvtHandler
*handler
= new MyEvtHandler(&shape
, &shape
, wxString(label
));
572 shape
.SetEventHandler(handler
);
585 IMPLEMENT_DYNAMIC_CLASS(wxRoundedRectangleShape
, wxRectangleShape
)
587 wxRoundedRectangleShape::wxRoundedRectangleShape(double w
, double h
):
588 wxRectangleShape(w
, h
)
590 // 0.3 of the smaller rectangle dimension
591 SetCornerRadius((double) -0.3);
594 IMPLEMENT_DYNAMIC_CLASS(wxDiamondShape
, wxPolygonShape
)
596 wxDiamondShape::wxDiamondShape(double w
, double h
):
599 // wxPolygonShape::SetSize relies on the shape having non-zero
606 wxList
*thePoints
= new wxList
;
607 wxRealPoint
*point
= new wxRealPoint(0.0, (-h
/2.0));
608 thePoints
->Append((wxObject
*) point
);
610 point
= new wxRealPoint((w
/2.0), 0.0);
611 thePoints
->Append((wxObject
*) point
);
613 point
= new wxRealPoint(0.0, (h
/2.0));
614 thePoints
->Append((wxObject
*) point
);
616 point
= new wxRealPoint((-w
/2.0), 0.0);
617 thePoints
->Append((wxObject
*) point
);