1 // =================================
  2 // Copyright (c) 2021 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 using System;
  7 using System.Collections;
  8 
  9 namespace System.Windows
 10 {
 11     public inline nothrow int DefaultControlPadding()
 12     {
 13         return 4;
 14     }
 15 
 16     public nothrow ControlCreateParams& PaddedControlControlCreateParams(ControlCreateParams& controlCreateParamsControl* child)
 17     {
 18         return controlCreateParams.SetWindowClassName("System.Windows.PaddedControl").SetBackgroundColor(child->BackgroundColor());
 19     }
 20 
 21     public class PaddedControlCreateParams
 22     {
 23         public nothrow PaddedControlCreateParams(ControlCreateParams& controlCreateParams_Control* child_) : 
 24             controlCreateParams(controlCreateParams_)
 25             child(child_)
 26             padding(Padding(DefaultControlPadding()DefaultControlPadding()DefaultControlPadding()DefaultControlPadding()))
 27         {
 28         }
 29         public nothrow PaddedControlCreateParams& Defaults()
 30         {
 31             return *this;
 32         }
 33         public nothrow PaddedControlCreateParams& SetPadding(const Padding& padding_)
 34         {
 35             padding = padding_;
 36             return *this;
 37         }
 38         public ControlCreateParams& controlCreateParams;
 39         public Control* child;
 40         public Padding padding;
 41     }
 42 
 43     public class PaddedControl : Control
 44     {
 45         public PaddedControl(Control* child_const Padding& padding_const Point& locationconst Size& sizeDock dockAnchors anchors) : 
 46             base("System.Windows.PaddedControl"DefaultWindowClassStyle()DefaultChildWindowStyle()DefaultExtendedWindowStyle()
 47             child_->BackgroundColor()"paddedControl"locationsizedockanchors)container(this)child(child_)padding(padding_)
 48         {
 49             container.AddChild(child);
 50             SetChildPos();
 51         }
 52         public PaddedControl(Control* child) : this(childPadding(DefaultControlPadding()DefaultControlPadding()DefaultControlPadding()DefaultControlPadding())
 53             Point()Size()Dock.noneAnchors.none)
 54         {
 55         }
 56         public PaddedControl(PaddedControlCreateParams& createParams) : 
 57             base(createParams.controlCreateParams)
 58             container(this)
 59             child(createParams.child)
 60             padding(createParams.padding)
 61         {
 62             container.AddChild(child);
 63             SetChildPos();
 64         }
 65         protected override nothrow bool IsDecoratorControl() const
 66         {
 67             return true;
 68         }
 69         public override nothrow Padding DefaultPadding() const
 70         {
 71             return padding;
 72         }
 73         protected override void OnLocationChanged()
 74         {
 75             base->OnLocationChanged();
 76             SetChildPos();
 77         }
 78         protected override void OnSizeChanged(uint windowState)
 79         {
 80             base->OnSizeChanged(windowState);
 81             SetChildPos();
 82         }
 83         protected override void OnChildContentChanged(ControlEventArgs& args)
 84         {
 85             base->OnChildContentChanged(args);
 86             Control* parentControl = ParentControl();
 87             if (parentControl != null)
 88             {
 89                 parentControl->OnChildContentChanged(args);
 90             }
 91         }
 92         protected override void OnChildContentLocationChanged(ControlEventArgs& args)
 93         {
 94             base->OnChildContentLocationChanged(args);
 95             Control* parentControl = ParentControl();
 96             if (parentControl != null)
 97             {
 98                 parentControl->OnChildContentLocationChanged(args);
 99             }
100         }
101         protected override void OnChildContentSizeChanged(ControlEventArgs& args)
102         {
103             base->OnChildContentSizeChanged(args);
104             Control* parentControl = ParentControl();
105             if (parentControl != null)
106             {
107                 parentControl->OnChildContentSizeChanged(args);
108             }
109         }
110         protected override void OnChildGotFocus(ControlEventArgs& args)
111         {
112             base->OnChildGotFocus(args);
113             Control* parentControl = ParentControl();
114             if (parentControl != null)
115             {
116                 parentControl->OnChildGotFocus(args);
117             }
118         }
119         protected override void OnChildLostFocus(ControlEventArgs& args)
120         {
121             base->OnChildLostFocus(args);
122             Control* parentControl = ParentControl();
123             if (parentControl != null)
124             {
125                 parentControl->OnChildLostFocus(args);
126             }
127         }
128         protected override void OnPaint(PaintEventArgs& args)
129         {
130             args.graphics.Clear(BackgroundColor());
131             base->OnPaint(args);
132         }
133         internal override nothrow Control* GetFirstEnabledTabStopControl() const
134         {
135             return child->GetFirstEnabledTabStopControl();
136         }
137         internal override nothrow Control* GetLastEnabledTabStopControl() const
138         {
139             return child->GetLastEnabledTabStopControl();
140         }
141         private void SetChildPos()
142         {
143             Point loc;
144             Size size = GetSize();
145             Rect childRect(locsize);
146             childRect.location.x = loc.x + padding.left;
147             childRect.location.y = loc.x + padding.top;
148             childRect.size.w = size.w - padding.Horizontal();
149             childRect.size.h = size.h - padding.Vertical();
150             child->SetLocation(childRect.location);
151             child->SetSize(childRect.size);
152         }
153         private Container container;
154         private Control* child;
155         private Padding padding;
156     }
157 }