GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Oct 20, 2014 10:52 pm

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Newbie Question - Dialog Box Leaves Trail
PostPosted: Wed Oct 23, 2013 10:04 pm 
Offline

Joined: Wed Oct 23, 2013 9:12 pm
Posts: 3
I am writing a basic image viewer to learn some C and I am using MinGW with GTK for user interface, with Windows XP as operating system.

I have a file open dialog which works OK but leaves a trail of window outlines when moved across screen. The main app window does not do this just the file open dialog.

I do notice that the lines appear only when the dialog box is dragged in front of the main app window. If main app window is resized to take up just portion of screen the dialog only causes lines when it crosses in front of this main window, not when dragged over the desktop itself.

Is there anything I should be doing to get around this issue?




Code:
#include <gtk\gtk.h>
#include <string.h>
#include <stdio.h>


GtkWidget *window;
GtkWidget *myimage;
GtkWidget *statusbar;
GtkWidget *toolbar;
GtkWidget *menubar;
GtkWidget *contentbox;


gboolean fullscreen;

GSList *fileslist;
guint filecount;
gchar currentfolder[200]; //= " ";


void alert(gchar *c) {
   
GtkWidget *dialog;
dialog = gtk_message_dialog_new (GTK_WINDOW(window),
         GTK_DIALOG_DESTROY_WITH_PARENT,
           GTK_MESSAGE_ERROR,
         GTK_BUTTONS_CLOSE,
         "Output: '%s': %s",
         c, NULL);
gtk_dialog_run (GTK_DIALOG (dialog));

}


void destroy(GtkWidget *widget, gpointer data) {
   gtk_main_quit();
   //gtk_widget_hide(widget);
}

void fileopen2(GtkWidget *widget, gpointer data){
   GtkWidget *filechooser;
   filechooser = gtk_file_chooser_dialog_new ("Open file",
                  GTK_WINDOW(window),
                  GTK_FILE_CHOOSER_ACTION_OPEN,
                  GTK_STOCK_CANCEL,
                  GTK_RESPONSE_CANCEL,
                  GTK_STOCK_OPEN,
                  GTK_RESPONSE_ACCEPT,
                  NULL );

   /*set current folder*/
   //if (strcmp(currentfolder, NULL)){

   //gchar path[] = " ";  //dont delete prints int to status bar
   //path[0] = 'C' + ':' + '\\';
   //currentfolder[] = " ";  //dont delete prints int to status bar
   //currentfolder[0] = 'C' + ':' + '\\';


      //gchar* imgpath = g_strescape(filename, NULL);
      gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(filechooser),
         &currentfolder[0]);//(gchar*)currentfolder);//g_strescape(currentfolder, NULL));
   //}


   /*add file filter*/
   GtkFileFilter *gff = gtk_file_filter_new();
   gtk_file_filter_add_pattern(gff, "*.jpg");
   gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(filechooser), gff);
   //gtk_file_chooser_select_all(GTK_FILE_CHOOSER(filechooser));


  if (gtk_dialog_run (GTK_DIALOG (filechooser)) == GTK_RESPONSE_ACCEPT){
   /*get filename*/
   char *filename;
   filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (filechooser));


   /*get current folder*/
   gchar *cf;
   cf = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(filechooser));

        int i;
        for (i = 0; i < strlen(cf); i++){
                      //printf("%c", filename[i]);
         currentfolder[i] = (gchar)cf[i];
         //alert(&currentfolder[i]);
         //alert( &filename[i]);
   }
         //alert(&currentfolder[0]);


   gtk_statusbar_push(GTK_STATUSBAR(statusbar), 0, &currentfolder[0]);



   /*initialise filecount*/
   filecount = 0;

   //gint idx;
   //idx = g_slist_index(fileslist, filename);
   //filecount = idx; //not working properly

   gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(filechooser), TRUE);
   gtk_file_chooser_select_all(GTK_FILE_CHOOSER(filechooser));
   fileslist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(filechooser));

   
   //gint ix;//for test
   //ix =  2;//for test

   //gchar txt[] = " ";  //dont delete prints int to status bar
   //txt[0] = '2' + idx;
   //gtk_statusbar_push(GTK_STATUSBAR(statusbar), 0, (gchar*)txt);//txt is ptr

   //gtk_statusbar_push(GTK_STATUSBAR(statusbar), 0, filename);//txt is ptr

   /* iterate through fileslist to find where filecount should be set*/
   GSList *iterator = NULL;
   i = 0;
   for (iterator = fileslist; iterator; iterator = iterator->next) {
      //gtk_statusbar_push(GTK_STATUSBAR(statusbar), 0,(char*)iterator->data);//txt is ptr
      //printf("%s ", (char*)iterator->data);
      if ( strcmp(((char*)iterator->data), filename) == 0 ) {
         //gtk_statusbar_push(GTK_STATUSBAR(statusbar), 0, "found!!!");//txt is ptr
         filecount = i;
      }
      i = i + 1;
   }
   


   /*replace image with new*/
   gchar* imgpath = g_strescape(filename, NULL);
   gtk_image_set_from_file(GTK_IMAGE(myimage), imgpath);
   //gtk_statusbar_push(GTK_STATUSBAR(statusbar), 0, filename);
  }
 
gtk_widget_destroy (filechooser);

}


void gobig(){
   if (fullscreen == FALSE){
       gtk_widget_hide(statusbar);
       gtk_widget_hide(menubar);
       gtk_widget_hide(toolbar);
            gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
       gtk_window_fullscreen(GTK_WINDOW(window));
       fullscreen = TRUE;
   }
   else{
       gtk_widget_show(statusbar);
       gtk_widget_show(menubar);
       gtk_widget_show(toolbar);
            gtk_window_set_decorated(GTK_WINDOW(window), TRUE);
       gtk_window_unfullscreen(GTK_WINDOW(window));
       fullscreen = FALSE;
   }
}


gboolean key_event(GtkWidget *widget, GdkEventKey *event){
   g_printerr("%s\n",
   gdk_keyval_name (event->keyval));

   if ((event->keyval == gdk_keyval_from_name("Escape"))
      && (fullscreen == TRUE)) {
      gobig();
   }
   else {   
      //alert(gdk_keyval_name (event->keyval));
      //alert(ch);
   }
   if ((event->keyval == gdk_keyval_from_name("f"))
      && (fullscreen == FALSE)) {
      gobig();
   }
   if ((event->keyval == gdk_keyval_from_name("w")) &&
      filecount < (g_slist_length(fileslist) - 1)) {
      /*replace image with new*/
      filecount = filecount + 1;
      gchar *filename;
      filename = (gchar*) g_slist_nth_data(fileslist, filecount);
      gchar* imgpath = g_strescape(filename, NULL);
      gtk_image_set_from_file(GTK_IMAGE(myimage), imgpath);
      //gtk_statusbar_push(GTK_STATUSBAR(statusbar), 0, filename);
 
   }   
   if ((event->keyval == gdk_keyval_from_name("q")) &&
      filecount > 0) {
      /*replace image with new*/
      filecount = filecount - 1;
      gchar *filename;
      filename = (gchar*) g_slist_nth_data(fileslist, filecount);
      gchar* imgpath = g_strescape(filename, NULL);
      gtk_image_set_from_file(GTK_IMAGE(myimage), imgpath);
      //gtk_statusbar_push(GTK_STATUSBAR(statusbar), 0, filename);
   }   
   return FALSE;
}




int main( int argc, char *argv[]) {
   /*declare vars*/
   //GtkWidget *window;

   //currentfolder[0] = 'C' + ':' + '\\';

   GtkWidget *mainbox;

   //GtkWidget *menubar;
   GtkWidget *filemenu;
   GtkWidget *file;
   GtkWidget *open;
   GtkWidget *close;
   GtkWidget *quit;
   GtkWidget *imagemenu;
   GtkWidget *image;
   GtkWidget *information;
   GtkWidget *optionsmenu;
   GtkWidget *options;
   GtkWidget *configuration;
   GtkWidget *viewmenu;
   GtkWidget *view;
   GtkWidget *zoomplus;
   GtkWidget *zoomminus;
   GtkWidget *helpmenu;
   GtkWidget *help;
   GtkWidget *about;

   //GtkWidget *toolbar;
   GtkToolItem *topen;
   GtkToolItem *tsave;
   GtkToolItem *tback;
   GtkToolItem *tforward;

   GtkWidget *contenteventbox;
   //GtkWidget *contentbox;

   //GtkWidget *myimage;

   //GtkWidget *statusbar;

   /*initiate*/   
   gtk_init(&argc, &argv);

   /*create window*/
   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   fullscreen = FALSE;
   gtk_window_set_title (GTK_WINDOW (window), "Image Viewer");
   gtk_window_maximize(GTK_WINDOW(window));

   /*create main container*/
   mainbox = gtk_vbox_new(FALSE, 0);
   gtk_container_add(GTK_CONTAINER(window), mainbox);

   /*create menu*/
   menubar = gtk_menu_bar_new();
   filemenu = gtk_menu_new();
   file = gtk_menu_item_new_with_label("File");
   open = gtk_menu_item_new_with_label("Open");
   close = gtk_menu_item_new_with_label("Close");
   quit = gtk_menu_item_new_with_label("Quit");
   gtk_menu_item_set_submenu(GTK_MENU_ITEM(file), filemenu);
   gtk_menu_shell_append(GTK_MENU_SHELL(menubar), file);
   gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), open);
   gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), close);
   gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), quit);
   imagemenu = gtk_menu_new();
   image = gtk_menu_item_new_with_label("Image");
   information = gtk_menu_item_new_with_label("Information");
   gtk_menu_item_set_submenu(GTK_MENU_ITEM(image), imagemenu);
   gtk_menu_shell_append(GTK_MENU_SHELL(menubar), image);
   gtk_menu_shell_append(GTK_MENU_SHELL(imagemenu), information);
   optionsmenu = gtk_menu_new();
   options = gtk_menu_item_new_with_label("Options");
   configuration = gtk_menu_item_new_with_label("Configuration");
   gtk_menu_item_set_submenu(GTK_MENU_ITEM(options), optionsmenu);
   gtk_menu_shell_append(GTK_MENU_SHELL(menubar), options);
   gtk_menu_shell_append(GTK_MENU_SHELL(optionsmenu), configuration);
   viewmenu = gtk_menu_new();
   view = gtk_menu_item_new_with_label("View");
   zoomplus = gtk_menu_item_new_with_label("Zoom +");
   zoomminus = gtk_menu_item_new_with_label("Zoom -");
   gtk_menu_item_set_submenu(GTK_MENU_ITEM(view), viewmenu);
   gtk_menu_shell_append(GTK_MENU_SHELL(menubar), view);
   gtk_menu_shell_append(GTK_MENU_SHELL(viewmenu), zoomplus);
   gtk_menu_shell_append(GTK_MENU_SHELL(viewmenu), zoomminus);
   helpmenu = gtk_menu_new();
   help = gtk_menu_item_new_with_label("Help");
   about = gtk_menu_item_new_with_label("About");
   gtk_menu_item_set_submenu(GTK_MENU_ITEM(help), helpmenu);
   gtk_menu_shell_append(GTK_MENU_SHELL(menubar), help);
   gtk_menu_shell_append(GTK_MENU_SHELL(helpmenu), about);
   gtk_box_pack_start(GTK_BOX(mainbox), menubar, FALSE, FALSE, 0);

   /*create toolbar*/
   toolbar = gtk_toolbar_new();
   gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
   gtk_container_set_border_width(GTK_CONTAINER(toolbar), 2);
   topen = gtk_tool_button_new_from_stock(GTK_STOCK_OPEN);
   gtk_toolbar_insert(GTK_TOOLBAR(toolbar), topen, -1);
   tsave = gtk_tool_button_new_from_stock(GTK_STOCK_SAVE);
   gtk_toolbar_insert(GTK_TOOLBAR(toolbar), tsave, -1);

   GtkWidget *backimg;
   backimg = gtk_image_new_from_file("C:\\Documents and Settings\\Owner\\My Documents\\icback.png");
   tback = gtk_tool_button_new (backimg, "");
   gtk_toolbar_insert(GTK_TOOLBAR(toolbar), tback, -1);

   GtkWidget *forwardimg;
   forwardimg = gtk_image_new_from_file("C:\\Documents and Settings\\Owner\\My Documents\\icforward.png");
   tforward = gtk_tool_button_new (forwardimg, "");
   gtk_toolbar_insert(GTK_TOOLBAR(toolbar), tforward, -1);
   
   gtk_box_pack_start(GTK_BOX(mainbox), toolbar, FALSE, FALSE, 0);

   /*create content container*/
   contenteventbox = gtk_event_box_new();
   contentbox = gtk_vbox_new(FALSE, 0);
   gtk_container_add(GTK_CONTAINER(contenteventbox), contentbox);
   GdkColor bgcolor;
   bgcolor.red = 0;
   bgcolor.green = 0;
   bgcolor.blue = 0;
   gtk_widget_modify_bg(contenteventbox, GTK_STATE_NORMAL, &bgcolor);
   gtk_container_add(GTK_CONTAINER(mainbox), contenteventbox);
   gtk_box_pack_start(GTK_BOX(mainbox), contentbox, FALSE, FALSE, 5);

   /*create image*/
   //myimage = gtk_image_new_from_file("C:\\Documents and Settings\\Owner\\My Documents\\balloons.jpg");
   myimage = gtk_image_new();
   gtk_box_pack_start(GTK_BOX(contentbox), myimage, FALSE, FALSE, 3);

   /*create status bar*/
   statusbar = gtk_statusbar_new();
   gtk_box_pack_end(GTK_BOX(mainbox), statusbar, FALSE, TRUE, 1);

   /*show window*/
   gtk_widget_show_all(window);

   /*events*/
   gtk_signal_connect (GTK_OBJECT (window), "delete_event", GTK_SIGNAL_FUNC (destroy), NULL);
        //g_signal_connect_swapped(G_OBJECT(window), "destroy",
          //G_CALLBACK(gtk_main_quit), NULL);


   g_signal_connect(G_OBJECT(quit), "activate", G_CALLBACK(destroy),
       NULL);


   g_signal_connect(G_OBJECT(topen), "clicked", G_CALLBACK(fileopen2),
       NULL);

   g_signal_connect(G_OBJECT(contenteventbox), "button_press_event", G_CALLBACK(gobig),
       NULL);

   g_signal_connect(window, "key-release-event", G_CALLBACK(key_event), NULL);


   /*main*/
   gtk_main();

   /*return*/
   return(0);
}




Top
 Profile  
 
 Post subject: Re: Newbie Question - Dialog Box Leaves Trail
PostPosted: Thu Nov 07, 2013 4:18 am 
Offline
GTK+ Guru

Joined: Tue Feb 05, 2008 8:39 am
Posts: 130
I have compiled and linked your code (GTK 2.24.10). On my Windows XP SP3 it works fine (except run-time message
Code:
(sample.exe:4876): Gtk-CRITICAL **: gtk_box_pack: assertion `child->parent == NULL
' failed

at start time). May be the reason is your GTK-theme engine


Top
 Profile  
 
 Post subject: Re: Newbie Question - Dialog Box Leaves Trail
PostPosted: Sun Nov 17, 2013 4:04 pm 
Offline

Joined: Wed Oct 23, 2013 9:12 pm
Posts: 3
Thanks for the reply, Wolfgang.

Yes, I also have XP SP3 and using gtk+-bundle_2.24.10-20120208_win32 with MinGW. Not sure of the error message you had as I don't get any at all. Runs OK apart from trail when initiating file open dialogue.


Top
 Profile  
 
 Post subject: Re: Newbie Question - Dialog Box Leaves Trail
PostPosted: Mon Nov 18, 2013 11:42 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 764
Location: UK
Hi I tried your code on my Linux machine. It worked OK, but I did have to do a small change to make it compile. I also worked out what is the cause of your run-time warning.

Code:
#include <gtk\gtk.h>
had to be changed to
Code:
#include <gtk/gtk.h>
to make it compile. This should also work on Windows compilers.

The following section shows where your run-time error occurs
Code:
   /*create content container*/
   contenteventbox = gtk_event_box_new();
   contentbox = gtk_vbox_new(FALSE, 0);
   gtk_container_add(GTK_CONTAINER(contenteventbox), contentbox);
   GdkColor bgcolor;
   bgcolor.red = 0;
   bgcolor.green = 0;
   bgcolor.blue = 0;
   gtk_widget_modify_bg(contenteventbox, GTK_STATE_NORMAL, &bgcolor);
   gtk_container_add(GTK_CONTAINER(mainbox), contenteventbox);
/*   gtk_box_pack_start(GTK_BOX(mainbox), contentbox, FALSE, FALSE, 5); <--- Gtk-Critical error cause here */

This is caused by adding the widget "contentbox" to two different containers.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Newbie Question - Dialog Box Leaves Trail
PostPosted: Thu Nov 21, 2013 9:06 pm 
Offline

Joined: Wed Oct 23, 2013 9:12 pm
Posts: 3
Thanks Errol, will take a look at the contentbox issue you mentioned.


Top
 Profile  
 
 Post subject: Re: Newbie Question - Dialog Box Leaves Trail
PostPosted: Fri Nov 22, 2013 4:12 am 
Offline
GTK+ Guru

Joined: Tue Feb 05, 2008 8:39 am
Posts: 130
With Gtk3 on Windows XP SP3 any dialogs leave trail on GtkWindow sometimes. I haven't detected conditions enough for that. Usually it happens for a few seconds after dialog creation. With Gtk2 there is no such problem.


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 5 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