]> git.saurik.com Git - wxWidgets.git/blob - utils/ogl/src/divided.cpp
Rewrote wxRadioBox (recompile)
[wxWidgets.git] / utils / ogl / src / divided.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: divided.cpp
3 // Purpose: wxDividedShape class
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 "divided.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 #ifdef PROLOGIO
28 #include <wx/wxexpr.h>
29 #endif
30
31 #include "basic.h"
32 #include "basicp.h"
33 #include "canvas.h"
34 #include "divided.h"
35 #include "lines.h"
36 #include "misc.h"
37
38 class wxDividedShapeControlPoint: public wxControlPoint
39 {
40 DECLARE_DYNAMIC_CLASS(wxDividedShapeControlPoint)
41 private:
42 int regionId;
43 public:
44 wxDividedShapeControlPoint() { regionId = 0; }
45 wxDividedShapeControlPoint(wxShapeCanvas *the_canvas, wxShape *object, int region,
46 double size, double the_xoffset, double the_yoffset, int the_type);
47 ~wxDividedShapeControlPoint();
48
49 void OnDragLeft(bool draw, double x, double y, int keys=0, int attachment = 0);
50 void OnBeginDragLeft(double x, double y, int keys=0, int attachment = 0);
51 void OnEndDragLeft(double x, double y, int keys=0, int attachment = 0);
52 };
53
54 IMPLEMENT_DYNAMIC_CLASS(wxDividedShapeControlPoint, wxControlPoint)
55
56 /*
57 * Divided object
58 *
59 */
60
61 IMPLEMENT_DYNAMIC_CLASS(wxDividedShape, wxRectangleShape)
62
63 wxDividedShape::wxDividedShape(double w, double h): wxRectangleShape(w, h)
64 {
65 ClearRegions();
66 }
67
68 wxDividedShape::~wxDividedShape()
69 {
70 }
71
72 void wxDividedShape::OnDraw(wxDC& dc)
73 {
74 wxRectangleShape::OnDraw(dc);
75 }
76
77 void wxDividedShape::OnDrawContents(wxDC& dc)
78 {
79 double defaultProportion = (double)(GetRegions().Number() > 0 ? (1.0/((double)(GetRegions().Number()))) : 0.0);
80 double currentY = (double)(m_ypos - (m_height / 2.0));
81 double maxY = (double)(m_ypos + (m_height / 2.0));
82
83 double leftX = (double)(m_xpos - (m_width / 2.0));
84 double rightX = (double)(m_xpos + (m_width / 2.0));
85
86 if (m_pen) dc.SetPen(m_pen);
87
88 if (m_textColour) dc.SetTextForeground(* m_textColour);
89
90 #ifdef __WXMSW__
91 // For efficiency, don't do this under X - doesn't make
92 // any visible difference for our purposes.
93 if (m_brush)
94 dc.SetTextBackground(m_brush->GetColour());
95 #endif
96 /*
97 if (!formatted)
98 {
99 FormatRegionText();
100 formatted = TRUE;
101 }
102 */
103 if (GetDisableLabel()) return;
104
105 double xMargin = 2;
106 double yMargin = 2;
107 dc.SetBackgroundMode(wxTRANSPARENT);
108
109 wxNode *node = GetRegions().First();
110 while (node)
111 {
112 wxShapeRegion *region = (wxShapeRegion *)node->Data();
113 dc.SetFont(region->GetFont());
114 dc.SetTextForeground(* region->GetActualColourObject());
115
116 double proportion =
117 region->m_regionProportionY < 0.0 ? defaultProportion : region->m_regionProportionY;
118
119 double y = currentY + m_height*proportion;
120 double actualY = maxY < y ? maxY : y;
121
122 double centreX = m_xpos;
123 double centreY = (double)(currentY + (actualY - currentY)/2.0);
124
125 oglDrawFormattedText(dc, &region->m_formattedText,
126 (double)(centreX), (double)(centreY), (double)(m_width-2*xMargin), (double)(actualY - currentY - 2*yMargin),
127 region->m_formatMode);
128 if ((y <= maxY) && (node->Next()))
129 {
130 wxPen *regionPen = region->GetActualPen();
131 if (regionPen)
132 {
133 dc.SetPen(regionPen);
134 dc.DrawLine(WXROUND(leftX), WXROUND(y), WXROUND(rightX), WXROUND(y));
135 }
136 }
137
138 currentY = actualY;
139
140 node = node->Next();
141 }
142 }
143
144 void wxDividedShape::SetSize(double w, double h, bool recursive)
145 {
146 SetAttachmentSize(w, h);
147 m_width = w;
148 m_height = h;
149 SetRegionSizes();
150 }
151
152 void wxDividedShape::SetRegionSizes()
153 {
154 if (GetRegions().Number() == 0)
155 return;
156
157 double defaultProportion = (double)(GetRegions().Number() > 0 ? (1.0/((double)(GetRegions().Number()))) : 0.0);
158 double currentY = (double)(m_ypos - (m_height / 2.0));
159 double maxY = (double)(m_ypos + (m_height / 2.0));
160
161 // double leftX = (double)(m_xpos - (m_width / 2.0));
162 // double rightX = (double)(m_xpos + (m_width / 2.0));
163
164 wxNode *node = GetRegions().First();
165 while (node)
166 {
167 wxShapeRegion *region = (wxShapeRegion *)node->Data();
168 double proportion =
169 region->m_regionProportionY <= 0.0 ? defaultProportion : region->m_regionProportionY;
170
171 double sizeY = (double)proportion*m_height;
172 double y = currentY + sizeY;
173 double actualY = maxY < y ? maxY : y;
174
175 double centreY = (double)(currentY + (actualY - currentY)/2.0);
176
177 region->SetSize(m_width, sizeY);
178 region->SetPosition(0.0, (double)(centreY - m_ypos));
179 currentY = actualY;
180 node = node->Next();
181 }
182 }
183
184 // Attachment points correspond to regions in the divided box
185 bool wxDividedShape::GetAttachmentPosition(int attachment, double *x, double *y, int nth, int no_arcs,
186 wxLineShape *line)
187 {
188 int totalNumberAttachments = (GetRegions().Number() * 2) + 2;
189 if (!GetAttachmentMode() || (attachment >= totalNumberAttachments))
190 {
191 return wxShape::GetAttachmentPosition(attachment, x, y, nth, no_arcs);
192 }
193
194 int n = GetRegions().Number();
195 bool isEnd = (line && line->IsEnd(this));
196
197 double left = (double)(m_xpos - m_width/2.0);
198 double right = (double)(m_xpos + m_width/2.0);
199 double top = (double)(m_ypos - m_height/2.0);
200 double bottom = (double)(m_ypos + m_height/2.0);
201
202 // Zero is top, n+1 is bottom.
203 if (attachment == 0)
204 {
205 *y = top;
206 if (m_spaceAttachments)
207 {
208 if (line && (line->GetAlignmentType(isEnd) == LINE_ALIGNMENT_TO_NEXT_HANDLE))
209 {
210 // Align line according to the next handle along
211 wxRealPoint *point = line->GetNextControlPoint(this);
212 if (point->x < left)
213 *x = left;
214 else if (point->x > right)
215 *x = right;
216 else
217 *x = point->x;
218 }
219 else
220 *x = left + (nth + 1)*m_width/(no_arcs + 1);
221 }
222 else
223 *x = m_xpos;
224 }
225 else if (attachment == (n+1))
226 {
227 *y = bottom;
228 if (m_spaceAttachments)
229 {
230 if (line && (line->GetAlignmentType(isEnd) == LINE_ALIGNMENT_TO_NEXT_HANDLE))
231 {
232 // Align line according to the next handle along
233 wxRealPoint *point = line->GetNextControlPoint(this);
234 if (point->x < left)
235 *x = left;
236 else if (point->x > right)
237 *x = right;
238 else
239 *x = point->x;
240 }
241 else
242 *x = left + (nth + 1)*m_width/(no_arcs + 1);
243 }
244 else
245 *x = m_xpos;
246 }
247 // Left or right.
248 else
249 {
250 int i = 0;
251 bool isLeft = FALSE;
252 if (attachment < (n+1))
253 {
254 i = attachment-1;
255 isLeft = FALSE;
256 }
257 else
258 {
259 i = (totalNumberAttachments - attachment - 1);
260 isLeft = TRUE;
261 }
262 wxNode *node = GetRegions().Nth(i);
263 if (node)
264 {
265 wxShapeRegion *region = (wxShapeRegion *)node->Data();
266
267 if (isLeft)
268 *x = left;
269 else
270 *x = right;
271
272 // Calculate top and bottom of region
273 top = (double)((m_ypos + region->m_y) - (region->m_height/2.0));
274 bottom = (double)((m_ypos + region->m_y) + (region->m_height/2.0));
275
276 // Assuming we can trust the absolute size and
277 // position of these regions...
278 if (m_spaceAttachments)
279 {
280 if (line && (line->GetAlignmentType(isEnd) == LINE_ALIGNMENT_TO_NEXT_HANDLE))
281 {
282 // Align line according to the next handle along
283 wxRealPoint *point = line->GetNextControlPoint(this);
284 if (point->y < bottom)
285 *y = bottom;
286 else if (point->y > top)
287 *y = top;
288 else
289 *y = point->y;
290 }
291 else
292 // *y = (double)(((m_ypos + region->m_y) - (region->m_height/2.0)) + (nth + 1)*region->m_height/(no_arcs+1));
293 *y = (double)(top + (nth + 1)*region->m_height/(no_arcs+1));
294 }
295 else
296 *y = (double)(m_ypos + region->m_y);
297 }
298 else
299 {
300 *x = m_xpos;
301 *y = m_ypos;
302 return FALSE;
303 }
304 }
305 return TRUE;
306 }
307
308 int wxDividedShape::GetNumberOfAttachments()
309 {
310 // There are two attachments for each region (left and right),
311 // plus one on the top and one on the bottom.
312 int n = (GetRegions().Number() * 2) + 2;
313
314 int maxN = n - 1;
315 wxNode *node = m_attachmentPoints.First();
316 while (node)
317 {
318 wxAttachmentPoint *point = (wxAttachmentPoint *)node->Data();
319 if (point->m_id > maxN)
320 maxN = point->m_id;
321 node = node->Next();
322 }
323 return maxN + 1;
324 }
325
326 bool wxDividedShape::AttachmentIsValid(int attachment)
327 {
328 int totalNumberAttachments = (GetRegions().Number() * 2) + 2;
329 if (attachment >= totalNumberAttachments)
330 {
331 return wxShape::AttachmentIsValid(attachment);
332 }
333 else if (attachment >= 0)
334 return TRUE;
335 else
336 return FALSE;
337 }
338
339 void wxDividedShape::Copy(wxShape& copy)
340 {
341 wxRectangleShape::Copy(copy);
342 }
343
344 // Region operations
345
346 void wxDividedShape::MakeControlPoints()
347 {
348 wxRectangleShape::MakeControlPoints();
349
350 MakeMandatoryControlPoints();
351 }
352
353 void wxDividedShape::MakeMandatoryControlPoints()
354 {
355 double currentY = (double)(GetY() - (m_height / 2.0));
356 double maxY = (double)(GetY() + (m_height / 2.0));
357
358 wxNode *node = GetRegions().First();
359 int i = 0;
360 while (node)
361 {
362 wxShapeRegion *region = (wxShapeRegion *)node->Data();
363
364 double proportion = region->m_regionProportionY;
365
366 double y = currentY + m_height*proportion;
367 double actualY = (double)(maxY < y ? maxY : y);
368
369 if (node->Next())
370 {
371 wxDividedShapeControlPoint *controlPoint =
372 new wxDividedShapeControlPoint(m_canvas, this, i, CONTROL_POINT_SIZE, 0.0, (double)(actualY - GetY()), 0);
373 m_canvas->AddShape(controlPoint);
374 m_controlPoints.Append(controlPoint);
375 }
376 currentY = actualY;
377 i ++;
378 node = node->Next();
379 }
380 }
381
382 void wxDividedShape::ResetControlPoints()
383 {
384 // May only have the region handles, (n - 1) of them.
385 if (m_controlPoints.Number() > (GetRegions().Number() - 1))
386 wxRectangleShape::ResetControlPoints();
387
388 ResetMandatoryControlPoints();
389 }
390
391 void wxDividedShape::ResetMandatoryControlPoints()
392 {
393 double currentY = (double)(GetY() - (m_height / 2.0));
394 double maxY = (double)(GetY() + (m_height / 2.0));
395
396 wxNode *node = m_controlPoints.First();
397 int i = 0;
398 while (node)
399 {
400 wxControlPoint *controlPoint = (wxControlPoint *)node->Data();
401 if (controlPoint->IsKindOf(CLASSINFO(wxDividedShapeControlPoint)))
402 {
403 wxNode *node1 = GetRegions().Nth(i);
404 wxShapeRegion *region = (wxShapeRegion *)node1->Data();
405
406 double proportion = region->m_regionProportionY;
407
408 double y = currentY + m_height*proportion;
409 double actualY = (double)(maxY < y ? maxY : y);
410
411 controlPoint->m_xoffset = 0.0;
412 controlPoint->m_yoffset = (double)(actualY - GetY());
413 currentY = actualY;
414 i ++;
415 }
416 node = node->Next();
417 }
418 }
419
420 #ifdef PROLOGIO
421 void wxDividedShape::WritePrologAttributes(wxExpr *clause)
422 {
423 wxRectangleShape::WritePrologAttributes(clause);
424 }
425
426 void wxDividedShape::ReadPrologAttributes(wxExpr *clause)
427 {
428 wxRectangleShape::ReadPrologAttributes(clause);
429 }
430 #endif
431
432 /*
433 * Edit the division colour/style
434 *
435 */
436
437 void wxDividedShape::EditRegions()
438 {
439 wxMessageBox("EditRegions() is unimplemented.", "OGL", wxOK);
440
441 // TODO
442 #if 0
443 if (GetRegions().Number() < 2)
444 return;
445
446 wxBeginBusyCursor();
447
448 GraphicsForm *form = new GraphicsForm("Divided nodes");
449 // Need an array to store all the style strings,
450 // since they need to be converted to integers
451 char **styleStrings = new char *[GetRegions().Number()];
452 for (int j = 0; j < GetRegions().Number(); j++)
453 styleStrings[j] = NULL;
454
455 int i = 0;
456 wxNode *node = GetRegions().First();
457 while (node && node->Next())
458 {
459 wxShapeRegion *region = (wxShapeRegion *)node->Data();
460 char buf[50];
461 sprintf(buf, "Region %d", (i+1));
462 form->Add(wxMakeFormMessage(buf));
463 form->Add(wxMakeFormNewLine());
464
465 form->Add(wxMakeFormString("Colour", &region->penColour, wxFORM_CHOICE,
466 new wxList(wxMakeConstraintStrings(
467 "Invisible" ,
468 "BLACK" ,
469 "BLUE" ,
470 "BROWN" ,
471 "CORAL" ,
472 "CYAN" ,
473 "DARK GREY" ,
474 "DARK GREEN" ,
475 "DIM GREY" ,
476 "GREY" ,
477 "GREEN" ,
478 "LIGHT BLUE" ,
479 "LIGHT GREY" ,
480 "MAGENTA" ,
481 "MAROON" ,
482 "NAVY" ,
483 "ORANGE" ,
484 "PURPLE" ,
485 "RED" ,
486 "TURQUOISE" ,
487 "VIOLET" ,
488 "WHITE" ,
489 "YELLOW" ,
490 NULL),
491 NULL), NULL, wxVERTICAL, 150));
492
493 char *styleString = NULL;
494 switch (region->penStyle)
495 {
496 case wxSHORT_DASH:
497 styleString = "Short Dash";
498 break;
499 case wxLONG_DASH:
500 styleString = "Long Dash";
501 break;
502 case wxDOT:
503 styleString = "Dot";
504 break;
505 case wxDOT_DASH:
506 styleString = "Dot Dash";
507 break;
508 case wxSOLID:
509 default:
510 styleString = "Solid";
511 break;
512 }
513 styleStrings[i] = copystring(styleString);
514 form->Add(wxMakeFormString("Style", &(styleStrings[i]), wxFORM_CHOICE,
515 new wxList(wxMakeConstraintStrings(
516 "Solid" ,
517 "Short Dash" ,
518 "Long Dash" ,
519 "Dot" ,
520 "Dot Dash" ,
521 NULL),
522 NULL), NULL, wxVERTICAL, 100));
523 node = node->Next();
524 i ++;
525 if (node && node->Next())
526 form->Add(wxMakeFormNewLine());
527 }
528 wxDialogBox *dialog = new wxDialogBox(m_canvas->GetParent(), "Divided object properties", 10, 10, 500, 500);
529 if (GraphicsLabelFont)
530 dialog->SetLabelFont(GraphicsLabelFont);
531 if (GraphicsButtonFont)
532 dialog->SetButtonFont(GraphicsButtonFont);
533 form->AssociatePanel(dialog);
534 form->dialog = dialog;
535
536 dialog->Fit();
537 dialog->Centre(wxBOTH);
538
539 wxEndBusyCursor();
540
541 dialog->Show(TRUE);
542
543 node = GetRegions().First();
544 i = 0;
545 while (node)
546 {
547 wxShapeRegion *region = (wxShapeRegion *)node->Data();
548
549 if (styleStrings[i])
550 {
551 if (strcmp(styleStrings[i], "Solid") == 0)
552 region->penStyle = wxSOLID;
553 else if (strcmp(styleStrings[i], "Dot") == 0)
554 region->penStyle = wxDOT;
555 else if (strcmp(styleStrings[i], "Short Dash") == 0)
556 region->penStyle = wxSHORT_DASH;
557 else if (strcmp(styleStrings[i], "Long Dash") == 0)
558 region->penStyle = wxLONG_DASH;
559 else if (strcmp(styleStrings[i], "Dot Dash") == 0)
560 region->penStyle = wxDOT_DASH;
561 delete[] styleStrings[i];
562 }
563 region->m_actualPenObject = NULL;
564 node = node->Next();
565 i ++;
566 }
567 delete[] styleStrings;
568 Draw(dc);
569 #endif
570 }
571
572 void wxDividedShape::OnRightClick(double x, double y, int keys, int attachment)
573 {
574 if (keys & KEY_CTRL)
575 {
576 EditRegions();
577 }
578 else
579 {
580 wxRectangleShape::OnRightClick(x, y, keys, attachment);
581 }
582 }
583
584 wxDividedShapeControlPoint::wxDividedShapeControlPoint(wxShapeCanvas *the_canvas, wxShape *object,
585 int region, double size, double the_m_xoffset, double the_m_yoffset, int the_type):
586 wxControlPoint(the_canvas, object, size, the_m_xoffset, the_m_yoffset, the_type)
587 {
588 regionId = region;
589 }
590
591 wxDividedShapeControlPoint::~wxDividedShapeControlPoint()
592 {
593 }
594
595 // Implement resizing of divided object division
596 void wxDividedShapeControlPoint::OnDragLeft(bool draw, double x, double y, int keys, int attachment)
597 {
598 wxClientDC dc(GetCanvas());
599 GetCanvas()->PrepareDC(dc);
600
601 dc.SetLogicalFunction(wxXOR);
602 wxPen dottedPen(wxColour(0, 0, 0), 1, wxDOT);
603 dc.SetPen(dottedPen);
604 dc.SetBrush((* wxTRANSPARENT_BRUSH));
605
606 wxDividedShape *dividedObject = (wxDividedShape *)m_shape;
607 double x1 = (double)(dividedObject->GetX() - (dividedObject->GetWidth()/2.0));
608 double y1 = y;
609 double x2 = (double)(dividedObject->GetX() + (dividedObject->GetWidth()/2.0));
610 double y2 = y;
611 dc.DrawLine(WXROUND(x1), WXROUND(y1), WXROUND(x2), WXROUND(y2));
612 }
613
614 void wxDividedShapeControlPoint::OnBeginDragLeft(double x, double y, int keys, int attachment)
615 {
616 wxClientDC dc(GetCanvas());
617 GetCanvas()->PrepareDC(dc);
618
619 wxDividedShape *dividedObject = (wxDividedShape *)m_shape;
620 dc.SetLogicalFunction(wxXOR);
621 wxPen dottedPen(wxColour(0, 0, 0), 1, wxDOT);
622 dc.SetPen(dottedPen);
623 dc.SetBrush((* wxTRANSPARENT_BRUSH));
624
625 double x1 = (double)(dividedObject->GetX() - (dividedObject->GetWidth()/2.0));
626 double y1 = y;
627 double x2 = (double)(dividedObject->GetX() + (dividedObject->GetWidth()/2.0));
628 double y2 = y;
629 dc.DrawLine(WXROUND(x1), WXROUND(y1), WXROUND(x2), WXROUND(y2));
630 m_canvas->CaptureMouse();
631 }
632
633 void wxDividedShapeControlPoint::OnEndDragLeft(double x, double y, int keys, int attachment)
634 {
635 wxClientDC dc(GetCanvas());
636 GetCanvas()->PrepareDC(dc);
637
638 wxDividedShape *dividedObject = (wxDividedShape *)m_shape;
639 wxNode *node = dividedObject->GetRegions().Nth(regionId);
640 if (!node)
641 return;
642
643 wxShapeRegion *thisRegion = (wxShapeRegion *)node->Data();
644 wxShapeRegion *nextRegion = NULL; // Region below this one
645
646 dc.SetLogicalFunction(wxCOPY);
647
648 m_canvas->ReleaseMouse();
649
650 // Find the old top and bottom of this region,
651 // and calculate the new proportion for this region
652 // if legal.
653
654 double currentY = (double)(dividedObject->GetY() - (dividedObject->GetHeight() / 2.0));
655 double maxY = (double)(dividedObject->GetY() + (dividedObject->GetHeight() / 2.0));
656
657 // Save values
658 double thisRegionTop = 0.0;
659 double thisRegionBottom = 0.0;
660 double nextRegionBottom = 0.0;
661
662 node = dividedObject->GetRegions().First();
663 while (node)
664 {
665 wxShapeRegion *region = (wxShapeRegion *)node->Data();
666
667 double proportion = region->m_regionProportionY;
668 double yy = currentY + (dividedObject->GetHeight()*proportion);
669 double actualY = (double)(maxY < yy ? maxY : yy);
670
671 if (region == thisRegion)
672 {
673 thisRegionTop = currentY;
674 thisRegionBottom = actualY;
675 if (node->Next())
676 nextRegion = (wxShapeRegion *)node->Next()->Data();
677 }
678 if (region == nextRegion)
679 {
680 nextRegionBottom = actualY;
681 }
682
683 currentY = actualY;
684 node = node->Next();
685 }
686 if (!nextRegion)
687 return;
688
689 // Check that we haven't gone above this region or below
690 // next region.
691 if ((y <= thisRegionTop) || (y >= nextRegionBottom))
692 return;
693
694 dividedObject->EraseLinks(dc);
695
696 // Now calculate the new proportions of this region and the next region.
697 double thisProportion = (double)((y - thisRegionTop)/dividedObject->GetHeight());
698 double nextProportion = (double)((nextRegionBottom - y)/dividedObject->GetHeight());
699 thisRegion->SetProportions(0.0, thisProportion);
700 nextRegion->SetProportions(0.0, nextProportion);
701 m_yoffset = (double)(y - dividedObject->GetY());
702
703 // Now reformat text
704 int i = 0;
705 node = dividedObject->GetRegions().First();
706 while (node)
707 {
708 wxShapeRegion *region = (wxShapeRegion *)node->Data();
709 if (region->GetText())
710 {
711 char *s = copystring(region->GetText());
712 dividedObject->FormatText(dc, s, i);
713 delete[] s;
714 }
715 node = node->Next();
716 i++;
717 }
718 dividedObject->SetRegionSizes();
719 dividedObject->Draw(dc);
720 dividedObject->GetEventHandler()->OnMoveLinks(dc);
721 }
722