#!/usr/bin/python

# -*- 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

import dialogbox


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


# ## These 4 global objects must be moved!
ctx    = uno.getComponentContext()
res    = ctx.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver",ctx)
cxt    = res.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
desktop= cxt.ServiceManager.createInstance("com.sun.star.frame.Desktop")


###########################################################
# Using the dialogbox
#
def showDialogTest(xParent):
    box = dialogbox.DialogBox(xParent, cxt)

    # Constructing the dialog
    dModel = box.startCreateDialog(100, 100, 150, 168)
    dModel.BackgroundColor = 0xafd2cc
    btnOK = box.addButton(dModel, "Btn1", "OK", 30, 145)
    btnCancel = box.addButton(dModel, "Btn2", "Cancel", 84, 145)
    btnCancel.PushButtonType = dialogbox.uno_BUTTON_CANCEL
    lbl1 = box.addLabel(dModel, "Lbl1", "Comment",  3, 10)
    lbl2 = box.addLabel(dModel, "Lbl2", "Category", 3, 27)
    lbl3 = box.addLabel(dModel, "Lbl3", "Options",  3, 44)
    editA = box.addEdit(dModel, "EditA", 45, 6)
 
    # items for dropdown and list
    arrItems = ("Item 1", "Item 2", "Item 3", "Item 4", "Item 5")
    comboA = box.addListBox(dModel, "ComboA", arrItems, 45, 25)
    comboA.HelpText  = "This is an example dropdown box or combo box"
    comboA.Dropdown = True

    listA = box.addListBox(dModel, "ListA", arrItems, 45, 44, 90, 52)
    listA.HelpText  = "This is an example list box"
    listA.MultiSelection = True

    numericFld = box.addNumeric(dModel, "NumField1", 25.1, 1, 60, 45, 108, 30, 12)

    checkA = box.addCheckBox(dModel, "CheckBox1", "Awesome", 0, 45, 128, 50, 12)

    # Finish constructing the dialog
    box.Dialog = box.finishCreateDialog(dModel, "Example dialog with Python", 150, 263, 170, 261)

    # Select the 3th item of the dropdown-box        
    control_comboA = box.Dialog.getControl("ComboA")
    control_comboA.selectItemPos(2, True)
    
    box.addListeners()
    box.Dialog.execute()
    box.removeListeners()
    if box.Response:
        print "Botton clicked = " + box.Response

        print "Text field = " + editA.Text

        print "Combo-box selected item = " + control_comboA.getSelectedItem()

        iPos = listA.SelectedItems
        if iPos:
            for i in iPos:
                print "Selected list item = " + listA.StringItemList[i]

        print "Numeric field = " + str(numericFld.Value)

        if checkA.State == 1:
            print "This is " + checkA.Label
        else:
            print "This is not " + checkA.Label

    box.Dialog.dispose()


def makePropertyValue(cName, uValue, nHandle, nState):
    core = cxt.ServiceManager.createInstance("com.sun.star.reflection.CoreReflection")
    oXIdlClass = core.forName("com.sun.star.beans.PropertyValue")
    oRV,oStruct = oXIdlClass.createObject(None)
    oStruct.Name   = cName
    oStruct.Value  = uValue
    oStruct.Handle = nHandle
    oStruct.State  = nState
    return oStruct


def testTheDialog():
    props=tuple([makePropertyValue("ReadOnly", False, 0, 0),makePropertyValue("Hidden", True, 0, 0)])
    doc=desktop.loadComponentFromURL("private:factory/scalc", "_blank", 0, props)

    frame=doc.getCurrentController().getFrame()
    if frame:
        xParentWindow=frame.getContainerWindow()
        showDialogTest(xParentWindow)
    else:
        print "frame not found"

    doc.close(True)


if __name__ == "__main__":
    testTheDialog()

