]>
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>
36 //using namespace std;
44 #include <wx/ogl/basic.h>
45 #include <wx/ogl/basicp.h>
46 #include <wx/ogl/canvas.h>
47 #include <wx/ogl/ogldiag.h>
48 #include <wx/ogl/lines.h>
49 #include <wx/ogl/composit.h>
50 #include <wx/ogl/misc.h>
52 IMPLEMENT_DYNAMIC_CLASS(wxDiagram
, wxObject
)
55 wxDiagram::wxDiagram()
57 m_diagramCanvas
= NULL
;
58 m_quickEditMode
= FALSE
;
61 m_shapeList
= new wxList
;
62 m_mouseTolerance
= DEFAULT_MOUSE_TOLERANCE
;
65 wxDiagram::~wxDiagram()
71 void wxDiagram::SetSnapToGrid(bool snap
)
76 void wxDiagram::SetGridSpacing(double spacing
)
78 m_gridSpacing
= spacing
;
81 void wxDiagram::Snap(double *x
, double *y
)
85 *x
= m_gridSpacing
* ((int)(*x
/m_gridSpacing
+ 0.5));
86 *y
= m_gridSpacing
* ((int)(*y
/m_gridSpacing
+ 0.5));
91 void wxDiagram::Redraw(wxDC
& dc
)
96 GetCanvas()->SetCursor(* wxHOURGLASS_CURSOR
);
97 wxNode
*current
= m_shapeList
->First();
101 wxShape
*object
= (wxShape
*)current
->Data();
102 if (!object
->GetParent())
105 current
= current
->Next();
108 GetCanvas()->SetCursor(* wxSTANDARD_CURSOR
);
112 void wxDiagram::Clear(wxDC
& dc
)
117 // Insert object after addAfter, or at end of list.
118 void wxDiagram::AddShape(wxShape
*object
, wxShape
*addAfter
)
120 wxNode
*nodeAfter
= NULL
;
122 nodeAfter
= m_shapeList
->Member(addAfter
);
124 if (!m_shapeList
->Member(object
))
128 if (nodeAfter
->Next())
129 m_shapeList
->Insert(nodeAfter
->Next(), object
);
131 m_shapeList
->Append(object
);
134 m_shapeList
->Append(object
);
135 object
->SetCanvas(GetCanvas());
139 void wxDiagram::InsertShape(wxShape
*object
)
141 m_shapeList
->Insert(object
);
142 object
->SetCanvas(GetCanvas());
145 void wxDiagram::RemoveShape(wxShape
*object
)
147 m_shapeList
->DeleteObject(object
);
150 // Should this delete the actual objects too? I think not.
151 void wxDiagram::RemoveAllShapes()
153 m_shapeList
->Clear();
156 void wxDiagram::DeleteAllShapes()
158 wxNode
*node
= m_shapeList
->First();
161 wxShape
*shape
= (wxShape
*)node
->Data();
162 if (!shape
->GetParent())
166 node
= m_shapeList
->First();
173 void wxDiagram::ShowAll(bool show
)
175 wxNode
*current
= m_shapeList
->First();
179 wxShape
*object
= (wxShape
*)current
->Data();
182 current
= current
->Next();
186 void wxDiagram::DrawOutline(wxDC
& dc
, double x1
, double y1
, double x2
, double y2
)
188 wxPen
dottedPen(wxColour(0, 0, 0), 1, wxDOT
);
189 dc
.SetPen(dottedPen
);
190 dc
.SetBrush((* wxTRANSPARENT_BRUSH
));
194 points
[0].x
= (int) x1
;
195 points
[0].y
= (int) y1
;
197 points
[1].x
= (int) x2
;
198 points
[1].y
= (int) y1
;
200 points
[2].x
= (int) x2
;
201 points
[2].y
= (int) y2
;
203 points
[3].x
= (int) x1
;
204 points
[3].y
= (int) y2
;
206 points
[4].x
= (int) x1
;
207 points
[4].y
= (int) y1
;
208 dc
.DrawLines(5, points
);
211 // Make sure all text that should be centred, is centred.
212 void wxDiagram::RecentreAll(wxDC
& dc
)
214 wxNode
*object_node
= m_shapeList
->First();
217 wxShape
*obj
= (wxShape
*)object_node
->Data();
219 object_node
= object_node
->Next();
225 bool wxDiagram::SaveFile(const wxString
& filename
)
229 wxExprDatabase
*database
= new wxExprDatabase
;
231 // First write the diagram type
232 wxExpr
*header
= new wxExpr("diagram");
233 OnHeaderSave(*database
, *header
);
235 database
->Append(header
);
237 wxNode
*node
= m_shapeList
->First();
240 wxShape
*shape
= (wxShape
*)node
->Data();
242 if (!shape
->IsKindOf(CLASSINFO(wxControlPoint
)))
245 if (shape
->IsKindOf(CLASSINFO(wxLineShape
)))
246 expr
= new wxExpr("line");
248 expr
= new wxExpr("shape");
250 OnShapeSave(*database
, *shape
, *expr
);
254 OnDatabaseSave(*database
);
257 wxGetTempFileName("diag", tempFile
);
258 FILE* file
= fopen(tempFile
, "w");
266 database
->Write(file
);
272 if (FileExists(filename))
276 sprintf(buf, "%s.bak", filename);
279 sprintf(buf, "_diagram.bak");
281 if (FileExists(buf)) wxRemoveFile(buf);
282 if (!wxRenameFile(filename, buf))
284 wxCopyFile(filename, buf);
285 wxRemoveFile(filename);
290 // Copy the temporary file to the correct filename
291 if (!wxRenameFile(tempFile
, filename
))
293 wxCopyFile(tempFile
, filename
);
294 wxRemoveFile(tempFile
);
301 bool wxDiagram::LoadFile(const wxString
& filename
)
305 wxExprDatabase
database(wxExprInteger
, "id");
306 if (!database
.Read(filename
))
314 database
.BeginFind();
315 wxExpr
*header
= database
.FindClauseByFunctor("diagram");
318 OnHeaderLoad(database
, *header
);
320 // Scan through all clauses and register the ids
321 wxNode
*node
= database
.First();
324 wxExpr
*clause
= (wxExpr
*)node
->Data();
326 clause
->GetAttributeValue("id", id
);
332 ReadContainerGeometry(database
);
335 OnDatabaseLoad(database
);
342 void wxDiagram::ReadNodes(wxExprDatabase
& database
)
344 // Find and create the node images
345 database
.BeginFind();
346 wxExpr
*clause
= database
.FindClauseByFunctor("shape");
352 clause
->AssignAttributeValue("type", &type
);
353 clause
->AssignAttributeValue("parent", &parentId
);
354 wxClassInfo
*classInfo
= wxClassInfo::FindClass(type
);
357 wxShape
*shape
= (wxShape
*)classInfo
->CreateObject();
358 OnShapeLoad(database
, *shape
, *clause
);
360 shape
->SetCanvas(GetCanvas());
363 m_shapeList
->Append(shape
);
365 // If child of composite, link up
368 wxExpr
*parentExpr
= database
.HashFind("shape", parentId
);
369 if (parentExpr
&& parentExpr
->GetClientData())
371 wxShape
*parent
= (wxShape
*)parentExpr
->GetClientData();
372 shape
->SetParent(parent
);
373 parent
->GetChildren().Append(shape
);
377 clause
->SetClientData(shape
);
382 clause
= database
.FindClauseByFunctor("shape");
387 void wxDiagram::ReadLines(wxExprDatabase
& database
)
389 database
.BeginFind();
390 wxExpr
*clause
= database
.FindClauseByFunctor("line");
396 clause
->GetAttributeValue("type", type
);
397 clause
->GetAttributeValue("parent", parentId
);
398 wxClassInfo
*classInfo
= wxClassInfo::FindClass((char*) (const char*) type
);
401 wxLineShape
*shape
= (wxLineShape
*)classInfo
->CreateObject();
404 OnShapeLoad(database
, *shape
, *clause
);
405 shape
->SetCanvas(GetCanvas());
407 long image_to
= -1; long image_from
= -1;
408 clause
->GetAttributeValue("to", image_to
);
409 clause
->GetAttributeValue("from", image_from
);
411 wxExpr
*image_to_expr
= database
.HashFind("shape", image_to
);
417 wxExpr
*image_from_expr
= database
.HashFind("shape", image_from
);
419 if (!image_from_expr
)
424 if (image_to_expr
&& image_from_expr
)
426 wxShape
*image_to_object
= (wxShape
*)image_to_expr
->GetClientData();
427 wxShape
*image_from_object
= (wxShape
*)image_from_expr
->GetClientData();
429 if (image_to_object
&& image_from_object
)
431 image_from_object
->AddLine(shape
, image_to_object
, shape
->GetAttachmentFrom(), shape
->GetAttachmentTo());
434 clause
->SetClientData(shape
);
436 m_shapeList
->Append(shape
);
439 clause
= database
.FindClauseByFunctor("line");
443 // Containers have divisions that reference adjoining divisions,
444 // so we need a separate pass to link everything up.
445 // Also used by Symbol Library.
446 void wxDiagram::ReadContainerGeometry(wxExprDatabase
& database
)
448 database
.BeginFind();
449 wxExpr
*clause
= database
.FindClauseByFunctor("shape");
452 wxShape
*image
= (wxShape
*)clause
->GetClientData();
453 if (image
&& image
->IsKindOf(CLASSINFO(wxCompositeShape
)))
455 wxCompositeShape
*composite
= (wxCompositeShape
*)image
;
456 wxExpr
*divisionExpr
= NULL
;
458 // Find the list of divisions in the composite
459 clause
->GetAttributeValue("divisions", &divisionExpr
);
463 wxExpr
*idExpr
= divisionExpr
->Nth(i
);
466 long divisionId
= idExpr
->IntegerValue();
467 wxExpr
*childExpr
= database
.HashFind("shape", divisionId
);
468 if (childExpr
&& childExpr
->GetClientData())
470 wxDivisionShape
*child
= (wxDivisionShape
*)childExpr
->GetClientData();
471 composite
->GetDivisions().Append(child
);
473 // Find the adjoining shapes
474 long leftSideId
= -1;
476 long rightSideId
= -1;
477 long bottomSideId
= -1;
478 childExpr
->GetAttributeValue("left_side", leftSideId
);
479 childExpr
->GetAttributeValue("top_side", topSideId
);
480 childExpr
->GetAttributeValue("right_side", rightSideId
);
481 childExpr
->GetAttributeValue("bottom_side", bottomSideId
);
484 wxExpr
*leftExpr
= database
.HashFind("shape", leftSideId
);
485 if (leftExpr
&& leftExpr
->GetClientData())
487 wxDivisionShape
*leftSide
= (wxDivisionShape
*)leftExpr
->GetClientData();
488 child
->SetLeftSide(leftSide
);
493 wxExpr
*topExpr
= database
.HashFind("shape", topSideId
);
494 if (topExpr
&& topExpr
->GetClientData())
496 wxDivisionShape
*topSide
= (wxDivisionShape
*)topExpr
->GetClientData();
497 child
->SetTopSide(topSide
);
500 if (rightSideId
> -1)
502 wxExpr
*rightExpr
= database
.HashFind("shape", rightSideId
);
503 if (rightExpr
&& rightExpr
->GetClientData())
505 wxDivisionShape
*rightSide
= (wxDivisionShape
*)rightExpr
->GetClientData();
506 child
->SetRightSide(rightSide
);
509 if (bottomSideId
> -1)
511 wxExpr
*bottomExpr
= database
.HashFind("shape", bottomSideId
);
512 if (bottomExpr
&& bottomExpr
->GetClientData())
514 wxDivisionShape
*bottomSide
= (wxDivisionShape
*)bottomExpr
->GetClientData();
515 child
->SetBottomSide(bottomSide
);
520 idExpr
= divisionExpr
->Nth(i
);
525 clause
= database
.FindClauseByFunctor("shape");
529 // Allow for modifying file
530 bool wxDiagram::OnDatabaseLoad(wxExprDatabase
& db
)
535 bool wxDiagram::OnDatabaseSave(wxExprDatabase
& db
)
540 bool wxDiagram::OnShapeSave(wxExprDatabase
& db
, wxShape
& shape
, wxExpr
& expr
)
542 shape
.WriteAttributes(&expr
);
545 if (shape
.IsKindOf(CLASSINFO(wxCompositeShape
)))
547 wxNode
*node
= shape
.GetChildren().First();
550 wxShape
*childShape
= (wxShape
*)node
->Data();
551 wxExpr
*childExpr
= new wxExpr("shape");
552 OnShapeSave(db
, *childShape
, *childExpr
);
560 bool wxDiagram::OnShapeLoad(wxExprDatabase
& db
, wxShape
& shape
, wxExpr
& expr
)
562 shape
.ReadAttributes(&expr
);
566 bool wxDiagram::OnHeaderSave(wxExprDatabase
& db
, wxExpr
& expr
)
571 bool wxDiagram::OnHeaderLoad(wxExprDatabase
& db
, wxExpr
& expr
)
578 void wxDiagram::SetCanvas(wxShapeCanvas
*can
)
580 m_diagramCanvas
= can
;
583 // Find a shape by its id
584 wxShape
* wxDiagram::FindShape(long id
) const
586 wxNode
* node
= GetShapeList()->First();
589 wxShape
* shape
= (wxShape
*) node
->Data();
590 if (shape
->GetId() == id
)
598 //// Crossings classes
600 wxLineCrossings::wxLineCrossings()
604 wxLineCrossings::~wxLineCrossings()
609 void wxLineCrossings::FindCrossings(wxDiagram
& diagram
)
612 wxNode
* node1
= diagram
.GetShapeList()->First();
615 wxShape
* shape1
= (wxShape
*) node1
->Data();
616 if (shape1
->IsKindOf(CLASSINFO(wxLineShape
)))
618 wxLineShape
* lineShape1
= (wxLineShape
*) shape1
;
619 // Iterate through the segments
620 wxList
* pts1
= lineShape1
->GetLineControlPoints();
622 for (i
= 0; i
< (pts1
->Number() - 1); i
++)
624 wxRealPoint
* pt1_a
= (wxRealPoint
*) (pts1
->Nth(i
)->Data());
625 wxRealPoint
* pt1_b
= (wxRealPoint
*) (pts1
->Nth(i
+1)->Data());
627 // Now we iterate through the segments again
629 wxNode
* node2
= diagram
.GetShapeList()->First();
632 wxShape
* shape2
= (wxShape
*) node2
->Data();
634 // Assume that the same line doesn't cross itself
635 if (shape2
->IsKindOf(CLASSINFO(wxLineShape
)) && (shape1
!= shape2
))
637 wxLineShape
* lineShape2
= (wxLineShape
*) shape2
;
638 // Iterate through the segments
639 wxList
* pts2
= lineShape2
->GetLineControlPoints();
641 for (j
= 0; j
< (pts2
->Number() - 1); j
++)
643 wxRealPoint
* pt2_a
= (wxRealPoint
*) (pts2
->Nth(j
)->Data());
644 wxRealPoint
* pt2_b
= (wxRealPoint
*) (pts2
->Nth(j
+1)->Data());
646 // Now let's see if these two segments cross.
647 double ratio1
, ratio2
;
648 oglCheckLineIntersection(pt1_a
->x
, pt1_a
->y
, pt1_b
->x
, pt1_b
->y
,
649 pt2_a
->x
, pt2_a
->y
, pt2_b
->x
, pt2_b
->y
,
652 if ((ratio1
< 1.0) && (ratio1
> -1.0))
655 wxLineCrossing
* crossing
= new wxLineCrossing
;
656 crossing
->m_intersect
.x
= (pt1_a
->x
+ (pt1_b
->x
- pt1_a
->x
)*ratio1
);
657 crossing
->m_intersect
.y
= (pt1_a
->y
+ (pt1_b
->y
- pt1_a
->y
)*ratio1
);
659 crossing
->m_pt1
= * pt1_a
;
660 crossing
->m_pt2
= * pt1_b
;
661 crossing
->m_pt3
= * pt2_a
;
662 crossing
->m_pt4
= * pt2_b
;
664 crossing
->m_lineShape1
= lineShape1
;
665 crossing
->m_lineShape2
= lineShape2
;
667 m_crossings
.Append(crossing
);
671 node2
= node2
->Next();
676 node1
= node1
->Next();
680 void wxLineCrossings::DrawCrossings(wxDiagram
& diagram
, wxDC
& dc
)
682 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
686 wxNode
* node
= m_crossings
.First();
689 wxLineCrossing
* crossing
= (wxLineCrossing
*) node
->Data();
690 // dc.DrawEllipse((long) (crossing->m_intersect.x - (arcWidth/2.0) + 0.5), (long) (crossing->m_intersect.y - (arcWidth/2.0) + 0.5),
691 // arcWidth, arcWidth);
694 // Let's do some geometry to find the points on either end of the arc.
706 | - x <-- centre of arc
713 |_______________\ (x2, y2)
718 double a1
= wxMax(crossing
->m_pt1
.y
, crossing
->m_pt2
.y
) - wxMin(crossing
->m_pt1
.y
, crossing
->m_pt2
.y
) ;
719 double b1
= wxMax(crossing
->m_pt1
.x
, crossing
->m_pt2
.x
) - wxMin(crossing
->m_pt1
.x
, crossing
->m_pt2
.x
) ;
720 double c1
= sqrt( (a1
*a1
) + (b1
*b1
) );
722 double c
= arcWidth
/ 2.0;
723 double a
= c
* a1
/c1
;
724 double b
= c
* b1
/c1
;
726 // I'm not sure this is right, since we don't know which direction we should be going in - need
727 // to know which way the line slopes and choose the sign appropriately.
728 double arcX1
= crossing
->m_intersect
.x
- b
;
729 double arcY1
= crossing
->m_intersect
.y
- a
;
731 double arcX2
= crossing
->m_intersect
.x
+ b
;
732 double arcY2
= crossing
->m_intersect
.y
+ a
;
734 dc
.SetPen(*wxBLACK_PEN
);
735 dc
.DrawArc( (long) arcX1
, (long) arcY1
, (long) arcX2
, (long) arcY2
,
736 (long) crossing
->m_intersect
.x
, (long) crossing
->m_intersect
.y
);
738 dc
.SetPen(*wxWHITE_PEN
);
739 dc
.DrawLine( (long) arcX1
, (long) arcY1
, (long) arcX2
, (long) arcY2
);
745 void wxLineCrossings::ClearCrossings()
747 wxNode
* node
= m_crossings
.First();
750 wxLineCrossing
* crossing
= (wxLineCrossing
*) node
->Data();