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 Oct 21, 2014 9:36 pm

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Hide window callback func.
PostPosted: Mon Jul 01, 2013 12:06 pm 
Offline

Joined: Sat Jun 29, 2013 8:04 am
Posts: 4
Hello to everyone,

my name is hicham and i am new too gtk/gui programming. I am experiencing a lot problems learning gtk, cuz i have the feeling that there isnt a big community behind gtk. I like c programming/gtk and dont want to switch to qt because of gtk lags community and tuts.

I have two questions:

1. Is there a non-plus-ultra book/guide/ebook/videos or what ever for learning gtk for advanced users (not only showing how to use widgets...) cuz i dont like to spam the forum with my noob questions...=} ?

2. Hide window callback func.

I programmed an app, which has a main screen with a menubar. This programm contains 5 sub-programms, which open a new window.

The plan:
I want to hide the main window when the users clicks a menu item, so that the user can't open the sub-programm twice.

Code:
//Function for hiding the widget

int hide (GtkWidget *widget)
{
    gtk_widget_hide (widget);
}
//----------
//Callback..
g_signal_connect(G_OBJECT(konjugator), "activate",G_CALLBACK(konjugatorfunc),NULL); // starts the sub-programm
g_signal_connect_after(G_OBJECT(konjugator), "activate",G_CALLBACK(hide(window),NULL); // hides the main-programm


it compiles and when I run it, the programm starts for mseconds and then quits immediately. Why that? any iedeas how to fix this?
Or how to prevent the user to open the konjugator func twice without hiding the man-window?


Top
 Profile  
 
 Post subject: Re: Hide window callback func.
PostPosted: Mon Jul 01, 2013 5:44 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 764
Location: UK
Hello and welcome,

There is a fair sized community using GTK+, you just need to look out for us. We normally do not shout out LOOK AT US. If you can also use normal English and not slang that would help everybody. For example use "because" and not "cuz".

There are many pointers/links and actual tutorials here on this forum at http://www.gtkforums.com/viewtopic.php?f=3&t=988

With regards to your problem I can not tell what could be going wrong with your code as all that is here is the connecting of two signals and a call back function. There is probably much more going on that is affecting what is going on.

Would it not be better to disable the menu items rather than hide the windows. The user can always un-hide the window and can then run the sub program twice.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Hide window callback func.
PostPosted: Wed Jul 03, 2013 3:24 pm 
Offline

Joined: Sat Jun 29, 2013 8:04 am
Posts: 4
Hello again,

thank you four your links and advices. They helped me a lot and i am a step further now.
I finally decided to take your tip and use gtk_widget_set_sensitive to avoid the program to start twice.
It works!!! And now I want to reactivate it, if the user clicks on "x" or close the window.

(Again mainprogram starts subprogramm by activating menuitem. Menuitem is deactivated in mainprogramm. After closeing subprogramm, menuitem shall be reactivated,to start subprogramm again)

But I am facing this problem:

the function:

Code:
static void sensitivity (GtkWidget *widget, gpointer data)
{
  gtk_widget_set_sensitive (widget,(gboolean *) data);
}


this g_signal for de-/activating it:
Code:
g_signal_connect(G_OBJECT(konjugator), "activate",G_CALLBACK(sensitivity),(gpointer) FALSE);
g_signal_connect_swapped(G_OBJECT(window), "destroy",G_CALLBACK(sensitivity), (gpointer) TRUE);


ok. it compiles but with this warning message:

Quote:
main.c: In function ‘sensitivity’:
main.c:20:3: warning: passing argument 2 of ‘gtk_widget_set_sensitive’ makes integer from pointer without a cast [enabled by default]
In file included from /usr/include/gtk-3.0/gtk/gtkapplication.h:27:0,
from /usr/include/gtk-3.0/gtk/gtkwindow.h:33,
from /usr/include/gtk-3.0/gtk/gtkdialog.h:33,
from /usr/include/gtk-3.0/gtk/gtkaboutdialog.h:30,
from /usr/include/gtk-3.0/gtk/gtk.h:31,
from main.c:2:
/usr/include/gtk-3.0/gtk/gtkwidget.h:584:23: note: expected ‘gboolean’ but argument is of type ‘gboolean *’


When i start the programm it starts the subprogramm and deactivates the menu item good so far. But when i close the subprogramm it doesn't reactivate.
The programm crashes and in the terminal it says: Speicherzugriffsfehler (segmentation fault i think in english).

Does someone see a mistake in my coding?

thank you


Top
 Profile  
 
 Post subject: Re: Hide window callback func.
PostPosted: Wed Jul 03, 2013 3:36 pm 
Offline

Joined: Sat Jun 29, 2013 8:04 am
Posts: 4
Ok i now figured out why it crashes my mistake =).
I just had to delete the "swapped" in g_signal.
But still how can I tell the subprogramm te reactivate a widget in the mainprogramm?? When i close the subprogramm the menuitem is still not reactivated so how can i tell him which widget to activate?

Code:
    static void sensitivity (GtkWidget *widget, gpointer data)
{
  gtk_widget_set_sensitive (widget,(gboolean) data);
}


Code:
g_signal_connect(G_OBJECT(window), "destroy",G_CALLBACK(sensitivity), (gpointer) TRUE);


Top
 Profile  
 
 Post subject: Re: Hide window callback func.
PostPosted: Thu Jul 04, 2013 6:41 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 764
Location: UK
You have misused the "destroy" signal.

The signal when it calls the call-back function passes the widget and some user data in the form of a pointer. The way you are using it will call the function sensitivity() with the parameters for the widget being the window and TRUE cast as a gpointer. Although this is not an error, it is also not what you want as it does not refer to the menu item.

Also when converting an integer to a pointer and back it would be better to use the macros
Code:
GPOINTER_TO_SIZE(p)
GSIZE_TO_POINTER(s)


To get around the problem you either have to hard code the widget that you want to enable/disable or pass a structure containing the widget and information on what needs to be done.

This section of code (untested) might be a way to do this kind of thing.
Code:
typedef struct {
    GtkWidget *menuitem;
    gboolean sen;
} menu_sen;

menu_sen[] = {
    {menu1, FALSE},
    {menu1, TRUE}
};

static void sensitivity (GtkWidget *widget, gpointer data)
{
  menu_sen *m = (gpointer) data;
  gtk_widget_set_sensitive (m->widget, m->sen);
}

g_signal_connect(G_OBJECT(window), "destroy",G_CALLBACK(sensitivity),  menu_sen[1]);

_________________
E.


Top
 Profile  
 
 Post subject: Re: Hide window callback func.
PostPosted: Thu Jul 04, 2013 1:48 pm 
Offline

Joined: Sat Jun 29, 2013 8:04 am
Posts: 4
Hello,

thank you for your tips and patient again. I am still facing some problems with the gpointer. I will post the summuarized and shortent code:

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

int main (int argc, char *argv[])
{
   GtkWidget *window, *menubar, *menuitem, *vbox, *sep, *modemenu, *mode, *konjugator;
   
        typedef struct{
         GtkWidget *widget;
         gboolean sen;
   } menu_sen;

   menu_sen menus[] ={ {konjugator, FALSE},{konjugator, TRUE} };

   void sensitivity (GtkWidget *widget, gpointer data){

      menu_sen *menus = (gpointer) data;
        gtk_widget_set_sensitive (menus->widget, menus->sen);
   }


   void konjugatorfunc(){
   
       GtkWidget *window;             
       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
       
        g_signal_connect(G_OBJECT(window), "destroy",G_CALLBACK(sensitivity), menus[0]);  //ERROR: expected ‘gpointer’ but argument is of type ‘menu_sen’
       gtk_widget_show_all (window);       
      }

   gtk_init(&argc,&argv);
   

   window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
   vbox=gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
   menubar = gtk_menu_bar_new();
   modemenu=gtk_menu_new();

   
   mode=gtk_menu_item_new_with_mnemonic("  _Mode  ");
   konjugator=gtk_menu_item_new_with_mnemonic("_Konjugator");
   
   //configure widgets
   gtk_window_set_default_size(GTK_WINDOW(window),1024,860);
   gtk_window_set_title(GTK_WINDOW(window)," test");
   gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
   
   //arrange widgets
   gtk_menu_item_set_submenu(GTK_MENU_ITEM(mode),modemenu);   

   gtk_menu_shell_append(GTK_MENU_SHELL(menubar),mode);
   
   gtk_menu_shell_append(GTK_MENU_SHELL(modemenu),konjugator);
   
   // widgets packing
   gtk_box_pack_start(GTK_BOX(vbox),menubar,0,0,0);
   
   gtk_container_add(GTK_CONTAINER(window),vbox);
   gtk_widget_show_all(window);
   
   // events
   
   g_signal_connect_swapped(G_OBJECT(window), "destroy",G_CALLBACK(gtk_main_quit),NULL);
   g_signal_connect(G_OBJECT(konjugator), "activate",G_CALLBACK(konjugatorfunc),NULL);
   g_signal_connect(G_OBJECT(window), "destroy",G_CALLBACK(sensitivity), menus[1]); //ERROR: expected ‘gpointer’ but argument is of type ‘menu_sen’   
   
   gtk_main();

   
   return 0;
}


Quote:
main2.c: In function ‘konjugatorfunc’:
main2.c:30:3: error: incompatible type for argument 4 of ‘g_signal_connect_data’
In file included from /usr/include/glib-2.0/gobject/gobject.h:30:0,
from /usr/include/glib-2.0/gobject/gbinding.h:31,
from /usr/include/glib-2.0/glib-object.h:25,
from /usr/include/glib-2.0/gio/gioenums.h:30,
from /usr/include/glib-2.0/gio/giotypes.h:30,
from /usr/include/glib-2.0/gio/gio.h:28,
from /usr/include/gtk-3.0/gdk/gdkapplaunchcontext.h:28,
from /usr/include/gtk-3.0/gdk/gdk.h:32,
from /usr/include/gtk-3.0/gtk/gtk.h:30,
from main2.c:2:
/usr/include/glib-2.0/gobject/gsignal.h:368:9: note: expected ‘gpointer’ but argument is of type ‘menu_sen’
main2.c: In function ‘main’:
main2.c:70:2: error: incompatible type for argument 4 of ‘g_signal_connect_data’
In file included from /usr/include/glib-2.0/gobject/gobject.h:30:0,
from /usr/include/glib-2.0/gobject/gbinding.h:31,
from /usr/include/glib-2.0/glib-object.h:25,
from /usr/include/glib-2.0/gio/gioenums.h:30,
from /usr/include/glib-2.0/gio/giotypes.h:30,
from /usr/include/glib-2.0/gio/gio.h:28,
from /usr/include/gtk-3.0/gdk/gdkapplaunchcontext.h:28,
from /usr/include/gtk-3.0/gdk/gdk.h:32,
from /usr/include/gtk-3.0/gtk/gtk.h:30,
from main2.c:2:
/usr/include/glib-2.0/gobject/gsignal.h:368:9: note: expected ‘gpointer’ but argument is of type ‘menu_sen’


Any ideas?


Top
 Profile  
 
 Post subject: Re: Hide window callback func.
PostPosted: Sat Jul 06, 2013 7:39 am 
Offline
GTK+ Guru

Joined: Tue Feb 05, 2008 8:39 am
Posts: 130
You may try this:

Code:
g_signal_connect(G_OBJECT(window), "destroy",G_CALLBACK(sensitivity), &menus[1])


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

All times are UTC


Who is online

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