Re: what's the best way to handle variables and objects?



My reply's in the body (I ain't no top-poster!):

On Wed, 2006-03-15 at 22:57 +0100, Andreas Kotowicz wrote:
On Wed, 2006-03-15 at 13:44 -0500, John (J5) Palmieri wrote:
You can create a generic struct but more often than not there is an
application object or struct that the application developer creates
which holds all the public variables he or she would care about which is
passed as userdata to things like g_signal_connect.  

sorry, but I don't exactly understand what you mean. Could you maybe
give me an example of how the application object and the interaction
would look like? would this application object also hold all the labels
and entry fields which might change?

but maybe I do understand you. what I use here is following:

main.c:

#include <gtk/gtk.h>
#include "interface.h"
int main (int argc, char *argv[])
{

  GtkWidget *window1;

  gtk_init(&argc, &argv);

  window1 = create_window1 ();
  gtk_widget_show_all(window1);

  gtk_main();
  return 0;
}


interface.h:
#include <gtk/gtk.h>

GtkWidget* create_window1 (void);

interface.c:

#include "callbacks.h"
#include "interface.h"

GtkWidget*
create_window1 (void)
{
  GtkWidget *window;
  GtkWidget *vbox;
  GtkWidget *progressbar;
  GtkWidget *button_start;
  GtkWidget *spin_sec;
  GtkWidget *spin_min;

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), "Window");
  g_signal_connect(G_OBJECT(window), "destroy", 
  G_CALLBACK(destroy_window_cb), NULL);
  
  /* vbox for whole window */
  vbox = gtk_vbox_new(FALSE, 6);
  gtk_container_add(GTK_CONTAINER(window), vbox);

  /* put progressbar at the top of the window */
  progressbar = gtk_progress_bar_new();
  gtk_box_pack_start(GTK_BOX(vbox), progressbar, TRUE, TRUE, 12);

  /* entry field for number of seconds */
  spin_sec = gtk_spin_button_new_with_range(0, 59, 1);  
  gtk_box_pack_start(GTK_BOX(vbox), spin_sec, FALSE, FALSE, 0);  

  
  button_start = gtk_button_new_with_mnemonic("_Start");
  gtk_box_pack_start(GTK_BOX(vbox), button_start, FALSE, FALSE, 0);

  g_signal_connect(GTK_BUTTON(button_start), "clicked", 
  G_CALLBACK(start_timer_cb), progressbar);
  
  return window;
}

is this what you mean by application object? here comes my problem now:
g_signal_connect on button_start only passes on the progressbar object
to start_timer_cb(). but I also want to have the user input from
spin_sec in this function. so how can I get a grip on both of these (or
even more) variables in start_timer_cb() ?

Define a struct that has as it's member fields all the values you want
accessible to the callback, including the progressbar, spin_sec, etc:

struct MyUserData
{
    GtkWidget *progress_bar;
    GtkWidget *spin_sec;
};

Allocate one of these on the heap (note: I'm using C++, so new's what I
use for this):

MyUserData *myData = new MyUserData;

myData->progress_bar = progressbar;
myData->spin_sec = spin_sec;

Pass the allocated storage as the last parm to g_signal_connect.

g_signal_connect(GTK_BUTTON(button_start), "clicked", G_CALLBACK
(start_timer_cb), myData);

In the callback, cast the pointer to the user-supplied parm to the type
of your struct and access it's members as needed:

void start_timer_cb(GtkObject *window, void *data)
{
    MyUserData *myData = (MyUserData*)data;

    // the progressbar is myData->progress_bar, etc.
    // ...
}

Remember to return the storage to the heap when you're done using it.


  // Wally

-- 
Your eyes are weary from staring at the CRT.  You feel sleepy.  Notice
how restful it is to watch the cursor blink.  Close your eyes.  The   
opinions stated above are yours.  You cannot imagine why you ever felt
otherwise.




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