]> git.saurik.com Git - wxWidgets.git/blob - utils/dialoged/src/dlghndlr.cpp
Dialog Editor edits
[wxWidgets.git] / utils / dialoged / src / dlghndlr.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dlghndlr.cpp
3 // Purpose: Dialog handler
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 // For compilers that support precompilation, includes "wx/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 <ctype.h>
28 #include <stdlib.h>
29 #include <math.h>
30 #include <string.h>
31
32 #if defined(__WXMSW__) && !defined(__GNUWIN32__)
33 #include <strstrea.h>
34 #else
35 #include <strstream.h>
36 #endif
37
38 #include "reseditr.h"
39 #include "winprop.h"
40 #include "dlghndlr.h"
41 #include "edlist.h"
42
43 IMPLEMENT_CLASS(wxResourceEditorDialogHandler, wxEvtHandler)
44 IMPLEMENT_CLASS(wxResourceEditorControlHandler, wxEvtHandler)
45
46 BEGIN_EVENT_TABLE(wxResourceEditorDialogHandler, wxEvtHandler)
47 EVT_PAINT(wxResourceEditorDialogHandler::OnPaint)
48 EVT_MOUSE_EVENTS(wxResourceEditorDialogHandler::OnMouseEvent)
49 END_EVENT_TABLE()
50
51 BEGIN_EVENT_TABLE(wxResourceEditorControlHandler, wxEvtHandler)
52 EVT_MOUSE_EVENTS(wxResourceEditorControlHandler::OnMouseEvent)
53 END_EVENT_TABLE()
54
55 /*
56 * Dialog box event handler
57 */
58
59 wxResourceEditorDialogHandler::wxResourceEditorDialogHandler(wxPanel *dialog, wxItemResource *resource,
60 wxEvtHandler *oldHandler, wxResourceManager *manager)
61 {
62 handlerDialog = dialog;
63 handlerResource = resource;
64 handlerOldHandler = oldHandler;
65 resourceManager = manager;
66
67 dragMode = wxDRAG_MODE_NONE;
68 dragType = wxDRAG_TYPE_NONE;
69 dragItem = NULL;
70 firstDragX = 0;
71 firstDragY = 0;
72 oldDragX = 0;
73 oldDragY = 0;
74 dragTolerance = 3;
75 checkTolerance = TRUE;
76 m_mouseCaptured = FALSE;
77 // m_treeItem = 0;
78 }
79
80 wxResourceEditorDialogHandler::~wxResourceEditorDialogHandler(void)
81 {
82 }
83
84 void wxResourceEditorDialogHandler::OnItemSelect(wxControl *item, bool select)
85 {
86 if (select)
87 resourceManager->AddSelection(item);
88 else
89 resourceManager->RemoveSelection(item);
90 }
91
92 void wxResourceEditorDialogHandler::OnPaint(wxPaintEvent& event)
93 {
94 wxPaintDC dc(handlerDialog);
95
96 PaintSelectionHandles(dc);
97 }
98
99 // Add event handlers for all children
100 void wxResourceEditorDialogHandler::AddChildHandlers(void)
101 {
102 wxNode *node = handlerDialog->GetChildren()->First();
103 while ( node )
104 {
105 wxControl *child = (wxControl *)node->Data();
106 wxEvtHandler *childHandler = child->GetEventHandler();
107 if ( child->IsKindOf(CLASSINFO(wxControl)) && childHandler == child )
108 child->PushEventHandler(new wxResourceEditorControlHandler(child, childHandler));
109 node = node->Next();
110 }
111 }
112
113 void wxResourceEditorDialogHandler::OnLeftClick(int x, int y, int keys)
114 {
115 if (keys & wxKEY_CTRL)
116 {
117 wxResourceManager::GetCurrentResourceManager()->EditWindow(handlerDialog);
118 return;
119 }
120
121 // Deselect all items if click on panel
122 if (wxResourceManager::GetCurrentResourceManager()->GetEditorControlList()->GetSelection() == RESED_POINTER)
123 {
124 int needsRefresh = 0;
125 wxNode *node = handlerDialog->GetChildren()->First();
126 while (node)
127 {
128 wxControl *item = (wxControl *)node->Data();
129 wxResourceEditorControlHandler *childHandler = (wxResourceEditorControlHandler *)item->GetEventHandler();
130 if (item->IsKindOf(CLASSINFO(wxControl)) && childHandler->IsSelected())
131 {
132 needsRefresh ++;
133 OnItemSelect(item, FALSE);
134 childHandler->SelectItem(FALSE);
135 }
136 node = node->Next();
137 }
138 if (needsRefresh > 0)
139 {
140 wxClientDC dc(handlerDialog);
141 dc.Clear();
142 handlerDialog->Refresh();
143 }
144 return;
145 }
146
147 wxResourceManager* manager = resourceManager;
148
149 switch (wxResourceManager::GetCurrentResourceManager()->GetEditorControlList()->GetSelection())
150 {
151 case RESED_BUTTON:
152 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxButton", x, y);
153 break;
154 case RESED_BMPBUTTON:
155 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxBitmapButton", x, y, TRUE);
156 break;
157 case RESED_STATICTEXT:
158 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxStaticText", x, y);
159 break;
160 case RESED_STATICBMP:
161 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxStaticBitmap", x, y, TRUE);
162 break;
163 case RESED_TEXTCTRL_SINGLE:
164 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxTextCtrl (single-line)", x, y);
165 break;
166 case RESED_TEXTCTRL_MULTIPLE:
167 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxTextCtrl (multi-line)", x, y);
168 break;
169 case RESED_CHOICE:
170 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxChoice", x, y);
171 break;
172 case RESED_CHECKBOX:
173 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxCheckBox", x, y);
174 break;
175 case RESED_RADIOBOX:
176 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxRadioBox", x, y);
177 break;
178 case RESED_RADIOBUTTON:
179 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxRadioButton", x, y);
180 break;
181 case RESED_LISTBOX:
182 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxListBox", x, y);
183 break;
184 case RESED_SLIDER:
185 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxSlider", x, y);
186 break;
187 case RESED_GAUGE:
188 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxGauge", x, y);
189 break;
190 case RESED_STATICBOX:
191 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxStaticBox", x, y);
192 break;
193 case RESED_SCROLLBAR:
194 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxScrollBar", x, y);
195 break;
196 default:
197 break;
198 }
199
200 // Now switch pointer on.
201 if (wxResourceManager::GetCurrentResourceManager()->GetEditorControlList()->GetSelection() != RESED_POINTER)
202 {
203 wxResourceManager::GetCurrentResourceManager()->GetEditorControlList()->SetItemState(RESED_POINTER, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
204 }
205 }
206
207 #if 0
208 void wxResourceEditorDialogHandler::OnLeftClick(int x, int y, int keys)
209 {
210 if (keys & wxKEY_CTRL)
211 {
212 wxResourceManager::GetCurrentResourceManager()->EditWindow(handlerDialog);
213 return;
214 }
215
216 // Deselect all items if click on panel
217 if (resourceManager->GetEditorPalette()->currentlySelected == PALETTE_ARROW)
218 {
219 int needsRefresh = 0;
220 wxNode *node = handlerDialog->GetChildren()->First();
221 while (node)
222 {
223 wxControl *item = (wxControl *)node->Data();
224 wxResourceEditorControlHandler *childHandler = (wxResourceEditorControlHandler *)item->GetEventHandler();
225 if (item->IsKindOf(CLASSINFO(wxControl)) && childHandler->IsSelected())
226 {
227 needsRefresh ++;
228 OnItemSelect(item, FALSE);
229 childHandler->SelectItem(FALSE);
230 }
231 node = node->Next();
232 }
233 if (needsRefresh > 0)
234 {
235 wxClientDC dc(handlerDialog);
236 dc.Clear();
237 handlerDialog->Refresh();
238 }
239 return;
240 }
241
242 wxResourceManager* manager = resourceManager;
243
244 switch (resourceManager->GetEditorPalette()->currentlySelected)
245 {
246 case PALETTE_FRAME:
247 break;
248 case PALETTE_DIALOG_BOX:
249 break;
250 case PALETTE_PANEL:
251 break;
252 case PALETTE_CANVAS:
253 break;
254 case PALETTE_TEXT_WINDOW:
255 break;
256 case PALETTE_BUTTON:
257 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxButton", x, y);
258 break;
259 case PALETTE_BITMAP_BUTTON:
260 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxButton", x, y, TRUE);
261 break;
262 case PALETTE_MESSAGE:
263 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxStaticText", x, y);
264 break;
265 case PALETTE_BITMAP_MESSAGE:
266 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxStaticBitmap", x, y, TRUE);
267 break;
268 case PALETTE_TEXT:
269 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxTextCtrl (single-line)", x, y);
270 break;
271 case PALETTE_MULTITEXT:
272 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxTextCtrl (multi-line)", x, y);
273 break;
274 case PALETTE_CHOICE:
275 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxChoice", x, y);
276 break;
277 case PALETTE_CHECKBOX:
278 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxCheckBox", x, y);
279 break;
280 case PALETTE_RADIOBOX:
281 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxRadioBox", x, y);
282 break;
283 case PALETTE_LISTBOX:
284 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxListBox", x, y);
285 break;
286 case PALETTE_SLIDER:
287 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxSlider", x, y);
288 break;
289 case PALETTE_GAUGE:
290 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxGauge", x, y);
291 break;
292 case PALETTE_GROUPBOX:
293 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxStaticBox", x, y);
294 break;
295 case PALETTE_SCROLLBAR:
296 resourceManager->CreatePanelItem(handlerResource, handlerDialog, "wxScrollBar", x, y);
297 break;
298 default:
299 break;
300 }
301
302 // Now switch pointer on.
303 if (manager->GetEditorPalette()->currentlySelected != PALETTE_ARROW)
304 {
305 manager->GetEditorPalette()->ToggleTool(manager->GetEditorPalette()->currentlySelected, FALSE);
306 manager->GetEditorPalette()->ToggleTool(PALETTE_ARROW, TRUE);
307 manager->GetEditorPalette()->currentlySelected = PALETTE_ARROW;
308 }
309 }
310 #endif
311
312 void wxResourceEditorDialogHandler::OnRightClick(int x, int y, int keys)
313 {
314 wxMenu *menu = resourceManager->GetPopupMenu();
315 menu->SetClientData((char *)handlerDialog);
316 handlerDialog->PopupMenu(menu, x, y);
317 }
318
319 void wxResourceEditorDialogHandler::OnItemLeftClick(wxControl *item, int x, int y, int keys)
320 {
321 if (keys & wxKEY_CTRL)
322 {
323 wxResourceManager::GetCurrentResourceManager()->EditWindow(item);
324 return;
325 }
326
327 /*
328 // If this is a wxStaticBox and the pointer isn't an arrow, chances
329 // are that we really meant to place an item on the panel.
330 // Fake this event.
331 if ((item->GetClassInfo() == CLASSINFO(wxStaticBox)) && resourceManager->GetEditorPalette()->currentlySelected != PALETTE_ARROW)
332 {
333 OnLeftClick(x, y, keys);
334 return;
335 }
336 */
337
338 wxResourceEditorControlHandler *childHandler = (wxResourceEditorControlHandler *)item->GetEventHandler();
339
340 if (childHandler->IsSelected())
341 {
342 childHandler->SelectItem(FALSE);
343 OnItemSelect(item, FALSE);
344
345 wxClientDC dc(handlerDialog);
346 dc.Clear();
347 handlerDialog->Refresh();
348 }
349 else
350 {
351 childHandler->SelectItem(TRUE);
352 OnItemSelect(item, TRUE);
353
354 // Deselect other items if shift is not pressed
355 int needsRefresh = 0;
356 if (!(keys & wxKEY_SHIFT))
357 {
358 wxNode *node = item->GetParent()->GetChildren()->First();
359 while (node)
360 {
361 wxControl *child = (wxControl *)node->Data();
362 wxResourceEditorControlHandler *childHandler2 = (wxResourceEditorControlHandler *)child->GetEventHandler();
363 if (child->IsKindOf(CLASSINFO(wxControl)) && childHandler2->IsSelected() && child != item)
364 {
365 childHandler2->SelectItem(FALSE);
366 OnItemSelect(child, FALSE);
367 needsRefresh ++;
368 }
369 node = node->Next();
370 }
371 }
372
373 wxClientDC dc(handlerDialog);
374 childHandler->DrawSelectionHandles(dc);
375
376 if (needsRefresh > 0)
377 {
378 dc.Clear();
379 handlerDialog->Refresh();
380 }
381 }
382 }
383
384 void wxResourceEditorDialogHandler::OnItemRightClick(wxControl *item, int x, int y, int keys)
385 {
386 /*
387 if (keys & wxKEY_CTRL)
388 {
389 wxDebugMsg("Item %s, selected = %d\n", item->GetName(), item->IsSelected());
390 return;
391 }
392 */
393
394 wxMenu *menu = resourceManager->GetPopupMenu();
395 menu->SetClientData((char *)item);
396 handlerDialog->PopupMenu(menu, x, y);
397 }
398
399 // An event outside any items: may be a drag event.
400 void wxResourceEditorDialogHandler::OnMouseEvent(wxMouseEvent& event)
401 {
402 if (GetEvtHandlerEnabled())
403 {
404 // If we're dragging an item or selection handle,
405 // continue dragging.
406 if (dragMode != wxDRAG_MODE_NONE)
407 {
408 ProcessItemEvent(dragItem, event, dragType);
409 return;
410 }
411
412 long x, y;
413 event.Position(&x, &y);
414
415 // Find which selection handle we're on, if any
416 wxNode *node = handlerDialog->GetChildren()->First();
417 while (node)
418 {
419 wxWindow *win = (wxWindow *)node->Data();
420 if (win->IsKindOf(CLASSINFO(wxControl)))
421 {
422 wxControl *item = (wxControl *)win;
423 wxResourceEditorControlHandler *childHandler = (wxResourceEditorControlHandler *)item->GetEventHandler();
424 int selHandle = childHandler->SelectionHandleHitTest(x, y);
425 if (selHandle > 0)
426 {
427 ProcessItemEvent(item, event, selHandle);
428 return;
429 }
430 }
431 node = node->Next();
432 }
433
434 // We're not on an item or selection handle.
435 // so... check for a left or right click event
436 // to send to the application.
437 int keys = 0;
438 if (event.ShiftDown()) keys = keys | wxKEY_SHIFT;
439 if (event.ControlDown()) keys = keys | wxKEY_CTRL;
440
441 if (event.LeftUp())
442 {
443 if (m_mouseCaptured)
444 {
445 handlerDialog->ReleaseMouse();
446 m_mouseCaptured = FALSE;
447 }
448
449 OnLeftClick(x, y, keys);
450 }
451 else if (event.RightUp())
452 {
453 if (m_mouseCaptured)
454 {
455 handlerDialog->ReleaseMouse();
456 m_mouseCaptured = FALSE;
457 }
458
459 OnRightClick(x, y, keys);
460 }
461 }
462 else
463 event.Skip();
464 }
465
466 void wxResourceEditorDialogHandler::OnItemEvent(wxControl *item, wxMouseEvent& event)
467 {
468 if (!GetEvtHandlerEnabled())
469 return;
470
471 // Not a selection handle event: just a normal item event.
472 // Transform to panel coordinates.
473 int x, y;
474 item->GetPosition(&x, &y);
475
476 event.m_x = event.m_x + x;
477 event.m_y = event.m_y + y;
478 ProcessItemEvent(item, event, dragType);
479 }
480
481 void wxResourceEditorDialogHandler::ProcessItemEvent(wxControl *item, wxMouseEvent& event, int selectionHandle)
482 {
483 wxResourceEditorControlHandler *childHandler = (wxResourceEditorControlHandler *)item->GetEventHandler();
484
485 long x, y;
486 event.Position(&x, &y);
487 int keys = 0;
488 if (event.ShiftDown()) keys = keys | wxKEY_SHIFT;
489 if (event.ControlDown()) keys = keys | wxKEY_CTRL;
490 bool dragging = event.Dragging();
491 if (dragging)
492 {
493 int dx = (int)abs((x - firstDragX));
494 int dy = (int)abs((y - firstDragY));
495 if (checkTolerance && (dx <= dragTolerance) && (dy <= dragTolerance))
496 {
497 return;
498 }
499 else
500 // If we've ignored the tolerance once, then ALWAYS ignore
501 // tolerance in this drag, even if we come back within
502 // the tolerance range.
503 {
504 checkTolerance = FALSE;
505 }
506 }
507 if (event.LeftDClick())
508 {
509 if (m_mouseCaptured)
510 {
511 handlerDialog->ReleaseMouse();
512 m_mouseCaptured = FALSE;
513 }
514
515 wxResourceManager::GetCurrentResourceManager()->EditWindow(item);
516 }
517 else if (dragging && dragItem && dragMode == wxDRAG_MODE_START_LEFT)
518 {
519 dragMode = wxDRAG_MODE_CONTINUE_LEFT;
520 wxClientDC dc(handlerDialog);
521 childHandler->OnDragBegin(x, y, keys, dc, selectionHandle);
522 oldDragX = x; oldDragY = y;
523 if (!m_mouseCaptured)
524 {
525 handlerDialog->CaptureMouse();
526 m_mouseCaptured = TRUE;
527 }
528 }
529 else if (dragging && dragItem && dragMode == wxDRAG_MODE_CONTINUE_LEFT)
530 {
531 wxClientDC dc(handlerDialog);
532 childHandler->OnDragContinue(FALSE, oldDragX, oldDragY, keys, dc, selectionHandle);
533 childHandler->OnDragContinue(TRUE, x, y, keys, dc, selectionHandle);
534 oldDragX = x; oldDragY = y;
535 }
536 else if (event.LeftUp() && dragItem && dragMode == wxDRAG_MODE_CONTINUE_LEFT)
537 {
538 wxClientDC dc(handlerDialog);
539 dragMode = wxDRAG_MODE_NONE;
540 checkTolerance = TRUE;
541
542 childHandler->OnDragContinue(FALSE, oldDragX, oldDragY, keys, dc, selectionHandle);
543 childHandler->OnDragEnd(x, y, keys, dc, selectionHandle);
544
545 dragItem = NULL;
546 dragType = wxDRAG_TYPE_NONE;
547
548 if (m_mouseCaptured)
549 {
550 handlerDialog->ReleaseMouse();
551 m_mouseCaptured = FALSE;
552 }
553 }
554 else if (dragging && dragItem && dragMode == wxDRAG_MODE_START_RIGHT)
555 {
556 wxClientDC dc(handlerDialog);
557 dragMode = wxDRAG_MODE_CONTINUE_RIGHT;
558 childHandler->OnDragBegin(x, y, keys, dc, selectionHandle);
559 oldDragX = x; oldDragY = y;
560
561 if (!m_mouseCaptured)
562 {
563 handlerDialog->CaptureMouse();
564 m_mouseCaptured = TRUE;
565 }
566 }
567 else if (dragging && dragItem && dragMode == wxDRAG_MODE_CONTINUE_RIGHT)
568 {
569 oldDragX = x; oldDragY = y;
570 }
571 else if (event.RightUp() && dragItem && dragMode == wxDRAG_MODE_CONTINUE_RIGHT)
572 {
573 dragMode = wxDRAG_MODE_NONE;
574 checkTolerance = TRUE;
575 dragItem = NULL;
576 dragType = wxDRAG_TYPE_NONE;
577
578 if (m_mouseCaptured)
579 {
580 handlerDialog->ReleaseMouse();
581 m_mouseCaptured = FALSE;
582 }
583 }
584 else if (event.IsButton())
585 {
586 checkTolerance = TRUE;
587
588 if (event.LeftDown())
589 {
590 dragItem = item;
591 dragMode = wxDRAG_MODE_START_LEFT;
592 firstDragX = x;
593 firstDragY = y;
594 dragType = selectionHandle;
595
596 if (!m_mouseCaptured)
597 {
598 handlerDialog->CaptureMouse();
599 m_mouseCaptured = TRUE;
600 }
601 }
602 else if (event.RightDown())
603 {
604 dragItem = item;
605 dragMode = wxDRAG_MODE_START_RIGHT;
606 firstDragX = x;
607 firstDragY = y;
608 dragType = selectionHandle;
609
610 if (!m_mouseCaptured)
611 {
612 handlerDialog->CaptureMouse();
613 m_mouseCaptured = TRUE;
614 }
615 }
616 else if (event.LeftUp())
617 {
618 if (dragItem)
619 childHandler->OnLeftClick(x, y, keys);
620 else
621 OnLeftClick(x, y, keys);
622
623 dragItem = NULL; dragMode = wxDRAG_MODE_NONE; dragType = wxDRAG_TYPE_NONE;
624
625 if (m_mouseCaptured)
626 {
627 handlerDialog->ReleaseMouse();
628 m_mouseCaptured = FALSE;
629 }
630 }
631 else if (event.RightUp())
632 {
633 if (dragItem)
634 childHandler->OnRightClick(x, y, keys);
635 else
636 OnRightClick(x, y, keys);
637
638 dragItem = NULL; dragMode = wxDRAG_MODE_NONE; dragType = wxDRAG_TYPE_NONE;
639
640 if (m_mouseCaptured)
641 {
642 handlerDialog->ReleaseMouse();
643 m_mouseCaptured = FALSE;
644 }
645 }
646 }
647 }
648
649 // Calls DrawSelectionHandles for all items if
650 // edit mode is on.
651 void wxResourceEditorDialogHandler::PaintSelectionHandles(wxDC& dc)
652 {
653 if (!GetEvtHandlerEnabled())
654 return;
655
656 dc.BeginDrawing();
657
658 wxNode *node = handlerDialog->GetChildren()->First();
659 while (node)
660 {
661 wxWindow *win = (wxWindow *)node->Data();
662 if (win->IsKindOf(CLASSINFO(wxControl)))
663 {
664 wxControl *item = (wxControl *)win;
665 wxResourceEditorControlHandler *childHandler = (wxResourceEditorControlHandler *)item->GetEventHandler();
666
667 // Don't draw handles for an item that's being moved: it'll
668 // smear.
669 if (childHandler->IsSelected() && (item != dragItem))
670 childHandler->DrawSelectionHandles(dc);
671 }
672 node = node->Next();
673 }
674 dc.EndDrawing();
675 }
676
677 /*
678 * Event handler for controls
679 */
680
681 int wxResourceEditorControlHandler::dragOffsetX = 0;
682 int wxResourceEditorControlHandler::dragOffsetY = 0;
683
684 wxResourceEditorControlHandler::wxResourceEditorControlHandler(wxControl *control,
685 wxEvtHandler *oldHandler)
686 {
687 handlerControl = control;
688 handlerOldHandler = oldHandler;
689
690 handleSize = 6;
691 handleMargin = 1;
692 isSelected = FALSE;
693 dragOffsetX = 0;
694 dragOffsetY = 0;
695 // m_treeItem = 0;
696 }
697
698 wxResourceEditorControlHandler::~wxResourceEditorControlHandler(void)
699 {
700 }
701
702 /*
703 * Manipulation and drawing of items in Edit Mode
704 */
705
706 void wxResourceEditorControlHandler::SelectItem(bool select)
707 {
708 isSelected = select;
709 }
710
711 // Returns TRUE or FALSE
712 bool wxResourceEditorControlHandler::HitTest(int x, int y)
713 {
714 int xpos, ypos, width, height;
715 handlerControl->GetPosition(&xpos, &ypos);
716 handlerControl->GetSize(&width, &height);
717
718 return ((x >= xpos) && (x <= (xpos + width)) && (y >= ypos) && (y <= (ypos + height)));
719 }
720
721 // Calculate position of the 8 handles
722 void wxResourceEditorControlHandler::CalcSelectionHandles(int *hx, int *hy)
723 {
724 int xpos, ypos, width, height;
725 handlerControl->GetPosition(&xpos, &ypos);
726 handlerControl->GetSize(&width, &height);
727 int middleX = (xpos + (width/2));
728 int middleY = (ypos + (height/2));
729
730 // Start from top middle, clockwise.
731 /*
732 7 0 1
733
734 6 2
735
736 5 4 3
737 */
738
739 hx[0] = (int)(middleX - (handleSize/2));
740 hy[0] = ypos - handleSize - handleMargin;
741
742 hx[1] = xpos + width + handleMargin;
743 hy[1] = ypos - handleSize - handleMargin;
744
745 hx[2] = xpos + width + handleMargin;
746 hy[2] = (int)(middleY - (handleSize/2));
747
748 hx[3] = xpos + width + handleMargin;
749 hy[3] = ypos + height + handleMargin;
750
751 hx[4] = (int)(middleX - (handleSize/2));
752 hy[4] = ypos + height + handleMargin;
753
754 hx[5] = xpos - handleSize - handleMargin;
755 hy[5] = ypos + height + handleMargin;
756
757 hx[6] = xpos - handleSize - handleMargin;
758 hy[6] = (int)(middleY - (handleSize/2));
759
760 hx[7] = xpos - handleSize - handleMargin;
761 hy[7] = ypos - handleSize - handleMargin;
762 }
763
764 // Returns 0 (no hit), 1 - 8 for which selection handle
765 // (clockwise from top middle)
766 int wxResourceEditorControlHandler::SelectionHandleHitTest(int x, int y)
767 {
768 // Handle positions
769 int hx[8];
770 int hy[8];
771 CalcSelectionHandles(hx, hy);
772
773 int i;
774 for (i = 0; i < 8; i++)
775 {
776 if ((x >= hx[i]) && (x <= (hx[i] + handleSize)) && (y >= hy[i]) && (y <= (hy[i] + handleSize)))
777 return (i + 1);
778 }
779 return 0;
780 }
781
782 void wxResourceEditorControlHandler::DrawSelectionHandles(wxDC& dc, bool WXUNUSED(erase))
783 {
784 dc.SetOptimization(FALSE);
785
786 dc.SetLogicalFunction(wxCOPY);
787 dc.SetPen(wxBLACK_PEN);
788 dc.SetBrush(wxBLACK_BRUSH);
789
790 dc.SetOptimization(TRUE);
791
792 // Handle positions
793 int hx[8];
794 int hy[8];
795 CalcSelectionHandles(hx, hy);
796
797 int i;
798 for (i = 0; i < 8; i++)
799 {
800 dc.DrawRectangle(hx[i], hy[i], handleSize, handleSize);
801 }
802 }
803
804 void wxResourceEditorControlHandler::DrawBoundingBox(wxDC& dc, int x, int y, int w, int h)
805 {
806 dc.DrawRectangle(x, y, w, h);
807 }
808
809 // If selectionHandle is zero, not dragging the selection handle.
810 void wxResourceEditorControlHandler::OnDragBegin(int x, int y, int WXUNUSED(keys), wxDC& dc, int selectionHandle)
811 {
812 int xpos, ypos, width, height;
813 handlerControl->GetPosition(&xpos, &ypos);
814 handlerControl->GetSize(&width, &height);
815
816 dc.BeginDrawing();
817
818 // dc.DestroyClippingRegion();
819
820 wxPanel *panel = (wxPanel *)handlerControl->GetParent();
821 wxResourceEditorDialogHandler *panelHandler = (wxResourceEditorDialogHandler *)panel->GetEventHandler();
822
823 // Erase selection handles
824 // DrawSelectionHandles(dc, TRUE);
825
826 dc.SetOptimization(FALSE);
827
828 dc.SetLogicalFunction(wxXOR);
829
830 wxPen pen(wxColour(0, 0, 0), 1, wxDOT);
831 dc.SetPen(pen);
832 dc.SetBrush(wxTRANSPARENT_BRUSH);
833
834 dc.SetOptimization(TRUE);
835
836 if (selectionHandle > 0)
837 {
838 panel->Refresh();
839
840 DrawBoundingBox(dc, xpos, ypos, width, height);
841 }
842 else
843 {
844 panel->Refresh();
845
846 dragOffsetX = (x - xpos);
847 dragOffsetY = (y - ypos);
848
849 DrawBoundingBox(dc, xpos, ypos, width, height);
850
851 // Also draw bounding boxes for other selected items
852 wxNode *node = panel->GetChildren()->First();
853 while (node)
854 {
855 wxWindow *win = (wxWindow *)node->Data();
856 if (win->IsKindOf(CLASSINFO(wxControl)))
857 {
858 wxControl *item = (wxControl *)win;
859 wxResourceEditorControlHandler *handler = (wxResourceEditorControlHandler *)item->GetEventHandler();
860 if ((item != handlerControl) && handler->IsSelected())
861 {
862 int x1, y1, w1, h1;
863 item->GetPosition(&x1, &y1);
864 item->GetSize(&w1, &h1);
865 handler->DrawBoundingBox(dc, x1, y1, w1, h1);
866 }
867 }
868 node = node->Next();
869 }
870 }
871 dc.EndDrawing();
872 }
873
874 void wxResourceEditorControlHandler::OnDragContinue(bool paintIt, int x, int y, int WXUNUSED(keys), wxDC& dc, int selectionHandle)
875 {
876 wxPanel *panel = (wxPanel *)handlerControl->GetParent();
877 wxResourceEditorDialogHandler *panelHandler = (wxResourceEditorDialogHandler *)panel->GetEventHandler();
878
879 int xpos, ypos, width, height;
880 handlerControl->GetPosition(&xpos, &ypos);
881 handlerControl->GetSize(&width, &height);
882
883 if (selectionHandle > 0)
884 {
885 /*
886 8 1 2
887
888 7 3
889
890 6 5 4
891 */
892
893 int x1, y1, width1, height1;
894
895 switch (selectionHandle)
896 {
897 case 1:
898 x1 = xpos;
899 y1 = y;
900 width1 = width;
901 height1 = (ypos + height) - y;
902 break;
903 case 5:
904 x1 = xpos;
905 y1 = ypos;
906 width1 = width;
907 height1 = (y - ypos);
908 break;
909 case 3:
910 x1 = xpos;
911 y1 = ypos;
912 width1 = (x - xpos);
913 height1 = height;
914 break;
915 case 7:
916 x1 = x;
917 y1 = ypos;
918 width1 = (xpos + width) - x;
919 height1 = height;
920 break;
921 case 2:
922 x1 = xpos;
923 y1 = y;
924 width1 = (x - xpos);
925 height1 = (ypos + height) - y;
926 break;
927 case 4:
928 x1 = xpos;
929 y1 = ypos;
930 width1 = (x - xpos);
931 height1 = (y - ypos);
932 break;
933 case 6:
934 x1 = x;
935 y1 = ypos;
936 width1 = (xpos + width) - x;
937 height1 = y - ypos;
938 break;
939 case 8:
940 x1 = x;
941 y1 = y;
942 width1 = (xpos + width) - x;
943 height1 = (ypos + height) - y;
944 break;
945 }
946 dc.BeginDrawing();
947
948 dc.SetLogicalFunction(wxXOR);
949 wxPen pen(wxColour(0, 0, 0), 1, wxDOT);
950 dc.SetPen(pen);
951 dc.SetBrush(wxTRANSPARENT_BRUSH);
952
953 DrawBoundingBox(dc, x1, y1, width1, height1);
954
955 dc.EndDrawing();
956 }
957 else
958 {
959 dc.BeginDrawing();
960 dc.SetLogicalFunction(wxXOR);
961 wxPen pen(wxColour(0, 0, 0), 1, wxDOT);
962 dc.SetPen(pen);
963 dc.SetBrush(wxTRANSPARENT_BRUSH);
964
965 DrawBoundingBox(dc, (int)(x - dragOffsetX), (int)(y - dragOffsetY), width, height);
966
967 // Also draw bounding boxes for other selected items
968 wxNode *node = panel->GetChildren()->First();
969 while (node)
970 {
971 wxWindow *win = (wxWindow *)node->Data();
972 if (win->IsKindOf(CLASSINFO(wxControl)))
973 {
974 wxControl *item = (wxControl *)win;
975 wxResourceEditorControlHandler *handler = (wxResourceEditorControlHandler *)item->GetEventHandler();
976 if ((item != handlerControl) && handler->IsSelected())
977 {
978 int x1, y1, w1, h1;
979 item->GetPosition(&x1, &y1);
980 item->GetSize(&w1, &h1);
981 int x2 = (int)(x1 + (x - dragOffsetX) - xpos);
982 int y2 = (int)(y1 + (y - dragOffsetY) - ypos);
983 handler->DrawBoundingBox(dc, x2, y2, w1, h1);
984 }
985 }
986 node = node->Next();
987 }
988 dc.EndDrawing();
989 }
990 }
991
992 void wxResourceEditorControlHandler::OnDragEnd(int x, int y, int WXUNUSED(keys), wxDC& dc, int selectionHandle)
993 {
994 wxPanel *panel = (wxPanel *)handlerControl->GetParent();
995 wxResourceEditorDialogHandler *panelHandler = (wxResourceEditorDialogHandler *)panel->GetEventHandler();
996
997 dc.BeginDrawing();
998
999 int xpos, ypos, width, height;
1000 handlerControl->GetPosition(&xpos, &ypos);
1001 handlerControl->GetSize(&width, &height);
1002
1003 if (selectionHandle > 0)
1004 {
1005 int x1, y1, width1, height1;
1006
1007 switch (selectionHandle)
1008 {
1009 case 1:
1010 x1 = xpos;
1011 y1 = y;
1012 width1 = width;
1013 height1 = (ypos + height) - y;
1014 break;
1015 case 5:
1016 x1 = xpos;
1017 y1 = ypos;
1018 width1 = width;
1019 height1 = (y - ypos);
1020 break;
1021 case 3:
1022 x1 = xpos;
1023 y1 = ypos;
1024 width1 = (x - xpos);
1025 height1 = height;
1026 break;
1027 case 7:
1028 x1 = x;
1029 y1 = ypos;
1030 width1 = (xpos + width) - x;
1031 height1 = height;
1032 break;
1033 case 2:
1034 x1 = xpos;
1035 y1 = y;
1036 width1 = (x - xpos);
1037 height1 = (ypos + height) - y;
1038 break;
1039 case 4:
1040 x1 = xpos;
1041 y1 = ypos;
1042 width1 = (x - xpos);
1043 height1 = (y - ypos);
1044 break;
1045 case 6:
1046 x1 = x;
1047 y1 = ypos;
1048 width1 = (xpos + width) - x;
1049 height1 = y - ypos;
1050 break;
1051 case 8:
1052 x1 = x;
1053 y1 = y;
1054 width1 = (xpos + width) - x;
1055 height1 = (ypos + height) - y;
1056 break;
1057 }
1058 handlerControl->SetSize(x1, y1, width1, height1);
1059 }
1060 else
1061 {
1062 handlerControl->Move((int)(x - dragOffsetX), (int)(y - dragOffsetY));
1063 OldOnMove((int)(x - dragOffsetX), (int)(y - dragOffsetY));
1064
1065 // Also move other selected items
1066 wxNode *node = panel->GetChildren()->First();
1067 while (node)
1068 {
1069 wxWindow *win = (wxWindow *)node->Data();
1070 if (win->IsKindOf(CLASSINFO(wxControl)))
1071 {
1072 wxControl *item = (wxControl *)win;
1073 wxResourceEditorControlHandler *handler = (wxResourceEditorControlHandler *)item->GetEventHandler();
1074 if ((item != handlerControl) && handler->IsSelected())
1075 {
1076 int x1, y1;
1077 item->GetPosition(&x1, &y1);
1078 int x2 = (int)(x1 + (x - dragOffsetX) - xpos);
1079 int y2 = (int)(y1 + (y - dragOffsetY) - ypos);
1080 item->Move(x2, y2);
1081 ((wxResourceEditorControlHandler *)item->GetEventHandler())->OldOnMove(x2, y2);
1082 ((wxResourceEditorControlHandler *)item->GetEventHandler())->DrawSelectionHandles(dc);
1083 }
1084 }
1085 node = node->Next();
1086 }
1087 }
1088 dc.SetOptimization(FALSE);
1089
1090 dc.SetLogicalFunction(wxCOPY);
1091 dc.SetPen(wxBLACK_PEN);
1092 dc.SetBrush(wxBLACK_BRUSH);
1093
1094 dc.SetOptimization(TRUE);
1095
1096 // Force it to repaint the selection handles (if any)
1097 // since the panel thinks we're still within a drag and
1098 // won't paint the handles.
1099 if (IsSelected())
1100 DrawSelectionHandles(dc);
1101
1102 dc.EndDrawing();
1103
1104 panel->Refresh();
1105 }
1106
1107 // These functions call OnItemEvent, OnItemMove and OnItemSize
1108 // by default.
1109 void wxResourceEditorControlHandler::OnMouseEvent(wxMouseEvent& event)
1110 {
1111 /*
1112 if ((event.m_eventType == wxEVENT_TYPE_LEFT_DCLICK) ||
1113 (event.m_eventType == wxEVENT_TYPE_RIGHT_DCLICK))
1114 return;
1115 */
1116 wxWindow *panel = handlerControl->GetParent();
1117 if ( !panel->GetEventHandler()->IsKindOf(CLASSINFO(wxResourceEditorDialogHandler)) )
1118 return;
1119 wxResourceEditorDialogHandler *panelHandler = (wxResourceEditorDialogHandler *)panel->GetEventHandler();
1120 if ( !panelHandler->GetEvtHandlerEnabled() )
1121 {
1122 event.Skip();
1123 return;
1124 }
1125
1126 panelHandler->OnItemEvent(handlerControl, event);
1127 }
1128
1129 void wxResourceEditorControlHandler::OldOnMove(int x, int y)
1130 {
1131 wxWindow *panel = handlerControl->GetParent();
1132 if ( !panel->GetEventHandler()->IsKindOf(CLASSINFO(wxResourceEditorDialogHandler)) )
1133 return;
1134
1135 wxResourceEditorDialogHandler *panelHandler = (wxResourceEditorDialogHandler *)panel->GetEventHandler();
1136 panelHandler->OnItemMove(handlerControl, x, y);
1137 }
1138
1139 void wxResourceEditorControlHandler::OldOnSize(int w, int h)
1140 {
1141 wxWindow *panel = handlerControl->GetParent();
1142 if ( !panel->GetEventHandler()->IsKindOf(CLASSINFO(wxResourceEditorDialogHandler)) )
1143 return;
1144
1145 wxResourceEditorDialogHandler *panelHandler = (wxResourceEditorDialogHandler *)panel->GetEventHandler();
1146 panelHandler->OnItemSize(handlerControl, w, h);
1147 }
1148
1149 void wxResourceEditorControlHandler::OnSelect(bool select)
1150 {
1151 wxWindow *panel = handlerControl->GetParent();
1152 if ( !panel->GetEventHandler()->IsKindOf(CLASSINFO(wxResourceEditorDialogHandler)) )
1153 return;
1154
1155 wxResourceEditorDialogHandler *panelHandler = (wxResourceEditorDialogHandler *)panel->GetEventHandler();
1156 panelHandler->OnItemSelect(handlerControl, select);
1157 }
1158
1159 void wxResourceEditorControlHandler::OnLeftClick(int x, int y, int keys)
1160 {
1161 wxWindow *panel = handlerControl->GetParent();
1162 if ( !panel->GetEventHandler()->IsKindOf(CLASSINFO(wxResourceEditorDialogHandler)) )
1163 return;
1164
1165 wxResourceEditorDialogHandler *panelHandler = (wxResourceEditorDialogHandler *)panel->GetEventHandler();
1166 panelHandler->OnItemLeftClick(handlerControl, x, y, keys);
1167 }
1168
1169 void wxResourceEditorControlHandler::OnRightClick(int x, int y, int keys)
1170 {
1171 wxWindow *panel = handlerControl->GetParent();
1172 if ( !panel->GetEventHandler()->IsKindOf(CLASSINFO(wxResourceEditorDialogHandler)) )
1173 return;
1174
1175 wxResourceEditorDialogHandler *panelHandler = (wxResourceEditorDialogHandler *)panel->GetEventHandler();
1176 panelHandler->OnItemRightClick(handlerControl, x, y, keys);
1177 }
1178
1179