Re: Multi-dimensional arrays of widgets...



On Thu, Oct 11, 2001 at 12:49:10PM -0400 Gordon R.Ellsworth Jr. wrote:

OK, can someone please tell me how I can use a
multi-dimensional array of widgets? How do I create the array
and how do I pass the array to functions? How do I handle
the array within other functions?

Basicly I just want to make a table of label widgets
10x10 and update them with a function that will take the
array and update each element without writing a seperate
function for each label (100 labels).

There's got to be a better way than using global
variables.

Sure. Use an abstract data object, or even better: use an
abstract data type, that means:

Write a module with functions (abstract data type):
header file:

typedef {
  int width, height;
  GtkLabel** label;
} label_array_t;
  
extern label_array_t* 
  label_array_new(int width, int heigth);
extern void
  label_array_destroy(label_array_t* labels);
extern void
  label_array_insert_gui(label_array_t* labels, GtkContainer* where);
extern void
  label_array_modify(label_array_t* labels);
[ more functions ]

The disadvantage of this version is: you need a (global?) pointer to
the label array.

If you do it with an abstract data object, then you dont need such a
pointer, because you declare the label array in your .c file:

static label_array_t labels;

then you dont need to pass the label array pointer to the functions
any longer, because they are able to access the static var.
You also dont need the *_new and *_destroy functions (but maybe an
*_initialize function).

the disadvantage of this method: there can only be exactly one
array of such labels in you application.

You could also separate view (gui) and functionality: dont use GtkLabel**
in the typedef, instead use char**. Then the label_array_insert_gui()
would not be really be implemented in the module, you would only call a 
external function (like: my_app_insert_labels(pass_pointer)).
Same with label_array_modify(): call my_app_visual_update_labels().
Even better: include function pointers to this functions in label_array_t;

The advantage of this method: you write this module once and can easily
use it in different applications (gtk apps, other GUI apps, even in
non-gui apps).

Maybe this is too much theory for such a simple thing, but they are
the basic concepts of software enigneering.

Greetings,
  Markus.




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