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