Interactive/CorrectSyntaxDialog.py

# (C) Copyright 2012 Autodesk, Inc.  All rights reserved.
#
# Use of this software is subject to the terms of the Autodesk license
# agreement provided at the time of installation or download, or which
# otherwise accompanies this software in either electronic or hard copy
# form.

# See the documentation in CorrectSyntaxCustom.py

__all__ = [ "CorrectSyntaxDialog" ]

import os
import sys
import wx

if __name__ == "__main__":

    import gettext
    gettext.install('unittest')

    build   = os.environ.get("BUILD")
    variant = os.environ.get("VARIANT")
    if build and variant:
        sys.path.append(os.path.join(build, variant, "runTime", "bin"))

from awx.Button                  import Button
from awx.TextCtrl                import TextCtrl
from awx.StaticText              import StaticText

from Dialogs.DialogButtonMixin   import CreateStandardButtonSizer
from EventPasser                 import EventPasser
from UiUtilities                 import OperationWindow, AddButtonSizerToVerticalSizer
from ShowHelpTopic               import ShowHelpSDK
from operator                    import isSequenceType

class CorrectSyntaxDialog(OperationWindow, EventPasser):
    """
    A syntax dialog with a language choice item and list for the item.
    """
    __kScrollWindowWidth  = 800
    __kScrollWindowHeight = 350
    __kComboBoxWidth      = 300 

    kKeyTitle         = 'title'
    kKeyHtmlItemLabel = 'html-item-label'
    kKeyHtmlItemId    = 'html-item-id'
    kKeyAjaxItemLabel = 'ajax-item-label'
    kKeyAjaxItemId    = 'ajax-item-id'
    kKeyHtmlNonItem   = 'html-non-item'
    kKeyAjaxNonItem   = 'ajax-non-item'
    kKeyScaleformItemLabel = 'scaleform-item-label'
    kKeyScaleformItemId    = 'scaleform-item-id'
    kKeyHtmlNonItem   = 'html-non-item'
    kKeyScaleformNonItem   = 'scaleform-non-item'

    def __init__(self, parent, title, parameters ):
        OperationWindow.__init__( self, parent, id=wx.ID_ANY, 
                                  name=u"CorrectSyntaxDialog",
                                  style=OperationWindow.kDefaultStyle|wx.RESIZE_BORDER)
        EventPasser.__init__(self)

        self.__myTitle = title
        self.__mySyntaxParameters = parameters
        self.CreateStandardLayout()
        self.SetSizerAndFit(self.GetMainSizer())

        # Because of gettext, this needs to be here, not as a constant
        self.__kTextItemLabel = _('using the user label')
        self.__kTextItemId    = _('using the internal ID')

        # Force the correct syntax to show
        self.refreshScroll(self.__mySyntaxParameters)


    def DoCreate(self):
        # Create all the controls that are going to be in syntax dialog box.
        # This is called indirectly by self.CreateStandardLayout(). 
        panel = wx.Panel(self)

        self.__myAjaxTitle   = StaticText(panel, label=_('Ajax/Javascript'))
        self.__myAjaxTitle.SetFont( wx.Font(10, wx.DEFAULT,
                                            wx.NORMAL, wx.BOLD))
        self.__myAjaxTitleD   = StaticText(panel, label=_('Use this version when the page is accessed using "http://localhost/..." link and the xmlhttp.js file,\nas in the documented ajax.html example, either through built in or external browser.'))

        self.__myScaleformTitle   = StaticText(panel, label=_('Scaleform Flash UI'))
        self.__myScaleformTitle.SetFont( wx.Font(10, wx.DEFAULT,
                                            wx.NORMAL, wx.BOLD))
        self.__myScaleformTitleD   = StaticText(panel, label=_('Use this version to build Scaleform Flash interface.'))

        self.__myHtmlTitle   = StaticText(panel, label=_('Internal/Html'))
        self.__myHtmlTitle.SetFont( wx.Font(10, wx.DEFAULT,
                                            wx.NORMAL, wx.BOLD))
        self.__myHtmlTitleD   = StaticText(panel, label=_('Use this version when the page is accessed using "file://..." link through built-in browser.'))

        # Create the Ajax Syntax List seperator
        self.__myAjaxScrollText = TextCtrl( panel, -1, '',
                                        size=(CorrectSyntaxDialog.__kScrollWindowWidth, CorrectSyntaxDialog.__kScrollWindowHeight/2),
                                        style=wx.TE_MULTILINE | wx.TE_READONLY )

        # Create the Ajax Syntax List seperator
        self.__myScaleformScrollText = TextCtrl( panel, -1, '',
                                        size=(CorrectSyntaxDialog.__kScrollWindowWidth, CorrectSyntaxDialog.__kScrollWindowHeight/2),
                                        style=wx.TE_MULTILINE | wx.TE_READONLY )

        # Create the Html Syntax List seperator
        self.__myHtmlScrollText = TextCtrl( panel, -1, '',
                                        size=(CorrectSyntaxDialog.__kScrollWindowWidth, CorrectSyntaxDialog.__kScrollWindowHeight/2),
                                        style=wx.TE_MULTILINE | wx.TE_READONLY )
        # Close button
        self.__myCloseButton = Button(panel, id=wx.ID_CLOSE, label=_('Close'))
        self.__myCloseButton.Bind(wx.EVT_BUTTON, self.__onClose)
        self.Bind(wx.EVT_CLOSE, self.__onCloseWindow)

        # Help button
        self.__myHelpButton = Button(panel, id=wx.ID_HELP, label= _('Help'))
        self.__myHelpButton.Bind(wx.EVT_BUTTON, self.__OnAPIDocs)

        return panel


    def DoLayout(self):
        # Create the layout for the controls that are going in syntax dialog box.
        # This is called indirectly by self.CreateStandardLayout()

        buttonSizer =CreateStandardButtonSizer((
                    self.__myHelpButton, self.__myCloseButton))

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.__myAjaxTitle, flag=wx.LEFT|wx.TOP, border=5)
        sizer.Add(self.__myAjaxTitleD, flag=wx.LEFT|wx.BOTTOM|wx.TOP, border=5)
        sizer.Add(self.__myAjaxScrollText, wx.EXPAND)
        sizer.Add(self.__myScaleformTitle, flag=wx.LEFT|wx.TOP, border=5)
        sizer.Add(self.__myScaleformTitleD, flag=wx.LEFT|wx.BOTTOM|wx.TOP, border=5)
        sizer.Add(self.__myScaleformScrollText, wx.EXPAND)
        sizer.Add(self.__myHtmlTitle, flag=wx.LEFT|wx.TOP, border=5)
        sizer.Add(self.__myHtmlTitleD, flag=wx.LEFT|wx.BOTTOM|wx.TOP, border=5)
        sizer.Add(self.__myHtmlScrollText, wx.EXPAND)
        AddButtonSizerToVerticalSizer(sizer, buttonSizer)

        return sizer


    def refreshScroll(self, parameters):
        param = parameters

        # Title:
        if CorrectSyntaxDialog.kKeyTitle in param:
            self.SetTitle(self.__myTitle % param[CorrectSyntaxDialog.kKeyTitle])

        # Ajax Syntax list
        listAjaxStr = '\n'

        # Ajax by label
        if CorrectSyntaxDialog.kKeyAjaxItemLabel in param:
            if isinstance(param[CorrectSyntaxDialog.kKeyAjaxItemLabel], basestring):
                listAjaxStr += param[CorrectSyntaxDialog.kKeyAjaxItemLabel] % (self.__kTextItemLabel) + '\n'
            elif isSequenceType(param[CorrectSyntaxDialog.kKeyAjaxItemLabel]):
                for one in param[CorrectSyntaxDialog.kKeyAjaxItemLabel]:
                    listAjaxStr += one % (self.__kTextItemLabel) + '\n'
            listAjaxStr += '\n'
        else:
            listAjaxStr += 'Nothing for ajax item label\n\n'

        # Ajax by id
        if CorrectSyntaxDialog.kKeyAjaxItemId in param:
            if isinstance(param[CorrectSyntaxDialog.kKeyAjaxItemId], basestring):
                listAjaxStr += param[CorrectSyntaxDialog.kKeyAjaxItemId] % (self.__kTextItemId) + '\n'
            elif isSequenceType(param[CorrectSyntaxDialog.kKeyAjaxItemId]):
                for one in param[CorrectSyntaxDialog.kKeyAjaxItemId]:
                    listAjaxStr += one % (self.__kTextItemId) + '\n'
            listAjaxStr += '\n'
        else:
            listAjaxStr += 'Nothing for ajax item id\n\n'

        # Ajax non-item
        if CorrectSyntaxDialog.kKeyAjaxNonItem in param:
            if isinstance(param[CorrectSyntaxDialog.kKeyAjaxNonItem], basestring):
                listAjaxStr += param[CorrectSyntaxDialog.kKeyAjaxNonItem] + '\n'
            elif isSequenceType(param[CorrectSyntaxDialog.kKeyAjaxNonItem]):
                for one in param[CorrectSyntaxDialog.kKeyAjaxNonItem]:
                    listAjaxStr += one + '\n'
        else:
            listAjaxStr += 'Nothing for ajax non-item'


        # Scaleform Syntax list
        listScaleformStr = '\n'

        # Scaleform by label
        if CorrectSyntaxDialog.kKeyScaleformItemLabel in param:
            if isinstance(param[CorrectSyntaxDialog.kKeyScaleformItemLabel], basestring):
                listScaleformStr += param[CorrectSyntaxDialog.kKeyScaleformItemLabel] % (self.__kTextItemLabel) + '\n'
            elif isSequenceType(param[CorrectSyntaxDialog.kKeyScaleformItemLabel]):
                for one in param[CorrectSyntaxDialog.kKeyScaleformItemLabel]:
                    listScaleformStr += one % (self.__kTextItemLabel) + '\n'
            listScaleformStr += '\n'
        else:
            listScaleformStr += 'Nothing for scaleform item label\n\n'

        # Scaleform by id
        if CorrectSyntaxDialog.kKeyScaleformItemId in param:
            if isinstance(param[CorrectSyntaxDialog.kKeyScaleformItemId], basestring):
                listScaleformStr += param[CorrectSyntaxDialog.kKeyScaleformItemId] % (self.__kTextItemId) + '\n'
            elif isSequenceType(param[CorrectSyntaxDialog.kKeyScaleformItemId]):
                for one in param[CorrectSyntaxDialog.kKeyScaleformItemId]:
                    listScaleformStr += one % (self.__kTextItemId) + '\n'
            listScaleformStr += '\n'
        else:
            listScaleformStr += 'Nothing for scaleform item id\n\n'

        # Scaleform non-item
        if CorrectSyntaxDialog.kKeyScaleformNonItem in param:
            if isinstance(param[CorrectSyntaxDialog.kKeyScaleformNonItem], basestring):
                listScaleformStr += param[CorrectSyntaxDialog.kKeyScaleformNonItem] + '\n'
            elif isSequenceType(param[CorrectSyntaxDialog.kKeyScaleformNonItem]):
                for one in param[CorrectSyntaxDialog.kKeyScaleformNonItem]:
                    listScaleformStr += one + '\n'
        else:
            listScaleformStr += 'Nothing for scaleform non-item'


        # Html Syntax List

        listHtmlStr = '\n'

        # Html by label
        if CorrectSyntaxDialog.kKeyHtmlItemLabel in param:
            if isinstance(param[CorrectSyntaxDialog.kKeyHtmlItemLabel], basestring):
                listHtmlStr += param[CorrectSyntaxDialog.kKeyHtmlItemLabel] % (self.__kTextItemLabel) + '\n'
            elif isSequenceType(param[CorrectSyntaxDialog.kKeyHtmlItemLabel]):
                for one in param[CorrectSyntaxDialog.kKeyHtmlItemLabel]:
                    listHtmlStr += one % (self.__kTextItemLabel) + '\n'
            listHtmlStr += '\n'
        else:
            listHtmlStr += 'Nothing for html item label\n\n'

        # Html by id
        if CorrectSyntaxDialog.kKeyHtmlItemId in param:
            if isinstance(param[CorrectSyntaxDialog.kKeyHtmlItemId], basestring):
                listHtmlStr += param[CorrectSyntaxDialog.kKeyHtmlItemId] % (self.__kTextItemId) + '\n'
            elif isSequenceType(param[CorrectSyntaxDialog.kKeyHtmlItemId]):
                for one in param[CorrectSyntaxDialog.kKeyHtmlItemId]:
                    listHtmlStr += one % (self.__kTextItemId) + '\n'
            listHtmlStr += '\n'
        else:
            listHtmlStr += 'Nothing for html item id\n\n'

        # Html non-item
        if CorrectSyntaxDialog.kKeyHtmlNonItem in param:
            if isinstance(param[CorrectSyntaxDialog.kKeyHtmlNonItem], basestring):
                listHtmlStr += param[CorrectSyntaxDialog.kKeyHtmlNonItem] + '\n'
            elif isSequenceType(param[CorrectSyntaxDialog.kKeyHtmlNonItem]):
                for one in param[CorrectSyntaxDialog.kKeyHtmlNonItem]:
                    listHtmlStr += one + '\n'
            listHtmlStr += '\n'
        else:
            listHtmlStr += 'Nothing for html non-item'

        self.__myAjaxScrollText.SetValue(listAjaxStr)
        self.__myScaleformScrollText.SetValue(listScaleformStr)
        self.__myHtmlScrollText.SetValue(listHtmlStr)


    def doUpdate(self, parameters ):
        self.refreshScroll(parameters)


    def __onCloseWindow(self, evnet):
        self.Show(False)
        self.sendMessage("DIALOG_DESTROY", (self, ""))


    def __onClose(self, event):
        self.Show(False)
        self.sendMessage("DIALOG_DESTROY", (self, ""))


    def __OnAPIDocs (self, event ):
        """
        Open the online API doc to the table of contents
        """
        ShowHelpSDK()


    def APPLICATION_CLOSE_SCENE(self, message):
        self.__myModels = None
        RunInUiThread(self.Show, False)

#==============================================================================
# Testing
if __name__ == "__main__":
    from UiUtilities import ExampleWindow_, ShowWindow

    class ExampleWindow(ExampleWindow_):

        def OnButton(self, event):
            dialog = CorrectSyntaxDialog(self,"Example",
                                         {'html-item-label':'ForHtmlLabel %s',
                                          'html-item-id':'ForHtmlId %s',
                                          'scaleform-item-label':'ForScaleformLabel %s',
                                          'scaleform-item-id':'ForScaleformId %s',
                                          'ajax-item-label':'ForAjaxLabel %s',
                                          'ajax-item-id':'ForAjaxId %s',
                                          })
            dialog.Show()

    ShowWindow(ExampleWindow)