~ubuntu-branches/ubuntu/raring/gedit-plugins/raring-proposed

« back to all changes in this revision

Viewing changes to plugins/charmap/charmap/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Michael Biebl
  • Date: 2011-07-22 20:45:45 UTC
  • mfrom: (1.1.26 upstream)
  • mto: (7.3.1 sid) (1.4.8)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: james.westby@ubuntu.com-20110722204545-15t6eui58z5tdb6l
Tags: upstream-3.0.5
Import upstream version 3.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Copyright (C) 2006 Steve Frécinaux <steve@istique.net>
 
4
#               2010 Ignacio Casal Quinteiro <icq@gnome.org>
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2, or (at your option)
 
9
# any later version.
 
10
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
19
 
 
20
from gi.repository import GObject, Gio, Pango, Gtk, Gedit, Gucharmap
 
21
from panel import CharmapPanel
 
22
import sys
 
23
import gettext
 
24
from gpdefs import *
 
25
 
 
26
try:
 
27
    gettext.bindtextdomain(GETTEXT_PACKAGE, GP_LOCALEDIR)
 
28
    _ = lambda s: gettext.dgettext(GETTEXT_PACKAGE, s);
 
29
except:
 
30
    _ = lambda s: s
 
31
 
 
32
class CharmapPlugin(GObject.Object, Gedit.WindowActivatable):
 
33
    __gtype_name__ = "CharmapPlugin"
 
34
 
 
35
    window = GObject.property(type=Gedit.Window)
 
36
 
 
37
    def __init__(self):
 
38
        GObject.Object.__init__(self)
 
39
 
 
40
    def do_activate(self):
 
41
        self.editor_settings = Gio.Settings.new("org.gnome.gedit.preferences.editor")
 
42
        self.editor_settings.connect("changed::use-default-font", self.font_changed)
 
43
        self.editor_settings.connect("changed::editor-font", self.font_changed)
 
44
        self.system_settings = Gio.Settings.new("org.gnome.desktop.interface")
 
45
        self.system_settings.connect("changed::monospace-font-name", self.font_changed)
 
46
 
 
47
        panel = self.window.get_side_panel()
 
48
        image = Gtk.Image.new_from_icon_name("accessories-character-map", Gtk.IconSize.MENU)
 
49
 
 
50
        self.create_charmap_panel()
 
51
        panel.add_item(self.panel, "GeditCharmapPanel", _("Character Map"), image)
 
52
 
 
53
        statusbar = self.window.get_statusbar()
 
54
        self.context_id = statusbar.get_context_id("Character Description")
 
55
 
 
56
    def do_deactivate(self):
 
57
        panel = self.window.get_side_panel()
 
58
        panel.remove_item(self.panel)
 
59
 
 
60
    def do_update_state(self):
 
61
        self.panel.set_sensitive(len(self.window.get_documents()) >= 1)
 
62
 
 
63
    def get_document_font(self):
 
64
        if self.editor_settings.get_boolean("use-default-font"):
 
65
            font = self.system_settings.get_string("monospace-font-name")
 
66
        else:
 
67
            font = self.editor_settings.get_string("editor-font")
 
68
 
 
69
        return font
 
70
 
 
71
    def font_changed(self, settings=None, key=None):
 
72
        font = self.get_document_font()
 
73
        font_desc = Pango.font_description_from_string(font)
 
74
 
 
75
        chartable = self.panel.get_chartable()
 
76
        chartable.set_font_desc(font_desc)
 
77
 
 
78
    def create_charmap_panel(self):
 
79
        self.panel = CharmapPanel()
 
80
        chartable = self.panel.get_chartable()
 
81
 
 
82
        # Use the same font as the document
 
83
        self.font_changed()
 
84
 
 
85
        chartable.connect("notify::active-character", self.on_table_sync_active_char)
 
86
        chartable.connect("focus-out-event", self.on_table_focus_out_event)
 
87
        chartable.connect("status-message", self.on_table_status_message)
 
88
        chartable.connect("activate", self.on_table_activate)
 
89
 
 
90
        self.panel.show()
 
91
 
 
92
    def on_table_sync_active_char(self, chartable, pspec):
 
93
        uc = chartable.get_active_character()
 
94
        text = "%s %s" % (uc, Gucharmap.get_unicode_name(uc))
 
95
 
 
96
        a = Gucharmap.get_nameslist_equals(uc)
 
97
        if a:
 
98
            text += "   = %s" % a[0]
 
99
            for i in range(len (a) - 1):
 
100
                text += "; %s" % a[i + 1]
 
101
 
 
102
        a = Gucharmap.get_nameslist_stars(uc)
 
103
        if a:
 
104
            text += "   \342\200\242 %s" % a[0]
 
105
            for i in range(len (a) - 1):
 
106
                text += "; %s" % a[i + 1]
 
107
 
 
108
        self.on_table_status_message(chartable, text)
 
109
 
 
110
    def on_table_focus_out_event(self, chartable, event):
 
111
        self.on_table_status_message (chartable, None)
 
112
 
 
113
        return False
 
114
 
 
115
    def on_table_status_message(self, chartable, message):
 
116
        statusbar = self.window.get_statusbar()
 
117
 
 
118
        statusbar.pop(self.context_id)
 
119
 
 
120
        if message:
 
121
            statusbar.push(self.context_id, message)
 
122
 
 
123
    def on_table_activate(self, chartable):
 
124
        uc = chartable.get_active_character()
 
125
        if not Gucharmap.unichar_validate(uc):
 
126
            raise ValueError
 
127
 
 
128
        view = self.window.get_active_view()
 
129
        if not view or not view.get_editable():
 
130
            return
 
131
 
 
132
        document = view.get_buffer()
 
133
 
 
134
        document.begin_user_action()
 
135
        iters = document.get_selection_bounds()
 
136
        if iters:
 
137
            document.delete_interactive(iters[0], iters[1], view.get_editable())
 
138
 
 
139
        document.insert_interactive_at_cursor(uc, -1, view.get_editable())
 
140
 
 
141
        document.end_user_action()
 
142
 
 
143
# ex:et:ts=4: