透明浮动GtkEntry

时间:2011-01-07 23:57:24

标签: c gtk

我想开发一款可以进行多语言输入的游戏,包括CJK角色。是否可以让GtkDrawingArea填充整个窗口,并将GtkEntry浮动到“顶部”?还可以使GtkEntry的背景和边框透明吗?谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

以下是我在my application中执行此操作的方法,其中包含一个函数,用于在父窗口小部件的顶部,相对于父窗口小部件的坐标(x,y)处弹出包含一些文本的GtkEntry左上角:

GtkWidget *
popup_edit_window(GtkWidget *parent, gint x, gint y, const gchar *text)
{
    gint px, py;
    gdk_window_get_origin(gtk_widget_get_window(parent), &px, &py);

    GtkWidget *edit_popup = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    /* stackoverflow.com/questions/1925568/how-to-give-keyboard-focus-to-a-pop-up-gtk-window */
    GTK_WIDGET_SET_FLAGS(edit_popup, GTK_CAN_FOCUS);
    gtk_window_set_decorated(GTK_WINDOW(edit_popup), FALSE);
    gtk_window_set_has_frame(GTK_WINDOW(edit_popup), FALSE);
    gtk_window_set_type_hint(GTK_WINDOW(edit_popup), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
    gtk_window_set_transient_for(GTK_WINDOW(edit_popup), GTK_WINDOW(parent));
    gtk_window_set_gravity(GTK_WINDOW(edit_popup), GDK_GRAVITY_CENTER);

    GtkWidget *entry = gtk_entry_new();
    gtk_widget_add_events(entry, GDK_FOCUS_CHANGE_MASK);
    gtk_entry_set_text(GTK_ENTRY(entry), text);

    gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
    gtk_container_add(GTK_CONTAINER(edit_popup), entry);
    g_signal_connect_swapped(entry, "focus-out-event", G_CALLBACK(gtk_widget_destroy), edit_popup);
    g_signal_connect(entry, "key-press-event", G_CALLBACK(on_edit_popup_key_press), edit_popup);

    gtk_widget_show_all(edit_popup);
    gtk_window_move(GTK_WINDOW(edit_popup), x + px, y + py);
    /* GDK_GRAVITY_CENTER seems to have no effect? */
    gtk_widget_grab_focus(entry);
    gtk_window_present(GTK_WINDOW(edit_popup));

    return edit_popup;
}

gboolean
on_edit_popup_key_press(GtkWidget *entry, GdkEventKey *event, GtkWidget *edit_popup)
{
    switch(event->keyval) {
    case GDK_Escape:
        gtk_widget_destroy(edit_popup);
        return TRUE;
    case GDK_Return:
    case GDK_KP_Enter:
        /* here, do whatever you need to do when finished editing */
        gtk_widget_destroy(edit_popup);
        return TRUE;
    }
    return FALSE; /* event not handled */
}

要使其透明,您应该能够:

gtk_window_set_opacity(GTK_WINDOW(edit_popup), 0.5);

但请注意,您的窗口管理器可能不支持,或者可能选择忽略它。

但是,您是否认为GTK具有相关的输入法库,以便用户可以选择输入CJK字符所需的输入法?无需重新发明轮子。