Beispiel #1
0
        private void CreateLinkPieces()
        {
            if (Text.Length == 0)
            {
                SetStyle(ControlStyles.Selectable, false);
                TabStop          = false;
                link_area.Start  = 0;
                link_area.Length = 0;
                return;
            }

            if (Links.Count == 1 && Links[0].Start == 0 && Links[0].Length == -1)
            {
                Links[0].Length = Text.Length;
            }

            SortLinks();

            // Set the LinkArea values based on first link.
            if (Links.Count > 0)
            {
                link_area.Start  = Links[0].Start;
                link_area.Length = Links[0].Length;
            }
            else
            {
                link_area.Start  = 0;
                link_area.Length = 0;
            }

            TabStop = (LinkArea.Length > 0);
            SetStyle(ControlStyles.Selectable, TabStop);

            /* don't bother doing the rest if our handle hasn't been created */
            if (!IsHandleCreated)
            {
                return;
            }

            ArrayList pieces_list = new ArrayList();

            int current_end = 0;

            for (int l = 0; l < sorted_links.Length; l++)
            {
                int new_link_start = sorted_links[l].Start;

                if (new_link_start > current_end)
                {
                    /* create/push a piece
                     * containing the text between
                     * the previous/new link */
                    ArrayList text_pieces = CreatePiecesFromText(current_end, new_link_start - current_end, null);
                    pieces_list.AddRange(text_pieces);
                }

                /* now create a group of pieces for
                 * the new link (split up by \n's) */
                ArrayList link_pieces = CreatePiecesFromText(new_link_start, sorted_links[l].Length, sorted_links[l]);
                pieces_list.AddRange(link_pieces);
                sorted_links[l].pieces.AddRange(link_pieces);

                current_end = sorted_links[l].Start + sorted_links[l].Length;
            }
            if (current_end < Text.Length)
            {
                ArrayList text_pieces = CreatePiecesFromText(current_end, Text.Length - current_end, null);
                pieces_list.AddRange(text_pieces);
            }

            pieces = new Piece[pieces_list.Count];
            pieces_list.CopyTo(pieces, 0);

            CharacterRange[] ranges = new CharacterRange[pieces.Length];

            for (int i = 0; i < pieces.Length; i++)
            {
                ranges[i] = new CharacterRange(pieces[i].start, pieces[i].length);
            }

            string_format.SetMeasurableCharacterRanges(ranges);

            Region[] regions = TextRenderer.MeasureCharacterRanges(Text,
                                                                   ThemeEngine.Current.GetLinkFont(this),
                                                                   PaddingClientRectangle,
                                                                   string_format);

            for (int i = 0; i < pieces.Length; i++)
            {
                pieces[i].region = regions[i];
#if NET_2_0
                pieces[i].region.Translate(Padding.Left, Padding.Top);
#endif
            }

            Invalidate();
        }
Beispiel #2
0
        private void CreateLinkPieces()
        {
            if (Text.Length == 0)
            {
                SetStyle(ControlStyles.Selectable, false);
                TabStop          = false;
                link_area.Start  = 0;
                link_area.Length = 0;
                return;
            }

            if (Links.Count == 1 && Links[0].Start == 0 && Links[0].Length == -1)
            {
                Links[0].Length = Text.Length;
            }

            SortLinks();

            // Set the LinkArea values based on first link.
            if (Links.Count > 0)
            {
                link_area.Start  = Links[0].Start;
                link_area.Length = Links[0].Length;
            }
            else
            {
                link_area.Start  = 0;
                link_area.Length = 0;
            }

            TabStop = (LinkArea.Length > 0);
            SetStyle(ControlStyles.Selectable, TabStop);

            /* don't bother doing the rest if our handle hasn't been created */
            if (!IsHandleCreated)
            {
                return;
            }

            ArrayList pieces_list = new ArrayList();

            int current_end = 0;

            for (int l = 0; l < sorted_links.Length; l++)
            {
                int new_link_start = sorted_links[l].Start;

                if (new_link_start > current_end)
                {
                    /* create/push a piece
                     * containing the text between
                     * the previous/new link */
                    ArrayList text_pieces = CreatePiecesFromText(current_end, new_link_start - current_end, null);
                    pieces_list.AddRange(text_pieces);
                }

                /* now create a group of pieces for
                 * the new link (split up by \n's) */
                ArrayList link_pieces = CreatePiecesFromText(new_link_start, sorted_links[l].Length, sorted_links[l]);
                pieces_list.AddRange(link_pieces);
                sorted_links[l].pieces.AddRange(link_pieces);

                current_end = sorted_links[l].Start + sorted_links[l].Length;
            }
            if (current_end < Text.Length)
            {
                ArrayList text_pieces = CreatePiecesFromText(current_end, Text.Length - current_end, null);
                pieces_list.AddRange(text_pieces);
            }

            pieces = new Piece[pieces_list.Count];
            pieces_list.CopyTo(pieces, 0);

            CharacterRange[] ranges = new CharacterRange[pieces.Length];

            for (int i = 0; i < pieces.Length; i++)
            {
                ranges[i] = new CharacterRange(pieces[i].start, pieces[i].length);
            }

            string_format.SetMeasurableCharacterRanges(ranges);

            Region[] regions = TextRenderer.MeasureCharacterRanges(Text,
                                                                   ThemeEngine.Current.GetLinkFont(this),
                                                                   PaddingClientRectangle,
                                                                   string_format);

            // Get offset for different text alignments
            float align_offset_x = 0F;
            float align_offset_y = 0F;

            if (TextAlign != ContentAlignment.TopLeft)
            {
                Region all_regions = new Region(new Rectangle());

                foreach (Region region in regions)
                {
                    all_regions.Union(region);
                }

                Graphics graphics = CreateGraphics();

                if (TextAlign == ContentAlignment.TopCenter)
                {
                    float text_width = all_regions.GetBounds(graphics).Width;

                    align_offset_x = (ClientRectangle.Width / 2 - text_width / 2);
                }
                if (TextAlign == ContentAlignment.TopRight)
                {
                    float text_width = all_regions.GetBounds(graphics).Width;

                    align_offset_x = (ClientRectangle.Width - text_width);
                }
                if (TextAlign == ContentAlignment.MiddleLeft)
                {
                    float text_height = all_regions.GetBounds(graphics).Height;

                    align_offset_y = (ClientRectangle.Height / 2 - text_height / 2);
                }
                if (TextAlign == ContentAlignment.MiddleCenter)
                {
                    float text_width  = all_regions.GetBounds(graphics).Width;
                    float text_height = all_regions.GetBounds(graphics).Height;

                    align_offset_x = (ClientRectangle.Width / 2 - text_width / 2);
                    align_offset_y = (ClientRectangle.Height / 2 - text_height / 2);
                }
                if (TextAlign == ContentAlignment.MiddleRight)
                {
                    float text_width  = all_regions.GetBounds(graphics).Width;
                    float text_height = all_regions.GetBounds(graphics).Height;

                    align_offset_x = (ClientRectangle.Width - text_width);
                    align_offset_y = (ClientRectangle.Height / 2 - text_height / 2);
                }
                if (TextAlign == ContentAlignment.BottomLeft)
                {
                    float text_height = all_regions.GetBounds(graphics).Height;

                    align_offset_y = (ClientRectangle.Height - text_height);
                }
                if (TextAlign == ContentAlignment.BottomCenter)
                {
                    float text_width  = all_regions.GetBounds(graphics).Width;
                    float text_height = all_regions.GetBounds(graphics).Height;

                    align_offset_x = (ClientRectangle.Width / 2 - text_width / 2);
                    align_offset_y = (ClientRectangle.Height - text_height);
                }
                if (TextAlign == ContentAlignment.BottomRight)
                {
                    float text_width  = all_regions.GetBounds(graphics).Width;
                    float text_height = all_regions.GetBounds(graphics).Height;

                    align_offset_x = (ClientRectangle.Width - text_width);
                    align_offset_y = (ClientRectangle.Height - text_height);
                }
            }


            for (int i = 0; i < pieces.Length; i++)
            {
                pieces[i].region = regions[i];
                pieces[i].region.Translate(Padding.Left + align_offset_x, Padding.Top + align_offset_y);
            }

            Invalidate();
        }