# 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.

"""
Message View
"""

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

from gtk import *
from EngineModel import EngineModel
from PrefModel import PrefModel

class MessageView(GtkHBox):
    """
    View of Engine Model : list of messages from Narval
    Use singleton pattern
    """

    _DICT_ = {}

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

        if not self.__dict__ :
            GtkHBox.__init__(self)
            # link to model
            EngineModel().attach_observer(self)
            #PrefModel().attach_observer(self)
            # remenber length of list
            self._length = 0

            self._text = GtkText()
            scroll = GtkVScrollbar(self._text.get_vadjustment())
            self.pack_start(self._text)
            self.pack_start(scroll,expand=FALSE)

    def EngineModel_update(self,observable,changes):
        # dont need to rewrite old messages
        if self._length < len(observable.get_messages_list()):
            for i in range(self._length,len(observable.get_messages_list())):
                self._text.thaw()
                self._text.insert_defaults(observable.get_messages_list()[i])
                self._text.freeze()
            self._length = len(observable.get_messages_list())
            self._text.set_point(self._text.get_length())
        elif len(observable.get_messages_list())==0:
            self._text.thaw()
            self._text.delete_text(0,self._text.get_length())
            self._text.freeze()
            self._length = 0
