Convenience APIs for selections


Right now, the only convenience APIs we have for munging/demunging a
GtkSelectionData data are gtk_selection_data_{set,get}_text().  All
other data types must be handled by hand.

We have several well-known data types that get passed around, primarily
through drag and drop, such as "application/x-color" and
"text/uri-list".  However, these must be parsed by hand, leading to
duplicated code, bugs, etc.

It would be nice to have functions like this:

/* Sets "application/x-color" from the 16-bit RGB values */
void gtk_selection_data_set_color (GtkSelectionData *data,
				   const GdkColor   *color);

/* Parses from "application/x-color", handling old KDE's broken
 * colors.  Returns an error if the selection type doesn't have
 * the right atom or if parsing fails.
gboolean gtk_selection_data_get_color (GtkSelectionData *data,
				       GdkColor         *color,
				       GError          **error);

/* Sets "text/uri-list" from a list of char *; uses the format
 * defined in RFC 2483 section 5.
void gtk_selection_data_set_uri_list (GtkSelectionData *data,
				      GList            *uri_list);

/* Parses "text/uri-list".  Returns a list of char *.  Returns an
 * error if the selection type doesn't have the right atom
 * or if parsing fails.
gboolean gtk_selection_data_get_uri_list (GtkSelectionData *data,
					  GList           **uri_list,
					  GError          **error);

We may also want to have a way to check whether a GtkSelectionData is of
a particular well-known target without calling gdk_atom_intern() by
hand.  The idea would be to write idiomatic callbacks like this:

static void
my_drag_data_received_callback (GtkWidget          *widget,
				GdkDragContext     *context,
				gint                x,
				gint                y,
				GtkSelectionData   *selection_data,
				guint               info,
				guint               time_,
				gpointer            user_data)
  /* equivalent to:
   * selection_data->target == gdk_atom_intern ("application/x-color",
   *                                            FALSE)
  if (gtk_selection_data_is_color (selection_data))
      GdkColor color;
      GError *error;

      error = NULL;
      if (!gtk_selection_data_get_color (selection_data, &color, &error))
          g_print ("broken color: %s", error->message);
          g_error_free (error);
        do_something_with_color (&color);
  else if (gtk_selection_data_is_uri_list (selection_data)
      GList *uri_list;
      GError *error;

      if (!gtk_selection_data_get_uri_list (selection_data, &uri_list, &error))
          g_print ("broken uri list: %s", error->message);
          g_error_free (error);
        do_something_with_uri_list (uri_list);
    g_print ("dude, you sent me an unknown target");

For GtkCliboard, we have these related functions:

However, we may just want to use GtkClipboard with GtkSelectionData and
the convenience functions proposed above, rather than have duplicated
convenience functions just for GtkClipboard.

What do people think of adding these functions?  [Would they be for GTK+
2.6.0, or for 2.4.x?]


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