# -*- coding: utf-8 -*-

#  Copyright (C) 2011 - Ignacio Casal Quinteiro
#
#  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, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330,
#  Boston, MA 02111-1307, USA.

from gi.repository import GObject, Gtk
import re
import sys

from gettext import gettext as _

class KeyVal(GObject.Object):
    __gtype_name__ = "GeditAccelEditorKeyVal"

    def __init__(self, key, mods):
        GObject.Object.__init__(self)
        self.key = key
        self.mods = mods

    def get_key(self):
        return self.key

    def set_key(self, key):
        self.key = key

    def get_mods(self):
        return self.mods

    def set_mods(self, mods):
        self.mods = mods

class AccelEditor(Gtk.Dialog, Gtk.Buildable):
    __gtype_name__ = "GeditAccelEditorDialog"

    ACTION_COLUMN = 0
    SHORTCUT_COLUMN = 1

    def __init__(self):
        Gtk.Dialog.__init__(self)
        self.model = None
        self.treeview = None

    def __getitem__(self, key):
        return self.builder.get_object(key)

    def accel_set_func(self, column, cell, model, it, data):
        keyval = model.get_value(it, self.SHORTCUT_COLUMN)

        if not keyval:
            cell.set_property('visible', False)
        else:
            cell.set_property('visible', True)
            cell.set_property('accel-key', keyval.get_key())
            cell.set_property('accel-mods', keyval.get_mods())

    def change_keyval(self, path_str, accel_key, accel_mods):
        it = self.model.get_iter_from_string(path_str)

        keyval = self.model.get_value(it, self.SHORTCUT_COLUMN)
        keyval.set_key(accel_key)
        keyval.set_mods(accel_mods)
        print accel_mods

        self.model.row_changed(Gtk.TreePath(path_str), it)

    def on_accel_cleared(self, accel, path_str):
        self.change_keyval(path_str, 0, 0)

    def on_accel_edited(self, accel, path_str, accel_key, accel_mods, hw_keycode):
        self.change_keyval(path_str, accel_key, accel_mods)

    def do_parser_finished(self, builder):
        self.builder = builder
        self.model = self['accel_store']
        self.treeview = self['accel_editor']
        accel_column = self['shortcut-column']
        accel_render = self['shortcut-renderer']

        accel_column.set_cell_data_func(accel_render, self.accel_set_func, None)

        handlers_dic = {
            'on_accel_edited' : self.on_accel_edited,
            'on_accel_cleared' : self.on_accel_cleared }

        self.builder.connect_signals(handlers_dic)

        #add the accels to the treeview
        keyval = KeyVal(0, 0)
        self.model.append(None, ("test", keyval))

        self.treeview.set_model(self.model)
        self.treeview.expand_all()

    def do_response(self, resp):
        self.destroy()

def _quit(*args):
    Gtk.main_quit()

def main(args = []):

    builder = Gtk.Builder()
    builder.add_from_file('accelmap.ui')

    window = builder.get_object('accel_dialog')

    window.show_all()
    Gtk.main()

if __name__ == '__main__':
    main(sys.argv)

# ex:ts=4:et:
