1# clrpick.rb
2#
3# This demonstration script prompts the user to select a color.
4#
5# widget demo prompts the user to select a color (called by 'widget')
6#
7#  Note: don't support ttk_wrapper. work with standard widgets only.
8#
9
10# toplevel widget
11if defined?($clrpick_demo) && $clrpick_demo
12  $clrpick_demo.destroy
13  $clrpick_demo = nil
14end
15
16# demo toplevel widget
17$clrpick_demo = TkToplevel.new {|w|
18  title("Color Selection Dialogs")
19  iconname("colors")
20  positionWindow(w)
21}
22
23base_frame = TkFrame.new($clrpick_demo).pack(:fill=>:both, :expand=>true)
24
25# label
26#TkLabel.new($clrpick_demo,'font'=>$font,'wraplength'=>'4i','justify'=>'left',
27Tk::Label.new($clrpick_demo,'font'=>$font,'wraplength'=>'4i','justify'=>'left',
28            'text'=>"Press the buttons below to choose the foreground and background colors for the widgets in this window.").pack('side'=>'top')
29
30# frame
31#TkFrame.new($clrpick_demo) {|frame|
32Tk::Frame.new($clrpick_demo) {|frame|
33  # TkButton.new(frame) {
34  Tk::Button.new(frame) {
35    text 'Dismiss'
36    command proc{
37      tmppath = $clrpick_demo
38      $clrpick_demo = nil
39      tmppath.destroy
40    }
41  }.pack('side'=>'left', 'expand'=>'yes')
42
43  # TkButton.new(frame) {
44  Tk::Button.new(frame) {
45    text 'Show Code'
46    command proc{showCode 'clrpick'}
47  }.pack('side'=>'left', 'expand'=>'yes')
48}.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m')
49
50# button
51# TkButton.new($clrpick_demo, 'text'=>'Set background color ...') {|b|
52Tk::Button.new($clrpick_demo, 'text'=>'Set background color ...') {|b|
53  command(proc{setColor $clrpick_demo, b, 'background',
54              ['background', 'highlightbackground']})
55  pack('side'=>'top', 'anchor'=>'c', 'pady'=>'2m')
56}
57
58# TkButton.new($clrpick_demo, 'text'=>'Set foreground color ...') {|b|
59Tk::Button.new($clrpick_demo, 'text'=>'Set foreground color ...') {|b|
60  command(proc{setColor $clrpick_demo, b, 'foreground', ['foreground']})
61  pack('side'=>'top', 'anchor'=>'c', 'pady'=>'2m')
62}
63
64def setColor(w,button,name,options)
65  w.grab
66  initialColor = button[name]
67  color = Tk.chooseColor('title'=>"Choose a #{name} color", 'parent'=>w,
68                         'initialcolor'=>initialColor)
69  if color != ""
70    setColor_helper(w,options,color)
71  end
72
73  w.grab('release')
74end
75
76def setColor_helper(w, options, color)
77  options.each{|opt|
78    begin
79      w[opt] = color
80    rescue
81    end
82  }
83  TkWinfo.children(w).each{|child|
84    setColor_helper child, options, color
85  }
86end
87
88