improvements in gtktreeview



Dear friends, 
below  I have copied the way I am creating a treeview. Maybe, you may
find some part is not necessary, but still I put them to show you how I
am doing the things. 

I need help in few things:
1) for my typical testing file, which has ~150 entry(to be parsed by
parse.sh, create file "fauth.dat" and then read by fauth) It takes
around 5 second! I am afraid what will happen for a bigger file. I tried
to create a parser in C that will directly store the entries of
fauth.dat in array, but failed(Any help in this will be hugely
welcome). 
Is there anyway to sppedup the process ?
2) Entries in "COL_BIB_NAME" can be arbitraryly long(I have set up max
length to 500). Problem is in my present settings, the full line is
written in a single line. Can I set the width of each sell fixed? So,
that when the entry is more then the width, they will break up in
several line? In other word, currently, my cells have fixed hight,
variable width. can I change it to variable hight, fixed width?
3) These two is my matter of concern. But being a novice, I  am looking
for any other advice as well. 

static GtkTreeModel *
create_and_fill_model(void) {

    treestore = gtk_tree_store_new(NUM_COLS,
            G_TYPE_STRING,
            G_TYPE_STRING,
            G_TYPE_STRING,
            G_TYPE_STRING);
    return GTK_TREE_MODEL(treestore);
}

static GtkWidget *
create_view_and_model(void) {
    GtkTreeViewColumn *col;
    GtkCellRenderer *renderer;
    GtkWidget *view;
    GtkTreeModel *model;

    view = gtk_tree_view_new();

    /* --- Column #0 --- */
    col = gtk_tree_view_column_new();
    gtk_tree_view_column_set_title(col, "Type");
    gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
    renderer = gtk_cell_renderer_text_new();
    gtk_tree_view_column_pack_start(col, renderer, TRUE);
    gtk_tree_view_column_add_attribute(col, renderer, "text",
COL_BIB_TYPE);

    /* --- Column #1 --- */
    col = gtk_tree_view_column_new();
    gtk_tree_view_column_set_title(col, "Name");
    gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
    renderer = gtk_cell_renderer_text_new();
    gtk_tree_view_column_pack_start(col, renderer, TRUE);
    gtk_tree_view_column_add_attribute(col, renderer, "text",
COL_BIB_NAME);


    /* --- Column #2 --- */
    col = gtk_tree_view_column_new();
    gtk_tree_view_column_set_title(col, "Year");
    gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
    renderer = gtk_cell_renderer_text_new();
    gtk_tree_view_column_pack_start(col, renderer, TRUE);
    gtk_tree_view_column_add_attribute(col, renderer, "text",
COL_BIB_YEAR);
/*    g_object_set(renderer,
            "weight", PANGO_WEIGHT_BOLD,
            "weight-set", TRUE,
            NULL);
*/          

    /* --- Column #3 --- */
    col = gtk_tree_view_column_new();
    gtk_tree_view_column_set_title(col, "Journal");
    gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
    renderer = gtk_cell_renderer_text_new();
    gtk_tree_view_column_pack_start(col, renderer, TRUE);
    gtk_tree_view_column_add_attribute(col, renderer, "text",
COL_BIB_PUB);
    /* connect a cell data function */
    //  gtk_tree_view_column_set_cell_data_func(col, renderer,
age_cell_data_func, NULL, NULL);


    model = create_and_fill_model();

    gtk_tree_view_set_model(GTK_TREE_VIEW(view), model);

    g_object_unref(model); /* destroy model automatically with view */


gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(view)),
            GTK_SELECTION_NONE);

    return view;
}

static void open_file(GtkWidget *widget, gpointer data) {
    GtkWidget *dialog; //, *entry;
    GtkFileFilter *filter;
    dialog = gtk_file_chooser_dialog_new("Open File", NULL,
            GTK_FILE_CHOOSER_ACTION_OPEN,
            GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
            NULL);

    filter = gtk_file_filter_new();
    gtk_file_filter_set_name(filter, "All files (*.*)");
    gtk_file_filter_add_pattern(filter, "*");
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);

    filter = gtk_file_filter_new();
    gtk_file_filter_set_name(filter, "Bibtex file (*.bib)");
    gtk_file_filter_add_pattern(filter, "*.bib");
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
    gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);

    if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
        filename =
gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
        char pcomm[1028];
        sprintf(pcomm, "bash parse.sh %s", filename);
        g_print("%s", pcomm);
        system((char *) pcomm);
    }
    int i=0;//,j=0,k=0;
    int num_line=0, ch;
    FILE *fauth, *fyear, *ftitle;
   FILE* tfauth=fopen("fauth.dat","r");
    do{
      ch=fgetc(tfauth);
      if(ch == '\n')
       num_line++;
      }while (ch != EOF);
/*   if(ch != '\n' && num_line != 0) 
    num_line++;*/
    fclose(tfauth);
      printf("%d\n",num_line);

    char
aauth[num_line][500],ayear[num_line][15],atitle[num_line][500];//,
buffer[500];

    fauth=fopen("fauth.dat","r");
    fyear=fopen("fyear.dat","r");
    ftitle=fopen("ftitle.dat","r");
                                   
    if(!fauth||!ftitle||!fyear){
            printf("fauth failed\n");
    }
    else{
  while (i < num_line && fgets(aauth[i],  sizeof aauth[i], fauth)
                  && fgets(ayear[i],  sizeof ayear[i], fyear)
                  && fgets(atitle[i],  sizeof atitle[i], ftitle))
          i++;
    }
        fclose(fauth);
        fclose(fyear);
        fclose(ftitle);
    for (i = 0; i < num_line; i++){
                gtk_tree_store_append(GTK_TREE_STORE(treestore),
&toplevel,NULL);
                gtk_tree_store_set(treestore, &toplevel,
                      COL_BIB_TYPE, NULL,
                      COL_BIB_NAME, aauth[i],
                      COL_BIB_YEAR, ayear[i],
                      COL_BIB_PUB,  atitle[i],
                      -1);
    }

    gtk_label_set_text(GTK_LABEL(flabel), filename);
    gtk_widget_destroy(dialog);
}





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