[Gtk-sharp-list] Gtk.Entry and Gtk.TextView (MERGE?)

Draekz draekz at gmail.com
Sun Jul 6 11:09:08 EDT 2008


Hey all,

We are needing to create a widget that looks like a Gtk.Entry (Blue
highlighted border when selected, and roundish shadow type border etc) yet
we need to tools provided in the TextView.

We have been able to draw a similar border around the TextView (its not 100%
but its close) but we have been unable to get it to highlight the border
blue when selected.

Could someone with more experiencing drawing custom styles etc on widgets
help us to mimic the Entry for our TextView? Any help in the right direction
would be appreciated. Below we have provided the code we have so far (keep
in mind there are a few extra tidbits in this code that have nothing to do
with what i'm asking about, but its just text tags and stuff):

----------------------------------------

public class ViewEntryWidget : Bin
{
    const int _borderSize = 2;

    bool _editing;
    string _markup;
    TextView _view;

    TextTag _tagBold = new TextTag ("bold");
    TextTag _tagItalic = new TextTag ("italic");
    TextTag _tagUnderline = new TextTag ("underline");
    TextTag _tagStrikethrough = new TextTag ("strikethrough");
    TextTag _tagUri = new TextTag ("uri");

    public string Text
    {
        get { return _markup; }
    }

    public ViewEntryWidget ()
    {
        Entry entry = new Entry ();

        _view = new TextView ();
        _view.AcceptsTab = false;
        _view.BorderWidth = 3;
        _view.CursorVisible = true;
        _view.Editable = true;
        // This is going to show white instead of gray, but it should be
using the theme color, not white.
        _view.ModifyBg (StateType.Normal, new Gdk.Color (255,255,255));

        _tagBold.Weight = Weight.Bold;
        _view.Buffer.TagTable.Add (_tagBold);

        _tagItalic.Style = Pango.Style.Italic;
        _view.Buffer.TagTable.Add (_tagItalic);

        _tagUnderline.Underline = Pango.Underline.Single;
        _view.Buffer.TagTable.Add (_tagUnderline);

        _tagStrikethrough.Strikethrough = true;
        _view.Buffer.TagTable.Add (_tagStrikethrough);

        //TODO: can this be gotten from the gtk theme?
        _tagUri.ForegroundGdk = new Gdk.Color (0, 0, 255);
        _tagUri.Underline = Pango.Underline.Single;
        _view.Buffer.TagTable.Add (_tagUri);

        _view.FocusInEvent += ViewFocusIn;
        _view.FocusOutEvent += ViewFocusOut;
        _view.KeyPressEvent += ViewKeyPress;
        Add (_view);

        _markup = string.Empty;

        _editing = false;
        SetContent ();
    }

    void SetContent ()
    {
        _view.Buffer.Clear ();

        if (string.IsNullOrEmpty (_markup))
            return;

        _view.Buffer.Text = _markup;
    }

    int PangoPixels (int p)
    {
        return ((int)(p) + 512) >> 10;
    }

    protected override void OnSizeRequested (ref Requisition requisition)
    {
        FontMetrics metrics = _view.PangoContext.GetMetrics
(_view.Style.FontDesc, _view.PangoContext.Language);

        int charPixels = (int)((Math.Max (metrics.ApproximateCharWidth,
metrics.ApproximateDigitWidth) + Pango.Scale.PangoScale - 1) /
Pango.Scale.PangoScale);

        requisition.Width = (charPixels * _view.Buffer.Text.Length) +
(_borderSize * 2);
        requisition.Height = PangoPixels (metrics.Ascent + metrics.Descent)
+ (int)(_view.BorderWidth * 2) + (_borderSize * 2);

        //Log.Debug ("Requisition = {0}x{1}", requisition.Width,
requisition.Height);

        metrics.Dispose ();
    }

    protected override void OnSizeAllocated (Gdk.Rectangle allocation)
    {
        base.OnSizeAllocated (allocation);

        _view.SizeAllocate (new Gdk.Rectangle (allocation.X + _borderSize,
allocation.Y + _borderSize,
                                               allocation.Width - (2 *
_borderSize),
                                               allocation.Height - (2 *
_borderSize)));
    }

    protected override void OnRealized ()
    {
        base.OnRealized ();

        GdkWindow.Background = Style.Base (StateType.Prelight);
    }

    protected override void OnStyleSet (Gtk.Style previous_style)
    {
        base.OnStyleSet (previous_style);

        if (IsRealized && (previous_style != null))
            GdkWindow.Background = Style.Base (StateType.Normal);
    }

    protected override bool OnExposeEvent (Gdk.EventExpose evnt)
    {
        //Gtk.Style.PaintFlatBox (Style, evnt.Window, StateType.Normal,
ShadowType.None, Allocation, this, "entry_bg",
        //                        Allocation.X, Allocation.Y,
Allocation.Width, Allocation.Height);

        Gtk.Style.PaintShadow (Style, evnt.Window, StateType.Normal,
ShadowType.In, Allocation, this, "entry",
                               Allocation.X, Allocation.Y, Allocation.Width,
Allocation.Height);

        //TODO: focus

        PropagateExpose (_view, evnt);

        return false;
    }

    void ViewFocusIn (object sender, FocusInEventArgs args)
    {
        QueueDraw ();

        if (!_editing)
        {
            _editing = true;
            SetContent ();
        }
    }

    void ViewFocusOut (object sender, FocusOutEventArgs args)
    {
        QueueDraw ();

        if (_editing)
        {
            _editing = false;
            _markup = _view.Buffer.Text;
            SetContent ();
        }
    }

    [GLib.ConnectBefore]
    void ViewKeyPress (object sender, KeyPressEventArgs args)
    {
        if (args.Event.Key == Gdk.Key.Return || args.Event.Key ==
Gdk.Key.KP_Enter)
        {
            // Treat enter as tab

            Toplevel.ChildFocus (DirectionType.TabForward);
            args.RetVal = true;
        }
    }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/gtk-sharp-list/attachments/20080706/2e6cc446/attachment.html 


More information about the Gtk-sharp-list mailing list