summaryrefslogtreecommitdiff
path: root/glib/src/binding.ccg
blob: c9e839b7317a6206d518d4cfa967098f15835095 (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
/* Copyright (C) 2014 The glibmm Development Team
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

#include <glibmm/binding.h>
#include <glib.h>

namespace
{
struct BindingTransformSlots
{
  BindingTransformSlots(
    const Glib::Binding::BindingTransformSlot& transform_to,
    const Glib::Binding::BindingTransformSlot& transform_from)
  :
  from_source_to_target(transform_to), from_target_to_source(transform_from)
  {}

  Glib::Binding::BindingTransformSlot from_source_to_target;
  Glib::Binding::BindingTransformSlot from_target_to_source;
};

gboolean Binding_transform_callback_common(GBinding* binding,
  const GValue* from_value, GValue* to_value,
  Glib::Binding::BindingTransformSlot& the_slot)
{
  bool result = false;
  try
  {
    Glib::RefPtr<Glib::Binding> cpp_binding = Glib::wrap(binding, true);
    result = the_slot(cpp_binding, from_value, to_value);
  }
  catch (...)
  {
    Glib::exception_handlers_invoke();
  }
  return result;
}

gboolean Binding_transform_to_callback(GBinding* binding,
  const GValue* from_value, GValue* to_value, gpointer user_data)
{
  Glib::Binding::BindingTransformSlot& the_slot =
    static_cast<BindingTransformSlots*>(user_data)->from_source_to_target;

  return Binding_transform_callback_common(binding, from_value, to_value, the_slot);
}

gboolean Binding_transform_from_callback(GBinding* binding,
  const GValue* from_value, GValue* to_value, gpointer user_data)
{
  Glib::Binding::BindingTransformSlot& the_slot =
    static_cast<BindingTransformSlots*>(user_data)->from_target_to_source;

  return Binding_transform_callback_common(binding, from_value, to_value, the_slot);
}

void Binding_transform_callback_destroy(gpointer user_data)
{
  delete static_cast<BindingTransformSlots*>(user_data);
}

} // anonymous namespace

namespace Glib
{
//static
Glib::RefPtr<Binding> Binding::bind_property_value(
  const PropertyProxy_Base& source_property,
  const PropertyProxy_Base& target_property,
  BindingFlags flags,
  const BindingTransformSlot& transform_to,
  const BindingTransformSlot& transform_from)
{
  GBinding* binding = 0;
  if (transform_to.empty() && transform_from.empty())
  {
    // No user-supplied transformations.
    binding = g_object_bind_property(
      source_property.get_object()->gobj(), source_property.get_name(),
      target_property.get_object()->gobj(), target_property.get_name(),
      (GBindingFlags)flags);
  }
  else
  {
    // Create copies of the slots. A pointer to this will be passed
    // through the callback's data parameter. It will be deleted
    // when Binding_transform_callback_destroy() is called.
    BindingTransformSlots* slots_copy = new BindingTransformSlots(transform_to, transform_from);

    binding = g_object_bind_property_full(
      source_property.get_object()->gobj(), source_property.get_name(),
      target_property.get_object()->gobj(), target_property.get_name(),
      (GBindingFlags)flags,
      transform_to.empty() ? 0 : &Binding_transform_to_callback,
      transform_from.empty() ? 0 : &Binding_transform_from_callback,
      slots_copy, &Binding_transform_callback_destroy);
  }

  if (!binding)
    return Glib::RefPtr<Binding>();

  // Take an extra ref. GBinding uses one ref itself, and drops it if
  // either the source object or the target object is finalized.
  // The GBinding object must not be destroyed while there are RefPtrs around.
  g_object_ref(binding);
  return Glib::RefPtr<Binding>(new Binding(binding));
}

void Binding::unbind()
{
  // Call g_binding_unbind() only once. It always calls g_object_unref().
  if (g_binding_get_source(gobj()))
    g_binding_unbind(gobj());
}

// Override unreference() from ObjectBase.
//
// Why is this necessary? Because GBinding is an unusual kind of GObject.
// It calls g_object_unref() itself, if either the source object or the
// target object is finalized, almost like g_binding_unbind().
// But the GBinding object shall be destroyed when and only when the last
// reference from a Glib::RefPtr is dropped.
void Binding::unreference() const
{
  GBinding* const binding = const_cast<GBinding*>(gobj());

  // If the last Glib::RefPtr is being deleted, and the binding has not been unbound,
  // then drop the extra reference that was added by bind_property_value().
  if (gobject_->ref_count == 2 && g_binding_get_source(binding))
    g_object_unref(binding);

  Object::unreference();
}

} // namespace Glib