~vcs-imports/kupfer/master-new

« back to all changes in this revision

Viewing changes to kupfer/uiutils.py

  • Committer: Ulrik Sverdrup
  • Date: 2009-10-26 21:35:29 UTC
  • Revision ID: git-v1:57375f89a2db6807ddea83f114e6de633f9da9d0
uiutils: User Interface Utility Functions

Implement a window for showing a text result, that plugins can use.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
User Interface Utility Functions for Kupfer
 
3
 
 
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
 
6
(default) thread.
 
7
"""
 
8
 
 
9
import gtk
 
10
import pango
 
11
 
 
12
from kupfer import config
 
13
 
 
14
def _window_destroy_on_escape(widget, event):
 
15
        """
 
16
        Callback function for Window's key press event, will destroy window
 
17
        on escape
 
18
        """
 
19
        if event.keyval == gtk.gdk.keyval_from_name("Escape"):
 
20
                widget.destroy()
 
21
                return True
 
22
 
 
23
def builder_get_objects_from_file(fname, attrs, autoconnect_to=None):
 
24
        """
 
25
        Open @fname with gtk.Builder and yield objects named @attrs
 
26
 
 
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
 
30
        """
 
31
        builder = gtk.Builder()
 
32
        try:
 
33
                import version_subst
 
34
        except ImportError:
 
35
                package_name = "kupfer"
 
36
        else:
 
37
                package_name = version_subst.PACKAGE_NAME
 
38
        builder.set_translation_domain(package_name)
 
39
 
 
40
        ui_file = config.get_data_file(fname)
 
41
        builder.add_from_file(ui_file)
 
42
        class Namespace (object):
 
43
                pass
 
44
        names = Namespace()
 
45
        for attr in attrs:
 
46
                obj = builder.get_object(attr)
 
47
                setattr(names, attr, obj)
 
48
                yield obj
 
49
        if autoconnect_to:
 
50
                builder.connect_signals(autoconnect_to, user_data=names)
 
51
 
 
52
def show_text_result(text, title=None):
 
53
        """
 
54
        Show @text in a result window.
 
55
 
 
56
        Use @title to set a window title
 
57
        """
 
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"):
 
61
                                widget.destroy()
 
62
                                return True
 
63
                def on_close_button_clicked(self, widget, names):
 
64
                        names.text_result_window.window.destroy()
 
65
                        return True
 
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)
 
72
 
 
73
        window, textview = builder_get_objects_from_file("result.ui",
 
74
                        ("text_result_window", "result_textview"),
 
75
                        autoconnect_to=ResultWindowBehavior())
 
76
 
 
77
 
 
78
        # Set up text buffer
 
79
        buf = gtk.TextBuffer()
 
80
        buf.set_text(text)
 
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)
 
88
 
 
89
        textview.set_buffer(buf)
 
90
        textview.set_wrap_mode(gtk.WRAP_NONE)
 
91
 
 
92
        if title:
 
93
                window.set_title(title)
 
94
 
 
95
        window.show_all()
 
96
 
 
97
        # Fix Sizing:
 
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()
 
103
 
 
104
        max_hsize, max_vsize = window.get_default_size()
 
105
        wid, hei = textview.size_request()
 
106
        textview.set_wrap_mode(gtk.WRAP_WORD)
 
107
 
 
108
        vsize = int(min(hei + (winhei - oldhei) + 5, max_vsize))
 
109
        hsize = int(min(wid + (winwid - oldwid) + 5, max_hsize))
 
110
 
 
111
        window.resize(hsize, vsize)
 
112
        window.present()
 
113