File: markup.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 (104 lines) | stat: -rw-r--r-- 2,377 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
# Copyright (c) 2015-2016 Ruby-GNOME2 Project Team
# This program is licenced under the same licence as Ruby-GNOME2.
#
=begin
= Text View/Markup

Gtk::TextBuffer lets you define your own tags that can influence
text formatting in a variety of ways. In this example, we show
that Gtk::TextBuffer can load Pango markup and automatically generate
suitable tags.

=end
class MarkupDemo
  def initialize(main_window)
    @window = Gtk::Window.new(:toplevel)
    @window.screen = main_window.screen
    @window.title = "Markup"
    @window.set_default_size(450, 450)

    initialize_stack
    initialize_show_source
    initialize_headerbar

    sw = generate_sourceview
    @view = sw.child
    @stack.add_named(sw, "formatted")

    sw = generate_sourceview
    @view2 = sw.child
    @stack.add_named(sw, "source")

    populate_sourceviews
  end

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

  private

  def initialize_stack
    @stack = Gtk::Stack.new
    @stack.show
    @window.add(@stack)
  end

  def initialize_headerbar
    header = Gtk::HeaderBar.new
    header.pack_start(@show_source)
    header.show_close_button = true
    header.show_all
    @window.titlebar = header
  end

  def show_source_toggled_signal
    @show_source.signal_connect "toggled" do |button|
      if button.active?
        @stack.visible_child_name = "source"
      else
        buffer = @view2.buffer
        markup = buffer.get_text(buffer.start_iter, buffer.end_iter, false)
        buffer = @view.buffer
        buffer.insert_markup(buffer.start_iter, markup, -1)
        @stack.visible_child_name = "formatted"
      end
    end
  end

  def initialize_show_source
    @show_source = Gtk::CheckButton.new("Source")
    @show_source.valign = :center
    show_source_toggled_signal
  end

  def generate_sourceview
    view = Gtk::TextView.new
    view.wrap_mode = :word
    view.left_margin = 10
    view.right_margin = 10

    sw = Gtk::ScrolledWindow.new(nil, nil)
    sw.set_policy(:automatic, :automatic)

    sw.add(view)
    sw.show_all
    sw
  end

  def populate_sourceviews
    markup = Gio::Resources.lookup_data("/markup/markup.txt", 0)

    buffer = @view.buffer
    buffer.insert_markup(buffer.start_iter, markup, -1)

    buffer = @view2.buffer
    buffer.insert(buffer.start_iter, markup)

    @stack.show
  end
end