[Gtk-sharp-list] Adding a custom widget to (designer generated) GUI

True Friend true.friend2004 at gmail.com
Sun Sep 14 19:33:09 EDT 2008


HI
I have subclassed a textview to type custom language (Urdu in my case).
Providing the code for others' benefit.
-----------------------------
public class UTextView: Gtk.TextView
    {
        bool lang = true;
        public UTextView()
        {
            this.ModifyFont(Pango.FontDescription.FromString("Nafees Web
Naskh 12"));
            this.WrapMode = Gtk.WrapMode.Word;
        }
        protected override bool OnKeyPressEvent (EventKey evnt)
        {
        Dictionary<char, char> dict = new Dictionary<char,char>();
        dict['`'] = 'ٍ';
        dict['1'] = '1';
        dict['2'] = '2';
        dict['3'] = '3';
        dict['4'] = '4';
        dict['5'] = '5';
        dict['6'] = '6';
        dict['7'] = '7';
        dict['8'] = '8';
        dict['9'] = '9';
        dict['0'] = '0';
        dict['-'] = 'أ';
        dict['='] = 'ؑ';
        dict['~'] = 'ً';
        dict['!'] = '!';
        dict['@'] = '٬';
        dict['#'] = '؀';
        dict['$'] = '؁';
        dict['%'] = '';
        dict['^'] = 'ۖ';
        dict['&'] = 'ٔ';
        dict['*'] = 'ٌ';
        dict['('] = ')';
        dict[')'] = '(';
        dict['_'] = 'ؐ';
        dict['+'] = 'ؑ';
        dict['q'] = 'ق';
        dict['Q'] = 'ْ';
        dict['w'] = 'و';
        dict['W'] = 'ؤ';
        dict['e'] = 'ع';
        dict['E'] = 'ّ';
        dict['r'] = 'ر';
        dict['R'] = 'ڑ';
        dict['t'] = 'ت';
        dict['T'] = 'ٹ';
        dict['y'] = 'ے';
        dict['Y'] = 'ۓ';
        dict['u'] = 'ئ';
        dict['U'] = 'ء';
        dict['i'] = 'ی';
        dict['I'] = 'ٰ';
        dict['o'] = 'ہ';
        dict['O'] = 'ۃ';
        dict['p'] = 'پ';
        dict['P'] = 'ُ';
        dict['['] = ']';
        dict['{'] = 'ؒ';
        dict[']'] = '[';
        dict['}'] = 'ؓ';
        dict['a'] = 'ا';
        dict['A'] = 'آ';
        dict['s'] = 'س';
        dict['S'] = 'ص';
        dict['d'] = 'د';
        dict['D'] = 'ڈ';
        dict['f'] = 'ف';
        dict['F'] = '';
        dict['g'] = 'گ';
        dict['G'] = 'غ';
        dict['h'] = 'ھ';
        dict['H'] = 'ح';
        dict['j'] = 'ج';
        dict['J'] = 'ض';
        dict['k'] = 'ک';
        dict['K'] = 'خ';
        dict['l'] = 'ل';
        dict['L'] = 'ؓ';
        dict[';'] = '؛';
        dict[':'] = ':';
        dict['\''] = 'ٰ';
        dict['"'] = '"';
        dict['\\'] = '؎';
        dict['|'] = 'ؔ';
        dict['z'] = 'ز';
        dict['Z'] = 'ذ';
        dict['x'] = 'ش';
        dict['X'] = 'ژ';
        dict['c'] = 'چ';
        dict['C'] = 'ث';
        dict['v'] = 'ط';
        dict['V'] = 'ظ';
        dict['b'] = 'ب';
        dict['B'] = 'ؒ';
        dict['n'] = 'ن';
        dict['N'] = 'ں';
        dict['m'] = 'م';
        dict['M'] = '';
        dict[','] = '،';
        dict['<'] = 'ِ';
        dict['.'] = '۔';
        dict['>'] = 'َ';
        dict['/'] = '';
        dict['?'] = '؟';
        dict[' '] = ' ';
        char key = (char)evnt.Key;
        try
            {
                if(evnt.State == Gdk.ModifierType.Mod2Mask ||
                   (evnt.State & Gdk.ModifierType.ShiftMask) != 0)
                {
                    if(lang == true)
                    {

this.Buffer.InsertInteractiveAtCursor(dict[key].ToString(), true);
                        return true;
                    }
                    else
                    {
                        return base.OnKeyPressEvent (evnt);
                        return false;
                    }

                }
                else
                {
                    if((evnt.State & Gdk.ModifierType.ControlMask) != 0)
                    {
                        if((evnt.State & Gdk.ModifierType.ControlMask) != 0)
                        {
                            if(key == (char)Gdk.Key.space)
                            {
                                if(lang == true)
                                {
                                    lang = false;
                                    this.ModifyBase(StateType.Normal, new
Gdk.Color(202, 225, 255));
                                }
                                else
                                {
                                    lang = true;
                                    this.ModifyBase(StateType.Normal, new
Gdk.Color(255, 255, 255));
                                }
                            }
                        return base.OnKeyPressEvent(evnt);
                        return false;
                        }
                    }
                    return base.OnKeyPressEvent(evnt);
                    return false;
                }
            }
            catch(System.Exception)
            {
                return base.OnKeyPressEvent (evnt);
                return false;
            }
            return true;
        }
    }
-------------------------------------------------------
Now the problem I am facing is, I want to add this textview to designer
generated code. I tried to do so in MainWindow Class's constructor under
Build() like this.
UrduLibraryAssistant.UTextView view = new UTextView();
this.scrolledwindow2.Add(view);
view.Show();
It works. I can see it and type in it. But when I try to use it for events,
like to open a file in it, the compiler gives the error of no such type
avilable (I mean no availability of such a thing in code). I wonder how it
can when it is located on same file, same class ... but there is something
wrong. I then tried to put it in Build(). Gone there, put it there but
didn't worked either. Then I added a Gtk.TextView from designer and replaced
Gtk.TextView to UrduLibraryAssistant.UTextView in gui desiger file (opened
by MD in c# systanx format). I saved it and it was workig fine for a
compile. But as fas as I do not disturb designer. When I create/edit a gui
component the partial class file for gui is overwritten and I haev once
again plain old Gtk.TextView.
Sorry for long post. Hopefully you could understand I want to know how can I
add custom textview to form without this dirty way I am using to get work
done.
Regards
-- 
Muhammad Shakir Aziz محمد شاکر عزیز
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/gtk-sharp-list/attachments/20080915/573e2c1d/attachment-0001.html 


More information about the Gtk-sharp-list mailing list