#! /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.
#
# Note: as the application is very simple, it is not encapsulated
# in a class, but rather coded in a 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 4) 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 window manager

# 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.
  """

  # A global variable which assigns a unique (sequence)
  # number to opened windows. Closed window numbers are
  # not recycled. 
  # In a pure OO implementation, application.py would
  # be encapsulated in a class. numObservers would then
  # be a class variable.
  # Note how there is some redundancy (for efficiency reasons)
  # numObservers can be obtained as len(observers)
  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=15, width=10,
                                 cellHeight=18, cellWidth=50,
				 padHor=10, padVer=40)
  # Add to the list of observers
  # This list will be used to notify() observers by 
  # sending each of them an update() message.
  # For efficient updating, we also keep track of 
  # the extent of each observer. 
  observers.append((gridView, 15, 10))

def addPlotView():
  """
  Creates a new PlotView window. 
  """

  # A global variable which assigns a unique (sequence)
  # number to opened windows. Closed window numbers are
  # not recycled. 
  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, rows, columns))

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

  # create a single SpreadsheetData instance (assignment 4)
  #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:
   root.mainloop()          # The main Tkinter event loop
  except KeyboardInterrupt: # catch SIGINT signal
   ctrlCQuit()
    
# End of file application.py

