Class Subtlext::Geometry < Object

Class for various sizes

Methods

public class

  1. new

public instance

  1. ==
  2. eql?
  3. hash
  4. to_a
  5. to_a
  6. to_h
  7. to_hash
  8. to_s
  9. to_str

Attributes

height [RW] Geometry height
width [RW] Geometry width
x [RW] X offset
y [RW] Y offset

Public class methods

new(x, y, width, height) → Subtlext::Geometry
new(array) → Subtlext::Geometry
new(hash) → Subtlext::Geometry
new(string) → Subtlext::Geometry
new(geometry) → Subtlext::Geometry

Create a new Geometry object from givem value which can be of following types:

Array
Must be an array with values for x, y, width and height
Hash
Must be a hash with values for x, y, width and height
String
Must be a string of following format: XxY+WIDTH+HEIGHT
Geometry
Copy geometry from a Geometry object

Or just pass one argument for x, y, width and height.

Creating a geometry with zero width or height will raise a StandardError.

 1 geom = Subtlext::Geometry.new(0, 0, 50, 50)
 2 => #<Subtlext::Geometry:xxx>
 3 
 4 geom = Subtlext::Geometry.new([ 0, 0, 50, 50 ])
 5 => #<Subtlext::Geometry:xxx>
 6 
 7 geom = Subtlext::Geometry.new({ :x => 0, :y => 0, :width => 50, :height => 50 })
 8 => #<Subtlext::Geometry:xxx>
 9 
10 geom = Subtlext::Geometry.new("0x0+100+100")
11 => #<Subtlext::Geometry:xxx>
12 
13 geom = Subtlext::Geometry.new(Subtlext::Geometry.new(0, 0, 50, 50))
14 => #<Subtlext::Geometry:xxx>
[show source]
VALUE
subextGeometryInit(int argc,
  VALUE *argv,
  VALUE self)
{
  VALUE value = Qnil, data[4] = { Qnil };

  rb_scan_args(argc, argv, "13", &data[0], &data[1], &data[2], &data[3]);
  value = data[0];

  /* Check object type */
  switch(rb_type(value))
    {
      case T_FIXNUM: break;
      case T_ARRAY:
        if(4 == FIX2INT(rb_funcall(value, rb_intern("size"), 0, NULL)))
          {
            int i;

            for(i = 0; 4 > i; i++)
              data[i] = rb_ary_entry(value, i);
          }
        break;
      case T_HASH:
          {
            int i;
            const char *syms[] = { "x", "y", "width", "height" };

            for(i = 0; 4 > i; i++)
              data[i] = rb_hash_lookup(value, CHAR2SYM(syms[i]));
          }
        break;
      case T_STRING:
          {
            XRectangle geom = { 0 };

            sscanf(RSTRING_PTR(value), "%hdx%hd+%hu+%hu",
              &geom.x, &geom.y, &geom.width, &geom.height);

            /* Convert values */
            data[0] = INT2FIX(geom.x);
            data[1] = INT2FIX(geom.y);
            data[2] = INT2FIX(geom.width);
            data[3] = INT2FIX(geom.height);
          }
        break;
      case T_OBJECT:
          {
            VALUE klass = rb_const_get(mod, rb_intern("Geometry"));

            /* Check object instance */
            if(rb_obj_is_instance_of(value, klass))
              {
                data[0] = rb_iv_get(value, "@x");
                data[1] = rb_iv_get(value, "@y");
                data[2] = rb_iv_get(value, "@width");
                data[3] = rb_iv_get(value, "@height");
              }
          }
        break;
      default: rb_raise(rb_eArgError, "Unexpected value-type `%s'",
          rb_obj_classname(value));
    }

  /* Set values */
  if(FIXNUM_P(data[0]) && FIXNUM_P(data[1]) && FIXNUM_P(data[2]) &&
      FIXNUM_P(data[3]) && 0 < FIX2INT(data[2]) && 0 < FIX2INT(data[3]))
    {
      rb_iv_set(self, "@x",      data[0]);
      rb_iv_set(self, "@y",      data[1]);
      rb_iv_set(self, "@width",  data[2]);
      rb_iv_set(self, "@height", data[3]);
    }
  else rb_raise(rb_eStandardError, "Invalid geometry");

  return self;
}

Public instance methods

==(other) → True or False

Whether both objects have the same values (based on geometry).

1 object1 == object2
2 => true
[show source]
VALUE
subextGeometryEqual(VALUE self,
  VALUE other)
{
  return GeometryEqual(self, other);
}
eql?(other) → True or False

Whether both objects have the same values and types (based on geometry).

1 object1.eql? object2
2 => true
[show source]
VALUE
subextGeometryEqualTyped(VALUE self,
  VALUE other)
{
  return GeometryEqual(self, other);
}
hash → Hash

Convert this object to hash.

1 puts object.hash
2 => 1746246187916025425
[show source]
static VALUE
SubtlextHash(VALUE self)
{
  VALUE str = Qnil, id = rb_intern("to_str");

  /* Convert to string */
  if(rb_respond_to(self, id))
    str = rb_funcall(self, id, 0, Qnil);

  return T_STRING == rb_type(str) ? INT2FIX(rb_str_hash(str)) : Qnil;
}
to_a ()

Alias for to_ary

to_a → Array

Convert this Geometry object to an array with one fixnum for x, y, width and height.

1 geom.to_a
2 => [0, 0, 50, 50]
[show source]
VALUE
subextGeometryToArray(VALUE self)
{
  VALUE ary = Qnil, x = Qnil, y = Qnil, width = Qnil, height = Qnil;

  /* Check ruby object */
  GET_ATTR(self, "@x",      x);
  GET_ATTR(self, "@y",      y);
  GET_ATTR(self, "@width",  width);
  GET_ATTR(self, "@height", height);

  /* Create new array */
  ary = rb_ary_new2(4);

  /* Set values */
  rb_ary_push(ary, x);
  rb_ary_push(ary, y);
  rb_ary_push(ary, width);
  rb_ary_push(ary, height);

  return ary;
}
to_h ()

Alias for to_hash

to_hash → Hash

Convert this Geometry object to hash with one symbol/fixnum pair for x, y, height and width.

1 geom.to_hash
2 => { :x => 0, :y => 0, :width => 50, :height => 50 }
[show source]
VALUE
subextGeometryToHash(VALUE self)
{
  VALUE klass = Qnil, hash = Qnil;
  VALUE x = Qnil, y = Qnil, width = Qnil, height = Qnil;

  /* Check ruby object */
  GET_ATTR(self, "@x",      x);
  GET_ATTR(self, "@y",      y);
  GET_ATTR(self, "@width",  width);
  GET_ATTR(self, "@height", height);

  /* Create new hash */
  klass = rb_const_get(rb_mKernel, rb_intern("Hash"));
  hash  = rb_funcall(klass, rb_intern("new"), 0, NULL);

  /* Set values */
  rb_hash_aset(hash, CHAR2SYM("x"),      x);
  rb_hash_aset(hash, CHAR2SYM("y"),      y);
  rb_hash_aset(hash, CHAR2SYM("width"),  width);
  rb_hash_aset(hash, CHAR2SYM("height"), height);

  return hash;
}
to_s ()

Alias for to_str

to_str → String

Convert this Geometry object to string.

1 puts geom
2 => "0x0+50+50"
[show source]
VALUE
subextGeometryToString(VALUE self)
{
  char buf[256] = { 0 };
  VALUE x = Qnil, y = Qnil, width = Qnil, height = Qnil;

  /* Check ruby object */
  GET_ATTR(self, "@x",      x);
  GET_ATTR(self, "@y",      y);
  GET_ATTR(self, "@width",  width);
  GET_ATTR(self, "@height", height);

  snprintf(buf, sizeof(buf), "%dx%d+%d+%d", (int)FIX2INT(x),
    (int)FIX2INT(y), (int)FIX2INT(width), (int)FIX2INT(height));

  return rb_str_new2(buf);
}