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
|
# Copyright (c) 2015-2016 Ruby-GNOME2 Project Team
# This program is licenced under the same licence as Ruby-GNOME2.
#
=begin
= Theming/Multiple Backgrounds
Gtk themes are written using CSS. Every widget is build of multiple items
that you can style very similarly to a regular website.
=end
class CssMultiplebgsDemo
def initialize(main_window)
@window = Gtk::Window.new(:toplevel)
@window.screen = main_window.screen
@window.title = "Mutiple Backgrounds"
@window.transient_for = main_window
@window.set_default_size(400, 300)
container = Gtk::Overlay.new
container.add_events([:enter_notify_mask, :leave_notify_mask,
:pointer_motion_mask])
@window.add(container)
da = initialize_drawing_area
container.add(da)
button = initialize_bricks_button
container.add_overlay(button)
paned = Gtk::Paned.new(:vertical)
container.add_overlay(paned)
css = "/css_multiplebgs/css_multiplebgs.css"
@default_css = Gio::Resources.lookup_data(css)
# Need a filler so we get a handle
child = Gtk::Box.new(:vertical, 0)
paned.add(child)
initialize_text_buffer
container = Gtk::ScrolledWindow.new
paned.add(container)
child = Gtk::TextView.new(@text)
container.add(child)
initialize_provider
apply_style(@window, @provider)
end
def run
if !@window.visible?
@window.show_all
else
@window.destroy
end
@window
end
private
def apply_style(widget, provider)
style_context = widget.style_context
style_context.add_provider(provider, Gtk::StyleProvider::PRIORITY_USER)
return unless widget.respond_to?(:children)
widget.children.each do |child|
apply_style(child, provider)
end
end
def initialize_drawing_area
da = Gtk::DrawingArea.new
da.name = "canvas"
da.signal_connect "draw" do |widget, cr|
context = widget.style_context
Gtk.render_background(context, cr, 0, 0, widget.allocated_width,
widget.allocated_height)
Gtk.render_frame(context, cr, 0, 0, widget.allocated_width,
widget.allocated_height)
false
end
da
end
def initialize_bricks_button
button = Gtk::Button.new
button.add_events([:enter_notify_mask, :leave_notify_mask,
:pointer_motion_mask])
button.name = "bricks-button"
button.halign = :center
button.valign = :center
button.set_size_request(250, 84)
button
end
def initialize_text_buffer
@text = Gtk::TextBuffer.new
@text.create_tag("warning", "underline" => Pango::UNDERLINE_SINGLE)
@text.create_tag("error", "underline" => Pango::UNDERLINE_ERROR)
@text.text = @default_css
text_buffer_signal_connect_changed
end
def text_buffer_signal_connect_changed
@text.signal_connect "changed" do |buffer|
buffer.remove_all_tags(buffer.start_iter, buffer.end_iter)
modified_text = buffer.get_text(buffer.start_iter,
buffer.end_iter,
false)
begin
@provider.load_from_data(modified_text)
rescue
@provider.load_from_data(@default_css)
end
Gtk::StyleContext.reset_widgets
end
end
def initialize_provider
@provider = Gtk::CssProvider.new
@provider.load_from_data(@default_css)
provider_signal_connect_parsing_error
end
def provider_signal_connect_parsing_error
@provider.signal_connect "parsing-error" do |_css_provider, section, error|
start_i = @text.get_iter_at(:line => section.start_line,
:index => section.start_position)
end_i = @text.get_iter_at(:line => section.end_line,
:index => section.end_position)
tag = error == Gtk::CssProviderError::DEPRECATED ? "warning" : "error"
@text.apply_tag_by_name(tag, start_i, end_i)
end
end
end
|