Writer Titlebar

Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This section is not for asking questions about writing your own macros.
Post Reply
JeJe
Volunteer
Posts: 2756
Joined: Wed Mar 09, 2016 2:40 pm

Writer Titlebar

Post by JeJe »

Inspired by a recent submission - shows a simple, easily modifiable, menu of items to make the
Writer titlebar a little more useful.

Put code in some place such as MyMacros/Standard Library.
Best used by setting a toolbar button to run sub TimeInTitle.

Menu items:

Current time - Appends to titlebar
Title - Appends document title to titlebar
Author - Appends document author to titlebar
Comments - Appends document comments/description to titlebar
Statistics - sets titlebar to document statistics
_
Clipboard - sets titlebar to clipboard text
Selected text - sets titlebar to current selected (plain) text
Insert - inserts titlebar text in document
Insert and restore - inserts titlebar text in document
and restores titlebar to component title and " - OpenOffice Writer" following
_
Clock - Appends a clock to the titlebar
Elapsed time - Appends elapsed time (since called) to the titlebar
Clock and Elapsed time - Appends both
_
Clear - sets titlebar to empty
Restore - restores titlebar to component title and " - OpenOffice Writer" following

Code: Select all

'TimeInTitle sub shows menu with choice of options for making
'the Writer titlebar a little more useful.
'Best used by setting a toolbar button to run this sub
'to adapt just add/remove item from the line of menu items (each separated with *
'and add/remove from the select case statement

	'Menu items

	'Current time - Appends to titlebar
	'Title - Appends document title to titlebar
	'Author - Appends document author to titlebar
	'Comments - Appends document comments/description to titlebar
	'Statistics - sets titlebar to document statistics
	'_
	'Clipboard - sets titlebar to clipboard text
	'Selected text - sets titlebar to current selected (plain) text
	'Insert - inserts titlebar text in document
	'Insert and restore - inserts titlebar text in document
	'	and restores titlebar to component title and " - 	OpenOffice Writer" following
	'_
	'Clock - Appends a clock to the titlebar
	'Elapsed time - Appends elapsed time (since called) to the titlebar
	'Clock and Elapsed time - Appends both
	'_
	'Clear - sets titlebar to empty
	'Restore - restores titlebar to component title and " - 	OpenOffice Writer" following
	'
global running as boolean

Sub timeInTitle()

	dim f,res,st,tt,timediff, addclock,props

	static ortime
	static ortitle as string

	on error goto hr
	if running =true then

		running = false
		'		goto hr
	end if

	f=thiscomponent.currentcontroller.frame
	props =thiscomponent.documentproperties
	ortitle = f.title
	st= "Current time*Title*Author*Comments*Statistics*_*Clipboard*Selected text*Insert*Insert and restore*_*Clock*Elapsed time*Clock and Elapsed time*_*Clear*Restore"
	res = showpopup3(thiscomponent.currentcontroller.componentwindow,st,0,0)
	select case res
	
	case "Clear"
		f.title =""
	case "Restore"
		'		with thiscomponent.currentcontroller
		'			select case .implementationname
		'			case "SwXTextView"
		'				doctype = " - OpenOffice Writer"
		'			case "ScTabViewObj"
		'				doctype = " - OpenOffice Calc"
		'			end select
		running = false
		ortitle = thiscomponent.title & " - OpenOffice Writer"
		f.title = thiscomponent.title & " - OpenOffice Writer"
		'			f.title = thiscomponent.title & doctype
		'		end with
	case "Current time"
		f.title = f.title & " - " & time
	case "Statistics"

		stats= thiscomponent.documentproperties.documentstatistics
		st = ""
		for i = 0 to ubound(stats)
		st = st & stats(i).name & ":" & stats(i).value & "  "
		next

		f.title =st

	case "Title"

		f.title =f.title & " - " & props.title
	case "Author"

		f.title = f.title & " - " & props.author
	case "Comments"
			f.title =f.title & " - " & props.description
	case "Clipboard"

		f.title = getClipboardText
	case "Insert"
		vc=Thiscomponent.currentcontroller.viewcursor
		vc.collapsetoend
		vc.string = f.title
		vc.collapsetoend
	case "Selected text"
		vc=Thiscomponent.currentcontroller.viewcursor
		f.title = vc.string
	case "Insert and restore"
		vc =Thiscomponent.currentcontroller.viewcursor
		vc.collapsetoend
		vc.string = f.title
		vc.collapsetoend

		running = false
		ortitle = thiscomponent.title & " - OpenOffice Writer"
		f.title = thiscomponent.title & " - OpenOffice Writer"

		f.title = thiscomponent.title & " - OpenOffice Writer"

	case "Clock"
		running = true
		do
			if running = false then exit do
			wait 800
			f.title = ortitle & " - " & time
		loop
		f.title = ortitle

	case "Elapsed time", "Clock and Elapsed time"
		if res = "Clock and Elapsed time" then addclock = true
		tt=split( time,":")
		ortime =  timeserial(tt(0),tt(1),tt(2))
		running = true
		do
			if running = false then exit do
			wait 800
			tt=split( time,":")
			timediff= timeserial(tt(0),tt(1),tt(2)) - ortime

			hrs = hour(timediff)
			mins = minute(timediff )
			secs = second(timediff)
			if addclock then
				f.title = ortitle & " - " & time &  " - " & hrs & ":" & mins & ":" & secs
			else
				f.title = ortitle & " - " & hrs & ":" & mins & ":" & secs
			end if
		loop
		f.title = ortitle

	end select

	'	end if
hr:
End Sub


function showpopup3(window,st as string,x,y) as string
	'split by *
	'separator_
	'note ~ identifies accelerator
	dim sts() as string,c as long,aRect,i, oPopup,n

	aRect = CreateUnoStruct("com.sun.star.awt.Rectangle")
	aRect.X =x 'oEvt.x + scontrol.PosSize.X +1
	aRect.Y =y ' oEvt.y + scontrol.PosSize.y+1


	oPopup = CreateUnoService("stardiv.vcl.PopupMenu")'"com.sun.star.awt.PopupMenu")

	sts = split(st,"*")

	for i = 0 to ubound(sts)
		c =c+1
		if sts(i) ="_" then
			oPopup.insertSeparator(c)
		else
			oPopup.insertItem(c, sts(i),0, c)
			'      oPopup.RADIOCHECK
			'		oPopup.checkitem(c,true)
			oPopup.setCommand(c, sts(i))
		end if
	next

	n = oPopup.execute( window, aRect, com.sun.star.awt.PopupMenuDirection.EXECUTE_DEFAULT)
	If n > 0 Then
		showpopup3 = oPopup.getCommand(n)
	end if

End function


	'clipboard functions from https://forum.openoffice.org/en/forum/viewtopic.php?f=21&t=93562
Function getClipboardText () AS String
	Dim oClip as object, oConverter as object
	Dim oClipContents as object, oTypes as object
	Dim i%
	'Access to the Systhem clipboard
	OClip = createUnoService ("com.sun.star.datatransfer.clipboard.SystemClipboard")
	'A converter: converts different variables and formats into one another.
	OConverter = createUnoService ("com.sun.star.script.Converter")
	On Error Resume Next 'If an error occurs: Ignore.
'Get the contents of the clipboard (images, files, text, .....):
	OClipContents = oClip.getContents
	'Read what types are currently dadrin (as array)
	OTypes = oClipContents.getTransferDataFlavors
	For i = LBound (oTypes) To UBound (oTypes) 'Loop from the beginning of the array to the end
		If oTypes (i).MimeType ="text/plain;charset=utf-16" Then '"The type we want

			'OClipContents.getTransferData (oTypes (i)) = unconvenient text
'Convert text and assign it to the function:


			GetClipboardText = oConverter.convertToSimpleType _
			(OClipContents.getTransferData (oTypes (i)), com.sun.star.uno.TypeClass.STRING)

			Exit Function 'function
		End If
	Next
End Function

Edit: Just a bit of fun, don't trust the only copy of your magnum opus to this code.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply