File: button_box.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 (111 lines) | stat: -rw-r--r-- 3,293 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
# Copyright (c) 2015-2016 Ruby-GNOME2 Project Team
# This program is licenced under the same licence as Ruby-GNOME2.
#
=begin
=  Button Boxes

 The Button Box widgets are used to arrange buttons with padding.
=end
class ButtonBoxDemo
  def initialize(main_window)
    @window = Gtk::Window.new(:toplevel)
    @window.screen = main_window.screen
    @window.title = "Button Boxes"
    @window.border_width = 10

    main_vbox = Gtk::Box.new(:vertical, 0)
    @window.add(main_vbox)
    frame_horz = generate_horizontal_frame
    main_vbox.pack_start(frame_horz,
                         :expand => true, :fill => true, :padding => 10)
    frame_vert = generate_vertical_frame
    main_vbox.pack_start(frame_vert,
                         :expand => true, :fill => true, :padding => 10)
  end

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

  private

  def generate_horizontal_frame
    frame_horz = Gtk::Frame.new("Horizontal Button Boxes")

    vbox = Gtk::Box.new(:vertical, 0)
    vbox.border_width = 10
    frame_horz.add(vbox)

    bbox = create_bbox(true, "Spread", 40, :spread)
    vbox.pack_start(bbox, :expand => true, :fill => true, :padding => 0)

    bbox = create_bbox(true, "Edge", 40, :edge)
    vbox.pack_start(bbox, :expand => true, :fill => true, :padding => 5)

    bbox = create_bbox(true, "Start", 40, :start)
    vbox.pack_start(bbox, :expand => true, :fill => true, :padding => 5)

    bbox = create_bbox(true, "End", 40, :end)
    vbox.pack_start(bbox, :expand => true, :fill => true, :padding => 5)

    bbox = create_bbox(true, "Center", 40, :center)
    vbox.pack_start(bbox, :expand => true, :fill => true, :padding => 5)

    bbox = create_bbox(true, "Expand", 0, :expand)
    vbox.pack_start(bbox, :expand => true, :fill => true, :padding => 5)
    frame_horz
  end

  def generate_vertical_frame
    frame_vert = Gtk::Frame.new("Vertical Button Boxes")

    hbox = Gtk::Box.new(:horizontal, 0)
    hbox.border_width = 10

    frame_vert.add(hbox)

    bbox = create_bbox(false, "Spread", 10, :spread)
    hbox.pack_start(bbox, :expand => true, :fill => true, :padding => 0)

    bbox = create_bbox(false, "Edge", 10, :edge)
    hbox.pack_start(bbox, :expand => true, :fill => true, :padding => 5)

    bbox = create_bbox(false, "Start", 10, :start)
    hbox.pack_start(bbox, :expand => true, :fill => true, :padding => 5)

    bbox = create_bbox(false, "End", 10, :end)
    hbox.pack_start(bbox, :expand => true, :fill => true, :padding => 5)

    bbox = create_bbox(false, "Center", 10, :center)
    hbox.pack_start(bbox, :expand => true, :fill => true, :padding => 5)

    bbox = create_bbox(false, "Expand", 0, :expand)
    hbox.pack_start(bbox, :expand => true, :fill => true, :padding => 5)
    frame_vert
  end

  def create_bbox(horizontal, title, spacing, layout)
    frame = Gtk::Frame.new(title)
    bbox = nil

    orientation = horizontal ? :horizontal : :vertical
    bbox = Gtk::ButtonBox.new(orientation)

    bbox.border_width = 5
    frame.add(bbox)
    bbox.layout = layout
    bbox.spacing = spacing

    %w(OK(_O) Cancel(_C) Help(_H)).each do |name|
      button = Gtk::Button.new(:label => name, :use_underline => true)
      bbox.add(button)
    end

    frame
  end
end