File: combobox.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 (85 lines) | stat: -rw-r--r-- 1,855 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/env ruby
=begin
  combobox.rb - Ruby/GTK sample script.

  Copyright (c) 2004-2006 Ruby-GNOME2 Project Team
  This program is licenced under the same licence as Ruby-GNOME2.

  $Id: combobox.rb,v 1.3 2006/06/17 13:18:12 mutoh Exp $
=end

require "gtk3"

if str = Gtk.check_version(3, 10, 7)
  puts "This sample requires GTK+ 3.10.7 or later"
  puts str
  exit
end

window = Gtk::Window.new("Gtk::ComboBox sample")
window.signal_connect("destroy") {Gtk.main_quit}

#
# Text only
#
combo1 = Gtk::ComboBoxText.new
["foo", "bar", "fuga", "hoge"].each do |val|
  combo1.append_text(val)
end
combo1.active = 1

combo1.signal_connect("changed") do
  p "combo1: #{combo1.active}, #{combo1.active_iter[0]}"
end

#
# Icon and text
#
model = Gtk::ListStore.new(Gdk::Pixbuf, String)
[[Gtk::Stock::QUIT, "quit"],
 [Gtk::Stock::CANCEL, "cancel"],
 [Gtk::Stock::OK, "ok"]].each do |stock, name|
  iter = model.append
  iter[0] = window.render_icon_pixbuf(stock, Gtk::IconSize::IconSize::MENU)
  iter[1] = name
end

combo2 = Gtk::ComboBox.new(:model => model)

# column 1
renderer = Gtk::CellRendererPixbuf.new
combo2.pack_start(renderer, false)
combo2.set_attributes(renderer, :pixbuf => 0)

# column 2
renderer = Gtk::CellRendererText.new
combo2.pack_start(renderer, true)
combo2.set_attributes(renderer, :text => 1)

combo2.active = 2

combo2.signal_connect("changed") do
  p "combo2: #{combo2.active}, #{combo2.active_iter[1]}"
end

#
# Gtk::ComboBoxEntry
#
combo3 = Gtk::ComboBoxText.new(:entry => true)
["foo", "bar", "fuga", "hoge"].each do |val|
  combo3.append_text(val)
end
combo3.active = 1

combo3.signal_connect("changed") do
  if combo3.active_iter
    p "combo3: #{combo3.active}, #{combo3.active_iter[0]}"
  end
end

# Show main window
vbox = Gtk::Box.new(:vertical)
vbox.add(combo1).add(combo2).add(combo3)
window.add(vbox).show_all

Gtk.main