summaryrefslogtreecommitdiff
path: root/plugins/gtk+/glade-gtk-message-dialog.c
blob: 131be7b27ef9ac0c34e3ab0b1c735d6993f7dec8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * glade-gtk-message-dialog.c - GladeWidgetAdaptor for GtkMessageDialog
 *
 * Copyright (C) 2013 Tristan Van Berkom
 *
 * Authors:
 *      Tristan Van Berkom <tristan.van.berkom@gmail.com>
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <config.h>
#include <glib/gi18n-lib.h>
#include <gladeui/glade.h>

static gboolean
glade_gtk_message_dialog_reset_image (GtkMessageDialog * dialog)
{
  GtkWidget *image;
  gint message_type;

  g_object_get (dialog, "message-type", &message_type, NULL);
  if (message_type != GTK_MESSAGE_OTHER)
    return FALSE;

G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  image = gtk_message_dialog_get_image (dialog);
G_GNUC_END_IGNORE_DEPRECATIONS
  if (glade_widget_get_from_gobject (image))
    {
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
      gtk_message_dialog_set_image (dialog,
                                    gtk_image_new_from_stock (NULL,
                                                              GTK_ICON_SIZE_DIALOG));
G_GNUC_END_IGNORE_DEPRECATIONS
      gtk_widget_show (image);

      return TRUE;
    }
  else
    return FALSE;
}

enum
{
  MD_IMAGE_ACTION_INVALID,
  MD_IMAGE_ACTION_RESET,
  MD_IMAGE_ACTION_SET
};

static gint
glade_gtk_message_dialog_image_determine_action (GtkMessageDialog * dialog,
                                                 const GValue * value,
                                                 GtkWidget ** image,
                                                 GladeWidget ** gimage)
{
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  GtkWidget *dialog_image = gtk_message_dialog_get_image (dialog);
G_GNUC_END_IGNORE_DEPRECATIONS

  *image = g_value_get_object (value);

  if (*image == NULL)
    if (dialog_image && glade_widget_get_from_gobject (dialog_image))
      return MD_IMAGE_ACTION_RESET;
    else
      return MD_IMAGE_ACTION_INVALID;
  else
    {
      *image = GTK_WIDGET (*image);
      if (dialog_image == *image)
        return MD_IMAGE_ACTION_INVALID;
      if (gtk_widget_get_parent (*image))
        return MD_IMAGE_ACTION_INVALID;

      *gimage = glade_widget_get_from_gobject (*image);

      if (!*gimage)
        {
          g_warning ("Setting property to an object outside the project");
          return MD_IMAGE_ACTION_INVALID;
        }

      if (glade_widget_get_parent (*gimage) ||
          GLADE_WIDGET_ADAPTOR_IS_TOPLEVEL (glade_widget_get_adaptor (*gimage)))
        return MD_IMAGE_ACTION_INVALID;

      return MD_IMAGE_ACTION_SET;
    }
}

void
glade_gtk_message_dialog_set_property (GladeWidgetAdaptor * adaptor,
                                       GObject * object,
                                       const gchar * id, const GValue * value)
{
  GtkMessageDialog *dialog = GTK_MESSAGE_DIALOG (object);
  GladeWidget *gwidget = glade_widget_get_from_gobject (object);

  g_return_if_fail (gwidget);

  if (strcmp (id, "image") == 0)
    {
      GtkWidget *image = NULL;
      GladeWidget *gimage = NULL;
      gint rslt;

      rslt = glade_gtk_message_dialog_image_determine_action (dialog, value,
                                                              &image, &gimage);
      switch (rslt)
        {
          case MD_IMAGE_ACTION_INVALID:
            return;
          case MD_IMAGE_ACTION_RESET:
            glade_gtk_message_dialog_reset_image (dialog);
            return;
          case MD_IMAGE_ACTION_SET:
            break;              /* continue setting the property */
        }

      if (gtk_widget_get_parent (image))
        g_critical ("Image should have no parent now");

G_GNUC_BEGIN_IGNORE_DEPRECATIONS
      gtk_message_dialog_set_image (dialog, image);
G_GNUC_END_IGNORE_DEPRECATIONS

      {
        /* syncing "message-type" property */
        GladeProperty *property;

        property = glade_widget_get_property (gwidget, "message-type");
        if (!glade_property_equals (property, GTK_MESSAGE_OTHER))
          glade_command_set_property (property, GTK_MESSAGE_OTHER);
      }
    }
  else
    {
      /* We must reset the image to internal,
       * external image would otherwise become internal
       */
      if (!strcmp (id, "message-type") &&
          g_value_get_enum (value) != GTK_MESSAGE_OTHER)
        {
          GladeProperty *property;

          property = glade_widget_get_property (gwidget, "image");
          if (!glade_property_equals (property, NULL))
            glade_command_set_property (property, NULL);
        }
      /* Chain up, even if property us message-type because
       * it's not fully handled here
       */
      GLADE_WIDGET_ADAPTOR_GET_ADAPTOR_CLASS (GTK_TYPE_DIALOG)->set_property (adaptor, object,
                                                     id, value);
    }
}

gboolean
glade_gtk_message_dialog_verify_property (GladeWidgetAdaptor * adaptor,
                                          GObject * object,
                                          const gchar * id,
                                          const GValue * value)
{
  if (!strcmp (id, "image"))
    {
      GtkWidget *image;
      GladeWidget *gimage;

      gboolean retval = MD_IMAGE_ACTION_INVALID !=
          glade_gtk_message_dialog_image_determine_action (GTK_MESSAGE_DIALOG
                                                           (object),
                                                           value, &image,
                                                           &gimage);

      return retval;
    }
  else if (GLADE_WIDGET_ADAPTOR_GET_ADAPTOR_CLASS (GTK_TYPE_CONTAINER)->verify_property)
    return GLADE_WIDGET_ADAPTOR_GET_ADAPTOR_CLASS (GTK_TYPE_CONTAINER)->verify_property (adaptor, object,
                                                                id, value);
  else
    return TRUE;
}

void
glade_gtk_message_dialog_get_property (GladeWidgetAdaptor * adaptor,
                                       GObject * object,
                                       const gchar * property_name,
                                       GValue * value)
{
  if (!strcmp (property_name, "image"))
    {
      GtkMessageDialog *dialog = GTK_MESSAGE_DIALOG (object);
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
      GtkWidget *image = gtk_message_dialog_get_image (dialog);
G_GNUC_END_IGNORE_DEPRECATIONS

      if (!glade_widget_get_from_gobject (image))
        g_value_set_object (value, NULL);
      else
        g_value_set_object (value, image);
    }
  else
    GLADE_WIDGET_ADAPTOR_GET_ADAPTOR_CLASS (GTK_TYPE_DIALOG)->get_property (adaptor, object,
                                                   property_name, value);
}