Editing in GtkTreeView - Automatic Edit and Unwanted Edit Box?



I'm new to Gtk, so I'm not sure if I've looked in the right places yet, but I have tried to RTFM before asking these two questions.

I'm working on Windows XP with MSYS/Mingw and GTK+ 2.10.11 and I'm using Glade/libglade to define the UI. I've pasted in the relevant source code, which is very much a "work in progress", at the end of this message.

First question - is there any (preferably easy) way with a GtkTreeView to make any editable cell that is selected automatically enter edit mode and automatically commit the changes when focus moves elsewhere?

Second - in the application that I'm trying to develop I have a GtkTreeview with data in a GtkListStore and some, but not all, of the cells editable. If I have a cell selected, but not opened for editing (this includes non-editable cells) and I then start typing, an editable box appears in the bottom right-hand corner of the GtkTreeView, which displays what I'm typing. If I press enter, it goes away and the text of the selected cell is not updated. Does anyone know why this odd behaviour is happening and what I can do to stop it?

Here are the relevant bits of source code:

/* Notebook Page Numbers */
enum {
    NB_PAGE_TITLE,
    NB_PAGE_LABOUR
};

/* List column numbers */
enum {
    LABOUR_GROUP,
    LABOUR_OPS_SHIFT,
    LABOUR_SHIFTS,
    LABOUR_TOTAL_OPS,
    LABOUR_OVERTIME,
    LABOUR_HIRE,
    LABOUR_FIRE,
    LABOUR_RETRAIN_FROM,
    LABOUR_RETRAIN_TO,
    LABOUR_LEFT_PAD,
    LABOUR_N_COLUMNS
};

#ifdef G_OS_WIN32
    #define HANDLER __declspec(dllexport)
#endif

void labour_cell_edited (GtkCellRendererText *cell,
                         gchar               *path_string,
                         gchar               *new_text,
                         gpointer             model) {
    if(!model) {g_error("Unable to get model from cell renderer");}
    GtkTreeIter row;
    gtk_tree_model_get_iter_from_string(model, &row, path_string);
    guint column_no = GPOINTER_TO_UINT(g_object_get_data(
                                            G_OBJECT(cell), "column_no"));
    g_print("Column :%d, new text: \"%s\"\n", column_no, new_text);
};

HANDLER void on_labour_menu_item_activate(GtkWidget *widget, gpointer user_data) {
    gtk_notebook_set_current_page(
                GTK_NOTEBOOK(glade_xml_get_widget(xml, "main_notebook")),
                NB_PAGE_LABOUR);
GtkTreeView *view = GTK_TREE_VIEW(glade_xml_get_widget(xml, "labour_treeview"));

    /* If the TreeView hasn't been intialised, we set it up here */
    gtk_tree_view_set_grid_lines(view, GTK_TREE_VIEW_GRID_LINES_BOTH);
    if (!gtk_tree_view_get_model(view)) {
        GtkListStore *new_store = gtk_list_store_new (LABOUR_N_COLUMNS,
                                                      G_TYPE_STRING,
                                                      G_TYPE_INT,
                                                      G_TYPE_INT,
                                                      G_TYPE_INT,
                                                      G_TYPE_DOUBLE,
                                                      G_TYPE_INT,
                                                      G_TYPE_INT,
                                                      G_TYPE_INT,
                                                      G_TYPE_INT,
                                                      G_TYPE_STRING);
        gtk_tree_view_set_model(view, GTK_TREE_MODEL(new_store));

        GtkCellRenderer *renderer;
        GtkTreeViewColumn *column;

        /* Labour Group */
        renderer = gtk_cell_renderer_text_new();
        column = gtk_tree_view_column_new_with_attributes (
                                        "Labour Group", renderer,
                                        "text", LABOUR_GROUP,
                                        NULL);
        gtk_tree_view_column_set_resizable (column, TRUE);
        gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);

        /* Ops Per Shift */
        renderer = gtk_cell_renderer_text_new();
        g_object_set_data(G_OBJECT(renderer), "column_no",
                          GUINT_TO_POINTER(LABOUR_OPS_SHIFT));
        g_object_set(renderer, "editable", TRUE, "xalign", 1.0, NULL);
        g_signal_connect(renderer, "edited",
                         (GCallback) labour_cell_edited,
                         gtk_tree_view_get_model(view));
        column = gtk_tree_view_column_new_with_attributes (
                                        "Operators/Shift", renderer,
                                        "text", LABOUR_OPS_SHIFT,
                                        NULL);
        gtk_tree_view_column_set_resizable (column, TRUE);
        gtk_tree_view_column_set_alignment(column, 1.0);
        gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);

        /* Shifts */
        renderer = gtk_cell_renderer_text_new();
        g_object_set(renderer, /*"editable", TRUE,*/ "xalign", 1.0, NULL);
        column = gtk_tree_view_column_new_with_attributes (
                                        "Shifts", renderer,
                                        "text", LABOUR_SHIFTS,
                                        NULL);
        gtk_tree_view_column_set_resizable (column, TRUE);
        gtk_tree_view_column_set_alignment(column, 1.0);
        gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);

        /* Total operators */
        renderer = gtk_cell_renderer_text_new();
        column = gtk_tree_view_column_new_with_attributes (
                                        "Total Operators", renderer,
                                        "text", LABOUR_TOTAL_OPS,
                                        NULL);
        gtk_tree_view_column_set_resizable (column, TRUE);
        gtk_tree_view_column_set_alignment(column, 1.0);
        gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);

        /* Overtime */
        renderer = gtk_cell_renderer_text_new();
        column = gtk_tree_view_column_new_with_attributes (
                                        "Overtime Hrs/Op.", renderer,
                                        "text", LABOUR_OVERTIME,
                                        NULL);
        gtk_tree_view_column_set_resizable (column, TRUE);
        gtk_tree_view_column_set_alignment(column, 1.0);
        gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);

        /* Hire */
        renderer = gtk_cell_renderer_text_new();
        column = gtk_tree_view_column_new_with_attributes (
                                        "Hire", renderer,
                                        "text", LABOUR_HIRE,
                                        NULL);
        gtk_tree_view_column_set_resizable (column, TRUE);
        gtk_tree_view_column_set_alignment(column, 1.0);
        gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);

        /* Fire */
        renderer = gtk_cell_renderer_text_new();
        column = gtk_tree_view_column_new_with_attributes ("Fire",
                                                           renderer,
                                                           "text",
                                                           LABOUR_FIRE,
                                                           NULL);
        gtk_tree_view_column_set_resizable (column, TRUE);
        gtk_tree_view_column_set_alignment(column, 1.0);
        gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);

        /* Retrain From */
        renderer = gtk_cell_renderer_text_new();
        column = gtk_tree_view_column_new_with_attributes ("Retrain From",
                                                           renderer,
                                                           "text",

LABOUR_RETRAIN_FROM,
                                                           NULL);
        gtk_tree_view_column_set_resizable (column, TRUE);
        gtk_tree_view_column_set_alignment(column, 1.0);
        gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);

        /* Retrain To */
        renderer = gtk_cell_renderer_text_new();
        column = gtk_tree_view_column_new_with_attributes ("Retrain To",
                                                           renderer,
                                                           "text",

LABOUR_RETRAIN_TO,
                                                           NULL);
        gtk_tree_view_column_set_resizable (column, TRUE);
        gtk_tree_view_column_set_alignment(column, 1.0);
        gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);

        /* Padding */
        renderer = gtk_cell_renderer_text_new();
        column = gtk_tree_view_column_new_with_attributes ("",
                                                           renderer,
                                                           "text",
                                                           LABOUR_LEFT_PAD,
                                                           NULL);
        gtk_tree_view_column_set_resizable (column, TRUE);
        gtk_tree_view_column_set_alignment(column, 1.0);
        gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);
   }

    /* Now we clear any existing data in the model and load data from
       the labour file */
    GtkListStore *model = GTK_LIST_STORE(gtk_tree_view_get_model(view));
    gtk_list_store_clear(model);
    gchar *work_path;
    if (team_mode) {
        work_path = g_key_file_get_string(app_rc_key_file, "Paths",
                                          "team_work_path", NULL);
    }
    else {
        work_path = g_key_file_get_string(app_rc_key_file, "Paths",
                                          "personal_path", NULL);
    }
gchar *labour_path = g_build_filename (work_path, LABOUR_FILE_NAME, NULL);
    g_free(work_path);
    GIOChannel *channel = g_io_channel_new_file(labour_path, "r", NULL);
    GString *line = g_string_new("");
    while ((g_io_channel_read_line_string(channel, line,
NULL, NULL)) == G_IO_STATUS_NORMAL) {
        gchar *group;
        group = g_strstrip(g_strndup(line->str, 16));
        gchar *field;
        field = g_strndup(line->str + 16, 4);
        gint ops_shift = g_ascii_strtoll(field, NULL, 10);
        g_free(field);
        field = g_strndup(line->str + 20, 3);
        gint shifts = g_ascii_strtoll(field, NULL, 10);
        g_free(field);
        field = g_strndup(line->str + 23, 5);
        gdouble overtime = g_ascii_strtod(field, NULL);
        g_free(field);
        GtkTreeIter row;
        gtk_list_store_append(model, &row);
        gtk_list_store_set(model, &row,
                           LABOUR_GROUP,        group,
                           LABOUR_OPS_SHIFT,    ops_shift,
                           LABOUR_SHIFTS,       shifts,
                           LABOUR_TOTAL_OPS,    ops_shift * shifts,
                           LABOUR_OVERTIME,     overtime,
                           LABOUR_HIRE,         0,
                           LABOUR_FIRE,         0,
                           LABOUR_RETRAIN_FROM, 0,
                           LABOUR_RETRAIN_TO,   0,
                           -1);
        g_free(group);
    }
    g_io_channel_shutdown(channel, FALSE, NULL);
    g_io_channel_unref(channel);
}


Any hints to where I should be looking will be much appeciated.

TIA

Tony Cowderoy




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]