OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Change Text Case (rotate through several possibilities)

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 9183
Location: Lexinton, Kentucky, USA

PostPosted: Mon Jan 12, 2004 8:39 am    Post subject: Change Text Case (rotate through several possibilities) Reply with quote

Hi Russ,

I'll go first here to get things started.

Here's a macro that changes text between uppercase, title case, small capitals and lowercase. The cursor can be before, in or after the word. It also works on a selected area.

Although someone else has probably already done this, I did it as a programing exercise in response to a question in the SO forum. When finished, I realized there is enough code in place that it can fairly easily be change (or expanded) to say, alternate between plain, bold, italics and underlined simply by using the recording feature to get the needed code. On the theory that some beginners might try this I have heavily commented the code.
Code:
REM  *****  BASIC  *****
'Set up variables that can track where we are from one macro run to the next
Global F3%, Was$ 'Counter & text present at the previous cursor position
Sub ChangeCase
Dim bCollapsed as Boolean 'true of false variable
oFrame = thisComponent.CurrentController.Frame 'get the current document
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")'set up dispatcher
oVC = oFrame.Controller.getViewCursor() 'This is the cursor you normally see. If something
'is selected then it's the entire highlighted area and thus has two ends. The cursor
'below is a programing cursor and this statement puts it wherever the view cursor is.
oTC = thisComponent.Text.createTextCursorByRange(oVC)
If (oVC.isCollapsed()) then bCollapsed = true 'no text selected
Is$ = oTC.String(oTC.goRight(10, true)) : oTC.collapseToStart()'Get some text to see if the
If (Lcase(Is$) <> Lcase(Was$)) then F3% = 0 : Was$ = Is$ 'cursor been moved between runs.     
Select Case F3% 'select the routine below based on the current value of F3%, initially 0.
 case 0 'TO UPPERCASE
  If (bCollapsed and oTC.isEndOfWord()) then'If both these things are true move into the word.
    oVC.goLeft(1,false) '"true" will cause the text to be selected, not needed here
   ElseIf (bCollapsed and oTC.isStartOfWord()) then                   'but is below.
    oVC.goRight(1,false)
  EndIf 'the code between here and the cleanup was recorded in all routines.
  dispatcher.executeDispatch(oFrame, ".uno:ChangeCaseToUpper", "", 0, Array())
  If (bCollapsed and oTC.isEndOfWord()) then 'this is just clean up and ensures that
    oVC.goRight(1,false)                     'the cursor ends up back where it started. 
   ElseIf (bCollapsed and oTC.isStartOfWord()) then
    oVC.goLeft(1,false)   
  EndIf
 case 1 'TO TITLECASE
  If (bCollapsed and oTC.isEndOfWord()) then
    oVC.goLeft(1,true)
    dispatcher.executeDispatch(oFrame, ".uno:WordLeftSel", "", 0, Array())
   ElseIf (bCollapsed and oTC.isStartOfWord()) then
    oVC.goRight(1,true)
    dispatcher.executeDispatch(oFrame, ".uno:WordRightSel", "", 0, Array())
  EndIf
  dispatcher.executeDispatch(oFrame, ".uno:ChangeCaseToLower", "", 0, Array())
  dim args1(0) as new com.sun.star.beans.PropertyValue
  args1(0).Name = "CaseMap"
  args1(0).Value = 3
  dispatcher.executeDispatch(oFrame, ".uno:CaseMap", "", 0, args1())
  If (bCollapsed and oTC.isEndOfWord()) then oVC.collapseToEnd() 'another clean up
  If (bCollapsed and oTC.isStartOfWord()) then oVC.collapseToStart()
 case 2 'TO SMALL CAPS
  If (bCollapsed and oTC.isEndOfWord()) then
    oVC.goLeft(1,true)
    dispatcher.executeDispatch(oFrame, ".uno:WordLeftSel", "", 0, Array())
   ElseIf (bCollapsed and oTC.isStartOfWord()) then
    oVC.goRight(1,true)
    dispatcher.executeDispatch(oFrame, ".uno:WordRightSel", "", 0, Array())
  EndIf
  dim args2(0) as new com.sun.star.beans.PropertyValue
  args2(0).Name = "CaseMap"
  args2(0).Value = 4
  dispatcher.executeDispatch(oFrame, ".uno:CaseMap", "", 0, args2())
  If (bCollapsed and oTC.isEndOfWord()) then oVC.collapseToEnd()
  If (bCollapsed and oTC.isStartOfWord()) then oVC.collapseToStart()
 case 3 'TO LOWERCASE
  If (bCollapsed and oTC.isEndOfWord()) then
    oVC.goLeft(1,true)
    dispatcher.executeDispatch(oFrame, ".uno:WordLeftSel", "", 0, Array())
   ElseIf (bCollapsed and oTC.isStartOfWord()) then
    oVC.goRight(1,true)
    dispatcher.executeDispatch(oFrame, ".uno:WordRightSel", "", 0, Array())
  EndIf
  dim args3(0) as new com.sun.star.beans.PropertyValue
  args3(0).Name = "CaseMap"
  args3(0).Value = 0
  dispatcher.executeDispatch(oFrame, ".uno:CaseMap", "", 0, args3())
  dispatcher.executeDispatch(oFrame, ".uno:ChangeCaseToLower", "", 0, Array())
  If (bCollapsed and oTC.isEndOfWord()) then oVC.collapseToEnd()
  If (bCollapsed and oTC.isStartOfWord()) then oVC.collapseToStart()
End Select
If (F3% = 3) then 'adjust the value of F3% 
  F3% = 0
 Else
  F3% = F3% +1
EndIF 
End Sub
Back to top
View user's profile Send private message
tlahtopil
Newbie
Newbie


Joined: 23 Feb 2010
Posts: 1

PostPosted: Tue Feb 23, 2010 8:34 am    Post subject: Why caps fall to lowcase? Reply with quote

When I copy case-changed text with your macro, caps paste as lowercase. I'm on Go-OO 3.0.1 over WinXP-64. I see you are cycling through advanced text attributes, but then, how can we preserve case outside OOo?
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group