GTK Problem



Hi, everyone. I'm writing an open-source text editor with wGLADE and Dev-C++ w/DevEx (which provides the GTK libs) named Free Notepad.

I'm having trouble with opening files. What happens is that when I click "Open File" under the "File" menu, a GTK file_selection window appears. I select the file, and it loads its contents into the editor.

The problem comes when I go to open a new file. I designed the program to open up a new window if the contents of the first have been changed (such as through opening a file). The program appears to work correctly, opening a new window with the file contents, but when I close one of the windows, they *all* close.

I can't figure out what's wrong with this. The relevant code from "callbacks.c" is below. Any help would be appreciated. Thanks.

BTW, any tips on making my code more compliant to the GNU Coding Standard would be nice, too. I figure they know what they're doing.

-J. Jensen

Code Snips (Comments are added for this post;
they're not in the real thing)
=============================================
...
#include <stdio.h>
...

struct _temp
{
   gboolean file_selected;
};

static struct _temp temp;
static guint32 buffer_size = 10240;
static gboolean changed = FALSE;

...

void
on_mnuFileOpen_activate (GtkMenuItem *menuitem, gpointer user_data)
{
   FILE *in_file;
   GtkWidget *file_opener, *window, *text;

   temp.file_selected = FALSE;

   file_opener = create_file_opener();
   gtk_widget_show(file_opener);
   gtk_main();

   if (temp.file_selected == FALSE) return;

   if (changed == TRUE)
     {
       window = create_window();
       text = lookup_widget(window, "text");
     }
   else
     {
       window = lookup_widget (GTK_WIDGET (menuitem), "window");
       text = lookup_widget (window, "text");
     }

/* Some of the following was all but copied-and-pasted from the "callbacks.c" file of the "editor" example project that came with DevEx. */
   in_file = fopen
       (gtk_file_selection_get_filename (GTK_FILE_SELECTION(file_opener)),
        "r");
   for (;;)
     {
       char str[buffer_size];
       guint32 nread = fread(&str, sizeof (char), buffer_size, in_file);
       if (nread > 0)
         {
           gtk_text_insert (GTK_TEXT(text), NULL, NULL, NULL, str, nread);
         }
       if (feof(in_file)) break;
     }
   fclose(in_file);

   gtk_widget_destroy (file_opener);
   if (changed == TRUE) gtk_widget_show(window);
   changed = TRUE;

   gtk_main();
}

...

void
on_btnOpenFile_clicked (GtkButton *button, gpointer user_data)
{
/* This function is called when the "OK" button is clicked in the file_opener from on_mnuFileOpen_activate. */

   temp.file_selected = TRUE;
   gtk_main_quit();
}

...

_________________________________________________________________
MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx




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