0

I want develop a game that can take multilingual input, including CJK characters. Is it possible to have a GtkDrawingArea fill the entire window and have a GtkEntry floating "on top" of it? Also is it possible to make the background and border of the GtkEntry transparent? Thanks for any help.

NoBugs
  • 9,310
  • 13
  • 80
  • 146
kaykun
  • 2,247
  • 2
  • 24
  • 37

1 Answers1

1

Here's how I did it in my application, with a function to pop up a GtkEntry containing some text, on top of a parent widget, at coordinates (x,y) relative to the parent widget's top left corner:

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 */
}

To make it transparent, you should just be able to do:

gtk_window_set_opacity(GTK_WINDOW(edit_popup), 0.5);

but be aware that your window manager may not support that, or may choose to ignore it.

However, have you considered that GTK has related input method libraries so users can select whatever input method they want for inputting CJK characters? No need to reinvent the wheel.

ptomato
  • 56,175
  • 13
  • 112
  • 165