File: infobar.rb

package info (click to toggle)
ruby-gnome2 3.1.0-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 16,072 kB
  • ctags: 17,433
  • sloc: ansic: 93,621; ruby: 62,273; xml: 335; sh: 246; makefile: 25
file content (113 lines) | stat: -rw-r--r-- 3,433 bytes parent folder | download
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
# Copyright (c) 2015-2016 Ruby-GNOME2 Project Team
# This program is licenced under the same licence as Ruby-GNOME2.
#
=begin
=  Info Bars

 Info bar widgets are used to report important messages to the user.
=end
class InfobarDemo
  def initialize(main_window)
    @actions = Gtk::Box.new(:horizontal, 0)

    @window = Gtk::Window.new(:toplevel)
    @window.screen = main_window.screen
    @window.set_title("Info Bars")
    @window.set_border_width(8)

    @vbox = Gtk::Box.new(:vertical, 0)
    @window.add(@vbox)

    generate_simple_infobar("info")
    generate_simple_infobar("warning")

    generate_infobar_with_dialog

    generate_simple_infobar("error")
    generate_simple_infobar("other")

    frame = Gtk::Frame.new("Info bars")
    @vbox.pack_start(frame, :expand => false, :fill => false, :padding => 8)

    vbox2 = Gtk::Box.new(:vertical, 8)
    vbox2.border_width = 8
    frame.add(vbox2)

    label = Gtk::Label.new("An example of different info bars")
    vbox2.pack_start(label, :expand => false, :fill => false, :padding => 0)
    @actions.show_all
    vbox2.pack_start(@actions, :expand => false, :fill => false, :padding => 0)
  end

  def run
    if !@window.visible?
      @window.show_all
    else
      @window.destroy
    end
    @window
  end

  private

  def generate_infobar_label(message)
    label = Gtk::Label.new(message)
    label.line_wrap = true
    label.xalign = 0
    label
  end

  def add_a_label_message_in_an_infobar(bar, message)
    label = generate_infobar_label(message)
    bar.content_area.pack_start(label, :expand => false, :fill => false, :padding => 0)
  end

  def generate_simple_infobar(message_type)
    bar = Gtk::InfoBar.new
    @vbox.pack_start(bar, :expand => false, :fill => false, :padding => 0)
    bar.message_type = message_type.to_sym

    message = "This is an info bar with message type Gtk::MessageType::#{message_type.upcase}"
    add_a_label_message_in_an_infobar(bar, message)

    link_bar_to_a_toggle_button(bar, message_type.capitalize)
  end

  def link_bar_to_a_toggle_button(bar, message)
    button = Gtk::ToggleButton.new(:label => message)
    button.bind_property("active", bar, "visible", :bidirectional)
    @actions.add(button)
    button
  end

  def generate_infobar_with_dialog
    bar = Gtk::InfoBar.new
    bar.add_button("_OK", Gtk::ResponseType::OK)
    bar.show_close_button = true

    manage_response_event_of_infobar(bar)

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

    bar.message_type = :question
    message = "This is an info bar with message type Gtk::MessageType::QUESTION"
    add_a_label_message_in_an_infobar(bar, message)

    button = link_bar_to_a_toggle_button(bar, "Question")
    button.active = false
  end

  def manage_response_event_of_infobar(bar)
    bar.signal_connect "response" do |info_bar, response_id|
      info_bar.hide if response_id == Gtk::ResponseType::CLOSE
      dialog = Gtk::MessageDialog.new(:parent => info_bar.toplevel,
                                      :flags => [:modal, :destroy_with_parent],
                                      :type => :info,
                                      :buttons => :ok,
                                      :message => "You clicked a button on an info bar")
      dialog.secondary_text = "Your response has id #{response_id}"
      dialog.signal_connect("response", &:destroy)
      dialog.show_all
    end
  end
end