[macruby-changes] [316] MacRuby/trunk/lib

source_changes at macosforge.org source_changes at macosforge.org
Mon Jul 7 18:21:39 PDT 2008


Revision: 316
          http://trac.macosforge.org/projects/ruby/changeset/316
Author:   rich at infoether.com
Date:     2008-07-07 18:21:38 -0700 (Mon, 07 Jul 2008)
Log Message:
-----------
added hotcocoa

Added Paths:
-----------
    MacRuby/trunk/lib/hotcocoa/
    MacRuby/trunk/lib/hotcocoa/combo_box_data_source.rb
    MacRuby/trunk/lib/hotcocoa/delegate_builder.rb
    MacRuby/trunk/lib/hotcocoa/kernel_ext.rb
    MacRuby/trunk/lib/hotcocoa/mapper.rb
    MacRuby/trunk/lib/hotcocoa/mapping_methods.rb
    MacRuby/trunk/lib/hotcocoa/mappings/
    MacRuby/trunk/lib/hotcocoa/mappings/box.rb
    MacRuby/trunk/lib/hotcocoa/mappings/button.rb
    MacRuby/trunk/lib/hotcocoa/mappings/color.rb
    MacRuby/trunk/lib/hotcocoa/mappings/column.rb
    MacRuby/trunk/lib/hotcocoa/mappings/combo_box.rb
    MacRuby/trunk/lib/hotcocoa/mappings/control.rb
    MacRuby/trunk/lib/hotcocoa/mappings/font.rb
    MacRuby/trunk/lib/hotcocoa/mappings/image.rb
    MacRuby/trunk/lib/hotcocoa/mappings/image_view.rb
    MacRuby/trunk/lib/hotcocoa/mappings/label.rb
    MacRuby/trunk/lib/hotcocoa/mappings/movie.rb
    MacRuby/trunk/lib/hotcocoa/mappings/movie_view.rb
    MacRuby/trunk/lib/hotcocoa/mappings/popup.rb
    MacRuby/trunk/lib/hotcocoa/mappings/scroll_view.rb
    MacRuby/trunk/lib/hotcocoa/mappings/secure_text_field.rb
    MacRuby/trunk/lib/hotcocoa/mappings/segmented_control.rb
    MacRuby/trunk/lib/hotcocoa/mappings/sound.rb
    MacRuby/trunk/lib/hotcocoa/mappings/split_view.rb
    MacRuby/trunk/lib/hotcocoa/mappings/table_view.rb
    MacRuby/trunk/lib/hotcocoa/mappings/text_field.rb
    MacRuby/trunk/lib/hotcocoa/mappings/view.rb
    MacRuby/trunk/lib/hotcocoa/mappings/web_view.rb
    MacRuby/trunk/lib/hotcocoa/mappings/window.rb
    MacRuby/trunk/lib/hotcocoa/mappings.rb
    MacRuby/trunk/lib/hotcocoa/table_data_source.rb
    MacRuby/trunk/lib/hotcocoa.rb

Added: MacRuby/trunk/lib/hotcocoa/combo_box_data_source.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/combo_box_data_source.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/combo_box_data_source.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,44 @@
+module HotCocoa
+  class ComboBoxDataSource
+    attr_reader :data
+
+    def initialize(data)
+      @data = data
+    end
+    
+    def comboBox(combo_box, completedString:string)
+      data.length.times do |index|
+        value = string_value_of_index(index)
+        return value if value.start_with?(string)
+      end
+      nil
+    end
+    
+    def comboBox(combo_box, indexOfItemWithStringValue:string)
+      data.length.times do |index|
+        return index if string_value_of_index(index) == string
+      end
+      NSNotFound
+    end
+    
+    def comboBox(combo_box, objectValueForItemAtIndex:index)
+      string_value_of_index(index)
+    end
+    
+    def numberOfItemsInComboBox(combo_box)
+      data.length
+    end
+    
+    private
+    
+      def string_value_of_index(i)
+        item = data[i]
+        if item.kind_of?(Hash)
+          item.values.first
+        else
+          item.to_str
+        end
+      end
+
+  end
+end
\ No newline at end of file

Added: MacRuby/trunk/lib/hotcocoa/delegate_builder.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/delegate_builder.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/delegate_builder.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,77 @@
+module HotCocoa
+    
+  class DelegateBuilder
+    
+    attr_reader :control, :delegate, :method_count
+    
+    def initialize(control)
+      @control = control
+      @method_count = 0
+      @delegate = Object.new
+    end
+    
+    def add_delegated_method(block, selector_name, *parameters)
+      clear_delegate
+      increment_method_count
+      bind_block_to_delegate_instance_variable(block)
+      create_delegate_method(selector_name, parameters)
+      set_delegate
+    end
+    
+    private 
+    
+      def increment_method_count
+        @method_count += 1
+      end
+      
+      def bind_block_to_delegate_instance_variable(block)
+        delegate.instance_variable_set(block_instance_variable, block)
+      end
+      
+      def create_delegate_method(selector_name, parameters)
+        eval %{
+          def delegate.#{parameterize_selector_name(selector_name)}
+            #{block_instance_variable}.call(#{parameter_values_for_mapping(selector_name, parameters)})
+          end
+        }
+      end
+      
+      def clear_delegate
+        control.setDelegate(nil)
+      end
+      
+      def set_delegate
+        control.setDelegate(delegate)
+      end
+      
+      def block_instance_variable
+        "@block#{method_count}"
+      end
+    
+      def parameterize_selector_name(selector_name)
+        return selector_name unless selector_name.include?(":")
+        params = selector_name.split(":")
+        result = "#{params.shift}(p1"
+        params.each_with_index do |param, i|
+          result << ", #{param}:p#{i+2}"
+        end
+        result + ")"
+      end
+      
+      def parameter_values_for_mapping(selector_name, parameters)
+        return if parameters.empty?
+        result = []
+        selector_params = selector_name.split(":")
+        parameters.each do |parameter|
+          if (dot = parameter.index("."))
+            result << "p#{selector_params.index(parameter[0...dot])+1}#{parameter[dot..-1]}"
+          else
+            result << "p#{selector_params.index(parameter)+1}"
+          end
+        end
+        result.join(", ")
+      end
+    
+  end
+  
+end
\ No newline at end of file

Added: MacRuby/trunk/lib/hotcocoa/kernel_ext.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/kernel_ext.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/kernel_ext.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,14 @@
+module Kernel
+  
+  alias_method :default_framework, :framework
+  
+  def framework(name)
+    if default_framework(name)
+      HotCocoa::Mappings.framework_loaded(name)
+      true
+    else
+      false
+    end
+  end
+  
+end
\ No newline at end of file

Added: MacRuby/trunk/lib/hotcocoa/mapper.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mapper.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mapper.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,111 @@
+module HotCocoa::Mappings
+  
+  class Mapper
+    
+    attr_reader :control_class, :builder_method, :control_module
+
+    def initialize(builder_method, control_class, &block)
+      @control_class = control_class
+      @builder_method = builder_method
+      mod = (class << self; self; end)
+      mod.extend MappingMethods
+      mod.module_eval &block
+      @control_module = mod
+      inst = self
+      HotCocoa.send(:define_method, builder_method) do |*args, &control_block|
+        map = args.length == 1 ? args[0] : args[1]
+        guid = args.length == 1 ? nil : args[0]
+        map = inst.remap_constants(map)
+        default_empty_rect_used = (map[:frame].__id__ == DefaultEmptyRect.__id__)
+        control = inst.respond_to?(:init_with_options) ? inst.init_with_options(control_class.alloc, map) : inst.alloc_with_options(map)
+        Views[guid] = control if guid
+        inst.customize(control)
+        map.each do |key, value|
+          if control.respond_to?(key) && value == true
+            control.send("#{key}")
+          else
+            eval "control.#{key}= value"
+          end
+        end
+        if default_empty_rect_used
+          control.sizeToFit
+        end
+        control_block.call(control) if control_block
+        control
+      end
+    end
+    
+    def inherited_constants
+      constants = {}
+      each_control_ancestor do |ancestor|
+        constants = constants.merge(ancestor.control_module.constants_map)
+      end
+      constants
+    end
+    
+    def inherited_delegate_methods
+      delegate_methods = {}
+      each_control_ancestor do |ancestor|
+        delegate_methods = delegate_methods.merge(ancestor.control_module.delegate_map)
+      end
+      delegate_methods
+    end
+    
+    def inherited_custom_methods
+      methods = []
+      each_control_ancestor do |ancestor|
+        methods << ancestor.control_module.custom_methods if ancestor.control_module.custom_methods
+      end
+      methods
+    end
+    
+    def each_control_ancestor
+      control_class.ancestors.each do |ancestor|
+        Mappings.mappings.values.each do |mapper|
+          yield mapper if mapper.control_class == ancestor
+        end
+      end
+    end
+    
+    def customize(control)
+      inherited_custom_methods.each do |custom_methods|
+        control.extend(custom_methods)
+      end
+      decorate_with_delegate_methods(control)
+    end
+
+    def decorate_with_delegate_methods(control)
+      delegate_module = Module.new
+      inherited_delegate_methods.each do |delegate_method, mapping|
+        parameters = mapping[:parameters] ? ", "+mapping[:parameters].map {|param| %{"#{param}"} }.join(",") : ""
+        delegate_module.module_eval %{
+          def #{mapping[:to]}(&block)
+            raise "Must pass in a block to use this delegate method" unless block_given?
+            @_delegate_builder ||= HotCocoa::DelegateBuilder.new(self)
+            @_delegate_builder.add_delegated_method(block, "#{delegate_method}" #{parameters})
+          end
+        }
+      end
+      control.extend(delegate_module)
+    end
+
+    def remap_constants(tags)
+      constants = inherited_constants
+      if control_module.defaults
+        control_module.defaults.each do |key, value| 
+          tags[key] = value unless tags.has_key?(key)
+        end
+      end
+      result = {}
+      tags.each do |tag, value|
+        if constants[tag]
+          result[tag] = value.kind_of?(Array) ? value.inject(0) {|a, i| a|constants[tag][i]} : constants[tag][value]
+        else
+          result[tag] = value
+        end
+      end
+      result
+    end
+
+  end
+end
\ No newline at end of file

Added: MacRuby/trunk/lib/hotcocoa/mapping_methods.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mapping_methods.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mapping_methods.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,40 @@
+module HotCocoa
+  
+  module MappingMethods
+    
+    def defaults(defaults=nil)
+      if defaults
+        @defaults = defaults
+      else
+        @defaults
+      end
+    end
+    
+    def constant(name, constants)
+      constants_map[name] = constants
+    end
+    
+    def constants_map
+      @constants_map ||= {}
+    end
+    
+    def custom_methods(&block)
+      if block
+        @custom_methods = Module.new
+        @custom_methods.module_eval(&block)
+      else
+        @custom_methods
+      end
+    end
+    
+    def delegating(name, options)
+      delegate_map[name] = options
+    end
+    
+    def delegate_map
+      @delegate_map ||= {}
+    end
+    
+  end
+  
+end
\ No newline at end of file

Added: MacRuby/trunk/lib/hotcocoa/mappings/box.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/box.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/box.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,55 @@
+HotCocoa::Mappings.map :box => :NSBox do
+  
+  defaults :frame => DefaultEmptyRect
+  
+  constant :title_position, {
+    :none           => NSNoTitle,
+    :above_top      => NSAboveTop,
+    :top            => NSAtTop,
+    :below_top      => NSBelowTop,
+    :above_bottom   => NSAboveBottom,
+    :bottom         => NSAtBottom,
+    :below_bottom   => NSBelowBottom
+  }
+  
+  constant :type, {
+    :primary        => NSBoxPrimary,
+    :secondary      => NSBoxSecondary,
+    :separator      => NSBoxSeparator,
+    :old            => NSBoxOldStyle,
+    :custom         => NSBoxCustom
+  }
+  
+  def init_with_options(box, options)
+    box.initWithFrame options.delete(:frame)
+  end
+  
+  custom_methods do
+    
+    def <<(view)
+      addSubview(view)
+    end
+    
+    def title_position=(value)
+      setTitlePosition(value)
+    end
+    
+    def type=(value)
+      setBoxType(value)
+    end
+    
+    def corner_radius=(value)
+      setCornerRadius(value)
+    end
+
+    def border=(value)
+      setBorderType(value)
+    end
+    
+    def title_font=(value)
+      setTitleFont(value)
+    end
+    
+  end
+  
+end

Added: MacRuby/trunk/lib/hotcocoa/mappings/button.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/button.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/button.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,72 @@
+include HotCocoa
+
+Mappings.map :button => :NSButton do
+  
+  defaults :bezel => :rounded, 
+           :frame => DefaultEmptyRect
+  
+  constant :bezel, {
+     :rounded             => NSRoundedBezelStyle,
+     :regular_square      => NSRegularSquareBezelStyle,
+     :thick_square        => NSThickSquareBezelStyle,
+     :thicker_square      => NSThickerSquareBezelStyle,
+     :disclosure          => NSDisclosureBezelStyle,
+     :shadowless_square   => NSShadowlessSquareBezelStyle,
+     :circular            => NSCircularBezelStyle,
+     :textured_square     => NSTexturedSquareBezelStyle,
+     :help_button         => NSHelpButtonBezelStyle,
+     :small_square        => NSSmallSquareBezelStyle,
+     :textured_rounded    => NSTexturedRoundedBezelStyle,
+     :round_rect          => NSRoundRectBezelStyle,
+     :recessed            => NSRecessedBezelStyle,
+     :rounded_disclosure  => NSRoundedDisclosureBezelStyle
+  }
+  
+  constant :type, {
+     :momentary_light     => NSMomentaryLightButton,
+     :push_on_push_off    => NSPushOnPushOffButton,
+     :toggle              => NSToggleButton,
+     :switch              => NSSwitchButton,
+     :radio               => NSRadioButton,
+     :momentary_change    => NSMomentaryChangeButton,
+     :on_off              => NSOnOffButton,
+     :momentary_push_in   => NSMomentaryPushInButton,
+     :momentary_push      => NSMomentaryPushButton,
+     :momentary_light     => NSMomentaryLight
+  }
+
+  constant :state, {
+    :on                   => NSOnState,
+    :off                  => NSOffState,
+    :mixed                => NSMixedState
+  }
+  
+  def init_with_options(button, options)
+    button.initWithFrame options.delete(:frame)
+  end
+
+  custom_methods do
+    
+    def bezel=(value)
+      setBezelStyle(value)
+    end
+    
+    def type=(value)
+      setButtonType(value)
+    end
+    
+    def on?
+      state == NSOnState
+    end
+
+    def off?
+      state == NSOffState
+    end
+
+    def mixed?
+      state == NSMixedState
+    end
+    
+  end
+  
+end
\ No newline at end of file

Added: MacRuby/trunk/lib/hotcocoa/mappings/color.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/color.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/color.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,25 @@
+HotCocoa::Mappings.map :color => :NSColor do
+  
+  def alloc_with_options(options)
+    if options.has_key?(:name)
+      color = eval("NSColor.#{options.delete(:name)}Color")
+      color = color.colorWithAlphaComponent(options.delete(:alpha)) if options.has_key?(:alpha)
+      return color
+    elsif options.has_key?(:rgb)
+      calibrated = !options.delete(:device)
+      rgb = options.delete(:rgb)
+      red   = ((rgb >> 16) & 0xff)/255.0
+      green = ((rgb >> 8) & 0xff)/255.0
+      blue  = (rgb & 0xff)/255.0
+      alpha = options.delete(:alpha) || 1.0
+      if calibrated
+        return NSColor.colorWithCalibratedRed red, green:green, blue:blue, alpha:alpha
+      else
+        return NSColor.colorWithDeviceRed red, green:green, blue:blue, alpha:alpha
+      end
+    else
+      raise "Cannot create color with the provided options"
+    end
+  end
+  
+end

Added: MacRuby/trunk/lib/hotcocoa/mappings/column.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/column.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/column.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,15 @@
+HotCocoa::Mappings.map :column => :NSTableColumn do
+
+  def init_with_options(column, options)
+    column.initWithIdentifier(options.delete(:id))
+  end
+
+  custom_methods do
+    
+    def text=(value)
+      headerCell.setStringValue(value)
+    end
+    
+  end
+
+end
\ No newline at end of file

Added: MacRuby/trunk/lib/hotcocoa/mappings/combo_box.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/combo_box.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/combo_box.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,19 @@
+HotCocoa::Mappings.map :combo_box => :NSComboBox do
+  
+  defaults :selectable => true, :editable => true, :completes => true
+  
+  def init_with_options(combo_box, options)
+    combo_box.initWithFrame options.delete(:frame)
+  end
+
+  custom_methods do
+
+    def data=(data_source)
+      setUsesDataSource(true)
+      data_source = ComboBoxDataSource.new(data_source) if data_source.kind_of?(Array)
+      setDataSource(data_source)
+    end
+
+  end
+  
+end

Added: MacRuby/trunk/lib/hotcocoa/mappings/control.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/control.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/control.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,25 @@
+HotCocoa::Mappings.map :control => :NSControl do
+  
+  custom_methods do
+    
+    def on_action=(behavior)
+      object = Object.new
+      object.instance_variable_set("@behavior", behavior)
+      def object.perform_action(sender)
+        @behavior.call(sender)
+      end
+      setTarget(object)
+      setAction("perform_action:")
+    end
+    
+    def on_action(&behavior)
+      self.on_action = behavior
+    end
+    
+    def text=(text)
+      setStringValue(text)
+    end
+    
+  end
+
+end

Added: MacRuby/trunk/lib/hotcocoa/mappings/font.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/font.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/font.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,29 @@
+HotCocoa::Mappings.map :font => :NSFont do
+  
+  def alloc_with_options(options)
+    if options.has_key?(:system) && options.delete(:bold)
+      return NSFont.boldSystemFontOfSize(options.delete(:system))
+    end
+    {
+      :label => :labelFontOfSize,
+      :system => :systemFontOfSize,
+      :control_content => :controlContentFontOfSize,
+      :menu_bar => :menuBarFontOfSize,
+      :message => :messageFontOfSize,
+      :palette =>  :paletteFontOfSize,
+      :small_system => :smallSystemFontOfSize,
+      :title_bar => :titleBarFontOfSize,
+      :tool_tip => :toolTipFontOfSize,
+      :user_fixed => :userFixedPitchFontOfSize,
+      :user => :userFontOfSize
+    }.each do |key, method|
+      return eval("NSFont.#{method}(#{options.delete(key)})") if options.has_key?(key)
+    end
+    if options.has_key?(:name)
+      return NSFont.fontWithName(options.delete(:name), size:(options.delete(:size) || 0))
+    else
+      raise "Cannot create font with the provided options"
+    end
+  end
+  
+end

Added: MacRuby/trunk/lib/hotcocoa/mappings/image.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/image.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/image.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,13 @@
+HotCocoa::Mappings.map :image => :NSImage do
+  
+  def init_with_options(image, options)
+    if options.has_key?(:file)
+      image.initWithContentsOfFile(options.delete(:file))
+    elsif options.has_key?(:url)
+      image.initByReferencingURL(NSURL.alloc.initWithString(options.delete(:url)))
+    else
+      image.init
+    end
+  end
+  
+end

Added: MacRuby/trunk/lib/hotcocoa/mappings/image_view.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/image_view.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/image_view.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,19 @@
+HotCocoa::Mappings.map :image_view => :NSImageView do
+  
+  def init_with_options(image_view, options)
+    image_view.initWithFrame options.delete(:frame)
+  end
+  
+  custom_methods do
+    
+    def url=(url)
+      setImage(NSImage.alloc.initByReferencingURL(NSURL.alloc.initWithString(url)))
+    end
+    
+    def file=(file)
+      setImage(NSImage.alloc.initWithContentsOfFile(file))
+    end
+    
+  end
+  
+end

Added: MacRuby/trunk/lib/hotcocoa/mappings/label.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/label.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/label.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,11 @@
+HotCocoa::Mappings.map :label => :NSTextField do
+  
+  defaults :selectable => false, :bordered => false, :drawsBackground => false, :frame => DefaultEmptyRect
+  
+  def init_with_options(text_field, options)
+    tf = text_field.initWithFrame options.delete(:frame)
+    tf.editable = false
+    tf
+  end
+  
+end

Added: MacRuby/trunk/lib/hotcocoa/mappings/movie.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/movie.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/movie.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,13 @@
+HotCocoa::Mappings.map :movie => :QTMovie, :framework => :QTKit do
+
+  def alloc_with_options(options)
+    if options.has_key?(:file)
+      QTMovie.movieWithFile(options.delete(:file), error:options.delete(:error))
+    elsif options.has_key?(:url)
+      QTMovie.movieWithURL(NSURL.alloc.initWithString(options.delete(:url)), error:options.delete(:error))
+    else
+      raise "Can only allocate a movie from a file or a url"
+    end
+  end
+
+end

Added: MacRuby/trunk/lib/hotcocoa/mappings/movie_view.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/movie_view.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/movie_view.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,29 @@
+HotCocoa::Mappings.map :movie_view => :QTMovieView, :framework => :QTKit do
+
+  def init_with_options(movie_view, options)
+    movie_view.initWithFrame(options.delete(:frame))
+  end
+
+  custom_methods do
+    
+    def controller_visible=(value)
+      setControllerVisible(value)
+    end
+    
+    def controller_buttons=(buttons)
+      setBackButtonVisible(buttons.include?(:back))
+      setCustomButtonVisible(buttons.include?(:custom))
+      setHotSpotButtonVisible(buttons.include?(:hot_spot))
+      setStepButtonsVisible(buttons.include?(:step))
+      setTranslateButtonVisible(buttons.include?(:translate))
+      setVolumeButtonVisible(buttons.include?(:volume))
+      setZoomButtonsVisible(buttons.include?(:zoom))
+    end
+    
+    def fill_color=(color)
+      setFillColor(color)
+    end
+
+  end
+
+end
\ No newline at end of file

Added: MacRuby/trunk/lib/hotcocoa/mappings/popup.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/popup.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/popup.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,56 @@
+HotCocoa::Mappings.map :popup => :NSPopUpButton do
+  
+  defaults :pulls_down => false, 
+           :frame => DefaultEmptyRect
+
+  def init_with_options(popup, options)
+    popup.initWithFrame(options.delete(:frame), pullsDown:options.delete(:pulls_down))
+  end
+  
+  custom_methods do
+    
+    class ItemList
+      
+      attr_reader :control
+      
+      def initialize(control)
+        @control = control
+      end
+      
+      def <<(title)
+        control.addItemWithTitle(title)
+      end
+      
+      def delete(title)
+        control.removeItemWithTitle(title)
+      end
+      
+      def insert(title, at:index)
+        control.insertItemWithTitle(title, atIndex:index)
+      end
+      
+      def selected
+        control.titleOfSelectedItem
+      end
+      
+      def selected_index
+        control.indexOfSelectedItem
+      end
+      
+      def size
+        control.numberOfItems
+      end
+    end
+    
+    def items=(values)
+      removeAllItems
+      addItemsWithTitles(values)
+    end
+    
+    def items
+      @_item_list ||= ItemList.new(self)
+    end
+    
+  end
+  
+end
\ No newline at end of file

Added: MacRuby/trunk/lib/hotcocoa/mappings/scroll_view.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/scroll_view.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/scroll_view.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,25 @@
+HotCocoa::Mappings.map :scroll_view => :NSScrollView do
+  
+  defaults :vertical_scroller => true, :horizontal_scroller => true
+  
+  def init_with_options(scroll_view, options)
+    scroll_view.initWithFrame options.delete(:frame)
+  end
+  
+  custom_methods do
+    
+    def <<(view)
+      setDocumentView(view)
+    end
+    
+    def vertical_scroller=(value)
+      setHasVerticalScroller(value)
+    end
+    
+    def horizontal_scroller=(value)
+      setHasHorizontalScroller(value)
+    end
+    
+  end
+  
+end

Added: MacRuby/trunk/lib/hotcocoa/mappings/secure_text_field.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/secure_text_field.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/secure_text_field.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,17 @@
+HotCocoa::Mappings.map :secure_text_field => :NSSecureTextField do
+  
+  defaults :selectable => true, :editable => true, :echo => true
+  
+  def init_with_options(secure_text_field, options)
+    secure_text_field.initWithFrame options.delete(:frame)
+  end
+  
+  custom_methods do
+
+    def echo=(value)
+      cell.setEchosBullets(value)
+    end
+
+  end
+  
+end

Added: MacRuby/trunk/lib/hotcocoa/mappings/segmented_control.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/segmented_control.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/segmented_control.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,93 @@
+HotCocoa::Mappings.map :segmented_control => :NSSegmentedControl do
+  
+  def init_with_options(segmented_control, options)
+    segmented_control.initWithFrame options.delete(:frame)
+  end
+  
+  custom_methods do
+    
+    class Segment
+      attr_reader :number, :control
+      def initialize(control, number)
+        @number = number
+        @control = control
+      end
+      
+      def width
+        control.widthForSegment(number)
+      end
+      
+      def width=(width)
+        control.setWidth(width, forSegment:number)
+      end
+      
+      def label
+        control.labelForSegment(number)
+      end
+      
+      def label=(label)
+        control.setLabel(label, forSegment:number)
+      end
+      
+      def image
+        control.imageForSegment(number)
+      end
+      
+      def image=(image)
+        control.setImage(image, forSegment:number)
+      end
+      
+      def menu
+        control.menuForSegment(number)
+      end
+      
+      def menu=(menu)
+        control.setMenu(menu, forSegment:number)
+      end
+      
+      def selected?
+        control.isSelectedForSegment(number)
+      end
+      
+      def selected=(value)
+        control.setSelected(value, forSegment:number)
+      end
+      
+      def enabled?
+        control.isEnabledForSegment(number)
+      end
+      
+      def enabled=(value)
+        control.setEnabled(value, forSegment:number)
+      end
+    end
+    
+    def segments=(segments)
+      segments.each do |segment|
+        self << segment
+      end
+    end
+
+    def <<(data)
+      setSegmentCount(segmentCount+1)
+      segment = Segment.new(self, segmentCount-1)
+      data.each do |key, value|
+        segment.send("#{key}=", value)
+      end
+    end
+    
+    def [](segment_number)
+      Segment.new(self, segment_number)
+    end
+    
+    def select(segment_number)
+      setSelectedSegment(segment_number)
+    end
+    
+    def selected_segment
+      Segment.new(self, selectedSegment)
+    end
+
+  end
+  
+end

Added: MacRuby/trunk/lib/hotcocoa/mappings/sound.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/sound.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/sound.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,9 @@
+HotCocoa::Mappings.map :sound => :NSSound do 
+  
+  defaults :by_reference => true
+  
+  def init_with_options(sound, options)
+    sound.initWithContentsOfFile options.delete(:file), byReference:options.delete(:by_reference)
+  end
+  
+end
\ No newline at end of file

Added: MacRuby/trunk/lib/hotcocoa/mappings/split_view.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/split_view.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/split_view.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,23 @@
+HotCocoa::Mappings.map :split_view => :NSSplitView do
+  
+  def init_with_options(split_view, options)
+    split_view.initWithFrame options.delete(:frame)
+  end
+  
+  custom_methods do
+    
+    def <<(view)
+      addSubview(view)
+    end
+    
+    def horizontal=(value)
+      setVertical(!value)
+    end
+    
+    def set_position(position, of_divider_at_index:index)
+      setPosition position, ofDividerAtIndex:index
+    end
+    
+  end
+  
+end

Added: MacRuby/trunk/lib/hotcocoa/mappings/table_view.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/table_view.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/table_view.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,45 @@
+HotCocoa::Mappings.map :table_view => :NSTableView do
+  
+  defaults :column_resize => :uniform, :frame => DefaultEmptyRect
+  
+  constant :column_resize, {
+    :none               => NSTableViewNoColumnAutoresizing,
+    :uniform            => NSTableViewUniformColumnAutoresizingStyle,
+    :sequential         => NSTableViewSequentialColumnAutoresizingStyle,
+    :reverse_sequential => NSTableViewReverseSequentialColumnAutoresizingStyle,
+    :last_column_only   => NSTableViewLastColumnOnlyAutoresizingStyle,
+    :first_column_only  => NSTableViewFirstColumnOnlyAutoresizingStyle
+  }
+
+  def init_with_options(table_view, options)
+    table_view.initWithFrame(options.delete(:frame))
+  end
+
+  custom_methods do
+    
+    def data=(data_source)
+      data_source = TableDataSource.new(data_source) if data_source.kind_of?(Array)
+      setDataSource(data_source)
+    end
+    
+    def columns=(columns)
+      columns.each do |column|
+        addTableColumn(column)
+      end
+    end
+    
+    def column=(column)
+      addTableColumn(column)
+    end
+    
+    def auto_size
+      setAutoresizingMask(NSViewHeightSizable|NSViewWidthSizable)
+    end
+    
+    def column_resize=(style)
+      setColumnAutoresizingStyle(style)
+    end
+    
+  end
+
+end
\ No newline at end of file

Added: MacRuby/trunk/lib/hotcocoa/mappings/text_field.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/text_field.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/text_field.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,9 @@
+HotCocoa::Mappings.map :text_field => :NSTextField do
+  
+  defaults :selectable => true, :editable => true
+  
+  def init_with_options(text_field, options)
+    text_field.initWithFrame options.delete(:frame)
+  end
+  
+end

Added: MacRuby/trunk/lib/hotcocoa/mappings/view.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/view.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/view.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,28 @@
+HotCocoa::Mappings.map :view => :NSView do
+
+  constant :auto_resize, {
+    :none   => NSViewNotSizable,
+    :width  => NSViewWidthSizable,
+    :height => NSViewHeightSizable,
+    :min_x  => NSViewMinXMargin,
+    :min_y  => NSViewMinYMargin,
+    :max_x  => NSViewMaxXMargin,
+    :max_y  => NSViewMaxYMargin
+  }
+
+  constant :border, {
+    :none           => NSNoBorder,
+    :line           => NSLineBorder,
+    :bezel          => NSBezelBorder,
+    :groove         => NSGrooveBorder
+  }
+  
+  custom_methods do
+
+    def auto_resize=(value)
+      setAutoresizingMask(value)
+    end
+
+  end
+    
+end
\ No newline at end of file

Added: MacRuby/trunk/lib/hotcocoa/mappings/web_view.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/web_view.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/web_view.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,24 @@
+framework 'WebKit'
+
+HotCocoa::Mappings.map :web_view => :WebView , :framework => :WebKit do
+
+  defaults :auto_size => true
+
+  def init_with_options(web_view, options)
+    web_view.initWithFrame(options.delete(:frame))
+  end
+
+  custom_methods do
+    
+    def url=(url)
+      url = url.kind_of?(String) ? NSURL.alloc.initWithString(url) : url
+      mainFrame.loadRequest(NSURLRequest.requestWithURL(url))
+    end
+
+    def auto_size
+      setAutoresizingMask(NSViewHeightSizable|NSViewWidthSizable)
+    end
+    
+  end
+
+end
\ No newline at end of file

Added: MacRuby/trunk/lib/hotcocoa/mappings/window.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/window.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/window.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,70 @@
+HotCocoa::Mappings.map :window => :NSWindow do
+    
+  defaults  :style => [:titled, :closable, :miniturizable, :resizable],
+            :backing => :buffered, 
+            :defer => true,
+            :show => true
+            
+  constant :backing, :buffered => NSBackingStoreBuffered
+  
+  constant :style,   {
+    :borderless         => NSBorderlessWindowMask, 
+    :titled             => NSTitledWindowMask, 
+    :closable           => NSClosableWindowMask, 
+    :miniturizable      => NSMiniaturizableWindowMask, 
+    :resizable          => NSResizableWindowMask
+  }
+
+  def init_with_options(window, options)
+    window.initWithContentRect options.delete(:frame), 
+                               styleMask:options.delete(:style), 
+                               backing:options.delete(:backing), 
+                               defer:options.delete(:defer)
+  end
+    
+  custom_methods do
+    
+    def <<(control)
+      contentView.addSubview control
+    end
+    
+    def view
+      contentView
+    end
+    
+    def show
+      display
+      makeKeyAndOrderFront(nil)
+      orderFrontRegardless
+    end
+    
+  end
+  
+  delegating "window:shouldDragDocumentWithEvent:from:withPasteboard:", :to => :should_drag_document?,    :parameters => [:shouldDragDocumentWithEvent, :from, :withPasteboard]
+  delegating "window:shouldPopUpDocumentPathMenu:",                     :to => :should_popup_path_menu?,  :parameters => [:shouldPopUpDocumentPathMenu]
+  delegating "window:willPositionSheet:usingRect:",                     :to => :will_position_sheet,      :parameters => [:willPositionSheet, :usingRect]
+  delegating "windowDidBecomeKey:",                                     :to => :did_become_key
+  delegating "windowDidBecomeMain:",                                    :to => :did_become_main
+  delegating "windowDidChangeScreen:",                                  :to => :did_change_screen
+  delegating "windowDidChangeScreenProfile:",                           :to => :did_change_screen_profile
+  delegating "windowDidDeminiaturize:",                                 :to => :did_deminiturize
+  delegating "windowDidEndSheet:",                                      :to => :did_end_sheet
+  delegating "windowDidExpose:",                                        :to => :did_expose,               :parameters => ["windowDidExpose.userInfo['NSExposedRect']"]
+  delegating "windowDidMiniaturize:",                                   :to => :did_miniturize
+  delegating "windowDidMove:",                                          :to => :did_move
+  delegating "windowDidResignKey:",                                     :to => :did_resign_key
+  delegating "windowDidResignMain:",                                    :to => :did_resign_main
+  delegating "windowDidResize:",                                        :to => :did_resize
+  delegating "windowDidUpdate:",                                        :to => :did_update
+  delegating "windowShouldClose:",                                      :to => :should_close?
+  delegating "windowShouldZoom:toFrame:",                               :to => :should_zoom?,             :parameters => [:toFrame]
+  delegating "windowWillBeginSheet:",                                   :to => :will_begin_sheet
+  delegating "windowWillClose:",                                        :to => :will_close
+  delegating "windowWillMiniaturize:",                                  :to => :will_miniturize
+  delegating "windowWillMove:",                                         :to => :will_move
+  delegating "windowWillResize:toSize:",                                :to => :will_resize,              :parameters => [:toSize]
+  delegating "windowWillReturnFieldEditor:toObject:",                   :to => :returning_field_editor,   :parameters => [:toObject]
+  delegating "windowWillReturnUndoManager:",                            :to => :returning_undo_manager
+  delegating "windowWillUseStandardFrame:defaultFrame:",                :to => :will_use_standard_frame,  :parameters => [:defaultFrame]
+
+end
\ No newline at end of file

Added: MacRuby/trunk/lib/hotcocoa/mappings.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,49 @@
+module HotCocoa
+  module Mappings
+  
+    def self.reload
+      Dir.glob(File.join(File.dirname(__FILE__), "mappings", "*.rb")).each do |mapping|
+        load mapping
+      end
+    end
+    
+    DefaultEmptyRect = [0,0,0,0]
+  
+    def self.map(options, &block)
+      framework = options.delete(:framework)
+      mapped_name = options.keys.first
+      mapped_value = options.values.first
+      if framework.nil? || Object.constants.include?(mapped_value)
+        m = Mapper.new(mapped_name, Object.const_get(mapped_value), &block)
+        mappings[m.builder_method] = m
+      else
+        on_framework(framework) do
+          m = Mapper.new(mapped_name, Object.const_get(mapped_value), &block)
+          mappings[m.builder_method] = m
+        end
+      end
+    end
+  
+    def self.mappings
+      @mappings ||= {}
+    end
+  
+    def self.on_framework(name, &block)
+      (frameworks[name.to_s.downcase] ||= []) << block
+    end
+  
+    def self.frameworks
+      @frameworks ||= {}
+    end
+  
+    def self.framework_loaded(name)
+      if frameworks[name.to_s.downcase]
+        frameworks[name.to_s.downcase].each do |mapper|
+          mapper.call
+        end
+      end
+    end
+    
+  end
+  
+end
\ No newline at end of file

Added: MacRuby/trunk/lib/hotcocoa/table_data_source.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/table_data_source.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/table_data_source.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,18 @@
+module HotCocoa
+  class TableDataSource
+    attr_reader :data
+
+    def initialize(data)
+      @data = data
+    end
+
+    def numberOfRowsInTableView(tableView)
+      data.length
+    end
+
+    def tableView(view, objectValueForTableColumn:column, row:i)
+      data[i][column.identifier.intern]
+    end
+
+  end
+end
\ No newline at end of file

Added: MacRuby/trunk/lib/hotcocoa.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa.rb	2008-07-08 01:21:38 UTC (rev 316)
@@ -0,0 +1,25 @@
+framework 'cocoa'
+
+module HotCocoa
+  
+  def application(&block)
+    app = NSApplication.sharedApplication
+    if block
+      block.call(app)
+      app.run
+    end
+    app
+  end
+  
+  Views = {}
+  
+end
+
+require 'hotcocoa/mappings'
+require 'hotcocoa/mapping_methods'
+require 'hotcocoa/mapper'
+require 'hotcocoa/delegate_builder'
+require 'hotcocoa/table_data_source'
+require 'hotcocoa/combo_box_data_source'
+
+HotCocoa::Mappings.reload
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/macruby-changes/attachments/20080707/6d82fbd8/attachment-0001.html 


More information about the macruby-changes mailing list