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

"""
Text View of the recipe
"""

__revision__ = "$Id: RecipeTextView.py,v 1.9 2002/01/04 14:13:05 syt Exp $"

from gtk import *
from RecipeModel import RecipeModel
from HornAbstraction import HORNElementsFactory #DOMReader, as_xml

TARGET_STRING = 1212

target = [
('STRING', 0, TARGET_STRING),
('text/plain', 0, TARGET_STRING)]

class RecipeTextView(GtkHBox):
    """
    View of recipe using text
    """
    
    def __init__(self,editable,controller):
        GtkHBox.__init__(self)
        ## record controller
        self._controller = controller
        ## text widget
        self._text = GtkText()
        self._text.connect('key-press-event',self._text_has_changed)
        ## drag and drop connection
        self.drag_dest_set(DEST_DEFAULT_ALL,target,GDK.ACTION_COPY)
        self.connect('drag_data_received',self._controller.received_drag_data)
        self.pack_start(self._text)
        self._text.set_editable(editable)
        ## scrollbar
        self._scroll = GtkVScrollbar(self._text.get_vadjustment())
        self.pack_start(self._scroll,expand = FALSE)
        ## reader
        self._f = HORNElementsFactory()
        
    def text_to_recipe(self):
        try:
    
            return self._f.element_from_string(self._text.get_chars(0,-1))
        except:
            print 'XML ERROR'
            import traceback
            traceback.print_exc()
            return None

    # Listeners ###############################################################
    def RecipeModel_update(self, observable, changes):
        self._text.delete_text(0,-1)
        self._text.insert_defaults(observable._recipe.as_xml())

    # Private #################################################################
    def _text_has_changed(self,*args):
        self._controller.has_changed = TRUE
