File: tk/menubar.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  class: TkMenubar#92
includes
  TkComposite   
  TkMenuSpec   
inherits from
  Frame ( Tk )
has properties
method: initialize / 3 #96
method: add_menu / 1 #112
method: [] / 1 #128

Class Hierarchy

Object ( Builtin-Module )
TkKernel
TkObject
TkWindow
Frame ( Tk )
  TkMenubar    #92

Code

   1  #
   2  # tk/menubar.rb
   3  #
   4  # Original version:
   5  #   Copyright (C) 1998 maeda shugo. All rights reserved. 
   6  #   This file can be distributed under the terms of the Ruby.
   7 
   8  # Usage:
   9  #
  10  # menu_spec = [
  11  #   [['File', 0],
  12  #     ['Open', proc{puts('Open clicked')}, 0],
  13  #     '---',
  14  #     ['Quit', proc{exit}, 0]],
  15  #   [['Edit', 0],
  16  #     ['Cut', proc{puts('Cut clicked')}, 2],
  17  #     ['Copy', proc{puts('Copy clicked')}, 0],
  18  #     ['Paste', proc{puts('Paste clicked')}, 0]]
  19  # ]
  20  # menubar = TkMenubar.new(nil, menu_spec,
  21  #                       'tearoff'=>false,
  22  #                       'foreground'=>'grey40',
  23  #                       'activeforeground'=>'red',
  24  #                       'font'=>'-adobe-helvetica-bold-r-*--12-*-iso8859-1')
  25  # menubar.pack('side'=>'top', 'fill'=>'x')
  26  #
  27  #
  28  # OR
  29  #
  30  #
  31  # menubar = TkMenubar.new
  32  # menubar.add_menu([['File', 0],
  33  #                  ['Open', proc{puts('Open clicked')}, 0],
  34  #                  '---',
  35  #                  ['Quit', proc{exit}, 0]])
  36  # menubar.add_menu([['Edit', 0],
  37  #                  ['Cut', proc{puts('Cut clicked')}, 2],
  38  #                  ['Copy', proc{puts('Copy clicked')}, 0],
  39  #                  ['Paste', proc{puts('Paste clicked')}, 0]])
  40  # menubar.configure('tearoff', false)
  41  # menubar.configure('foreground', 'grey40')
  42  # menubar.configure('activeforeground', 'red')
  43  # menubar.configure('font', '-adobe-helvetica-bold-r-*--12-*-iso8859-1')
  44  # menubar.pack('side'=>'top', 'fill'=>'x')
  45  #
  46  #
  47  # OR
  48  #
  49  # radio_var = TkVariable.new('y')
  50  # menu_spec = [
  51  #   [['File', 0],
  52  #     {:label=>'Open', :command=>proc{puts('Open clicked')}, :underline=>0},
  53  #     '---',
  54  #     ['Check_A', TkVariable.new(true), 6],
  55  #     {:type=>'checkbutton', :label=>'Check_B', 
  56  #                 :variable=>TkVariable.new, :underline=>6},
  57  #     '---',
  58  #     ['Radio_X', [radio_var, 'x'], 6],
  59  #     ['Radio_Y', [radio_var, 'y'], 6],
  60  #     ['Radio_Z', [radio_var, 'z'], 6],
  61  #     '---',
  62  #     ['cascade', [ 
  63  #                    ['sss', proc{p 'sss'}, 0], 
  64  #                    ['ttt', proc{p 'ttt'}, 0], 
  65  #                    ['uuu', proc{p 'uuu'}, 0], 
  66  #                    ['vvv', proc{p 'vvv'}, 0], 
  67  #                 ], 0],
  68  #     '---',
  69  #     ['Quit', proc{exit}, 0]],
  70  #   [['Edit', 0],
  71  #     ['Cut', proc{puts('Cut clicked')}, 2],
  72  #     ['Copy', proc{puts('Copy clicked')}, 0],
  73  #     ['Paste', proc{puts('Paste clicked')}, 0]]
  74  # ]
  75  # menubar = TkMenubar.new(nil, menu_spec,
  76  #                        'tearoff'=>false,
  77  #                        'foreground'=>'grey40',
  78  #                        'activeforeground'=>'red',
  79  #                        'font'=>'Helvetia 12 bold')
  80  # menubar.pack('side'=>'top', 'fill'=>'x')
  81 
  82  # See tk/menuspce.rb about the format of the menu_spec
  83 
  84  # To use add_menu, configuration must be done by calling configure after
  85  # adding all menus by add_menu, not by the constructor arguments.
  86 
  87  require 'tk'
  88  require 'tk/frame'
  89  require 'tk/composite'
  90  require 'tk/menuspec'
  91 
  92  class TkMenubar<Tk::Frame
  93    include TkComposite
  94    include TkMenuSpec
  95    
  96    def initialize(parent = nil, spec = nil, options = nil)
  97      if parent.kind_of? Hash
  98        options = _symbolkey2str(parent)
  99        spec = options.delete('spec')
 100        super(options)
 101      else
 102        super(parent, options)
 103      end
 104 
 105      @menus = []
 106      
 107      spec.each{|info| add_menu(info)} if spec
 108 
 109      options.each{|key, value| configure(key, value)} if options
 110    end
 111 
 112    def add_menu(menu_info)
 113      mbtn, menu = _create_menubutton(@frame, menu_info)
 114 
 115      submenus = _get_cascade_menus(menu).flatten
 116 
 117      @menus.push([mbtn, menu])
 118      delegate('tearoff', menu, *submenus)
 119      delegate('foreground', mbtn, menu, *submenus)
 120      delegate('background', mbtn, menu, *submenus)
 121      delegate('disabledforeground', mbtn, menu, *submenus)
 122      delegate('activeforeground', mbtn, menu, *submenus)
 123      delegate('activebackground', mbtn, menu, *submenus)
 124      delegate('font', mbtn, menu, *submenus)
 125      delegate('kanjifont', mbtn, menu, *submenus)
 126    end
 127    
 128    def [](index)
 129      return @menus[index]
 130    end
 131  end