]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/ogl/ogldiag.cpp
Added bakefile for the OGL contrib
[wxWidgets.git] / contrib / src / ogl / ogldiag.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: ogldiag.cpp
3 // Purpose: wxDiagram
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 12/07/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "ogldiag.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include <wx/wx.h>
25 #endif
26
27 #if wxUSE_DEPRECATED
28 #include <wx/deprecated/wxexpr.h>
29 #endif
30
31 #ifdef new
32 #undef new
33 #endif
34
35 #include <ctype.h>
36 #include <math.h>
37 #include <stdlib.h>
38
39 #include <wx/ogl/basic.h>
40 #include <wx/ogl/basicp.h>
41 #include <wx/ogl/canvas.h>
42 #include <wx/ogl/ogldiag.h>
43 #include <wx/ogl/lines.h>
44 #include <wx/ogl/composit.h>
45 #include <wx/ogl/misc.h>
46
47 IMPLEMENT_DYNAMIC_CLASS(wxDiagram, wxObject)
48
49 // Object canvas
50 wxDiagram::wxDiagram()
51 {
52 m_diagramCanvas = NULL;
53 m_quickEditMode = FALSE;
54 m_snapToGrid = TRUE;
55 m_gridSpacing = 5.0;
56 m_shapeList = new wxList;
57 m_mouseTolerance = DEFAULT_MOUSE_TOLERANCE;
58 }
59
60 wxDiagram::~wxDiagram()
61 {
62 if (m_shapeList)
63 delete m_shapeList;
64 }
65
66 void wxDiagram::SetSnapToGrid(bool snap)
67 {
68 m_snapToGrid = snap;
69 }
70
71 void wxDiagram::SetGridSpacing(double spacing)
72 {
73 m_gridSpacing = spacing;
74 }
75
76 void wxDiagram::Snap(double *x, double *y)
77 {
78 if (m_snapToGrid)
79 {
80 *x = m_gridSpacing * ((int)(*x/m_gridSpacing + 0.5));
81 *y = m_gridSpacing * ((int)(*y/m_gridSpacing + 0.5));
82 }
83 }
84
85
86 void wxDiagram::Redraw(wxDC& dc)
87 {
88 if (m_shapeList)
89 {
90 if (GetCanvas())
91 GetCanvas()->SetCursor(* wxHOURGLASS_CURSOR);
92 wxNode *current = m_shapeList->GetFirst();
93
94 while (current)
95 {
96 wxShape *object = (wxShape *)current->GetData();
97 if (!object->GetParent())
98 object->Draw(dc);
99
100 current = current->GetNext();
101 }
102 if (GetCanvas())
103 GetCanvas()->SetCursor(* wxSTANDARD_CURSOR);
104 }
105 }
106
107 void wxDiagram::Clear(wxDC& dc)
108 {
109 dc.Clear();
110 }
111
112 // Insert object after addAfter, or at end of list.
113 void wxDiagram::AddShape(wxShape *object, wxShape *addAfter)
114 {
115 wxNode *nodeAfter = NULL;
116 if (addAfter)
117 nodeAfter = m_shapeList->Member(addAfter);
118
119 if (!m_shapeList->Member(object))
120 {
121 if (nodeAfter)
122 {
123 if (nodeAfter->GetNext())
124 m_shapeList->Insert(nodeAfter->GetNext(), object);
125 else
126 m_shapeList->Append(object);
127 }
128 else
129 m_shapeList->Append(object);
130 object->SetCanvas(GetCanvas());
131 }
132 }
133
134 void wxDiagram::InsertShape(wxShape *object)
135 {
136 m_shapeList->Insert(object);
137 object->SetCanvas(GetCanvas());
138 }
139
140 void wxDiagram::RemoveShape(wxShape *object)
141 {
142 m_shapeList->DeleteObject(object);
143 }
144
145 // Should this delete the actual objects too? I think not.
146 void wxDiagram::RemoveAllShapes()
147 {
148 m_shapeList->Clear();
149 }
150
151 void wxDiagram::DeleteAllShapes()
152 {
153 wxNode *node = m_shapeList->GetFirst();
154 while (node)
155 {
156 wxShape *shape = (wxShape *)node->GetData();
157 if (!shape->GetParent())
158 {
159 RemoveShape(shape);
160 delete shape;
161 node = m_shapeList->GetFirst();
162 }
163 else
164 node = node->GetNext();
165 }
166 }
167
168 void wxDiagram::ShowAll(bool show)
169 {
170 wxNode *current = m_shapeList->GetFirst();
171
172 while (current)
173 {
174 wxShape *object = (wxShape *)current->GetData();
175 object->Show(show);
176
177 current = current->GetNext();
178 }
179 }
180
181 void wxDiagram::DrawOutline(wxDC& dc, double x1, double y1, double x2, double y2)
182 {
183 wxPen dottedPen(wxColour(0, 0, 0), 1, wxDOT);
184 dc.SetPen(dottedPen);
185 dc.SetBrush((* wxTRANSPARENT_BRUSH));
186
187 wxPoint points[5];
188
189 points[0].x = (int) x1;
190 points[0].y = (int) y1;
191
192 points[1].x = (int) x2;
193 points[1].y = (int) y1;
194
195 points[2].x = (int) x2;
196 points[2].y = (int) y2;
197
198 points[3].x = (int) x1;
199 points[3].y = (int) y2;
200
201 points[4].x = (int) x1;
202 points[4].y = (int) y1;
203 dc.DrawLines(5, points);
204 }
205
206 // Make sure all text that should be centred, is centred.
207 void wxDiagram::RecentreAll(wxDC& dc)
208 {
209 wxNode *object_node = m_shapeList->GetFirst();
210 while (object_node)
211 {
212 wxShape *obj = (wxShape *)object_node->GetData();
213 obj->Recentre(dc);
214 object_node = object_node->GetNext();
215 }
216 }
217
218 // Input/output
219 #if wxUSE_PROLOGIO
220 bool wxDiagram::SaveFile(const wxString& filename)
221 {
222 wxBeginBusyCursor();
223
224 wxExprDatabase *database = new wxExprDatabase;
225
226 // First write the diagram type
227 wxExpr *header = new wxExpr("diagram");
228 OnHeaderSave(*database, *header);
229
230 database->Append(header);
231
232 wxNode *node = m_shapeList->GetFirst();
233 while (node)
234 {
235 wxShape *shape = (wxShape *)node->GetData();
236
237 if (!shape->IsKindOf(CLASSINFO(wxControlPoint)))
238 {
239 wxExpr *expr = NULL;
240 if (shape->IsKindOf(CLASSINFO(wxLineShape)))
241 expr = new wxExpr("line");
242 else
243 expr = new wxExpr("shape");
244
245 OnShapeSave(*database, *shape, *expr);
246 }
247 node = node->Next();
248 }
249 OnDatabaseSave(*database);
250
251 wxString tempFile;
252 wxGetTempFileName(wxT("diag"), tempFile);
253 FILE* file = fopen(tempFile.mb_str(wxConvFile), "w");
254 if (! file)
255 {
256 wxEndBusyCursor();
257 delete database;
258 return FALSE;
259 }
260
261 database->Write(file);
262 fclose(file);
263 delete database;
264
265 /*
266 // Save backup
267 if (FileExists(filename))
268 {
269 char buf[400];
270 #ifdef __X__
271 sprintf(buf, "%s.bak", filename);
272 #endif
273 #ifdef __WXMSW__
274 sprintf(buf, "_diagram.bak");
275 #endif
276 if (FileExists(buf)) wxRemoveFile(buf);
277 if (!wxRenameFile(filename, buf))
278 {
279 wxCopyFile(filename, buf);
280 wxRemoveFile(filename);
281 }
282 }
283 */
284
285 // Copy the temporary file to the correct filename
286 if (!wxRenameFile(tempFile, filename))
287 {
288 wxCopyFile(tempFile, filename);
289 wxRemoveFile(tempFile);
290 }
291
292 wxEndBusyCursor();
293 return TRUE;
294 }
295
296 bool wxDiagram::LoadFile(const wxString& filename)
297 {
298 wxBeginBusyCursor();
299
300 wxExprDatabase database(wxExprInteger, "id");
301 if (!database.Read(filename))
302 {
303 wxEndBusyCursor();
304 return FALSE;
305 }
306
307 DeleteAllShapes();
308
309 database.BeginFind();
310 wxExpr *header = database.FindClauseByFunctor("diagram");
311
312 if (header)
313 OnHeaderLoad(database, *header);
314
315 // Scan through all clauses and register the ids
316 wxNode *node = database.First();
317 while (node)
318 {
319 wxExpr *clause = (wxExpr *)node->GetData();
320 long id = -1;
321 clause->GetAttributeValue("id", id);
322 wxRegisterId(id);
323 node = node->Next();
324 }
325
326 ReadNodes(database);
327 ReadContainerGeometry(database);
328 ReadLines(database);
329
330 OnDatabaseLoad(database);
331
332 wxEndBusyCursor();
333
334 return TRUE;
335 }
336
337 void wxDiagram::ReadNodes(wxExprDatabase& database)
338 {
339 // Find and create the node images
340 database.BeginFind();
341 wxExpr *clause = database.FindClauseByFunctor("shape");
342 while (clause)
343 {
344 wxChar *type = NULL;
345 long parentId = -1;
346
347 clause->AssignAttributeValue(wxT("type"), &type);
348 clause->AssignAttributeValue(wxT("parent"), &parentId);
349 wxClassInfo *classInfo = wxClassInfo::FindClass(type);
350 if (classInfo)
351 {
352 wxShape *shape = (wxShape *)classInfo->CreateObject();
353 OnShapeLoad(database, *shape, *clause);
354
355 shape->SetCanvas(GetCanvas());
356 shape->Show(TRUE);
357
358 m_shapeList->Append(shape);
359
360 // If child of composite, link up
361 if (parentId > -1)
362 {
363 wxExpr *parentExpr = database.HashFind("shape", parentId);
364 if (parentExpr && parentExpr->GetClientData())
365 {
366 wxShape *parent = (wxShape *)parentExpr->GetClientData();
367 shape->SetParent(parent);
368 parent->GetChildren().Append(shape);
369 }
370 }
371
372 clause->SetClientData(shape);
373 }
374 if (type)
375 delete[] type;
376
377 clause = database.FindClauseByFunctor("shape");
378 }
379 return;
380 }
381
382 void wxDiagram::ReadLines(wxExprDatabase& database)
383 {
384 database.BeginFind();
385 wxExpr *clause = database.FindClauseByFunctor("line");
386 while (clause)
387 {
388 wxString type;
389 long parentId = -1;
390
391 clause->GetAttributeValue("type", type);
392 clause->GetAttributeValue("parent", parentId);
393 wxClassInfo *classInfo = wxClassInfo::FindClass(type);
394 if (classInfo)
395 {
396 wxLineShape *shape = (wxLineShape *)classInfo->CreateObject();
397 shape->Show(TRUE);
398
399 OnShapeLoad(database, *shape, *clause);
400 shape->SetCanvas(GetCanvas());
401
402 long image_to = -1; long image_from = -1;
403 clause->GetAttributeValue("to", image_to);
404 clause->GetAttributeValue("from", image_from);
405
406 wxExpr *image_to_expr = database.HashFind("shape", image_to);
407
408 if (!image_to_expr)
409 {
410 // Error
411 }
412 wxExpr *image_from_expr = database.HashFind("shape", image_from);
413
414 if (!image_from_expr)
415 {
416 // Error
417 }
418
419 if (image_to_expr && image_from_expr)
420 {
421 wxShape *image_to_object = (wxShape *)image_to_expr->GetClientData();
422 wxShape *image_from_object = (wxShape *)image_from_expr->GetClientData();
423
424 if (image_to_object && image_from_object)
425 {
426 image_from_object->AddLine(shape, image_to_object, shape->GetAttachmentFrom(), shape->GetAttachmentTo());
427 }
428 }
429 clause->SetClientData(shape);
430
431 m_shapeList->Append(shape);
432 }
433
434 clause = database.FindClauseByFunctor("line");
435 }
436 }
437
438 // Containers have divisions that reference adjoining divisions,
439 // so we need a separate pass to link everything up.
440 // Also used by Symbol Library.
441 void wxDiagram::ReadContainerGeometry(wxExprDatabase& database)
442 {
443 database.BeginFind();
444 wxExpr *clause = database.FindClauseByFunctor("shape");
445 while (clause)
446 {
447 wxShape *image = (wxShape *)clause->GetClientData();
448 if (image && image->IsKindOf(CLASSINFO(wxCompositeShape)))
449 {
450 wxCompositeShape *composite = (wxCompositeShape *)image;
451 wxExpr *divisionExpr = NULL;
452
453 // Find the list of divisions in the composite
454 clause->GetAttributeValue("divisions", &divisionExpr);
455 if (divisionExpr)
456 {
457 int i = 0;
458 wxExpr *idExpr = divisionExpr->Nth(i);
459 while (idExpr)
460 {
461 long divisionId = idExpr->IntegerValue();
462 wxExpr *childExpr = database.HashFind("shape", divisionId);
463 if (childExpr && childExpr->GetClientData())
464 {
465 wxDivisionShape *child = (wxDivisionShape *)childExpr->GetClientData();
466 composite->GetDivisions().Append(child);
467
468 // Find the adjoining shapes
469 long leftSideId = -1;
470 long topSideId = -1;
471 long rightSideId = -1;
472 long bottomSideId = -1;
473 childExpr->GetAttributeValue("left_side", leftSideId);
474 childExpr->GetAttributeValue("top_side", topSideId);
475 childExpr->GetAttributeValue("right_side", rightSideId);
476 childExpr->GetAttributeValue("bottom_side", bottomSideId);
477 if (leftSideId > -1)
478 {
479 wxExpr *leftExpr = database.HashFind("shape", leftSideId);
480 if (leftExpr && leftExpr->GetClientData())
481 {
482 wxDivisionShape *leftSide = (wxDivisionShape *)leftExpr->GetClientData();
483 child->SetLeftSide(leftSide);
484 }
485 }
486 if (topSideId > -1)
487 {
488 wxExpr *topExpr = database.HashFind("shape", topSideId);
489 if (topExpr && topExpr->GetClientData())
490 {
491 wxDivisionShape *topSide = (wxDivisionShape *)topExpr->GetClientData();
492 child->SetTopSide(topSide);
493 }
494 }
495 if (rightSideId > -1)
496 {
497 wxExpr *rightExpr = database.HashFind("shape", rightSideId);
498 if (rightExpr && rightExpr->GetClientData())
499 {
500 wxDivisionShape *rightSide = (wxDivisionShape *)rightExpr->GetClientData();
501 child->SetRightSide(rightSide);
502 }
503 }
504 if (bottomSideId > -1)
505 {
506 wxExpr *bottomExpr = database.HashFind("shape", bottomSideId);
507 if (bottomExpr && bottomExpr->GetClientData())
508 {
509 wxDivisionShape *bottomSide = (wxDivisionShape *)bottomExpr->GetClientData();
510 child->SetBottomSide(bottomSide);
511 }
512 }
513 }
514 i ++;
515 idExpr = divisionExpr->Nth(i);
516 }
517 }
518 }
519
520 clause = database.FindClauseByFunctor("shape");
521 }
522 }
523
524 // Allow for modifying file
525 bool wxDiagram::OnDatabaseLoad(wxExprDatabase& db)
526 {
527 return TRUE;
528 }
529
530 bool wxDiagram::OnDatabaseSave(wxExprDatabase& db)
531 {
532 return TRUE;
533 }
534
535 bool wxDiagram::OnShapeSave(wxExprDatabase& db, wxShape& shape, wxExpr& expr)
536 {
537 shape.WriteAttributes(&expr);
538 db.Append(&expr);
539
540 if (shape.IsKindOf(CLASSINFO(wxCompositeShape)))
541 {
542 wxNode *node = shape.GetChildren().First();
543 while (node)
544 {
545 wxShape *childShape = (wxShape *)node->GetData();
546 wxExpr *childExpr = new wxExpr("shape");
547 OnShapeSave(db, *childShape, *childExpr);
548 node = node->Next();
549 }
550 }
551
552 return TRUE;
553 }
554
555 bool wxDiagram::OnShapeLoad(wxExprDatabase& db, wxShape& shape, wxExpr& expr)
556 {
557 shape.ReadAttributes(&expr);
558 return TRUE;
559 }
560
561 bool wxDiagram::OnHeaderSave(wxExprDatabase& db, wxExpr& expr)
562 {
563 return TRUE;
564 }
565
566 bool wxDiagram::OnHeaderLoad(wxExprDatabase& db, wxExpr& expr)
567 {
568 return TRUE;
569 }
570
571 #endif
572
573 void wxDiagram::SetCanvas(wxShapeCanvas *can)
574 {
575 m_diagramCanvas = can;
576 }
577
578 // Find a shape by its id
579 wxShape* wxDiagram::FindShape(long id) const
580 {
581 wxNode* node = GetShapeList()->GetFirst();
582 while (node)
583 {
584 wxShape* shape = (wxShape*) node->GetData();
585 if (shape->GetId() == id)
586 return shape;
587 node = node->GetNext();
588 }
589 return NULL;
590 }
591
592
593 //// Crossings classes
594
595 wxLineCrossings::wxLineCrossings()
596 {
597 }
598
599 wxLineCrossings::~wxLineCrossings()
600 {
601 ClearCrossings();
602 }
603
604 void wxLineCrossings::FindCrossings(wxDiagram& diagram)
605 {
606 ClearCrossings();
607 wxNode* node1 = diagram.GetShapeList()->GetFirst();
608 while (node1)
609 {
610 wxShape* shape1 = (wxShape*) node1->GetData();
611 if (shape1->IsKindOf(CLASSINFO(wxLineShape)))
612 {
613 wxLineShape* lineShape1 = (wxLineShape*) shape1;
614 // Iterate through the segments
615 wxList* pts1 = lineShape1->GetLineControlPoints();
616 int i;
617 for (i = 0; i < (pts1->GetCount() - 1); i++)
618 {
619 wxRealPoint* pt1_a = (wxRealPoint*) (pts1->Item(i)->GetData());
620 wxRealPoint* pt1_b = (wxRealPoint*) (pts1->Item(i+1)->GetData());
621
622 // Now we iterate through the segments again
623
624 wxNode* node2 = diagram.GetShapeList()->GetFirst();
625 while (node2)
626 {
627 wxShape* shape2 = (wxShape*) node2->GetData();
628
629 // Assume that the same line doesn't cross itself
630 if (shape2->IsKindOf(CLASSINFO(wxLineShape)) && (shape1 != shape2))
631 {
632 wxLineShape* lineShape2 = (wxLineShape*) shape2;
633 // Iterate through the segments
634 wxList* pts2 = lineShape2->GetLineControlPoints();
635 int j;
636 for (j = 0; j < (pts2->GetCount() - 1); j++)
637 {
638 wxRealPoint* pt2_a = (wxRealPoint*) (pts2->Item(j)->GetData());
639 wxRealPoint* pt2_b = (wxRealPoint*) (pts2->Item(j+1)->GetData());
640
641 // Now let's see if these two segments cross.
642 double ratio1, ratio2;
643 oglCheckLineIntersection(pt1_a->x, pt1_a->y, pt1_b->x, pt1_b->y,
644 pt2_a->x, pt2_a->y, pt2_b->x, pt2_b->y,
645 & ratio1, & ratio2);
646
647 if ((ratio1 < 1.0) && (ratio1 > -1.0))
648 {
649 // Intersection!
650 wxLineCrossing* crossing = new wxLineCrossing;
651 crossing->m_intersect.x = (pt1_a->x + (pt1_b->x - pt1_a->x)*ratio1);
652 crossing->m_intersect.y = (pt1_a->y + (pt1_b->y - pt1_a->y)*ratio1);
653
654 crossing->m_pt1 = * pt1_a;
655 crossing->m_pt2 = * pt1_b;
656 crossing->m_pt3 = * pt2_a;
657 crossing->m_pt4 = * pt2_b;
658
659 crossing->m_lineShape1 = lineShape1;
660 crossing->m_lineShape2 = lineShape2;
661
662 m_crossings.Append(crossing);
663 }
664 }
665 }
666 node2 = node2->GetNext();
667 }
668 }
669 }
670
671 node1 = node1->GetNext();
672 }
673 }
674
675 void wxLineCrossings::DrawCrossings(wxDiagram& diagram, wxDC& dc)
676 {
677 dc.SetBrush(*wxTRANSPARENT_BRUSH);
678
679 long arcWidth = 8;
680
681 wxNode* node = m_crossings.GetFirst();
682 while (node)
683 {
684 wxLineCrossing* crossing = (wxLineCrossing*) node->GetData();
685 // dc.DrawEllipse((long) (crossing->m_intersect.x - (arcWidth/2.0) + 0.5), (long) (crossing->m_intersect.y - (arcWidth/2.0) + 0.5),
686 // arcWidth, arcWidth);
687
688
689 // Let's do some geometry to find the points on either end of the arc.
690 /*
691
692 (x1, y1)
693 |\
694 | \
695 | \
696 | \
697 | \
698 | |\ c c1
699 | a | \
700 | \
701 | - x <-- centre of arc
702 a1 | b |\
703 | | \ c2
704 | a2 | \
705 | - \
706 | b2 \
707 | \
708 |_______________\ (x2, y2)
709 b1
710
711 */
712
713 double a1 = wxMax(crossing->m_pt1.y, crossing->m_pt2.y) - wxMin(crossing->m_pt1.y, crossing->m_pt2.y) ;
714 double b1 = wxMax(crossing->m_pt1.x, crossing->m_pt2.x) - wxMin(crossing->m_pt1.x, crossing->m_pt2.x) ;
715 double c1 = sqrt( (a1*a1) + (b1*b1) );
716
717 double c = arcWidth / 2.0;
718 double a = c * a1/c1 ;
719 double b = c * b1/c1 ;
720
721 // I'm not sure this is right, since we don't know which direction we should be going in - need
722 // to know which way the line slopes and choose the sign appropriately.
723 double arcX1 = crossing->m_intersect.x - b;
724 double arcY1 = crossing->m_intersect.y - a;
725
726 double arcX2 = crossing->m_intersect.x + b;
727 double arcY2 = crossing->m_intersect.y + a;
728
729 dc.SetPen(*wxBLACK_PEN);
730 dc.DrawArc( (long) arcX1, (long) arcY1, (long) arcX2, (long) arcY2,
731 (long) crossing->m_intersect.x, (long) crossing->m_intersect.y);
732
733 dc.SetPen(*wxWHITE_PEN);
734 dc.DrawLine( (long) arcX1, (long) arcY1, (long) arcX2, (long) arcY2 );
735
736 node = node->GetNext();
737 }
738 }
739
740 void wxLineCrossings::ClearCrossings()
741 {
742 wxNode* node = m_crossings.GetFirst();
743 while (node)
744 {
745 wxLineCrossing* crossing = (wxLineCrossing*) node->GetData();
746 delete crossing;
747 node = node->GetNext();
748 }
749 m_crossings.Clear();
750 }
751