File: assistant.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 (341 lines) | stat: -rwxr-xr-x 9,734 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env ruby
=begin
  test-assistant.rb - Ruby/GTK version of testassistant.c from gtk+ 2.10 sources.

  Guillaume Cottenceau for the ruby-gnome2 project.

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

  $Id: assistant.rb,v 1.1 2006/11/23 08:39:12 mutoh Exp $
=end

require "gtk3"

class AssistantRunner
  def initialize
    @simple_assistant = nil
    @generous_assistant = nil
    @selected_branch = "A"
    @nonlinear_assistant = nil
    @full_featured_assistant = nil
  end

  def run_simple_assistant
    @simple_assistant = run_assistant(@simple_assistant,
                                      :create_simple_assistant)
  end

  def run_generous_assistant
    @generous_assistant = run_assistant(@generous_assistant,
                                        :create_generous_assistant)
  end

  def run_nonlinear_assistant
    @nonlinear_assistant = run_assistant(@nonlinear_assistant,
                                         :create_nonlinear_assistant)
  end

  def run_full_featured_assistant
    @full_featured_assistant = run_assistant(@full_featured_assistant,
                                             :create_full_featured_assistant)
  end

  private
  def run_assistant(assistant, assistant_create_method)
    assistant ||= send(assistant_create_method)
    if !assistant.visible?
      assistant.show
    else
      assistant.destroy
      assistant = nil
    end
    assistant
  end

  def add_completion_test_page(assistant, text, visible, complete)
    page = Gtk::Box.new(:vertical, 0)
    check = Gtk::CheckButton.new("Complete")
    page.add(Gtk::Label.new(text))
    page.add(check)
    check.active = complete
    check.signal_connect("toggled") do
      complete = check.active?
      assistant.set_page_complete(page, complete)
    end
    page.show_all if visible
    assistant.append_page(page)
    assistant.set_page_title(page, text)
    assistant.set_page_complete(page, complete)
    page
  end

  def create_test_page(text)
    Gtk::Label.new(text)
  end

  def prepare_cb(assistant, page)
    if page.is_a?(Gtk::Label)
      puts "prepare: #{page.text}"
    elsif assistant.get_page_type(page) == :progress
      progress = page.child
      assistant.set_page_complete(page, false)
      progress.fraction = 0.0
      GLib::Timeout.add(300) do
        page = assistant.get_nth_page(assistant.current_page)
        progress = page.child
        value = progress.fraction = progress.fraction + 0.1
        continue = value < 1.0
        assistant.set_page_complete(page, true) unless continue
        continue
      end
    else
      puts "prepare: #{assistant.current_page}"
    end
  end

  def create_simple_assistant
    assistant = Gtk::Assistant.new
    assistant.set_default_size(400, 300)
    assistant.signal_connect("cancel") do
      puts "cancel"
      assistant.hide
    end
    assistant.signal_connect("close") do
      puts "close"
      assistant.hide
    end
    assistant.signal_connect("apply") do
      puts "apply"
    end
    assistant.signal_connect("prepare") do |_assistant, page|
      prepare_cb(_assistant, page)
    end

    page = create_test_page("Page 1")
    page.show
    assistant.append_page(page)
    assistant.set_page_title(page, "Page 1")
    assistant.set_page_complete(page, true)

    page = create_test_page("Page 2")
    page.show
    assistant.append_page(page)
    assistant.set_page_title(page, "Page 2")
    assistant.set_page_type(page, :confirm)
    assistant.set_page_complete(page, true)
  end

  def create_generous_assistant
    assistant = Gtk::Assistant.new
    assistant.set_default_size(400, 300)
    assistant.signal_connect("cancel") do
      puts "cancel"
      assistant.hide
    end
    assistant.signal_connect("close") do
      puts "close"
      assistant.hide
    end
    assistant.signal_connect("apply") do
      puts "apply"
    end
    assistant.signal_connect("prepare") do|_assistant, page|
      prepare_cb(_assistant, page)
    end

    page = create_test_page("Introduction")
    page.show
    assistant.append_page(page)
    assistant.set_page_title(page, "Introduction")
    assistant.set_page_type(page, :intro)
    assistant.set_page_complete(page, true)

    page = add_completion_test_page(assistant, "Content", true, false)
    next_page = add_completion_test_page(assistant, "More Content", true, true)

    check = Gtk::CheckButton.new("Next page visible");
    check.active = true
    check.signal_connect("toggled") do
      puts "beuh"
      next_page.visible = check.active?
    end
    check.show
    page.add(check)

    add_completion_test_page(assistant, "Even More Content", true, true)

    page = create_test_page("Confirmation")
    page.show
    assistant.append_page(page)
    assistant.set_page_title(page, "Confirmation")
    assistant.set_page_type(page, :confirm)
    assistant.set_page_complete(page, true)

    page = Gtk::Alignment.new(0.5, 0.5, 0.9, 0.0)
    page.add(Gtk::ProgressBar.new)
    page.show_all
    assistant.append_page(page)
    assistant.set_page_title(page, "Progress")
    assistant.set_page_type(page, :progress)
    assistant.set_page_complete(page, true)

    page = create_test_page("Summary")
    page.show
    assistant.append_page(page)
    assistant.set_page_title(page, "Summary")
    assistant.set_page_type(page, :summary)
    assistant.set_page_complete(page, true)
  end

  def create_nonlinear_assistant
    assistant = Gtk::Assistant.new
    assistant.set_default_size(400, 300)
    assistant.signal_connect("cancel") do
      puts "cancel"
      assistant.hide
    end
    assistant.signal_connect("close") do
      puts "close"
      assistant.hide
    end
    assistant.signal_connect("apply") do
      puts "apply"
    end
    assistant.signal_connect("prepare") do |_assistant, page|
      prepare_cb(_assistant, page)
    end

    assistant.set_forward_page_func do |current_page|
      retval = -1
      if current_page == 0
        if @selected_branch == "A"
          retval = 1
        else
          retval = 2
        end
      elsif current_page == 1 || current_page == 2
        retval = 3
      end
      retval
    end

    page = Gtk::Box.new(:vertical, 6)
    button = Gtk::RadioButton.new("branch A")
    page.pack_start(button, :expand => false, :fill => false, :padding => 0)
    button.signal_connect("toggled") do
      @selected_branch = "A"
    end
    button.active = true
    button = Gtk::RadioButton.new(button, "branch B")
    page.pack_start(button, :expand => false, :fill => false, :padding => 0)
    button.signal_connect("toggled") do
      @selected_branch = "B"
    end
    page.show_all
    assistant.append_page(page)
    assistant.set_page_title(page, "Page 1")
    assistant.set_page_complete(page, true)

    page = create_test_page("Page 2A")
    page.show
    assistant.append_page(page)
    assistant.set_page_title(page, "Page 2A")
    assistant.set_page_complete(page, true)

    page = create_test_page("Page 2B")
    page.show
    assistant.append_page(page)
    assistant.set_page_title(page, "Page 2B")
    assistant.set_page_complete(page, true)

    page = create_test_page("Confirmation")
    page.show
    assistant.append_page(page)
    assistant.set_page_title(page, "Confirmation")
    assistant.set_page_type(page, :confirm)
    assistant.set_page_complete(page, true)
  end

  def create_full_featured_assistant
    assistant = Gtk::Assistant.new
    assistant.set_default_size(400, 300)
    assistant.signal_connect("cancel") do
      puts "cancel"
      assistant.hide
    end
    assistant.signal_connect("close") do
      puts "close"
      assistant.hide
    end
    assistant.signal_connect("apply") do
      puts "apply"
    end
    assistant.signal_connect("prepare") do |_assistant, page|
      prepare_cb(_assistant, page)
    end

    button = Gtk::Button.new(:stock_id => Gtk::Stock::STOP)
    button.show
    assistant.add_action_widget(button)

    page = create_test_page("Page 1")
    page.show
    assistant.append_page(page)
    assistant.set_page_title(page, "Page 1")
    assistant.set_page_complete(page, true)

    #- set a side image
    pixbuf = page.render_icon(Gtk::Stock::DIALOG_WARNING, :dialog)
    assistant.set_page_side_image(page, pixbuf)

    #- set a header image
    pixbuf = page.render_icon(Gtk::Stock::DIALOG_INFO, :dialog)
    assistant.set_page_header_image(page, pixbuf)

    page = create_test_page("Invisible page")
    assistant.append_page(page)

    page = create_test_page("Page 3")
    page.show
    assistant.append_page(page)
    assistant.set_page_title(page, "Page 3")
    assistant.set_page_type(page, :confirm)
    assistant.set_page_complete(page, true)

    #- set a header image
    pixbuf = page.render_icon(Gtk::Stock::DIALOG_INFO, :dialog)
    assistant.set_page_header_image(page, pixbuf)

    assistant
  end
end

runner = AssistantRunner.new
buttons = [
    [ "simple assistant", proc { runner.run_simple_assistant } ],
    [ "generous assistant", proc { runner.run_generous_assistant } ],
    [ "nonlinear assistant", proc { runner.run_nonlinear_assistant } ],
    [ "full featured assistant", proc { runner.run_full_featured_assistant } ],
]

if ENV["RTL"]
  Gtk::Widget.default_direction = Gtk::Widget::TEXT_DIR_RTL
end

window = Gtk::Window.new(:toplevel)
window.signal_connect("destroy") { Gtk.main_quit }
window.signal_connect("delete-event") { false }

box = Gtk::Box.new(:vertical, 6)
window.add(box)

buttons.each do |label, callback|
  button = Gtk::Button.new(:label => label)
  button.signal_connect("clicked") do
    callback.call
  end
  box.pack_start(button, :expand => true, :fill => true, :padding => 0)
end
window.show_all
Gtk.main