Revision
601
Author
lsansonetti@apple.com
Date
2008-09-17 15:33:35 -0700 (Wed, 17 Sep 2008)

Log Message

adding preliminary mappings for toolbar and text view

Modified Paths

Added Paths

Diff

Modified: MacRuby/trunk/lib/hotcocoa/mappings/control.rb (600 => 601)


--- MacRuby/trunk/lib/hotcocoa/mappings/control.rb	2008-09-16 01:05:31 UTC (rev 600)
+++ MacRuby/trunk/lib/hotcocoa/mappings/control.rb	2008-09-17 22:33:35 UTC (rev 601)
@@ -1,22 +1,9 @@
 HotCocoa::Mappings.map :control => :NSControl do
   
   custom_methods do
+   
+    include TargetActionConvenience 
     
-    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
-      self
-    end
-    
     def text=(text)
       setStringValue(text)
     end

Added: MacRuby/trunk/lib/hotcocoa/mappings/text_view.rb (0 => 601)


--- MacRuby/trunk/lib/hotcocoa/mappings/text_view.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/text_view.rb	2008-09-17 22:33:35 UTC (rev 601)
@@ -0,0 +1,9 @@
+HotCocoa::Mappings.map :text_view => :NSTextView do
+
+  defaults :frame => [0,0,0,0]
+
+  def init_with_options(text_view, options)
+    text_view.initWithFrame options.delete(:frame)
+  end
+
+end

Added: MacRuby/trunk/lib/hotcocoa/mappings/toolbar.rb (0 => 601)


--- MacRuby/trunk/lib/hotcocoa/mappings/toolbar.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/toolbar.rb	2008-09-17 22:33:35 UTC (rev 601)
@@ -0,0 +1,59 @@
+HotCocoa::Mappings.map :toolbar => :NSToolbar do
+
+  defaults :identifier => 'DefaultToolbarIdentifier',
+           :allowed => [:separator, :space, :flexible_space, :show_colors,
+                        :show_fonts, :customize, :print],
+           :default => [],
+           :allow_customization => true
+
+  def init_with_options(toolbar, options)
+    toolbar.initWithIdentifier options.delete(:identifier)
+
+    allowed = options.delete(:allowed).dup
+    default = options.delete(:default).dup
+
+    ary = default.select { |x| x.is_a?(NSToolbarItem) }
+    default -= ary
+    custom_items = {}
+    ary.each { |x| custom_items[x.itemIdentifier] = x } 
+    allowed.concat(custom_items.keys)
+    default.concat(custom_items.keys)
+
+    [allowed, default].each do |a|
+      a.map! do |i|
+        case i
+          when :separator
+            NSToolbarSeparatorItemIdentifier
+          when :space
+            NSToolbarSpaceItemIdentifier
+          when :flexible_space
+            NSToolbarFlexibleSpaceItemIdentifier
+          when :show_colors
+            NSToolbarShowColorsItemIdentifier
+          when :show_fonts
+            NSToolbarShowFontsItemIdentifier
+          when :customize
+            NSToolbarCustomizeToolbarItemIdentifier
+          when :print
+            NSToolbarPrintItemIdentifier
+          else
+            i
+        end
+      end 
+    end 
+    o = Object.new
+    o.instance_variable_set(:@allowed, allowed)
+    o.instance_variable_set(:@default, default)
+    o.instance_variable_set(:@custom_items, custom_items)
+    def o.toolbarAllowedItemIdentifiers(sender); @allowed; end
+    def o.toolbarDefaultItemIdentifiers(sender); @default; end
+    def o.toolbar(sender, itemForItemIdentifier:identifier, 
+                  willBeInsertedIntoToolbar:flag)
+      @custom_items[identifier]
+    end
+    toolbar.delegate = o
+    toolbar.allowsUserCustomization = options.delete(:allow_customization)
+    toolbar
+  end
+
+end

Added: MacRuby/trunk/lib/hotcocoa/mappings/toolbar_item.rb (0 => 601)


--- MacRuby/trunk/lib/hotcocoa/mappings/toolbar_item.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/toolbar_item.rb	2008-09-17 22:33:35 UTC (rev 601)
@@ -0,0 +1,20 @@
+HotCocoa::Mappings.map :toolbar_item => :NSToolbarItem do
+
+  def init_with_options(toolbar_item, options)
+    identifier = options.delete(:identifier)
+    unless identifier
+      label = options[:label]
+      if label
+        identifier = label.tr(' ', '_')
+      else
+        raise ArgumentError, ":identifier or :label required"
+      end
+    end
+    toolbar_item.initWithItemIdentifier identifier
+  end
+
+  custom_methods do
+    include TargetActionConvenience
+  end
+
+end

Modified: MacRuby/trunk/lib/hotcocoa/mappings.rb (600 => 601)


--- MacRuby/trunk/lib/hotcocoa/mappings.rb	2008-09-16 01:05:31 UTC (rev 600)
+++ MacRuby/trunk/lib/hotcocoa/mappings.rb	2008-09-17 22:33:35 UTC (rev 601)
@@ -8,6 +8,23 @@
     end
     
     DefaultEmptyRect = [0,0,0,0]
+
+    module TargetActionConvenience
+      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
+        self
+      end
+    end
   
     def self.map(options, &block)
       framework = options.delete(:framework)