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
|
#! /usr/bin/python3
"""
A demo/test script for the XAppKbdLayoutController class
"""
import sys, os
import signal
import gettext
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('XApp', '1.0')
from gi.repository import Gtk, XApp, GObject
import cairo
signal.signal(signal.SIGINT, signal.SIG_DFL)
class Main:
def __init__(self):
win = Gtk.Window()
win.set_default_size(320, 200)
frame = Gtk.Frame()
frame.set_margin_start(2)
frame.set_margin_end(2)
frame.set_margin_top(2)
frame.set_margin_bottom(2)
win.add(frame)
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
box.set_margin_start(2)
box.set_margin_end(2)
box.set_margin_top(2)
box.set_margin_bottom(2)
frame.add(box)
self.use_caps = False
self.controller = XApp.KbdLayoutController()
self.controller.connect("layout-changed", self.on_layout_changed)
self.controller.connect("config-changed", self.on_config_changed)
self.label = Gtk.Label()
self.label.set_text(self.controller.get_current_name())
box.pack_start(self.label, True, True, 4)
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
box.pack_start(hbox, True, True, 4)
self.flag_button = Gtk.Button()
hbox.pack_start(self.flag_button, True, True, 4)
self.flag_button.connect("clicked", self.on_button_clicked)
self.group_button = Gtk.Button()
hbox.pack_start(self.group_button, True, True, 4)
self.group_button.connect("clicked", self.on_button_clicked)
self.variant_button = Gtk.Button()
hbox.pack_start(self.variant_button, True, True, 4)
self.variant_button.connect("clicked", self.on_button_clicked)
check = Gtk.CheckButton.new_with_label("Use caps")
check.connect("toggled", self.on_caps_toggled)
box.pack_start(check, True, True, 4)
frame.show_all()
win.connect("delete-event", lambda w, e: Gtk.main_quit())
self.on_layout_changed(self.controller)
win.present()
Gtk.main()
def on_caps_toggled(self, widget):
self.use_caps = widget.get_active()
self.on_layout_changed(self.controller)
def on_button_clicked(self, widget, data=None):
self.controller.next_group()
def on_layout_changed(self, controller, group=None):
handled = False
name = self.controller.get_current_icon_name()
if name != None:
filename = "/usr/share/iso-flag-png/%s.png" % name
if os.path.exists(filename):
valid, width, height = Gtk.IconSize.lookup(Gtk.IconSize.LARGE_TOOLBAR)
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
flag_surface = cairo.ImageSurface.create_from_png(filename)
cr = cairo.Context(surface)
cr.save()
factor = width / flag_surface.get_width()
true_width = flag_surface.get_width() * factor
true_height = flag_surface.get_height() * factor
x_offset = y_offset = 0
if flag_surface.get_width() >= flag_surface.get_height():
x_offset = 0
y_offset = ((height * (1 / factor)) - flag_surface.get_height()) / 2
else:
x_offset = ((width * (1 / factor)) - flag_surface.get_width()) / 2
y_offset = 0
true_x_offset = (width - true_width) / 2;
true_y_offset = (height - true_height) / 2;
cr.scale(factor, factor);
cr.set_source_surface(flag_surface, x_offset, y_offset);
cr.get_source().set_filter(cairo.FILTER_BEST);
cr.set_operator(cairo.OPERATOR_SOURCE);
cr.paint();
cr.restore()
XApp.KbdLayoutController.render_cairo_subscript(cr,
true_x_offset + (true_width / 2),
true_y_offset + (true_height / 2),
true_width / 2,
true_height / 2,
self.controller.get_current_flag_id())
image = Gtk.Image.new_from_surface(cr.get_target())
self.flag_button.set_image(image)
handled = True
else:
print("Missing /usr/share/iso-flag-png flag image - text only")
if not handled:
name = self.controller.get_current_short_group_label()
if self.use_caps:
self.flag_button.set_label(name.upper())
else:
self.flag_button.set_label(name)
self.flag_button.set_image(None)
self.label.set_text(self.controller.get_current_name())
group_label = self.controller.get_current_short_group_label()
variant_label = self.controller.get_current_variant_label()
if self.use_caps:
group_label = group_label.upper()
variant_label = variant_label.upper()
self.group_button.set_label(group_label)
self.variant_button.set_label(variant_label)
def on_config_changed(self, controller):
GObject.idle_add(self.on_layout_changed, controller)
if __name__ == "__main__":
main = Main()
|