Commit 4ee291c8 authored by Matthias Clasen's avatar Matthias Clasen

GtkDialog: Use the new helpers

Instead of issuing g_warning, fill the provided GError.
This lets us test this error handling, and is the right
thing to do. Use the new GtkBuilder helpers and
g_markup_collect_attributes to do so.
parent f11fedb7
...@@ -42,6 +42,7 @@ ...@@ -42,6 +42,7 @@
#include "gtkbindings.h" #include "gtkbindings.h"
#include "gtkprivate.h" #include "gtkprivate.h"
#include "gtkbuildable.h" #include "gtkbuildable.h"
#include "gtkbuilderprivate.h"
#include "gtksettings.h" #include "gtksettings.h"
#include "gtktypebuiltins.h" #include "gtktypebuiltins.h"
#include "deprecated/gtkstock.h" #include "deprecated/gtkstock.h"
...@@ -1672,119 +1673,144 @@ typedef struct { ...@@ -1672,119 +1673,144 @@ typedef struct {
GtkDialog *dialog; GtkDialog *dialog;
GtkBuilder *builder; GtkBuilder *builder;
GSList *items; GSList *items;
gchar *response; gint response_id;
gboolean is_default; gboolean is_default;
} ActionWidgetsSubParserData; gboolean is_text;
GString *string;
gboolean in_action_widgets;
} SubParserData;
static gint static void
parse_response_id (const gchar *response_attr) parser_start_element (GMarkupParseContext *context,
const gchar *element_name,
const gchar **names,
const gchar **values,
gpointer user_data,
GError **error)
{ {
int response_id; SubParserData *data = (SubParserData*)user_data;
GEnumClass *enum_class = NULL;
GEnumValue *enum_value; if (strcmp (element_name, "action-widget") == 0)
{
const gchar *response;
gboolean is_default = FALSE;
GValue gvalue = G_VALUE_INIT;
response_id = g_ascii_strtoll (response_attr, NULL, 10); if (!_gtk_builder_check_parent (data->builder, context, "action-widgets", error))
if (response_id != 0) return;
goto out;
if (!g_markup_collect_attributes (element_name, names, values, error,
G_MARKUP_COLLECT_STRING, "response", &response,
G_MARKUP_COLLECT_BOOLEAN|G_MARKUP_COLLECT_OPTIONAL, "default", &is_default,
G_MARKUP_COLLECT_INVALID))
{
_gtk_builder_prefix_error (data->builder, context, error);
return;
}
enum_class = g_type_class_ref (GTK_TYPE_RESPONSE_TYPE); if (!gtk_builder_value_from_string_type (data->builder, GTK_TYPE_RESPONSE_TYPE, response, &gvalue, error))
enum_value = g_enum_get_value_by_nick (enum_class, response_attr); {
if (enum_value == NULL) _gtk_builder_prefix_error (data->builder, context, error);
goto out; return;
}
response_id = enum_value->value; data->response_id = g_value_get_enum (&gvalue);
data->is_default = is_default;
data->is_text = TRUE;
g_string_set_size (data->string, 0);
}
else if (strcmp (element_name, "action-widgets") == 0)
{
if (!_gtk_builder_check_parent (data->builder, context, "object", error))
return;
out: if (!g_markup_collect_attributes (element_name, names, values, error,
if (enum_class) G_MARKUP_COLLECT_INVALID, NULL, NULL,
g_type_class_unref (enum_class); G_MARKUP_COLLECT_INVALID))
_gtk_builder_prefix_error (data->builder, context, error);
return response_id; data->in_action_widgets = TRUE;
}
else
{
_gtk_builder_error_unhandled_tag (data->builder, context,
"GtkDialog", element_name,
error);
}
} }
static void static void
attributes_start_element (GMarkupParseContext *context, parser_text_element (GMarkupParseContext *context,
const gchar *element_name, const gchar *text,
const gchar **names, gsize text_len,
const gchar **values, gpointer user_data,
gpointer user_data, GError **error)
GError **error)
{ {
ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data; SubParserData *data = (SubParserData*)user_data;
guint i;
if (strcmp (element_name, "action-widget") == 0) if (data->is_text)
{ g_string_append_len (data->string, text, text_len);
for (i = 0; names[i]; i++)
{
if (strcmp (names[i], "response") == 0)
parser_data->response = g_strdup (values[i]);
else if (strcmp (names[i], "default") == 0)
parser_data->is_default = TRUE;
}
}
else if (strcmp (element_name, "action-widgets") == 0)
return;
else
g_warning ("Unsupported tag for GtkDialog: %s\n", element_name);
} }
static void static void
attributes_text_element (GMarkupParseContext *context, parser_end_element (GMarkupParseContext *context,
const gchar *text, const gchar *element_name,
gsize text_len, gpointer user_data,
gpointer user_data, GError **error)
GError **error)
{ {
ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data; SubParserData *data = (SubParserData*)user_data;
ActionWidgetInfo *item;
if (data->is_text)
if (!parser_data->response) {
return; ActionWidgetInfo *item;
item = g_new (ActionWidgetInfo, 1); item = g_new (ActionWidgetInfo, 1);
item->widget_name = g_strndup (text, text_len); item->widget_name = g_strdup (data->string->str);
item->response_id = parse_response_id (parser_data->response); item->response_id = data->response_id;
g_free (parser_data->response); item->is_default = data->is_default;
item->is_default = parser_data->is_default;
parser_data->items = g_slist_prepend (parser_data->items, item); data->items = g_slist_prepend (data->items, item);
parser_data->response = NULL; data->is_default = FALSE;
parser_data->is_default = FALSE; data->is_text = FALSE;
}
} }
static const GMarkupParser attributes_parser = static const GMarkupParser sub_parser =
{ {
attributes_start_element, parser_start_element,
NULL, parser_end_element,
attributes_text_element, parser_text_element,
}; };
static gboolean static gboolean
gtk_dialog_buildable_custom_tag_start (GtkBuildable *buildable, gtk_dialog_buildable_custom_tag_start (GtkBuildable *buildable,
GtkBuilder *builder, GtkBuilder *builder,
GObject *child, GObject *child,
const gchar *tagname, const gchar *tagname,
GMarkupParser *parser, GMarkupParser *parser,
gpointer *data) gpointer *parser_data)
{ {
ActionWidgetsSubParserData *parser_data; SubParserData *data;
if (child) if (child)
return FALSE; return FALSE;
if (strcmp (tagname, "action-widgets") == 0) if (strcmp (tagname, "action-widgets") == 0)
{ {
parser_data = g_slice_new0 (ActionWidgetsSubParserData); data = g_slice_new0 (SubParserData);
parser_data->dialog = GTK_DIALOG (buildable); data->dialog = GTK_DIALOG (buildable);
parser_data->items = NULL; data->builder = builder;
data->string = g_string_new ("");
*parser = attributes_parser; data->items = NULL;
*data = parser_data; data->in_action_widgets = FALSE;
*parser = sub_parser;
*parser_data = data;
return TRUE; return TRUE;
} }
return parent_buildable_iface->custom_tag_start (buildable, builder, child, return parent_buildable_iface->custom_tag_start (buildable, builder, child,
tagname, parser, data); tagname, parser, parser_data);
} }
static void static void
...@@ -1797,7 +1823,7 @@ gtk_dialog_buildable_custom_finished (GtkBuildable *buildable, ...@@ -1797,7 +1823,7 @@ gtk_dialog_buildable_custom_finished (GtkBuildable *buildable,
GtkDialog *dialog = GTK_DIALOG (buildable); GtkDialog *dialog = GTK_DIALOG (buildable);
GtkDialogPrivate *priv = dialog->priv; GtkDialogPrivate *priv = dialog->priv;
GSList *l; GSList *l;
ActionWidgetsSubParserData *parser_data; SubParserData *data;
GObject *object; GObject *object;
ResponseData *ad; ResponseData *ad;
guint signal_id; guint signal_id;
...@@ -1809,10 +1835,10 @@ gtk_dialog_buildable_custom_finished (GtkBuildable *buildable, ...@@ -1809,10 +1835,10 @@ gtk_dialog_buildable_custom_finished (GtkBuildable *buildable,
return; return;
} }
parser_data = (ActionWidgetsSubParserData*)user_data; data = (SubParserData*)user_data;
parser_data->items = g_slist_reverse (parser_data->items); data->items = g_slist_reverse (data->items);
for (l = parser_data->items; l; l = l->next) for (l = data->items; l; l = l->next)
{ {
ActionWidgetInfo *item = l->data; ActionWidgetInfo *item = l->data;
gboolean is_action; gboolean is_action;
...@@ -1867,8 +1893,10 @@ gtk_dialog_buildable_custom_finished (GtkBuildable *buildable, ...@@ -1867,8 +1893,10 @@ gtk_dialog_buildable_custom_finished (GtkBuildable *buildable,
g_free (item->widget_name); g_free (item->widget_name);
g_free (item); g_free (item);
} }
g_slist_free (parser_data->items);
g_slice_free (ActionWidgetsSubParserData, parser_data); g_slist_free (data->items);
g_string_free (data->string, TRUE);
g_slice_free (SubParserData, data);
update_suggested_action (dialog); update_suggested_action (dialog);
} }
...@@ -1889,7 +1917,7 @@ gtk_dialog_buildable_add_child (GtkBuildable *buildable, ...@@ -1889,7 +1917,7 @@ gtk_dialog_buildable_add_child (GtkBuildable *buildable,
priv->headerbar = GTK_WIDGET (child); priv->headerbar = GTK_WIDGET (child);
gtk_window_set_titlebar (GTK_WINDOW (buildable), priv->headerbar); gtk_window_set_titlebar (GTK_WINDOW (buildable), priv->headerbar);
} }
else if (g_strcmp0 (type, "action") == 0) else if (g_str_equal (type, "action"))
gtk_dialog_add_action_widget (GTK_DIALOG (buildable), GTK_WIDGET (child), GTK_RESPONSE_NONE); gtk_dialog_add_action_widget (GTK_DIALOG (buildable), GTK_WIDGET (child), GTK_RESPONSE_NONE);
else else
GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type); GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment