summaryrefslogtreecommitdiff
path: root/tp-account-widgets/tpaw-builder.c
blob: cd5a3ea778d22bb8966739115551c3a6916914da (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
/*
 * Copyright (C) 2013 Collabora Ltd.
 *
 * Authors: Marco Barisione <marco.barisione@collabora.co.uk>
 *          Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
 *          Xavier Claessens <xavier.claessens@collabora.co.uk>
 *          Mikael Hallendal <micke@imendio.com>
 *          Richard Hult <richard@imendio.com>
 *          Martyn Russell <martyn@imendio.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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"
#include "tpaw-builder.h"

#define DEBUG_FLAG TPAW_DEBUG_OTHER
#include "tpaw-debug.h"

enum _BuilderSource
{
  BUILDER_SOURCE_FILE,
  BUILDER_SOURCE_RESOURCE
};

static GtkBuilder *
builder_get_valist (const gchar *sourcename,
    enum _BuilderSource source,
    const gchar *translation_domain,
    const gchar *first_object,
    va_list args)
{
  GtkBuilder *gui;
  const gchar *name;
  GObject **object_ptr;
  GError *error = NULL;
  gboolean success;

  DEBUG ("Loading %s '%s'", source == BUILDER_SOURCE_FILE ? "file" : "resource", sourcename);

  gui = gtk_builder_new ();
  gtk_builder_set_translation_domain (gui, translation_domain);

  switch (source)
    {
    case BUILDER_SOURCE_FILE:
      success = gtk_builder_add_from_file (gui, sourcename, &error);
      break;
    case BUILDER_SOURCE_RESOURCE:
      success = gtk_builder_add_from_resource (gui, sourcename, &error);
      break;
    default:
      g_assert_not_reached ();
    }

  if (!success)
    {
      g_critical ("GtkBuilder Error (%s): %s",
          sourcename, error->message);

      g_clear_error (&error);
      g_object_unref (gui);

      /* we need to iterate and set all of the pointers to NULL */
      for (name = first_object; name; name = va_arg (args, const gchar *))
        {
          object_ptr = va_arg (args, GObject**);

          *object_ptr = NULL;
        }

      return NULL;
    }

  for (name = first_object; name; name = va_arg (args, const gchar *))
    {
      object_ptr = va_arg (args, GObject**);

      *object_ptr = gtk_builder_get_object (gui, name);

      if (!*object_ptr)
        {
          g_warning ("File is missing object '%s'.", name);
          continue;
        }
    }

  return gui;
}

GtkBuilder *
tpaw_builder_get_file_with_domain (const gchar *filename,
    const gchar *translation_domain,
    const gchar *first_object,
    ...)
{
  GtkBuilder *gui;
  va_list args;

  va_start (args, first_object);
  gui = builder_get_valist (filename, BUILDER_SOURCE_FILE,
      translation_domain, first_object, args);
  va_end (args);

  return gui;
}

GtkBuilder *
tpaw_builder_get_resource_with_domain (const gchar *resourcename,
    const gchar *translation_domain,
    const gchar *first_object,
    ...)
{
  GtkBuilder *gui;
  va_list args;

  va_start (args, first_object);
  gui = builder_get_valist (resourcename, BUILDER_SOURCE_RESOURCE,
      translation_domain, first_object, args);
  va_end (args);

  return gui;
}

void
tpaw_builder_connect (GtkBuilder *gui,
    gpointer user_data,
    const gchar *first_object,
    ...)
{
  va_list args;
  const gchar *name;
  const gchar *sig;
  GObject *object;
  GCallback callback;

  va_start (args, first_object);
  for (name = first_object; name; name = va_arg (args, const gchar *))
    {
      sig = va_arg (args, const gchar *);
      callback = va_arg (args, GCallback);

      object = gtk_builder_get_object (gui, name);
      if (!object)
        {
          g_warning ("File is missing object '%s'.", name);
          continue;
        }

      g_signal_connect (object, sig, callback, user_data);
    }

  va_end (args);
}

GtkWidget *
tpaw_builder_unref_and_keep_widget (GtkBuilder *gui,
    GtkWidget *widget)
{
  /* On construction gui sinks the initial reference to widget. When gui
   * is finalized it will drop its ref to widget. We take our own ref to
   * prevent widget being finalised. The widget is forced to have a
   * floating reference, like when it was initially unowned so that it can
   * be used like any other GtkWidget. */

  g_object_ref (widget);
  g_object_force_floating (G_OBJECT (widget));
  g_object_unref (gui);

  return widget;
}