Difference between revisions of "PythonDialogBox"

From Apache OpenOffice Wiki
Jump to: navigation, search
(Registro de instrucciones.)
 
m (MsgBox in Python, simplified)
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Originally created by Paolo Mantovani for the [Code Snippet Project]. The snippet explains how to generate a MsgBox within the Python-Uno environment.  
+
Originally created by Paolo Mantovani for the [http://codesnippets.services.openoffice.org/ Code Snippet Project]. The snippet explains how to generate a '''MsgBox''' within the Python-Uno environment.  
  
<source lang=Python>
+
<syntaxhighlight lang="python">
 
from com.sun.star.awt import Rectangle
 
from com.sun.star.awt import Rectangle
 
from com.sun.star.awt import WindowDescriptor
 
from com.sun.star.awt import WindowDescriptor
Line 17: Line 17:
 
 
 
s = res
 
s = res
MessageBox(parentwin, s, t, "infobox")
+
MessageBox(parentwin, s, t, INFOBOX)
  
 
# Show a message box with the UNO based toolkit
 
# Show a message box with the UNO based toolkit
Line 48: Line 48:
 
return msgbox.execute()
 
return msgbox.execute()
  
g_exportedScripts = TestMessageBox,</source>
+
g_exportedScripts = TestMessageBox,
 +
</syntaxhighlight>
  
 
===Analysis===
 
===Analysis===
 
Analyzing the script we can consider the following:
 
Analyzing the script we can consider the following:
* Studying the series of UNO components for the UNO widgets
+
* Studying the series of UNO components for the UNO widgets
* Difference between TextMessageBox() and MessageBox()
+
* Difference between '''TextMessageBox()''' and '''MessageBox()'''
* Dialog object properties  
+
* Dialog object properties  
  
 
====UNO component composition====
 
====UNO component composition====
First thing we notice is the amount of widgets imported from the [http://www.openoffice.org/api/docs/common/ref/com/sun/star/awt/module-ix.html awt module]. The [http://www.openoffice.org/api/docs/common/ref/com/sun/star/awt/WindowDescriptor.html WindowDescriptor struct]
+
First thing we notice is the amount of widgets imported from the <idlmodule>com.sun.star.awt</idlmodule>. The <idl>com.sun.star.awt.WindowDescriptor</idl> struct is well documented on the '''API documentation'''.
 +
 
 +
=== Anatomy of the snippet ===
 +
The snippet has two different functions : the one in charge of creating the message box called '''MessageBox()''' which design the specs of the dialog and the '''TestMessageBox()''' which pass the arguments to the dialog. The MessageBox() set the following properties:
 +
* <idlm>com.sun.star.awt.WindowDescriptor:Type</idlm> - specifies the type of window.
 +
* <idlm>com.sun.star.awt.WindowDescriptor:WindowServiceName</idlm> - specifies the name of the component service.
 +
* <idlm>com.sun.star.awt.WindowDescriptor:Parent</idlm> - specifies the index of the parent window, if available.
 +
* <idlm>com.sun.star.awt.WindowDescriptor:Bounds</idlm> - specifies the position and size of the window.
 +
* <idlm>com.sun.star.awt.WindowDescriptor:WindowAttributes</idlm> - specifies the window attributes.
 +
The above properties can be found in the WindowDescriptor struct on the API documentation.
 +
 
 +
==MsgBox in Python, simplified==
 +
Since Paolo Mantovani created his snippet, interface <idls>com.sun.star.awt.XMessageBoxFactory</idls> was added to the API.
 +
 
 +
The code is now simpler:
 +
<syntaxhighlight lang="python">
 +
import uno
 +
 
 +
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
 +
 
 +
 +
def TestMessageBox():
 +
  doc = XSCRIPTCONTEXT.getDocument()
 +
  parentwin = doc.CurrentController.Frame.ContainerWindow
 +
 +
  s = "This a message"
 +
  t = "Title of the box"
 +
  res = MessageBox(parentwin, s, t, QUERYBOX, BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_NO)
 +
 +
  s = res
 +
  MessageBox(parentwin, s, t, "infobox")
 +
 +
# Show a message box with the UNO based toolkit
 +
def MessageBox(ParentWin, MsgText, MsgTitle, MsgType=MESSAGEBOX, MsgButtons=BUTTONS_OK):
 +
 +
  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()
 +
 +
g_exportedScripts = TestMessageBox,
 +
</syntaxhighlight>
 +
 
 +
===Analysis===
 +
All the constants and enums that can be used by the box are imported.
 +
 
 +
The function TestMessageBox() invokes the service <idl>com.sun.star.awt.Toolkit</idl> which exposes the interface <idls>com.sun.star.awt.XMessageBoxFactory</idls>.
 +
To invoke the service, we need the Service Manager, and the context.
 +
 
 +
Method createMessageBox of interface XMessageBoxFactory initializes a box with all its options.
 +
[[Category:Python]]

Latest revision as of 14:01, 3 February 2021

Originally created by Paolo Mantovani for the Code Snippet Project. The snippet explains how to generate a MsgBox within the Python-Uno environment.

from com.sun.star.awt import Rectangle
from com.sun.star.awt import WindowDescriptor
 
from com.sun.star.awt.WindowClass import MODALTOP
from com.sun.star.awt.VclWindowPeerAttribute import OK, OK_CANCEL, YES_NO, YES_NO_CANCEL, RETRY_CANCEL, DEF_OK, DEF_CANCEL, DEF_RETRY, DEF_YES, DEF_NO
 
def TestMessageBox():
	doc = XSCRIPTCONTEXT.getDocument()
	parentwin = doc.CurrentController.Frame.ContainerWindow
 
	s = "This is a test"
	t = "Test"
	res = MessageBox(parentwin, s, t, "querybox", YES_NO_CANCEL + DEF_NO)
 
	s = res
	MessageBox(parentwin, s, t, INFOBOX)
 
# Show a message box with the UNO based toolkit
def MessageBox(ParentWin, MsgText, MsgTitle, MsgType="messbox", MsgButtons=OK):
 
	MsgType = MsgType.lower()
 
	#available msg types
	MsgTypes = ("messbox", "infobox", "errorbox", "warningbox", "querybox")
 
	if not ( MsgType in MsgTypes ):
		MsgType = "messbox"
 
	#describe window properties.
	aDescriptor = WindowDescriptor()
	aDescriptor.Type = MODALTOP
	aDescriptor.WindowServiceName = MsgType
	aDescriptor.ParentIndex = -1
	aDescriptor.Parent = ParentWin
	#aDescriptor.Bounds = Rectangle()
	aDescriptor.WindowAttributes = MsgButtons
 
	tk = ParentWin.getToolkit()
	msgbox = tk.createWindow(aDescriptor)
 
	msgbox.setMessageText(MsgText)
	if MsgTitle :
		msgbox.setCaptionText(MsgTitle)
 
	return msgbox.execute()
 
g_exportedScripts = TestMessageBox,

Analysis

Analyzing the script we can consider the following:

  • Studying the series of UNO components for the UNO widgets
  • Difference between TextMessageBox() and MessageBox()
  • Dialog object properties

UNO component composition

First thing we notice is the amount of widgets imported from the com.sun.star.awt. The com.sun.star.awt.WindowDescriptor struct is well documented on the API documentation.

Anatomy of the snippet

The snippet has two different functions : the one in charge of creating the message box called MessageBox() which design the specs of the dialog and the TestMessageBox() which pass the arguments to the dialog. The MessageBox() set the following properties:

  • Type - specifies the type of window.
  • WindowServiceName - specifies the name of the component service.
  • Parent - specifies the index of the parent window, if available.
  • Bounds - specifies the position and size of the window.
  • WindowAttributes - specifies the window attributes.

The above properties can be found in the WindowDescriptor struct on the API documentation.

MsgBox in Python, simplified

Since Paolo Mantovani created his snippet, interface XMessageBoxFactory was added to the API.

The code is now simpler:

import uno
 
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
 
 
def TestMessageBox():
  doc = XSCRIPTCONTEXT.getDocument()
  parentwin = doc.CurrentController.Frame.ContainerWindow
 
  s = "This a message"
  t = "Title of the box"
  res = MessageBox(parentwin, s, t, QUERYBOX, BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_NO)
 
  s = res
  MessageBox(parentwin, s, t, "infobox")
 
# Show a message box with the UNO based toolkit
def MessageBox(ParentWin, MsgText, MsgTitle, MsgType=MESSAGEBOX, MsgButtons=BUTTONS_OK):
 
  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()
 
g_exportedScripts = TestMessageBox,

Analysis

All the constants and enums that can be used by the box are imported.

The function TestMessageBox() invokes the service com.sun.star.awt.Toolkit which exposes the interface XMessageBoxFactory. To invoke the service, we need the Service Manager, and the context.

Method createMessageBox of interface XMessageBoxFactory initializes a box with all its options.

Personal tools