# vim:set et sts=4 sw=4:
# -*- coding: utf-8 -*-
#
# ibus - The Input Bus
#
# Copyright (c) 2014 Peng Huang <shawn.p.huang@gmail.com>
# Copyright (c) 2014 Takao Fujiwara <takao.fujiwara1@gmail.com>
# Copyright (c) 2013-2014 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

# This file is ported from
# gnome-control-center/panels/region/cc-input-chooser.c

from gi.repository import Gtk
from gi.repository import Gdk

import gettext
import locale

import config

_ = lambda a : gettext.dgettext(config.gettext_package, a)
N_ = lambda a: a

class AutotextDialog(Gtk.Dialog):
    __gtype_name__ = 'AutotextDialog'

    def __init__(self, transient_for):
        super(AutotextDialog, self).__init__(
                transient_for = transient_for,
                resizable = False)
        self.transient_for = transient_for
        buttons = (_("_Cancel"), Gtk.ResponseType.CANCEL,
                        _("_OK"), Gtk.ResponseType.OK)
        self.add_buttons(*buttons)
        self.set_title(_("Set an autotext"))
        self.set_response_sensitive(Gtk.ResponseType.OK, False)

        self.__key_box = Gtk.Box(orientation = Gtk.Orientation.HORIZONTAL)
        self.__autotext_box = Gtk.Box(orientation = Gtk.Orientation.HORIZONTAL)
        self.__comment_box = Gtk.Box(orientation = Gtk.Orientation.HORIZONTAL)

        self.__key_entry = Gtk.Entry()
        self.__key_entry.set_text('')
        self.__key_entry.set_editable(False)
        self.__key_entry.connect("preedit-changed", self.on_entry_changed)
        button = Gtk.Button()
        button.set_label('...')
        button.connect("clicked", self.set_key)
        self.__key_in_box = Gtk.Box(orientation = Gtk.Orientation.HORIZONTAL)
        self.__key_in_box.pack_start(self.__key_entry, False, True, 0)
        self.__key_in_box.pack_start(button, False, True, 0)
        self.__key_in_box.set_spacing(6)
        label = Gtk.Label()
        label.set_text(_('_Key:'))
        label.set_width_chars(10)
        label.set_use_underline(True)
        label.set_mnemonic_widget(button)
        self.__key_box.pack_start(label, False, True, 0)
        self.__key_box.pack_start(self.__key_in_box, False, True, 0)
        self.__key_box.set_spacing(6)

        self.__autotext_entry = Gtk.Entry()
        self.__autotext_entry.set_text('')
        self.__autotext_entry.set_editable(True)
        self.__autotext_entry.connect("preedit-changed", self.on_entry_changed)
        label = Gtk.Label()
        label.set_text(_('_Autotext:'))
        label.set_width_chars(10)
        label.set_use_underline(True)
        label.set_mnemonic_widget(self.__autotext_entry)
        self.__autotext_box.pack_start(label, False, True, 0)
        self.__autotext_box.pack_start(self.__autotext_entry, False, True, 0)
        self.__autotext_box.set_spacing(6)

        self.__comment_entry = Gtk.Entry()
        self.__comment_entry.set_text('')
        self.__comment_entry.set_editable(True)
        self.__comment_entry.connect("preedit-changed", self.on_entry_changed)
        label = Gtk.Label()
        label.set_text(_('Co_mment:'))
        label.set_width_chars(10)
        label.set_use_underline(True)
        label.set_mnemonic_widget(self.__comment_entry)
        self.__comment_box.pack_start(label, False, True, 0)
        self.__comment_box.pack_start(self.__comment_entry, False, True, 0)
        self.__comment_box.set_spacing(6)
        #self.__comment_box.set_homogeneous(True)

        self.vbox.add(self.__key_box)
        self.vbox.add(self.__autotext_box)
        self.vbox.add(self.__comment_box)
        self.vbox.set_spacing(6)
        self.vbox.set_margin_left(6)
        self.vbox.set_margin_right(6)
        self.vbox.set_margin_top(6)
        self.vbox.set_margin_bottom(6)
        self.show_all()

    def get_autotext(self):
        return (self.__key_entry.get_text(), \
                        self.__autotext_entry.get_text(), \
                        self.__comment_entry.get_text())

    def set_key(self, widget):
        self.__key_str = ''
        self.__dialog = Gtk.MessageDialog(transient_for = self)
        buttons = (_("_Cancel"), Gtk.ResponseType.CANCEL,
                        _("_Ok"), Gtk.ResponseType.OK)
        self.__dialog.add_buttons(*buttons)
        self.__dialog.set_markup(_("Press any key which you want to use as hotkey. \n"
                "The key you pressed is displayed below.\n"
                "If you want to use it, click \"Ok\" or click \"Cancel\""))

        self.__dialog.format_secondary_markup(" ")

        self.__dialog.connect("key-press-event", self.on_keypress)
        response = self.__dialog.run()
        if response == Gtk.ResponseType.OK:
            self.__key_entry.set_text(self.__key_str)
            if self.__key_entry.get_text() and \
                    self.__autotext_entry.get_text():
                self.set_response_sensitive(Gtk.ResponseType.OK, True)
        self.__dialog.destroy()

    def on_keypress(self, widget, event, data = None):
        # space_bar, enter, tab, arrow [ up, left, right, down]
        #if event.hardware_keycode in [23, 36, 65, 111, 113, 114, 116]:
        if Gdk.keyval_name(event.keyval) not in config.AutotextKeyList:
            return

        self.__key_str = ""
        self.__key_str = Gdk.keyval_name(event.keyval)
        self.__dialog.format_secondary_markup(\
                                            '<span size="large" weight="bold">%s</span>' \
                                            % self.__key_str)

    def on_entry_changed(self, widget, preedit, data = None):
        if self.__key_entry.get_text() and \
                self.__autotext_entry.get_text():
            self.set_response_sensitive(Gtk.ResponseType.OK, True)
