2009-12-01 Jeffrey Stedfast <fejj@novell.com>
[moon.git] / src / usercontrol.cpp
blobdbc6fdedccde769f11c2a7a171be55b9b29b7a81
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(GetDeployment (), 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(GetDeployment (), 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 Thickness border = *GetPadding () + *GetBorderThickness ();
74 // Get the desired size of our child, and include any margins we set
75 VisualTreeWalker walker = VisualTreeWalker (this);
76 while (UIElement *child = walker.Step ()) {
77 child->Measure (availableSize.GrowBy (-border));
78 desired = child->GetDesiredSize ();
81 desired = desired.GrowBy (border);
83 return desired;
86 Size
87 UserControl::ArrangeOverride (Size finalSize)
89 Thickness border = *GetPadding () + *GetBorderThickness ();
91 Size arranged = finalSize;
93 VisualTreeWalker walker = VisualTreeWalker (this);
94 while (UIElement *child = walker.Step ()) {
95 Size desired = child->GetDesiredSize ();
96 Rect childRect (0,0,finalSize.width,finalSize.height);
98 childRect = childRect.GrowBy (-border);
100 child->Arrange (childRect);
102 arranged = Size (childRect.width, childRect.height).GrowBy (border);
104 return arranged;