summaryrefslogtreecommitdiff
path: root/glib/glibmm/property.h
blob: daf92eca1b9aff722e12c8435f6cf10db68f3373 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#ifndef _GLIBMM_PROPERTY_H
#define _GLIBMM_PROPERTY_H

/* Copyright 2002 The gtkmm 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 <glibmmconfig.h>
#include <glibmm/propertyproxy.h>
#include <glibmm/value.h>

namespace Glib
{

#ifndef DOXYGEN_SHOULD_SKIP_THIS

#ifdef GLIBMM_CXX_CAN_USE_NAMESPACES_INSIDE_EXTERNC
// For the AIX xlC compiler, I can not find a way to do this without putting the functions in the
// global namespace. murrayc
extern "C" {
#endif // GLIBMM_CXX_CAN_USE_NAMESPACES_INSIDE_EXTERNC

GLIBMM_API
void custom_get_property_callback(
  GObject* object, unsigned int property_id, GValue* value, GParamSpec* param_spec);

GLIBMM_API
void custom_set_property_callback(
  GObject* object, unsigned int property_id, const GValue* value, GParamSpec* param_spec);

#ifdef GLIBMM_CXX_CAN_USE_NAMESPACES_INSIDE_EXTERNC
} // extern "C"
#endif // GLIBMM_CXX_CAN_USE_NAMESPACES_INSIDE_EXTERNC

#endif /* DOXYGEN_SHOULD_SKIP_THIS */

/** This is the base class for Glib::Object properties.
 *
 * This class manages the generic parts of the object properties.
 * Derived (templated) classes handle the specific value types.
 */
class GLIBMM_API PropertyBase
{
public:
  // noncopyable
  PropertyBase(const PropertyBase&) = delete;
  PropertyBase& operator=(const PropertyBase&) = delete;

  /** Returns the name of the property.
   */
  Glib::ustring get_name() const;

  /** Returns the nickname of the property.
   */
  Glib::ustring get_nick() const;

  /** Returns the short description of the property.
   */
  Glib::ustring get_blurb() const;

  /** Notifies the object containing the property that the property has changed.
   * This emits the "notify" signal, passing the property name.
   */
  void notify();

protected:
  Glib::Object* object_;
  Glib::ValueBase value_;
  GParamSpec* param_spec_;

  /** This constructs a property of type @a value_type for the @a object.
   * The property is not registered in the GObject object system
   * until install_property() has been called. Derived classes do this in
   * their constructors.
   *
   * The properties are usually installed during the initialization of the
   * first instance of an object.
   */
  PropertyBase(Glib::Object& object, GType value_type);
  ~PropertyBase() noexcept;

  /**
   * Checks if the property has already been installed.
   */
  bool lookup_property(const Glib::ustring& name);

  /**
   * Installs the property specified by the given @a param_spec.
   */
  void install_property(GParamSpec* param_spec);

  /**
   * Returns the name of the property.
   */
  const char* get_name_internal() const;

private:
#ifndef DOXYGEN_SHOULD_SKIP_THIS

  friend void Glib::custom_get_property_callback(
    GObject* object, unsigned int property_id, GValue* value, GParamSpec* param_spec);

  friend void Glib::custom_set_property_callback(
    GObject* object, unsigned int property_id, const GValue* value, GParamSpec* param_spec);

#endif /* DOXYGEN_SHOULD_SKIP_THIS */
};

/** A Glib::Object property.
 *
 * This class wraps a GObject property, providing a C++ API to the GObject property
 * system, for use with classes derived from Glib::Object or Glib::Interface.
 *
 * A property is a value associated with each instance of a type and some
 * class data for each property:
 *  * Its unique name, used to identify the property.
 *  * A human-readable nick name.
 *  * A short description.
 *  * The default value and the minimum and maximum bounds (depending on the type of the property).
 *  * Flags, defining, among other things, whether the property can be read or written.
 *
 * This %Property class currently supports the name, nick name, description, default value and flags.
 * The minimum and maximum bounds are set to the full range of the value.
 * Because of internal implementation, flags shouldn't be set to values: Glib::ParamFlags::STATIC_NAME,
 * Glib::ParamFlags::STATIC_NICK, Glib::ParamFlags::STATIC_BLURB, Glib::ParamFlags::CONSTRUCT and
 * Glib::ParamFlags::CONSTRUCT_ONLY.
 *
 * The class information must be installed into the GObject system once per
 * property, but this is handled automatically.
 *
 * Each property belongs to an object, inheriting from Glib::Object.
 * A reference to the object must be passed to the constructor of the property.
 *
 * Each instance of a Glib::Object-derived type must construct the same properties
 * (same type, same name) in the same order. One way to achieve this is to
 * declare all properties as direct data members of the type.
 *
 * You may register new properties for your class (actually for the underlying GType)
 * simply by adding a %Property instance as a class member.
 * However, your constructor must call the Glib::ObjectBase constructor with a new GType name,
 * in order to register a new GType.
 *
 * Example:
 * @code
 * class MyCellRenderer : public Gtk::CellRenderer
 * {
 * public:
 *   MyCellRenderer()
 *   :
 *   Glib::ObjectBase (typeid(MyCellRenderer)),
 *   Gtk::CellRenderer(),
 *   property_mybool  (*this, "mybool", true),
 *   property_myint_  (*this, "myint",    42)
 *   {}
 *
 *   virtual ~MyCellRenderer() {}
 *
 *   // Glib::Property<> can be public,
 *   Glib::Property<bool> property_mybool;
 *   // or private, and combined with Glib::PropertyProxy<>.
 *   Glib::PropertyProxy<int> property_myint() { return property_myint_.get_proxy(); }
 *   Glib::PropertyProxy_ReadOnly<int> property_myint() const { return property_myint_.get_proxy(); }
 *
 * private:
 *   Glib::Property<int> property_myint_;
 * };
 * @endcode
 *
 * @par %Glib::Property and Gtk::Builder
 * The new GType is registered, and the properties installed in the GType, when
 * the first instance of the class is created. When the underlying GObject-derived
 * instance is created before the wrapping Glib::Object-derived instance, you may
 * have to first create a dummy instance just to register the GType.
 * See the description of Gtk::Builder for instructions how to combine %Property
 * with Gtk::Builder.
 */
template <class T>
class Property : public PropertyBase
{
public:
  using PropertyType = T;
  using ValueType = Glib::Value<T>;

  /**  Constructs a property of the @a object with the specified @a name.
   * For each instance of the object, the same property must be constructed with the same name.
   */
  Property(Glib::Object& object, const Glib::ustring& name);

  /** Constructs a property of the @a object with the specified @a name and @a default_value.
   * For  each instance of the object, the same property must be constructed with the same name.
   */
  Property(Glib::Object& object, const Glib::ustring& name, const PropertyType& default_value);

  /** Constructs a property of the @a object with the specified @a name, @a nick, @a blurb and
   * @a flags.
   * For  each instance of the object, the same property must be constructed with the same name.
   */
  Property(Glib::Object& object, const Glib::ustring& name, const Glib::ustring& nick,
           const Glib::ustring& blurb, Glib::ParamFlags flags);

  /** Constructs a property of the @a object with the specified @a name, @a default_value, @a nick,
   * @a blurb and @a flags.
   * For  each instance of the object, the same property must be constructed with the same name.
   */
  Property(Glib::Object& object, const Glib::ustring& name, const PropertyType& default_value,
           const Glib::ustring& nick, const Glib::ustring& blurb, Glib::ParamFlags flags);

  /** Sets the value of the property to @a data.
   * The object containing the property will be notified about the change.
   */
  inline void set_value(const PropertyType& data);

  /** Returns the value of the property.
   */
  inline PropertyType get_value() const;

  /** Sets the value of the property to @a data.
   * The object containing the property will be notified about the change.
   */
  inline Property<T>& operator=(const PropertyType& data);

  /** Returns the value of the property.
   */
  inline operator PropertyType() const;

  /** Returns a proxy object that can be used to read or write this property.
   */
  inline Glib::PropertyProxy<T> get_proxy();

  /** Returns a proxy object that can be used to read this property.
   */
  inline Glib::PropertyProxy_ReadOnly<T> get_proxy() const;
};

/** See Property.
 * This property can be read, but not written, so there is no set_value() method.
 */
template <class T>
class Property_ReadOnly : public PropertyBase
{
public:
  typedef T PropertyType;
  typedef Glib::Value<T> ValueType;

  /**  Constructs a property of the @a object with the specified @a name.
   * For each instance of the object, the same property must be constructed with the same name.
   */
  Property_ReadOnly(Glib::Object& object, const Glib::ustring& name);

  /** Constructs a property of the @a object with the specified @a name and @a default_value.
   * For  each instance of the object, the same property must be constructed with the same name.
   */
  Property_ReadOnly(Glib::Object& object, const Glib::ustring& name, const PropertyType& default_value);

  /** Constructs a property of the @a object with the specified @a name, @a nick, @a blurb and
   * @a flags.
   * For  each instance of the object, the same property must be constructed with the same name.
   */
  Property_ReadOnly(Glib::Object& object, const Glib::ustring& name, const Glib::ustring& nick,
           const Glib::ustring& blurb, Glib::ParamFlags flags);

  /** Constructs a property of the @a object with the specified @a name, @a default_value, @a nick,
   * @a blurb and @a flags.
   * For  each instance of the object, the same property must be constructed with the same name.
   */
  Property_ReadOnly(Glib::Object& object, const Glib::ustring& name, const PropertyType& default_value,
           const Glib::ustring& nick, const Glib::ustring& blurb, Glib::ParamFlags flags);

  /** Returns the value of the property.
   */
  inline PropertyType get_value() const;

  /** Returns the value of the property.
   */
  inline operator PropertyType() const;

  /** Returns a proxy object that can be used to read this property.
   */
  inline Glib::PropertyProxy_ReadOnly<T> get_proxy() const;
};

/** See Property.
 * This property can be written, but not read, so there is no get_value() method.
 */
template <class T>
class Property_WriteOnly : public PropertyBase
{
public:
  typedef T PropertyType;
  typedef Glib::Value<T> ValueType;

  /**  Constructs a property of the @a object with the specified @a name.
   * For each instance of the object, the same property must be constructed with the same name.
   */
  Property_WriteOnly(Glib::Object& object, const Glib::ustring& name);

  /** Constructs a property of the @a object with the specified @a name and @a default_value.
   * For  each instance of the object, the same property must be constructed with the same name.
   */
  Property_WriteOnly(Glib::Object& object, const Glib::ustring& name, const PropertyType& default_value);

  /** Constructs a property of the @a object with the specified @a name, @a nick, @a blurb and
   * @a flags.
   * For  each instance of the object, the same property must be constructed with the same name.
   */
  Property_WriteOnly(Glib::Object& object, const Glib::ustring& name, const Glib::ustring& nick,
           const Glib::ustring& blurb, Glib::ParamFlags flags);

  /** Constructs a property of the @a object with the specified @a name, @a default_value, @a nick,
   * @a blurb and @a flags.
   * For  each instance of the object, the same property must be constructed with the same name.
   */
  Property_WriteOnly(Glib::Object& object, const Glib::ustring& name, const PropertyType& default_value,
           const Glib::ustring& nick, const Glib::ustring& blurb, Glib::ParamFlags flags);

  /** Sets the value of the property to @a data.
   * The object containing the property will be notified about the change.
   */
  inline void set_value(const PropertyType& data);

  /** Sets the value of the property to @a data.
   * The object containing the property will be notified about the change.
   */
  inline Property_WriteOnly<T>& operator=(const PropertyType& data);

  /** Returns a proxy object that can be used to write this property.
   */
  inline Glib::PropertyProxy_WriteOnly<T> get_proxy();
};

#ifndef DOXYGEN_SHOULD_SKIP_THIS

/**** Glib::Property<T> ****************************************************/

template <class T>
Property<T>::Property(Glib::Object& object, const Glib::ustring& name)
: Property(object, name, Glib::ustring(), Glib::ustring(), Glib::ParamFlags::READWRITE)
{
}

template <class T>
Property<T>::Property(Glib::Object& object, const Glib::ustring& name,
  const typename Property<T>::PropertyType& default_value)
: Property(object, name, default_value, Glib::ustring(),
    Glib::ustring(), Glib::ParamFlags::READWRITE)
{
}

template <class T>
Property<T>::Property(Glib::Object& object, const Glib::ustring& name,
           const Glib::ustring& nick, const Glib::ustring& blurb, Glib::ParamFlags flags)
: PropertyBase(object, ValueType::value_type())
{
  flags |= Glib::ParamFlags::READWRITE;

  if (!lookup_property(name))
    install_property(static_cast<ValueType&>(value_).create_param_spec(name, nick, blurb, flags));
}

template <class T>
Property<T>::Property(Glib::Object& object, const Glib::ustring& name, const PropertyType& default_value,
           const Glib::ustring& nick, const Glib::ustring& blurb, Glib::ParamFlags flags)
:
  PropertyBase(object, ValueType::value_type())
{
  flags |= Glib::ParamFlags::READWRITE;

  static_cast<ValueType&>(value_).set(default_value);

  if (!lookup_property(name))
    install_property(static_cast<ValueType&>(value_).create_param_spec(name, nick, blurb, flags));
}

template <class T>
inline void
Property<T>::set_value(const typename Property<T>::PropertyType& data)
{
  static_cast<ValueType&>(value_).set(data);
  this->notify();
}

template <class T>
inline typename Property<T>::PropertyType
Property<T>::get_value() const
{
  return static_cast<const ValueType&>(value_).get();
}

template <class T>
inline Property<T>&
Property<T>::operator=(const typename Property<T>::PropertyType& data)
{
  static_cast<ValueType&>(value_).set(data);
  this->notify();
  return *this;
}

template <class T>
inline Property<T>::operator T() const
{
  return static_cast<const ValueType&>(value_).get();
}

template <class T>
inline Glib::PropertyProxy<T>
Property<T>::get_proxy()
{
  return Glib::PropertyProxy<T>(object_, get_name_internal());
}

template <class T>
inline Glib::PropertyProxy_ReadOnly<T>
Property<T>::get_proxy() const
{
  return Glib::PropertyProxy_ReadOnly<T>(object_, get_name_internal());
}

/**** Glib::Property_ReadOnly<T> ****************************************************/

template <class T>
Property_ReadOnly<T>::Property_ReadOnly(Glib::Object& object, const Glib::ustring& name)
: Property_ReadOnly(object, name, Glib::ustring(), Glib::ustring(), Glib::ParamFlags::READABLE)
{
}

template <class T>
Property_ReadOnly<T>::Property_ReadOnly(Glib::Object& object, const Glib::ustring& name,
  const typename Property_ReadOnly<T>::PropertyType& default_value)
: Property_ReadOnly(object, name, default_value, Glib::ustring(), Glib::ustring(),
    Glib::ParamFlags::READABLE)
{
}

template <class T>
Property_ReadOnly<T>::Property_ReadOnly(Glib::Object& object, const Glib::ustring& name,
           const Glib::ustring& nick, const Glib::ustring& blurb, Glib::ParamFlags flags)
: PropertyBase(object, ValueType::value_type())
{
  flags |= Glib::ParamFlags::READABLE;
  flags &= ~Glib::ParamFlags::WRITABLE;

  if (!lookup_property(name))
    install_property(static_cast<ValueType&>(value_).create_param_spec(name, nick, blurb, flags));
}

template <class T>
Property_ReadOnly<T>::Property_ReadOnly(Glib::Object& object, const Glib::ustring& name, const PropertyType& default_value,
           const Glib::ustring& nick, const Glib::ustring& blurb, Glib::ParamFlags flags)
: PropertyBase(object, ValueType::value_type())
{
  flags |= Glib::ParamFlags::READABLE;
  flags &= ~Glib::ParamFlags::WRITABLE;

  static_cast<ValueType&>(value_).set(default_value);

  if (!lookup_property(name))
    install_property(static_cast<ValueType&>(value_).create_param_spec(name, nick, blurb, flags));
}

template <class T>
inline typename Property_ReadOnly<T>::PropertyType
Property_ReadOnly<T>::get_value() const
{
  return static_cast<const ValueType&>(value_).get();
}

template <class T>
inline Property_ReadOnly<T>::operator T() const
{
  return static_cast<const ValueType&>(value_).get();
}

template <class T>
inline Glib::PropertyProxy_ReadOnly<T>
Property_ReadOnly<T>::get_proxy() const
{
  return Glib::PropertyProxy_ReadOnly<T>(object_, get_name_internal());
}

/**** Glib::Property_WriteOnly<T> ****************************************************/

template <class T>
Property_WriteOnly<T>::Property_WriteOnly(Glib::Object& object, const Glib::ustring& name)
: Property_WriteOnly(object, name, Glib::ustring(),
    Glib::ustring(), Glib::ParamFlags::WRITABLE)
{
}

template <class T>
Property_WriteOnly<T>::Property_WriteOnly(Glib::Object& object, const Glib::ustring& name,
  const typename Property_WriteOnly<T>::PropertyType& default_value)
: Property_WriteOnly(object, name, default_value, Glib::ustring(),
    Glib::ustring(), Glib::ParamFlags::WRITABLE)
{
}

template <class T>
Property_WriteOnly<T>::Property_WriteOnly(Glib::Object& object, const Glib::ustring& name,
           const Glib::ustring& nick, const Glib::ustring& blurb, Glib::ParamFlags flags)
: PropertyBase(object, ValueType::value_type())
{
  flags |= Glib::ParamFlags::WRITABLE;
  flags &= ~Glib::ParamFlags::READABLE;

  if (!lookup_property(name))
    install_property(static_cast<ValueType&>(value_).create_param_spec(name, nick, blurb, flags));

}

template <class T>
Property_WriteOnly<T>::Property_WriteOnly(Glib::Object& object, const Glib::ustring& name, const PropertyType& default_value,
           const Glib::ustring& nick, const Glib::ustring& blurb, Glib::ParamFlags flags)
: PropertyBase(object, ValueType::value_type())
{
  flags |= Glib::ParamFlags::WRITABLE;
  flags &= ~Glib::ParamFlags::READABLE;

  static_cast<ValueType&>(value_).set(default_value);

  if (!lookup_property(name))
    install_property(static_cast<ValueType&>(value_).create_param_spec(name, nick, blurb, flags));
}

template <class T>
inline void
Property_WriteOnly<T>::set_value(const typename Property_WriteOnly<T>::PropertyType& data)
{
  static_cast<ValueType&>(value_).set(data);
  this->notify();
}

template <class T>
inline Property_WriteOnly<T>&
Property_WriteOnly<T>::operator=(const typename Property_WriteOnly<T>::PropertyType& data)
{
  static_cast<ValueType&>(value_).set(data);
  this->notify();
  return *this;
}

template <class T>
inline Glib::PropertyProxy_WriteOnly<T>
Property_WriteOnly<T>::get_proxy()
{
  return Glib::PropertyProxy_WriteOnly<T>(object_, get_name_internal());
}
#endif /* DOXYGEN_SHOULD_SKIP_THIS */

} // namespace Glib

#endif /* _GLIBMM_PROPERTY_H */