File: colorsel.rb

package info (click to toggle)
ruby-gnome2 2.2.0-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,360 kB
  • ctags: 14,222
  • sloc: ansic: 85,875; ruby: 38,232; sh: 80; xml: 41; makefile: 22
file content (82 lines) | stat: -rw-r--r-- 2,117 bytes parent folder | download | duplicates (6)
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
# Copyright (c) 2003-2005 Ruby-GNOME2 Project Team
# This program is licenced under the same licence as Ruby-GNOME2.
#
# $Id: colorsel.rb,v 1.4 2005/02/12 23:02:43 kzys Exp $
=begin
= Color Selector

Gtk::ColorSelection lets the user choose a color. Gtk::ColorSelectionDialog
is a prebuilt dialog containing a Gtk::ColorSelection.
=end
require 'common'

module Demo
  class ColorSel < BasicWindow
    def initialize
      super('Color Selection')

      @color = Gdk::RGBA.new(0, 0, 1, 1)

      set_border_width(8)

      vbox = Gtk::Box.new(:vertical, 0)
      vbox.set_border_width(8)
      add(vbox)

      ## Create the color swatch area
      @frame = Gtk::Frame.new
      @frame.set_shadow_type(:in)
      vbox.pack_start(@frame, :expand => true, :fill => true, :padding => 0)

      @da = Gtk::DrawingArea.new

      @da.signal_connect('draw') do |widget, event|
        if widget.window
          context = widget.style_context
          background_color = context.get_background_color(:normal)
          event.set_source_rgba(background_color.to_a)
          event.paint
        end
      end

      # set a minimum size
      @da.set_size_request(200, 200)
      # set the color
      @da.override_background_color(:normal, @color)

      @frame.add(@da)

      alignment = Gtk::Alignment.new(1.0, 0.5, 0.0, 0.0)

      button = Gtk::Button.new(:mnemonic => '_Change the above color')
      alignment.add(button)

      vbox.pack_start(alignment, :expand => false, :fill => false, :padding => 0)

      button.signal_connect('clicked') do
        change_color_callback
      end
    end

    def change_color_callback
      dialog = Gtk::ColorSelectionDialog.new(:title => 'Changing color')

      dialog.set_transient_for(self)

      colorsel = dialog.color_selection

      colorsel.set_previous_rgba(@color)
      colorsel.set_current_rgba(@color)
      colorsel.set_has_palette(true)

      response = dialog.run

      if response == Gtk::ResponseType::OK
        @color = colorsel.current_rgba
        @da.override_background_color(:normal, @color)
      end

      dialog.destroy
    end
  end
end