File: tk/dialog.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  class: TkDialogObj#7
extends
  Tk   
inherits from
  TkWindow   
has properties
constant: TkCommandNames #10
class method: show (1/2) / 1 #12
method: _set_button_config / 1 #18
method: create_self / 1 #59
method: show (2/E) #165
method: value #196
method: name #201
method: title #212
method: message #216
method: message_config #220
method: msgframe_config #224
method: bitmap #228
method: bitmap_config #233
method: default_button #237
method: buttons #242
method: button_configs / 1 #246
method: btnframe_config #250
method: prev_command #254
  class: TkDialog#264
inherits from
  TkDialogObj   
has properties
class method: show / 1 #265
method: initialize / 1 #269
  class: TkWarningObj#279
inherits from
  TkDialogObj   
has properties
method: initialize / 2 #280
method: show / 1 #292
method: title #303
method: bitmap #306
method: default_button #309
method: buttons #312
  class: TkWarning#318
inherits from
  TkWarningObj   
has properties
class method: show / 1 #319
method: initialize / 1 #322

Class Hierarchy

Object ( Builtin-Module )
TkKernel
TkObject
TkWindow
TkDialogObj#7
TkDialog#264
TkWarningObj#279
  TkWarning    #318

Code

   1  #
   2  #   tk/dialog.rb : create dialog boxes
   3  #
   4  require 'tk'
   5  require 'tk/variable.rb'
   6 
   7  class TkDialogObj < TkWindow
   8    extend Tk
   9 
  10    TkCommandNames = ['tk_dialog'.freeze].freeze
  11 
  12    def self.show(*args)
  13      dlog = self.new(*args)
  14      dlog.show
  15      dlog
  16    end
  17 
  18    def _set_button_config(configs)
  19      set_config = proc{|c,i|
  20        if $VERBOSE && (c.has_key?('command') || c.has_key?(:command))
  21          STDERR.print("Warning: cannot give a command option " + 
  22                       "to the dialog button#{i}. It was removed.\n")
  23        end
  24        c.delete('command'); c.delete(:command)
  25        # @config << Kernel.format("%s.button%s configure %s; ", 
  26        #                                @path, i, hash_kv(c).join(' '))
  27        # @config << @path+'.button'+i.to_s+' configure '+hash_kv(c).join(' ')+'; '
  28        @config << @path+'.button'+i.to_s+' configure '+
  29                     array2tk_list(hash_kv(c))+'; '
  30      }
  31      case configs
  32      when Proc
  33        @buttons.each_index{|i|
  34          if (c = configs.call(i)).kind_of?(Hash)
  35            set_config.call(c,i)
  36          end
  37        }
  38 
  39      when Array
  40        @buttons.each_index{|i|
  41          if (c = configs[i]).kind_of?(Hash)
  42            set_config.call(c,i)
  43          end
  44        }
  45 
  46      when Hash
  47        @buttons.each_with_index{|s,i|
  48          if (c = configs[s]).kind_of?(Hash)
  49            set_config.call(c,i)
  50          end
  51        }
  52      end
  53      # @config = 'after idle {' + @config + '};' if @config != ""
  54      @config = array2tk_list(['after', 'idle', @config]) << ';' if @config != ""
  55    end
  56    private :_set_button_config
  57 
  58    # initialize tk_dialog
  59    def create_self(keys)
  60      # @var = TkVariable.new
  61      @val = nil
  62 
  63      @title   = title
  64 
  65      @message = message
  66      @message_config = message_config
  67      @msgframe_config = msgframe_config
  68 
  69      @bitmap  = bitmap
  70      @bitmap_config = message_config
  71 
  72      @default_button = default_button
  73 
  74      @buttons = buttons
  75      @button_configs = proc{|num| button_configs(num)}
  76      @btnframe_config = btnframe_config
  77 
  78      #@config = "puts [winfo children .w0000];"
  79      @config = ""
  80 
  81      @command = prev_command
  82 
  83      if keys.kind_of?(Hash)
  84        @title   = keys['title'] if keys.key? 'title'
  85        @message = keys['message'] if keys.key? 'message'
  86        @bitmap  = keys['bitmap'] if keys.key? 'bitmap'
  87        # @bitmap  = '{}' if @bitmap == nil || @bitmap == ""
  88        @bitmap  = '' unless @bitmap
  89        @default_button = keys['default'] if keys.key? 'default'
  90        @buttons = keys['buttons'] if keys.key? 'buttons'
  91 
  92        @command = keys['prev_command'] if keys.key? 'prev_command'
  93 
  94        @message_config = keys['message_config'] if keys.key? 'message_config'
  95        @msgframe_config = keys['msgframe_config'] if keys.key? 'msgframe_config'
  96        @bitmap_config  = keys['bitmap_config']  if keys.key? 'bitmap_config'
  97        @button_configs = keys['button_configs'] if keys.key? 'button_configs'
  98        @btnframe_config = keys['btnframe_config'] if keys.key? 'btnframe_config'
  99      end
 100 
 101      #if @title.include? ?\s
 102      #  @title = '{' + @title + '}'
 103      #end
 104 
 105      if @buttons.kind_of?(Array)
 106        _set_button_config(@buttons.collect{|cfg| 
 107                             (cfg.kind_of? Array)? cfg[1]: nil})
 108        @buttons = @buttons.collect{|cfg| (cfg.kind_of? Array)? cfg[0]: cfg}
 109      end
 110      if @buttons.kind_of?(Hash)
 111        _set_button_config(@buttons)
 112        @buttons = @buttons.keys
 113      end
 114      @buttons = tk_split_simplelist(@buttons) if @buttons.kind_of?(String)
 115      @buttons = [] unless @buttons
 116  =begin
 117      @buttons = @buttons.collect{|s|
 118        if s.kind_of?(Array)
 119          s = s.join(' ')
 120        end
 121        if s.include? ?\s
 122          '{' + s + '}'
 123        else
 124          s
 125        end
 126      }
 127  =end
 128 
 129      if @message_config.kind_of?(Hash)
 130        # @config << Kernel.format("%s.msg configure %s;", 
 131        #                        @path, hash_kv(@message_config).join(' '))
 132        # @config << @path+'.msg configure '+hash_kv(@message_config).join(' ')+';'
 133        @config << @path+'.msg configure '+
 134                     array2tk_list(hash_kv(@message_config))+';'
 135      end
 136 
 137      if @msgframe_config.kind_of?(Hash)
 138        # @config << Kernel.format("%s.top configure %s;", 
 139        #                        @path, hash_kv(@msgframe_config).join(' '))
 140        # @config << @path+'.top configure '+hash_kv(@msgframe_config).join(' ')+';'
 141        @config << @path+'.top configure '+
 142                     array2tk_list(hash_kv(@msgframe_config))+';'
 143      end
 144 
 145      if @btnframe_config.kind_of?(Hash)
 146        # @config << Kernel.format("%s.bot configure %s;", 
 147        #                        @path, hash_kv(@btnframe_config).join(' '))
 148        # @config << @path+'.bot configure '+hash_kv(@btnframe_config).join(' ')+';'
 149        @config << @path+'.bot configure '+
 150                     array2tk_list(hash_kv(@btnframe_config))+';'
 151      end
 152 
 153      if @bitmap_config.kind_of?(Hash)
 154        # @config << Kernel.format("%s.bitmap configure %s;", 
 155        #                        @path, hash_kv(@bitmap_config).join(' '))
 156        # @config << @path+'.bitmap configure '+hash_kv(@bitmap_config).join(' ')+';'
 157        @config << @path+'.bitmap configure '+
 158                      array2tk_list(hash_kv(@bitmap_config))+';'
 159      end
 160 
 161      _set_button_config(@button_configs) if @button_configs
 162    end
 163    private :create_self
 164 
 165    def show
 166      # if @command.kind_of?(Proc)
 167      if TkComm._callback_entry?(@command)
 168        @command.call(self)
 169      end
 170 
 171      if @default_button.kind_of?(String)
 172        default_button = @buttons.index(@default_button)
 173      else
 174        default_button = @default_button
 175      end
 176      # default_button = '{}' if default_button == nil
 177      default_button = '' if default_button == nil
 178      #Tk.ip_eval('eval {global '+@var.id+';'+@config+
 179      #          'set '+@var.id+' [tk_dialog '+ 
 180      #          @path+" "+@title+" {#{@message}} "+@bitmap+" "+
 181      #          String(default_button)+" "+@buttons.join(' ')+']}')
 182      Tk.ip_eval(@config)
 183      # @val = Tk.ip_eval('tk_dialog ' + @path + ' ' + @title + 
 184      #                 ' {' + @message + '} ' + @bitmap + ' ' + 
 185      #                 String(default_button) + ' ' + @buttons.join(' ')).to_i
 186      # @val = Tk.ip_eval(self.class::TkCommandNames[0] + ' ' + @path + ' ' + 
 187      #                   @title + ' {' + @message + '} ' + @bitmap + ' ' + 
 188      #                   String(default_button) + ' ' + @buttons.join(' ')).to_i
 189      @val = Tk.ip_eval(array2tk_list([
 190                                        self.class::TkCommandNames[0], 
 191                                        @path, @title, @message, @bitmap, 
 192                                        String(default_button)
 193                                      ].concat(@buttons))).to_i
 194    end
 195 
 196    def value
 197      # @var.value.to_i
 198      @val
 199    end
 200 
 201    def name
 202      (@val)? @buttons[@val]: nil
 203    end
 204 
 205    ############################################################
 206    #                                                          #
 207    #  following methods should be overridden for each dialog  #
 208    #                                                          #
 209    ############################################################
 210    private
 211 
 212    def title
 213      # returns a title string of the dialog window
 214      return "DIALOG"
 215    end
 216    def message
 217      # returns a message text to display on the dialog
 218      return "MESSAGE"
 219    end
 220    def message_config
 221      # returns a Hash {option=>value, ...} for the message text
 222      return nil
 223    end
 224    def msgframe_config
 225      # returns a Hash {option=>value, ...} for the message text frame
 226      return nil
 227    end
 228    def bitmap
 229      # returns a bitmap name or a bitmap file path 
 230      # (@ + path ; e.g. '@/usr/share/bitmap/sample.xbm')
 231      return "info"
 232    end
 233    def bitmap_config
 234      # returns nil or a Hash {option=>value, ...} for the bitmap
 235      return nil
 236    end
 237    def default_button
 238      # returns a default button's number or name
 239      # if nil or null string, set no-default
 240      return 0
 241    end
 242    def buttons
 243      #return "BUTTON1 BUTTON2"
 244      return ["BUTTON1", "BUTTON2"]
 245    end
 246    def button_configs(num)
 247      # returns nil / Proc / Array or Hash (see _set_button_config)
 248      return nil
 249    end
 250    def btnframe_config
 251      # returns nil or a Hash {option=>value, ...} for the button frame
 252      return nil
 253    end
 254    def prev_command
 255      # returns nil or a Proc
 256      return nil
 257    end
 258  end
 259  TkDialog2 = TkDialogObj
 260 
 261  #
 262  # TkDialog : with showing at initialize
 263  #
 264  class TkDialog < TkDialogObj
 265    def self.show(*args)
 266      self.new(*args)
 267    end
 268 
 269    def initialize(*args)
 270      super(*args)
 271      show
 272    end
 273  end
 274 
 275 
 276  #
 277  # dialog for warning
 278  #
 279  class TkWarningObj < TkDialogObj
 280    def initialize(parent = nil, mes = nil)
 281      if !mes
 282        if parent.kind_of?(TkWindow)
 283          mes = ""
 284        else
 285          mes = parent.to_s
 286          parent = nil
 287        end
 288      end
 289      super(parent, :message=>mes)
 290    end
 291 
 292    def show(mes = nil)
 293      mes_bup = @message
 294      @message = mes if mes
 295      ret = super()
 296      @message = mes_bup
 297      ret
 298    end
 299 
 300    #######
 301    private
 302 
 303    def title
 304      return "WARNING";
 305    end
 306    def bitmap
 307      return "warning";
 308    end
 309    def default_button
 310      return 0;
 311    end
 312    def buttons
 313      return "OK";
 314    end
 315  end
 316  TkWarning2 = TkWarningObj
 317 
 318  class TkWarning < TkWarningObj
 319    def self.show(*args)
 320      self.new(*args)
 321    end
 322    def initialize(*args)
 323      super(*args)
 324      show
 325    end
 326  end