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 #include <wx/ioswrap.h>
29 #if !wxUSE_DOC_VIEW_ARCHITECTURE
30 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in wx_setup.h!
37 IMPLEMENT_DYNAMIC_CLASS(DiagramDocument
, wxDocument
)
39 DiagramDocument::DiagramDocument(void)
43 DiagramDocument::~DiagramDocument(void)
47 bool DiagramDocument::OnCloseDocument(void)
49 diagram
.DeleteAllShapes();
53 #if wxUSE_STD_IOSTREAM
54 wxSTD ostream
& DiagramDocument::SaveObject(wxSTD ostream
& stream
)
56 wxDocument::SaveObject(stream
);
59 (void) wxGetTempFileName("diag", buf
);
61 diagram
.SaveFile(buf
);
62 wxTransferFileToStream(buf
, stream
);
69 wxSTD istream
& DiagramDocument::LoadObject(wxSTD istream
& stream
)
71 wxDocument::LoadObject(stream
);
74 (void) wxGetTempFileName("diag", buf
);
76 wxTransferStreamToFile(stream
, buf
);
78 diagram
.DeleteAllShapes();
79 diagram
.LoadFile(buf
);
86 wxOutputStream
& DiagramDocument::SaveObject(wxOutputStream
& stream
)
90 wxDocument::SaveObject(stream
);
92 (void) wxGetTempFileName(_T("diag"), buf
);
94 diagram
.SaveFile(buf
);
96 wxTransferFileToStream(buf
, stream
);
105 wxInputStream
& DiagramDocument::LoadObject(wxInputStream
& stream
)
108 wxDocument::LoadObject(stream
);
111 (void) wxGetTempFileName(_T("diag"), buf
);
113 wxTransferStreamToFile(stream
, buf
);
115 diagram
.DeleteAllShapes();
116 diagram
.LoadFile(buf
);
126 * Implementation of drawing command
129 DiagramCommand::DiagramCommand(const wxString
& name
, int command
, DiagramDocument
*ddoc
, wxClassInfo
*info
, double xx
, double yy
,
130 bool sel
, wxShape
*theShape
, wxShape
*fs
, wxShape
*ts
):
131 wxCommand(true, name
)
147 DiagramCommand::DiagramCommand(const wxString
& name
, int command
, DiagramDocument
*ddoc
, wxBrush
*backgroundColour
, wxShape
*theShape
):
148 wxCommand(true, name
)
160 shapeBrush
= backgroundColour
;
164 DiagramCommand::DiagramCommand(const wxString
& name
, int command
, DiagramDocument
*ddoc
, const wxString
& lab
, wxShape
*theShape
):
165 wxCommand(true, name
)
182 DiagramCommand::~DiagramCommand(void)
184 if (shape
&& deleteShape
)
186 shape
->SetCanvas(NULL
);
191 bool DiagramCommand::Do(void)
201 shape
->Select(false);
203 // Generate commands to explicitly remove each connected line.
206 doc
->GetDiagram()->RemoveShape(shape
);
207 if (shape
->IsKindOf(CLASSINFO(wxLineShape
)))
209 wxLineShape
*lineShape
= (wxLineShape
*)shape
;
210 fromShape
= lineShape
->GetFrom();
211 toShape
= lineShape
->GetTo();
216 doc
->UpdateAllViews();
221 case OGLEDIT_ADD_SHAPE
:
225 theShape
= shape
; // Saved from undoing the shape
228 theShape
= (wxShape
*)shapeInfo
->CreateObject();
229 theShape
->AssignNewIds();
230 theShape
->SetEventHandler(new MyEvtHandler(theShape
, theShape
, wxEmptyString
));
231 theShape
->SetCentreResize(false);
232 theShape
->SetPen(wxBLACK_PEN
);
233 theShape
->SetBrush(wxCYAN_BRUSH
);
235 theShape
->SetSize(60, 60);
237 doc
->GetDiagram()->AddShape(theShape
);
238 theShape
->Show(true);
240 wxClientDC
dc(theShape
->GetCanvas());
241 theShape
->GetCanvas()->PrepareDC(dc
);
243 theShape
->Move(dc
, x
, y
);
249 doc
->UpdateAllViews();
252 case OGLEDIT_ADD_LINE
:
256 theShape
= shape
; // Saved from undoing the line
259 theShape
= (wxShape
*)shapeInfo
->CreateObject();
260 theShape
->AssignNewIds();
261 theShape
->SetEventHandler(new MyEvtHandler(theShape
, theShape
, wxEmptyString
));
262 theShape
->SetPen(wxBLACK_PEN
);
263 theShape
->SetBrush(wxRED_BRUSH
);
265 wxLineShape
*lineShape
= (wxLineShape
*)theShape
;
267 // Yes, you can have more than 2 control points, in which case
268 // it becomes a multi-segment line.
269 lineShape
->MakeLineControlPoints(2);
270 lineShape
->AddArrow(ARROW_ARROW
, ARROW_POSITION_END
, 10.0, 0.0, _T("Normal arrowhead"));
273 doc
->GetDiagram()->AddShape(theShape
);
275 fromShape
->AddLine((wxLineShape
*)theShape
, toShape
);
277 theShape
->Show(true);
279 wxClientDC
dc(theShape
->GetCanvas());
280 theShape
->GetCanvas()->PrepareDC(dc
);
282 // It won't get drawn properly unless you move both
284 fromShape
->Move(dc
, fromShape
->GetX(), fromShape
->GetY());
285 toShape
->Move(dc
, toShape
->GetX(), toShape
->GetY());
291 doc
->UpdateAllViews();
294 case OGLEDIT_CHANGE_BACKGROUND_COLOUR
:
298 wxClientDC
dc(shape
->GetCanvas());
299 shape
->GetCanvas()->PrepareDC(dc
);
301 wxBrush
*oldBrush
= shape
->GetBrush();
302 shape
->SetBrush(shapeBrush
);
303 shapeBrush
= oldBrush
;
307 doc
->UpdateAllViews();
312 case OGLEDIT_EDIT_LABEL
:
316 MyEvtHandler
*myHandler
= (MyEvtHandler
*)shape
->GetEventHandler();
317 wxString
oldLabel(myHandler
->label
);
318 myHandler
->label
= shapeLabel
;
319 shapeLabel
= oldLabel
;
321 wxClientDC
dc(shape
->GetCanvas());
322 shape
->GetCanvas()->PrepareDC(dc
);
324 shape
->FormatText(dc
, /* (char*) (const char*) */ myHandler
->label
);
328 doc
->UpdateAllViews();
337 bool DiagramCommand::Undo(void)
345 doc
->GetDiagram()->AddShape(shape
);
348 if (shape
->IsKindOf(CLASSINFO(wxLineShape
)))
350 wxLineShape
*lineShape
= (wxLineShape
*)shape
;
352 fromShape
->AddLine(lineShape
, toShape
);
360 doc
->UpdateAllViews();
363 case OGLEDIT_ADD_SHAPE
:
364 case OGLEDIT_ADD_LINE
:
368 wxClientDC
dc(shape
->GetCanvas());
369 shape
->GetCanvas()->PrepareDC(dc
);
371 shape
->Select(false, &dc
);
372 doc
->GetDiagram()->RemoveShape(shape
);
377 doc
->UpdateAllViews();
380 case OGLEDIT_CHANGE_BACKGROUND_COLOUR
:
384 wxClientDC
dc(shape
->GetCanvas());
385 shape
->GetCanvas()->PrepareDC(dc
);
387 wxBrush
*oldBrush
= shape
->GetBrush();
388 shape
->SetBrush(shapeBrush
);
389 shapeBrush
= oldBrush
;
393 doc
->UpdateAllViews();
397 case OGLEDIT_EDIT_LABEL
:
401 MyEvtHandler
*myHandler
= (MyEvtHandler
*)shape
->GetEventHandler();
402 wxString
oldLabel(myHandler
->label
);
403 myHandler
->label
= shapeLabel
;
404 shapeLabel
= oldLabel
;
406 wxClientDC
dc(shape
->GetCanvas());
407 shape
->GetCanvas()->PrepareDC(dc
);
409 shape
->FormatText(dc
, /* (char*) (const char*) */ myHandler
->label
);
413 doc
->UpdateAllViews();
422 // Remove each individual line connected to a shape by sending a command.
423 void DiagramCommand::RemoveLines(wxShape
*shape
)
425 wxObjectList::compatibility_iterator node
= shape
->GetLines().GetFirst();
428 wxLineShape
*line
= (wxLineShape
*)node
->GetData();
429 doc
->GetCommandProcessor()->Submit(new DiagramCommand(_T("Cut"), wxID_CUT
, doc
, NULL
, 0.0, 0.0, line
->Selected(), line
));
431 node
= shape
->GetLines().GetFirst();
436 * MyEvtHandler: an event handler class for all shapes
439 void MyEvtHandler::OnLeftClick(double WXUNUSED(x
), double WXUNUSED(y
), int keys
, int WXUNUSED(attachment
))
441 wxClientDC
dc(GetShape()->GetCanvas());
442 GetShape()->GetCanvas()->PrepareDC(dc
);
446 // Selection is a concept the library knows about
447 if (GetShape()->Selected())
449 GetShape()->Select(false, &dc
);
450 GetShape()->GetCanvas()->Redraw(dc
); // Redraw because bits of objects will be are missing
454 // Ensure no other shape is selected, to simplify Undo/Redo code
456 wxObjectList::compatibility_iterator node
= GetShape()->GetCanvas()->GetDiagram()->GetShapeList()->GetFirst();
459 wxShape
*eachShape
= (wxShape
*)node
->GetData();
460 if (eachShape
->GetParent() == NULL
)
462 if (eachShape
->Selected())
464 eachShape
->Select(false, &dc
);
468 node
= node
->GetNext();
470 GetShape()->Select(true, &dc
);
472 GetShape()->GetCanvas()->Redraw(dc
);
475 else if (keys
& KEY_CTRL
)
477 // Do something for CONTROL
482 wxGetApp().frame
->SetStatusText(label
);
483 #endif // wxUSE_STATUSBAR
488 * Implement connection of two shapes by right-dragging between them.
491 void MyEvtHandler::OnBeginDragRight(double x
, double y
, int WXUNUSED(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(*wxBLACK
, 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 WXUNUSED(draw
), double x
, double y
, int WXUNUSED(keys
), int attachment
)
511 // Force attachment to be zero for now
514 wxClientDC
dc(GetShape()->GetCanvas());
515 GetShape()->GetCanvas()->PrepareDC(dc
);
517 wxPen
dottedPen(*wxBLACK
, 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 WXUNUSED(keys
), int WXUNUSED(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(_T("wxLineShape"), OGLEDIT_ADD_LINE
, (DiagramDocument
*)canvas
->view
->GetDocument(), CLASSINFO(wxLineShape
),
538 0.0, 0.0, false, NULL
, GetShape(), otherShape
));
542 void MyEvtHandler::OnEndSize(double WXUNUSED(x
), double WXUNUSED(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(_T("label"), handler
->label
);
564 bool MyDiagram::OnShapeLoad(wxExprDatabase
& db
, wxShape
& shape
, wxExpr
& expr
)
566 wxDiagram::OnShapeLoad(db
, shape
, expr
);
567 wxChar
*label
= NULL
;
568 expr
.AssignAttributeValue(_T("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
);