public MOTDTextBox()
        {
            InitializeComponent();
            lastFocusedBox = motdLine1;

            foreach (var colorCode in ColorCode)
            {
                var button = new Button
                {
                    Background = new SolidColorBrush(colorCode.Key),
                    Tag        = colorCode.Key,
                    Style      = FindResource("FlatSquareButton") as Style
                };
                button.Click += colorButtonClick;

                codedColorsPanel.Children.Add(button);
            }
            // check the default color
            //((RadioButton)codedColorsPanel.Children[0]).IsChecked = true;

            foreach (Button modeCheckbox in modePanel.Children)
            {
                modeCheckbox.Click += styleButtonClick;
            }

            generatedCode.TextChanged += (s, e) => onTextChanged(e);
        }
        StylisedCharacter GetCurrentCharacter(int line)
        {
            StylisedTextBox box = null;

            if (line == 1)
            {
                box = motdLine1;
            }
            else if (line == 2)
            {
                box = motdLine2;
            }

            if (box != null && box.TextLength > 0)
            {
                int charIndex = box.CaretIndex - 1;
                if (charIndex < 0)
                {
                    charIndex = 0;
                }

                return(box.GetStylisedCharacter(charIndex));
            }
            return(null);
        }
        // generate the formatted text from a given box, with the specified prefix and the last style and color
        static string generateText(
            StylisedTextBox box,
            string prefix,
            ref CharacterStyle lastStyle,
            ref Color lastColor)
        {
            // if empty return empty
            var characters = box.GetStylisedCharacters();

            if (characters.Count == 0)
            {
                return(string.Empty);
            }

            var sb = new StringBuilder();

            for (int i = 0; i < characters.Count; i++)
            {
                // if it's not the first character, update the last style
                if (i > 0)
                {
                    lastStyle = characters[i - 1].Style;
                    lastColor = characters[i - 1].Color;
                }

                // if the character color has changed
                if (lastColor != characters[i].Color)
                {
                    sb.Append(prefix);
                    sb.Append(ColorCode[characters[i].Color]);

                    // the style is reset every time the color changes
                    // so if it's not the same as the initial style, update it
                    if (characters[i].Style != initialStyle)
                    {
                        sb.Append(getFormat(prefix, characters[i].Style));
                    }
                }
                // else if the character style has changed
                else if (lastStyle != characters[i].Style)
                {
                    sb.Append(prefix);
                    sb.Append(FormatCode[CharacterStyle.Reset]);
                    if (lastColor != initialColor)
                    {
                        sb.Append(prefix);
                        sb.Append(ColorCode[characters[i].Color]);
                    }

                    sb.Append(getFormat(prefix, characters[i].Style));
                }

                sb.Append(characters[i].Character);
            }

            return(sb.ToString());
        }
        void checkBoxLength(StylisedTextBox box, bool updatePreview)
        {
            if (calculatePixelsLength(box) >= 1)
            {
                --box.TextLength;
            }

            if (updatePreview)
            {
                updatePreviewAndResult();
            }
        }
        // calculates the current stylised text box text length in pixels
        static double calculatePixelsLength(StylisedTextBox box)
        {
            double result = 0;

            var characters = box.GetStylisedCharacters();

            foreach (var character in characters)
            {
                result += getCharacterPixelSize(
                    character.Character, character.Style.HasFlag(CharacterStyle.Bold));
            }

            return(result);
        }
        // perfoms a line adjustment (adds white spaces if the box needs them) on a stylised textbox
        static void performAlignmentAdjustment(
            StylisedTextBox box,
            List <StylisedCharacter> characters,
            TextAlignment alignment)

        {
            box.SetStylisedCharacters(characters);

            double boxPixelsLength = calculatePixelsLength(box);
            double remainingPixels = maxPixelLength - boxPixelsLength;

            if (characters.Count == 0)
            {
                return;
            }

            switch (alignment)
            {
            case TextAlignment.Right:

                box.InsertText(0, getMaxAvailableStringRoundingTop(remainingPixels, ' ', false),
                               ColorCode.First().Key, CharacterStyle.Reset);

                break;

            case TextAlignment.Center:

                box.InsertText(0,
                               getMaxAvailableStringRoundingTop(remainingPixels / 2d, ' ', false),
                               ColorCode.First().Key, CharacterStyle.Reset);

                break;

            default:
                // do nothing
                break;
            }
        }
 // motd box focused or lost focus
 void motd_Focused(object sender, RoutedEventArgs e)
 {
     lastFocusedBox = (StylisedTextBox)sender;
 }