]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/ogl/ogldiag.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "ogldiag.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include <wx/wxprec.h>
27 #include <wx/wxexpr.h>
40 //using namespace std;
48 #include <wx/ogl/basic.h>
49 #include <wx/ogl/basicp.h>
50 #include <wx/ogl/canvas.h>
51 #include <wx/ogl/ogldiag.h>
52 #include <wx/ogl/lines.h>
53 #include <wx/ogl/composit.h>
54 #include <wx/ogl/misc.h>
56 IMPLEMENT_DYNAMIC_CLASS(wxDiagram
, wxObject
)
59 wxDiagram::wxDiagram()
61 m_diagramCanvas
= NULL
;
62 m_quickEditMode
= FALSE
;
65 m_shapeList
= new wxList
;
66 m_mouseTolerance
= DEFAULT_MOUSE_TOLERANCE
;
69 wxDiagram::~wxDiagram()
75 void wxDiagram::SetSnapToGrid(bool snap
)
80 void wxDiagram::SetGridSpacing(double spacing
)
82 m_gridSpacing
= spacing
;
85 void wxDiagram::Snap(double *x
, double *y
)
89 *x
= m_gridSpacing
* ((int)(*x
/m_gridSpacing
+ 0.5));
90 *y
= m_gridSpacing
* ((int)(*y
/m_gridSpacing
+ 0.5));
95 void wxDiagram::Redraw(wxDC
& dc
)
100 GetCanvas()->SetCursor(* wxHOURGLASS_CURSOR
);
101 wxNode
*current
= m_shapeList
->First();
105 wxShape
*object
= (wxShape
*)current
->Data();
106 if (!object
->GetParent())
109 current
= current
->Next();
112 GetCanvas()->SetCursor(* wxSTANDARD_CURSOR
);
116 void wxDiagram::Clear(wxDC
& dc
)
121 // Insert object after addAfter, or at end of list.
122 void wxDiagram::AddShape(wxShape
*object
, wxShape
*addAfter
)
124 wxNode
*nodeAfter
= NULL
;
126 nodeAfter
= m_shapeList
->Member(addAfter
);
128 if (!m_shapeList
->Member(object
))
132 if (nodeAfter
->Next())
133 m_shapeList
->Insert(nodeAfter
->Next(), object
);
135 m_shapeList
->Append(object
);
138 m_shapeList
->Append(object
);
139 object
->SetCanvas(GetCanvas());
143 void wxDiagram::InsertShape(wxShape
*object
)
145 m_shapeList
->Insert(object
);
146 object
->SetCanvas(GetCanvas());
149 void wxDiagram::RemoveShape(wxShape
*object
)
151 m_shapeList
->DeleteObject(object
);
154 // Should this delete the actual objects too? I think not.
155 void wxDiagram::RemoveAllShapes()
157 m_shapeList
->Clear();
160 void wxDiagram::DeleteAllShapes()
162 wxNode
*node
= m_shapeList
->First();
165 wxShape
*shape
= (wxShape
*)node
->Data();
166 if (!shape
->GetParent())
170 node
= m_shapeList
->First();
177 void wxDiagram::ShowAll(bool show
)
179 wxNode
*current
= m_shapeList
->First();
183 wxShape
*object
= (wxShape
*)current
->Data();
186 current
= current
->Next();
190 void wxDiagram::DrawOutline(wxDC
& dc
, double x1
, double y1
, double x2
, double y2
)
192 wxPen
dottedPen(wxColour(0, 0, 0), 1, wxDOT
);
193 dc
.SetPen(dottedPen
);
194 dc
.SetBrush((* wxTRANSPARENT_BRUSH
));
198 points
[0].x
= (int) x1
;
199 points
[0].y
= (int) y1
;
201 points
[1].x
= (int) x2
;
202 points
[1].y
= (int) y1
;
204 points
[2].x
= (int) x2
;
205 points
[2].y
= (int) y2
;
207 points
[3].x
= (int) x1
;
208 points
[3].y
= (int) y2
;
210 points
[4].x
= (int) x1
;
211 points
[4].y
= (int) y1
;
212 dc
.DrawLines(5, points
);
215 // Make sure all text that should be centred, is centred.
216 void wxDiagram::RecentreAll(wxDC
& dc
)
218 wxNode
*object_node
= m_shapeList
->First();
221 wxShape
*obj
= (wxShape
*)object_node
->Data();
223 object_node
= object_node
->Next();
229 bool wxDiagram::SaveFile(const wxString
& filename
)
233 wxExprDatabase
*database
= new wxExprDatabase
;
235 // First write the diagram type
236 wxExpr
*header
= new wxExpr("diagram");
237 OnHeaderSave(*database
, *header
);
239 database
->Append(header
);
241 wxNode
*node
= m_shapeList
->First();
244 wxShape
*shape
= (wxShape
*)node
->Data();
246 if (!shape
->IsKindOf(CLASSINFO(wxControlPoint
)))
249 if (shape
->IsKindOf(CLASSINFO(wxLineShape
)))
250 expr
= new wxExpr("line");
252 expr
= new wxExpr("shape");
254 OnShapeSave(*database
, *shape
, *expr
);
258 OnDatabaseSave(*database
);
261 wxGetTempFileName("diag", tempFile
);
262 FILE* file
= fopen(tempFile
, "w");
270 database
->Write(file
);
276 if (FileExists(filename))
280 sprintf(buf, "%s.bak", filename);
283 sprintf(buf, "_diagram.bak");
285 if (FileExists(buf)) wxRemoveFile(buf);
286 if (!wxRenameFile(filename, buf))
288 wxCopyFile(filename, buf);
289 wxRemoveFile(filename);
294 // Copy the temporary file to the correct filename
295 if (!wxRenameFile(tempFile
, filename
))
297 wxCopyFile(tempFile
, filename
);
298 wxRemoveFile(tempFile
);
305 bool wxDiagram::LoadFile(const wxString
& filename
)
309 wxExprDatabase
database(wxExprInteger
, "id");
310 if (!database
.Read(filename
))
318 database
.BeginFind();
319 wxExpr
*header
= database
.FindClauseByFunctor("diagram");
322 OnHeaderLoad(database
, *header
);
324 // Scan through all clauses and register the ids
325 wxNode
*node
= database
.First();
328 wxExpr
*clause
= (wxExpr
*)node
->Data();
330 clause
->GetAttributeValue("id", id
);
336 ReadContainerGeometry(database
);
339 OnDatabaseLoad(database
);
346 void wxDiagram::ReadNodes(wxExprDatabase
& database
)
348 // Find and create the node images
349 database
.BeginFind();
350 wxExpr
*clause
= database
.FindClauseByFunctor("shape");
356 clause
->AssignAttributeValue("type", &type
);
357 clause
->AssignAttributeValue("parent", &parentId
);
358 wxClassInfo
*classInfo
= wxClassInfo::FindClass(type
);
361 wxShape
*shape
= (wxShape
*)classInfo
->CreateObject();
362 OnShapeLoad(database
, *shape
, *clause
);
364 shape
->SetCanvas(GetCanvas());
367 m_shapeList
->Append(shape
);
369 // If child of composite, link up
372 wxExpr
*parentExpr
= database
.HashFind("shape", parentId
);
373 if (parentExpr
&& parentExpr
->GetClientData())
375 wxShape
*parent
= (wxShape
*)parentExpr
->GetClientData();
376 shape
->SetParent(parent
);
377 parent
->GetChildren().Append(shape
);
381 clause
->SetClientData(shape
);
386 clause
= database
.FindClauseByFunctor("shape");
391 void wxDiagram::ReadLines(wxExprDatabase
& database
)
393 database
.BeginFind();
394 wxExpr
*clause
= database
.FindClauseByFunctor("line");
400 clause
->GetAttributeValue("type", type
);
401 clause
->GetAttributeValue("parent", parentId
);
402 wxClassInfo
*classInfo
= wxClassInfo::FindClass((char*) (const char*) type
);
405 wxLineShape
*shape
= (wxLineShape
*)classInfo
->CreateObject();
408 OnShapeLoad(database
, *shape
, *clause
);
409 shape
->SetCanvas(GetCanvas());
411 long image_to
= -1; long image_from
= -1;
412 clause
->GetAttributeValue("to", image_to
);
413 clause
->GetAttributeValue("from", image_from
);
415 wxExpr
*image_to_expr
= database
.HashFind("shape", image_to
);
421 wxExpr
*image_from_expr
= database
.HashFind("shape", image_from
);
423 if (!image_from_expr
)
428 if (image_to_expr
&& image_from_expr
)
430 wxShape
*image_to_object
= (wxShape
*)image_to_expr
->GetClientData();
431 wxShape
*image_from_object
= (wxShape
*)image_from_expr
->GetClientData();
433 if (image_to_object
&& image_from_object
)
435 image_from_object
->AddLine(shape
, image_to_object
, shape
->GetAttachmentFrom(), shape
->GetAttachmentTo());
438 clause
->SetClientData(shape
);
440 m_shapeList
->Append(shape
);
443 clause
= database
.FindClauseByFunctor("line");
447 // Containers have divisions that reference adjoining divisions,
448 // so we need a separate pass to link everything up.
449 // Also used by Symbol Library.
450 void wxDiagram::ReadContainerGeometry(wxExprDatabase
& database
)
452 database
.BeginFind();
453 wxExpr
*clause
= database
.FindClauseByFunctor("shape");
456 wxShape
*image
= (wxShape
*)clause
->GetClientData();
457 if (image
&& image
->IsKindOf(CLASSINFO(wxCompositeShape
)))
459 wxCompositeShape
*composite
= (wxCompositeShape
*)image
;
460 wxExpr
*divisionExpr
= NULL
;
462 // Find the list of divisions in the composite
463 clause
->GetAttributeValue("divisions", &divisionExpr
);
467 wxExpr
*idExpr
= divisionExpr
->Nth(i
);
470 long divisionId
= idExpr
->IntegerValue();
471 wxExpr
*childExpr
= database
.HashFind("shape", divisionId
);
472 if (childExpr
&& childExpr
->GetClientData())
474 wxDivisionShape
*child
= (wxDivisionShape
*)childExpr
->GetClientData();
475 composite
->GetDivisions().Append(child
);
477 // Find the adjoining shapes
478 long leftSideId
= -1;
480 long rightSideId
= -1;
481 long bottomSideId
= -1;
482 childExpr
->GetAttributeValue("left_side", leftSideId
);
483 childExpr
->GetAttributeValue("top_side", topSideId
);
484 childExpr
->GetAttributeValue("right_side", rightSideId
);
485 childExpr
->GetAttributeValue("bottom_side", bottomSideId
);
488 wxExpr
*leftExpr
= database
.HashFind("shape", leftSideId
);
489 if (leftExpr
&& leftExpr
->GetClientData())
491 wxDivisionShape
*leftSide
= (wxDivisionShape
*)leftExpr
->GetClientData();
492 child
->SetLeftSide(leftSide
);
497 wxExpr
*topExpr
= database
.HashFind("shape", topSideId
);
498 if (topExpr
&& topExpr
->GetClientData())
500 wxDivisionShape
*topSide
= (wxDivisionShape
*)topExpr
->GetClientData();
501 child
->SetTopSide(topSide
);
504 if (rightSideId
> -1)
506 wxExpr
*rightExpr
= database
.HashFind("shape", rightSideId
);
507 if (rightExpr
&& rightExpr
->GetClientData())
509 wxDivisionShape
*rightSide
= (wxDivisionShape
*)rightExpr
->GetClientData();
510 child
->SetRightSide(rightSide
);
513 if (bottomSideId
> -1)
515 wxExpr
*bottomExpr
= database
.HashFind("shape", bottomSideId
);
516 if (bottomExpr
&& bottomExpr
->GetClientData())
518 wxDivisionShape
*bottomSide
= (wxDivisionShape
*)bottomExpr
->GetClientData();
519 child
->SetBottomSide(bottomSide
);
524 idExpr
= divisionExpr
->Nth(i
);
529 clause
= database
.FindClauseByFunctor("shape");
533 // Allow for modifying file
534 bool wxDiagram::OnDatabaseLoad(wxExprDatabase
& db
)
539 bool wxDiagram::OnDatabaseSave(wxExprDatabase
& db
)
544 bool wxDiagram::OnShapeSave(wxExprDatabase
& db
, wxShape
& shape
, wxExpr
& expr
)
546 shape
.WriteAttributes(&expr
);
549 if (shape
.IsKindOf(CLASSINFO(wxCompositeShape
)))
551 wxNode
*node
= shape
.GetChildren().First();
554 wxShape
*childShape
= (wxShape
*)node
->Data();
555 wxExpr
*childExpr
= new wxExpr("shape");
556 OnShapeSave(db
, *childShape
, *childExpr
);
564 bool wxDiagram::OnShapeLoad(wxExprDatabase
& db
, wxShape
& shape
, wxExpr
& expr
)
566 shape
.ReadAttributes(&expr
);
570 bool wxDiagram::OnHeaderSave(wxExprDatabase
& db
, wxExpr
& expr
)
575 bool wxDiagram::OnHeaderLoad(wxExprDatabase
& db
, wxExpr
& expr
)
582 void wxDiagram::SetCanvas(wxShapeCanvas
*can
)
584 m_diagramCanvas
= can
;
587 // Find a shape by its id
588 wxShape
* wxDiagram::FindShape(long id
) const
590 wxNode
* node
= GetShapeList()->First();
593 wxShape
* shape
= (wxShape
*) node
->Data();
594 if (shape
->GetId() == id
)
602 //// Crossings classes
604 wxLineCrossings::wxLineCrossings()
608 wxLineCrossings::~wxLineCrossings()
613 void wxLineCrossings::FindCrossings(wxDiagram
& diagram
)
616 wxNode
* node1
= diagram
.GetShapeList()->First();
619 wxShape
* shape1
= (wxShape
*) node1
->Data();
620 if (shape1
->IsKindOf(CLASSINFO(wxLineShape
)))
622 wxLineShape
* lineShape1
= (wxLineShape
*) shape1
;
623 // Iterate through the segments
624 wxList
* pts1
= lineShape1
->GetLineControlPoints();
626 for (i
= 0; i
< (pts1
->Number() - 1); i
++)
628 wxRealPoint
* pt1_a
= (wxRealPoint
*) (pts1
->Nth(i
)->Data());
629 wxRealPoint
* pt1_b
= (wxRealPoint
*) (pts1
->Nth(i
+1)->Data());
631 // Now we iterate through the segments again
633 wxNode
* node2
= diagram
.GetShapeList()->First();
636 wxShape
* shape2
= (wxShape
*) node2
->Data();
638 // Assume that the same line doesn't cross itself
639 if (shape2
->IsKindOf(CLASSINFO(wxLineShape
)) && (shape1
!= shape2
))
641 wxLineShape
* lineShape2
= (wxLineShape
*) shape2
;
642 // Iterate through the segments
643 wxList
* pts2
= lineShape2
->GetLineControlPoints();
645 for (j
= 0; j
< (pts2
->Number() - 1); j
++)
647 wxRealPoint
* pt2_a
= (wxRealPoint
*) (pts2
->Nth(j
)->Data());
648 wxRealPoint
* pt2_b
= (wxRealPoint
*) (pts2
->Nth(j
+1)->Data());
650 // Now let's see if these two segments cross.
651 double ratio1
, ratio2
;
652 oglCheckLineIntersection(pt1_a
->x
, pt1_a
->y
, pt1_b
->x
, pt1_b
->y
,
653 pt2_a
->x
, pt2_a
->y
, pt2_b
->x
, pt2_b
->y
,
656 if ((ratio1
< 1.0) && (ratio1
> -1.0))
659 wxLineCrossing
* crossing
= new wxLineCrossing
;
660 crossing
->m_intersect
.x
= (pt1_a
->x
+ (pt1_b
->x
- pt1_a
->x
)*ratio1
);
661 crossing
->m_intersect
.y
= (pt1_a
->y
+ (pt1_b
->y
- pt1_a
->y
)*ratio1
);
663 crossing
->m_pt1
= * pt1_a
;
664 crossing
->m_pt2
= * pt1_b
;
665 crossing
->m_pt3
= * pt2_a
;
666 crossing
->m_pt4
= * pt2_b
;
668 crossing
->m_lineShape1
= lineShape1
;
669 crossing
->m_lineShape2
= lineShape2
;
671 m_crossings
.Append(crossing
);
675 node2
= node2
->Next();
680 node1
= node1
->Next();
684 void wxLineCrossings::DrawCrossings(wxDiagram
& diagram
, wxDC
& dc
)
686 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
690 wxNode
* node
= m_crossings
.First();
693 wxLineCrossing
* crossing
= (wxLineCrossing
*) node
->Data();
694 // dc.DrawEllipse((long) (crossing->m_intersect.x - (arcWidth/2.0) + 0.5), (long) (crossing->m_intersect.y - (arcWidth/2.0) + 0.5),
695 // arcWidth, arcWidth);
698 // Let's do some geometry to find the points on either end of the arc.
710 | - x <-- centre of arc
717 |_______________\ (x2, y2)
722 double a1
= wxMax(crossing
->m_pt1
.y
, crossing
->m_pt2
.y
) - wxMin(crossing
->m_pt1
.y
, crossing
->m_pt2
.y
) ;
723 double b1
= wxMax(crossing
->m_pt1
.x
, crossing
->m_pt2
.x
) - wxMin(crossing
->m_pt1
.x
, crossing
->m_pt2
.x
) ;
724 double c1
= sqrt( (a1
*a1
) + (b1
*b1
) );
726 double c
= arcWidth
/ 2.0;
727 double a
= c
* a1
/c1
;
728 double b
= c
* b1
/c1
;
730 // I'm not sure this is right, since we don't know which direction we should be going in - need
731 // to know which way the line slopes and choose the sign appropriately.
732 double arcX1
= crossing
->m_intersect
.x
- b
;
733 double arcY1
= crossing
->m_intersect
.y
- a
;
735 double arcX2
= crossing
->m_intersect
.x
+ b
;
736 double arcY2
= crossing
->m_intersect
.y
+ a
;
738 dc
.SetPen(*wxBLACK_PEN
);
739 dc
.DrawArc( (long) arcX1
, (long) arcY1
, (long) arcX2
, (long) arcY2
,
740 (long) crossing
->m_intersect
.x
, (long) crossing
->m_intersect
.y
);
742 dc
.SetPen(*wxWHITE_PEN
);
743 dc
.DrawLine( (long) arcX1
, (long) arcY1
, (long) arcX2
, (long) arcY2
);
749 void wxLineCrossings::ClearCrossings()
751 wxNode
* node
= m_crossings
.First();
754 wxLineCrossing
* crossing
= (wxLineCrossing
*) node
->Data();