GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

understanding of g_signal_connect()

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
xaos5
Familiar Face


Joined: 05 Nov 2006
Posts: 19

PostPosted: Sun Nov 05, 2006 9:08 am    Post subject: understanding of g_signal_connect() Reply with quote

I'm new to gtk and currently learning. I've come across a problem I can't seem to understand. my program has two labels, two text entries, and one button. When the button is pushed I want it to grab the values in the two text boxes and store them in a structure. I have one function for creating my gui and another for doing what I will with the two text entries.

Code: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

/* create our main window */
void create_main_window(void);

/* callback to display a window with label displaying results calculated */
void calc_and_show_window(GtkWidget *widget, gpointer data);

struct car_data {
    gfloat horsepower;
    gfloat time;
    gfloat weight;
};

...
...
...

void create_main_window(void)
{
...
...
...
/* calculate button */
button = gtk_button_new_with_mnemonic("_Calculate");
g_signal_connect(G_OBJECT(button), "clicked",
     G_CALLBACK(calc_and_show_window), (gpointer)entry_weight); /* two arguments here maybe? */
g_signal_connect(G_OBJECT(button), "clicked",
     G_CALLBACK(calc_and_show_window), (gpointer)entry_horsepower);
...
...
...
}

/* this is the function I haven't completed yet because I don't understand how to do it */
void calc_and_show_window(GtkWidget *widget, gpointer callback_data)
{
    const gchar * horsepower =
        gtk_entry_get_text(GTK_ENTRY(callback_data));   
    g_print("%s INCOMPLETE, FINISH ME!\n", horsepower);
}


I can post all my code if this isn't clear enough. Thanks in advance.
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 376
Location: State College, Pennsylvania

PostPosted: Sun Nov 05, 2006 5:17 pm    Post subject: Reply with quote

You can't send multiple objects with g_signal_connect(). Instead, you want to do the following:

Code: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
typedef struct
{
  GtkWidget *entry_weight, *entry_horsepower;
} Entries;

void create_main_window ();
void calc_and_show_window (GtkWidget*, Entries*);

struct car_data {
   gfloat horsepower;
   gfloat time;
   gfloat weight;
};


void create_main_window(void)
{
  Entries *w = g_slice_new (Entries);

  button = gtk_button_new_with_mnemonic("_Calculate");
  g_signal_connect(G_OBJECT(button), "clicked",
    G_CALLBACK(calc_and_show_window), (gpointer) w);
}

void calc_and_show_window(GtkWidget *widget, Entries *w)
{
  /* now you can use both entries here! */
}


You can cast the object type in your callback function automatically ...
Back to top
xaos5
Familiar Face


Joined: 05 Nov 2006
Posts: 19

PostPosted: Sun Nov 05, 2006 11:06 pm    Post subject: Reply with quote

Thank you! Could you do me a favor and explain or direct me to site on g_slice_new()? Google has failed me. Now to go look up typedef in more detail (first language is c++ and been coding for about 6 months now).
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 376
Location: State College, Pennsylvania

PostPosted: Mon Nov 06, 2006 12:18 am    Post subject: Reply with quote

Ok, well, if you go to the GLib API documentation, there's a section called Memory Slices that you should go over. Also, I have a book on GTK+ development coming in March 2007 that is available for preorder (http://book.andrewkrause.net), which covers memory allocation in detail. Here's an excerpt about g_slice_new():

Quote:
If you need to allocate a single block of memory with a slice allocation, instead of using the method presented in Listing 6-1, you can call g_slice_new(). This function is defined as shown below, which casts the value returned by g_slice_alloc() as the desired type.

#define g_slice_new(type) ((type*) g_slice_alloc (sizeof (type))

g_slice_alloc() was used to allocate one hundred strings of length SLICE_SIZE. Slice allocation is very simple because all you need to do is supply the size of memory that the slice should be. Similar to malloc(), this function returns a gpointer to the memory instead of an object that is cast.

Internally, GLib decides whether to use slab allocation or delegate the memory allocation to g_malloc(). Memory allocation is performed by g_malloc() when the desired memory slice is very large. GLib also provides g_slice_alloc0(), which will initialize the returned memory chunk to zero.
Back to top
xaos5
Familiar Face


Joined: 05 Nov 2006
Posts: 19

PostPosted: Mon Nov 06, 2006 2:19 am    Post subject: Reply with quote

cool, how much does your book go in terms of introducing GTK+? I would like a book that shows the basics and at the same time explains itself, I usually end up finding one or the other.
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 376
Location: State College, Pennsylvania

PostPosted: Mon Nov 06, 2006 3:16 am    Post subject: Reply with quote

It starts with the basics and then gets pretty heavy into things. You'll get a thorough coverage of GTK+, GLib, GObject and some Pango, GdkPixbuf and GDK. You'll learn how to make your own objects and there are examples for using almost every existing widget in GTK+.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP