2009-10-09 Chris Toshok <toshok@ximian.com>
[moon.git] / src / usercontrol.cpp
blob0be087a24f91a590434ab69ca755c3b9db5f877f
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * usercontrol.cpp:
5 * Copyright 2007 Novell, Inc. (http://www.novell.com)
7 * See the LICENSE file included with the distribution for details.
8 *
9 */
11 #include <config.h>
13 #include "usercontrol.h"
14 #include "collection.h"
15 #include "runtime.h"
17 UserControl::UserControl ()
19 SetObjectType (Type::USERCONTROL);
20 SetIsTabStop (false);
23 UserControl::~UserControl ()
27 UIElement *
28 user_control_get_content (UserControl *user_control)
30 Value* v =user_control-> GetValue (UserControl::ContentProperty);
31 if (!v)
32 return NULL;
33 return v->AsUIElement ();
36 void
37 UserControl::OnPropertyChanged (PropertyChangedEventArgs *args, MoonError *error)
39 if (args->GetProperty ()->GetOwnerType() != Type::USERCONTROL) {
40 Control::OnPropertyChanged (args, error);
41 return;
44 if (args->GetId () == UserControl::ContentProperty){
45 if (args->GetOldValue()) {
46 if (args->GetOldValue()->Is(Type::FRAMEWORKELEMENT)) {
47 args->GetOldValue()->AsFrameworkElement()->SetLogicalParent (NULL, error);
48 if (error->number)
49 return;
51 ElementRemoved (args->GetOldValue()->AsUIElement ());
53 if (args->GetNewValue()) {
54 if (args->GetNewValue()->Is(Type::FRAMEWORKELEMENT)) {
55 args->GetNewValue()->AsFrameworkElement()->SetLogicalParent (this, error);
56 if (error->number)
57 return;
59 ElementAdded (args->GetNewValue()->AsUIElement ());
62 UpdateBounds ();
64 NotifyListenersOfPropertyChange (args, error);
67 Size
68 UserControl::MeasureOverride (Size availableSize)
70 Size desired = Size (0,0);
72 availableSize = ApplySizeConstraints (availableSize);
74 Thickness border = *GetPadding () + *GetBorderThickness ();
76 // Get the desired size of our child, and include any margins we set
77 VisualTreeWalker walker = VisualTreeWalker (this);
78 while (UIElement *child = walker.Step ()) {
79 if (child->GetVisibility () != VisibilityVisible)
80 continue;
82 child->Measure (availableSize.GrowBy (-border));
83 desired = child->GetDesiredSize ();
86 desired = desired.GrowBy (border);
88 return desired;
91 Size
92 UserControl::ArrangeOverride (Size finalSize)
94 Thickness border = *GetPadding () + *GetBorderThickness ();
96 finalSize = ApplySizeConstraints (finalSize);
98 Size arranged = finalSize;
100 VisualTreeWalker walker = VisualTreeWalker (this);
101 while (UIElement *child = walker.Step ()) {
102 if (child->GetVisibility () != VisibilityVisible)
103 continue;
105 Size desired = child->GetDesiredSize ();
106 Rect childRect (0,0,finalSize.width,finalSize.height);
108 childRect = childRect.GrowBy (-border);
110 child->Arrange (childRect);
111 arranged = child->GetRenderSize ();
113 arranged = arranged.GrowBy (border);
115 arranged = arranged.Max (finalSize);
118 return arranged;