# Copyright (c) 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.

"""
Event View : messages received from Narval
"""

__revision__ = "$Id: EventView.py,v 1.8 2001/11/29 13:41:50 syt Exp $"

from gtk import GtkHBox, GtkCList, GtkScrolledWindow, POLICY_AUTOMATIC
from EngineModel import EngineModel
from narval.gui import unicode_to_gtk

class EventView(GtkHBox):

    _DICT_ = {}

    def __init__(self):
        self.__dict__ = self._DICT_
        if not self.__dict__ :
            GtkHBox.__init__(self)
            self._event_queue_list = GtkCList(2,[unicode_to_gtk(_('Id')),unicode_to_gtk(_('Event'))])
            self._event_queue_list.set_column_auto_resize(0,1)
            self._event_queue_list.set_column_auto_resize(1,1)
            scroll = GtkScrolledWindow()
            scroll.add(self._event_queue_list)
            scroll.set_policy(POLICY_AUTOMATIC,POLICY_AUTOMATIC)
            self.pack_start(scroll)
            # link to model
            EngineModel().attach_observer(self)
            # get color from PrefModel
            from PrefModel import PrefModel
            pm = PrefModel()
            pm.attach_observer(self)
            self._popped_colour = pm.messages_style['popped']['gtkcolor']
            self._pushed_colour = pm.messages_style['pushed']['gtkcolor']

    ## Observer interface #####################################################
    def EngineModel_update(self, observable, changes):
        self._build_lists(observable.get_event_queue(),observable.get_event_queue_messages())

    def PrefModel_update(self, observable, changes):
        self._pushed_colour = observable.messages_style['pushed']['gtkcolor']
        self._popped_colour = observable.messages_style['popped']['gtkcolor']
        
    ## Private ################################################################
    def _build_lists(self, queue, message):
        self._event_queue_list.thaw()
        self._event_queue_list.clear()
        for i in range(len(queue)):
            elt = message[queue[i]]
            row = self._event_queue_list.append([queue[i],elt[0]])
            if elt[1]:
                self._event_queue_list.set_background(row, self._popped_colour)
            else:
                self._event_queue_list.set_background(row, self._pushed_colour)            
        #self._event_queue_list.freeze()
