Jump to content

Macros/ScriptForge/TabbedDialogExample

From The Document Foundation Wiki


How to use a tabbed interface in a dialog

Authored by Jean-Pierre Ledure.

The shown tabbed interface uses a TabPageContainer control. Such a control cannot be defined with the Basic IDE. Combined with the Page property of dialogs and dialog controls, the illusion is visually excellent.

A dialog with tabs.
A dialog with tabs.

How to run it in BASIC

  1. Create a new document
  2. Open the Basic IDE
  3. Select the first available blank module
  4. Copy and paste the Basic code
  5. Verify line 14
  6. Run Main()

How to run it in Python

  1. Create a new document
  2. Run APSO
  3. Create a new module, call it 'Module1'
  4. Copy and paste the Python code below
  5. Verify line 15
  6. Save and run the Main() method

Code

=

REM  SCRIPTFORGE WIKI EXAMPLE
REM 	How to simulate a tabbed interface in a dialog ?
REM		Minimal required version: LibreOffice 25.8
REM 	Used services
REM 		Dialog, DialogControl

Option Explicit

Public dialog		As Object		'	Dialog object

'*******************************************************************
'***	Adjust module name if not "Module1"
'*******************************************************************
Const onselect = "vnd.sun.star.script:Standard.Module1.SwitchPage?language=Basic&location=document"

'*******************************************************************
'* Run the demo below
'*******************************************************************
Sub Main(Optional event As Object)
	GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
	SetupDialog()
	PreparePageSwitching(initpage := 4)
	dialog.Execute()
	dialog.Terminate()
End Sub

'*******************************************************************
'* Specific demo code
'*******************************************************************
Sub PreparePageSwitching(initpage)
	dialog.Page = initpage
	dialog.Controls("TabPages").Value = initpage
	'	"TabPages" is the name of the tab page container control
	dialog.Controls("TabPages").OnTabSelected = onselect
End Sub

'*******************************************************************
'* Fired by a tab selection (OnTabSelected)
'*******************************************************************
Sub SwitchPage(Optional event)
Dim tab As Object		'	The tabpagecontainer control
	Set tab = CreateScriptService("DialogEvent", event)
	'	The Value property returns the selected tab
	If Not IsNull(tab) Then dialog.Page = tab.Value
End Sub

'*******************************************************************
'* May be defined with the Basic IDE
'*******************************************************************
Sub SetupDialog()
'	Build from scratch the example dialog and its controls

Dim control As Object			'	DialogControl object
Dim i As Integer

	Set dialog = CreateScriptService("NewDialog", "PageManager", Array(160, 90, 380, 120))
	dialog.Caption = "Page Manager"
	With dialog
		'	The close button
		Set control = .CreateButton("CloseButton", place := Array(340, 10, 30, 10), push := "OK")
		control.Caption = "Close"

		'	The TabPageContainer
		Set control = .CreateTabPageContainer("TabPages", Border := "3D", Place := Array(25, 10, 280, 95), _
												TabHeaders := Array("Page 1", "Page 2", "Page 3", "Page 4"))

		'	The page contents
		For i = 1 To 4
			Set control = .CreateFixedText("Page" & i, place := Array(160 + i * 10, 20 + i * 10, 200, 40))
			control.Caption = "Page " & i
			control.XControlModel.FontHeight = 32			'	Font size
			control.XControlModel.FontWeight = 150.0		'	Bold
			'	Define which controls belong to each page
			control.Page = i
		Next i

	End With

End Sub
=
# coding: utf-8
from __future__ import unicode_literals

from scriptforge import ScriptForge, CreateScriptService

###  SCRIPTFORGE WIKI EXAMPLE
### 	How to simulate a tabbed interface in a dialog ?
###		Minimal required version: LibreOffice 25.8
### 	Used services
### 		Dialog, DialogControl

# *******************************************************************
# ***	Adjust module name if not 'Module1'
# *******************************************************************
onselect = 'vnd.sun.star.script:Module1.py$SwitchPage?language=Python&location=document'


# *******************************************************************
# * Run the demo below
# *******************************************************************
def Main(event = None):
	# dialog			The dialog with a tabbed interface
	dialog = SetupDialog()
	PreparePageSwitching(dialog, initpage = 4)
	dialog.Execute()
	dialog.Terminate()


# *******************************************************************
# * Specific demo code
# *******************************************************************
def PreparePageSwitching(dialog, initpage = 1):
	dialog.Page = initpage
	# 	'TabPages' is the name of the tab page container control
	control = dialog.Controls('TabPages')
	control.OnTabSelected = onselect
	control.Value = initpage


# *******************************************************************
# * Fired by a tab selection (OnTabSelected)
# *******************************************************************
def SwitchPage(event = None):
	# tab				The tabpagecontainer control
	tab = CreateScriptService('DialogEvent', event)
	dialog = tab.Parent
	# 	The Value property returns the selected tab
	if tab is not None:
		dialog.Page = tab.Value


# *******************************************************************
# * May be defined with the Basic IDE
# *******************************************************************
def SetupDialog():
	# 	Build from scratch the example dialog and its controls

	# dialog			The dialog with a tabbed interface
	# control			DialogControl object

	dialog = CreateScriptService('NewDialog', 'PageSwitcher', (160, 90, 380, 120))
	dialog.Caption = 'Page Switcher'

	# 	The close button
	control = dialog.CreateButton('CloseButton', place = (340, 10, 30, 10), push = 'OK')
	control.Caption = 'Close'

	# 	The TabPageContainer
	control = dialog.CreateTabPageContainer('TabPages', border = '3D', place = (25, 10, 280, 95),
											tabheaders = ('Page 1', 'Page 2', 'Page 3', 'Page 4'))

	# 	The page contents
	for i in range(1, 5):
		control = dialog.CreateFixedText('Page' + str(i), place = (160 + i * 10, 20 + i * 10, 200, 40))
		control.Caption = 'Page ' + str(i)
		control.XControlModel.FontHeight = 32  # Font size
		control.XControlModel.FontWeight = 150.0  # Bold
		# 	Define which controls belong to each page
		control.Page = i

	return dialog


g_exportedScripts = (Main,)

if __name__ == "__main__":
	Main()

See also