[macruby-changes] [440] MacRuby/trunk/lib/hotcocoa

source_changes at macosforge.org source_changes at macosforge.org
Thu Aug 14 14:37:36 PDT 2008


Revision: 440
          http://trac.macosforge.org/projects/ruby/changeset/440
Author:   rich at infoether.com
Date:     2008-08-14 14:37:35 -0700 (Thu, 14 Aug 2008)
Log Message:
-----------
add menu and menu_item and defualt application to load the menu, also add the template builder

Modified Paths:
--------------
    MacRuby/trunk/lib/hotcocoa/mappings/application.rb

Added Paths:
-----------
    MacRuby/trunk/lib/hotcocoa/mappings/menu.rb
    MacRuby/trunk/lib/hotcocoa/mappings/menu_item.rb
    MacRuby/trunk/lib/hotcocoa/template.rb

Modified: MacRuby/trunk/lib/hotcocoa/mappings/application.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/application.rb	2008-08-14 21:27:19 UTC (rev 439)
+++ MacRuby/trunk/lib/hotcocoa/mappings/application.rb	2008-08-14 21:37:35 UTC (rev 440)
@@ -6,6 +6,14 @@
   
   def handle_block(application, &block)
     block.call(application)
+    unless application.menu
+      begin
+        require 'lib/menu'
+        application.menu = application_menu(application)
+      rescue LoadError => e
+        puts "No menu specified"
+      end
+    end
     application.run
   end
   
@@ -19,6 +27,14 @@
       @name
     end
     
+    def menu=(menu)
+      setMainMenu(menu)
+    end
+    
+    def menu
+      mainMenu
+    end
+    
   end
   
 end
\ No newline at end of file

Added: MacRuby/trunk/lib/hotcocoa/mappings/menu.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/menu.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/menu.rb	2008-08-14 21:37:35 UTC (rev 440)
@@ -0,0 +1,69 @@
+HotCocoa::Mappings.map :menu => :NSMenu do
+  
+  defaults :title => ""
+  
+  def alloc_with_options(options)
+    NSMenu.alloc.initWithTitle(options.delete(:title))
+  end
+
+  custom_methods do
+    
+    def submenu(symbol, options={}, &block)
+      item = addItemWithTitle((options[:title] || titleize(symbol)), :action => nil, :keyEquivalent => "")
+      submenu = builder.menu :title => (options[:title] || titleize(symbol))
+      case symbol
+      when :apple
+        app.setAppleMenu(submenu)
+      when :services
+        app.setServicesMenu(submenu)
+      when :window
+        app.setWindowsMenu(submenu)
+      end
+      item_map[symbol] = submenu
+      block.call(submenu) if block
+      setSubmenu submenu, :forItem => item
+      submenu
+    end
+    
+    def item(symbol, options={})
+      options[:title] ||= titleize(symbol)
+      item = builder.menu_item(options)
+      item_map[symbol] = item
+      addItem item
+      item
+    end
+    
+    def separator
+      addItem NSMenuItem.separatorItem
+    end
+    
+    def [](symbol)
+      item_map[symbol]
+    end
+    
+    private
+    
+      def builder
+        @builder || create_builder
+      end
+      
+      def create_builder
+        @builder = Object.new
+        @builder.extend HotCocoa
+        @builder
+      end
+
+      def item_map
+        @item_map ||= {}
+      end
+      
+      def titleize(symbol)
+        symbol.to_s.split("_").collect(&:capitalize).join(" ")
+      end
+      
+      def app
+        @app ||= NSApplication.sharedApplication
+      end
+  end
+
+end
\ No newline at end of file

Added: MacRuby/trunk/lib/hotcocoa/mappings/menu_item.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/menu_item.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/menu_item.rb	2008-08-14 21:37:35 UTC (rev 440)
@@ -0,0 +1,32 @@
+HotCocoa::Mappings.map :menu_item => :NSMenuItem do
+  
+  defaults :title => "", :key => "", :action => nil
+  
+  constant :modifiers, {
+    :control    => NSControlKeyMask,
+    :alt        => NSAlternateKeyMask,
+    :command    => NSCommandKeyMask,
+    :shift      => NSShiftKeyMask
+  }
+  
+  def alloc_with_options(options)
+    NSMenuItem.alloc.initWithTitle options.delete(:title), :action => options.delete(:action), :keyEquivalent => options.delete(:key)
+  end
+  
+  custom_methods do
+    
+    def key
+      keyEquivalent
+    end
+    
+    def key=(value)
+      setKeyEquivalent(value)
+    end
+    
+    def modifiers=(value)
+      setKeyEquivalentModifierMask(value)
+    end
+    
+  end
+
+end
\ No newline at end of file

Added: MacRuby/trunk/lib/hotcocoa/template.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/template.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/template.rb	2008-08-14 21:37:35 UTC (rev 440)
@@ -0,0 +1,22 @@
+require 'fileutils'
+require 'rbconfig'
+
+module HotCocoa
+  class Template
+    def self.copy_to(directory, app_name)
+      dir = Config::CONFIG['datadir']
+      Dir.glob(File.join(dir, "hotcocoa_template", "**/*")).each do |file|
+        short_name = file[(dir.length+19)..-1]
+        if File.directory?(file)
+          FileUtils.mkdir_p File.join(directory, short_name)
+        else
+          File.open(File.join(directory, short_name), "w") do |out|
+            input =  File.read(file)
+            input.gsub!(/__APPLICATION_NAME__/, app_name)
+            out.write input
+          end
+        end
+      end
+    end
+  end
+end
\ No newline at end of file
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/macruby-changes/attachments/20080814/05bbcd30/attachment-0001.html 


More information about the macruby-changes mailing list