# encoding: utf-8
# api: uno
# type: functions
# version: 0.2
# title: UNO helper
# description: compatibility between LO and AOO
#
# Just the shorter `PropertyValue` for now.
# And a convenience wrapper for TextFrames, to match the iterator interface.
#

import uno, unohelper
from com.sun.star.beans import PropertyValue as UNO_PropertyValue
from com.sun.star.awt.MessageBoxButtons import BUTTONS_OK, BUTTONS_OK_CANCEL, BUTTONS_YES_NO, BUTTONS_YES_NO_CANCEL, BUTTONS_RETRY_CANCEL, BUTTONS_ABORT_IGNORE_RETRY
from com.sun.star.awt.MessageBoxButtons import DEFAULT_BUTTON_OK, DEFAULT_BUTTON_CANCEL, DEFAULT_BUTTON_RETRY, DEFAULT_BUTTON_YES, DEFAULT_BUTTON_NO, DEFAULT_BUTTON_IGNORE
from com.sun.star.awt.MessageBoxType import MESSAGEBOX, INFOBOX, WARNINGBOX, ERRORBOX, QUERYBOX

# AOO uno version doesn't have constructor with name/value params
def PropertyValue(Name="nodepath", Value=None):
    pv = UNO_PropertyValue()
    pv.Name = Name
    pv.Value = Value
    return pv

# faux XEnumerationAccesss for XNamedAccess
class XNamedAsEnumeration:
    names = []

    def __init__(self, elements):
        self.elements = elements
        self.names = elements.getElementNames()
        if type(self.names) is tuple:
            self.names = list(self.names)
        else:
            self.names = []

    def hasMoreElements(self):
        return len(self.names) > 0

    def nextElement(self):
        return self.elements.getByName(self.names.pop(0))

# user notifications
def MessageBox(parent, MsgText, MsgTitle="", MsgType=MESSAGEBOX, MsgButtons=BUTTONS_OK):
    ParentWin = parent.document.getCurrentController().Frame.ContainerWindow
    ctx = uno.getComponentContext()
    sm = ctx.ServiceManager
    sv = sm.createInstanceWithContext("com.sun.star.awt.Toolkit", ctx)
    myBox = sv.createMessageBox(ParentWin, MsgType, MsgButtons, MsgTitle, MsgText)
    return myBox.execute()
