File: search_entry2.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 (119 lines) | stat: -rw-r--r-- 3,597 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
114
115
116
117
118
119
# Copyright (c) 2015-2016 Ruby-GNOME2 Project Team
# This program is licenced under the same licence as Ruby-GNOME2.
#
=begin
=  Entry/Delayed Search Entry

 GtkSearchEntry sets up GtkEntries ready for search. Search entries
 have their "changed" signal delayed and should be used
 when the searched operation is slow such as loads of entries
 to search, or online searches.
=end
class SearchEntry2Demo
  def initialize(main_window)
    @window = Gtk::Window.new(:toplevel)
    @window.title = "Delayed Search Entry"
    @window.transient_for = main_window
    @window.resizable = true
    @window.set_size_request(200, -1)

    initialize_vbox

    @entry = Gtk::SearchEntry.new
    initialize_search_bar
    @vbox.pack_start(@searchbar,
                     :expand => false, :fill => false, :padding => 0)

    # Hook the search bar to key presses
    @window.signal_connect("key-press-event") do |_widget, event|
      @searchbar.handle_event(event)
    end

    initialize_help_label
    initialize_toggle_button
    initialize_result_hbox
    initialize_signal_hbox
  end

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

  private

  def initialize_search_bar
    container = Gtk::Box.new(:horizontal, 10)
    container.halign = :center
    container.pack_start(@entry,
                         :expand => false, :fill => false, :padding => 0)

    @searchbar = Gtk::SearchBar.new
    @searchbar.connect_entry(@entry)
    @searchbar.show_close_button = false
    @searchbar.add(container)
  end

  def initialize_vbox
    @vbox = Gtk::Box.new(:vertical, 0)
    @window.add(@vbox)
    @vbox.border_width = 0
  end

  def initialize_help_label
    # Help
    label = Gtk::Label.new("Start Typing to search")
    @vbox.pack_start(label, :expand => true, :fill => true, :padding => 0)
  end

  def initialize_toggle_button
    # Toggle button
    button = Gtk::ToggleButton.new(:label => "Search")
    button.bind_property("active", @searchbar, "search-mode-enabled",
                         :bidirectional)
    @vbox.pack_start(button, :expand => true, :fill => true, :padding => 0)
  end

  def initialize_result_hbox
    hbox = Gtk::Box.new(:horizontal, 10)
    @vbox.pack_start(hbox, :expand => true, :fill => true, :padding => 0)
    hbox.border_width = 0

    # Result
    label = Gtk::Label.new("Result:")
    label.xalign = 0.0
    label.margin_start = 6
    hbox.pack_start(label, :expand => true, :fill => true, :padding => 0)
    label = Gtk::Label.new("")
    hbox.pack_start(label, :expand => true, :fill => true, :padding => 0)

    @entry.signal_connect("search-changed") do |widget|
      puts "search changed: #{widget.text || ''}"
      label.text = widget.text || ""
    end
    @entry.signal_connect("changed") { puts "changed: #{label.text || ''}" }
  end

  def initialize_signal_hbox
    hbox = Gtk::Box.new(:horizontal, 10)
    @vbox.pack_start(hbox, :expand => true, :fill => true, :padding => 0)
    hbox.border_width = 0

    label = Gtk::Label.new("Signal:")
    label.xalign = 0.0
    label.margin_start = 6
    hbox.pack_start(label, :expand => true, :fill => true, :padding => 0)

    label = Gtk::Label.new("")
    hbox.pack_start(label, :expand => true, :fill => true, :padding => 0)

    @entry.signal_connect("search-changed") { label.text = "search-changed" }
    @entry.signal_connect("next-match") { label.text = "next-match" }
    @entry.signal_connect("previous-match") { label.text = "previous-match" }
    @entry.signal_connect("stop-search") { label.text = "stop-search" }
  end
end