Example #1
0
        /// <summary>
        /// Sends a custom button press (click) event to a widget.
        /// </summary>
        /// <param name="target">Widget which should receive the button press event.</param>
        /// <param name="eventType">Type of event to be sent.</param>
        /// <param name="state">Modifiers for the click - ie control click, shift click, etc.</param>
        /// <param name="button">Type of click - ie left click, middle click or right click.</param>
        /// <param name="x">x-coordinate of the click, relative to the top-left corner of the widget.</param>
        /// <param name="y">y-coordinate of the click, relative to the top-left corner of the widget.</param>
        /// <param name="wait">Iff true, will wait for gtk to process the event.</param>
        public static void Click(Widget target, EventType eventType, ModifierType state, ButtonPressType button, double x = 0, double y = 0, bool wait = true)
        {
            Gdk.Window win = target.GdkWindow;

            int rx, ry;

            win.GetRootOrigin(out rx, out ry);

            var nativeEvent = new NativeEventButtonStruct
            {
                type       = eventType,
                send_event = 1,
                window     = win.Handle,
                state      = (uint)state,
                button     = (uint)button,
                x          = x,
                y          = y,
                axes       = IntPtr.Zero,
                device     = IntPtr.Zero,
                time       = Gtk.Global.CurrentEventTime,
                x_root     = x + rx,
                y_root     = y + ry
            };

            IntPtr ptr = GLib.Marshaller.StructureToPtrAlloc(nativeEvent);

            try
            {
                EventHelper.Put(new EventButton(ptr));
            }
            finally
            {
                Marshal.FreeHGlobal(ptr);
            }

            if (wait)
            {
                // Clear gtk event loop.
                while (GLib.MainContext.Iteration())
                {
                    ;
                }
            }
        }