[macruby-changes] [375] MacRuby/trunk/sample-macruby/HotCocoa/collection_view.rb

source_changes at macosforge.org source_changes at macosforge.org
Mon Jul 14 21:28:57 PDT 2008


Revision: 375
          http://trac.macosforge.org/projects/ruby/changeset/375
Author:   rich at infoether.com
Date:     2008-07-14 21:28:57 -0700 (Mon, 14 Jul 2008)
Log Message:
-----------
example for collection view

Added Paths:
-----------
    MacRuby/trunk/sample-macruby/HotCocoa/collection_view.rb

Added: MacRuby/trunk/sample-macruby/HotCocoa/collection_view.rb
===================================================================
--- MacRuby/trunk/sample-macruby/HotCocoa/collection_view.rb	                        (rev 0)
+++ MacRuby/trunk/sample-macruby/HotCocoa/collection_view.rb	2008-07-15 04:28:57 UTC (rev 375)
@@ -0,0 +1,60 @@
+require 'hotcocoa'
+
+include HotCocoa
+
+class Icon
+  attr_accessor :name, :image
+  def initialize(name, image)
+    @name = name
+    @image = image
+  end
+end
+
+class MyIconView < NSView
+  
+  include HotCocoa::Behaviors
+  
+  def self.create
+    view = alloc.initWithFrame([0,0,60,60])
+    view.create_subviews
+    view
+  end
+  
+  attr_reader :image
+  
+  def collection_item=(item)
+    image.bind "value",   toObject:item, withKeyPath:"representedObject.image", options:nil
+    image.bind "toolTip", toObject:item, withKeyPath:"representedObject.name",  options:nil
+  end
+  
+  def create_subviews
+    @image = image_view :frame => [0,0,60,60]
+    addSubview(image)
+  end
+  
+  def hitTest(point)
+    nil
+  end
+  
+end
+
+array_controller = NSArrayController.new
+array_controller.setAvoidsEmptySelection(false)
+array_controller.setPreservesSelection(false)
+array_controller.setSelectsInsertedObjects(false)
+array_controller.setAutomaticallyRearrangesObjects(true)
+array_controller.setSortDescriptors(NSArray.arrayWithObject(NSSortDescriptor.alloc.initWithKey("name", ascending: false)))
+array_controller.addObject Icon.new("Rich", image(:file => "rich.jpg"))
+
+application do |app|
+  window :frame => [100, 100, 500, 500], :title => "HotCocoa!" do |win|
+    win << scroll_view(:frame => [10,10,480,470]) do |scroll|
+      cv = collection_view :frame => [0,0,480,470], 
+                                       :content => {array_controller => "arrangedObjects"}, 
+                                       :selection_indexes => {array_controller => "selectionIndexes"},
+                                       :item_view => MyIconView.create
+      scroll << cv
+    end
+  end
+end
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/macruby-changes/attachments/20080714/a0fe1c3e/attachment-0001.html 


More information about the macruby-changes mailing list