import pygtk
# pygtk.require('2.0')
import gtk
import sys
import os
import gobject
import logger, project, workflow
import workflows

DATADIR = os.path.dirname(sys.modules['fluents'].__file__)
GLADEFILENAME = os.path.join(DATADIR, 'fluents.glade')

class CreateProjectDruid(gtk.Window):
    """A druid for creating a new project.

    The CreateProjectDruid gets a list of all classes derived from
    Workflow, and asks the user to select one of these. A new project of
    the selected class is added to the application."""
    
    def __init__(self, app):
        gtk.Window.__init__(self)
        self.app = app
        self.widget_tree = gtk.glade.XML(GLADEFILENAME, 'new_project_druid')
        self.workflows = self.make_workflow_list()
        self.selected = None
        
        renderer = gtk.CellRendererText()
        wf_name = gtk.TreeViewColumn('Workflow Name', renderer, text=0)
        self['workflow_list'].insert_column(wf_name, 0)

        self.wf_info = gtk.TextBuffer()
        self['workflow_info'].set_buffer(self.wf_info)

    def __getitem__(self, key):
        return self.widget_tree.get_widget(key)

    def make_workflow_list(self):
        store = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)
        for wf in workflow.workflow_list():
            store.insert_after(None, (wf.name, wf))
        return store
        
    def run(self):
        self['workflow_list'].set_model(self.workflows)

        self['druidpagestart1'].show()
        self['druidpagefinish1'].show()
        self['new_project_druid'].show()
 
        self['druidpagefinish1'].connect('finish', self.finish)
        self['workflow_list'].connect('cursor_changed', self.selection_updated)
        self['druid'].connect('cancel', self.cancel)
        self.connect('destroy', self.delete)

    def delete(self, widget):
        return False

    def hide(self):
        self['druidpagestart1'].hide()
        self['druidpagefinish1'].hide()
        self['new_project_druid'].hide()
        gtk.Window.hide(self)
        
    def finish(self, *rest):
        tree, it = self['workflow_list'].get_selection().get_selected()
        wf = self.workflows.get_value(it, 1)
        proj = project.Project()
        self.app.set_workflow(wf(self.app))
        self.app.set_project(proj)
        self.hide()
        self.destroy()

    def cancel(self, *ignored):
        self.hide()
        self.destroy()

    def selection_updated(self, *rest):
        tree, it = self['workflow_list'].get_selection().get_selected()
        wf = self.workflows.get_value(it, 1)
        self.wf_info.set_text(wf.description) 


def get_text(title, text):
    """Allow user to type in a string for text."""
    dlg = gtk.Dialog(title)
    dlg.show()

    text = gtk.Label(text)
    text.show()

    entry = gtk.Entry()
    entry.show()
    entry.set_activates_default(True)
    dlg.vbox.pack_start(text)
    dlg.vbox.pack_start(entry)

    dlg.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
    dlg.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
    dlg.set_default_response(gtk.RESPONSE_OK)
    response = dlg.run()

    retval = None

    if response == gtk.RESPONSE_OK:
        retval = entry.get_text()
    dlg.destroy()
    return retval
    
