1# Copyright (c) 2006-2008, The RubyCocoa Project. 2# Copyright (c) 2001-2006, FUJIMOTO Hisakuni. 3# All Rights Reserved. 4# 5# RubyCocoa is free software, covered under either the Ruby's license or the 6# LGPL. See the COPYRIGHT file for more information. 7 8# Same as oc_types but AppKit-specific. 9 10class OSX::NSRange 11 class << self 12 alias_method :orig_new, :new 13 def new(*args) 14 location, length = case args.size 15 when 0 16 [0, 0] 17 when 1 18 if args.first.is_a?(Range) 19 range = args.first 20 [range.first, range.last - range.first + (range.exclude_end? ? 0 : 1)] 21 else 22 raise ArgumentError, "wrong type of argument #1 (expected Range, got #{args.first.class})" 23 end 24 when 2 25 if args.first.is_a?(Range) 26 range, count = args 27 first = range.first 28 first += count if first < 0 29 last = range.last 30 last += count if last < 0 31 len = last - first + (range.exclude_end? ? 0 : 1) 32 len = count - first if count < first + len 33 len = 0 if len < 0 34 [first, len] 35 else 36 [args[0], args[1]] 37 end 38 else 39 raise ArgumentError, "wrong number of arguments (#{args.size} for either 0, 1 or 2)" 40 end 41 orig_new(location, length) 42 end 43 end 44 def to_range 45 Range.new(location, location + length, true) 46 end 47 def size; length; end 48 def size=(v); length = v; end 49 def contain?(arg) 50 case arg 51 when OSX::NSRange 52 location <= arg.location and arg.location + arg.length <= location + length 53 when Numeric 54 OSX::NSLocationInRange(arg, self) 55 else 56 raise ArgumentError, "argument should be NSRange or Numeric" 57 end 58 end 59 def empty?; length == 0 || not_found?; end 60 def intersect?(range); !intersection(range).empty?; end 61 def intersection(range); OSX::NSIntersectionRange(self, range); end 62 def union(range); OSX::NSUnionRange(self, range); end 63 def max; location + length; end 64 def not_found?; location == OSX::NSNotFound; end 65 def inspect; "#<#{self.class} location=#{location}, length=#{length}>"; end 66end 67