1 // =================================
  2 // Copyright (c) 2022 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 using System;
  7 
  8 namespace System.Screen
  9 {
 10     public class LabelCreateParams
 11     {
 12         public nothrow LabelCreateParams() : controlCreateParams()autoSize(true)text()
 13         {
 14         }
 15         public nothrow LabelCreateParams& Defaults()
 16         {
 17             return *this;
 18         }
 19         public nothrow LabelCreateParams& SetLocation(const Point& loc)
 20         {
 21             controlCreateParams.SetLocation(loc);
 22             return *this;
 23         }
 24         public nothrow LabelCreateParams& SetSize(const Size& size_)
 25         {
 26             controlCreateParams.SetSize(size_);
 27             return *this;
 28         }
 29         public nothrow LabelCreateParams& SetForeColor(ConsoleColor foreColor_)
 30         {
 31             controlCreateParams.SetForeColor(foreColor_);
 32             return *this;
 33         }
 34         public nothrow LabelCreateParams& SetBackColor(ConsoleColor backColor_)
 35         {
 36             controlCreateParams.SetBackColor(backColor_);
 37             return *this;
 38         }
 39         public nothrow LabelCreateParams& SetText(const string& text_)
 40         {
 41             text = ToUtf32(text_);
 42             return *this;
 43         }
 44         public nothrow LabelCreateParams& SetAutoSize(bool autoSize_)
 45         {
 46             autoSize = autoSize_;
 47             return *this;
 48         }
 49         public ControlCreateParams controlCreateParams;
 50         public ustring text;
 51         public bool autoSize;
 52     }
 53     
 54     public class Label : Control
 55     {
 56         public nothrow Label(LabelCreateParams& createParams) : base(createParams.controlCreateParams)autoSize(createParams.autoSize)text(createParams.text)
 57         {
 58             InvalidateGuard invalidateGuard(thisInvalidateKind.dontInvalidate);
 59             if (autoSize)
 60             {
 61                 SetSize(Size(cast<int>(text.Length())1));
 62             }
 63             if (ForeColor() == ConsoleColor.defaultColor)
 64             {
 65                 SetForeColor(ConsoleColor.black);
 66             }
 67             if (BackColor() == ConsoleColor.defaultColor)
 68             {
 69                 SetBackColor(ConsoleColor.gray);
 70             }
 71         }
 72         public override nothrow bool CanFocus() const
 73         {
 74             return false;
 75         }
 76         public void SetText(const string& text_)
 77         {
 78             ustring t = ToUtf32(text_);
 79             if (text != t)
 80             {
 81                 InvalidateGuard guard(thisInvalidateKind.invalidateIfNotDefault);
 82                 text = t;
 83                 if (autoSize)
 84                 {
 85                     SetSize(Size(cast<int>(text.Length())1));
 86                 }
 87                 OnTextChanged();
 88             }
 89         }
 90         public virtual void OnTextChanged()
 91         {
 92             textChangedEvent.Fire();
 93         }
 94         public override void OnWriteScreen(WriteScreenEventArgs& args)
 95         {
 96             base->OnWriteScreen(args);
 97             Point loc = Location();
 98             SetCursorPos(loc.xloc.y);
 99             Terminal.Out() << SetColors(ForeColor()BackColor()) << text;
100         }
101         public nothrow Event<ChangedEventHandler>& TextChangedEvent()
102         {
103             return textChangedEvent;
104         }
105         private bool autoSize;
106         private ustring text;
107         private Event<ChangedEventHandler> textChangedEvent;
108     }
109 }