File: textbuffer_serialize.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 (130 lines) | stat: -rw-r--r-- 4,430 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/env ruby
=begin
  textbuffer_serialize.rb - Ruby/GTK sample script.

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

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

require 'gtk3'

if str = Gtk.check_version(2, 10, 0)
  puts "This sample requires GTK+ 2.10.0 or later"
  puts str
  exit
end

current_folder = ENV['HOME'] || "."
file_name = "serialized.dat"

textview = Gtk::TextView.new
textview.set_size_request(600, 400)
buffer = textview.buffer
buffer.text = DATA.read
format = buffer.serialize_formats[0]
buffer.register_serialize_target(nil)
buffer.register_deserialize_target(nil)

window = Gtk::Window.new("Gtk::TextBuffer Serialize Demo")
vbox = Gtk::VBox.new

serialize_button = Gtk::FileChooserButton.new("Serialize to a file", 
                                              Gtk::FileChooser::ACTION_OPEN)
deserialize_button = Gtk::FileChooserButton.new("Serialize to a file", 
                                                Gtk::FileChooser::ACTION_OPEN)

serialize_button.current_folder = current_folder
deserialize_button.current_folder = current_folder

toolbar = Gtk::Toolbar.new

toolbar.append(Gtk::Stock::OPEN, "Deserialize from a file") do
  dialog = Gtk::FileChooserDialog.new("Deserialize from a file",
                                      window,
                                      Gtk::FileChooser::ACTION_OPEN,
                                      nil,
                                      [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL],
                                      [Gtk::Stock::OPEN, Gtk::Dialog::RESPONSE_ACCEPT])
  
  dialog.filename = File.expand_path(file_name)
  if dialog.run == Gtk::Dialog::RESPONSE_ACCEPT
    file_name = dialog.filename
    File.open(file_name, "rb") {|io|
      buffer.delete(buffer.start_iter, buffer.end_iter)
      buffer.deserialize(buffer, format, buffer.start_iter, io.read)
    }
  end
  dialog.destroy
end

toolbar.append(Gtk::Stock::SAVE, "Serialize to a file") do
  dialog = Gtk::FileChooserDialog.new("Serialize from a file",
                                      window,
                                      Gtk::FileChooser::ACTION_SAVE,
                                      nil,
                                      [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL],
                                      [Gtk::Stock::OPEN, Gtk::Dialog::RESPONSE_ACCEPT])
  dialog.current_name = file_name
  if dialog.run == Gtk::Dialog::RESPONSE_ACCEPT
    file_name = dialog.filename
    File.open(file_name, "wb") {|io|
      io.write(buffer.serialize(buffer, format, buffer.start_iter, buffer.end_iter))
    }
  end
  dialog.destroy
end

toolbar.append(Gtk::Stock::CLEAR, "Clear all") do
  buffer.delete(buffer.start_iter, buffer.end_iter)
end
toolbar.append_space
toolbar.append(Gtk::Stock::SELECT_COLOR, "Color the region") do
  dialog = Gtk::ColorSelectionDialog.new("Color the region")
  if dialog.run == Gtk::Dialog::RESPONSE_OK
    bounds = buffer.selection_bounds
    color = dialog.colorsel.current_color
    tag_name = color.to_a.inspect
    unless tag = buffer.tag_table.lookup(tag_name)
      tag = Gtk::TextTag.new(tag_name).set_foreground_gdk(color)
    end
    buffer.tag_table.add(tag)
    buffer.apply_tag(tag, bounds[0], bounds[1])
  end
  dialog.destroy  
end

toolbar.append(Gtk::Stock::SELECT_FONT, "Set a font to the region") do
  dialog = Gtk::FontSelectionDialog.new("Set font to the region")
  if dialog.run == Gtk::Dialog::RESPONSE_OK
    bounds = buffer.selection_bounds
    font = dialog.font_name
    unless tag = buffer.tag_table.lookup(font)
      tag = Gtk::TextTag.new(font).set_font(font)
    end
    buffer.tag_table.add(tag)
    buffer.apply_tag(tag, bounds[0], bounds[1])
  end
  dialog.destroy  
end

toolbar.append_space
toolbar.append(Gtk::Stock::QUIT, "Quit this application") do
  Gtk.main_quit
end

vbox.pack_start(toolbar, false, false).add(Gtk::ScrolledWindow.new.add(textview))
window.add(vbox)
window.show_all
window.signal_connect("destroy") { Gtk.main_quit }

Gtk.main

__END__
This is a sample script for rich text serialization/deserialization.

1. Edit this text using font/color buttons.
2. Click save button and save this text (= serialize this as rich text to a file)
3. Click clear button and clear this text buffer.
4. Click load button and load the file which you save (= deserialize this).