ext/tk/lib/tkdialog.rb


DEFINITIONS

This source file includes following functions.


   1  require "tk"
   2  
   3  class TkDialog < TkWindow
   4    extend Tk
   5  
   6    # initialize tk_dialog
   7    def initialize(keys = nil)
   8      super()
   9      @var = TkVariable.new
  10      id = @var.id
  11  
  12      @title   = title
  13  
  14      @message = message
  15      @message_config = message_config
  16  
  17      @bitmap  = bitmap
  18      @bitmap_config = message_config
  19  
  20      @default_button = default_button
  21  
  22      @buttons = buttons
  23      @button_configs = proc{|num| button_configs num}
  24  
  25      if keys.kind_of? Hash
  26        keys = _symbolkey2str(keys)
  27        @title   = keys['title'] if keys['title']
  28        @message = keys['message'] if keys['message']
  29        @bitmap  = keys['bitmap'] if keys['bitmap']
  30        @default_button = keys['default'] if keys['default']
  31        @buttons = keys['buttons'] if keys['buttons']
  32  
  33        @command = keys['prev_command']
  34  
  35        @message_config = keys['message_config'] if keys['message_config']
  36        @bitmap_config  = keys['bitmap_config']  if keys['bitmap_config']
  37        @button_configs = keys['button_configs'] if keys['button_configs']
  38      end
  39  
  40      if @title.include? ?\s
  41        @title = '{' + @title + '}'
  42      end
  43  
  44      @buttons = tk_split_list(@buttons) if @buttons.kind_of? String
  45      @buttons = @buttons.collect{|s|
  46        if s.kind_of? Array
  47          s = s.join(' ')
  48        end
  49        if s.include? ?\s
  50          '{' + s + '}'
  51        else
  52          s
  53        end
  54      }
  55  
  56      config = ""
  57      if @message_config.kind_of? Hash
  58        config << format("%s.msg configure %s\n", 
  59                         @path, hash_kv(@message_config).join(' '))
  60      end
  61      if @bitmap_config.kind_of? Hash
  62        config << format("%s.msg configure %s\n", 
  63                         @path, hash_kv(@bitmap_config).join(' '))
  64      end
  65      if @button_configs.kind_of? Proc
  66        @buttons.each_index{|i|
  67          if (c = @button_configs.call(i)).kind_of? Hash
  68            config << format("%s.button%s configure %s\n", 
  69                             @path, i, hash_kv(c).join(' '))
  70          end
  71        }
  72      end
  73      config = 'after idle {' + config + '};' if config != ""
  74  
  75      if @command.kind_of? Proc
  76        @command.call(self)
  77      end
  78  
  79      INTERP._eval('eval {global '+id+';'+config+
  80                   'set '+id+' [tk_dialog '+ 
  81                   @path+" "+@title+" {#{@message}} "+@bitmap+" "+
  82                   String(@default_button)+" "+@buttons.join(' ')+']}')
  83    end
  84    def value
  85      return @var.value.to_i
  86    end
  87    ######################################################
  88    #                                                    #
  89    # these methods must be overridden for each dialog   #
  90    #                                                    #
  91    ######################################################
  92    def title
  93      return "DIALOG"
  94    end
  95    def message
  96      return "MESSAGE"
  97    end
  98    def message_config
  99      return nil
 100    end
 101    def bitmap
 102      return "info"
 103    end
 104    def bitmap_config
 105      return nil
 106    end
 107    def default_button
 108      return 0
 109    end
 110    def buttons
 111      #return "BUTTON1 BUTTON2"
 112      return ["BUTTON1", "BUTTON2"]
 113    end
 114    def button_configs(num)
 115      return nil
 116    end
 117  end
 118  
 119  #
 120  # dialog for warning
 121  #
 122  class TkWarning < TkDialog
 123    def initialize(mes)
 124      @mes = mes
 125      super()
 126    end
 127    def message
 128      return @mes
 129    end
 130    def title
 131      return "WARNING";
 132    end
 133    def bitmap
 134      return "warning";
 135    end
 136    def default_button
 137      return 0;
 138    end
 139    def buttons
 140      return "OK";
 141    end
 142  end