GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Oct 23, 2014 5:13 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: good programming: is this ok?
PostPosted: Thu Jun 13, 2013 9:01 am 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Hello friends,
copied below is a piece of code I am using.
It is working, but I don't know if this is working "well".
Comments needed.


##################
IN SUBROUTINE A
##################
Code:
     
    GtkWidget *create_view_and_model(void) {
      GtkCellRenderer *cell;
      void
        cell_edited(GtkCellRendererText *renderer,
            gchar *path,
            gchar *new_text,
            GtkTreeView *treeview);
      GtkTreeViewColumn *col_key,*col_year,*col_type,*col_auth,*col_title,*col_journal;
     
      store = gtk_list_store_new (NUM_COLS,
          G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
          G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
          G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
          G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
          G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
     
      tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
      g_object_unref(G_OBJECT(store));
      gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
     
     
      /* #1: KEY COLUMN */ 
      cell = gtk_cell_renderer_text_new ();
      g_object_set (cell, "editable", TRUE, NULL);
      g_signal_connect (cell, "edited",G_CALLBACK(cell_edited), tree);
      g_object_set_data (G_OBJECT (cell),
          "column", GINT_TO_POINTER (COL_BIB_KEY));
      col_key=gtk_tree_view_column_new_with_attributes (
          "Key", cell,
          "text", COL_BIB_KEY,
          NULL);
      gtk_tree_view_column_set_sort_column_id( col_key, ID_KEY);
      gtk_tree_view_append_column (GTK_TREE_VIEW (tree), col_key);
      gtk_tree_view_column_set_max_width  (col_key,100);
     
    /* Other Columns */
     
      return tree;
    }


##################
IN MAIN.C
##################

Code:
      gtk_container_add(GTK_CONTAINER(scrolledwindow), create_view_and_model());

NB: I specially want to know if the adding of create_view_model is ok


Top
 Profile  
 
 Post subject: Re: good programming: is this ok?
PostPosted: Fri Jun 14, 2013 6:19 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 764
Location: UK
Code wise it is OK, formatting wise it is not so great. It is a little bit inconsistent in the formatting, with a function declaration within a function. Personally I would like to see space around the '=' sign just to make them stand out.

_________________
E.


Top
 Profile  
 
 Post subject: Re: good programming: is this ok?
PostPosted: Fri Jun 14, 2013 7:21 am 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Errol,
So, if you were writing this, main.c would look like
Code:
tree = create_view_and_model()
gtk_container_add( GTK_CONTAINER(scrolledwindow), tree );


right?


Top
 Profile  
 
 Post subject: Re: good programming: is this ok?
PostPosted: Mon Jun 17, 2013 1:11 pm 
Offline
Familiar Face

Joined: Tue Jun 11, 2013 7:08 am
Posts: 8
Rudra,

I think that errol would speak about the cell_edited() function declaration into your create_view_and_model() function. It is not so easy to read.

Calling create_and_view first and storing result into a GtkWidget would have an interest if you want to control return value before... in order to avoid NULL pointer.

Code:
tree = create_view_and_model()
if (tree != NULL)
    gtk_container_add( GTK_CONTAINER(scrolledwindow), tree );


But, in your code, the called function never returns NULL, so, tree would never be null, and the if is not so important.

In general meaning, for reliability, you may always test return value of a called function... The GTK functions you are calling into create_view_and_model() may sometime fail (memory leaks, ...) and you're better test their return values in order to leave create_view_and_model() before end when exception happens, returning NULL...


Top
 Profile  
 
 Post subject: Re: good programming: is this ok?
PostPosted: Wed Nov 27, 2013 12:04 am 
Offline
GTK+ Guru

Joined: Fri Mar 25, 2011 5:16 pm
Posts: 177
Location: USA
Official GTK+ preferred Coding style: http://git.gnome.org/browse/gtk+/tree/docs/CODING-STYLE


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group