]> git.saurik.com Git - wxWidgets.git/blob - contrib/samples/ogl/studio/view.cpp
wxUSE_STL fixes.
[wxWidgets.git] / contrib / samples / ogl / studio / view.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: view.cpp
3 // Purpose: Implements view functionality
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 12/07/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence:
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 // #pragma implementation
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 #include <wx/colordlg.h>
28
29 #if !wxUSE_DOC_VIEW_ARCHITECTURE
30 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in wx_setup.h!
31 #endif
32
33 #include "studio.h"
34 #include "doc.h"
35 #include "view.h"
36 #include "cspalette.h"
37 #include "symbols.h"
38 #include "dialogs.h"
39 #include <wx/ogl/basicp.h>
40 #include <wx/ogl/linesp.h>
41
42 IMPLEMENT_DYNAMIC_CLASS(csDiagramView, wxView)
43
44 BEGIN_EVENT_TABLE(csDiagramView, wxView)
45 EVT_MENU(wxID_CUT, csDiagramView::OnCut)
46 EVT_MENU(wxID_COPY, csDiagramView::OnCopy)
47 EVT_MENU(wxID_CLEAR, csDiagramView::OnClear)
48 EVT_MENU(wxID_PASTE, csDiagramView::OnPaste)
49 EVT_MENU(wxID_DUPLICATE, csDiagramView::OnDuplicate)
50 EVT_MENU(ID_CS_CHANGE_BACKGROUND_COLOUR, csDiagramView::OnChangeBackgroundColour)
51 EVT_MENU(ID_CS_EDIT_PROPERTIES, csDiagramView::OnEditProperties)
52 EVT_MENU(ID_CS_SELECT_ALL, csDiagramView::OnSelectAll)
53 EVT_TOOL(DIAGRAM_TOOLBAR_LINE_ARROW, csDiagramView::OnToggleArrowTool)
54 EVT_COMBOBOX(ID_WINDOW_POINT_SIZE_COMBOBOX, csDiagramView::OnPointSizeComboSel)
55 EVT_COMBOBOX(ID_WINDOW_ZOOM_COMBOBOX, csDiagramView::OnZoomSel)
56 EVT_TEXT(ID_WINDOW_POINT_SIZE_COMBOBOX, csDiagramView::OnPointSizeComboText)
57 EVT_TOOL(DIAGRAM_TOOLBAR_ALIGNL, csDiagramView::OnAlign)
58 EVT_TOOL(DIAGRAM_TOOLBAR_ALIGNR, csDiagramView::OnAlign)
59 EVT_TOOL(DIAGRAM_TOOLBAR_ALIGNB, csDiagramView::OnAlign)
60 EVT_TOOL(DIAGRAM_TOOLBAR_ALIGNT, csDiagramView::OnAlign)
61 EVT_TOOL(DIAGRAM_TOOLBAR_ALIGN_HORIZ, csDiagramView::OnAlign)
62 EVT_TOOL(DIAGRAM_TOOLBAR_ALIGN_VERT, csDiagramView::OnAlign)
63 EVT_TOOL(DIAGRAM_TOOLBAR_COPY_SIZE, csDiagramView::OnAlign)
64 EVT_TOOL(DIAGRAM_TOOLBAR_NEW_POINT, csDiagramView::OnNewLinePoint)
65 EVT_TOOL(DIAGRAM_TOOLBAR_CUT_POINT, csDiagramView::OnCutLinePoint)
66 EVT_TOOL(DIAGRAM_TOOLBAR_STRAIGHTEN, csDiagramView::OnStraightenLines)
67 EVT_UPDATE_UI(DIAGRAM_TOOLBAR_ALIGNL, csDiagramView::OnAlignUpdate)
68 EVT_UPDATE_UI(DIAGRAM_TOOLBAR_ALIGNR, csDiagramView::OnAlignUpdate)
69 EVT_UPDATE_UI(DIAGRAM_TOOLBAR_ALIGNB, csDiagramView::OnAlignUpdate)
70 EVT_UPDATE_UI(DIAGRAM_TOOLBAR_ALIGNT, csDiagramView::OnAlignUpdate)
71 EVT_UPDATE_UI(DIAGRAM_TOOLBAR_ALIGN_HORIZ, csDiagramView::OnAlignUpdate)
72 EVT_UPDATE_UI(DIAGRAM_TOOLBAR_ALIGN_VERT, csDiagramView::OnAlignUpdate)
73 EVT_UPDATE_UI(DIAGRAM_TOOLBAR_COPY_SIZE, csDiagramView::OnAlignUpdate)
74 EVT_UPDATE_UI(DIAGRAM_TOOLBAR_NEW_POINT, csDiagramView::OnNewLinePointUpdate)
75 EVT_UPDATE_UI(DIAGRAM_TOOLBAR_CUT_POINT, csDiagramView::OnCutLinePointUpdate)
76 EVT_UPDATE_UI(DIAGRAM_TOOLBAR_STRAIGHTEN, csDiagramView::OnStraightenLinesUpdate)
77 EVT_UPDATE_UI(DIAGRAM_TOOLBAR_LINE_ARROW, csDiagramView::OnToggleArrowToolUpdate)
78 EVT_UPDATE_UI(wxID_CUT, csDiagramView::OnCutUpdate)
79 EVT_UPDATE_UI(wxID_COPY, csDiagramView::OnCopyUpdate)
80 EVT_UPDATE_UI(wxID_CLEAR, csDiagramView::OnClearUpdate)
81 EVT_UPDATE_UI(wxID_PASTE, csDiagramView::OnPasteUpdate)
82 EVT_UPDATE_UI(wxID_DUPLICATE, csDiagramView::OnDuplicateUpdate)
83 EVT_UPDATE_UI(ID_CS_EDIT_PROPERTIES, csDiagramView::OnEditPropertiesUpdate)
84 EVT_UPDATE_UI(wxID_UNDO, csDiagramView::OnUndoUpdate)
85 EVT_UPDATE_UI(wxID_REDO, csDiagramView::OnRedoUpdate)
86 END_EVENT_TABLE()
87
88 // What to do when a view is created. Creates actual
89 // windows for displaying the view.
90 bool csDiagramView::OnCreate(wxDocument *doc, long WXUNUSED(flags))
91 {
92 wxMenu* editMenu;
93 frame = wxGetApp().CreateChildFrame(doc, this, &editMenu);
94 canvas = wxGetApp().CreateCanvas(this, frame);
95 canvas->SetView(this);
96
97 SetFrame(frame);
98 Activate(true);
99
100 // Initialize the edit menu Undo and Redo items
101 doc->GetCommandProcessor()->SetEditMenu(editMenu);
102 doc->GetCommandProcessor()->Initialize();
103
104 wxShapeCanvas *shapeCanvas = (wxShapeCanvas *)canvas;
105 csDiagramDocument *diagramDoc = (csDiagramDocument *)doc;
106 shapeCanvas->SetDiagram(diagramDoc->GetDiagram());
107 diagramDoc->GetDiagram()->SetCanvas(shapeCanvas);
108
109 diagramDoc->GetDiagram()->SetGridSpacing((double) wxGetApp().GetGridSpacing());
110
111 switch (wxGetApp().GetGridStyle())
112 {
113 case csGRID_STYLE_NONE:
114 {
115 diagramDoc->GetDiagram()->SetSnapToGrid(false);
116 break;
117 }
118 case csGRID_STYLE_INVISIBLE:
119 {
120 diagramDoc->GetDiagram()->SetSnapToGrid(true);
121 break;
122 }
123 case csGRID_STYLE_DOTTED:
124 {
125 // TODO (not implemented in OGL)
126 break;
127 }
128 }
129
130
131 return true;
132 }
133
134 csDiagramView::~csDiagramView(void)
135 {
136 if (frame)
137 {
138 ((wxDocMDIChildFrame*)frame)->SetView(NULL);
139 }
140 }
141
142 // Sneakily gets used for default print/preview
143 // as well as drawing on the screen.
144 void csDiagramView::OnDraw(wxDC *WXUNUSED(dc))
145 {
146 }
147
148 void csDiagramView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint))
149 {
150 if (canvas)
151 canvas->Refresh();
152 }
153
154 // Clean up windows used for displaying the view.
155 bool csDiagramView::OnClose(bool deleteWindow)
156 {
157 if (!GetDocument()->Close())
158 return false;
159
160 csDiagramDocument *diagramDoc = (csDiagramDocument *)GetDocument();
161 diagramDoc->GetDiagram()->SetCanvas(NULL);
162
163 canvas->ClearBackground();
164 canvas->SetDiagram(NULL);
165 canvas->SetView(NULL);
166 canvas = NULL;
167
168 wxMenu* fileMenu = frame->GetMenuBar()->GetMenu(0);
169
170 // Remove file menu from those managed by the command history
171 wxGetApp().GetDocManager()->FileHistoryRemoveMenu(fileMenu);
172
173 Activate(false);
174 frame->Show(false);
175
176 if (deleteWindow)
177 {
178 frame->Destroy();
179 }
180
181 return true;
182 }
183
184 // Adds or removes shape from m_selections
185 void csDiagramView::SelectShape(wxShape* shape, bool select)
186 {
187 if (select && !m_selections.Member(shape))
188 m_selections.Append(shape);
189 else if (!select)
190 m_selections.DeleteObject(shape);
191 }
192
193 void csDiagramView::OnSelectAll(wxCommandEvent& WXUNUSED(event))
194 {
195 SelectAll(true);
196 }
197
198 wxShape *csDiagramView::FindFirstSelectedShape(void)
199 {
200 csDiagramDocument *doc = (csDiagramDocument *)GetDocument();
201 wxObjectList::compatibility_iterator node = doc->GetDiagram()->GetShapeList()->GetFirst();
202 while (node)
203 {
204 wxShape *eachShape = (wxShape *)node->GetData();
205 if ((eachShape->GetParent() == NULL) && !eachShape->IsKindOf(CLASSINFO(wxLabelShape)) && eachShape->Selected())
206 {
207 return eachShape;
208 }
209 else node = node->GetNext();
210 }
211 return NULL;
212 }
213
214 void csDiagramView::FindSelectedShapes(wxList& selections, wxClassInfo* toFind)
215 {
216 csDiagramDocument *doc = (csDiagramDocument *)GetDocument();
217 wxObjectList::compatibility_iterator node = doc->GetDiagram()->GetShapeList()->GetFirst();
218 while (node)
219 {
220 wxShape *eachShape = (wxShape *)node->GetData();
221 if ((eachShape->GetParent() == NULL) && !eachShape->IsKindOf(CLASSINFO(wxLabelShape)) && eachShape->Selected() && ((toFind == NULL) || (eachShape->IsKindOf(toFind))))
222 {
223 selections.Append(eachShape);
224 }
225 node = node->GetNext();
226 }
227 }
228
229 void csDiagramView::OnUndoUpdate(wxUpdateUIEvent& event)
230 {
231 csDiagramDocument *doc = (csDiagramDocument *)GetDocument();
232 event.Enable(doc->GetCommandProcessor()->CanUndo());
233 }
234
235 void csDiagramView::OnRedoUpdate(wxUpdateUIEvent& event)
236 {
237 csDiagramDocument *doc = (csDiagramDocument *)GetDocument();
238 event.Enable(doc->GetCommandProcessor()->CanRedo());
239 }
240
241 void csDiagramView::OnCut(wxCommandEvent& WXUNUSED(event))
242 {
243 csDiagramDocument *doc = (csDiagramDocument *)GetDocument();
244
245 // Copy the shapes to the clipboard
246 wxGetApp().GetDiagramClipboard().Copy(doc->GetDiagram());
247
248 wxList selections;
249 FindSelectedShapes(selections);
250
251 DoCut(selections);
252 }
253
254 void csDiagramView::OnClear(wxCommandEvent& WXUNUSED(event))
255 {
256 wxList selections;
257 FindSelectedShapes(selections);
258
259 DoCut(selections);
260 }
261
262 void csDiagramView::OnCopy(wxCommandEvent& WXUNUSED(event))
263 {
264 csDiagramDocument *doc = (csDiagramDocument *)GetDocument();
265
266 // Copy the shapes to the clipboard
267 if (wxGetApp().GetDiagramClipboard().Copy(doc->GetDiagram()))
268 {
269 #ifdef __WXMSW__
270 // Copy to the Windows clipboard
271 wxGetApp().GetDiagramClipboard().CopyToClipboard(1.0);
272 #endif
273 }
274 }
275
276 void csDiagramView::OnPaste(wxCommandEvent& WXUNUSED(event))
277 {
278 csDiagramDocument *doc = (csDiagramDocument *)GetDocument();
279
280 wxGetApp().GetDiagramClipboard().Paste(doc->GetDiagram());
281 }
282
283 void csDiagramView::OnDuplicate(wxCommandEvent& WXUNUSED(event))
284 {
285 csDiagramDocument *doc = (csDiagramDocument *)GetDocument();
286
287 // Do a copy, then a paste
288 wxGetApp().GetDiagramClipboard().Copy(doc->GetDiagram());
289
290 // Apply an offset. Really, this offset should keep being incremented,
291 // but where do we reset it again?
292 wxGetApp().GetDiagramClipboard().Paste(doc->GetDiagram(), NULL, 20, 20);
293 }
294
295 void csDiagramView::OnCutUpdate(wxUpdateUIEvent& event)
296 {
297 event.Enable( (m_selections.GetCount() > 0) );
298 }
299
300 void csDiagramView::OnClearUpdate(wxUpdateUIEvent& event)
301 {
302 event.Enable( (m_selections.GetCount() > 0) );
303 }
304
305 void csDiagramView::OnCopyUpdate(wxUpdateUIEvent& event)
306 {
307 event.Enable( (m_selections.GetCount() > 0) );
308 }
309
310 void csDiagramView::OnPasteUpdate(wxUpdateUIEvent& event)
311 {
312 /* csDiagramDocument *doc = */ (csDiagramDocument *)GetDocument();
313
314 int n = wxGetApp().GetDiagramClipboard().GetCount();
315
316 event.Enable( (n > 0) );
317 }
318
319 void csDiagramView::OnDuplicateUpdate(wxUpdateUIEvent& event)
320 {
321 event.Enable( (m_selections.GetCount() > 0) );
322 }
323
324 void csDiagramView::DoCut(wxList& shapes)
325 {
326 csDiagramDocument *doc = (csDiagramDocument *)GetDocument();
327
328 if (shapes.GetCount() > 0)
329 {
330 csDiagramCommand* cmd = new csDiagramCommand(_T("Cut"), doc);
331
332 wxObjectList::compatibility_iterator node = shapes.GetFirst();
333 while (node)
334 {
335 wxShape *theShape = (wxShape*) node->GetData();
336 csCommandState* state = new csCommandState(ID_CS_CUT, NULL, theShape);
337
338 // Insert lines at the front, so they are cut first.
339 // Otherwise we may try to remove a shape with a line still
340 // attached.
341 if (theShape->IsKindOf(CLASSINFO(wxLineShape)))
342 cmd->InsertState(state);
343 else
344 cmd->AddState(state);
345
346 node = node->GetNext();
347 }
348 cmd->RemoveLines(); // Schedule any connected lines, not already mentioned,
349 // to be removed first
350
351 doc->GetCommandProcessor()->Submit(cmd);
352 }
353 }
354
355 // Generalised command
356 void csDiagramView::DoCmd(wxList& shapes, wxList& oldShapes, int cmd, const wxString& op)
357 {
358 csDiagramDocument *doc = (csDiagramDocument *)GetDocument();
359
360 if (shapes.GetCount() > 0)
361 {
362 csDiagramCommand* command = new csDiagramCommand(op, doc);
363
364 wxObjectList::compatibility_iterator node = shapes.GetFirst();
365 wxObjectList::compatibility_iterator node1 = oldShapes.GetFirst();
366 while (node && node1)
367 {
368 wxShape *theShape = (wxShape*) node->GetData();
369 wxShape *oldShape = (wxShape*) node1->GetData();
370 csCommandState* state = new csCommandState(cmd, theShape, oldShape);
371 command->AddState(state);
372
373 node = node->GetNext();
374 node1 = node1->GetNext();
375 }
376 doc->GetCommandProcessor()->Submit(command);
377 }
378 }
379
380 void csDiagramView::OnChangeBackgroundColour(wxCommandEvent& WXUNUSED(event))
381 {
382 csDiagramDocument *doc = (csDiagramDocument *)GetDocument();
383
384 wxList selections;
385 FindSelectedShapes(selections);
386
387 if (selections.GetCount() > 0)
388 {
389 wxColourData data;
390 data.SetChooseFull(true);
391 if (selections.GetCount() == 1)
392 {
393 wxShape* firstShape = (wxShape*) selections.GetFirst()->GetData();
394 data.SetColour(firstShape->GetBrush()->GetColour());
395 }
396
397 wxColourDialog *dialog = new wxColourDialog(frame, &data);
398 wxBrush *theBrush = NULL;
399 if (dialog->ShowModal() == wxID_OK)
400 {
401 wxColourData retData = dialog->GetColourData();
402 wxColour col = retData.GetColour();
403 theBrush = wxTheBrushList->FindOrCreateBrush(col, wxSOLID);
404 }
405 dialog->Close(true);
406 if (!theBrush)
407 return;
408
409 csDiagramCommand* cmd = new csDiagramCommand(_T("Change colour"), doc);
410
411 wxObjectList::compatibility_iterator node = selections.GetFirst();
412 while (node)
413 {
414 wxShape *theShape = (wxShape*) node->GetData();
415 wxShape* newShape = theShape->CreateNewCopy();
416 newShape->SetBrush(theBrush);
417
418 csCommandState* state = new csCommandState(ID_CS_CHANGE_BACKGROUND_COLOUR, newShape, theShape);
419 cmd->AddState(state);
420
421 node = node->GetNext();
422 }
423 doc->GetCommandProcessor()->Submit(cmd);
424 }
425 }
426
427 void csDiagramView::OnEditProperties(wxCommandEvent& WXUNUSED(event))
428 {
429 wxShape *theShape = FindFirstSelectedShape();
430 if (theShape)
431 ((csEvtHandler *)theShape->GetEventHandler())->EditProperties();
432 }
433
434 void csDiagramView::OnEditPropertiesUpdate(wxUpdateUIEvent& event)
435 {
436 wxList selections;
437 FindSelectedShapes(selections);
438 event.Enable( (selections.GetCount() > 0) );
439 }
440
441 void csDiagramView::OnPointSizeComboSel(wxCommandEvent& event)
442 {
443 wxComboBox* combo = (wxComboBox*) event.GetEventObject();
444 wxASSERT( combo != NULL );
445
446 int newPointSize = (combo->GetSelection() + 1);
447
448 ApplyPointSize(newPointSize);
449
450 }
451
452 // TODO: must find out how to intercept the Return key, rather than
453 // every key stroke. But for now, do every key stroke.
454 void csDiagramView::OnPointSizeComboText(wxCommandEvent& event)
455 {
456 wxComboBox* combo = (wxComboBox*) event.GetEventObject();
457 wxASSERT( combo != NULL );
458
459 wxString str(combo->GetValue());
460 long newPointSize;
461 str.ToLong( &newPointSize );
462
463 if (newPointSize < 2)
464 return;
465
466 ApplyPointSize(newPointSize);
467 }
468
469 void csDiagramView::ApplyPointSize(int pointSize)
470 {
471 csDiagramDocument *doc = (csDiagramDocument *)GetDocument();
472
473 wxList selections;
474 FindSelectedShapes(selections);
475
476 if (selections.GetCount() > 0)
477 {
478 csDiagramCommand* cmd = new csDiagramCommand(_T("Point size"), doc);
479
480 wxObjectList::compatibility_iterator node = selections.GetFirst();
481 while (node)
482 {
483 wxShape *theShape = (wxShape*) node->GetData();
484 wxShape *newShape = theShape->CreateNewCopy();
485
486 wxFont* newFont = wxTheFontList->FindOrCreateFont(pointSize,
487 theShape->GetFont()->GetFamily(),
488 theShape->GetFont()->GetStyle(),
489 theShape->GetFont()->GetWeight(),
490 theShape->GetFont()->GetUnderlined(),
491 theShape->GetFont()->GetFaceName());
492
493 newShape->SetFont(newFont);
494
495 csCommandState* state = new csCommandState(ID_CS_FONT_CHANGE, newShape, theShape);
496
497 cmd->AddState(state);
498
499 node = node->GetNext();
500 }
501 doc->GetCommandProcessor()->Submit(cmd);
502 }
503 }
504
505 void csDiagramView::OnZoomSel(wxCommandEvent& event)
506 {
507 int maxZoom = 200;
508 int minZoom = 5;
509 int inc = 5;
510 int noStrings = (maxZoom - minZoom)/inc ;
511
512 wxComboBox* combo = (wxComboBox*) event.GetEventObject();
513 wxASSERT( combo != NULL );
514
515 int scale = (int) ((noStrings - combo->GetSelection() - 1)*inc + minZoom);
516
517 canvas->SetScale((double) (scale/100.0), (double) (scale/100.0));
518 canvas->Refresh();
519 }
520
521 // Select or deselect all
522 void csDiagramView::SelectAll(bool select)
523 {
524 wxClientDC dc(canvas);
525 canvas->PrepareDC(dc);
526
527 if (!select)
528 {
529 wxList selections;
530 FindSelectedShapes(selections);
531
532 wxObjectList::compatibility_iterator node = selections.GetFirst();
533 while (node)
534 {
535 wxShape *theShape = (wxShape*) node->GetData();
536 theShape->Select(false, &dc);
537 SelectShape(theShape, false);
538
539 node = node->GetNext();
540 }
541 }
542 else
543 {
544 csDiagramDocument *doc = (csDiagramDocument *)GetDocument();
545 wxObjectList::compatibility_iterator node = doc->GetDiagram()->GetShapeList()->GetFirst();
546 while (node)
547 {
548 wxShape *eachShape = (wxShape *)node->GetData();
549 if (eachShape->GetParent() == NULL &&
550 !eachShape->IsKindOf(CLASSINFO(wxControlPoint)) &&
551 !eachShape->IsKindOf(CLASSINFO(wxLabelShape)))
552 {
553 eachShape->Select(true, &dc);
554 SelectShape(eachShape, true);
555 }
556 node = node->GetNext();
557 }
558 }
559 }
560
561
562 void csDiagramView::OnToggleArrowTool(wxCommandEvent& WXUNUSED(event))
563 {
564 csDiagramDocument *doc = (csDiagramDocument *)GetDocument();
565
566 bool state = wxGetApp().GetDiagramToolBar()->GetToolState(DIAGRAM_TOOLBAR_LINE_ARROW);
567 wxString stateName;
568 if (state)
569 stateName = _T("Arrow on");
570 else
571 stateName = _T("Arrow off");
572
573 wxList selections;
574 FindSelectedShapes(selections, CLASSINFO(wxLineShape));
575
576 if (selections.GetCount() > 0)
577 {
578 csDiagramCommand* cmd = new csDiagramCommand(stateName, doc);
579
580 wxObjectList::compatibility_iterator node = selections.GetFirst();
581 while (node)
582 {
583 wxLineShape *theShape = (wxLineShape*) node->GetData();
584 wxLineShape *newShape = NULL;
585
586 if (state)
587 {
588 // Add arrow
589 if (theShape->GetArrows().GetCount() == 0)
590 {
591 newShape = (wxLineShape*) theShape->CreateNewCopy();
592 newShape->AddArrow(ARROW_ARROW, ARROW_POSITION_MIDDLE, 10.0, 0.0, _T("Normal arrowhead"));
593 }
594 }
595 else
596 {
597 if (theShape->GetArrows().GetCount() > 0)
598 {
599 newShape = (wxLineShape*) theShape->CreateNewCopy();
600 newShape->ClearArrowsAtPosition();
601 }
602 }
603
604 // If the new state is the same as the old, don't bother adding it to the command state.
605 if (newShape)
606 {
607 csCommandState* state = new csCommandState(ID_CS_ARROW_CHANGE, newShape, theShape);
608 cmd->AddState(state);
609 }
610
611 node = node->GetNext();
612 }
613 doc->GetCommandProcessor()->Submit(cmd);
614 }
615 }
616
617 void csDiagramView::OnToggleArrowToolUpdate(wxUpdateUIEvent& event)
618 {
619 wxList selections;
620 FindSelectedShapes(selections, CLASSINFO(wxLineShape));
621 event.Enable( (selections.GetCount() > 0) );
622 }
623
624 // Make the point size combobox reflect this
625 void csDiagramView::ReflectPointSize(int pointSize)
626 {
627 wxComboBox* comboBox = wxGetApp().GetPointSizeComboBox();
628 comboBox->SetSelection(pointSize -1);
629 }
630
631 // Make the arrow toggle button reflect the state of the line
632 void csDiagramView::ReflectArrowState(wxLineShape* lineShape)
633 {
634 bool haveArrow = false;
635 wxObjectList::compatibility_iterator node = lineShape->GetArrows().GetFirst();
636 while (node)
637 {
638 wxArrowHead *arrow = (wxArrowHead *)node->GetData();
639 if (ARROW_POSITION_MIDDLE == arrow->GetArrowEnd())
640 haveArrow = true;
641 node = node->GetNext();
642 }
643
644 wxGetApp().GetDiagramToolBar()->ToggleTool(DIAGRAM_TOOLBAR_LINE_ARROW, haveArrow);
645 }
646
647 void csDiagramView::OnAlign(wxCommandEvent& event)
648 {
649 // Make a copy of the selections, keeping only those shapes
650 // that are top-level non-line shapes.
651 wxList selections;
652 wxObjectList::compatibility_iterator node = GetSelectionList().GetFirst();
653 while (node)
654 {
655 wxShape* shape = (wxShape*) node->GetData();
656 if ((shape->GetParent() == NULL) && (!shape->IsKindOf(CLASSINFO(wxLineShape))))
657 {
658 selections.Append(shape);
659 }
660 node = node->GetNext();
661 }
662
663 if (selections.GetCount() == 0)
664 return;
665
666 csDiagramDocument *doc = (csDiagramDocument *)GetDocument();
667 csDiagramCommand* cmd = new csDiagramCommand(_T("Align"), doc);
668
669 node = selections.GetFirst();
670 wxShape* firstShape = (wxShape*) node->GetData();
671
672 double x = firstShape->GetX();
673 double y = firstShape->GetY();
674 double width, height;
675 firstShape->GetBoundingBoxMax(&width, &height);
676
677 node = selections.GetFirst();
678 while (node)
679 {
680 wxShape* shape = (wxShape*) node->GetData();
681 if (shape != firstShape)
682 {
683 /* double x1 = */ shape->GetX();
684 /* double y1 = */ shape->GetY();
685 double width1, height1;
686 shape->GetBoundingBoxMax(& width1, & height1);
687
688 wxShape* newShape = shape->CreateNewCopy();
689
690 switch (event.GetId())
691 {
692 case DIAGRAM_TOOLBAR_ALIGNL:
693 {
694 double x2 = (double)(x - (width/2.0) + (width1/2.0));
695 newShape->SetX(x2);
696 break;
697 }
698 case DIAGRAM_TOOLBAR_ALIGNR:
699 {
700 double x2 = (double)(x + (width/2.0) - (width1/2.0));
701 newShape->SetX(x2);
702 break;
703 }
704 case DIAGRAM_TOOLBAR_ALIGNB:
705 {
706 double y2 = (double)(y + (height/2.0) - (height1/2.0));
707 newShape->SetY(y2);
708 break;
709 }
710 case DIAGRAM_TOOLBAR_ALIGNT:
711 {
712 double y2 = (double)(y - (height/2.0) + (height1/2.0));
713 newShape->SetY(y2);
714 break;
715 }
716 case DIAGRAM_TOOLBAR_ALIGN_HORIZ:
717 {
718 newShape->SetX(x);
719 break;
720 }
721 case DIAGRAM_TOOLBAR_ALIGN_VERT:
722 {
723 newShape->SetY(y);
724 break;
725 }
726 case DIAGRAM_TOOLBAR_COPY_SIZE:
727 {
728 newShape->SetSize(width, height);
729 break;
730 }
731 }
732 csCommandState* state = new csCommandState(ID_CS_ALIGN, newShape, shape);
733 cmd->AddState(state);
734 }
735 node = node->GetNext();
736 }
737 doc->GetCommandProcessor()->Submit(cmd);
738 }
739
740 void csDiagramView::OnAlignUpdate(wxUpdateUIEvent& event)
741 {
742 // This is an approximation, since there may be lines
743 // amongst the selections.
744 event.Enable( (m_selections.GetCount() > 1) ) ;
745 }
746
747 void csDiagramView::OnNewLinePoint(wxCommandEvent& WXUNUSED(event))
748 {
749 csDiagramDocument *doc = (csDiagramDocument *)GetDocument();
750 csDiagramCommand* cmd = new csDiagramCommand(_T("New line point"), doc);
751
752 wxObjectList::compatibility_iterator node = m_selections.GetFirst();
753 while (node)
754 {
755 wxShape* shape = (wxShape*) node->GetData();
756 if (shape->IsKindOf(CLASSINFO(wxLineShape)))
757 {
758 wxShape* newShape = shape->CreateNewCopy();
759 ((wxLineShape*)newShape)->InsertLineControlPoint(NULL);
760 csCommandState* state = new csCommandState(ID_CS_NEW_POINT, newShape, shape);
761 cmd->AddState(state);
762 }
763 node = node->GetNext();
764 }
765 doc->GetCommandProcessor()->Submit(cmd);
766 }
767
768 void csDiagramView::OnCutLinePoint(wxCommandEvent& WXUNUSED(event))
769 {
770 csDiagramDocument *doc = (csDiagramDocument *)GetDocument();
771 csDiagramCommand* cmd = new csDiagramCommand(_T("Cut line point"), doc);
772
773 wxObjectList::compatibility_iterator node = m_selections.GetFirst();
774 while (node)
775 {
776 wxShape* shape = (wxShape*) node->GetData();
777 if (shape->IsKindOf(CLASSINFO(wxLineShape)))
778 {
779 wxShape* newShape = shape->CreateNewCopy();
780 ((wxLineShape*)newShape)->DeleteLineControlPoint();
781 csCommandState* state = new csCommandState(ID_CS_CUT_POINT, newShape, shape);
782 cmd->AddState(state);
783 }
784 node = node->GetNext();
785 }
786 doc->GetCommandProcessor()->Submit(cmd);
787 }
788
789 void csDiagramView::OnStraightenLines(wxCommandEvent& WXUNUSED(event))
790 {
791 csDiagramDocument *doc = (csDiagramDocument *)GetDocument();
792 csDiagramCommand* cmd = new csDiagramCommand(_T("Straighten lines"), doc);
793
794 wxObjectList::compatibility_iterator node = m_selections.GetFirst();
795 while (node)
796 {
797 wxShape* shape = (wxShape*) node->GetData();
798 if (shape->IsKindOf(CLASSINFO(wxLineShape)))
799 {
800 wxShape* newShape = shape->CreateNewCopy();
801 ((wxLineShape*)newShape)->Straighten();
802 csCommandState* state = new csCommandState(ID_CS_STRAIGHTEN, newShape, shape);
803 cmd->AddState(state);
804 }
805 node = node->GetNext();
806 }
807 doc->GetCommandProcessor()->Submit(cmd);
808 }
809
810 void csDiagramView::OnNewLinePointUpdate(wxUpdateUIEvent& event)
811 {
812 wxList selections;
813 FindSelectedShapes(selections, CLASSINFO(wxLineShape));
814 event.Enable( (selections.GetCount() > 0) );
815 }
816
817 void csDiagramView::OnCutLinePointUpdate(wxUpdateUIEvent& event)
818 {
819 wxList selections;
820 FindSelectedShapes(selections, CLASSINFO(wxLineShape));
821 event.Enable( (selections.GetCount() > 0) );
822 }
823
824 void csDiagramView::OnStraightenLinesUpdate(wxUpdateUIEvent& event)
825 {
826 wxList selections;
827 FindSelectedShapes(selections, CLASSINFO(wxLineShape));
828 event.Enable( (selections.GetCount() > 0) );
829 }
830
831 /*
832 * Window implementations
833 */
834
835 IMPLEMENT_CLASS(csCanvas, wxShapeCanvas)
836
837 BEGIN_EVENT_TABLE(csCanvas, wxShapeCanvas)
838 EVT_MOUSE_EVENTS(csCanvas::OnMouseEvent)
839 EVT_PAINT(csCanvas::OnPaint)
840 END_EVENT_TABLE()
841
842 // Define a constructor for my canvas
843 csCanvas::csCanvas(csDiagramView *v, wxWindow *parent, wxWindowID id, const wxPoint& pos,
844 const wxSize& size, long style):
845 wxShapeCanvas(parent, id, pos, size, style)
846 {
847 m_view = v;
848 }
849
850 csCanvas::~csCanvas(void)
851 {
852 }
853
854 void csCanvas::DrawOutline(wxDC& dc, double x1, double y1, double x2, double y2)
855 {
856 wxPen dottedPen(wxColour(0, 0, 0), 1, wxDOT);
857 dc.SetPen(dottedPen);
858 dc.SetBrush(* wxTRANSPARENT_BRUSH);
859
860 dc.DrawRectangle((long) x1, (long) y1, (long) (x2 - x1), (long) (y2 - y1));
861 }
862
863 void csCanvas::OnLeftClick(double x, double y, int WXUNUSED(keys))
864 {
865 csEditorToolPalette *palette = wxGetApp().GetDiagramPalette();
866
867 if (palette->GetSelection() == PALETTE_ARROW)
868 {
869 GetView()->SelectAll(false);
870
871 wxClientDC dc(this);
872 PrepareDC(dc);
873
874 Redraw(dc);
875 return;
876 }
877
878 if (palette->GetSelection() == PALETTE_TEXT_TOOL)
879 {
880 // Ask for a label and create a new free-floating text region
881 csLabelEditingDialog* dialog = new csLabelEditingDialog(GetParent());
882
883 dialog->SetShapeLabel( wxEmptyString );
884 dialog->SetTitle(_T("New text box"));
885 if (dialog->ShowModal() == wxID_CANCEL)
886 {
887 dialog->Destroy();
888 return;
889 }
890
891 wxString newLabel = dialog->GetShapeLabel();
892 dialog->Destroy();
893
894 wxShape* shape = new csTextBoxShape;
895 shape->AssignNewIds();
896 shape->SetEventHandler(new csEvtHandler(shape, shape, newLabel));
897
898 wxComboBox* comboBox = wxGetApp().GetPointSizeComboBox();
899 wxString str(comboBox->GetValue());
900 long pointSize;
901 str.ToLong( &pointSize );
902
903 wxFont* newFont = wxTheFontList->FindOrCreateFont(pointSize,
904 shape->GetFont()->GetFamily(),
905 shape->GetFont()->GetStyle(),
906 shape->GetFont()->GetWeight(),
907 shape->GetFont()->GetUnderlined(),
908 shape->GetFont()->GetFaceName());
909
910 shape->SetFont(newFont);
911
912 shape->SetX(x);
913 shape->SetY(y);
914
915 csDiagramCommand* cmd = new csDiagramCommand(_T("Text box"),
916 (csDiagramDocument *)GetView()->GetDocument(),
917 new csCommandState(ID_CS_ADD_SHAPE, shape, NULL));
918 GetView()->GetDocument()->GetCommandProcessor()->Submit(cmd);
919
920 palette->SetSelection(PALETTE_ARROW);
921
922 return;
923 }
924
925 csSymbol* symbol = wxGetApp().GetSymbolDatabase()->FindSymbol(palette->GetSelection());
926 if (symbol)
927 {
928 wxShape* theShape = symbol->GetShape()->CreateNewCopy();
929
930 wxComboBox* comboBox = wxGetApp().GetPointSizeComboBox();
931 wxString str(comboBox->GetValue());
932 long pointSize;
933 str.ToLong( &pointSize );
934
935 wxFont* newFont = wxTheFontList->FindOrCreateFont(pointSize,
936 symbol->GetShape()->GetFont()->GetFamily(),
937 symbol->GetShape()->GetFont()->GetStyle(),
938 symbol->GetShape()->GetFont()->GetWeight(),
939 symbol->GetShape()->GetFont()->GetUnderlined(),
940 symbol->GetShape()->GetFont()->GetFaceName());
941
942 theShape->SetFont(newFont);
943
944 theShape->AssignNewIds();
945 theShape->SetX(x);
946 theShape->SetY(y);
947
948 csDiagramCommand* cmd = new csDiagramCommand(symbol->GetName(),
949 (csDiagramDocument *)GetView()->GetDocument(),
950 new csCommandState(ID_CS_ADD_SHAPE, theShape, NULL));
951 GetView()->GetDocument()->GetCommandProcessor()->Submit(cmd);
952
953 palette->SetSelection(PALETTE_ARROW);
954 }
955 }
956
957 void csCanvas::OnRightClick(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
958 {
959 }
960
961 // Initial point
962 static double sg_initialX, sg_initialY;
963
964 void csCanvas::OnDragLeft(bool WXUNUSED(draw), double x, double y, int WXUNUSED(keys))
965 {
966 wxClientDC dc(this);
967 PrepareDC(dc);
968
969 dc.SetLogicalFunction(OGLRBLF);
970 DrawOutline(dc, sg_initialX, sg_initialY, x, y);
971 }
972
973 void csCanvas::OnBeginDragLeft(double x, double y, int WXUNUSED(keys))
974 {
975 sg_initialX = x;
976 sg_initialY = y;
977
978 wxClientDC dc(this);
979 PrepareDC(dc);
980
981 dc.SetLogicalFunction(OGLRBLF);
982 DrawOutline(dc, sg_initialX, sg_initialY, x, y);
983 CaptureMouse();
984 }
985
986 void csCanvas::OnEndDragLeft(double x, double y, int WXUNUSED(keys))
987 {
988 ReleaseMouse();
989
990 wxClientDC dc(this);
991 PrepareDC(dc);
992
993 // Select all images within the rectangle
994 float min_x, max_x, min_y, max_y;
995 min_x = wxMin(x, sg_initialX);
996 max_x = wxMax(x, sg_initialX);
997 min_y = wxMin(y, sg_initialY);
998 max_y = wxMax(y, sg_initialY);
999
1000 wxObjectList::compatibility_iterator node = GetDiagram()->GetShapeList()->GetFirst();
1001 while (node)
1002 {
1003 wxShape *shape = (wxShape *)node->GetData();
1004 if (shape->GetParent() == NULL && !shape->IsKindOf(CLASSINFO(wxControlPoint)))
1005 {
1006 float image_x = shape->GetX();
1007 float image_y = shape->GetY();
1008 if (image_x >= min_x && image_x <= max_x &&
1009 image_y >= min_y && image_y <= max_y)
1010 {
1011 shape->Select(true, &dc);
1012 GetView()->SelectShape(shape, true);
1013 }
1014 }
1015 node = node->GetNext();
1016 }
1017 }
1018
1019 void csCanvas::OnDragRight(bool WXUNUSED(draw), double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1020 {
1021 }
1022
1023 void csCanvas::OnBeginDragRight(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1024 {
1025 }
1026
1027 void csCanvas::OnEndDragRight(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1028 {
1029 }
1030
1031 void csCanvas::OnMouseEvent(wxMouseEvent& event)
1032 {
1033 wxShapeCanvas::OnMouseEvent(event);
1034 }
1035
1036 void csCanvas::OnPaint(wxPaintEvent& event)
1037 {
1038 // if (GetDiagram())
1039 wxShapeCanvas::OnPaint(event);
1040 }