an additional argument flag




hi all,

i just talked with jay about default construction of GtkCList on #gimp.

this is the current constructor of GtkCList:

GtkWidget *
gtk_clist_new (gint columns)
{
  GtkCList *clist;

  if (columns < 1)
    return NULL;

  clist = gtk_type_new (gtk_clist_get_type ());
  gtk_clist_construct (clist, columns, NULL);
  return GTK_WIDGET (clist);
}


gtk_clist_construct() does amazingly many things like setting up
the clist's mem chunks and creation of the subwidgets.
gtk widgets can perform several very different types of tasks,
therefore it sometimes makes sense to optimize (limit) a widget
to one (or a few) certain parameters that are given at startup.

one drawback of this optimization is the losage of the default
constructor which in gtk looks like

clist = gtk_type_new (gtk_clist_get_type ());

jay moved the clist initializaton stuff into a seperate function
gtk_clist_construct() so that at least derivation can easily be done
ala

GtkWidget*
gtk_my_widget_that_inherits_from_clist_new (GtkType list_type)
{
  GtkWidget *my_widget;
  const gint computed_columns;
  
  my_widget = gtk_type_new (list_type);
  
  [...]
  
  if (list_type == gtk_clist_get_type ())
    gtk_clist_construct (GTK_CLIST (my_widget), computed_columns);

  return my_widget;
}

this is also needed by the Gtk-- folks.

but this still disqualifies this widget for pure generic generation
since e.g. a builder couldn't tell that he is supposed to actually
call gtk_clist_construct() after default-constructing the widget
before he is actually going to use it.

another example is GtkTty (from gemvt) that derives from GtkTerm.
GtkTerm needs to know the maximum character grid size in advance
and will perform memory allocation and other stuff similar to the
gtk_clist_construct() function.
i therefore moved that initialization stuff into a gtk_term_setup()
function.

therefore i'd like to propose that such special setup functions
will be exported to the widget arguments interface, and that the
arguments will be flagged in a special way (via GTK_ARG_CONSTRUCT),
so that the builder knows these arguments _must_ be supplied after
initial creation.

the infra structure for widget argument flags is already in place,
but currently only GTK_ARG_READABLE and GTK_ARG_WRITABLE are
supplied.

i might just as well use GtkCList for an example:

static void
gtk_clist_set_arg (GtkCList               *clist,
                   GtkArg                 *arg,
                   guint                   arg_id)
{
  const gint title_prefix = 8 /*GtkCList*/ + 2 /*::*/ + 5;
  
  switch (arg_id)
    {
    case ARG_COLUMNS:
      if (!clist->row_mem_chunk)
        gtk_clist_construct (clist, GTK_VALUE_INT (*arg));
      else
        arg->type = GTK_TYPE_INVALID;
      break;
    case ARG_ROW_HEIGHT:
      gtk_clist_set_row_height (clist, GTK_VALUE_INT (*arg));
      break;
    case ARG_TITLE:
      if ((arg->name[title_prefix] == ':') && (arg->name[title_prefix + 1] == ':'))
        {
          gint c;
      
          c = strtol (arg->name[title_prefix + 2]);
          gtk_clist_set_column_title (clist, c, GTK_VALUE_STRING (*title));
        }
      else
        {
          g_warning ("GtkCList: invalid title argument: \"%s\"", arg->name);
          arg->type = GTK_TYPE_INVALID;
        }
      break;
    default:
      arg->type = GTK_TYPE_INVALID;
      break;
    }
}

with the following argument announcements in gtk_clist_class_init:
gtk_object_add_arg_type ("GtkCList::columns",
                         GTK_TYPE_INT,
                         GTK_ARG_WRITABLE | GTK_ARG_CONSTRUCT,
                         ARG_COLUMNS);
gtk_object_add_arg_type ("GtkCList::row_height",
                         GTK_TYPE_INT,
                         GTK_ARG_READWRITE,
                         ARG_ROW_HEIGHT);
gtk_object_add_arg_type ("GtkCList::title",
                         GTK_TYPE_STRING,
                         GTK_ARG_READWRITE,
                         ARG_TITLE);


now this would allow code like:
clist = gtk_widget_new (gtk_clist_get_type (),
                        "GtkCList::columns", 3,
                        "GtkCList::row_height", 25,
                        "GtkCList::title::1", "First",
                        "GtkCList::title::2", "Middle",
                        "GtkCList::title::3", "Last",
                        NULL);

where as a builder would know that he has to provide GtkCList::columns
right after gtk_type_new (gtk_clist_get_type ()) because that argument
was flagged GTK_ARG_CONSTRUCT on the announcement.

now what do people think?


---
ciaoTJ



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