From Apache OpenOffice Wiki
Jump to: navigation, search

The easiest way to capture this source is simply to select the contents of the box below, copy it, and paste into the Basic IDE window. You don't need to edit this page, to do that.

To run the macro, you can assign the keyboard shortcut of your choice, and/or a toolbar icon. I added this icon: UL.png to my Formatting toolbar.

REM Toggle Case between Upper and Lower 
REM User Documentation:
' To toggle one character, place the cursor in front of that character,
'   and run the macro. It is not necessary to select a single character.  
' To toggle one or more characters, select the characters, and run the macro.
Sub ToggleCaseUL
REM Toggle one character, to the right of the cursor, 
REM   from upper to lower case, or vice versa.
' This operation is its own inverse. Do twice to undo.
Dim oCursor as Object	'text cursor
Dim oDoc as Object
Dim oIC as Object	'text cursor for insert
Dim oVC as Object	'view cursor
Dim bEmpty as Boolean	'remember oVC initial condition
Dim ix as Integer
Dim s1 as String
Dim sTarget as String
oDoc = ThisComponent
' get the current cursor position in the GUI.
oVC = oDoc.getCurrentController().getViewCursor()
sTarget = oVC.getString()	'fetch selected char(s), if any
' select one character, if necessary.
bEmpty = (len(sTarget) = 0)
If bEmpty Then
  oVC.goRight( 1, True )	'select one character
'  Trying to get around the problem by selecting from the
'    other direction, but got the same result.
'  oVC.goRight( 1, False )	'select one character
'  oVC.goLeft( 1, True )	'select one character
  sTarget = oVC.getString()	'fetch selected char
End If 'bEmpty
If len(sTarget) > 0 Then	'we have a character
  oCursor = oVC.getText.createTextCursorByRange(oVC)
  ' assumes that char is either UC or LC, or invariant.
  For ix = 1 To len(sTarget)
    s1 = Mid(sTarget, ix, 1)
    If Ucase(s1) = s1 Then				'already UC (or invariant)
      Mid(sTarget, ix, 1, Lcase(s1))	' so set LC
    Else								'set UC
      Mid(sTarget, ix, 1, Ucase(s1))
    End If 'have upper case
  Next ix
'  oVC.setString(sTarget)		'set result (loses formatting)
REM The following code compensates for several interesting
REM  design features.
REM  (1) Replacing the first character of a portion effectively
REM      moves that character into the previous portion, with
REM      the previous portion's formatting.
REM      (Workaround here, insert new, then delete old.)
REM  (2) insert.String affects the range of all (?) text cursors
REM      on that text. (Here, oIC changes oCursor range.)
REM  (3) insert.String moves the visual cursor, too.
'print oCursor.string		**DEBUG**
  oIC = oVC.getText.createTextCursorByRange(oVC)
  oIC.Text.insertString( oIC, sTarget, False )	'preserve portion
'  oIC.Text.insertString( oIC, "", True )	'erases too much
'  oCursor.Text.insertString( oCursor, sTarget, True )	'loses formatting
'print oCursor.string		**DEBUG**
  oCursor.goLeft( len(sTarget), True )		'shrink range back to original
  oCursor.Text.insertString( oCursor, "", True )	'erase previous char(s)
  oVC.gotoRange(oCursor, False )	'insStr moves vc, so move it back
    msgBox "No character to toggle.",,"ToggleCaseUL"
End If 'len(sTarget) 
If bEmpty Then	' un-select the character
End If 'initial selection empty
End Sub 'ToggleCaseUL
Personal tools