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



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?

Yes.  It depends on your application.  Some people will subclass a
GObject and add the window object plus all of the other widgets as
aggregates.  If you are feeling more daring you can subclass your main
window where your app class would inherit from GtkWindow.  However if
you do not want to get into the intricacies of GObject inheritance
simply creating a struct would be fine:

typedef struct _FooApp
{
  gchar *app_name;

  GtkWidget *main_window;
  GtkWidget *progress_bar;
  GtkWidget *button_start;
 
} FooApp;

FooApp *
create_app (void)
{
  FooApp *app;

  app = g_new0 (FooApp, 1);

  app_name = g_strdup ("My Foo App");

  app->main_window = ...
  app->progress_bar = ...

  gtk_container_add (GTK_CONTAINER (app->main_window),
app->progress_bar);

  .
  .
  .

  g_signal_connect(app->button_start, "clicked",
G_CALLBACK(start_timer_cb), app);

  gtk_widget_show_all (app->main_window);

  return app;
}

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() ?

callbacks.c:

void start_timer_cb(GtkObject *window, void *data)
{
  GtkWidget *progressbar = data;
  gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progressbar), 
  gtk_progress_bar_get_fraction(GTK_PROGRESS_BAR(progressbar))+0.1);
}


BTW GtkObject is usually not used directly anymore.  Usually objects are
passed to callbacks as GObject, GtkWidget or their actual class (in your
case GtkButton) as they are the most useful casts. 

thanks, good to know. 


andreas

-- 
John (J5) Palmieri <johnp redhat com>




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