# -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
#*************************************************************************
#
# Copyright 2000, 2012 LibreOffice contributors and/or their affiliates. All rights reserved.
#
# Copyright information: The source code of LibreOffice is licensed under the GNU Lesser General Public License
# <http://www.libreoffice.org/download/license/>
#
# "LibreOffice" and "The Document Foundation" are registered trademarks of their corresponding registered owners
# or are in actual use as trademarks in one or more countries.
# Their respective logos and icons are also subject to international copyright laws.
# Use thereof is explained in our <http://wiki.documentfoundation.org/TradeMark_Policy>
#
#*************************************************************************

# Use uno, which uses pyuno
import uno, unohelper
import time

# Use UNO listeners
from com.sun.star.awt import XActionListener

# Get some uno constants
from com.sun.star.awt.WindowClass import TOP, SIMPLE

uno_BUTTON_STANDARD  = uno.getConstantByName("com.sun.star.awt.PushButtonType.STANDARD")
uno_BUTTON_OK        = uno.getConstantByName("com.sun.star.awt.PushButtonType.OK")
uno_BUTTON_CANCEL    = uno.getConstantByName("com.sun.star.awt.PushButtonType.CANCEL")
uno_BUTTON_HELP      = uno.getConstantByName("com.sun.star.awt.PushButtonType.HELP")
uno_WINDOW_CLOSEABLE = uno.getConstantByName("com.sun.star.awt.WindowAttribute.CLOSEABLE")
uno_WINDOW_SHOW      = uno.getConstantByName("com.sun.star.awt.WindowAttribute.SHOW")
uno_WINDOW_BORDER     = uno.getConstantByName("com.sun.star.awt.WindowAttribute.BORDER")
uno_ALIGN_LEFT  = uno.getConstantByName("com.sun.star.awt.TextAlign.LEFT")
uno_ALIGN_RIGHT = uno.getConstantByName("com.sun.star.awt.TextAlign.RIGHT")
#unoPOSSIZE   = uno.getConstantByName("com.sun.star.awt.PosSize.POSSIZE") 


# Event handlers for the buttons
class ButtonListener(unohelper.Base, XActionListener):
    """Stops the DialogBox, sets the button label as returned value"""

    def __init__(self, caller):
        self.caller = caller

    def disposing(self, eventObject):
        pass

    def actionPerformed(self, actionEvent):
        button = actionEvent.Source
        self.caller.Response = button.Model.Label
        self.caller.Dialog.endExecute()
        return


# The dialog box
class DialogBox:
    """DialogBox with methods to add controls"""

    def __init__(self, XParentWindow, cxt):
        self.smgr = cxt.ServiceManager
        self.cxt  = cxt

        # UI Dialog object
        self.Dialog = None

        # List of Buttons on the dialog
        self.Buttons = []

        # List of opened Listeners
        self.lst_listeners={}

        self.Response = ""
        self.TabIdx = 0
        try:
            if XParentWindow is None:
                frame = desktop.getCurrentFrame()
                XParentWindow = frame.getContainerWindow()
            self.Parent  = XParentWindow
            self.Toolkit = XParentWindow.getToolkit()
        except:
            raise AttributeError, "Did not get a valid parent window"


    def startCreateDialog(self, x=50, y=50, w=150, h=120):
        """Start creating the Dialog User Interface"""
        dialogModel = self.smgr.createInstanceWithContext("com.sun.star.awt.UnoControlDialogModel", self.cxt)
        dialogModel.PositionX = x
        dialogModel.PositionY = y
        dialogModel.Width     = w
        dialogModel.Height    = h
        dialogModel.TabIndex  = self.TabIdx
        self.TabIdx += 1

        return dialogModel


    def addLabel(self, dlgModel, cName, cLabel, x=5, y=10, w=38, h=12):
        """Add a Label"""
        fixedLabel = dlgModel.createInstance("com.sun.star.awt.UnoControlFixedTextModel")
        fixedLabel.PositionX = x
        fixedLabel.PositionY = y
        fixedLabel.Width     = w
        fixedLabel.Height    = h
        fixedLabel.Label     = cLabel
        fixedLabel.Align     = uno_ALIGN_RIGHT
        dlgModel.insertByName(cName, fixedLabel)

        return fixedLabel


    def addButton(self, dlgModel, cName, cLabel, x=40, y=145, w=30, h=12):
        """Add a Button"""
        button = dlgModel.createInstance("com.sun.star.awt.UnoControlButtonModel")
        button.PositionX = x
        button.PositionY = y
        button.Width     = w
        button.Height    = h
        button.Label     = cLabel
        button.PushButtonType = uno_BUTTON_STANDARD
        button.DefaultButton  = True
        button.TabIndex       = self.TabIdx
        self.TabIdx += 1
        self.Buttons.append(button.Label)
        dlgModel.insertByName(cName, button)

        return button

        
    def addListBox(self, dlgModel, cName, itemsArray, x=45, y=50, w=90, h=12):
        """Add a List box or a Combo box"""
        listBox= dlgModel.createInstance("com.sun.star.awt.UnoControlListBoxModel")
        listBox.PositionX = x
        listBox.PositionY = y
        listBox.Width     = w
        listBox.Height    = h
        listBox.Dropdown  = False
        listBox.LineCount = 20
        listBox.Tag       = ""
        listBox.StringItemList = itemsArray
        listBox.Border    = 2
        listBox.TabIndex  = self.TabIdx
        self.TabIdx += 1
        dlgModel.insertByName(cName, listBox)

        return listBox


    def addEdit(self, dlgModel, cName, x=45, y=6, w=90, h=13):
        """Add a Edit field"""
        edit = dlgModel.createInstance("com.sun.star.awt.UnoControlEditModel")
        edit.PositionX = x
        edit.PositionY = y
        edit.Width     = w
        edit.Height    = h
        edit.Align     = uno_ALIGN_LEFT
        edit.ReadOnly  = False
        edit.Tabstop   = True
        edit.TabIndex  = self.TabIdx
        self.TabIdx += 1
        dlgModel.insertByName(cName, edit)

        return edit


    def addNumeric(self, dlgModel, cName, value, valMin, valMax, x=45, y=6, w=30, h=12):
        """Add a Edit field"""
        numField = dlgModel.createInstance("com.sun.star.awt.UnoControlNumericFieldModel")
        numField.PositionX = x
        numField.PositionY = y
        numField.Width     = w
        numField.Height    = h
        numField.Value     = value
        numField.ValueMin  = valMin
        numField.ValueMax  = valMax
        numField.StrictFormat    = True
        numField.DecimalAccuracy = 1
        numField.Align     =  uno_ALIGN_RIGHT
        numField.Tabstop   = True
        numField.TabIndex  = self.TabIdx
        self.TabIdx += 1
        dlgModel.insertByName(cName, numField)

        return numField


    def addCheckBox(self, dlgModel, cName, cLabel, nState, x=40, y=145, w=30, h=12):
        """Add a Button"""
        checkBox = dlgModel.createInstance("com.sun.star.awt.UnoControlCheckBoxModel")
        checkBox.PositionX = x
        checkBox.PositionY = y
        checkBox.Width     = w
        checkBox.Height    = h
        checkBox.Label     = cLabel
        checkBox.State     = nState
        checkBox.TabIndex  = self.TabIdx
        self.TabIdx += 1
        dlgModel.insertByName(cName, checkBox)

        return checkBox


    def finishCreateDialog(self, dlgModel, cTitle, x=100, y=100, w=200, h=300):
        """Complete creating the Dialog User Interface"""
        dialog = self.smgr.createInstanceWithContext("com.sun.star.awt.UnoControlDialog", self.cxt)

        if not dialog.getModel():
            dialog.setModel(dlgModel)

        dialog.setTitle(cTitle)

        rectBounds = uno.createUnoStruct("com.sun.star.awt.Rectangle")
        rectBounds.X      = x
        rectBounds.Y      = y
        rectBounds.Width  = w
        rectBounds.Height = h

        winDescriptor = uno.createUnoStruct("com.sun.star.awt.WindowDescriptor")
        winDescriptor.Type        = TOP
        winDescriptor.ParentIndex = -1
        winDescriptor.Bounds      = rectBounds

        toolkit = self.smgr.createInstanceWithContext("com.sun.star.awt.Toolkit", self.cxt)
        peer = toolkit.createWindow(winDescriptor)
        dialog.createPeer(toolkit, peer)

        return dialog


    def addListeners(self):
        """Add listeners to dialog"""
        nb = 0
        for buttonName in self.Buttons:
            nb += 1
            a_control = self.Dialog.getControl("Btn" + str(nb))
            the_listener = ButtonListener(self)
            a_control.addActionListener(the_listener)
            self.lst_listeners["Btn" + str(nb)] = the_listener

        return


    def removeListeners(self):
        """ remove listeners on exiting"""
        nb = 0
        for buttonName in self.Buttons:
            nb +=1
            a_control = self.Dialog.getControl("Btn" + str(nb))
            a_control.removeActionListener(self.lst_listeners["Btn" + str(nb)])

        return        

