2
User Interface Utility Functions for Kupfer
4
These helper functions can be called from plugins (are meant to serve this
5
purpose), but care should be taken to only call UI functions from the main
12
from kupfer import config
14
def _window_destroy_on_escape(widget, event):
16
Callback function for Window's key press event, will destroy window
19
if event.keyval == gtk.gdk.keyval_from_name("Escape"):
23
def builder_get_objects_from_file(fname, attrs, autoconnect_to=None):
25
Open @fname with gtk.Builder and yield objects named @attrs
27
@fname is sought in the data directories.
28
If @autoconnect_to is not None, signals are autoconnected to this object,
29
and a user_data object is passed as a namespace containing all @attrs
31
builder = gtk.Builder()
35
package_name = "kupfer"
37
package_name = version_subst.PACKAGE_NAME
38
builder.set_translation_domain(package_name)
40
ui_file = config.get_data_file(fname)
41
builder.add_from_file(ui_file)
42
class Namespace (object):
46
obj = builder.get_object(attr)
47
setattr(names, attr, obj)
50
builder.connect_signals(autoconnect_to, user_data=names)
52
def show_text_result(text, title=None):
54
Show @text in a result window.
56
Use @title to set a window title
58
class ResultWindowBehavior (object):
59
def on_text_result_window_key_press_event(self, widget, event, names):
60
if event.keyval == gtk.gdk.keyval_from_name("Escape"):
63
def on_close_button_clicked(self, widget, names):
64
names.text_result_window.window.destroy()
66
def on_copy_button_clicked(self, widget, names):
67
clip = gtk.clipboard_get(gtk.gdk.SELECTION_CLIPBOARD)
68
textview = names.result_textview
69
buf = textview.get_buffer()
70
buf.select_range(*buf.get_bounds())
71
buf.copy_clipboard(clip)
73
window, textview = builder_get_objects_from_file("result.ui",
74
("text_result_window", "result_textview"),
75
autoconnect_to=ResultWindowBehavior())
79
buf = gtk.TextBuffer()
81
monospace = gtk.TextTag("fixed")
82
monospace.set_property("family", "Monospace")
83
monospace.set_property("scale", pango.SCALE_LARGE)
84
beg, end = buf.get_bounds()
85
tag_table = buf.get_tag_table()
86
tag_table.add(monospace)
87
buf.apply_tag(monospace, beg, end)
89
textview.set_buffer(buf)
90
textview.set_wrap_mode(gtk.WRAP_NONE)
93
window.set_title(title)
98
# We want to size the window so that the
99
# TextView is displayed without scrollbars
100
# initially, if it fits on screen.
101
oldwid, oldhei = textview.window.get_size()
102
winwid, winhei = window.get_size()
104
max_hsize, max_vsize = window.get_default_size()
105
wid, hei = textview.size_request()
106
textview.set_wrap_mode(gtk.WRAP_WORD)
108
vsize = int(min(hei + (winhei - oldhei) + 5, max_vsize))
109
hsize = int(min(wid + (winwid - oldwid) + 5, max_hsize))
111
window.resize(hsize, vsize)