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("diag", buf
);
96 diagram
.SaveFile(buf
);
98 wxTransferFileToStream(buf
, stream
);
107 wxInputStream
& DiagramDocument::LoadObject(wxInputStream
& stream
)
110 wxDocument::LoadObject(stream
);
113 (void) wxGetTempFileName("diag", buf
);
115 wxTransferStreamToFile(stream
, buf
);
117 diagram
.DeleteAllShapes();
118 diagram
.LoadFile(buf
);
128 * Implementation of drawing command
131 DiagramCommand::DiagramCommand(char *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(char *name
, int command
, DiagramDocument
*ddoc
, wxBrush
*backgroundColour
, wxShape
*theShape
):
150 wxCommand(TRUE
, name
)
162 shapeBrush
= backgroundColour
;
166 DiagramCommand::DiagramCommand(char *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
:
225 wxShape
*theShape
= NULL
;
227 theShape
= shape
; // Saved from undoing the shape
230 theShape
= (wxShape
*)shapeInfo
->CreateObject();
231 theShape
->AssignNewIds();
232 theShape
->SetEventHandler(new MyEvtHandler(theShape
, theShape
, wxString("")));
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
:
256 wxShape
*theShape
= NULL
;
258 theShape
= shape
; // Saved from undoing the line
261 theShape
= (wxShape
*)shapeInfo
->CreateObject();
262 theShape
->AssignNewIds();
263 theShape
->SetEventHandler(new MyEvtHandler(theShape
, theShape
, wxString("")));
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, "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().First();
430 wxLineShape
*line
= (wxLineShape
*)node
->Data();
431 doc
->GetCommandProcessor()->Submit(new DiagramCommand("Cut", OGLEDIT_CUT
, doc
, NULL
, 0.0, 0.0, line
->Selected(), line
));
433 node
= shape
->GetLines().First();
438 * MyEvtHandler: an event handler class for all shapes
441 void MyEvtHandler::OnLeftClick(double x
, double y
, int keys
, int 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()->First();
461 wxShape
*eachShape
= (wxShape
*)node
->Data();
462 if (eachShape
->GetParent() == NULL
)
464 if (eachShape
->Selected())
466 eachShape
->Select(FALSE
, &dc
);
472 GetShape()->Select(TRUE
, &dc
);
474 GetShape()->GetCanvas()->Redraw(dc
);
477 else if (keys
& KEY_CTRL
)
479 // Do something for CONTROL
483 wxGetApp().frame
->SetStatusText(label
);
488 * Implement connection of two shapes by right-dragging between them.
491 void MyEvtHandler::OnBeginDragRight(double x
, double y
, int keys
, int attachment
)
493 // Force attachment to be zero for now. Eventually we can deal with
494 // the actual attachment point, e.g. a rectangle side if attachment mode is on.
497 wxClientDC
dc(GetShape()->GetCanvas());
498 GetShape()->GetCanvas()->PrepareDC(dc
);
500 wxPen
dottedPen(wxColour(0, 0, 0), 1, wxDOT
);
501 dc
.SetLogicalFunction(OGLRBLF
);
502 dc
.SetPen(dottedPen
);
504 GetShape()->GetAttachmentPosition(attachment
, &xp
, &yp
);
505 dc
.DrawLine((long) xp
, (long) yp
, (long) x
, (long) y
);
506 GetShape()->GetCanvas()->CaptureMouse();
509 void MyEvtHandler::OnDragRight(bool draw
, double x
, double y
, int keys
, int attachment
)
511 // Force attachment to be zero for now
514 wxClientDC
dc(GetShape()->GetCanvas());
515 GetShape()->GetCanvas()->PrepareDC(dc
);
517 wxPen
dottedPen(wxColour(0, 0, 0), 1, wxDOT
);
518 dc
.SetLogicalFunction(OGLRBLF
);
519 dc
.SetPen(dottedPen
);
521 GetShape()->GetAttachmentPosition(attachment
, &xp
, &yp
);
522 dc
.DrawLine((long) xp
, (long) yp
, (long) x
, (long) y
);
525 void MyEvtHandler::OnEndDragRight(double x
, double y
, int keys
, int attachment
)
527 GetShape()->GetCanvas()->ReleaseMouse();
528 MyCanvas
*canvas
= (MyCanvas
*)GetShape()->GetCanvas();
530 // Check if we're on an object
532 wxShape
*otherShape
= canvas
->FindFirstSensitiveShape(x
, y
, &new_attachment
, OP_DRAG_RIGHT
);
534 if (otherShape
&& !otherShape
->IsKindOf(CLASSINFO(wxLineShape
)))
536 canvas
->view
->GetDocument()->GetCommandProcessor()->Submit(
537 new DiagramCommand("wxLineShape", OGLEDIT_ADD_LINE
, (DiagramDocument
*)canvas
->view
->GetDocument(), CLASSINFO(wxLineShape
),
538 0.0, 0.0, FALSE
, NULL
, GetShape(), otherShape
));
542 void MyEvtHandler::OnEndSize(double x
, double y
)
544 wxClientDC
dc(GetShape()->GetCanvas());
545 GetShape()->GetCanvas()->PrepareDC(dc
);
547 GetShape()->FormatText(dc
, (char*) (const char*) label
);
556 bool MyDiagram::OnShapeSave(wxExprDatabase
& db
, wxShape
& shape
, wxExpr
& expr
)
558 wxDiagram::OnShapeSave(db
, shape
, expr
);
559 MyEvtHandler
*handler
= (MyEvtHandler
*)shape
.GetEventHandler();
560 expr
.AddAttributeValueString("label", handler
->label
);
564 bool MyDiagram::OnShapeLoad(wxExprDatabase
& db
, wxShape
& shape
, wxExpr
& expr
)
566 wxDiagram::OnShapeLoad(db
, shape
, expr
);
568 expr
.AssignAttributeValue("label", &label
);
569 MyEvtHandler
*handler
= new MyEvtHandler(&shape
, &shape
, wxString(label
));
570 shape
.SetEventHandler(handler
);
583 IMPLEMENT_DYNAMIC_CLASS(wxRoundedRectangleShape
, wxRectangleShape
)
585 wxRoundedRectangleShape::wxRoundedRectangleShape(double w
, double h
):
586 wxRectangleShape(w
, h
)
588 // 0.3 of the smaller rectangle dimension
589 SetCornerRadius((double) -0.3);
592 IMPLEMENT_DYNAMIC_CLASS(wxDiamondShape
, wxPolygonShape
)
594 wxDiamondShape::wxDiamondShape(double w
, double h
):
597 // wxPolygonShape::SetSize relies on the shape having non-zero
604 wxList
*thePoints
= new wxList
;
605 wxRealPoint
*point
= new wxRealPoint(0.0, (-h
/2.0));
606 thePoints
->Append((wxObject
*) point
);
608 point
= new wxRealPoint((w
/2.0), 0.0);
609 thePoints
->Append((wxObject
*) point
);
611 point
= new wxRealPoint(0.0, (h
/2.0));
612 thePoints
->Append((wxObject
*) point
);
614 point
= new wxRealPoint((-w
/2.0), 0.0);
615 thePoints
->Append((wxObject
*) point
);