]>
git.saurik.com Git - wxWidgets.git/blob - utils/ogl/src/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>
28 #include <wx/wxexpr.h>
54 IMPLEMENT_DYNAMIC_CLASS(wxDiagram
, wxObject
)
57 wxDiagram::wxDiagram()
59 m_diagramCanvas
= NULL
;
60 m_quickEditMode
= FALSE
;
63 m_shapeList
= new wxList
;
64 m_mouseTolerance
= DEFAULT_MOUSE_TOLERANCE
;
67 wxDiagram::~wxDiagram()
73 void wxDiagram::SetSnapToGrid(bool snap
)
78 void wxDiagram::SetGridSpacing(double spacing
)
80 m_gridSpacing
= spacing
;
83 void wxDiagram::Snap(double *x
, double *y
)
87 *x
= m_gridSpacing
* ((int)(*x
/m_gridSpacing
+ 0.5));
88 *y
= m_gridSpacing
* ((int)(*y
/m_gridSpacing
+ 0.5));
93 void wxDiagram::Redraw(wxDC
& dc
)
98 GetCanvas()->SetCursor(wxHOURGLASS_CURSOR
);
99 wxNode
*current
= m_shapeList
->First();
103 wxShape
*object
= (wxShape
*)current
->Data();
104 if (!object
->GetParent())
107 current
= current
->Next();
110 GetCanvas()->SetCursor(wxSTANDARD_CURSOR
);
114 void wxDiagram::Clear(wxDC
& dc
)
119 // Insert object after addAfter, or at end of list.
120 void wxDiagram::AddShape(wxShape
*object
, wxShape
*addAfter
)
122 wxNode
*nodeAfter
= NULL
;
124 nodeAfter
= m_shapeList
->Member(addAfter
);
126 if (!m_shapeList
->Member(object
))
130 if (nodeAfter
->Next())
131 m_shapeList
->Insert(nodeAfter
->Next(), object
);
133 m_shapeList
->Append(object
);
136 m_shapeList
->Append(object
);
137 object
->SetCanvas(GetCanvas());
141 void wxDiagram::InsertShape(wxShape
*object
)
143 m_shapeList
->Insert(object
);
144 object
->SetCanvas(GetCanvas());
147 void wxDiagram::RemoveShape(wxShape
*object
)
149 m_shapeList
->DeleteObject(object
);
152 // Should this delete the actual objects too? I think not.
153 void wxDiagram::RemoveAllShapes()
155 m_shapeList
->Clear();
158 void wxDiagram::DeleteAllShapes()
160 wxNode
*node
= m_shapeList
->First();
163 wxShape
*shape
= (wxShape
*)node
->Data();
164 if (!shape
->GetParent())
168 node
= m_shapeList
->First();
175 void wxDiagram::ShowAll(bool show
)
177 wxNode
*current
= m_shapeList
->First();
181 wxShape
*object
= (wxShape
*)current
->Data();
184 current
= current
->Next();
188 void wxDiagram::DrawOutline(wxDC
& dc
, double x1
, double y1
, double x2
, double y2
)
190 wxPen
dottedPen(wxColour(0, 0, 0), 1, wxDOT
);
191 dc
.SetPen(dottedPen
);
192 dc
.SetBrush((* wxTRANSPARENT_BRUSH
));
210 dc
.DrawLines(5, points
);
213 // Make sure all text that should be centred, is centred.
214 void wxDiagram::RecentreAll(wxDC
& dc
)
216 wxNode
*object_node
= m_shapeList
->First();
219 wxShape
*obj
= (wxShape
*)object_node
->Data();
221 object_node
= object_node
->Next();
227 bool wxDiagram::SaveFile(const wxString
& filename
)
231 wxExprDatabase
*database
= new wxExprDatabase
;
233 // First write the diagram type
234 wxExpr
*header
= new wxExpr("diagram");
235 OnHeaderSave(*database
, *header
);
237 database
->Append(header
);
239 wxNode
*node
= m_shapeList
->First();
242 wxShape
*shape
= (wxShape
*)node
->Data();
244 if (!shape
->IsKindOf(CLASSINFO(wxControlPoint
)))
247 if (shape
->IsKindOf(CLASSINFO(wxLineShape
)))
248 expr
= new wxExpr("line");
250 expr
= new wxExpr("shape");
252 OnShapeSave(*database
, *shape
, *expr
);
256 OnDatabaseSave(*database
);
259 wxGetTempFileName("diag", tempFile
);
260 ofstream
stream(tempFile
);
268 database
->Write(stream
);
274 if (FileExists(filename))
278 sprintf(buf, "%s.bak", filename);
281 sprintf(buf, "_diagram.bak");
283 if (FileExists(buf)) wxRemoveFile(buf);
284 if (!wxRenameFile(filename, buf))
286 wxCopyFile(filename, buf);
287 wxRemoveFile(filename);
292 // Copy the temporary file to the correct filename
293 if (!wxRenameFile(tempFile
, filename
))
295 wxCopyFile(tempFile
, filename
);
296 wxRemoveFile(tempFile
);
303 bool wxDiagram::LoadFile(const wxString
& filename
)
307 wxExprDatabase
database(wxExprInteger
, "id");
308 if (!database
.Read(filename
))
316 database
.BeginFind();
317 wxExpr
*header
= database
.FindClauseByFunctor("diagram");
320 OnHeaderLoad(database
, *header
);
322 // Scan through all clauses and register the ids
323 wxNode
*node
= database
.First();
326 wxExpr
*clause
= (wxExpr
*)node
->Data();
328 clause
->GetAttributeValue("id", id
);
334 ReadContainerGeometry(database
);
337 OnDatabaseLoad(database
);
344 void wxDiagram::ReadNodes(wxExprDatabase
& database
)
346 // Find and create the node images
347 database
.BeginFind();
348 wxExpr
*clause
= database
.FindClauseByFunctor("shape");
354 clause
->AssignAttributeValue("type", &type
);
355 clause
->AssignAttributeValue("parent", &parentId
);
356 wxClassInfo
*classInfo
= wxClassInfo::FindClass(type
);
359 wxShape
*shape
= (wxShape
*)classInfo
->CreateObject();
360 OnShapeLoad(database
, *shape
, *clause
);
362 shape
->SetCanvas(GetCanvas());
365 m_shapeList
->Append(shape
);
367 // If child of composite, link up
370 wxExpr
*parentExpr
= database
.HashFind("shape", parentId
);
371 if (parentExpr
&& parentExpr
->GetClientData())
373 wxShape
*parent
= (wxShape
*)parentExpr
->GetClientData();
374 shape
->SetParent(parent
);
375 parent
->GetChildren().Append(shape
);
379 clause
->SetClientData(shape
);
384 clause
= database
.FindClauseByFunctor("shape");
389 void wxDiagram::ReadLines(wxExprDatabase
& database
)
391 database
.BeginFind();
392 wxExpr
*clause
= database
.FindClauseByFunctor("line");
398 clause
->GetAttributeValue("type", type
);
399 clause
->GetAttributeValue("parent", parentId
);
400 wxClassInfo
*classInfo
= wxClassInfo::FindClass((char*) (const char*) type
);
403 wxLineShape
*shape
= (wxLineShape
*)classInfo
->CreateObject();
406 OnShapeLoad(database
, *shape
, *clause
);
407 shape
->SetCanvas(GetCanvas());
409 long image_to
= -1; long image_from
= -1;
410 clause
->GetAttributeValue("to", image_to
);
411 clause
->GetAttributeValue("from", image_from
);
413 wxExpr
*image_to_expr
= database
.HashFind("shape", image_to
);
419 wxExpr
*image_from_expr
= database
.HashFind("shape", image_from
);
421 if (!image_from_expr
)
426 if (image_to_expr
&& image_from_expr
)
428 wxShape
*image_to_object
= (wxShape
*)image_to_expr
->GetClientData();
429 wxShape
*image_from_object
= (wxShape
*)image_from_expr
->GetClientData();
431 if (image_to_object
&& image_from_object
)
433 image_from_object
->AddLine(shape
, image_to_object
, shape
->GetAttachmentFrom(), shape
->GetAttachmentTo());
436 clause
->SetClientData(shape
);
438 m_shapeList
->Append(shape
);
441 clause
= database
.FindClauseByFunctor("line");
445 // Containers have divisions that reference adjoining divisions,
446 // so we need a separate pass to link everything up.
447 // Also used by Symbol Library.
448 void wxDiagram::ReadContainerGeometry(wxExprDatabase
& database
)
450 database
.BeginFind();
451 wxExpr
*clause
= database
.FindClauseByFunctor("shape");
454 wxShape
*image
= (wxShape
*)clause
->GetClientData();
455 if (image
&& image
->IsKindOf(CLASSINFO(wxCompositeShape
)))
457 wxCompositeShape
*composite
= (wxCompositeShape
*)image
;
458 wxExpr
*divisionExpr
= NULL
;
460 // Find the list of divisions in the composite
461 clause
->GetAttributeValue("divisions", &divisionExpr
);
465 wxExpr
*idExpr
= divisionExpr
->Nth(i
);
468 long divisionId
= idExpr
->IntegerValue();
469 wxExpr
*childExpr
= database
.HashFind("shape", divisionId
);
470 if (childExpr
&& childExpr
->GetClientData())
472 wxDivisionShape
*child
= (wxDivisionShape
*)childExpr
->GetClientData();
473 composite
->GetDivisions().Append(child
);
475 // Find the adjoining shapes
476 long leftSideId
= -1;
478 long rightSideId
= -1;
479 long bottomSideId
= -1;
480 childExpr
->GetAttributeValue("left_side", leftSideId
);
481 childExpr
->GetAttributeValue("top_side", topSideId
);
482 childExpr
->GetAttributeValue("right_side", rightSideId
);
483 childExpr
->GetAttributeValue("bottom_side", bottomSideId
);
486 wxExpr
*leftExpr
= database
.HashFind("shape", leftSideId
);
487 if (leftExpr
&& leftExpr
->GetClientData())
489 wxDivisionShape
*leftSide
= (wxDivisionShape
*)leftExpr
->GetClientData();
490 child
->SetLeftSide(leftSide
);
495 wxExpr
*topExpr
= database
.HashFind("shape", topSideId
);
496 if (topExpr
&& topExpr
->GetClientData())
498 wxDivisionShape
*topSide
= (wxDivisionShape
*)topExpr
->GetClientData();
499 child
->SetTopSide(topSide
);
502 if (rightSideId
> -1)
504 wxExpr
*rightExpr
= database
.HashFind("shape", rightSideId
);
505 if (rightExpr
&& rightExpr
->GetClientData())
507 wxDivisionShape
*rightSide
= (wxDivisionShape
*)rightExpr
->GetClientData();
508 child
->SetRightSide(rightSide
);
511 if (bottomSideId
> -1)
513 wxExpr
*bottomExpr
= database
.HashFind("shape", bottomSideId
);
514 if (bottomExpr
&& bottomExpr
->GetClientData())
516 wxDivisionShape
*bottomSide
= (wxDivisionShape
*)bottomExpr
->GetClientData();
517 child
->SetBottomSide(bottomSide
);
522 idExpr
= divisionExpr
->Nth(i
);
527 clause
= database
.FindClauseByFunctor("shape");
531 // Allow for modifying file
532 bool wxDiagram::OnDatabaseLoad(wxExprDatabase
& db
)
537 bool wxDiagram::OnDatabaseSave(wxExprDatabase
& db
)
542 bool wxDiagram::OnShapeSave(wxExprDatabase
& db
, wxShape
& shape
, wxExpr
& expr
)
544 shape
.WriteAttributes(&expr
);
547 if (shape
.IsKindOf(CLASSINFO(wxCompositeShape
)))
549 wxNode
*node
= shape
.GetChildren().First();
552 wxShape
*childShape
= (wxShape
*)node
->Data();
553 wxExpr
*childExpr
= new wxExpr("shape");
554 OnShapeSave(db
, *childShape
, *childExpr
);
562 bool wxDiagram::OnShapeLoad(wxExprDatabase
& db
, wxShape
& shape
, wxExpr
& expr
)
564 shape
.ReadAttributes(&expr
);
568 bool wxDiagram::OnHeaderSave(wxExprDatabase
& db
, wxExpr
& expr
)
573 bool wxDiagram::OnHeaderLoad(wxExprDatabase
& db
, wxExpr
& expr
)
580 void wxDiagram::SetCanvas(wxShapeCanvas
*can
)
582 m_diagramCanvas
= can
;
585 // Find a shape by its id
586 wxShape
* wxDiagram::FindShape(long id
) const
588 wxNode
* node
= GetShapeList()->First();
591 wxShape
* shape
= (wxShape
*) node
->Data();
592 if (shape
->GetId() == id
)
600 //// Crossings classes
602 wxLineCrossings::wxLineCrossings()
606 wxLineCrossings::~wxLineCrossings()
611 void wxLineCrossings::FindCrossings(wxDiagram
& diagram
)
614 wxNode
* node1
= diagram
.GetShapeList()->First();
617 wxShape
* shape1
= (wxShape
*) node1
->Data();
618 if (shape1
->IsKindOf(CLASSINFO(wxLineShape
)))
620 wxLineShape
* lineShape1
= (wxLineShape
*) shape1
;
621 // Iterate through the segments
622 wxList
* pts1
= lineShape1
->GetLineControlPoints();
624 for (i
= 0; i
< (pts1
->Number() - 1); i
++)
626 wxRealPoint
* pt1_a
= (wxRealPoint
*) (pts1
->Nth(i
)->Data());
627 wxRealPoint
* pt1_b
= (wxRealPoint
*) (pts1
->Nth(i
+1)->Data());
629 // Now we iterate through the segments again
631 wxNode
* node2
= diagram
.GetShapeList()->First();
634 wxShape
* shape2
= (wxShape
*) node2
->Data();
636 // Assume that the same line doesn't cross itself
637 if (shape2
->IsKindOf(CLASSINFO(wxLineShape
)) && (shape1
!= shape2
))
639 wxLineShape
* lineShape2
= (wxLineShape
*) shape2
;
640 // Iterate through the segments
641 wxList
* pts2
= lineShape2
->GetLineControlPoints();
643 for (j
= 0; j
< (pts2
->Number() - 1); j
++)
645 wxRealPoint
* pt2_a
= (wxRealPoint
*) (pts2
->Nth(j
)->Data());
646 wxRealPoint
* pt2_b
= (wxRealPoint
*) (pts2
->Nth(j
+1)->Data());
648 // Now let's see if these two segments cross.
649 double ratio1
, ratio2
;
650 oglCheckLineIntersection(pt1_a
->x
, pt1_a
->y
, pt1_b
->x
, pt1_b
->y
,
651 pt2_a
->x
, pt2_a
->y
, pt2_b
->x
, pt2_b
->y
,
654 if ((ratio1
< 1.0) && (ratio1
> -1.0))
657 wxLineCrossing
* crossing
= new wxLineCrossing
;
658 crossing
->m_intersect
.x
= (pt1_a
->x
+ (pt1_b
->x
- pt1_a
->x
)*ratio1
);
659 crossing
->m_intersect
.y
= (pt1_a
->y
+ (pt1_b
->y
- pt1_a
->y
)*ratio1
);
661 crossing
->m_pt1
= * pt1_a
;
662 crossing
->m_pt2
= * pt1_b
;
663 crossing
->m_pt3
= * pt2_a
;
664 crossing
->m_pt4
= * pt2_b
;
666 crossing
->m_lineShape1
= lineShape1
;
667 crossing
->m_lineShape2
= lineShape2
;
669 m_crossings
.Append(crossing
);
673 node2
= node2
->Next();
678 node1
= node1
->Next();
682 void wxLineCrossings::DrawCrossings(wxDiagram
& diagram
, wxDC
& dc
)
684 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
688 wxNode
* node
= m_crossings
.First();
691 wxLineCrossing
* crossing
= (wxLineCrossing
*) node
->Data();
692 // dc.DrawEllipse((long) (crossing->m_intersect.x - (arcWidth/2.0) + 0.5), (long) (crossing->m_intersect.y - (arcWidth/2.0) + 0.5),
693 // arcWidth, arcWidth);
696 // Let's do some geometry to find the points on either end of the arc.
708 | - x <-- centre of arc
715 |_______________\ (x2, y2)
720 double a1
= wxMax(crossing
->m_pt1
.y
, crossing
->m_pt2
.y
) - wxMin(crossing
->m_pt1
.y
, crossing
->m_pt2
.y
) ;
721 double b1
= wxMax(crossing
->m_pt1
.x
, crossing
->m_pt2
.x
) - wxMin(crossing
->m_pt1
.x
, crossing
->m_pt2
.x
) ;
722 double c1
= sqrt( (a1
*a1
) + (b1
*b1
) );
724 double c
= arcWidth
/ 2.0;
725 double a
= c
* a1
/c1
;
726 double b
= c
* b1
/c1
;
728 // I'm not sure this is right, since we don't know which direction we should be going in - need
729 // to know which way the line slopes and choose the sign appropriately.
730 double arcX1
= crossing
->m_intersect
.x
- b
;
731 double arcY1
= crossing
->m_intersect
.y
- a
;
733 double arcX2
= crossing
->m_intersect
.x
+ b
;
734 double arcY2
= crossing
->m_intersect
.y
+ a
;
736 dc
.SetPen(*wxBLACK_PEN
);
737 dc
.DrawArc( (long) arcX1
, (long) arcY1
, (long) arcX2
, (long) arcY2
,
738 (long) crossing
->m_intersect
.x
, (long) crossing
->m_intersect
.y
);
740 dc
.SetPen(*wxWHITE_PEN
);
741 dc
.DrawLine( (long) arcX1
, (long) arcY1
, (long) arcX2
, (long) arcY2
);
747 void wxLineCrossings::ClearCrossings()
749 wxNode
* node
= m_crossings
.First();
752 wxLineCrossing
* crossing
= (wxLineCrossing
*) node
->Data();