示例#1
0
        public Position PointToTextPosition(Point point)
        {
            int index, trailing;
            int x = Pango.Units.FromPixels(point.X - origin.X);
            int y = Pango.Units.FromPixels(point.Y - origin.Y);

            layout.XyToIndex(x, y, out index, out trailing);

            return(IndexToPosition(index + trailing));
        }
        /// <summary>
        /// Get character index after which layout should be broken to not exceed maxHeight
        /// </summary>
        /// <returns>
        ///  -1 if maxHeight is smaller than top padding. -2 if maxHeight is after text and character index if maxHeight is in the middle of the text.
        /// </returns>
        /// <param name='g'>
        /// G.
        /// </param>
        /// <param name='tb'>
        /// Tb.
        /// </param>
        /// <param name='maxHeight'>
        /// Max height.
        /// </param>

        public static int GetBreakLineCharacterIndexbyMaxHeight(this Context g, TextBlock tb, double maxHeight)
        {
            int result = 0;

            Pango.Rectangle unused;
            Pango.Rectangle te;
            double          vertAlgSpan = 0;
            int             chi         = 0;
            int             gi          = 0;


            if (maxHeight > 0)
            {
                Pango.Layout layout = createLayoutFromTextBlock(g, tb);
                layout.GetExtents(out unused, out te);
                double measuredHeight = te.Height / Pango.Scale.PangoScale;

                if (tb.VerticalAlignment != VerticalAlignment.Top)
                {
                    vertAlgSpan = measureVerticlaSpan(tb, measuredHeight);
                }


                double realTbStart = tb.Padding.Top + vertAlgSpan;


                if (realTbStart >= maxHeight)
                {
                    result = -1;
                }
                else if (maxHeight > realTbStart + measuredHeight)
                {
                    return(-2);
                }
                else
                {
                    layout.XyToIndex(0, (int)((maxHeight - realTbStart) * Pango.Scale.PangoScale), out chi, out gi);
                    byte[] bytes = System.Text.Encoding.UTF8.GetBytes(tb.Text);
                    int    o     = System.Text.Encoding.UTF8.GetCharCount(bytes, 0, chi);
                    result = o;
                }

                (layout as IDisposable).Dispose();
            }

            return(result);
        }
            void DrawList()
            {
                int winWidth, winHeight;

                this.GdkWindow.GetSize(out winWidth, out winHeight);

                int ypos      = margin;
                int lineWidth = winWidth - margin * 2;
                int xpos      = margin + padding;

                int n = 0;

                while (ypos < winHeight - margin && (page + n) < win.DataProvider.IconCount)
                {
                    string text = win.DataProvider.GetText(page + n) ?? "<null>";
                    layout.SetText(text);

                    Gdk.Pixbuf icon       = win.DataProvider.GetIcon(page + n);
                    int        iconHeight = icon != null ? icon.Height : 24;
                    int        iconWidth  = icon != null ? icon.Width : 0;

                    int wi, he, typos, iypos;
                    layout.GetPixelSize(out wi, out he);
                    if (wi > Allocation.Width)
                    {
                        int idx, trail;
                        if (layout.XyToIndex((int)((Allocation.Width - xpos - iconWidth - 2) * Pango.Scale.PangoScale), 0, out idx, out trail) && idx > 3)
                        {
                            text = text.Substring(0, idx - 3) + "...";
                            layout.SetText(text);
                            layout.GetPixelSize(out wi, out he);
                        }
                    }
                    typos = he < rowHeight ? ypos + (rowHeight - he) / 2 : ypos;
                    iypos = iconHeight < rowHeight ? ypos + (rowHeight - iconHeight) / 2 : ypos;

                    if (page + n == selection)
                    {
                        if (!disableSelection)
                        {
                            this.GdkWindow.DrawRectangle(this.Style.BaseGC(StateType.Selected),
                                                         true, margin, ypos, lineWidth, he + padding);
                            this.GdkWindow.DrawLayout(this.Style.TextGC(StateType.Selected),
                                                      xpos + iconWidth + 2, typos, layout);
                        }
                        else
                        {
                            this.GdkWindow.DrawRectangle(this.Style.BaseGC(StateType.Selected),
                                                         false, margin, ypos, lineWidth, he + padding);
                            this.GdkWindow.DrawLayout(this.Style.TextGC(StateType.Normal),
                                                      xpos + iconWidth + 2, typos, layout);
                        }
                    }
                    else
                    {
                        this.GdkWindow.DrawLayout(this.Style.TextGC(StateType.Normal),
                                                  xpos + iconWidth + 2, typos, layout);
                    }

                    if (icon != null)
                    {
                        this.GdkWindow.DrawPixbuf(this.Style.ForegroundGC(StateType.Normal), icon, 0, 0,
                                                  xpos, iypos, iconWidth, iconHeight, Gdk.RgbDither.None, 0, 0);
                    }

                    ypos += rowHeight;
                    n++;

//reset the markup or it carries over to the next SetText
                    layout.SetMarkup(string.Empty);
                }
            }
示例#4
0
 public bool XyToIndex(int x, int y, out int index, out int trailing)
 {
     return(layout.XyToIndex(x, y, out index, out trailing));
 }