#!/usr/bin/env python

# Taken and modified from
# PyLan - Python + PyGTK todo list
# Copyright (C) 2007 Mark Mruss <selsine@gmail.com>
# http://www.learningpython.com
#
# 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 Library 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.
#
# URL: http://www.learningpython.com
_ = lambda x : x

try:
  import pygtk
  pygtk.require("2.0")
except ImportError, e:
  print "Error: pygtk 2.0 required", e
  sys.exit(1)


try:
  import os
  import gtk
except ImportError, e:
  print "Import error in helper:", e
  sys.exit(1)



def gui_ask_pass_func(context, prompt):
  dialog = gtk.Dialog("Password", None, 0,
                      (gtk.STOCK_OK, gtk.RESPONSE_OK,
                       gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))

  hbox = gtk.HBox(False, 8)
  hbox.set_border_width(8)
  dialog.vbox.pack_start(hbox, False, False, 0)

  stock = gtk.image_new_from_stock(
          gtk.STOCK_DIALOG_QUESTION,
          gtk.ICON_SIZE_DIALOG)
  hbox.pack_start(stock, False, False, 0)

  table = gtk.Table(2, 1)
  table.set_row_spacings(4)
  table.set_col_spacings(4)
  hbox.pack_start(table, True, True, 0)

  label = gtk.Label("Enter Root password")
  label.set_use_underline(True)
  table.attach(label, 0, 1, 0, 1)
  lentry1 = gtk.Entry()
  lentry1.set_visibility(False)
  table.attach(lentry1, 1, 2, 0, 1)

  dialog.show_all()

  response = dialog.run()

  if response == gtk.RESPONSE_OK:
    st= lentry1.get_text()
  else:
    st= ""
  dialog.destroy()
  return st



def show_question_dlg(question_string, w_parent=None):
  qtn_dlg = gtk.MessageDialog(type=gtk.MESSAGE_QUESTION, message_format=question_string, buttons=gtk.BUTTONS_YES_NO
                               )
  response= qtn_dlg.run()
  qtn_dlg.destroy()
  return response

def show_info_dlg(info_string):
  """This Function is used to show an info dialog.  
  info_string -  The info string that will be displayed on the dialog.
  """
  info_dlg = gtk.MessageDialog(type=gtk.MESSAGE_INFO
				, message_format=info_string
				, buttons=gtk.BUTTONS_OK)
  info_dlg.run()
  info_dlg.destroy()

def show_error_dlg(error_string):
  """This Function is used to show an error dialog when an error occurs.  error_string -
  The error string that will be displayed on the dialog.
  """
  error_dlg = gtk.MessageDialog(type=gtk.MESSAGE_ERROR
				, message_format=error_string
				, buttons=gtk.BUTTONS_OK)
  error_dlg.run()
  error_dlg.destroy()



# pygtk version: Maik Hertha <maik.hertha@berlin.de>
class EntryCompletion(gtk.Dialog):
  '''Entry Completion
  
  GtkEntryCompletion provides a mechanism for adding support for completion in GtkEntry.
  '''
  listwords=["MikTeX"]
  search_string=""

  def __init__(self, parent=None):
    gtk.Dialog.__init__(self, self.__class__.__name__, parent,
                        0,
                        (gtk.STOCK_CANCEL, gtk.RESPONSE_NONE
                         ,gtk.STOCK_OK, gtk.RESPONSE_OK)
                        )
    try:
      self.set_screen(parent.get_screen())
    except AttributeError:
      self.connect('destroy', lambda *w: gtk.main_quit())
#     self.connect("response", lambda d, r: d.destroy())
    self.connect("response", self.on_ok_pressed)
    self.set_resizable(True)

    vbox = gtk.VBox(False, 5)
    self.vbox.pack_start(vbox, True, True, 0)
    vbox.set_border_width(5)
    
    label = gtk.Label()
    label.set_markup("Enter <b>keyword</b> to search")
    vbox.pack_start(label, False, False, 0)
    
        # Create our entry
    entry = gtk.Entry()
    vbox.pack_start(entry, False, False, 0)
    self.entry=entry
# Create the completion object
    completion = gtk.EntryCompletion()

        # Assign the completion to the entry
    self.entry.set_completion(completion)
    
        # Create a tree model and use it as the completion model
    completion_model = self.__create_completion_model()
    completion.set_model(completion_model)
    
        # Use model column 0 as the text column
    completion.set_text_column(0)
    self.show_all()
    self.run()


  def on_ok_pressed(self, widget, response):
    if response == gtk.RESPONSE_OK:
      self.search_string= self.entry.get_text()
#       print self.search_string
      self.listwords.append(self.search_string)
    self.hide()
    return response,self.search_string

  def __create_completion_model(self):
    ''' Creates a tree model containing the completions.
    '''
    store = gtk.ListStore(str)
    
        # Append new word
    for palabra in self.listwords:
      iter = store.append()
      store.set(iter, 0, palabra)
    
    return store


def main():
#   entry=EntryCompletion()
#   gtk.main()
#   gtk.main.quit()
  response= show_question_dlg('Queres?')
  if response == gtk.RESPONSE_OK:
    print 'Ok pressed'
  else:
    print 'Cancel pressed'
  return

if __name__ == '__main__':
    main()

