glib2 2.2.5 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,6 @@
1
1
  /* -*- c-file-style: "ruby"; indent-tabs-mode: nil -*- */
2
2
  /*
3
- * Copyright (C) 2011 Ruby-GNOME2 Project Team
4
- * Copyright (C) 2004-2006 Ruby-GNOME2 Project Team
3
+ * Copyright (C) 2004-2015 Ruby-GNOME2 Project Team
5
4
  * Copyright (C) 2002,2003 Masahiro Sakai
6
5
  *
7
6
  * This library is free software; you can redistribute it and/or
@@ -30,33 +29,10 @@ VALUE RG_TARGET_NAMESPACE;
30
29
  static ID id_new;
31
30
  static ID id_module_eval;
32
31
  static ID id_or;
32
+ static ID id_to_i;
33
33
 
34
34
  /**********************************************************************/
35
35
 
36
- static VALUE
37
- resolve_flags_value(VALUE klass, VALUE nick_or_nicks)
38
- {
39
- int i, len;
40
- VALUE flags_value;
41
-
42
- if (!RVAL2CBOOL(rb_obj_is_kind_of(nick_or_nicks, rb_cArray)))
43
- return rg_enum_resolve_value(klass, nick_or_nicks);
44
-
45
- len = RARRAY_LEN(nick_or_nicks);
46
- flags_value = rb_funcall(klass, id_new, 0);
47
- for (i = 0; i < len; i++) {
48
- VALUE value;
49
-
50
- value = rg_enum_resolve_value(klass, RARRAY_PTR(nick_or_nicks)[i]);
51
- if (NIL_P(value))
52
- return Qnil;
53
-
54
- flags_value = rb_funcall(flags_value, id_or, 1, value);
55
- }
56
-
57
- return flags_value;
58
- }
59
-
60
36
  void
61
37
  rg_flags_add_constants(VALUE mod, GType flags_type, const gchar *strip_prefix)
62
38
  {
@@ -127,25 +103,13 @@ rbgobj_get_flags(VALUE obj, GType gtype)
127
103
  rb_raise(rb_eTypeError, "%s is not a %s",
128
104
  g_type_name(gtype), g_type_name(G_TYPE_FLAGS));
129
105
 
130
- /* for compatibility */
131
- if (rb_obj_is_kind_of(obj, rb_cInteger))
132
- obj = rbgobj_make_flags(NUM2UINT(obj), gtype);
133
-
134
106
  klass = GTYPE2CLASS(gtype);
135
107
 
136
- if (!rb_obj_is_kind_of(obj, klass)) {
137
- VALUE flags_value = Qnil;
138
-
139
- flags_value = resolve_flags_value(klass, obj);
140
- if (!NIL_P(flags_value))
141
- obj = flags_value;
108
+ if (!RVAL2CBOOL(rb_obj_is_kind_of(obj, klass))) {
109
+ obj = rb_funcall(klass, id_new, 1, obj);
142
110
  }
143
111
 
144
- if (rb_obj_is_kind_of(obj, klass))
145
- return flags_get_holder(obj)->value;
146
- else
147
- rb_raise(rb_eTypeError, "not a %s: %s",
148
- rb_class2name(klass), RBG_INSPECT(obj));
112
+ return flags_get_holder(obj)->value;
149
113
  }
150
114
 
151
115
  /**********************************************************************/
@@ -251,29 +215,80 @@ flags_s_allocate(VALUE self)
251
215
  }
252
216
  }
253
217
 
218
+ static guint
219
+ resolve_flags_value(VALUE klass, GFlagsClass *gclass, VALUE flag_or_flags)
220
+ {
221
+ guint value = 0;
222
+
223
+ switch (TYPE(flag_or_flags)) {
224
+ case RUBY_T_NIL:
225
+ value = 0;
226
+ break;
227
+ case RUBY_T_FIXNUM:
228
+ case RUBY_T_BIGNUM:
229
+ value = NUM2UINT(flag_or_flags);
230
+ break;
231
+ case RUBY_T_STRING:
232
+ case RUBY_T_SYMBOL:
233
+ {
234
+ const gchar *name;
235
+ GFlagsValue *info;
236
+
237
+ name = RVAL2CSTR_ACCEPT_SYMBOL(flag_or_flags);
238
+ info = g_flags_get_value_by_name(gclass, name);
239
+ if (!info) {
240
+ gchar *nick;
241
+ nick = rbg_name_to_nick(name);
242
+ info = g_flags_get_value_by_nick(gclass, nick);
243
+ g_free(nick);
244
+ }
245
+ if (!info) {
246
+ rb_raise(rb_eArgError,
247
+ "unknown flag name: <%s>(%s)",
248
+ name,
249
+ g_type_name(G_TYPE_FROM_CLASS(gclass)));
250
+ }
251
+ value = info->value;
252
+ break;
253
+ }
254
+ case RUBY_T_ARRAY:
255
+ {
256
+ int i, n;
257
+ n = RARRAY_LEN(flag_or_flags);
258
+ for (i = 0; i < n; i++) {
259
+ value |= resolve_flags_value(klass,
260
+ gclass,
261
+ RARRAY_PTR(flag_or_flags)[i]);
262
+ }
263
+ break;
264
+ }
265
+ default:
266
+ if (RVAL2CBOOL(rb_obj_is_kind_of(flag_or_flags, klass))) {
267
+ value = NUM2UINT(rb_funcall(flag_or_flags, id_to_i, 0));
268
+ } else {
269
+ rb_raise(rb_eArgError,
270
+ "flag value must be one of "
271
+ "nil, Fixnum, String, Symbol, %s or Array of them: "
272
+ "<%s>(%s)",
273
+ RBG_INSPECT(klass),
274
+ RBG_INSPECT(flag_or_flags),
275
+ g_type_name(G_TYPE_FROM_CLASS(gclass)));
276
+ }
277
+ break;
278
+ }
279
+
280
+ return value;
281
+ }
282
+
254
283
  static VALUE
255
284
  rg_initialize(int argc, VALUE* argv, VALUE self)
256
285
  {
257
286
  flags_holder* p = flags_get_holder(self);
258
- VALUE arg;
287
+ VALUE flag_or_flags;
259
288
 
260
- rb_scan_args(argc, argv, "01", &arg);
289
+ rb_scan_args(argc, argv, "01", &flag_or_flags);
261
290
 
262
- if (argc == 0) {
263
- p->value = 0;
264
- } else {
265
- if (rb_respond_to(arg, rb_intern("to_str"))) {
266
- const char* str = StringValuePtr(arg);
267
- p->info = g_flags_get_value_by_name(p->gclass, str);
268
- if (!p->info)
269
- p->info = g_flags_get_value_by_nick(p->gclass, str);
270
- if (!p->info)
271
- rb_raise(rb_eArgError, "invalid argument");
272
- p->value = p->info->value;
273
- } else {
274
- p->value = NUM2UINT(arg);
275
- }
276
- }
291
+ p->value = resolve_flags_value(CLASS_OF(self), p->gclass, flag_or_flags);
277
292
 
278
293
  if (!p->info) {
279
294
  guint i;
@@ -316,27 +331,56 @@ rg_nick(VALUE self)
316
331
  #define FLAGS_COMP_ELSE -2
317
332
  #define FLAGS_COMP_INCOMPARABLE -3
318
333
 
334
+ typedef struct {
335
+ GType gtype;
336
+ VALUE rb_value;
337
+ guint value;
338
+ gboolean compatible;
339
+ } compare_data;
340
+
341
+ static VALUE
342
+ flags_compare_get_flags_body(VALUE user_data)
343
+ {
344
+ compare_data *data = (compare_data *)user_data;
345
+
346
+ data->value = rbgobj_get_flags(data->rb_value, data->gtype);
347
+ data->compatible = TRUE;
348
+
349
+ return Qnil;
350
+ }
351
+
352
+ static VALUE
353
+ flags_compare_get_flags_rescue(VALUE user_data)
354
+ {
355
+ compare_data *data = (compare_data *)user_data;
356
+
357
+ data->compatible = FALSE;
358
+
359
+ return Qnil;
360
+ }
361
+
319
362
  static gint
320
363
  flags_compare(VALUE self, VALUE rhs)
321
364
  {
322
365
  flags_holder* p = flags_get_holder(self);
323
- GType gtype = G_TYPE_FROM_CLASS(p->gclass);
324
- VALUE klass = GTYPE2CLASS(gtype);
325
- guint rhs_val;
366
+ compare_data data;
326
367
 
327
- if (!rb_obj_is_kind_of(rhs, rb_cInteger)) {
328
- rhs = resolve_flags_value(klass, rhs);
329
- if (CLASS_OF(rhs) != CLASS_OF(self))
330
- return FLAGS_COMP_INCOMPARABLE;
331
- }
368
+ data.gtype = G_TYPE_FROM_CLASS(p->gclass);
369
+ data.rb_value = rhs;
370
+ data.value = 0;
371
+ data.compatible = TRUE;
332
372
 
333
- rhs_val = rbgobj_get_flags(rhs, gtype);
373
+ rb_rescue(flags_compare_get_flags_body, (VALUE)&data,
374
+ flags_compare_get_flags_rescue, (VALUE)&data);
375
+ if (!data.compatible) {
376
+ return FLAGS_COMP_INCOMPARABLE;
377
+ }
334
378
 
335
- if (p->value == rhs_val)
379
+ if (p->value == data.value)
336
380
  return FLAGS_COMP_EQUAL;
337
- else if ((p->value & rhs_val) == rhs_val)
381
+ else if ((p->value & data.value) == data.value)
338
382
  return FLAGS_COMP_GREATER;
339
- else if ((p->value & rhs_val) == p->value)
383
+ else if ((p->value & data.value) == p->value)
340
384
  return FLAGS_COMP_LESS;
341
385
  else
342
386
  return FLAGS_COMP_ELSE;
@@ -476,6 +520,7 @@ Init_gobject_gflags(void)
476
520
  id_module_eval = rb_intern("module_eval");
477
521
  id_new = rb_intern("new");
478
522
  id_or = rb_intern("|");
523
+ id_to_i = rb_intern("to_i");
479
524
 
480
525
  RG_TARGET_NAMESPACE = G_DEF_CLASS(G_TYPE_FLAGS, "Flags", mGLib);
481
526
 
@@ -1,6 +1,6 @@
1
1
  /* -*- c-file-style: "ruby"; indent-tabs-mode: nil -*- */
2
2
  /*
3
- * Copyright (C) 2011 Ruby-GNOME2 Project Team
3
+ * Copyright (C) 2011-2015 Ruby-GNOME2 Project Team
4
4
  * Copyright (C) 2002-2004 Ruby-GNOME2 Project Team
5
5
  * Copyright (C) 2002-2003 Masahiro Sakai
6
6
  * Copyright (C) 1998-2000 Yukihiro Matsumoto,
@@ -170,15 +170,6 @@ rbgobj_init_object_class(VALUE klass)
170
170
 
171
171
  /**********************************************************************/
172
172
 
173
- static gboolean
174
- is_gtkobject(GObject *gobj)
175
- {
176
- static GType gtype_gtkobject = G_TYPE_INVALID;
177
- if (!gtype_gtkobject)
178
- gtype_gtkobject = g_type_from_name("GtkObject");
179
- return gtype_gtkobject && g_type_is_a(G_OBJECT_TYPE(gobj), gtype_gtkobject);
180
- }
181
-
182
173
  static void
183
174
  gobj_mark(gpointer ptr)
184
175
  {
@@ -227,15 +218,7 @@ rg_s_new_bang(int argc, VALUE *argv, VALUE self)
227
218
 
228
219
  gobj = rbgobj_gobject_new(cinfo->gtype, params_hash);
229
220
  result = GOBJ2RVAL(gobj);
230
-
231
- // XXX: Ughhhhh
232
- if (is_gtkobject(gobj)){
233
- // We can't call gtk_object_sink() here.
234
- // But hopefully someone will call it in the future.
235
- //gtk_object_sink(gobj);
236
- } else {
237
- g_object_unref(gobj);
238
- }
221
+ g_object_unref(gobj);
239
222
 
240
223
  return result;
241
224
  }
@@ -637,6 +620,35 @@ rg_type_name(VALUE self)
637
620
  return CSTR2RVAL(G_OBJECT_TYPE_NAME(RVAL2GOBJ(self)));
638
621
  }
639
622
 
623
+ #if GLIB_CHECK_VERSION(2, 26, 0)
624
+ static VALUE
625
+ rg_bind_property(VALUE self,
626
+ VALUE rb_source_property,
627
+ VALUE rb_target,
628
+ VALUE rb_target_property,
629
+ VALUE rb_flags)
630
+ {
631
+ gpointer source;
632
+ const gchar *source_property;
633
+ gpointer target;
634
+ const gchar *target_property;
635
+ GBindingFlags flags;
636
+ GBinding *binding;
637
+
638
+ source = RVAL2GOBJ(self);
639
+ source_property = RVAL2CSTR(rb_source_property);
640
+ target = RVAL2GOBJ(rb_target);
641
+ target_property = RVAL2CSTR(rb_target_property);
642
+ flags = RVAL2GBINDINGFLAGS(rb_flags);
643
+
644
+ binding = g_object_bind_property(source, source_property,
645
+ target, target_property,
646
+ flags);
647
+
648
+ return GOBJ2RVAL(binding);
649
+ }
650
+ #endif
651
+
640
652
  static VALUE
641
653
  rg_initialize(int argc, VALUE *argv, VALUE self)
642
654
  {
@@ -658,13 +670,6 @@ rg_initialize(int argc, VALUE *argv, VALUE self)
658
670
 
659
671
  gobj = rbgobj_gobject_new(RVAL2GTYPE(self), params_hash);
660
672
 
661
- if (is_gtkobject(gobj)){
662
- gobj = g_object_ref(gobj);
663
- // We can't call gtk_object_sink() here.
664
- // But hopefully someone will call it in the future.
665
- //gtk_object_sink(gobj);
666
- }
667
-
668
673
  G_INITIALIZE(self, gobj);
669
674
  return Qnil;
670
675
  }
@@ -679,7 +684,6 @@ gobj_ref_count(VALUE self)
679
684
 
680
685
  /**********************************************************************/
681
686
 
682
- static VALUE proc_mod_eval;
683
687
  static GQuark q_ruby_setter;
684
688
  static GQuark q_ruby_getter;
685
689
 
@@ -732,55 +736,51 @@ set_prop_func(GObject* object,
732
736
  rb_funcall(GOBJ2RVAL(object), ruby_setter, 1, GVAL2RVAL(value));
733
737
  }
734
738
 
735
- // FIXME: use rb_protect
736
- static void
737
- class_init_func(gpointer g_class_, G_GNUC_UNUSED gpointer class_data)
739
+ void
740
+ rbgobj_class_init_func(gpointer g_class, G_GNUC_UNUSED gpointer class_data)
738
741
  {
739
- GObjectClass* g_class = G_OBJECT_CLASS(g_class_);
742
+ GObjectClass* g_object_class = G_OBJECT_CLASS(g_class);
740
743
 
741
- g_class->set_property = set_prop_func;
742
- g_class->get_property = get_prop_func;
743
-
744
- #if 0
745
- VALUE class_init_proc = (VALUE)class_data;
746
- rb_funcall(proc_mod_eval, rb_intern("call"), 2,
747
- GTYPE2CLASS(G_TYPE_FROM_CLASS(g_class)), class_init_proc);
748
- #endif
744
+ g_object_class->set_property = set_prop_func;
745
+ g_object_class->get_property = get_prop_func;
749
746
  }
750
747
 
751
- static VALUE
752
- rg_s_type_register(int argc, VALUE* argv, VALUE self)
748
+ void
749
+ rbgobj_register_type(VALUE klass, VALUE type_name, GClassInitFunc class_init)
753
750
  {
754
- VALUE type_name, flags;
755
- volatile VALUE class_init_proc = Qnil;
756
751
  GType parent_type;
757
- GTypeInfo* info;
758
-
759
- rb_scan_args(argc, argv, "03", &type_name, &info, &flags);
752
+ GTypeInfo *info;
760
753
 
761
754
  {
762
- const RGObjClassInfo* cinfo = rbgobj_lookup_class(self);
763
- if (cinfo->klass == self)
764
- rb_raise(rb_eTypeError, "already registered");
755
+ const RGObjClassInfo *cinfo = rbgobj_lookup_class(klass);
756
+ if (cinfo->klass == klass)
757
+ rb_raise(rb_eTypeError,
758
+ "already registered class: <%s>",
759
+ RBG_INSPECT(klass));
765
760
  }
766
761
 
767
762
  {
768
- VALUE superclass = rb_funcall(self, rb_intern("superclass"), 0);
769
- const RGObjClassInfo* cinfo = rbgobj_lookup_class(superclass);
763
+ VALUE superclass = rb_funcall(klass, rb_intern("superclass"), 0);
764
+ const RGObjClassInfo *cinfo = rbgobj_lookup_class(superclass);
770
765
  if (cinfo->klass != superclass)
771
- rb_raise(rb_eTypeError, "super class must be registered to GLib");
766
+ rb_raise(rb_eTypeError,
767
+ "super class must be registered to GLib: <%s>",
768
+ RBG_INSPECT(superclass));
772
769
  parent_type = cinfo->gtype;
773
770
  }
774
771
 
775
772
  if (NIL_P(type_name)) {
776
- VALUE s = rb_funcall(self, rb_intern("name"), 0);
773
+ VALUE klass_name = rb_funcall(klass, rb_intern("name"), 0);
777
774
 
778
- if (strlen(StringValuePtr(s)) == 0)
779
- rb_raise(rb_eTypeError, "can't determine type name");
775
+ if (strlen(StringValueCStr(klass_name)) == 0)
776
+ rb_raise(rb_eTypeError,
777
+ "can't determine type name: <%s>",
778
+ RBG_INSPECT(klass));
780
779
 
781
- type_name = rb_funcall(
782
- rb_eval_string("lambda{|x| x.gsub(/::/,'') }"),
783
- rb_intern("call"), 1, s);
780
+ type_name = rb_funcall(klass_name, rb_intern("gsub"),
781
+ 2,
782
+ rb_str_new_cstr("::"),
783
+ rb_str_new_cstr(""));
784
784
  }
785
785
 
786
786
  {
@@ -793,9 +793,9 @@ rg_s_type_register(int argc, VALUE* argv, VALUE self)
793
793
  info->class_size = query.class_size;
794
794
  info->base_init = NULL;
795
795
  info->base_finalize = NULL;
796
- info->class_init = class_init_func;
796
+ info->class_init = class_init;
797
797
  info->class_finalize = NULL;
798
- info->class_data = (gpointer)class_init_proc;
798
+ info->class_data = NULL;
799
799
  info->instance_size = query.instance_size;
800
800
  info->n_preallocs = 0;
801
801
  info->instance_init = NULL;
@@ -804,37 +804,49 @@ rg_s_type_register(int argc, VALUE* argv, VALUE self)
804
804
 
805
805
  {
806
806
  GType type = g_type_register_static(parent_type,
807
- StringValuePtr(type_name),
807
+ StringValueCStr(type_name),
808
808
  info,
809
- NIL_P(flags) ? 0 : NUM2INT(flags));
810
- G_RELATIVE(self, class_init_proc);
809
+ 0);
811
810
 
812
- rbgobj_register_class(self, type, TRUE, TRUE);
811
+ rbgobj_register_class(klass, type, TRUE, TRUE);
813
812
 
814
813
  {
815
- RGObjClassInfo* cinfo = (RGObjClassInfo*)rbgobj_lookup_class(self);
814
+ RGObjClassInfo *cinfo = (RGObjClassInfo *)rbgobj_lookup_class(klass);
816
815
  cinfo->flags |= RBGOBJ_DEFINED_BY_RUBY;
817
816
  }
818
817
 
819
818
  {
820
819
  GType parent = g_type_parent(type);
821
- const RGObjClassInfo* cinfo = GTYPE2CINFO(parent);
822
- VALUE m = rb_define_module_under(self, RubyGObjectHookModule);
823
-
824
- if (! (cinfo->flags & RBGOBJ_DEFINED_BY_RUBY)) {
825
- rbg_define_method(m, "initialize", rg_initialize, -1);
820
+ const RGObjClassInfo *cinfo = GTYPE2CINFO(parent);
821
+ VALUE initialize_module;
822
+
823
+ initialize_module = rb_define_module_under(klass,
824
+ RubyGObjectHookModule);
825
+ if (!(cinfo->flags & RBGOBJ_DEFINED_BY_RUBY)) {
826
+ rbg_define_method(initialize_module,
827
+ "initialize", rg_initialize, -1);
826
828
  }
827
829
 
828
- rb_include_module(self, m);
830
+ rb_include_module(klass, initialize_module);
829
831
  }
830
-
831
- return Qnil;
832
832
  }
833
833
  }
834
834
 
835
+ static VALUE
836
+ rg_s_type_register(int argc, VALUE *argv, VALUE self)
837
+ {
838
+ VALUE type_name;
839
+
840
+ rb_scan_args(argc, argv, "01", &type_name);
841
+
842
+ rbgobj_register_type(self, type_name, rbgobj_class_init_func);
843
+
844
+ return Qnil;
845
+ }
846
+
835
847
  /**********************************************************************/
836
848
 
837
- void
849
+ void
838
850
  Init_gobject_gobject(void)
839
851
  {
840
852
  RG_TARGET_NAMESPACE = G_DEF_CLASS_WITH_GC_FUNC(G_TYPE_OBJECT, "Object", mGLib,
@@ -869,6 +881,11 @@ Init_gobject_gobject(void)
869
881
  RG_DEF_METHOD(inspect, 0);
870
882
  RG_DEF_METHOD(type_name, 0);
871
883
 
884
+ #if GLIB_CHECK_VERSION(2, 26, 0)
885
+ RG_DEF_METHOD(bind_property, 4);
886
+ G_DEF_CLASS(G_TYPE_BINDING_FLAGS, "BindingFlags", mGLib);
887
+ #endif
888
+
872
889
  eNoPropertyError = rb_define_class_under(mGLib, "NoPropertyError",
873
890
  rb_eNameError);
874
891
 
@@ -879,7 +896,4 @@ Init_gobject_gobject(void)
879
896
 
880
897
  /* subclass */
881
898
  RG_DEF_SMETHOD(type_register, -1);
882
-
883
- rb_global_variable(&proc_mod_eval);
884
- proc_mod_eval = rb_eval_string("lambda{|obj,proc| obj.module_eval(&proc)}");
885
899
  }