开发者

How to have gtk control+c to be handled, or not?

开发者 https://www.devze.com 2023-01-12 01:23 出处:网络
I\'m using pygtk, and would like to handle control+c sometimes to do a special copy action, but other times to let gtk handle it. For example, I\'d like to put an object on my clipboard if it is avail

I'm using pygtk, and would like to handle control+c sometimes to do a special copy action, but other times to let gtk handle it. For example, I'd like to put an object on my clipboard if it is available, or just let control+c be used in the normal fashion in a text entry.

Currently I have an ActionGroup associated with "c" but that always eats the keystroke, even if I return False. If I remove the ActionGroup, it always works in the text areas. If I add the ActionGroup, it always handles it, and copy doesn't work in the text areas.

What is the proper manner to have co开发者_运维知识库ntrol+c appear in the menu, handle the keystroke sometimes, but other times, let it fall to a text widget?


I don't know if this is the "proper" way, but here is how I do it. I pass the application window as the user data parameter to the action callback. Then I find out which widget is focused in the window, and I pass the copy command on to that widget if that makes sense to do (i.e. the focused widget is a text entry like you say). If that doesn't make sense, then I copy from the window's 'default' text view.

void
action_copy(GtkAction *action, gpointer user_data)
{
    GtkWidget *widget = gtk_window_get_focus(GTK_WINDOW(user_data));

    /* What actually happens depends on the type of widget that is focused */
    if(WEBKIT_IS_WEB_VIEW(widget))
        webkit_web_view_copy_clipboard(WEBKIT_WEB_VIEW(widget));
    else if((GTK_IS_LABEL(widget) && gtk_label_get_selectable(GTK_LABEL(widget)))
        || GTK_IS_ENTRY(widget) || GTK_IS_TEXT_VIEW(widget))
        g_signal_emit_by_name(widget, "copy-clipboard", NULL);
    else
        g_signal_emit_by_name(/* ...default text view... */, "copy-clipboard", NULL);
}

(Obtaining the default text view is actually done by calling a get_default_view() method on my application class, which is a subclass of GtkWindow; but I didn't want to complicate matters here.)

0

精彩评论

暂无评论...
验证码 换一张
取 消