#!/usr/bin/python

# -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
#
# Copyright 2012 LibreOffice contributors.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
#
# vim:set shiftwidth=4 softtabstop=4 expandtab:


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

import wizards.ui.UIConsts
from wizards.ui.UnoDialog2 import *

from wizards.common.SystemDialog import SystemDialog
from com.sun.star.awt.VclWindowPeerAttribute import OK, YES_NO, DEF_YES

uno_ALIGN_LEFT  = uno.getConstantByName("com.sun.star.awt.TextAlign.LEFT")
uno_ALIGN_RIGHT = uno.getConstantByName("com.sun.star.awt.TextAlign.RIGHT")

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")


# Those control properties that will be used in the "insert" methods
class MyProps():
    PROPNAMES_STANDARD = (
        PropertyNames.PROPERTY_HEIGHT,
        PropertyNames.PROPERTY_NAME,
        PropertyNames.PROPERTY_POSITION_X,
        PropertyNames.PROPERTY_POSITION_Y,
        PropertyNames.PROPERTY_STEP,
        PropertyNames.PROPERTY_TABINDEX,
        PropertyNames.PROPERTY_WIDTH)    

    PROPNAMES_LBL = (
        "Align",
        PropertyNames.PROPERTY_LABEL,) + PROPNAMES_STANDARD

    PROPNAMES_TXT = (
        PropertyNames.PROPERTY_HELPURL,) + PROPNAMES_STANDARD

    PROPNAMES_BUTTON = (
        PropertyNames.PROPERTY_LABEL,
        PropertyNames.PROPERTY_HELPURL,) + PROPNAMES_STANDARD

    PROPNAMES_CHKBOX = (
        "State",) + PROPNAMES_BUTTON

    PROPNAMES_LIST = (
        "Dropdown",
        "MultiSelection",
        "LineCount",
        PropertyNames.PROPERTY_HELPURL,) + PROPNAMES_STANDARD

    PROPNAMES_NUM = (
        "Value",
        "ValueMin",
        "ValueMax",
        "DecimalAccuracy",
        "Align",
        PropertyNames.PROPERTY_HELPURL,) + PROPNAMES_STANDARD


# Using the UnoDialog2
class CustomDialog(UnoDialog2):

    def __init__(self, xMSF):
        super(CustomDialog, self).__init__(xMSF)
        #set dialog properties...
        Helper.setUnoPropertyValues(self.xDialogModel,
            ("Closeable",
            PropertyNames.PROPERTY_HEIGHT,
            PropertyNames.PROPERTY_HELPURL, "Moveable",
            PropertyNames.PROPERTY_NAME,
            PropertyNames.PROPERTY_POSITION_X,
            PropertyNames.PROPERTY_POSITION_Y,
            PropertyNames.PROPERTY_STEP,
            PropertyNames.PROPERTY_TABINDEX, "Title",
            PropertyNames.PROPERTY_WIDTH),
            (True, 170, "", True,
            "CustomDialog", 102, 52, 1, 6,
            "Example Custom Dialog", 150))

    def showCustomDialog(self, xParentWindow, propSet):
        self.xDialogModel.BackgroundColor = 0xafd2cc
        self.createWindowPeer(xParentWindow)
        tabIndex = 1;

        self.btnOK = self.insertButton(
            "btnOK", "actionOK", propSet.PROPNAMES_BUTTON,
            ("OK", "", 14, "btnOK", 38, 145, 2, tabIndex + 1, 50), self)

        self.btnCancel = self.insertButton(
            "btnCancel", "actionCancel", propSet.PROPNAMES_BUTTON,
            ("Cancel", "", 14, "btnCancel", 91, 145, 2, tabIndex + 1, 50), self)

        self.lblComment = self.insertLabel(
            "lblComment", propSet.PROPNAMES_LBL,
            (uno_ALIGN_RIGHT, "Comment", 8, "lblComment", 3, 10, 1, tabIndex + 1, 38))

        self.txtComment = self.insertTextField(
            "txtComment", None, propSet.PROPNAMES_TXT,
            ("Give your comment", 12, "txtComment",
                45, 8, 6, tabIndex + 1, 95), self)

        self.lblGraphType = self.insertLabel(
            "lblGraphType", propSet.PROPNAMES_LBL,
            (uno_ALIGN_RIGHT, "Graph type", 8, "lblGraphType", 3, 27, 1, tabIndex + 1, 38))

        self.lstGraphType = self.insertListBox(
            "lstGraphType", None, None, propSet.PROPNAMES_LIST,
            (True, False, 5, "", 12, "lstGraphType", 45, 25, 1, tabIndex + 1, 52), self)
        self.lstGraphType.addItems(("Area", "Bar", "Line", "Donut", "Bubble"), 0)
        self.selectListBoxItem(self.lstGraphType, 1)
        itemChanged = getattr(self, "actionTypeChanged")
        self.lstGraphType.addItemListener(ItemListenerProcAdapter(itemChanged))

        self.lblOptions = self.insertLabel(
            "lblOptions", propSet.PROPNAMES_LBL,
            (uno_ALIGN_RIGHT, "Options", 8, "lblOptions", 3, 44, 1, tabIndex + 1, 38))

        self.lstOptions = self.insertListBox(
            "lstOptions", None, None, propSet.PROPNAMES_LIST,
            (False, True, 5, "", 52, "lstOptions", 45, 43, 1, tabIndex + 1, 52), self)
        self.lstOptions.addItems(("Item 1", "Item 2", "Item 3", "Item 4", "Item 5"), 0)

        self.lblStart = self.insertLabel(
            "lblStart", propSet.PROPNAMES_LBL,
            (uno_ALIGN_RIGHT, "Start", 8, "lblStart", 3, 108, 1, tabIndex + 1, 38))

        self.numStart = self.insertNumericField(
            "numStart", None, propSet.PROPNAMES_NUM,
            (25.1, 1, 60, 1, uno_ALIGN_RIGHT, "", 12, "numStart", 45, 106, 1, tabIndex + 1, 32), self)

        self.chkObvious = self.insertCheckBox(
            "chkObvious", None, propSet.PROPNAMES_CHKBOX,
            (1, "Obvious", "", 9, "chkObvious", 45, 125, 2, tabIndex + 1, 75), self)

        self.running = True
        rectBounds = uno.createUnoStruct("com.sun.star.awt.Rectangle")
        rectBounds.X      = 100
        rectBounds.Y      = 200
        rectBounds.Width  = 440
        rectBounds.Height = 200
        self.executeDialog(rectBounds)

    def actionTypeChanged(self):
        print "action combo GraphType changed"
        sMsg = str(self.chkObvious.getModel().Label)
        #print sMsg

    def actionOK(self):
        print "Action OK"
        answer = SystemDialog.showMessageBox(self.xMSF, "MessBox", YES_NO + DEF_YES, "Show results?",
            self.xUnoDialog.Peer)

        if answer != 3:
            # user said YES 
            sMsg = "Comment = " + self.txtComment.Text
            sMsg += "\nGraph type = " + self.lstGraphType.getSelectedItem()

            fieldnames = self.lstOptions.getSelectedItems()
            if fieldnames:
                for i in fieldnames:
                    sMsg += "\nOption = " + i

            sMsg += "\nStart = " + str(self.numStart.Value)

            if self.chkObvious.getState() == 1:
                sMsg += "\nThis is " + self.chkObvious.getModel().Label
            else:
                sMsg += "\nThis is not " + self.chkObvious.getModel().Label

            SystemDialog.showMessageBox(self.xMSF, "MessBox", OK, sMsg, self.xUnoDialog.Peer)

        self.xUnoDialog.endExecute()
        self.running = False

    def actionCancel(self):
        print "action Cancel"
        self.xUnoDialog.endExecute()
        self.running = False


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()
        customDlg = CustomDialog(cxt.ServiceManager)
        propset = MyProps()
        customDlg.showCustomDialog(xParentWindow, propset)
    else:
        print "frame not found"

    doc.close(True)


if __name__ == "__main__":
    testTheDialog()

