#
# This file is part of GNU Enterprise.
#
# GNU Enterprise 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, or (at your option) any later version.
#
# GNU Enterprise 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 program; see the file COPYING. If not,
# write to the Free Software Foundation, Inc., 59 Temple Place
# - Suite 330, Boston, MA 02111-1307, USA.
#
# Copyright 2002-2003 Free Software Foundation
#
# FILE:
# Dialog.py
#
# DESCRIPTION:
#
# NOTES:
#

###
### TODO: This desparately needs to
###       subclass the new Frame class
###

#from gnue.common.apps import GDebug
from constants import *
import sio
from utility import _paintBorder
from ScreenContainer import ScreenContainer

class Dialog(ScreenContainer):
  """
    like a full screen container, but only a smaller portion of it.
    the user defines a screen region which makes up the dialog...
    they may only put controls within this region.
  """
  def __init__(self, ParentContainer, r1=0, c1=0, r2=0, c2=0, caption="",
                border=1, dropshadow=1, **properties):
    SIOInstance = ParentContainer.Screen()
    apply (ScreenContainer.__init__, (self, SIOInstance), properties)
    self.PARENT = ParentContainer
    self.scrnbuff = {}
    self.Controls = []
    self.SCREEN = SIOInstance
    self.STARTPOINT = 0
    self.DIALOGWINDOW = None
    self.CAPTION = caption
    self.R1 = r1
    self.R2 = r2
    self.C1 = c1
    self.C2 = c2
    self.DROPSHADOW = dropshadow
    self.BORDER = border
    self._ChildOffsetX = 2
    self._ChildOffsetY = 1

    # need to add some sanity checks for bad params here:
    #if r1 > 0 and r1 <= SIOInstance.MAXROW:
    self._Y = r1
    #else:
    #  self._Y = 0
    #if c1 > 0 and c1 <= SIOInstance.MAXCOL:
    self._X = c1
    #else:
    #  self._X = 0

    self._DialogWidth = c2 - c1
    self._DialogHeight = r2 - r1
    self.INSET = 0
    self.buffer = self.Screen().CaptureScreen(self.R1,
        self.C1, self.R2 + dropshadow + border*2, self.C2 + dropshadow + border*2)
    self.SetColor('DIALOG')

  def __del__(self):
     pass

  def AddDialogControl(self, TheControl):
    self.AddControl(TheControl)
    return TheControl

  def _PaintBorder(self):
    if not self.BORDER:
      return

    _paintBorder( self, self._Y,self._X, self._DialogHeight + 2,
      self._DialogWidth + 2, self.INSET, caption=self.CAPTION,
      color = self.COLOR,
      captionColor=self.SCREEN.colors.C_DIALOG_TITLE)

  def _PaintDropShadow(self):
    if not self.DROPSHADOW:
      return

    self.SCREEN.PrintAt( self.R2 + self.BORDER*2, self.C1 + 1,
           (self._DialogWidth + self.BORDER*2) * " ", self.SCREEN.colors.C_DROPSHADOW)
    for i in range(1,self._DialogHeight + self.BORDER*2):
      self.SCREEN.PrintAt( self.R1 + i, self.C2 + self.BORDER*2, " ", self.SCREEN.colors.C_DROPSHADOW)

  def RunDialog(self):
    self._PaintBorder()
    self._PaintDropShadow()
    t = self.Run()
    self.Screen().RestoreScreen(self._Y,self._X,
             self.R2 + self.DROPSHADOW + self.BORDER*2,
             self.C2 + self.DROPSHADOW + self.BORDER*2,
             self.buffer)
    self.SCREEN.Refresh()
    self.PARENT.Paint()
    return t

  def Height(self):
    return self.R2- self._Y

  def Width(self):
    return self.C2- self._X


