cocoa 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,6 @@
1
- class Cocoa::NSObject
2
- def self.const_missing name
3
- Cocoa.const_get name
4
- end
5
- end
6
-
7
1
  class Cocoa::NSString < Cocoa::NSObject
8
2
  def to_s
9
- Cocoa::NSString_to_String(object)
3
+ ObjC.NSString_to_String(object)
10
4
  end
11
5
  end
12
6
 
@@ -14,10 +8,66 @@ class String
14
8
  def method_missing meth,*args
15
9
  if Cocoa::NSString.method_defined?(meth)
16
10
  str = Cocoa::NSString.new(true)
17
- str.object = Cocoa::String_to_NSString(self)
11
+ str.object = ObjC.String_to_NSString(self)
18
12
  str.send(meth,*args)
19
13
  else
20
14
  super
21
15
  end
22
16
  end
23
17
  end
18
+
19
+ class Cocoa::NSObject
20
+ def self.const_missing name
21
+ Cocoa.const_get name
22
+ end
23
+
24
+ def self.alloc
25
+ new(true).alloc
26
+ end
27
+
28
+ def self.inherited(parent)
29
+ include ClassMethods
30
+ if parent.name
31
+ klass = ObjC.objc_allocateClassPair(ObjC.objc_getClass(name.split('::').last),parent.name,0)
32
+ ObjC.objc_registerClassPair(klass)
33
+ end
34
+ end
35
+
36
+ def initialize allocated=false
37
+ @klass = ObjC.objc_getClass(self.native_name)
38
+ unless allocated
39
+ self.object = @klass
40
+ new
41
+ end
42
+ end
43
+
44
+ def get_class
45
+ ObjC.objc_getClass(self.native_name)
46
+ end
47
+
48
+ def alloc
49
+ self.object = ObjC.msgSend(ObjC.objc_getClass(self.native_name),"alloc")
50
+ self
51
+ end
52
+
53
+ def init
54
+ self.object = ObjC.msgSend(@object,"init")
55
+ self
56
+ end
57
+
58
+ def new
59
+ self.object = ObjC.msgSend(@object,"new")
60
+ self
61
+ end
62
+
63
+ def autorelease
64
+ self.object = ObjC.msgSend(@object,"autorelease")
65
+ self
66
+ end
67
+
68
+ module ClassMethods
69
+ def native_name
70
+ self.class.name.split('::').last
71
+ end
72
+ end
73
+ end
data/lib/cocoa/helpers.rb CHANGED
@@ -7,22 +7,7 @@ module Cocoa; end
7
7
  require 'cocoa/structs/NSPoint'
8
8
  require 'cocoa/structs/NSSize'
9
9
  require 'cocoa/structs/NSRect'
10
-
11
- class Cocoa::CFRange < FFI::Struct
12
- def initialize *args
13
- options = args.first
14
- if options.is_a? Hash
15
- self[:location] = options[:location]
16
- self[:length] = options[:length]
17
- else
18
- super *args
19
- end
20
- end
21
- def to_s
22
- "<CFRange: #{self[:location]} #{self[:length]}>"
23
- end
24
- layout :location, :long_long, :length, :long_long
25
- end
10
+ require 'cocoa/structs/NSRange'
26
11
 
27
12
  module Cocoa
28
13
 
@@ -31,76 +16,20 @@ module Cocoa
31
16
  end
32
17
  def instance_for data
33
18
  if data.is_a?(FFI::Pointer)
34
- @@instances[data.address] ||= translate_retval nil,data,'@'
19
+ @@instances[data.address] ||= ObjC.translate_retval nil,data,'@'
35
20
  else
36
21
  data
37
22
  end
38
23
  end
39
24
 
40
- def NSMakeSize width,height
41
- CGSize.new(width: width, height: height)
42
- end
43
- def NSMakePoint x,y
44
- CGPoint.new(x: x, y: y)
45
- end
46
- def CGRectMake x,y,width,height
47
- CGRect.new(x: x, y: y, width: width, height: height)
48
- end
49
- NSZeroPoint = CGPoint.new(x: 0, y: 0)
50
- NSZeroRect = CGRect.new(x: 0, y: 0, width: 0, height: 0)
51
-
52
- def CFRangeMake loc,len
53
- CFRange.new(location: loc, length: len)
54
- end
55
-
56
- def self.String_to_NSString( string )
57
- nsstring_class = ObjC.objc_getClass("NSString")
58
- ObjC.msgSend( nsstring_class, "stringWithUTF8String:",
59
- :string, string )
60
- end
61
-
62
- def self.NSString_to_String( nsstring_pointer )
63
- c_string_pointer = ObjC.msgSend( nsstring_pointer, "UTF8String" )
64
- if c_string_pointer.null?
65
- return "(NULL)"
66
- else
67
- return c_string_pointer.read_string()
68
- end
69
- end
70
-
71
- def self.fixed_args args,options={}
72
- ([args.first]+options.values).map do |arg|
73
- case arg
74
- when TrueClass
75
- [:bool,arg]
76
- when FalseClass
77
- [:bool,arg]
78
- when Fixnum
79
- [:int,arg]
80
- when String
81
- [:pointer,Cocoa.String_to_NSString(arg)]
82
- when NilClass
83
- [:pointer,nil]
84
- when Symbol
85
- [:pointer,ObjC.sel_registerName("#{arg}:")]
86
- when NSObject
87
- [:pointer,arg.object]
88
- when FFI::Struct
89
- [arg.class.by_value,arg]
90
- else
91
- raise ArgumentError
92
- end
93
- end.flatten
94
- end
95
-
96
25
  def self.attach_nsstring_getter method
97
26
  raw = "get_objc_#{method}".to_sym
98
27
  attach_variable raw, method, :pointer
99
28
  if /[A-Z]/ =~ method.to_s[0]
100
- Cocoa.const_set(method,Cocoa::NSString_to_String(send(raw)))
29
+ Cocoa.const_set(method,ObjC.NSString_to_String(send(raw)))
101
30
  else
102
31
  define_singleton_method method do
103
- Cocoa::NSString_to_String(send(raw))
32
+ ObjC.NSString_to_String(send(raw))
104
33
  end
105
34
  end
106
35
  end
@@ -121,150 +50,6 @@ module Cocoa
121
50
  end
122
51
  end
123
52
 
124
- def self.apple_type_to_ffi type
125
- # TODO: These are just stubbed on guess - check'em'all
126
- case type
127
- when 'C'
128
- :uchar
129
- when '@'
130
- :pointer
131
- when '#'
132
- :pointer
133
- when 'Q'
134
- :int
135
- when ':'
136
- :pointer
137
- when 'I'
138
- :int
139
- when 'L'
140
- :int
141
- when 'd'
142
- :double
143
- when 'f'
144
- :float
145
- when 'i'
146
- :int
147
- when 's'
148
- :int
149
- when 'q'
150
- :long_long
151
- when 'S'
152
- :int
153
- when /^\^/
154
- :pointer
155
- when 'B'
156
- :bool
157
- when 'v'
158
- :void
159
- when '[5*]'
160
- :void
161
- when /^{[^=]*=.*}$/
162
- begin
163
- /^{_*([^=]*)=.*}$/.match(type)[1].constantize.by_value
164
- rescue => e
165
- begin
166
- "Cocoa::#{/^{_*([^=]*)=.*}$/.match(type)[1]}".constantize.by_value
167
- rescue => e
168
- match = /^{_*([^=]*)=(.*)}$/.match(type)
169
- klass = begin
170
- Cocoa.const_get(match[1])
171
- rescue
172
- # puts "defining struct Cocoa::#{match[1]} as #{match[2]}"
173
- # this stuff doesnt work with jruby
174
- klass = Class.new(FFI::Struct)
175
- Cocoa.const_set(match[1], klass)
176
- name = 'a'
177
- layout = []
178
- match[2].each_char do |c|
179
- case c
180
- when 'd'
181
- layout << name.to_sym
182
- name = name.next
183
- layout << :double
184
- end
185
- end
186
- klass = "Cocoa::#{match[1]}".constantize
187
- klass.layout *layout
188
- klass
189
- end
190
- klass.by_ref
191
- end
192
- end
193
- when nil
194
- :void
195
- when '*' # character string
196
- :pointer
197
- when '@?'
198
- :pointer
199
- else
200
- raise type.inspect
201
- end
202
- end
203
-
204
- def self.translate_retval method,ret,type
205
- case type
206
- when '@'
207
- return nil if ret.address == 0
208
- return ret if method == :NSStringFromClass
209
- klass_name = Cocoa::NSString_to_String(Cocoa::NSStringFromClass(ObjC.msgSend(ret,"class")))
210
- return self if klass_name == '(NULL)'
211
- instance = begin
212
- ("Cocoa::"+klass_name).constantize.new(true)
213
- rescue
214
- klass_name = if klass_name =~ /^__NSCF/
215
- "NS#{klass_name[6..-1]}"
216
- elsif klass_name[0]=='_'
217
- "FIX_#{klass_name}"
218
- else
219
- klass_name
220
- end
221
- klass = begin
222
- Cocoa.const_get(klass_name)
223
- rescue => e
224
- superclass_name = Cocoa::NSString_to_String(Cocoa::NSStringFromClass(ObjC.msgSend(ret,'superclass')))
225
- superclass = "Cocoa::#{superclass_name}".constantize
226
- proxy = Class.new(superclass)
227
- Cocoa.const_set(klass_name, proxy)
228
- klass = ("Cocoa::"+klass_name).constantize
229
- superclass.inherited(klass)
230
- klass
231
- end
232
- klass.new(true)
233
- end
234
- instance.object = ret
235
- instance
236
- when '#'
237
- ret
238
- when /^{([^=]*)=.*}$/
239
- ret
240
- when /^\^{[^=]*=.*}$/
241
- return nil if ret.address == 0
242
- klass_name = Cocoa::NSString_to_String(Cocoa::NSStringFromClass(ObjC.msgSend(ret,"class")))
243
- return self if klass_name == '(NULL)'
244
- instance = begin
245
- ("Cocoa::"+klass_name).constantize.new(true)
246
- rescue
247
- klass_name = "FIX_#{klass_name}" if klass_name[0]=='_'
248
- klass = begin
249
- Cocoa.const_get(klass_name)
250
- rescue => e
251
- superclass_name = Cocoa::NSString_to_String(Cocoa::NSStringFromClass(ObjC.msgSend(ret,'superclass')))
252
- superclass = "Cocoa::#{superclass_name}".constantize
253
- proxy = Class.new(superclass)
254
- Cocoa.const_set(klass_name, proxy)
255
- klass = ("Cocoa::"+klass_name).constantize
256
- superclass.inherited(klass)
257
- klass
258
- end
259
- klass.new(true)
260
- end
261
- instance.object = ret
262
- instance
263
- else
264
- raise type
265
- end
266
- end
267
-
268
53
  def self.attach_method method,*params
269
54
  if params.first.is_a? Array
270
55
  params = params.last
@@ -272,9 +57,9 @@ module Cocoa
272
57
 
273
58
  params = params.extract_options!
274
59
  params.freeze
275
- if params[:args] == 0
276
- begin
277
- attach_function "call_#{method}".to_sym, method, [], apple_type_to_ffi(params[:retval])
60
+ begin
61
+ if params[:args] == 0
62
+ attach_function "call_#{method}".to_sym, method, [], ObjC.apple_type_to_ffi(params[:retval])
278
63
  define_method method do
279
64
  case params[:retval]
280
65
  when /^\^{[^=]*=.*}$/
@@ -283,172 +68,27 @@ module Cocoa
283
68
  raise params.inspect
284
69
  end
285
70
  end
286
- rescue FFI::NotFoundError => e
287
- puts "method missing: #{e.message}"
288
- end
289
- else
290
- begin
291
- attach_function "call_#{method}".to_sym, method, params[:types].map{ |arg| apple_type_to_ffi(arg) }, apple_type_to_ffi(params[:retval])
71
+ else
72
+ attach_function "call_#{method}".to_sym, method, params[:types].map{ |arg| ObjC.apple_type_to_ffi(arg) }, ObjC.apple_type_to_ffi(params[:retval])
292
73
  define_method method do |*args|
293
74
  raise ArgumentError.new("#{method} requires #{params[:args]} argument(s)") unless params[:args] == args.size
294
- fixed_args = []
295
- args.each_with_index do |arg,i|
296
- case params[:types][i]
297
- when '@'
298
- fixed_args << arg
299
- when 'd'
300
- if arg.is_a?(Fixnum)
301
- fixed_args << arg.to_f
302
- else
303
- raise ArgumentError.new("float expected, got #{arg.class.name}") unless arg.is_a?(Float)
304
- fixed_args << arg
305
- end
306
- when 'I'
307
- raise ArgumentError unless arg.is_a?(Fixnum)
308
- fixed_args << arg
309
- when 'Q'
310
- raise ArgumentError.new(arg.inspect) unless arg.is_a?(Fixnum)
311
- fixed_args << arg
312
- when 'q'
313
- raise ArgumentError unless arg.is_a?(Fixnum)
314
- fixed_args << arg
315
- when '#'
316
- raise ArgumentError unless arg.is_a?(FFI::Pointer)
317
- fixed_args << arg
318
- when /^{[^=]*=.*}$/
319
- raise ArgumentError.new(arg.inspect) unless arg.kind_of?(FFI::Struct)
320
- fixed_args << arg
321
- when /^\^{([^=]*)=.*}$/
322
- case arg
323
- when FFI::Pointer
324
- fixed_args << arg
325
- when Array
326
- raise ArgumentError unless $1 == '__CFArray'
327
- fixed_args << NSArray.arrayWithObjects(arg).object
328
- else
329
- match = $1
330
- if arg.class.name =~ /^Cocoa::/ # "Cocoa::#{$1}".constantize
331
- fixed_args << arg.object
332
- elsif arg.is_a?(NilClass)
333
- fixed_args << FFI::MemoryPointer::NULL
334
- elsif arg.is_a?(String) && match == '__CFString'
335
- fixed_args << Cocoa::String_to_NSString(arg)
336
- else
337
- raise ArgumentError.new("expected #{params[:types][i]} got #{arg.class.name} (#{match})")
338
- end
339
- end
340
- when '^d'
341
- raise ArgumentError unless arg.is_a?(Array)
342
- arr = FFI::MemoryPointer.new(:double,arg.size)
343
- arr.write_array_of_double(arg)
344
- fixed_args << arr
345
- when '^v'
346
- raise ArgumentError unless arg.is_a?(NilClass)
347
- fixed_args << FFI::MemoryPointer::NULL
348
- else
349
- raise params[:types][i]
350
- end
351
- end
352
- ret = send("call_#{method}".to_sym,*fixed_args)
75
+ ret = send("call_#{method}".to_sym,*ObjC.call_arguments(params,args))
353
76
  if params[:retval]=='v'
354
77
  self
355
78
  else
356
- Cocoa::translate_retval(method,ret,params[:retval])
79
+ ObjC.translate_retval(method,ret,params[:retval])
357
80
  end
358
81
  end
359
- rescue FFI::NotFoundError => e
360
- # puts "!!!"
361
- # puts e.message
362
82
  end
83
+ rescue FFI::NotFoundError => e
363
84
  end
364
85
  end
365
86
  extend self
366
87
 
367
88
  class NSObject
368
-
369
- def self.inherited(parent)
370
- if parent.name
371
- klass = ObjC.objc_allocateClassPair(ObjC.objc_getClass(name.split('::').last),parent.name,0)
372
- ObjC.objc_registerClassPair(klass)
373
- end
374
- end
375
-
376
- def initialize allocated=false
377
- @klass = ObjC.objc_getClass(self.class.name.split('::').last)
378
- unless allocated
379
- self.object = @klass
380
- new
381
- end
382
- end
383
- def self.alloc
384
- new(true).alloc
385
- end
386
- def get_class
387
- ObjC.objc_getClass(self.class.name.split('::').last)
388
- end
389
- def alloc
390
- self.object = ObjC.msgSend(ObjC.objc_getClass(self.class.name.split('::').last),"alloc")
391
- self
392
- end
393
- def init
394
- self.object = ObjC.msgSend(@object,"init")
395
- self
396
- end
397
- def new
398
- self.object = ObjC.msgSend(@object,"new")
399
- self
400
- end
401
- def autorelease
402
- self.object = ObjC.msgSend(@object,"autorelease")
403
- self
404
- end
405
- # protected
406
89
  attr_accessor :klass
407
90
  attr_reader :object
408
91
 
409
- def self.fixed_args args,params={},types=[],options={}
410
- if options[:variadic]
411
- _types = (types.dup*args.size)
412
- args
413
- else
414
- _types = types.dup
415
- ([args.first]+params.values)
416
- end.map do |arg|
417
- type = _types.shift
418
- case arg
419
- when TrueClass, FalseClass
420
- [:bool,arg]
421
- when Fixnum, Bignum
422
- case type
423
- when 'q'
424
- [:long_long,arg]
425
- when 'Q'
426
- [:ulong_long,arg]
427
- when 'd'
428
- [:double,arg]
429
- else
430
- raise type.inspect
431
- end
432
- when Float
433
- [:double,arg]
434
- when String
435
- [:pointer,Cocoa.String_to_NSString(arg)]
436
- when NilClass
437
- [:pointer,nil]
438
- when Symbol
439
- [:pointer,ObjC.sel_registerName("#{arg}:")]
440
- when NSObject
441
- [:pointer,arg.object]
442
- when FFI::Struct
443
- [arg.class.by_value,arg]
444
- when FFI::Pointer
445
- [:pointer,arg]
446
- else
447
- raise ArgumentError.new("#{arg.class.name}: #{arg.inspect}")
448
- end
449
- end.flatten
450
- end
451
-
452
92
  def self.smart_constantize ret,klass_name
453
93
  begin
454
94
  Cocoa.const_get(klass_name)
@@ -501,17 +141,7 @@ module Cocoa
501
141
  case params[:retval]
502
142
  when '@'
503
143
  return nil if ret.address == 0
504
- klass_name = Cocoa::NSString_to_String(Cocoa::NSStringFromClass(ObjC.msgSend(ret,"class")))
505
- return self if klass_name == '(NULL)'
506
- instance = begin
507
- ("Cocoa::"+klass_name).constantize.new(true)
508
- rescue
509
- klass_name = klass_name.gsub(/^_*/,'') if klass_name[0]=='_'
510
- klass = smart_constantize(ret,klass_name)
511
- klass.new(true)
512
- end
513
- instance.object = ret
514
- instance
144
+ ObjC.object_to_instance(ret)
515
145
  when 'v'
516
146
  self
517
147
  else
@@ -535,7 +165,7 @@ module Cocoa
535
165
  @@method_specs[method] = params
536
166
 
537
167
  options = args.extract_options!
538
- fixed_args = NSObject.fixed_args(args,options,params[:types])
168
+ fixed_args = ObjC.fixed_args(args,options,params[:types])
539
169
  # puts "ObjC.msgSend(@object,\"#{method}:#{params[:names].join(':')}:\",#{fixed_args.inspect})"
540
170
  ObjC.msgSend(@object,"#{method}#{(['']+params[:names]).join(':')}:",*fixed_args)
541
171
  self
@@ -544,7 +174,7 @@ module Cocoa
544
174
  raise ArgumentError unless params.size == 1
545
175
  @@method_specs[method] = params
546
176
 
547
- fixed_args = NSObject.fixed_args(args)
177
+ fixed_args = ObjC.fixed_args(args)
548
178
  # puts "ObjC.msgSend(#{@object.inspect},#{method},#{fixed_args.inspect})"
549
179
  ObjC.msgSend(@object,"#{method}:",*fixed_args)
550
180
  self
@@ -558,13 +188,13 @@ module Cocoa
558
188
  define_method method do
559
189
  case params[:retval]
560
190
  when '@'
561
- Cocoa::translate_retval(method,ObjC.msgSend(@object,method.to_s),params[:retval])
191
+ ObjC.translate_retval(method,ObjC.msgSend(@object,method.to_s),params[:retval])
562
192
  when 'v'
563
193
  ObjC.msgSend(@object,method.to_s)
564
194
  self
565
195
  when '^v'
566
196
  ObjC.msgSend(@object,method.to_s)
567
- when 'Q'
197
+ when 'Q', 'q'
568
198
  ObjC.msgSend(@object,method.to_s).address
569
199
  when 'B'
570
200
  ret = ObjC.msgSend(@object,method.to_s)
@@ -573,30 +203,7 @@ module Cocoa
573
203
  ObjC.msgSend_stret($1.constantize,@object,method.to_s)
574
204
  when /^\^{([^=]*)=.*}$/
575
205
  ret = ObjC.msgSend(@object,method.to_s)
576
- klass_name = Cocoa::NSString_to_String(Cocoa::NSStringFromClass(ObjC.msgSend(ret,"class")))
577
- if klass_name == '__NSCFType'
578
- # dont know what the hell this is, experiment:
579
- klass_name = $1
580
- end
581
- instance = begin
582
- ("Cocoa::"+klass_name).constantize.new(true)
583
- rescue
584
- klass_name = "FIX_#{klass_name}" if klass_name[0]=='_'
585
- klass = begin
586
- Cocoa.const_get(klass_name)
587
- rescue => e
588
- superclass_name = Cocoa::NSString_to_String(Cocoa::NSStringFromClass(ObjC.msgSend(ret,'superclass')))
589
- superclass = "Cocoa::#{superclass_name}".constantize
590
- proxy = Class.new(superclass)
591
- Cocoa.const_set(klass_name, proxy)
592
- klass = ("Cocoa::"+klass_name).constantize
593
- superclass.inherited(klass)
594
- klass
595
- end
596
- klass.new(true)
597
- end
598
- instance.object = ret
599
- instance
206
+ ObjC.object_to_instance(ret)
600
207
  when '*'
601
208
  ObjC.msgSend(@object,method.to_s).read_string
602
209
  else
@@ -607,10 +214,10 @@ module Cocoa
607
214
  else
608
215
  define_method method do |*args|
609
216
  options = args.extract_options!
610
- fixed_args = NSObject.fixed_args(args,options,params[:types])
217
+ fixed_args = ObjC.fixed_args(args,options,params[:types])
611
218
  case params[:retval]
612
219
  when '@'
613
- Cocoa::translate_retval(method,ObjC.msgSend(@object,"#{method}#{(['']+params[:names]).join(':')}:",*fixed_args),params[:retval])
220
+ ObjC.translate_retval(method,ObjC.msgSend(@object,"#{method}#{(['']+params[:names]).join(':')}:",*fixed_args),params[:retval])
614
221
  else
615
222
  ObjC.msgSend(@object,"#{method}#{(['']+params[:names]).join(':')}:",*fixed_args)
616
223
  self
@@ -646,30 +253,17 @@ module Cocoa
646
253
  :void
647
254
  when 'v'
648
255
  :void
649
- else
650
- raise @@method_specs[method][:retval].inspect
651
- end
652
- end
653
-
654
- def self.objc_type type,default='i'
655
- case type
656
- when nil
657
- default
658
256
  when '@'
659
- type
660
- when 'v'
661
- type
662
- when /^{([^=]*)=.*}$/
663
- type
257
+ :pointer
664
258
  else
665
- raise type.inspect
259
+ raise @@method_specs[method][:retval].inspect
666
260
  end
667
261
  end
668
262
 
669
263
  def self.objc_argument_types method
670
264
  #"@:^{CGRect={CGPoint=dd}{CGSize=dd}}@"
671
265
  return 'v@:@' unless @@method_specs[method] # own method probably TODO: @v: + @ times argument count
672
- objc_type(@@method_specs[method][:retval],'v')+'@:'+@@method_specs[method][:types].map{ |type| objc_type(type) }.join
266
+ ObjC.objc_type(@@method_specs[method][:retval],'v')+'@:'+@@method_specs[method][:types].map{ |type| ObjC.objc_type(type) }.join
673
267
  end
674
268
 
675
269
  def self.method_added(name)