]> git.saurik.com Git - wxWidgets.git/blob - src/qt/window.cpp
regenerated
[wxWidgets.git] / src / qt / window.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: windows.cpp
3 // Purpose: wxWindow
4 // Author: AUTHOR
5 // Modified by:
6 // Created: ??/??/98
7 // RCS-ID: $Id$
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "window.h"
14 #endif
15
16 #include "wx/setup.h"
17 #include "wx/menu.h"
18 #include "wx/dc.h"
19 #include "wx/dcclient.h"
20 #include "wx/utils.h"
21 #include "wx/app.h"
22 #include "wx/panel.h"
23 #include "wx/layout.h"
24 #include "wx/dialog.h"
25 #include "wx/listbox.h"
26 #include "wx/button.h"
27 #include "wx/settings.h"
28 #include "wx/msgdlg.h"
29
30 #include "wx/menuitem.h"
31 #include "wx/log.h"
32
33 #if wxUSE_DRAG_AND_DROP
34 #include "wx/dnd.h"
35 #endif
36
37 #include <string.h>
38
39 extern wxList wxPendingDelete;
40
41 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxEvtHandler)
42
43 BEGIN_EVENT_TABLE(wxWindow, wxEvtHandler)
44 EVT_CHAR(wxWindow::OnChar)
45 EVT_ERASE_BACKGROUND(wxWindow::OnEraseBackground)
46 EVT_SYS_COLOUR_CHANGED(wxWindow::OnSysColourChanged)
47 EVT_INIT_DIALOG(wxWindow::OnInitDialog)
48 EVT_IDLE(wxWindow::OnIdle)
49 END_EVENT_TABLE()
50
51
52
53 // Constructor
54 wxWindow::wxWindow()
55 {
56 // Generic
57 m_windowId = 0;
58 m_windowStyle = 0;
59 m_windowParent = NULL;
60 m_windowEventHandler = this;
61 m_windowName = "";
62 m_windowCursor = *wxSTANDARD_CURSOR;
63 m_children = new wxList;
64 m_constraints = NULL;
65 m_constraintsInvolvedIn = NULL;
66 m_windowSizer = NULL;
67 m_sizerParent = NULL;
68 m_autoLayout = FALSE;
69 m_windowValidator = NULL;
70 m_defaultItem = NULL;
71 m_returnCode = 0;
72 m_caretWidth = 0; m_caretHeight = 0;
73 m_caretEnabled = FALSE;
74 m_caretShown = FALSE;
75 m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW) ; ;
76 m_foregroundColour = *wxBLACK;
77 m_defaultForegroundColour = *wxBLACK ;
78 m_defaultBackgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE) ;
79
80 #if wxUSE_DRAG_AND_DROP
81 m_pDropTarget = NULL;
82 #endif
83 }
84
85 // Destructor
86 wxWindow::~wxWindow()
87 {
88 // Have to delete constraints/sizer FIRST otherwise
89 // sizers may try to look at deleted windows as they
90 // delete themselves.
91 #if wxUSE_CONSTRAINTS
92 DeleteRelatedConstraints();
93 if (m_constraints)
94 {
95 // This removes any dangling pointers to this window
96 // in other windows' constraintsInvolvedIn lists.
97 UnsetConstraints(m_constraints);
98 delete m_constraints;
99 m_constraints = NULL;
100 }
101 if (m_windowSizer)
102 {
103 delete m_windowSizer;
104 m_windowSizer = NULL;
105 }
106 // If this is a child of a sizer, remove self from parent
107 if (m_sizerParent)
108 m_sizerParent->RemoveChild((wxWindow *)this);
109 #endif
110
111 if (m_windowParent)
112 m_windowParent->RemoveChild(this);
113
114 DestroyChildren();
115
116 // TODO: destroy the window
117
118 delete m_children;
119 m_children = NULL;
120
121 // Just in case the window has been Closed, but
122 // we're then deleting immediately: don't leave
123 // dangling pointers.
124 wxPendingDelete.DeleteObject(this);
125
126 if ( m_windowValidator )
127 delete m_windowValidator;
128 }
129
130 // Destroy the window (delayed, if a managed window)
131 bool wxWindow::Destroy()
132 {
133 delete this;
134 return TRUE;
135 }
136
137 // Constructor
138 bool wxWindow::Create(wxWindow *parent, wxWindowID id,
139 const wxPoint& pos,
140 const wxSize& size,
141 long style,
142 const wxString& name)
143 {
144 // Generic
145 m_windowId = 0;
146 m_windowStyle = 0;
147 m_windowParent = NULL;
148 m_windowEventHandler = this;
149 m_windowName = "";
150 m_windowCursor = *wxSTANDARD_CURSOR;
151 m_constraints = NULL;
152 m_constraintsInvolvedIn = NULL;
153 m_windowSizer = NULL;
154 m_sizerParent = NULL;
155 m_autoLayout = FALSE;
156 m_windowValidator = NULL;
157
158 #if wxUSE_DRAG_AND_DROP
159 m_pDropTarget = NULL;
160 #endif
161
162 m_caretWidth = 0; m_caretHeight = 0;
163 m_caretEnabled = FALSE;
164 m_caretShown = FALSE;
165 m_minSizeX = -1;
166 m_minSizeY = -1;
167 m_maxSizeX = -1;
168 m_maxSizeY = -1;
169 m_defaultItem = NULL;
170 m_windowParent = NULL;
171 if (!parent)
172 return FALSE;
173
174 if (parent) parent->AddChild(this);
175
176 m_returnCode = 0;
177
178 SetName(name);
179
180 if ( id == -1 )
181 m_windowId = (int)NewControlId();
182 else
183 m_windowId = id;
184
185 m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW) ; ;
186 m_foregroundColour = *wxBLACK;
187 m_defaultForegroundColour = *wxBLACK ;
188 m_defaultBackgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE) ;
189
190 m_windowStyle = style;
191
192 if ( id == -1 )
193 m_windowId = (int)NewControlId();
194 else
195 m_windowId = id;
196
197 // TODO: create the window
198
199 return TRUE;
200 }
201
202 void wxWindow::SetFocus()
203 {
204 // TODO
205 }
206
207 void wxWindow::Enable(bool enable)
208 {
209 // TODO
210 }
211
212 void wxWindow::CaptureMouse()
213 {
214 // TODO
215 }
216
217 void wxWindow::ReleaseMouse()
218 {
219 // TODO
220 }
221
222 // Push/pop event handler (i.e. allow a chain of event handlers
223 // be searched)
224 void wxWindow::PushEventHandler(wxEvtHandler *handler)
225 {
226 handler->SetNextHandler(GetEventHandler());
227 SetEventHandler(handler);
228 }
229
230 wxEvtHandler *wxWindow::PopEventHandler(bool deleteHandler)
231 {
232 if ( GetEventHandler() )
233 {
234 wxEvtHandler *handlerA = GetEventHandler();
235 wxEvtHandler *handlerB = handlerA->GetNextHandler();
236 handlerA->SetNextHandler(NULL);
237 SetEventHandler(handlerB);
238 if ( deleteHandler )
239 {
240 delete handlerA;
241 return NULL;
242 }
243 else
244 return handlerA;
245 }
246 else
247 return NULL;
248 }
249
250 #if wxUSE_DRAG_AND_DROP
251
252 void wxWindow::SetDropTarget(wxDropTarget *pDropTarget)
253 {
254 if ( m_pDropTarget != 0 ) {
255 m_pDropTarget->Revoke(m_hWnd);
256 delete m_pDropTarget;
257 }
258
259 m_pDropTarget = pDropTarget;
260 if ( m_pDropTarget != 0 )
261 m_pDropTarget->Register(m_hWnd);
262 }
263
264 #endif
265
266 // Old style file-manager drag&drop
267 void wxWindow::DragAcceptFiles(bool accept)
268 {
269 // TODO
270 }
271
272 // Get total size
273 void wxWindow::GetSize(int *x, int *y) const
274 {
275 // TODO
276 }
277
278 void wxWindow::GetPosition(int *x, int *y) const
279 {
280 // TODO
281 }
282
283 void wxWindow::ScreenToClient(int *x, int *y) const
284 {
285 // TODO
286 }
287
288 void wxWindow::ClientToScreen(int *x, int *y) const
289 {
290 // TODO
291 }
292
293 void wxWindow::SetCursor(const wxCursor& cursor)
294 {
295 m_windowCursor = cursor;
296 if (m_windowCursor.Ok())
297 {
298 // TODO
299 }
300 }
301
302
303 // Get size *available for subwindows* i.e. excluding menu bar etc.
304 void wxWindow::GetClientSize(int *x, int *y) const
305 {
306 // TODO
307 }
308
309 void wxWindow::SetSize(int x, int y, int width, int height, int sizeFlags)
310 {
311 // TODO
312 }
313
314 void wxWindow::SetClientSize(int width, int height)
315 {
316 // TODO
317 }
318
319 // For implementation purposes - sometimes decorations make the client area
320 // smaller
321 wxPoint wxWindow::GetClientAreaOrigin() const
322 {
323 return wxPoint(0, 0);
324 }
325
326 // Makes an adjustment to the window position (for example, a frame that has
327 // a toolbar that it manages itself).
328 void wxWindow::AdjustForParentClientOrigin(int& x, int& y, int sizeFlags)
329 {
330 if (((sizeFlags & wxSIZE_NO_ADJUSTMENTS) == 0) && GetParent())
331 {
332 wxPoint pt(GetParent()->GetClientAreaOrigin());
333 x += pt.x; y += pt.y;
334 }
335 }
336
337 bool wxWindow::Show(bool show)
338 {
339 // TODO
340 return FALSE;
341 }
342
343 bool wxWindow::IsShown() const
344 {
345 // TODO
346 return FALSE;
347 }
348
349 int wxWindow::GetCharHeight() const
350 {
351 // TODO
352 return 0;
353 }
354
355 int wxWindow::GetCharWidth() const
356 {
357 // TODO
358 return 0;
359 }
360
361 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
362 int *descent, int *externalLeading, const wxFont *theFont, bool) const
363 {
364 wxFont *fontToUse = (wxFont *)theFont;
365 if (!fontToUse)
366 fontToUse = (wxFont *) & m_windowFont;
367
368 // TODO
369 }
370
371 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
372 {
373 // TODO
374 }
375
376 // Responds to colour changes: passes event on to children.
377 void wxWindow::OnSysColourChanged(wxSysColourChangedEvent& event)
378 {
379 wxNode *node = GetChildren()->First();
380 while ( node )
381 {
382 // Only propagate to non-top-level windows
383 wxWindow *win = (wxWindow *)node->Data();
384 if ( win->GetParent() )
385 {
386 wxSysColourChangedEvent event2;
387 event.m_eventObject = win;
388 win->GetEventHandler()->ProcessEvent(event2);
389 }
390
391 node = node->Next();
392 }
393 }
394
395 // This can be called by the app (or wxWindows) to do default processing for the current
396 // event. Save message/event info in wxWindow so they can be used in this function.
397 long wxWindow::Default()
398 {
399 // TODO
400 return 0;
401 }
402
403 void wxWindow::InitDialog()
404 {
405 wxInitDialogEvent event(GetId());
406 event.SetEventObject( this );
407 GetEventHandler()->ProcessEvent(event);
408 }
409
410 // Default init dialog behaviour is to transfer data to window
411 void wxWindow::OnInitDialog(wxInitDialogEvent& event)
412 {
413 TransferDataToWindow();
414 }
415
416 // Caret manipulation
417 void wxWindow::CreateCaret(int w, int h)
418 {
419 m_caretWidth = w;
420 m_caretHeight = h;
421 m_caretEnabled = TRUE;
422 }
423
424 void wxWindow::CreateCaret(const wxBitmap *WXUNUSED(bitmap))
425 {
426 // TODO
427 }
428
429 void wxWindow::ShowCaret(bool show)
430 {
431 // TODO
432 }
433
434 void wxWindow::DestroyCaret()
435 {
436 // TODO
437 m_caretEnabled = FALSE;
438 }
439
440 void wxWindow::SetCaretPos(int x, int y)
441 {
442 // TODO
443 }
444
445 void wxWindow::GetCaretPos(int *x, int *y) const
446 {
447 // TODO
448 }
449
450 wxWindow *wxGetActiveWindow()
451 {
452 // TODO
453 return NULL;
454 }
455
456 void wxWindow::SetSizeHints(int minW, int minH, int maxW, int maxH, int WXUNUSED(incW), int WXUNUSED(incH))
457 {
458 m_minSizeX = minW;
459 m_minSizeY = minH;
460 m_maxSizeX = maxW;
461 m_maxSizeY = maxH;
462 }
463
464 void wxWindow::Centre(int direction)
465 {
466 int x, y, width, height, panel_width, panel_height, new_x, new_y;
467
468 wxWindow *father = (wxWindow *)GetParent();
469 if (!father)
470 return;
471
472 father->GetClientSize(&panel_width, &panel_height);
473 GetSize(&width, &height);
474 GetPosition(&x, &y);
475
476 new_x = -1;
477 new_y = -1;
478
479 if (direction & wxHORIZONTAL)
480 new_x = (int)((panel_width - width)/2);
481
482 if (direction & wxVERTICAL)
483 new_y = (int)((panel_height - height)/2);
484
485 SetSize(new_x, new_y, -1, -1);
486
487 }
488
489 // Coordinates relative to the window
490 void wxWindow::WarpPointer (int x_pos, int y_pos)
491 {
492 // TODO
493 }
494
495 void wxWindow::OnEraseBackground(wxEraseEvent& event)
496 {
497 // TODO
498 Default();
499 }
500
501 int wxWindow::GetScrollPos(int orient) const
502 {
503 // TODO
504 return 0;
505 }
506
507 // This now returns the whole range, not just the number
508 // of positions that we can scroll.
509 int wxWindow::GetScrollRange(int orient) const
510 {
511 // TODO
512 return 0;
513 }
514
515 int wxWindow::GetScrollThumb(int orient) const
516 {
517 // TODO
518 return 0;
519 }
520
521 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
522 {
523 // TODO
524 return 0;
525 }
526
527 // New function that will replace some of the above.
528 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
529 int range, bool refresh)
530 {
531 // TODO
532 }
533
534 // Does a physical scroll
535 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
536 {
537 // TODO
538 return 0;
539 }
540
541 void wxWindow::SetFont(const wxFont& font)
542 {
543 m_windowFont = font;
544
545 if (!m_windowFont.Ok())
546 return;
547 // TODO
548 }
549
550 void wxWindow::OnChar(wxKeyEvent& event)
551 {
552 if ( event.KeyCode() == WXK_TAB ) {
553 // propagate the TABs to the parent - it's up to it to decide what
554 // to do with it
555 if ( GetParent() ) {
556 if ( GetParent()->ProcessEvent(event) )
557 return;
558 }
559 }
560 }
561
562 void wxWindow::OnPaint(wxPaintEvent& event)
563 {
564 Default();
565 }
566
567 bool wxWindow::IsEnabled() const
568 {
569 // TODO
570 return FALSE;
571 }
572
573 // Dialog support: override these and call
574 // base class members to add functionality
575 // that can't be done using validators.
576 // NOTE: these functions assume that controls
577 // are direct children of this window, not grandchildren
578 // or other levels of descendant.
579
580 // Transfer values to controls. If returns FALSE,
581 // it's an application error (pops up a dialog)
582 bool wxWindow::TransferDataToWindow()
583 {
584 wxNode *node = GetChildren()->First();
585 while ( node )
586 {
587 wxWindow *child = (wxWindow *)node->Data();
588 if ( child->GetValidator() &&
589 !child->GetValidator()->TransferToWindow() )
590 {
591 wxMessageBox("Application Error", "Could not transfer data to window", wxOK|wxICON_EXCLAMATION);
592 return FALSE;
593 }
594
595 node = node->Next();
596 }
597 return TRUE;
598 }
599
600 // Transfer values from controls. If returns FALSE,
601 // validation failed: don't quit
602 bool wxWindow::TransferDataFromWindow()
603 {
604 wxNode *node = GetChildren()->First();
605 while ( node )
606 {
607 wxWindow *child = (wxWindow *)node->Data();
608 if ( child->GetValidator() && !child->GetValidator()->TransferFromWindow() )
609 {
610 return FALSE;
611 }
612
613 node = node->Next();
614 }
615 return TRUE;
616 }
617
618 bool wxWindow::Validate()
619 {
620 wxNode *node = GetChildren()->First();
621 while ( node )
622 {
623 wxWindow *child = (wxWindow *)node->Data();
624 if ( child->GetValidator() && /* child->GetValidator()->Ok() && */ !child->GetValidator()->Validate(this) )
625 {
626 return FALSE;
627 }
628
629 node = node->Next();
630 }
631 return TRUE;
632 }
633
634 // Get the window with the focus
635 wxWindow *wxWindow::FindFocus()
636 {
637 // TODO
638 return NULL;
639 }
640
641 void wxWindow::AddChild(wxWindow *child)
642 {
643 GetChildren()->Append(child);
644 child->m_windowParent = this;
645 }
646
647 void wxWindow::RemoveChild(wxWindow *child)
648 {
649 if (GetChildren())
650 GetChildren()->DeleteObject(child);
651 child->m_windowParent = NULL;
652 }
653
654 void wxWindow::DestroyChildren()
655 {
656 if (GetChildren()) {
657 wxNode *node;
658 while ((node = GetChildren()->First()) != (wxNode *)NULL) {
659 wxWindow *child;
660 if ((child = (wxWindow *)node->Data()) != (wxWindow *)NULL) {
661 delete child;
662 if ( GetChildren()->Member(child) )
663 delete node;
664 }
665 } /* while */
666 }
667 }
668
669 void wxWindow::MakeModal(bool modal)
670 {
671 // Disable all other windows
672 if (this->IsKindOf(CLASSINFO(wxDialog)) || this->IsKindOf(CLASSINFO(wxFrame)))
673 {
674 wxNode *node = wxTopLevelWindows.First();
675 while (node)
676 {
677 wxWindow *win = (wxWindow *)node->Data();
678 if (win != this)
679 win->Enable(!modal);
680
681 node = node->Next();
682 }
683 }
684 }
685
686 // If nothing defined for this, try the parent.
687 // E.g. we may be a button loaded from a resource, with no callback function
688 // defined.
689 void wxWindow::OnCommand(wxWindow& win, wxCommandEvent& event)
690 {
691 if (GetEventHandler()->ProcessEvent(event) )
692 return;
693 if (m_windowParent)
694 m_windowParent->GetEventHandler()->OnCommand(win, event);
695 }
696
697 void wxWindow::SetConstraints(wxLayoutConstraints *c)
698 {
699 if (m_constraints)
700 {
701 UnsetConstraints(m_constraints);
702 delete m_constraints;
703 }
704 m_constraints = c;
705 if (m_constraints)
706 {
707 // Make sure other windows know they're part of a 'meaningful relationship'
708 if (m_constraints->left.GetOtherWindow() && (m_constraints->left.GetOtherWindow() != this))
709 m_constraints->left.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
710 if (m_constraints->top.GetOtherWindow() && (m_constraints->top.GetOtherWindow() != this))
711 m_constraints->top.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
712 if (m_constraints->right.GetOtherWindow() && (m_constraints->right.GetOtherWindow() != this))
713 m_constraints->right.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
714 if (m_constraints->bottom.GetOtherWindow() && (m_constraints->bottom.GetOtherWindow() != this))
715 m_constraints->bottom.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
716 if (m_constraints->width.GetOtherWindow() && (m_constraints->width.GetOtherWindow() != this))
717 m_constraints->width.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
718 if (m_constraints->height.GetOtherWindow() && (m_constraints->height.GetOtherWindow() != this))
719 m_constraints->height.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
720 if (m_constraints->centreX.GetOtherWindow() && (m_constraints->centreX.GetOtherWindow() != this))
721 m_constraints->centreX.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
722 if (m_constraints->centreY.GetOtherWindow() && (m_constraints->centreY.GetOtherWindow() != this))
723 m_constraints->centreY.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
724 }
725 }
726
727 // This removes any dangling pointers to this window
728 // in other windows' constraintsInvolvedIn lists.
729 void wxWindow::UnsetConstraints(wxLayoutConstraints *c)
730 {
731 if (c)
732 {
733 if (c->left.GetOtherWindow() && (c->top.GetOtherWindow() != this))
734 c->left.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
735 if (c->top.GetOtherWindow() && (c->top.GetOtherWindow() != this))
736 c->top.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
737 if (c->right.GetOtherWindow() && (c->right.GetOtherWindow() != this))
738 c->right.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
739 if (c->bottom.GetOtherWindow() && (c->bottom.GetOtherWindow() != this))
740 c->bottom.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
741 if (c->width.GetOtherWindow() && (c->width.GetOtherWindow() != this))
742 c->width.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
743 if (c->height.GetOtherWindow() && (c->height.GetOtherWindow() != this))
744 c->height.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
745 if (c->centreX.GetOtherWindow() && (c->centreX.GetOtherWindow() != this))
746 c->centreX.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
747 if (c->centreY.GetOtherWindow() && (c->centreY.GetOtherWindow() != this))
748 c->centreY.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
749 }
750 }
751
752 // Back-pointer to other windows we're involved with, so if we delete
753 // this window, we must delete any constraints we're involved with.
754 void wxWindow::AddConstraintReference(wxWindow *otherWin)
755 {
756 if (!m_constraintsInvolvedIn)
757 m_constraintsInvolvedIn = new wxList;
758 if (!m_constraintsInvolvedIn->Member(otherWin))
759 m_constraintsInvolvedIn->Append(otherWin);
760 }
761
762 // REMOVE back-pointer to other windows we're involved with.
763 void wxWindow::RemoveConstraintReference(wxWindow *otherWin)
764 {
765 if (m_constraintsInvolvedIn)
766 m_constraintsInvolvedIn->DeleteObject(otherWin);
767 }
768
769 // Reset any constraints that mention this window
770 void wxWindow::DeleteRelatedConstraints()
771 {
772 if (m_constraintsInvolvedIn)
773 {
774 wxNode *node = m_constraintsInvolvedIn->First();
775 while (node)
776 {
777 wxWindow *win = (wxWindow *)node->Data();
778 wxNode *next = node->Next();
779 wxLayoutConstraints *constr = win->GetConstraints();
780
781 // Reset any constraints involving this window
782 if (constr)
783 {
784 constr->left.ResetIfWin((wxWindow *)this);
785 constr->top.ResetIfWin((wxWindow *)this);
786 constr->right.ResetIfWin((wxWindow *)this);
787 constr->bottom.ResetIfWin((wxWindow *)this);
788 constr->width.ResetIfWin((wxWindow *)this);
789 constr->height.ResetIfWin((wxWindow *)this);
790 constr->centreX.ResetIfWin((wxWindow *)this);
791 constr->centreY.ResetIfWin((wxWindow *)this);
792 }
793 delete node;
794 node = next;
795 }
796 delete m_constraintsInvolvedIn;
797 m_constraintsInvolvedIn = NULL;
798 }
799 }
800
801 void wxWindow::SetSizer(wxSizer *sizer)
802 {
803 m_windowSizer = sizer;
804 if (sizer)
805 sizer->SetSizerParent((wxWindow *)this);
806 }
807
808 /*
809 * New version
810 */
811
812 bool wxWindow::Layout()
813 {
814 if (GetConstraints())
815 {
816 int w, h;
817 GetClientSize(&w, &h);
818 GetConstraints()->width.SetValue(w);
819 GetConstraints()->height.SetValue(h);
820 }
821
822 // If top level (one sizer), evaluate the sizer's constraints.
823 if (GetSizer())
824 {
825 int noChanges;
826 GetSizer()->ResetConstraints(); // Mark all constraints as unevaluated
827 GetSizer()->LayoutPhase1(&noChanges);
828 GetSizer()->LayoutPhase2(&noChanges);
829 GetSizer()->SetConstraintSizes(); // Recursively set the real window sizes
830 return TRUE;
831 }
832 else
833 {
834 // Otherwise, evaluate child constraints
835 ResetConstraints(); // Mark all constraints as unevaluated
836 DoPhase(1); // Just one phase need if no sizers involved
837 DoPhase(2);
838 SetConstraintSizes(); // Recursively set the real window sizes
839 }
840 return TRUE;
841 }
842
843
844 // Do a phase of evaluating constraints:
845 // the default behaviour. wxSizers may do a similar
846 // thing, but also impose their own 'constraints'
847 // and order the evaluation differently.
848 bool wxWindow::LayoutPhase1(int *noChanges)
849 {
850 wxLayoutConstraints *constr = GetConstraints();
851 if (constr)
852 {
853 return constr->SatisfyConstraints((wxWindow *)this, noChanges);
854 }
855 else
856 return TRUE;
857 }
858
859 bool wxWindow::LayoutPhase2(int *noChanges)
860 {
861 *noChanges = 0;
862
863 // Layout children
864 DoPhase(1);
865 DoPhase(2);
866 return TRUE;
867 }
868
869 // Do a phase of evaluating child constraints
870 bool wxWindow::DoPhase(int phase)
871 {
872 int noIterations = 0;
873 int maxIterations = 500;
874 int noChanges = 1;
875 int noFailures = 0;
876 wxList succeeded;
877 while ((noChanges > 0) && (noIterations < maxIterations))
878 {
879 noChanges = 0;
880 noFailures = 0;
881 wxNode *node = GetChildren()->First();
882 while (node)
883 {
884 wxWindow *child = (wxWindow *)node->Data();
885 if (!child->IsKindOf(CLASSINFO(wxFrame)) && !child->IsKindOf(CLASSINFO(wxDialog)))
886 {
887 wxLayoutConstraints *constr = child->GetConstraints();
888 if (constr)
889 {
890 if (succeeded.Member(child))
891 {
892 }
893 else
894 {
895 int tempNoChanges = 0;
896 bool success = ( (phase == 1) ? child->LayoutPhase1(&tempNoChanges) : child->LayoutPhase2(&tempNoChanges) ) ;
897 noChanges += tempNoChanges;
898 if (success)
899 {
900 succeeded.Append(child);
901 }
902 }
903 }
904 }
905 node = node->Next();
906 }
907 noIterations ++;
908 }
909 return TRUE;
910 }
911
912 void wxWindow::ResetConstraints()
913 {
914 wxLayoutConstraints *constr = GetConstraints();
915 if (constr)
916 {
917 constr->left.SetDone(FALSE);
918 constr->top.SetDone(FALSE);
919 constr->right.SetDone(FALSE);
920 constr->bottom.SetDone(FALSE);
921 constr->width.SetDone(FALSE);
922 constr->height.SetDone(FALSE);
923 constr->centreX.SetDone(FALSE);
924 constr->centreY.SetDone(FALSE);
925 }
926 wxNode *node = GetChildren()->First();
927 while (node)
928 {
929 wxWindow *win = (wxWindow *)node->Data();
930 if (!win->IsKindOf(CLASSINFO(wxFrame)) && !win->IsKindOf(CLASSINFO(wxDialog)))
931 win->ResetConstraints();
932 node = node->Next();
933 }
934 }
935
936 // Need to distinguish between setting the 'fake' size for
937 // windows and sizers, and setting the real values.
938 void wxWindow::SetConstraintSizes(bool recurse)
939 {
940 wxLayoutConstraints *constr = GetConstraints();
941 if (constr && constr->left.GetDone() && constr->right.GetDone() &&
942 constr->width.GetDone() && constr->height.GetDone())
943 {
944 int x = constr->left.GetValue();
945 int y = constr->top.GetValue();
946 int w = constr->width.GetValue();
947 int h = constr->height.GetValue();
948
949 // If we don't want to resize this window, just move it...
950 if ((constr->width.GetRelationship() != wxAsIs) ||
951 (constr->height.GetRelationship() != wxAsIs))
952 {
953 // Calls Layout() recursively. AAAGH. How can we stop that.
954 // Simply take Layout() out of non-top level OnSizes.
955 SizerSetSize(x, y, w, h);
956 }
957 else
958 {
959 SizerMove(x, y);
960 }
961 }
962 else if (constr)
963 {
964 char *windowClass = this->GetClassInfo()->GetClassName();
965
966 wxString winName;
967 if (GetName() == "")
968 winName = "unnamed";
969 else
970 winName = GetName();
971 wxDebugMsg("Constraint(s) not satisfied for window of type %s, name %s:\n", (const char *)windowClass, (const char *)winName);
972 if (!constr->left.GetDone())
973 wxDebugMsg(" unsatisfied 'left' constraint.\n");
974 if (!constr->right.GetDone())
975 wxDebugMsg(" unsatisfied 'right' constraint.\n");
976 if (!constr->width.GetDone())
977 wxDebugMsg(" unsatisfied 'width' constraint.\n");
978 if (!constr->height.GetDone())
979 wxDebugMsg(" unsatisfied 'height' constraint.\n");
980 wxDebugMsg("Please check constraints: try adding AsIs() constraints.\n");
981 }
982
983 if (recurse)
984 {
985 wxNode *node = GetChildren()->First();
986 while (node)
987 {
988 wxWindow *win = (wxWindow *)node->Data();
989 if (!win->IsKindOf(CLASSINFO(wxFrame)) && !win->IsKindOf(CLASSINFO(wxDialog)))
990 win->SetConstraintSizes();
991 node = node->Next();
992 }
993 }
994 }
995
996 // This assumes that all sizers are 'on' the same
997 // window, i.e. the parent of this window.
998 void wxWindow::TransformSizerToActual(int *x, int *y) const
999 {
1000 if (!m_sizerParent || m_sizerParent->IsKindOf(CLASSINFO(wxDialog)) ||
1001 m_sizerParent->IsKindOf(CLASSINFO(wxFrame)) )
1002 return;
1003
1004 int xp, yp;
1005 m_sizerParent->GetPosition(&xp, &yp);
1006 m_sizerParent->TransformSizerToActual(&xp, &yp);
1007 *x += xp;
1008 *y += yp;
1009 }
1010
1011 void wxWindow::SizerSetSize(int x, int y, int w, int h)
1012 {
1013 int xx = x;
1014 int yy = y;
1015 TransformSizerToActual(&xx, &yy);
1016 SetSize(xx, yy, w, h);
1017 }
1018
1019 void wxWindow::SizerMove(int x, int y)
1020 {
1021 int xx = x;
1022 int yy = y;
1023 TransformSizerToActual(&xx, &yy);
1024 Move(xx, yy);
1025 }
1026
1027 // Only set the size/position of the constraint (if any)
1028 void wxWindow::SetSizeConstraint(int x, int y, int w, int h)
1029 {
1030 wxLayoutConstraints *constr = GetConstraints();
1031 if (constr)
1032 {
1033 if (x != -1)
1034 {
1035 constr->left.SetValue(x);
1036 constr->left.SetDone(TRUE);
1037 }
1038 if (y != -1)
1039 {
1040 constr->top.SetValue(y);
1041 constr->top.SetDone(TRUE);
1042 }
1043 if (w != -1)
1044 {
1045 constr->width.SetValue(w);
1046 constr->width.SetDone(TRUE);
1047 }
1048 if (h != -1)
1049 {
1050 constr->height.SetValue(h);
1051 constr->height.SetDone(TRUE);
1052 }
1053 }
1054 }
1055
1056 void wxWindow::MoveConstraint(int x, int y)
1057 {
1058 wxLayoutConstraints *constr = GetConstraints();
1059 if (constr)
1060 {
1061 if (x != -1)
1062 {
1063 constr->left.SetValue(x);
1064 constr->left.SetDone(TRUE);
1065 }
1066 if (y != -1)
1067 {
1068 constr->top.SetValue(y);
1069 constr->top.SetDone(TRUE);
1070 }
1071 }
1072 }
1073
1074 void wxWindow::GetSizeConstraint(int *w, int *h) const
1075 {
1076 wxLayoutConstraints *constr = GetConstraints();
1077 if (constr)
1078 {
1079 *w = constr->width.GetValue();
1080 *h = constr->height.GetValue();
1081 }
1082 else
1083 GetSize(w, h);
1084 }
1085
1086 void wxWindow::GetClientSizeConstraint(int *w, int *h) const
1087 {
1088 wxLayoutConstraints *constr = GetConstraints();
1089 if (constr)
1090 {
1091 *w = constr->width.GetValue();
1092 *h = constr->height.GetValue();
1093 }
1094 else
1095 GetClientSize(w, h);
1096 }
1097
1098 void wxWindow::GetPositionConstraint(int *x, int *y) const
1099 {
1100 wxLayoutConstraints *constr = GetConstraints();
1101 if (constr)
1102 {
1103 *x = constr->left.GetValue();
1104 *y = constr->top.GetValue();
1105 }
1106 else
1107 GetPosition(x, y);
1108 }
1109
1110 bool wxWindow::Close(bool force)
1111 {
1112 wxCloseEvent event(wxEVT_CLOSE_WINDOW, m_windowId);
1113 event.SetEventObject(this);
1114 #if WXWIN_COMPATIBILITY
1115 event.SetForce(force);
1116 #endif
1117 event.SetCanVeto(!force);
1118
1119 return GetEventHandler()->ProcessEvent(event);
1120 }
1121
1122 wxObject* wxWindow::GetChild(int number) const
1123 {
1124 // Return a pointer to the Nth object in the window
1125 if (!GetChildren())
1126 return(NULL) ;
1127 wxNode *node = GetChildren()->First();
1128 int n = number;
1129 while (node && n--)
1130 node = node->Next() ;
1131 if (node)
1132 {
1133 wxObject *obj = (wxObject *)node->Data();
1134 return(obj) ;
1135 }
1136 else
1137 return NULL ;
1138 }
1139
1140 void wxWindow::OnDefaultAction(wxControl *initiatingItem)
1141 {
1142 // Obsolete function
1143 }
1144
1145 void wxWindow::Clear()
1146 {
1147 wxClientDC dc(this);
1148 wxBrush brush(GetBackgroundColour(), wxSOLID);
1149 dc.SetBackground(brush);
1150 dc.Clear();
1151 }
1152
1153 // Fits the panel around the items
1154 void wxWindow::Fit()
1155 {
1156 int maxX = 0;
1157 int maxY = 0;
1158 wxNode *node = GetChildren()->First();
1159 while ( node )
1160 {
1161 wxWindow *win = (wxWindow *)node->Data();
1162 int wx, wy, ww, wh;
1163 win->GetPosition(&wx, &wy);
1164 win->GetSize(&ww, &wh);
1165 if ( wx + ww > maxX )
1166 maxX = wx + ww;
1167 if ( wy + wh > maxY )
1168 maxY = wy + wh;
1169
1170 node = node->Next();
1171 }
1172 SetClientSize(maxX + 5, maxY + 5);
1173 }
1174
1175 void wxWindow::SetValidator(const wxValidator& validator)
1176 {
1177 if ( m_windowValidator )
1178 delete m_windowValidator;
1179 m_windowValidator = validator.Clone();
1180
1181 if ( m_windowValidator )
1182 m_windowValidator->SetWindow(this) ;
1183 }
1184
1185 // Find a window by id or name
1186 wxWindow *wxWindow::FindWindow(long id)
1187 {
1188 if ( GetId() == id)
1189 return this;
1190
1191 wxNode *node = GetChildren()->First();
1192 while ( node )
1193 {
1194 wxWindow *child = (wxWindow *)node->Data();
1195 wxWindow *found = child->FindWindow(id);
1196 if ( found )
1197 return found;
1198 node = node->Next();
1199 }
1200 return NULL;
1201 }
1202
1203 wxWindow *wxWindow::FindWindow(const wxString& name)
1204 {
1205 if ( GetName() == name)
1206 return this;
1207
1208 wxNode *node = GetChildren()->First();
1209 while ( node )
1210 {
1211 wxWindow *child = (wxWindow *)node->Data();
1212 wxWindow *found = child->FindWindow(name);
1213 if ( found )
1214 return found;
1215 node = node->Next();
1216 }
1217 return NULL;
1218 }
1219
1220 void wxWindow::OnIdle(wxIdleEvent& event)
1221 {
1222 /* TODO: you may need to do something like this
1223 * if your GUI doesn't generate enter/leave events
1224
1225 // Check if we need to send a LEAVE event
1226 if (m_mouseInWindow)
1227 {
1228 POINT pt;
1229 ::GetCursorPos(&pt);
1230 if (::WindowFromPoint(pt) != (HWND) GetHWND())
1231 {
1232 // Generate a LEAVE event
1233 m_mouseInWindow = FALSE;
1234 MSWOnMouseLeave(pt.x, pt.y, 0);
1235 }
1236 }
1237 */
1238
1239 // This calls the UI-update mechanism (querying windows for
1240 // menu/toolbar/control state information)
1241 UpdateWindowUI();
1242 }
1243
1244 // Raise the window to the top of the Z order
1245 void wxWindow::Raise()
1246 {
1247 // TODO
1248 }
1249
1250 // Lower the window to the bottom of the Z order
1251 void wxWindow::Lower()
1252 {
1253 // TODO
1254 }
1255
1256 bool wxWindow::AcceptsFocus() const
1257 {
1258 return IsShown() && IsEnabled();
1259 }
1260
1261 // Update region access
1262 wxRegion wxWindow::GetUpdateRegion() const
1263 {
1264 return m_updateRegion;
1265 }
1266
1267 bool wxWindow::IsExposed(int x, int y, int w, int h) const
1268 {
1269 return (m_updateRegion.Contains(x, y, w, h) != wxOutRegion);
1270 }
1271
1272 bool wxWindow::IsExposed(const wxPoint& pt) const
1273 {
1274 return (m_updateRegion.Contains(pt) != wxOutRegion);
1275 }
1276
1277 bool wxWindow::IsExposed(const wxRect& rect) const
1278 {
1279 return (m_updateRegion.Contains(rect) != wxOutRegion);
1280 }
1281
1282