Writer - insert tabs with the keyboard

Creating Extension - 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 forum is not for asking questions about writing your own macros.

Writer - insert tabs with the keyboard

Postby JeJe » Mon Mar 29, 2021 3:28 pm

Almost never used tabs but was inspired by a recent thread to write this
as I noticed Writer lets you insert tabs with the mouse and ruler very easily but there
seems to be no easy way with the keyboard.

This code converts some spaces to a tab with a tab added to the paragraph's list of them.


'WRITER INSERTING TABS WITH KEYBOARD
'You'll to put this code in MyMacros or somewhere
'and set a shortcut to run one of the macros
'SpacesToTabStop or SpacesToTabStopWithMenu

'type some spaces and run SpacesToTabStop
'a tab will be set at that position
'and the spaces will be converted to a tab

'OR run SpacesToTabStopWithMenu()
'a popup menu will give a choice of setting
'left, right, centred or decimal tab
'or clearing all the tabs in the paragraph
'by setting one at the far end

'USUAL BLURB VERY PRELIMINARY, LIMITED TESTING FOR ERRORS USE AT OWN RISK


Code: Select all   Expand viewCollapse view

   REM  *****  BASIC  *****
'WRITER INSERTING TABS WITH KEYBOARD
'You'll to put this code in MyMacros or somewhere
'and set a shortcut to run one of the macros
'SpacesToTabStop or SpacesToTabStopWithMenu

'type some spaces and run SpacesToTabStop
'a tab will be set at that position
'and the spaces will be converted to a tab

'OR run SpacesToTabStopWithMenu()
'a popup menu will give a choice of setting
'left, right, centred or decimal tab
'or clearing all the tabs in the paragraph
'by setting one at the far end

'USUAL BLURB VERY LIMITED TESTING, USE AT OWN RISK

Sub SpacesToTabStopWithMenu()
   SpacesToTabStop true
end sub

Sub SpacesToTabStop(optional showmenu)
   dim doneinsert as boolean,newtabalign,vc,tc,x1 as long,x2 as long,res as string,ret as long
   '   Dim Tabs '() 'as New com.sun.star.style.TabStop

   if ismissing(showmenu)=false then
      st="Left*Right*Centred*Decimal*_*Clear ~all tabs"

      res = showpopup3(thiscomponent.currentcontroller.frame.componentwindow,st,0,00
      thiscomponent.currentcontroller.frame.componentwindow.setfocus
      select case res
      case ""
         exit sub
      case "Clear ~all tabs"
         clearalltabs
      case "Left"
         newtabalign= com.sun.star.style.TabAlign.LEFT
      case "Right"
         newtabalign= com.sun.star.style.TabAlign.RIGHT
      case "Centred"
         newtabalign= com.sun.star.style.TabAlign.CENTER
      case "Decimal"
         newtabalign= com.sun.star.style.TabAlign.DECIMAL
      end select

   else
      newtabalign= com.sun.star.style.TabAlign.LEFT
   end if


   vc = thiscomponent.currentcontroller.viewcursor
   tc = vc.text.createtextcursorbyrange(vc)
   x1 =vc.position.x
   vc.gotostartofline(false)
   x2= vc.position.x
   vc.gotorange(tc,false)
   do
      tc.collapsetostart
      ret = tc.goleft(1,true)
      if ret = 0 then exit do
      if tc.string <>" "  then exit do
   loop
   tc.collapsetoend
   tc.gotorange(vc.start,true)
   if len(tc.string) =0 then exit sub '?

   t = tc.ParaTabStops
   pos =  x1-x2
   ub  =ubound(t)

   if ub =0 and t(0).position=0 then
      redim preserve tabs(0) as New com.sun.star.style.TabStop
      tabs(0).position = pos
      Tabs(0).Alignment = newtabalign 'com.sun.star.style.TabAlign.LEFT 'DEFAULT
      ' settabstop(tabs(c),.Position,.Alignment,.DecimalChar,.FillChar)
   else
      redim preserve tabs(ub+1) as New com.sun.star.style.TabStop
      c=-1
      for i =0 to ub
         select case t(i).position
         case 0
         case < pos
            c=c+1
            with t(i)
               settabstop(tabs(c),.Position,.Alignment,.DecimalChar,.FillChar)
            end with

         case =pos
            exit sub
         case >pos

            if not doneinsert then
               c=c+1
               doneinsert = true
               tabs(c).position = pos
               Tabs(c).Alignment =newtabalign ' com.sun.star.style.TabAlign.LEFT

            end if
            c=c+1
            with t(i)
               settabstop(tabs(c),.Position,.Alignment,.DecimalChar,.FillChar)
            end with
         end select

      next
      if not doneinsert then
         c=c+1
         tabs(c).position = pos
         Tabs(c).Alignment = newtabalign 'com.sun.star.style.TabAlign.LEFT
      end if
      redim preserve tabs(c)

   end if

   tc.ParaTabStops = tabs()
   tc.string =chr(9)

End Sub

sub settabstop(tstop,Position,Alignment,DecimalChar,FillChar)
   'Position      long              3057
   'Alignment     .style.TabAlign   LEFT
   'DecimalChar   char              .
   'FillChar      char              NUL

   with tstop
      .Position = Position
      .Alignment = Alignment
      .DecimalChar = DecimalChar
      .FillChar=FillChar
   end with
end sub


sub clearalltabs
   dim document   as object
   dim dispatcher as object
   document   = ThisComponent.CurrentController.Frame
   dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
   dim args1(0) as new com.sun.star.beans.PropertyValue
   args1(0).Name = "Tabstops.TabStops"
   args1(0).Value = Array(Array(30000,com.sun.star.style.TabAlign.DEFAULT,"."," "))
   dispatcher.executeDispatch(document, ".uno:Tabstops", "", 0, args1())
end sub

'**************************************


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

   aRect = CreateUnoStruct("com.sun.star.awt.Rectangle")

   arect.x = x
   arect.y =y

   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

         if mid(sts(i),1,1) ="!" then isdefault = true else isdefault = false

         if isdefault then
            mid(sts(i),1,1) =""
            oPopup.insertItem(c, sts(i),0, c)
            opopup.setDefaultItem c
         else
            oPopup.insertItem(c, sts(i),0, c)
            oPopup.setCommand(c, sts(i))
         end if
      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





Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1498
Joined: Wed Mar 09, 2016 2:40 pm

Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 1 guest