GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Sep 23, 2014 10:22 am

All times are UTC




Post new topic Reply to topic  [ 15 posts ] 
Author Message
 Post subject: need help in tightening this function.
PostPosted: Thu Jan 10, 2013 4:07 am 
Offline
GTK+ Geek

Joined: Sun Dec 04, 2011 9:29 pm
Posts: 71
Location: Seattle, WA
guys,

can anybody help me figure out if there is a way to tighten this function? Even without the comments, it is over 80 lines. All it does is open a file and print in within a dialog, I use one of these functions to describe how-to use my program; and at least four other cases within the top menu.

Code:
void
show_instructions_cb (void)
{

   GtkWidget *window, *view, *vbox, *separator;
   GtkWidget *close, *hbox;
   GtkWidget *frame;
   GtkTextBuffer *buffer;
   GtkTextIter iter;
   FILE *infile;
   gchar file_buff[MAXCHAT];
   gchar instruction[32];
   gint nchars;


   /*
    * set up file to be read into frame
    */
   sprintf (instruction, "%s%s", vhome,INSTRUCT);/* name of instructions file */

   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   gtk_window_set_default_size (GTK_WINDOW (window), 550, 525);

   gtk_window_set_title (GTK_WINDOW (window), "Instructions");
   /* this tells the window manager the smallest size
    * good idea to set this, else the UI can start to look goofy
    */
   gtk_widget_set_usize (window, 200, 200);
   gtk_container_set_border_width (GTK_CONTAINER (window), 8);

   /* I add a frame around the vbox to make things look better */
   frame = gtk_frame_new ("Let the Computer Be Your Voice");
   gtk_container_add (GTK_CONTAINER (window), frame);

   /* at this point we have:
    * window with a frame inside it ( a frame is a container )
   vbox = gtk_vbox_new (FALSE, 5);
   gtk_container_add (GTK_CONTAINER (frame), vbox);

   /* now we added a vbox container, to our frame*/
   view = gtk_text_view_new ();
   gtk_container_set_border_width (GTK_CONTAINER (view), 6);
   gtk_container_add (GTK_CONTAINER (vbox), view);

   /* added the text view to the vbox */
   separator = gtk_hseparator_new ();
   gtk_box_pack_start (GTK_BOX (vbox), separator, FALSE, TRUE, 0);
 
   /* using box packing allows automatic sizing, good idea for
    * a seperator (let gtk figure it out)
    * so now we packed a seperator below our text view
    */
   hbox = gtk_hbox_new (FALSE, 5);

   /* fetch the buffer from the text view and get offset 0 */
   buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
   gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);

   /* pull in data from instructions file and insert starting at iter */
   chdir (vhome);
   infile = fopen (instruction, "r");
   if (infile)
     {
        while (1)
          {
             nchars = fread (file_buff, 1, MAXCHAT, infile);
             gtk_text_buffer_insert (buffer, &iter, file_buff, nchars);
             if (nchars < MAXCHAT)
                break;
          }
        fclose (infile);
     }
   else
      g_print ("\nERROR! could not open file %s\n", instruction);
   hbox = gtk_hbutton_box_new ();
   gtk_button_box_set_layout (GTK_BUTTON_BOX (hbox), GTK_BUTTONBOX_END);
   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);

   close = gtk_button_new_with_label ("Close");
   gtk_container_add (GTK_CONTAINER (hbox), close);
   g_signal_connect (close, "clicked", G_CALLBACK (remove_text_window),
                     window);

   gtk_widget_show_all (window);
   return;
}


Top
 Profile  
 
 Post subject: Re: need help in tightening this function.
PostPosted: Thu Jan 10, 2013 1:41 pm 
Offline
Familiar Face

Joined: Thu Aug 27, 2009 9:44 pm
Posts: 21
I appended my implementation that contains several corrections of your version.

The most important correction is that i don't create the window by hand but use an instance of GtkDialog.

If your project is bigger than 500lines i further recommend you that you put the dialog related things into a separate file. So that each file is related to a logical consistent part of your program. Further you should try to outsource code into functions and use data structures.

Please don't look at the total of line numbers, since this is a weak criterion for the quality of code.

Probably my code isn't really wise at well and maybe it doesn't meet your taste, but at least it gives you a new perspective onto the "same" thing.

Code:
#include <gtk/gtk.h>
#include <glib.h>

#define MAXCHAR 255
#define INSTRUCT "info"

static char vhome[] = { "/home/user/" };


typedef struct {
    GtkWidget *main;
    GtkWidget *content_area;
    GtkWidget *frame;
    GtkWidget *view;
} Dialog;


void
show_instructions_create_dialog(Dialog *dialog, GtkWindow *parent) {

    dialog->main = gtk_dialog_new_with_buttons("title",
       parent,
       GTK_DIALOG_MODAL,
       GTK_STOCK_CLOSE,
       0, NULL);

    dialog->content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog->main));

    gtk_window_resize (GTK_WINDOW(dialog->main), 400, 200);
   
    gtk_container_set_border_width (GTK_CONTAINER (dialog->main), 8);

    dialog->frame = gtk_frame_new ("Let the Computer Be Your Voice");
    gtk_box_pack_start (GTK_BOX (dialog->content_area), dialog->frame, TRUE, TRUE, 0);

    /* now we added a vbox container, to our frame*/
    dialog->view = gtk_text_view_new ();
    gtk_container_set_border_width (GTK_CONTAINER (dialog->view), 6);
    gtk_container_add (GTK_CONTAINER (dialog->frame), dialog->view);
}


void
show_instrcutions_put_text_into_buffer(Dialog *dialog) {

    GtkTextBuffer *buffer;
    GtkTextIter iter;
    FILE *infile;
    gchar file_buff[MAXCHAR];
    guint nchars;

    /* fetch the buffer from the text view and get offset 0 */
    buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (dialog->view));
    gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);

    /* pull in data from instructions file and insert starting at iter */
    chdir (vhome);
    infile = fopen (INSTRUCT, "r");
    if (infile)
    {
   do {
       nchars = fread (file_buff, 1, MAXCHAR, infile);
       gtk_text_buffer_insert (buffer, &iter, file_buff, nchars);
   } while (nchars == MAXCHAR);

   fclose (infile);
    }
    else
   g_print ("\nERROR! could not open file %s\n", INSTRUCT);
}


void
show_instructions_cb (GtkWidget *widget, gpointer data)
{

    Dialog *dialog = g_new(Dialog, 1);

    show_instructions_create_dialog(dialog, NULL);
    show_instrcutions_put_text_into_buffer(dialog);

    gtk_widget_show_all(dialog->main);

    gtk_dialog_run(GTK_DIALOG(dialog->main));

    gtk_widget_destroy(dialog->main);

    g_free(dialog);
}


int main(int argc, char *argv[]) {

    gtk_init(&argc, &argv);

    show_instructions_cb(NULL, NULL);

    return 0;
}


Top
 Profile  
 
 Post subject: Re: need help in tightening this function.
PostPosted: Sat Jan 12, 2013 3:10 am 
Offline
GTK+ Geek

Joined: Sun Dec 04, 2011 9:29 pm
Posts: 71
Location: Seattle, WA
thanks for your code and data structs. got to get a printout and sit in a corner and study. [i have trouble reading too much online.]

the reason i was pondering the 80+ line was because i am trying to keep the program small. i recently switched to linux from freebsd and my old style Makefile fails ... or perhaps it is something
in gtk. in any event, i cant seem to have more than one file, or the makefile fails.

the entire file was just under 1,000 lines; then i discovered i was missing my version of a cut down grspeaker. {my program is for the speech impaired. my version of gespeaker added 700 lines.} Sigh.

if i paste my Makefile, can you or someone else of this forum clue me in on what i need to do to break up my VBC [voice by computer] into at least three files?

thanks again!


Top
 Profile  
 
 Post subject: Re: need help in tightening this function.
PostPosted: Sat Jan 12, 2013 9:08 am 
Offline
Familiar Face

Joined: Thu Aug 27, 2009 9:44 pm
Posts: 21
kline wrote:
thanks for your code and data structs. got to get a printout and sit in a corner and study. [i have trouble reading too much online.]

the reason i was pondering the 80+ line was because i am trying to keep the program small. i recently switched to linux from freebsd and my old style Makefile fails ... or perhaps it is something
in gtk. in any event, i cant seem to have more than one file, or the makefile fails.

the entire file was just under 1,000 lines; then i discovered i was missing my version of a cut down grspeaker. {my program is for the speech impaired. my version of gespeaker added 700 lines.} Sigh.

1700 lines are pretty much for one file and likely different parts of your code (like the gespeaker part) are related to different tasks. So i highly recommend you to split your program into smaller interrelated parts. This is a tremendous help to cope with the complexity of your program and the related concept. To get a deeper grasp of what i mean i suggest you to study articles about modular programming. If you google for "modular programming in c" you get several hits. The hard thing about it is not the understanding but it's strict implementation. By experience the more planning you have done in advance the easier it is to make your project strictly modular.

Quote:
if i paste my Makefile, can you or someone else of this forum clue me in on what i need to do to break up my VBC [voice by computer] into at least three files?

thanks again!


You are welcome to post your Makefile, but it would make more sense if you post your whole project (at least for me)

best regards


Top
 Profile  
 
 Post subject: Re: need help in tightening this function.
PostPosted: Sat Jan 12, 2013 10:54 pm 
Offline
GTK+ Geek

Joined: Sun Dec 04, 2011 9:29 pm
Posts: 71
Location: Seattle, WA
Hm, how exactly can i post 1700 lines of code? this includes the voice-options of 700 lines {that has one glaring C error} and the rest of 900 to 1000 lines. it is in two files vbc.c and vbc.h; then the Makefile.

so far, i have been mouse-swiping and then mouse-pasting it into this "Post a reply" window. is there another way?


Top
 Profile  
 
 Post subject: Re: need help in tightening this function.
PostPosted: Sat Jan 12, 2013 11:32 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 747
Location: UK
Hi,

It is probably best not to post any piece of code that big here. There is also a limit on the size of posting you can put here.

If you have a place we can access your project then that may well be better.

_________________
E.


Top
 Profile  
 
 Post subject: Re: need help in tightening this function.
PostPosted: Sun Jan 13, 2013 2:23 am 
Offline
GTK+ Geek

Joined: Sun Dec 04, 2011 9:29 pm
Posts: 71
Location: Seattle, WA
errol wrote:
Hi,

It is probably best not to post any piece of code that big here. There is also a limit on the size of posting you can put here.

If you have a place we can access your project then that may well be better.


well, having done html by-hand since 1993, it sure took me long enough!! aw well. anyway, all three files: vbc.c , vbc.h, and the Makefile sre at http:/www.thought.org/vbc/src/index.html . if you have any trouble finding my src files, let me know.

Again, note that the Option/Setvoice piece is messed up. for some reason, that piece fails to save the voice data, so only the default is available. the voice is male and it speaks at something around 191 words/minute.

i'll fix what i can in this; what i really want to do is break it up into several *.c files.

thanks for any clues... .


Top
 Profile  
 
 Post subject: Re: need help in tightening this function.
PostPosted: Sun Jan 13, 2013 8:03 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 747
Location: UK
Hi Gary,

I have now got your code to look at. Do you want to stay with GTK+ version 2 and if so which version do you want to stay with?

_________________
E.


Top
 Profile  
 
 Post subject: Re: need help in tightening this function.
PostPosted: Sun Jan 13, 2013 7:52 pm 
Offline
GTK+ Geek

Joined: Sun Dec 04, 2011 9:29 pm
Posts: 71
Location: Seattle, WA
errol wrote:
Hi Gary,

I have now got your code to look at. Do you want to stay with GTK+ version 2 and if so which version do you want to stay with?


i am not sure. if you mean: "do i want to move to GTK, version 3?" the answer is yes. i intend to work on this for some time to come---a year or two at least. so it makes sense to stay with the latest version of GTK+ that there is.

what would you suggest?

gary


Top
 Profile  
 
 Post subject: Re: need help in tightening this function.
PostPosted: Sun Jan 13, 2013 10:07 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 747
Location: UK
Moving to GTK+ v3 would always be a good idea as most Linux distributions have or are moving to that version. Though some people do have good reasons to stay with version 2.

I have already looked at your code. There are obvious places where the code can be split to separate files to make your code more logical. Your code also contains sections that is not used at all. With some work it should be possible to get your project working so that it is in separate sections and works better.

_________________
E.


Top
 Profile  
 
 Post subject: Re: need help in tightening this function.
PostPosted: Sun Jan 13, 2013 11:11 pm 
Offline
GTK+ Geek

Joined: Sun Dec 04, 2011 9:29 pm
Posts: 71
Location: Seattle, WA
i would be very much obliged for any help you can give me. --i know that there were some places not use as well as some variables that i stuck in before having better thoughts and not using!

thanks in advance for your insights!


Top
 Profile  
 
 Post subject: Re: need help in tightening this function.
PostPosted: Thu Jan 17, 2013 4:57 am 
Offline
GTK+ Geek

Joined: Sun Dec 04, 2011 9:29 pm
Posts: 71
Location: Seattle, WA
errol wrote:
Moving to GTK+ v3 would always be a good idea as most Linux distributions have or are moving to that version. Though some people do have good reasons to stay with version 2.

I have already looked at your code. There are obvious places where the code can be split to separate files to make your code more logical. Your code also contains sections that is not used at all. With some work it should be possible to get your project working so that it is in separate sections and works better.


Well, i stilll need help in "close" button in the Options/VoiceSet widget. i successfully read in the
saved data, i can change it, but when i hit the "Close" [or "Reset"] button, zip. i've made minor
changes to what i have in my html src code. Why wont the widget close? And_Or, how do i get
"Reset" working? i'm lost.

When i hit ctrl-C to get out of the application, my ~/.VBC/.Config is all 0's.


Top
 Profile  
 
 Post subject: Re: need help in tightening this function.
PostPosted: Thu Jan 17, 2013 7:30 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 747
Location: UK
Do you use anything like Sourceforge to host your project? This would make is easier to share changes and track back what has been done.

Options is the latest section that I am working on, which I have now separated out into a file of its own. So far I have 4 C files and one header file. The main C file is now just over 900 lines with the others being very much shorter.

Many of the things that I have done so far are to :-
- Split the project to logical sections, for example put a type of window into a C file of its own
- Remove some of the dead code
- Change some of the code so that it does not use deprecated functions. So far I am targeting GTK+ 2.24
- Prefer library functions rather that using our own functions.

If you do not use anything like Sourceforge what would be the best way to share my changes back to you?

_________________
E.


Top
 Profile  
 
 Post subject: Re: need help in tightening this function.
PostPosted: Thu Jan 17, 2013 9:25 pm 
Offline
GTK+ Geek

Joined: Sun Dec 04, 2011 9:29 pm
Posts: 71
Location: Seattle, WA
hi errol,

WOW, it sounds liked you've done a lot! it points out my sheer ignorance not only of things-gtk, but of the ways of sharing code via sites of the web. i have an account of Sourceforge, but don't have a
=clue= about asking how-to set up a place for my project. [if you do then perhaps you can help me there.] i do Not care who gets the credit for this speech program; the only thing that counts is that the project gets done. ---FWIW, i Just heard from a speech therapist down in OZ who is svery interested this voice-by-computer program.

my email address is kline@thought.org if we can share code via email. i have been using RCS for many years. i do know CVS, but just barely.

once the voice-selections part is done, i was hoping to just =give= this project to the gnu-accessibility folks or the OLPC project. {they are at laptop.org} to me, the ideas that 'small-is beautiful' and 'keep is simple, Sir' win over complexity. so VBC is pretty close to being finished.

gary


Top
 Profile  
 
 Post subject: Re: need help in tightening this function.
PostPosted: Sun Jan 20, 2013 10:01 am 
Offline
GTK+ Geek

Joined: Sun Dec 04, 2011 9:29 pm
Posts: 71
Location: Seattle, WA
I have it on google. a forum for discussion, and google.code using subversion for VCS.

:-)


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


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