#! /usr/bin/env python2

# application.py
#
# Demonstrates the use of SSGridView.py.
# Multiple instances of a SpreadsheetGridView window are created.
# Currently, there is no link between the data in individual windows.
# This is used to build a simple spreadsheet GUI (without the
# processing of the data in the windows)
#
# Note: as the application is very simple, it is not encapsulated
# in a class, but rather coded in an non-OO (procedural encapsulation
# only) fashion. This makes the code simple, but not very re-usable.

# Hans Vangheluwe March 2002

# Required modules
import sys                     # for exit()
from Tkinter    import *       # basic Tkinter
from SSGridView import *       # the SpreadsheetGridView class

# Will be needed later (from assignment 1) when coupling the
# GridViews to spreadsheet data
#from CData      import *
#from CCoord     import *
#from SSheetDICT import *

# The application consists of one root window with 3 buttons
#  - Add GridView      : creates a new GridView window
#  - Add PlotView      : creates a new PlotView window 
#                        (empty in this prototype)
#  - Quit Application
#
# The application will also terminate cleanly when it receives
# a window-closed event from the windowmanager

# The callbacks

def wmQuit():
  """
  Handle root window close: exit() application
  """
  # we may wish to put cleanup actions here
  print "Spreadsheet Application was closed (by Window Manager)"
  sys.exit()

def rootQuit():
  """
  Handle Quit clicked in root window: exit() application
  """
  # we may wish to put cleanup actions here
  print "Spreadsheet Application was closed (by Quit in root window)"
  sys.exit()

def ctrlCQuit():
  """
  Handle SIGINT signal: exit() application
  """
  # we may wish to put cleanup actions here
  print "Spreadsheet Application was closed (by SIGINT signal)"
  sys.exit()

def addGridView():
  """
  Creates a new GridView window.
  """
  global numObservers   
  numObservers = numObservers + 1

  window = Toplevel(root)
  windowTitle= "Spreadsheet View " + str(numObservers)
  gridView = SpreadsheetGridView(master=window,
                                 title=windowTitle,
                                 data=theSubject, # not yet used
				 height=100, width=15,
                                 cellHeight=18, cellWidth=50,
				 padHor=10, padVer=40)
  # Add to list of observers
  observers.append(gridView)

def addPlotView():
  """
  Creates a new PlotView window. 
  """
  global numObservers
  numObservers = numObservers + 1

  window = Toplevel(root)
  windowTitle= "Spreadsheet View " + str(numObservers)

  # Not yet implemented
  # plotView = SpreadsheetPlotView(master=window, ...)
  #
  # # Add to list of observers
  # observers.append(plotView)

# The application
#
if __name__ == "__main__":

  # create a single SpreadsheetData instance (later)
  #theSubject = SpreadsheetData()
  theSubject = None 

  # root window
  root = Tk()
  root.protocol("WM_DELETE_WINDOW", wmQuit)
  root.title("Spreadsheet control")

  # Keep track of observers 
  observers = []   
  numObservers = 0 

  # Button to add a grid spreadsheet view 
  gridViewButton = Button(master=root,
                          text="Add GridView",
                          command=addGridView)
  gridViewButton.pack()

  # Button to add a plot spreadsheet view 
  plotViewButton = Button(master=root,
                          text="Add PlotView",
                          command=addPlotView)
  plotViewButton.pack()

  # Button to quit the application
  quitButton = Button(master=root,
                      text="Quit Application",
                      command=rootQuit)
  quitButton.pack()

  try:
   # The main Tkinter event loop
   root.mainloop()
  except KeyboardInterrupt: # catch SIGINT signal
   ctrlCQuit()
    
# End of file application.py

