1

GtkEntry我有一个程序可以在(此处为sscce代码)下打开一个窗口。当程序运行时,这是它的窗口:

点击前的程序窗口

当我单击“打开”按钮时,将在条目正下方打开一个未装饰的窗口,并且其左侧与条目的左侧对齐

子窗口打开

这是按钮的以下回调方法的结果,该方法接收条目作为回调参数:

bool on_button_clicked(GtkWidget *button, gpointer data) {
    GtkWidget *entry = data,
            *subwindow = gtk_window_new(GTK_WINDOW_TOPLEVEL),
            *sublabel = gtk_label_new("This is a rather long message\n"
                    "with many lines whose mere purpose is\n"
                    "to test some stuff. It will appear in \n"
                    "the RIGHT PLACE.");
    gtk_container_add(GTK_CONTAINER(subwindow), sublabel);
    gtk_window_set_decorated(GTK_WINDOW(subwindow), FALSE);
    gtk_window_set_modal(GTK_WINDOW(subwindow), TRUE);


    GtkWidget *window = gtk_widget_get_toplevel(entry);
    gtk_window_set_transient_for(
            GTK_WINDOW(subwindow), GTK_WINDOW(window));

    gint dx, dy;
    gtk_window_get_position(GTK_WINDOW(window), &dx, &dy);

    GtkAllocation allocation;
    gtk_widget_get_allocation(entry, &allocation);

    gtk_window_move(
            GTK_WINDOW(subwindow), 
            allocation.x+dx, allocation.y+allocation.height*2+dy);

    gtk_widget_show_all(subwindow);
    return false;
}

当开始从表格视图编辑单元格时,我想做类似的事情,特别是如果单元格在第二列中。未装饰的窗口应出现在编辑单元格的正下方,与其左侧对齐。所以我创建了一个"editing-started"事件回调,假设editable回调的参数是一个小部件:

static bool on_cell_renderer_text_editing_started(
        GtkCellRenderer *renderer, GtkCellEditable *editable, 
        gchar *path, gpointer data) {
    GtkWidget *subwindow = gtk_window_new(GTK_WINDOW_TOPLEVEL),
            *sublabel = gtk_label_new("This is a rather long message\n"
                    "with many lines whose mere purpose is\n"
                    "to test some stuff. It will appear in\n"
                    "the WRONG PLACE!");
    gtk_container_add(GTK_CONTAINER(subwindow), sublabel);
    gtk_window_set_decorated(GTK_WINDOW(subwindow), FALSE);
    gtk_window_set_modal(GTK_WINDOW(subwindow), TRUE);

    GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(editable));
    gtk_window_set_transient_for(
            GTK_WINDOW(subwindow), GTK_WINDOW(window));

    gint dx, dy;
    gtk_window_get_position(GTK_WINDOW(window), &dx, &dy);

    GtkAllocation allocation;
    gtk_widget_get_allocation(GTK_WIDGET(editable), &allocation);

    gtk_window_move(
            GTK_WINDOW(subwindow),
           allocation.x+dx, allocation.y+allocation.height*2+dy);
    gtk_widget_show_all(subwindow);
    return false;
}

结果不是我所期望的。未装饰的窗口出现在各个地方,通常在屏幕的左上方:

新窗口出现在窗口的左上角

另外,我得到了这个(很有启发性但死胡同)警告:

(teste-subwindow-under-entry:32300):Gtk-CRITICAL **:IA__gtk_window_get_position:断言“GTK_IS_WINDOW(窗口)”失败

我尝试了一些其他方法(例如,使用 获取第一个窗口的尺寸gdk_window_get_geometry())但没有奏效(我并不感到惊讶,因为它们只是盲目尝试)。

那么,如何在特定的单元格渲染器下打开未装饰的窗口呢?

4

0 回答 0