# -*- coding: iso-8859-1 -*-
#
#-------------------------------------------------------------------------------
#                    Code_Saturne version 1.3
#                    ------------------------
#
#
#     This file is part of the Code_Saturne User Interface, element of the
#     Code_Saturne CFD tool.
#
#     Copyright (C) 1998-2007 EDF S.A., France
#
#     contact: saturne-support@edf.fr
#
#     The Code_Saturne User Interface 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.
#
#     The Code_Saturne User Interface 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 the Code_Saturne Kernel; if not, write to the
#     Free Software Foundation, Inc.,
#     51 Franklin St, Fifth Floor,
#     Boston, MA  02110-1301  USA
#
#-------------------------------------------------------------------------------

"""
This module defines the TreeNavigator class.
"""

#-------------------------------------------------------------------------------
# Library modules import
#-------------------------------------------------------------------------------


import Tix
from Tkconstants import *
import tkColorChooser


#-------------------------------------------------------------------------------
# Application modules import
#-------------------------------------------------------------------------------


from Common import *
import Toolbox
import Pages.WelcomePage
from XMLmodel import configureTree


#-------------------------------------------------------------------------------
# Class for the TreeNavigator generation, it's configure the appearence,
# and comportment of the Tree
#-------------------------------------------------------------------------------


class TreeNavigator(Tix.Tree):
    """
    This class is the builder of a tixTree. This Tree is
    the part of the GUI which allow to navigate
    in the different entry. For each entry there is a page
    to display. The Tree is located at the left part of the paned window.
    """
    def __init__(self, master):
        """
        Constructor of the TreeNavigator.
        The TreeNavigator is composed in four parts,
        the tree itself, a kind of base, and three subwidget:
        a hlist and two scrolled bars (named vsb and hsb)
        """
        self.master = master.fr2
        self.text = Toolbox.Text()
        self.leaves = Toolbox.CreateHlist()

        # Let's configure the appearance of the HList subwidget
        #
        self.tree = Tix.Tree(self.master.p1,
                             options='                        \
                             hlist.drawBranch "1"             \
                             hlist.wideSelection "1"          \
                             hlist.selectmode "single" '      )

        self.tree.pack(side=TOP, fill=BOTH, expand=1, padx=4, pady=6)

        self.tree.config(ignoreinvoke='0', scrollbar='auto', bd='1')
        self.tree.hlist.config(font=fH,
                               background=wm['treebackground'],
                               selectbackground=wm['treeselectcolor'])
        #self.tree.hlist.config(font=fH)

        self.loadHList(self.tree.hlist, self.leaves)

        self.tree.autosetmode()
        self.closeTreeFolder()

        # Popup Menu of Tree
        #
        t = self.text
        self.tree.pm = Tix.PopupMenu(self.tree.hlist, title=t.TREEPOPUP, state=NORMAL)
        self.tree.pm.bind_widget(self.tree.hlist)
        self.tree.pm.menu.add_separator()
        self.tree.pm.menu.add_command(label=t.OPENRUB, command=self.openTreeFolder, accelerator='F5')
        self.tree.pm.menu.add_command(label=t.CLOSERUB, command=self.closeTreeFolder, accelerator='F6')
        self.tree.pm.menu.add_separator()
        self.tree.pm.menu.add_command(label=t.WELCOME_PAGE, command=self.displayWelcomePage)
        self.tree.pm.menu.add_separator()
        self.tree.pm.menu.m1 = Tix.Menu(self.tree.pm.menu)
        self.tree.pm.menu.m1.add_command(label=t.BGCOLOR, command=self.setTreeBgColor)
        self.tree.pm.menu.add_cascade(label=t.OPTION, menu=self.tree.pm.menu.m1)

        self.master.bind_all("<F5>", self.openTreeFolder)
        self.master.bind_all("<F6>", self.closeTreeFolder)


    def loadHList(self, hlist, leaves):
        """
        This method is a part of the class constructor. Its purpose
        is to 'upload' the informations from our relational database
        contained in self.leaves and create the HList.
        """
        # '/' is the separator character we chose.
        # parent entryPath / child's name
        #
        hlist.configure(separator="/")

        for name, label in leaves.parents:
            hlist.add(name, text=label, itemtype=Tix.IMAGETEXT,
                      image=hlist.tk.call('tix', 'getimage', 'folder'))

        # hlist.tk.call('tix', 'getimage', 'file')
        #       ^^^^^^^
        #       call directly from 'tix' the function 'getimage' and the image 'file'
        #
        for child, parent, label in leaves.childs:
            key = parent + '/' + child
            hlist.add(key, text=label, itemtype=Tix.IMAGETEXT,
                      image=hlist.tk.call('tix', 'getimage', 'tree_white_file'))


    def addAttr(self, case):
        """
        This method allows to hang on an attribute (particularly the 'case')
        *after* the constructor call.
        """
        self.case = case


    def delAttr(self):
        """
        Destroy the 'case' attribute from the current TreeNavigator instance.
        """
        if hasattr(self, 'case'):
            delattr(self, 'case')


    def selectTreeEntry(self, entry):
        """
        When an error occurs, the selected Page is unselected and
        the currentPage is selected again.
        """
        self.tree.hlist.selection_clear()
        self.tree.hlist.selection_set(entry)



    def openSingleFolder(self, name):
        """
        Open a single folder of the Tree.
        """
        self.tree.open(name)
        self.tree.setmode(name, 'close')
        self.tree.hlist.item_configure(name, 0, \
             image=self.tree.tk.call('tix', 'getimage', 'act_fold'))


    def openTreeFolder(self, event=None):
        """
        Open all folders of the Tree.
        """
        for name in self.leaves.parents_key:
            self.openSingleFolder(name)

        if hasattr(self, 'case'):
            configureTree(self.tree, self.case)


    def closeSingleFolder(self, name):
        """
        Close all folders of the Tree.
        """
        self.tree.close(name)
        self.tree.setmode(name, 'open')
        self.tree.hlist.item_configure(name, 0, \
             image=self.tree.tk.call('tix', 'getimage', 'folder'))


    def closeTreeFolder(self, event=None):
        """
        Close a single folder of the Tree.
        """
        for name in self.leaves.parents_key:
            self.closeSingleFolder(name)


    def setWhiteFiles(self):
        """
        Color in white all icons file for child entry.
        """
        for name in self.leaves.childs_key:
            self.tree.hlist.item_configure(name, 0, \
                image=self.tree.tk.call('tix', 'getimage', 'tree_white_file'))


    def setBlankFile(self, entry):
        """
        Color in white the file icon for child entry.
        """
        self.tree.hlist.item_configure(entry, 0, \
              image=self.tree.tk.call('tix', 'getimage', 'tree_white_file'))


    def setGreenFile(self, entry):
        """
        Color in green the file icon for child entry.
        """
        self.tree.hlist.item_configure(entry, 0, \
              image=self.tree.tk.call('tix', 'getimage', 'tree_green_file'))


    def setRedFile(self, entry):
        """
        Color in red the file icon for child entry.
        """
        self.tree.hlist.item_configure(entry, 0, \
              image=self.tree.tk.call('tix', 'getimage', 'tree_red_file'))


    def folderStatus(self, entry):
        """
        Change the image for the folder : depend of the parent
        status (open or close)
        """
        if entry in self.leaves.parents_key:
            mode = self.tree.getmode(entry)

            if mode == 'open':
                self.tree.hlist.item_configure(entry, 0, \
                     image=self.tree.tk.call('tix', 'getimage', 'folder'))
                self.tree.setmode(entry, 'open')
            else:
                self.tree.hlist.item_configure(entry, 0, \
                     image=self.tree.tk.call('tix', 'getimage', 'act_fold'))
                self.tree.setmode(entry, 'close')


    def displayWelcomePage(self, event=None):
        """
        This calls back the Welcome Page for the Popup Menu.
        """
        for w in self.master.p2.fr.window.winfo_children():
            w.destroy()

        self.master.p2.fr.window.myPage = \
             Pages.WelcomePage.WelcomePage(self.master.p2.fr.window)


    def setTreeBgColor(self, event=None):
        """
        Choose and set a background color from a GUI color chooser.
        """
        t = self.text
        cur_col = self.tree.hlist.cget('background')
        new_col = tkColorChooser.askcolor(title=t.BGCOLOR, initialcolor=cur_col)
        if new_col[1] != None:
            self.tree.hlist.configure(bg=new_col[1])
            self.tree.pm.menu.configure(bg=new_col[1])
            self.tree.pm.menu.m1.configure(bg=new_col[1])
            wm['treebackground'] = new_col[1]


#-------------------------------------------------------------------------------
# End of Tree
#-------------------------------------------------------------------------------


syntax highlighted by Code2HTML, v. 0.9.1