# Copyright (c) 2000-2001 LOGILAB S.A. (Paris, FRANCE).
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# 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.
__revision__ = "$Id: PrefController.py,v 1.10 2002/01/11 16:39:36 zoe Exp $"

from PrefModel import PrefModel
from gtk import *
try:
    from xml.xpath import Evaluate
except:
    from Ft.Xml.XPath import Evaluate
from xml.dom.ext import Print,PrettyPrint

from narval.lib import *
from narval.gui import unicode_to_gtk

class PrefController:
    """
    Controller of user preferences using singleton pattern
    """

    _DICT_ = {}

    def __init__(self):
        self.__dict__ = self._DICT_

        if not self.__dict__ :
            # intialisation
            self._type = 'step'
            self._state = {}
            self.pmodel = PrefModel()
            self.default()
    
    def save_preferences(self, *args):
        self.pmodel.save_preferences()

    def default(self):
        # see PrefView default values
        self._state['step'] = 'history'
        self._state['transition'] = 'wait-time'
        self.pmodel.update_sample_from_menu(self._type,self._state[self._type])

    # style callbacks #########################################################
    def type_selection_changed(self,*args):
        self._type = args[0].get_data('type')
        args[3].remove_menu()
        if self._type == 'step':
            args[3].set_menu(args[1])
        else:
            args[3].set_menu(args[2])            
        self.pmodel.update_sample_from_menu(self._type,self._state[self._type])
        args[3].show_all()

    # state selection callback ################################################
    def state_selection_changed(self,*args):
        self._state[self._type] = args[0].get_data('state')
        self.pmodel.update_sample_from_menu(self._type,self._state[self._type])

    # choose color callbacks ##################################################
    def choose_color(self, btn, key):
        import string
        if key == 'font-color':
            cc = self.pmodel.current_style['gtkstyle'].fg[STATE_NORMAL]
            title = unicode_to_gtk(_("Font color"))
        elif key == 'border-color':
            cc =  self.pmodel.current_style['gtkstyle'].dark[STATE_NORMAL]
            title = unicode_to_gtk(_("Border color"))
        elif key == 'background':
            cc = self.pmodel.current_style['gtkstyle'].bg[STATE_NORMAL]
            title = unicode_to_gtk(_("Background color"))
        elif key == 'popped-color':
            cc = self.pmodel.messages_style['popped']['gtkcolor']
            title = unicode_to_gtk(_("Popped color"))
        elif key == 'pushed-color':
            cc = self.pmodel.messages_style['pushed']['gtkcolor']
            title = unicode_to_gtk(_("Pushed color"))
        else:
            cc = None
        cs = GtkColorSelectionDialog(title)
        color_channel = (float(cc.red)/65535,float(cc.green)/65535,float(cc.blue)/65535,-1)
        cs.colorsel.set_color(color_channel)
        cs.set_position(WIN_POS_MOUSE)
        cs.show_all()
        cs.help_button.hide()
        cs.ok_button.connect_object('clicked',self.color_changed,cs,key)
        cs.cancel_button.connect('clicked',lambda x,y:y.destroy(),cs)

    def color_changed(self, selector, key):
        import string 
        color_channel = selector.colorsel.get_color()        
        red, green, blue = color_channel[0],color_channel[1],color_channel[2]
        tmpstyle = None
        cmap = selector.get_colormap()
        if key == 'font-color':
            tmpstyle = self.pmodel.current_style['gtkstyle'].copy()
            for i in range(5):
                tmpstyle.fg[i] = cmap.alloc(int(red*65535),int(green*65535),
                                            int(65535*blue))
        elif key == 'border-color':
            tmpstyle = self.pmodel.current_style['gtkstyle'].copy()
            for i in range(5):
                tmpstyle.dark[i] = cmap.alloc(int(red*65535),int(green*65535),
                                              int(65535*blue))
        elif key == 'background':
            tmpstyle = self.pmodel.current_style['gtkstyle'].copy()
            for i in range(5):
                tmpstyle.bg[i] = cmap.alloc(int(red*65535), int(green*65535),
                                            int(65535*blue))
        elif key == 'popped-color':
            self.pmodel.set_popped_color(cmap.alloc(int(red*65535),
                                                    int(green*65535),
                                                    int(65535*blue)))
        elif key == 'pushed-color':
            self.pmodel.set_pushed_color(cmap.alloc(int(red*65535),
                                                    int(green*65535),
                                                    int(blue*65535)))
        if tmpstyle:
            self.pmodel.current_style['gtkstyle'] = tmpstyle
            self.pmodel.update_sample_from_change(self._type,
                                                  self._state[self._type])
        selector.destroy()

    # choose font callback ####################################################
    def choose_font(self,btn,key):
        import string 
        fs = GtkFontSelectionDialog(unicode_to_gtk(_('Choose label font')))
        fs.set_font_name(self.pmodel.current_style['font'])
        fs.set_position(WIN_POS_MOUSE)
        fs.show_all()
        fs.apply_button.hide()
        fs.ok_button.connect_object('clicked',self.font_changed,fs,key)
        fs.cancel_button.connect('clicked',lambda x,y:y.destroy(),fs)

    def font_changed(self, selector, key):
        self.pmodel.set_font(selector.fontsel.get_font_name(), self._type,
                             self._state[self._type])
        selector.destroy()

    def maxchars_changed(self, *args):
        self.pmodel.set_maxchars(args[0].get_text())
        

    # destroy plan callback ###################################################
    def destroy_plan(self,*args):
        self.pmodel.set_destroy(args[0].get_active())
