Ejemplo n.º 1
0
            public void ConnectFunc(Builder builder, GLib.Object objekt, string signal_name, string handler_name, GLib.Object connect_object, GLib.ConnectFlags flags)
            {
                /* search for the event to connect */
                System.Reflection.MemberInfo[] evnts = objekt.GetType().
                                                       FindMembers(System.Reflection.MemberTypes.Event,
                                                                   System.Reflection.BindingFlags.Instance
                                                                   | System.Reflection.BindingFlags.Static
                                                                   | System.Reflection.BindingFlags.Public
                                                                   | System.Reflection.BindingFlags.NonPublic,
                                                                   new System.Reflection.MemberFilter(SignalFilter), signal_name);
                foreach (System.Reflection.EventInfo ei in evnts)
                {
                    bool connected = false;
                    System.Reflection.MethodInfo      add   = ei.GetAddMethod();
                    System.Reflection.ParameterInfo[] addpi = add.GetParameters();
                    if (addpi.Length == 1)       /* this should be always true, unless there's something broken */
                    {
                        Type delegate_type = addpi[0].ParameterType;

                        /* look for an instance method */
                        if (connect_object != null || handler != null)
                        {
                            try {
                                Delegate d = Delegate.CreateDelegate(delegate_type, connect_object != null ? connect_object : handler, handler_name);
                                add.Invoke(objekt, new object[] { d });
                                connected = true;
                            } catch (ArgumentException) {             /* ignore if there is not such instance method */
                            }
                        }

                        /* look for a static method if no instance method has been found */
                        if (!connected && handler_type != null)
                        {
                            try  {
                                Delegate d = Delegate.CreateDelegate(delegate_type, handler_type, handler_name);
                                add.Invoke(objekt, new object[] { d });
                                connected = true;
                            } catch (ArgumentException) {             /* ignore if there is not such static method */
                            }
                        }

                        if (!connected)
                        {
                            string msg = ExplainError(ei.Name, delegate_type, handler_type, handler_name);
                            throw new HandlerNotFoundException(msg, handler_name, signal_name, ei, delegate_type);
                        }
                    }
                }
            }
Ejemplo n.º 2
0
            public Gdk.Pixbuf WrapperIcon()
            {
                Gdk.Pixbuf icon = null;
                Type       type = d_wrapper != null?d_wrapper.GetType() : d_object.GetType();

                string stockid;

                if (s_iconmap.TryGetValue(type, out icon))
                {
                    return(icon);
                }

                if (d_wrapper != null)
                {
                    if (d_wrapper is Wrappers.Node)
                    {
                        stockid = Stock.Node;
                    }
                    else if (d_wrapper is Wrappers.Edge)
                    {
                        stockid = Stock.Edge;
                    }
                    else if (d_wrapper is Wrappers.Function)
                    {
                        stockid = Stock.Function;
                    }
                    else
                    {
                        stockid = Gtk.Stock.MissingImage;
                    }

                    icon = d_widget.RenderIcon(stockid, IconSize.Menu, null);
                }

                s_iconmap[type] = icon;

                return(icon);
            }
Ejemplo n.º 3
0
        public void AddBinding(GLib.Object control, object dataSource, string propertyName)
        {
            // Get the type of control being bound.
            ObjectType widgetType = ObjectType.None;
            Type       ctrlType   = control.GetType();

            if (IsSameOrSubclass(ctrlType, typeof(Entry)))
            {
                widgetType = ObjectType.Entry;
            }
            else if (IsSameOrSubclass(ctrlType, typeof(TextView)))
            {
                widgetType = ObjectType.TextView;
            }
            else if (IsSameOrSubclass(ctrlType, typeof(ToggleButton)))
            {
                widgetType = ObjectType.ToggleButton;
            }
            else if (IsSameOrSubclass(ctrlType, typeof(ToggleAction)))
            {
                widgetType = ObjectType.ToggleAction;
            }

            // Get the datasource and ensure that the types are compatible.
            DataSource src         = new DataSource(dataSource, propertyName);
            bool       typeIsValid = false;

            switch (widgetType)
            {
            case ObjectType.Entry:
            case ObjectType.TextView:
                if (src.DataType == typeof(string))
                {
                    typeIsValid = true;
                }
                break;

            case ObjectType.ToggleButton:
            case ObjectType.ToggleAction:
                if (src.DataType == typeof(bool))
                {
                    typeIsValid = true;
                }
                break;
            }

            if (!typeIsValid)
            {
                string name = "unknown";
                if (IsSameOrSubclass(ctrlType, typeof(Gtk.Action)))
                {
                    name = ((Gtk.Action)control).Name;
                }
                else if (IsSameOrSubclass(ctrlType, typeof(Gtk.Widget)))
                {
                    name = ((Gtk.Widget)control).Name;
                }

                throw new Exception(string.Format("The binding for control '{0}' to '{1}' failed: incompatible data types.", name, propertyName));
            }

            // Add an event handler for the control.
            switch (widgetType)
            {
            case ObjectType.Entry:
                ((Entry)control).Changed += (object sender, EventArgs e) => {
                    if (!_updateInProgress)
                    {
                        BindingDef def = _bindings.Find(x => x.Control == sender);
                        if (def.DataSource.DataType == typeof(string))
                        {
                            def.DataSource.Value = ((Entry)sender).Text;
                        }

                        UpdateBindings(def.DataSource.Key, sender);
                    }
                };
                break;

            case ObjectType.ToggleButton:
                ((ToggleButton)control).Toggled += (object sender, EventArgs e) => {
                    if (!_updateInProgress)
                    {
                        BindingDef def = _bindings.Find(x => x.Control == sender);
                        def.DataSource.Value = ((ToggleButton)sender).Active;

                        UpdateBindings(def.DataSource.Key, sender);
                    }
                };
                break;

            case ObjectType.ToggleAction:
                ((ToggleAction)control).Toggled += (object sender, EventArgs e) => {
                    if (!_updateInProgress)
                    {
                        BindingDef def = _bindings.Find(x => x.Control == sender);
                        def.DataSource.Value = ((ToggleAction)sender).Active;

                        UpdateBindings(def.DataSource.Key, sender);
                    }
                };
                break;
            }

            _bindings.Add(new BindingDef(src, control, widgetType));
        }