如何使窗口显示在一行中的单元格渲染器下方

时间:2012-12-05 19:07:56

标签: c gtk gtk2 gtktreeview

我有一个程序可以在GtkEntrysscce代码here)下打开一个窗口。程序运行时,这是它的窗口:

The window of the program before clicking in "Open"

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

Subwindow opened

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

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;
}

结果不是我的预期。未修饰的窗口出现在各个地方,通常位于屏幕的左上角:

The new window appears a the top left of the window

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

  

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

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

那么,我怎样才能打开特定单元格渲染器下的未修饰窗口?

0 个答案:

没有答案