+ // Make sure other windows know they're part of a 'meaningful relationship'
+ if ( m_constraints->left.GetOtherWindow() && (m_constraints->left.GetOtherWindow() != this) )
+ m_constraints->left.GetOtherWindow()->AddConstraintReference(this);
+ if ( m_constraints->top.GetOtherWindow() && (m_constraints->top.GetOtherWindow() != this) )
+ m_constraints->top.GetOtherWindow()->AddConstraintReference(this);
+ if ( m_constraints->right.GetOtherWindow() && (m_constraints->right.GetOtherWindow() != this) )
+ m_constraints->right.GetOtherWindow()->AddConstraintReference(this);
+ if ( m_constraints->bottom.GetOtherWindow() && (m_constraints->bottom.GetOtherWindow() != this) )
+ m_constraints->bottom.GetOtherWindow()->AddConstraintReference(this);
+ if ( m_constraints->width.GetOtherWindow() && (m_constraints->width.GetOtherWindow() != this) )
+ m_constraints->width.GetOtherWindow()->AddConstraintReference(this);
+ if ( m_constraints->height.GetOtherWindow() && (m_constraints->height.GetOtherWindow() != this) )
+ m_constraints->height.GetOtherWindow()->AddConstraintReference(this);
+ if ( m_constraints->centreX.GetOtherWindow() && (m_constraints->centreX.GetOtherWindow() != this) )
+ m_constraints->centreX.GetOtherWindow()->AddConstraintReference(this);
+ if ( m_constraints->centreY.GetOtherWindow() && (m_constraints->centreY.GetOtherWindow() != this) )
+ m_constraints->centreY.GetOtherWindow()->AddConstraintReference(this);
+ }
+}
+
+// This removes any dangling pointers to this window in other windows'
+// constraintsInvolvedIn lists.
+void wxWindowBase::UnsetConstraints(wxLayoutConstraints *c)
+{
+ if ( c )
+ {
+ if ( c->left.GetOtherWindow() && (c->left.GetOtherWindow() != this) )
+ c->left.GetOtherWindow()->RemoveConstraintReference(this);
+ if ( c->top.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
+ c->top.GetOtherWindow()->RemoveConstraintReference(this);
+ if ( c->right.GetOtherWindow() && (c->right.GetOtherWindow() != this) )
+ c->right.GetOtherWindow()->RemoveConstraintReference(this);
+ if ( c->bottom.GetOtherWindow() && (c->bottom.GetOtherWindow() != this) )
+ c->bottom.GetOtherWindow()->RemoveConstraintReference(this);
+ if ( c->width.GetOtherWindow() && (c->width.GetOtherWindow() != this) )
+ c->width.GetOtherWindow()->RemoveConstraintReference(this);
+ if ( c->height.GetOtherWindow() && (c->height.GetOtherWindow() != this) )
+ c->height.GetOtherWindow()->RemoveConstraintReference(this);
+ if ( c->centreX.GetOtherWindow() && (c->centreX.GetOtherWindow() != this) )
+ c->centreX.GetOtherWindow()->RemoveConstraintReference(this);
+ if ( c->centreY.GetOtherWindow() && (c->centreY.GetOtherWindow() != this) )
+ c->centreY.GetOtherWindow()->RemoveConstraintReference(this);
+ }
+}
+
+// Back-pointer to other windows we're involved with, so if we delete this
+// window, we must delete any constraints we're involved with.
+void wxWindowBase::AddConstraintReference(wxWindowBase *otherWin)
+{
+ if ( !m_constraintsInvolvedIn )
+ m_constraintsInvolvedIn = new wxWindowList;
+ if ( !m_constraintsInvolvedIn->Find((wxWindow *)otherWin) )
+ m_constraintsInvolvedIn->Append((wxWindow *)otherWin);
+}
+
+// REMOVE back-pointer to other windows we're involved with.
+void wxWindowBase::RemoveConstraintReference(wxWindowBase *otherWin)
+{
+ if ( m_constraintsInvolvedIn )
+ m_constraintsInvolvedIn->DeleteObject((wxWindow *)otherWin);
+}
+
+// Reset any constraints that mention this window
+void wxWindowBase::DeleteRelatedConstraints()
+{
+ if ( m_constraintsInvolvedIn )
+ {
+ wxWindowList::compatibility_iterator node = m_constraintsInvolvedIn->GetFirst();
+ while (node)
+ {
+ wxWindow *win = node->GetData();
+ wxLayoutConstraints *constr = win->GetConstraints();
+
+ // Reset any constraints involving this window
+ if ( constr )
+ {
+ constr->left.ResetIfWin(this);
+ constr->top.ResetIfWin(this);
+ constr->right.ResetIfWin(this);
+ constr->bottom.ResetIfWin(this);
+ constr->width.ResetIfWin(this);
+ constr->height.ResetIfWin(this);
+ constr->centreX.ResetIfWin(this);
+ constr->centreY.ResetIfWin(this);
+ }
+
+ wxWindowList::compatibility_iterator next = node->GetNext();
+ m_constraintsInvolvedIn->Erase(node);
+ node = next;
+ }
+
+ wxDELETE(m_constraintsInvolvedIn);
+ }
+}
+
+#endif // wxUSE_CONSTRAINTS
+
+void wxWindowBase::SetSizer(wxSizer *sizer, bool deleteOld)
+{
+ if ( sizer == m_windowSizer)
+ return;
+
+ if ( m_windowSizer )
+ {
+ m_windowSizer->SetContainingWindow(NULL);
+
+ if ( deleteOld )
+ delete m_windowSizer;
+ }
+
+ m_windowSizer = sizer;
+ if ( m_windowSizer )
+ {
+ m_windowSizer->SetContainingWindow((wxWindow *)this);
+ }
+
+ SetAutoLayout(m_windowSizer != NULL);
+}
+
+void wxWindowBase::SetSizerAndFit(wxSizer *sizer, bool deleteOld)
+{
+ SetSizer( sizer, deleteOld );
+ sizer->SetSizeHints( (wxWindow*) this );
+}
+
+
+void wxWindowBase::SetContainingSizer(wxSizer* sizer)
+{
+ // adding a window to a sizer twice is going to result in fatal and
+ // hard to debug problems later because when deleting the second
+ // associated wxSizerItem we're going to dereference a dangling
+ // pointer; so try to detect this as early as possible
+ wxASSERT_MSG( !sizer || m_containingSizer != sizer,
+ wxT("Adding a window to the same sizer twice?") );
+
+ m_containingSizer = sizer;
+}
+
+#if wxUSE_CONSTRAINTS
+
+void wxWindowBase::SatisfyConstraints()
+{
+ wxLayoutConstraints *constr = GetConstraints();
+ bool wasOk = constr && constr->AreSatisfied();
+
+ ResetConstraints(); // Mark all constraints as unevaluated
+
+ int noChanges = 1;
+
+ // if we're a top level panel (i.e. our parent is frame/dialog), our
+ // own constraints will never be satisfied any more unless we do it
+ // here
+ if ( wasOk )
+ {
+ while ( noChanges > 0 )
+ {
+ LayoutPhase1(&noChanges);
+ }
+ }
+
+ LayoutPhase2(&noChanges);
+}
+
+#endif // wxUSE_CONSTRAINTS
+
+bool wxWindowBase::Layout()
+{
+ // If there is a sizer, use it instead of the constraints
+ if ( GetSizer() )
+ {
+ int w = 0, h = 0;
+ GetVirtualSize(&w, &h);
+ GetSizer()->SetDimension( 0, 0, w, h );
+ }
+#if wxUSE_CONSTRAINTS
+ else
+ {
+ SatisfyConstraints(); // Find the right constraints values
+ SetConstraintSizes(); // Recursively set the real window sizes
+ }
+#endif
+
+ return true;
+}
+
+void wxWindowBase::InternalOnSize(wxSizeEvent& event)
+{
+ if ( GetAutoLayout() )
+ Layout();
+
+ event.Skip();
+}
+
+#if wxUSE_CONSTRAINTS
+
+// first phase of the constraints evaluation: set our own constraints
+bool wxWindowBase::LayoutPhase1(int *noChanges)
+{
+ wxLayoutConstraints *constr = GetConstraints();
+
+ return !constr || constr->SatisfyConstraints(this, noChanges);
+}
+
+// second phase: set the constraints for our children
+bool wxWindowBase::LayoutPhase2(int *noChanges)
+{
+ *noChanges = 0;
+
+ // Layout children
+ DoPhase(1);
+
+ // Layout grand children
+ DoPhase(2);
+
+ return true;
+}
+
+// Do a phase of evaluating child constraints
+bool wxWindowBase::DoPhase(int phase)
+{
+ // the list containing the children for which the constraints are already
+ // set correctly
+ wxWindowList succeeded;
+
+ // the max number of iterations we loop before concluding that we can't set
+ // the constraints
+ static const int maxIterations = 500;
+
+ for ( int noIterations = 0; noIterations < maxIterations; noIterations++ )
+ {
+ int noChanges = 0;
+
+ // loop over all children setting their constraints
+ for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
+ node;
+ node = node->GetNext() )
+ {
+ wxWindow *child = node->GetData();
+ if ( child->IsTopLevel() )
+ {
+ // top level children are not inside our client area
+ continue;
+ }
+
+ if ( !child->GetConstraints() || succeeded.Find(child) )
+ {
+ // this one is either already ok or nothing we can do about it
+ continue;
+ }
+
+ int tempNoChanges = 0;
+ bool success = phase == 1 ? child->LayoutPhase1(&tempNoChanges)
+ : child->LayoutPhase2(&tempNoChanges);
+ noChanges += tempNoChanges;
+
+ if ( success )
+ {
+ succeeded.Append(child);
+ }
+ }
+
+ if ( !noChanges )
+ {
+ // constraints are set
+ break;
+ }
+ }
+
+ return true;
+}
+
+void wxWindowBase::ResetConstraints()
+{
+ wxLayoutConstraints *constr = GetConstraints();
+ if ( constr )
+ {
+ constr->left.SetDone(false);
+ constr->top.SetDone(false);
+ constr->right.SetDone(false);
+ constr->bottom.SetDone(false);
+ constr->width.SetDone(false);
+ constr->height.SetDone(false);
+ constr->centreX.SetDone(false);
+ constr->centreY.SetDone(false);
+ }
+
+ wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
+ while (node)
+ {
+ wxWindow *win = node->GetData();
+ if ( !win->IsTopLevel() )
+ win->ResetConstraints();
+ node = node->GetNext();
+ }
+}
+
+// Need to distinguish between setting the 'fake' size for windows and sizers,
+// and setting the real values.
+void wxWindowBase::SetConstraintSizes(bool recurse)
+{
+ wxLayoutConstraints *constr = GetConstraints();
+ if ( constr && constr->AreSatisfied() )
+ {
+ int x = constr->left.GetValue();
+ int y = constr->top.GetValue();
+ int w = constr->width.GetValue();
+ int h = constr->height.GetValue();
+
+ if ( (constr->width.GetRelationship() != wxAsIs ) ||
+ (constr->height.GetRelationship() != wxAsIs) )
+ {
+ // We really shouldn't set negative sizes for the windows so make
+ // them at least of 1*1 size
+ SetSize(x, y, w > 0 ? w : 1, h > 0 ? h : 1);
+ }
+ else
+ {
+ // If we don't want to resize this window, just move it...
+ Move(x, y);
+ }
+ }
+ else if ( constr )
+ {
+ wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
+ GetClassInfo()->GetClassName(),
+ GetName().c_str());
+ }
+
+ if ( recurse )
+ {
+ wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
+ while (node)
+ {
+ wxWindow *win = node->GetData();
+ if ( !win->IsTopLevel() && win->GetConstraints() )
+ win->SetConstraintSizes();
+ node = node->GetNext();
+ }
+ }
+}
+
+// Only set the size/position of the constraint (if any)
+void wxWindowBase::SetSizeConstraint(int x, int y, int w, int h)
+{
+ wxLayoutConstraints *constr = GetConstraints();
+ if ( constr )
+ {
+ if ( x != wxDefaultCoord )
+ {
+ constr->left.SetValue(x);
+ constr->left.SetDone(true);
+ }
+ if ( y != wxDefaultCoord )
+ {
+ constr->top.SetValue(y);
+ constr->top.SetDone(true);
+ }
+ if ( w != wxDefaultCoord )
+ {
+ constr->width.SetValue(w);
+ constr->width.SetDone(true);
+ }
+ if ( h != wxDefaultCoord )
+ {
+ constr->height.SetValue(h);
+ constr->height.SetDone(true);
+ }
+ }
+}
+
+void wxWindowBase::MoveConstraint(int x, int y)
+{
+ wxLayoutConstraints *constr = GetConstraints();
+ if ( constr )
+ {
+ if ( x != wxDefaultCoord )
+ {
+ constr->left.SetValue(x);
+ constr->left.SetDone(true);
+ }
+ if ( y != wxDefaultCoord )
+ {
+ constr->top.SetValue(y);
+ constr->top.SetDone(true);
+ }
+ }
+}
+
+void wxWindowBase::GetSizeConstraint(int *w, int *h) const
+{
+ wxLayoutConstraints *constr = GetConstraints();
+ if ( constr )
+ {
+ *w = constr->width.GetValue();
+ *h = constr->height.GetValue();
+ }
+ else
+ GetSize(w, h);
+}
+
+void wxWindowBase::GetClientSizeConstraint(int *w, int *h) const
+{
+ wxLayoutConstraints *constr = GetConstraints();
+ if ( constr )
+ {
+ *w = constr->width.GetValue();
+ *h = constr->height.GetValue();
+ }
+ else
+ GetClientSize(w, h);
+}
+
+void wxWindowBase::GetPositionConstraint(int *x, int *y) const
+{
+ wxLayoutConstraints *constr = GetConstraints();
+ if ( constr )
+ {
+ *x = constr->left.GetValue();
+ *y = constr->top.GetValue();
+ }
+ else
+ GetPosition(x, y);
+}
+
+#endif // wxUSE_CONSTRAINTS
+
+void wxWindowBase::AdjustForParentClientOrigin(int& x, int& y, int sizeFlags) const
+{
+ wxWindow *parent = GetParent();
+ if ( !(sizeFlags & wxSIZE_NO_ADJUSTMENTS) && parent )
+ {
+ wxPoint pt(parent->GetClientAreaOrigin());
+ x += pt.x;
+ y += pt.y;
+ }
+}
+
+// ----------------------------------------------------------------------------
+// Update UI processing
+// ----------------------------------------------------------------------------
+
+void wxWindowBase::UpdateWindowUI(long flags)
+{
+ wxUpdateUIEvent event(GetId());
+ event.SetEventObject(this);
+
+ if ( GetEventHandler()->ProcessEvent(event) )
+ {
+ DoUpdateWindowUI(event);
+ }
+
+ if (flags & wxUPDATE_UI_RECURSE)
+ {
+ wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
+ while (node)
+ {
+ wxWindow* child = (wxWindow*) node->GetData();
+ child->UpdateWindowUI(flags);
+ node = node->GetNext();
+ }
+ }
+}
+
+// do the window-specific processing after processing the update event
+void wxWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
+{
+ if ( event.GetSetEnabled() )
+ Enable(event.GetEnabled());
+
+ if ( event.GetSetShown() )
+ Show(event.GetShown());
+}
+
+// ----------------------------------------------------------------------------
+// Idle processing
+// ----------------------------------------------------------------------------
+
+// Send idle event to window and all subwindows
+bool wxWindowBase::SendIdleEvents(wxIdleEvent& event)
+{
+ bool needMore = false;
+
+ OnInternalIdle();
+
+ // should we send idle event to this window?
+ if (wxIdleEvent::GetMode() == wxIDLE_PROCESS_ALL ||
+ HasExtraStyle(wxWS_EX_PROCESS_IDLE))
+ {
+ event.SetEventObject(this);
+ HandleWindowEvent(event);
+
+ if (event.MoreRequested())
+ needMore = true;
+ }
+ wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
+ for (; node; node = node->GetNext())
+ {
+ wxWindow* child = node->GetData();
+ if (child->SendIdleEvents(event))
+ needMore = true;
+ }
+
+ return needMore;
+}
+
+void wxWindowBase::OnInternalIdle()
+{
+ if ( wxUpdateUIEvent::CanUpdate(this) )
+ UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
+}
+
+// ----------------------------------------------------------------------------
+// dialog units translations
+// ----------------------------------------------------------------------------
+
+// Windows' computes dialog units using average character width over upper-
+// and lower-case ASCII alphabet and not using the average character width
+// metadata stored in the font; see
+// http://support.microsoft.com/default.aspx/kb/145994 for detailed discussion.
+// It's important that we perform the conversion in identical way, because
+// dialog units natively exist only on Windows and Windows HIG is expressed
+// using them.
+wxSize wxWindowBase::GetDlgUnitBase() const
+{
+ const wxWindowBase * const parent = wxGetTopLevelParent((wxWindow*)this);
+
+ wxCHECK_MSG( parent, wxDefaultSize, wxS("Must have TLW parent") );
+
+ if ( !parent->m_font.IsOk() )
+ {
+ // Default GUI font is used. This is the most common case, so
+ // cache the results.
+ static wxSize s_defFontSize;
+ if ( s_defFontSize.x == 0 )
+ s_defFontSize = wxPrivate::GetAverageASCIILetterSize(*parent);
+ return s_defFontSize;
+ }
+ else
+ {
+ // Custom font, we always need to compute the result
+ return wxPrivate::GetAverageASCIILetterSize(*parent);
+ }
+}
+
+wxPoint wxWindowBase::ConvertPixelsToDialog(const wxPoint& pt) const
+{
+ const wxSize base = GetDlgUnitBase();
+
+ // NB: wxMulDivInt32() is used, because it correctly rounds the result
+
+ wxPoint pt2 = wxDefaultPosition;
+ if (pt.x != wxDefaultCoord)
+ pt2.x = wxMulDivInt32(pt.x, 4, base.x);
+ if (pt.y != wxDefaultCoord)
+ pt2.y = wxMulDivInt32(pt.y, 8, base.y);
+
+ return pt2;
+}
+
+wxPoint wxWindowBase::ConvertDialogToPixels(const wxPoint& pt) const
+{
+ const wxSize base = GetDlgUnitBase();
+
+ wxPoint pt2 = wxDefaultPosition;
+ if (pt.x != wxDefaultCoord)
+ pt2.x = wxMulDivInt32(pt.x, base.x, 4);
+ if (pt.y != wxDefaultCoord)
+ pt2.y = wxMulDivInt32(pt.y, base.y, 8);
+
+ return pt2;
+}
+
+// ----------------------------------------------------------------------------
+// event handlers
+// ----------------------------------------------------------------------------
+
+// propagate the colour change event to the subwindows
+void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
+{
+ wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
+ while ( node )
+ {
+ // Only propagate to non-top-level windows
+ wxWindow *win = node->GetData();
+ if ( !win->IsTopLevel() )
+ {
+ wxSysColourChangedEvent event2;
+ event2.SetEventObject(win);
+ win->GetEventHandler()->ProcessEvent(event2);
+ }
+
+ node = node->GetNext();
+ }
+
+ Refresh();
+}
+
+// the default action is to populate dialog with data when it's created,
+// and nudge the UI into displaying itself correctly in case
+// we've turned the wxUpdateUIEvents frequency down low.
+void wxWindowBase::OnInitDialog( wxInitDialogEvent &WXUNUSED(event) )
+{
+ TransferDataToWindow();
+
+ // Update the UI at this point
+ UpdateWindowUI(wxUPDATE_UI_RECURSE);
+}
+
+// ----------------------------------------------------------------------------
+// menu-related functions
+// ----------------------------------------------------------------------------
+
+#if wxUSE_MENUS
+
+bool wxWindowBase::PopupMenu(wxMenu *menu, int x, int y)
+{
+ wxCHECK_MSG( menu, false, "can't popup NULL menu" );
+
+ wxMenuInvokingWindowSetter
+ setInvokingWin(*menu, static_cast<wxWindow *>(this));
+
+ wxCurrentPopupMenu = menu;
+ const bool rc = DoPopupMenu(menu, x, y);
+ wxCurrentPopupMenu = NULL;
+
+ return rc;
+}
+
+// this is used to pass the id of the selected item from the menu event handler
+// to the main function itself
+//
+// it's ok to use a global here as there can be at most one popup menu shown at
+// any time
+static int gs_popupMenuSelection = wxID_NONE;
+
+void wxWindowBase::InternalOnPopupMenu(wxCommandEvent& event)
+{
+ // store the id in a global variable where we'll retrieve it from later
+ gs_popupMenuSelection = event.GetId();
+}
+
+void wxWindowBase::InternalOnPopupMenuUpdate(wxUpdateUIEvent& WXUNUSED(event))
+{
+ // nothing to do but do not skip it
+}
+
+int
+wxWindowBase::DoGetPopupMenuSelectionFromUser(wxMenu& menu, int x, int y)
+{
+ gs_popupMenuSelection = wxID_NONE;
+
+ Connect(wxEVT_COMMAND_MENU_SELECTED,
+ wxCommandEventHandler(wxWindowBase::InternalOnPopupMenu),
+ NULL,
+ this);
+
+ // it is common to construct the menu passed to this function dynamically
+ // using some fixed range of ids which could clash with the ids used
+ // elsewhere in the program, which could result in some menu items being
+ // unintentionally disabled or otherwise modified by update UI handlers
+ // elsewhere in the program code and this is difficult to avoid in the
+ // program itself, so instead we just temporarily suspend UI updating while
+ // this menu is shown
+ Connect(wxEVT_UPDATE_UI,
+ wxUpdateUIEventHandler(wxWindowBase::InternalOnPopupMenuUpdate),
+ NULL,
+ this);
+
+ PopupMenu(&menu, x, y);
+
+ Disconnect(wxEVT_UPDATE_UI,
+ wxUpdateUIEventHandler(wxWindowBase::InternalOnPopupMenuUpdate),
+ NULL,
+ this);
+ Disconnect(wxEVT_COMMAND_MENU_SELECTED,
+ wxCommandEventHandler(wxWindowBase::InternalOnPopupMenu),
+ NULL,
+ this);
+
+ return gs_popupMenuSelection;
+}
+
+#endif // wxUSE_MENUS
+
+// methods for drawing the sizers in a visible way: this is currently only
+// enabled for "full debug" builds with wxDEBUG_LEVEL==2 as it doesn't work
+// that well and also because we don't want to leave it enabled in default
+// builds used for production
+#if wxDEBUG_LEVEL > 1
+
+static void DrawSizers(wxWindowBase *win);
+
+static void DrawBorder(wxWindowBase *win, const wxRect& rect, bool fill, const wxPen* pen)
+{
+ wxClientDC dc((wxWindow *)win);
+ dc.SetPen(*pen);
+ dc.SetBrush(fill ? wxBrush(pen->GetColour(), wxBRUSHSTYLE_CROSSDIAG_HATCH) :
+ *wxTRANSPARENT_BRUSH);
+ dc.DrawRectangle(rect.Deflate(1, 1));
+}
+
+static void DrawSizer(wxWindowBase *win, wxSizer *sizer)
+{
+ const wxSizerItemList& items = sizer->GetChildren();
+ for ( wxSizerItemList::const_iterator i = items.begin(),
+ end = items.end();
+ i != end;
+ ++i )
+ {
+ wxSizerItem *item = *i;
+ if ( item->IsSizer() )
+ {
+ DrawBorder(win, item->GetRect().Deflate(2), false, wxRED_PEN);
+ DrawSizer(win, item->GetSizer());
+ }
+ else if ( item->IsSpacer() )
+ {
+ DrawBorder(win, item->GetRect().Deflate(2), true, wxBLUE_PEN);
+ }
+ else if ( item->IsWindow() )
+ {
+ DrawSizers(item->GetWindow());
+ }
+ else
+ wxFAIL_MSG("inconsistent wxSizerItem status!");