# GNU Solfege - eartraining for GNOME
# Copyright (C) 2000, 2001, 2002, 2003  Tom Cato Amundsen
#
# 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

import gtk, gnome, gu
from htmlwidget import HtmlWidget
import dataparser

class SelectLessonfileWidget(gtk.Frame):
    """
    The class will parse call the teacher to parse the
    lessonfile because the widget need some data from the
    lessonfile header.
    """
    def __init__(self, teacher, win, change_cb):
        gtk.Frame.__init__(self, _("Lesson file"))
        self.m_t = teacher
        self.m_ui = win
        self.change_cb = change_cb
        vbox = gtk.VBox()
        vbox.set_border_width(gnome.ui.PAD)
        vbox.set_spacing(gnome.ui.PAD)
        self.add(vbox)
        # the box the optionmenus go into
        hbox = gu.bHBox(vbox, gtk.FALSE)
        hbox.set_homogeneous(gtk.TRUE)

        self.g_collection = gtk.OptionMenu()
        hbox.pack_start(self.g_collection)

        self.g_files = gtk.OptionMenu()
        hbox.pack_start(self.g_files)

        # descriptions
        hbox = gu.bHBox(vbox, gtk.FALSE)
        gu.bLabel(hbox, _("Title")+": ", gtk.FALSE)
        self.g_title = gu.bLabel(hbox, "", gtk.FALSE)

        self.g_toggle_description = gtk.ToggleButton(_("Details"))
        self.g_toggle_description.connect('clicked', self.toggle_lessonfile_details)
        hbox.pack_end(self.g_toggle_description, gtk.FALSE, gtk.FALSE)
        self.g_description_frame = gtk.Frame()
        self.g_lessonfile_details = HtmlWidget(self.m_t.m_app.handle_href, None)
        self.g_description_frame.add(self.g_lessonfile_details)
        vbox.pack_start(self.g_description_frame)
        self.update()
        self._sc = self._sf = None
        self.connect_changed_signals()
        self._fw = self.m_t.add_watch('lessonfile', self._lessonfilewatch)
        self._cw = self.m_t.add_watch('lessoncollection', self._lessoncollectionwatch)
    def connect_changed_signals(self):
        assert self._sc == self._sf == None
        self._sc = self.g_collection.connect('changed', self.on_change_collection)
        self._sf = self.g_files.connect('changed', self.on_change_file)
    def disconnect_changed_signals(self):
        assert self._sc
        assert self._sf
        self.g_collection.disconnect(self._sc)
        self.g_files.disconnect(self._sf)
        self._sc = self._sf = None
    def _lessonfilewatch(self, s):
        self.do_change_file(self.m_t.get_string(s))
        v = self.m_t.m_app.lessonfile_manager.get_filenames(
           self.m_t.get_string('lessoncollection'), self.m_t.m_content_type
           )
        self.g_files.set_history(v.index(self.m_t.get_string(s)))
    def _lessoncollectionwatch(self, s):
        self.do_change_collection(self.m_t.get_string(s))
    def on_change_collection(self, widget=None):
        collection = self.m_t.m_app.lessonfile_manager.get_collections()[widget.get_history()]
        self.do_change_collection(collection)
    def do_change_collection(self, collection):
        self.disconnect_changed_signals()
        self.m_t.remove_watch('lessonfile', self._fw)
        self.m_t.remove_watch('lessoncollection', self._cw)
        try:
            self.m_t.set_lessoncollection(collection)
        except dataparser.DataparserException, e:
            self.m_ui.display_error_message(str(e))
            self.m_t.m_P = None
        self.update(1)
        self.change_cb()
        self._cw = self.m_t.add_watch('lessoncollection', self._lessoncollectionwatch)
        self._fw = self.m_t.add_watch('lessonfile', self._lessonfilewatch)
        self.connect_changed_signals()
    def on_change_file(self, widget):
        """This function is hooked up to the changed signal for the option menu
        """
        new_filename = self.m_t.m_app.lessonfile_manager.get_filenames(
           self.m_t.get_string('lessoncollection'), self.m_t.m_content_type
           )[widget.get_history()]
        self.do_change_file(new_filename)
    def do_change_file(self, new_filename):
        self.disconnect_changed_signals()
        self.m_t.remove_watch('lessonfile', self._fw)
        try:
            self.m_t.set_lessonfile(new_filename)
        except dataparser.DataparserException, e:
            self.m_ui.display_error_message(str(e))
            self.m_t.m_P = None
        self.update(2)
        self.change_cb()
        self._fw = self.m_t.add_watch('lessonfile', self._lessonfilewatch)
        self.connect_changed_signals()
    def update(self, level=0):
        """0: everything
           1: don't include collection combo
           2: only information label
        """
        if level == 0:
            if not self.g_collection.get_menu():
                menu = gtk.Menu()
                for coll in self.m_t.m_app.lessonfile_manager.get_collections():
                    menu.add(gtk.MenuItem(coll))
                self.g_collection.set_menu(menu)
                self.g_collection.set_history(self.m_t.m_app.lessonfile_manager.get_collections().index(self.m_t.get_string('lessoncollection')))
        if level <= 1:
            F = self.m_t.m_app.lessonfile_manager.get_filenames( \
                    self.m_t.get_string('lessoncollection'), self.m_t.m_content_type)
            if F:
                menu = self.g_files.get_menu()
                if menu:
                    self.g_files.remove_menu()
                    menu.destroy()
                menu = gtk.Menu()
                for lessonfile in F:
                    menu.add(gtk.MenuItem(lessonfile))
                self.g_files.set_menu(menu)
                menu.show_all()
                self.g_files.set_sensitive(gtk.TRUE)
                self.g_files.set_history(self.m_t.m_app.lessonfile_manager.get_filenames(self.m_t.get_string('lessoncollection'), self.m_t.m_content_type).index(self.m_t.get_string('lessonfile')))
            else:
                menu = self.g_files.get_menu()
                if menu:
                    self.g_files.remove_menu()
                    menu.destroy()
                self.g_files.set_sensitive(gtk.FALSE)
                self.g_title.set_text('')
                self.g_lessonfile_details.source("<html></html>")
        if not self.m_t.m_P:
            return
        # the info labels
        if self.m_t.m_P:
            self.g_title.set_text(self.m_t.m_P.header.title)
        else:
            self.g_title.set_text('')
        s = self.m_t.m_P.header.description
        if s:
            self.g_lessonfile_details.source("<html><body bgcolor=white>%s</body></html>" % s)
        else:
           self.g_lessonfile_details.source("<html></html>")
    def toggle_lessonfile_details(self, widget=None):
        if widget.get_active():
            self.g_description_frame.show()
        else:
            self.g_description_frame.hide()
    def show(self):
        self.show_all()
        self.g_description_frame.hide()
