summaryrefslogtreecommitdiff
path: root/ext/tk/lib/tkdialog.rb
blob: 1133db6ae9f796b435d06ff1f303ae7c95e11ba2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
require "tk"

class TkDialog < TkWindow
  extend Tk

  # initialize tk_dialog
  def initialize(keys = nil)
    super()
    @var = TkVariable.new
    id = @var.id

    @title   = title

    @message = message
    @message_config = message_config

    @bitmap  = bitmap
    @bitmap_config = message_config

    @default_button = default_button

    @buttons = buttons
    @button_configs = proc{|num| button_configs num}

    if keys.kind_of? Hash
      @title   = keys['title'] if keys['title']
      @message = keys['message'] if keys['message']
      @bitmap  = keys['bitmap'] if keys['bitmap']
      @default_button = keys['default'] if keys['default']
      @buttons = keys['buttons'] if keys['buttons']

      @command = keys['prev_command']

      @message_config = keys['message_config'] if keys['message_config']
      @bitmap_config  = keys['bitmap_config']  if keys['bitmap_config']
      @button_configs = keys['button_configs'] if keys['button_configs']
    end

    if @title.include? ?\s
      @title = '{' + @title + '}'
    end

    @buttons = tk_split_list(@buttons) if @buttons.kind_of? String
    @buttons = @buttons.collect{|s|
      if s.kind_of? Array
	s = s.join(' ')
      end
      if s.include? ?\s
	'{' + s + '}'
      else
	s
      end
    }

    config = ""
    if @message_config.kind_of? Hash
      config << format("%s.msg configure %s\n", 
		       @path, hash_kv(@message_config).join(' '))
    end
    if @bitmap_config.kind_of? Hash
      config << format("%s.msg configure %s\n", 
		       @path, hash_kv(@bitmap_config).join(' '))
    end
    if @button_configs.kind_of? Proc
      @buttons.each_index{|i|
	if (c = @button_configs.call(i)).kind_of? Hash
	  config << format("%s.button%s configure %s\n", 
			   @path, i, hash_kv(c).join(' '))
	end
      }
    end
    config = 'after idle {' + config + '};' if config != ""

    if @command.kind_of? Proc
      @command.call(self)
    end

    INTERP._eval('eval {global '+id+';'+config+
		 'set '+id+' [tk_dialog '+ 
		 @path+" "+@title+" {#{@message}} "+@bitmap+" "+
		 String(@default_button)+" "+@buttons.join(' ')+']}')
  end
  def value
    return @var.value.to_i
  end
  ######################################################
  #                                                    #
  # these methods must be overridden for each dialog   #
  #                                                    #
  ######################################################
  def title
    return "DIALOG"
  end
  def message
    return "MESSAGE"
  end
  def message_config
    return nil
  end
  def bitmap
    return "info"
  end
  def bitmap_config
    return nil
  end
  def default_button
    return 0
  end
  def buttons
    #return "BUTTON1 BUTTON2"
    return ["BUTTON1", "BUTTON2"]
  end
  def button_configs(num)
    return nil
  end
end

#
# dialog for warning
#
class TkWarning < TkDialog
  def initialize(mes)
    @mes = mes
    super()
  end
  def message
    return @mes
  end
  def title
    return "WARNING";
  end
  def bitmap
    return "warning";
  end
  def default_button
    return 0;
  end
  def buttons
    return "OK";
  end
end