Re: Controlling a widget from an other



On Sun, Nov 12, 2006 at 05:00:22PM +0000, Kalap kabat wrote:
> 
> I tried this:
> 
>      scrolled.emit('scroll-child', gtk.SCROLL_PAGE_FORWARD, False)
> 
> and it's scrolling the treeview, but it doesn't move the selection like when 
> the treeview has the focus and I press PgDown there. 

But that was not to be expected, we talked about srcolling,
not about moving focus.

> I have the feeling it would be much simpler to send a PgDown keypress to the 
> treeview somehow.

If the thing you need is to emulate a keypress, then yes,
the thing you want is to emulate a keypress.

> I made a Winforms GUI recently and there it was trivial to 
> send keypresses to an other widget using SendKeys. Surely, after all these 
> years of development GTK has to have some method to do that.

Do you really expect someone will help you after this rant?
Anyway, I will try to remain constructive.

Sending keypresses to widgets is a hack and last resort
measure.  Facilitating sending keypresses encourages poor
design.  If you need it, it usually means (a) the widget
interface is insufficient, because the action should be
possible to invoke programatically (b) you want to do
something unsual.

So if you still want this

    g_signal_connect(entry, "key-press-event",
                     G_CALLBACK(key_pressed), treeview);

static gboolean
key_pressed(GtkWidget *entry, GdkEventKey *event, GtkWidget *treeview)
{
    if (event->keyval == etc.) {
        GdkEvent *sevent;

        sevent = gdk_event_new(GDK_KEY_PRESS);
        sevent->key.window = g_object_ref(treeview->window);
        sevent->key.send_event = TRUE;
        sevent->key.time = GDK_CURRENT_TIME;  /* not good */
        sevent->key.state = event->state;
        sevent->key.keyval = event->keyval;
        sevent->key.length = event->length;
        sevent->key.string = g_strdup(event->string);
        sevent->key.hardware_keycode = event->hardware_keycode;
        sevent->key.group = event->group;
        sevent->key.is_modifier = event->is_modifier;
        gtk_widget_grab_focus(treeview);
        gtk_widget_event(treeview, sevent);
        gdk_event_free(sevent);
        gtk_widget_grab_focus(entry);
        return TRUE;
    }

    return FALSE;
}

Note some keys have bindings for both tree views and entries
(e.g. Home and End) so there is an inherent conflict.  In
addition, bindings are user-configurable, so to respect
user's preferences you'd have to analyze the bindings run-time.

Yeti


--
Whatever.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]