Exemple #1
0
        /**
         * Function: WidthInPixels
         *
         * Measures the string's length in pixels.
         *
         * Author: wellinthatcase
         *
         * Date: 12/29/2020
         *
         * Parameters:
         * str -       The str to act on.
         * font -      The font.
         *
         * Returns: An integer representing how many pixels the string is in length.
         */

        public static int WidthInPixels(this string str, wfFont font)
        {
            using (wfGraphics gfx = wfGraphics.FromImage(new wfBitmap(1, 1)))
            {
                return((int)gfx.MeasureString(str, font).Width);
            }
        }
        /**
         * Function: BufferEditor
         *
         * Exlusively used to edit the buffer defined in MeasureNoteName.
         *
         * Author: wellinthatcase
         *
         * Date: 12/27/2020
         *
         * Parameters:
         * str -  The string.
         *
         * Returns: A bool indicating if str was successfully appended to the buffer.
         */

        private bool EditBuffer(ref string buff, string str, int maxLen, wfFont font)
        {
            int totalSegmentLength = str.Length + buff.Length;
            int totalContentLength = maxLen - "...".WidthInPixels(font);

            return(totalSegmentLength < totalContentLength ? (buff += str).Length > 0 : false);
        }
        /**
         * Function: MeasureNoteName
         *
         * Measures the proposed text's length to see if it would fit as a note name.
         *
         * Author: wellinthatcase
         *
         * Date: 12/27/2020
         *
         * Parameters:
         * text -  The text.
         *
         * Returns: The trunacated string to fit in the note name text box, if required.
         */

        private string MeasureNoteName(string text)
        {
            wfFont font = new wfFont(
                TextEntry.FontFamily.ToString(),
                (float)TextEntry.FontSize,
                wfFontStyle.Regular,
                wfGfxUnit.Point);

            int pxMaxContentLen = (int)NoteName.ActualWidth;

            if (text.WidthInPixels(font) >= pxMaxContentLen)
            {
                int    chunkSize = 3;
                string buffer    = string.Empty;

                IEnumerable <string> segments = Enumerable.Range(0, text.Length / chunkSize)
                                                .Select(index => text.Substring(index * chunkSize, chunkSize))
                                                .TakeWhile(st => EditBuffer(ref buffer, st, pxMaxContentLen, font))
                                                .ToArray();

                text = string.Join(string.Empty, segments) + "...";
            }

            return(text);
        }
        /**
         * Function: ShowFontDialog
         *
         * Control: Logo
         *
         * Event: Click
         *
         * Shows the font dialog from the context menu.
         *
         * Author: wellinthatcase
         *
         * Date: 12/27/2020
         *
         * Parameters:
         * sender -    Source of the event.
         * e -         Routed event information.
         */

        private void ShowFontDialog(object sender, RoutedEventArgs e)
        {
            float curFontSize = (float)TextEntry.FontSize;

            Color        oldColor  = TextEntry.Foreground.ToString().AsSolidColorBrush().Color;
            wfColor      curColor  = wfColor.FromArgb(oldColor.A, oldColor.R, oldColor.G, oldColor.B);
            wfFontFamily curFamily = new wfFontFamily(TextEntry.FontFamily.ToString());
            wfFont       curFont   = new wfFont(curFamily, curFontSize);

            wfFontDialog dialog = new wfFontDialog
            {
                Font      = curFont,
                Color     = curColor,
                ShowColor = true,
            };

            if (dialog.ShowDialog() != wfDialogResult.Cancel)
            {
                byte clrAlp = dialog.Color.A;
                byte clrRed = dialog.Color.R;
                byte clrGrn = dialog.Color.G;
                byte clrBlu = dialog.Color.B;

                TextEntry.Foreground = new SolidColorBrush(Color.FromArgb(clrAlp, clrRed, clrGrn, clrBlu));
                TextEntry.FontFamily = dialog.Font.Name.IntoFontFamily();
                TextEntry.FontWeight = dialog.Font.Bold   ? FontWeights.Bold  : FontWeights.Normal;
                TextEntry.FontStyle  = dialog.Font.Italic ? FontStyles.Italic : FontStyles.Normal;
                TextEntry.FontSize   = dialog.Font.Size;
            }
        }