Deprecated types and GtkOrientable string representation

Hi,

I am just curious about if there is any recommended replacement to represent deprecated types as string:

  • GtkVScale
  • GtkHScale

Prior, I was able to get the matching type by its string representation:

g_type_from_name();

Is there any recommended string notation to use with a factory pattern?


Joël

I don’t understand what you’re trying to achieve. Care to give a bit more context?

Note: You can only call g_type_from_name() if the type has been registered already; GTK registers its types when you call gtk_init().

I pass some type names to a function to instantiate the matching Gtk+ widget.

I am looking for something like this: GtkScale[@orientation=“vertical”]

And here is the matching factory:

GtkWidget*
my_factory_create(gchar *widget_type_name)
{
  GtkWidget *widget;

  widget = NULL;

  if(g_str_has_prefix(widget_type_name, "GtkScale")){
    if(g_strstr_len(widget_type_name, -1, "@orientation=\"horizontal\"") != NULL){
      widget = gtk_scale_new(GTK_ORIENTATION_HORIZONTAL, NULL);
    }else{
      widget = gtk_scale_new(GTK_ORIENTATION_VERTICAL, NULL);
    }
  }

  return(widget);
}

Sine I have got a plugin browser allowing you to choose the desired control:

Prior, I was able to just use g_type_from_name().

GtkWidget*
my_factory_create(gchar *widget_type_name)
{
  GtkWidget *widget;
  GType widget_type;
  
  widget_type = g_type_from_name(widget_type_name);

  widget = g_object_new(widget_type, NULL);

  return(widget);
}

regards, Joël

If you’re providing your own compatibility layer with deprecated classes, then you will need to add a call to g_type_ensure() in your factory function, e.g.:

GtkWidget *
my_factory_create (const char *widget_name)
{
  // ensure deprecated types are registered
  g_type_ensure (GTK_TYPE_VSCALE);
  g_type_ensure (GTK_TYPE_HSCALE);

  // ... etc

  GType widget_type = g_type_from_name (widget_name);
  if (widget_type == G_TYPE_INVALID)
    return NULL;

  return g_object_new (widget_type, NULL);
}
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.