1 // =================================
  2 // Copyright (c) 2024 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 ControlCreateParams& PanelControlCreateParams(ControlCreateParams& controlCreateParams)
 12     {
 13         return controlCreateParams.SetWindowClassName("System.Windows.Panel");
 14     }
 15 
 16     public class PanelCreateParams
 17     {
 18         public PanelCreateParams(ControlCreateParams& controlCreateParams_) : controlCreateParams(controlCreateParams_)
 19         {
 20         }
 21         public PanelCreateParams& Defaults()
 22         {
 23             return *this;
 24         }
 25         public ControlCreateParams& controlCreateParams;
 26     }
 27 
 28     public class Panel : ContainerControl
 29     {
 30         private enum Flags : sbyte
 31         {
 32             none = 0dontPaint = 1 << 0
 33         }
 34         public Panel(const string& windowClassNameconst string& textconst Point& locationconst Size& sizeDock dockAnchors anchors
 35             const Color& backgroundColor) : 
 36             base(windowClassNameDefaultWindowClassStyle()DefaultChildWindowStyle()DefaultExtendedWindowStyle()
 37             backgroundColortextlocationsizedockanchors)
 38         {
 39         }
 40         public Panel(const Point& locationconst Size& sizeDock dockAnchors anchors) : 
 41             this("System.Windows.Panel""panel"locationsizedockanchorsDefaultControlBackgroundColor())
 42         {
 43         }
 44         public Panel(PanelCreateParams& createParams) : base(createParams.controlCreateParams)
 45         {
 46         }
 47         [nodiscard]
 48         public override Result<bool> PrintWindowTree(int level)
 49         {
 50             LogView* log = Application.GetLogView();
 51             if (log != null)
 52             {
 53                 auto hexStringResult = ToHexString(cast<ulong>(Handle()));
 54                 if (hexStringResult.Error())
 55                 {
 56                     return Result<bool>(ErrorId(hexStringResult.GetErrorId()));
 57                 }
 58                 auto parentTextResult = ParentText();
 59                 if (parentTextResult.Error())
 60                 {
 61                     return Result<bool>(ErrorId(parentTextResult.GetErrorId()));
 62                 }
 63                 auto result = log->WriteLine(string(' 'level) + "Panel." + Text() + ".handle=" + hexStringResult.Value() + " " + parentTextResult.Value() + 
 64                     "[" + Rect(Point()GetSize()).ToString() + "]");
 65                 if (result.Error()) return result;
 66             }
 67             Component* child = Children().FirstChild();
 68             while (child != null)
 69             {
 70                 if (child is Control*)
 71                 {
 72                     Control* childControl = cast<Control*>(child);
 73                     auto result = childControl->PrintWindowTree(level + 1);
 74                     if (result.Error()) return result;
 75                 }
 76                 child = child->NextSibling();
 77             }
 78             return Result<bool>(true);
 79         }
 80         [nodiscard]
 81         protected override Result<bool> OnPaint(PaintEventArgs& args)
 82         {
 83             if (DontPaint()) return Result<bool>(false);
 84             if (Debug.Paint())
 85             {
 86                 auto locationResult = Location();
 87                 if (locationResult.Error())
 88                 {
 89                     return Result<bool>(ErrorId(locationResult.GetErrorId()));
 90                 }
 91                 Point location = locationResult.Value();
 92                 Rect r(locationGetSize());
 93                 LogView* log = Application.GetLogView();
 94                 if (log != null)
 95                 {
 96                     auto result = log->WriteLine("Panel.OnPaint: " + r.ToString());
 97                     if (result.Error()) return result;
 98                 }
 99             }
100             auto clearResult = args.graphics.Clear(BackgroundColor());
101             if (clearResult.Error())
102             {
103                 return Result<bool>(ErrorId(clearResult.GetErrorId()));
104             }
105             return base->OnPaint(args);
106         }
107         public inline bool DontPaint() const
108         {
109             return (flags & Flags.dontPaint) != Flags.none;
110         }
111         public void SetDontPaint()
112         {
113             flags = cast<Flags>(flags | Flags.dontPaint);
114         }
115         public void ResetDontPaint()
116         {
117             flags = cast<Flags>(flags & ~Flags.dontPaint);
118         }
119         private Flags flags;
120     }