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

Capturing a character code entered into a dialog box.

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
mdintzis
General User
General User


Joined: 25 Oct 2004
Posts: 28
Location: Lansdale, PA

PostPosted: Thu May 18, 2006 9:40 am    Post subject: Capturing a character code entered into a dialog box. Reply with quote

I'm new to the OOo macro programming language, and I'm struggling with how to create a macro involving a simple dialog box. The purpose of this dialog box is to enable me to apply character and paragraph styles using one- or two-character key sequences. This frees me from the mouse-driven "Styles and Formatting" window.

When I hit a certain shortcut key, I want to produce a dialog box (lets call it ChooseStyle) that presents two elements. At the top of the box, there will be a simple text box (view-only) that lists available OOo character styles. To the left of each style name will be a one- or two-character shortcut key code. At the bottom of the combo box, there will be a two-character type-in text field.

Once I enter a two-character shortcut code into the text field, I want that inputted text to be stored in a global variable (MyCharStyle$). Another subroutine will use that code to apply the chosen style to whatever text is currently selected in an OOo Writer document.

It's easy to design a new dialog box under Tools > Macros > Organize Dialogs. I've already done this.

It's also easy to write a macro that applies a specific character style to whatever text is currently selected. I've already done this.

What I can't figure out is:

1. How to call the dialog box from within a macro?

2. How do I capture the information entered into the text input box? I've looked at the "List of Events" section of each control, and I don't understand how to make use of those features.

To simplify things, let's assume right here that we're not supposed to enter two-character shortcut codes into the dialog box, but we're entering the full character style name. In the subroutine "Choose_MyStyle" shown below, what changes would I have to make in order to import the text box data from the dialog box?

-------------------------------------------------------------------------------------------
Global MyCharStyle$ As String

Sub Choose_MyStyle
MyCharStyle$ = "Emphasis"
Call ApplyCharStyle (MyCharStyle$)
End Sub

Sub ApplyCharStyle (MyCharStyle$ as String)
Dim document as object
Dim dispatcher as object

document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

dim args1(1) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Template"
args1(0).Value = MyCharStyle$
args1(1).Name = "Family"
args1(1).Value = 1
dispatcher.executeDispatch(document, ".uno:StyleApply", "", 0, args1())
End Sub
----------------------------------------------------------------------------------------------
Back to top
View user's profile Send private message
Villeroy
Super User
Super User


Joined: 04 Oct 2004
Posts: 10106
Location: Germany

PostPosted: Thu May 18, 2006 11:46 am    Post subject: Reply with quote

You may assign any named style to a shortcut, toolbar-button or menu-entry. No need for macros.
Does this help?
Back to top
View user's profile Send private message
mdintzis
General User
General User


Joined: 25 Oct 2004
Posts: 28
Location: Lansdale, PA

PostPosted: Thu May 18, 2006 11:54 am    Post subject: Reply with quote

I'm aware of this. But I want to learn how to do it through a macro that brings up a dialog box. There are several reasons. For one thing, all of the customizations that you mentioned get saved in the OOo document. Evenually, I could have many documents, each of which has the same customizations? What do I do if I want to change the custom menus for all of them?

If I use a macro that brings up a dialog box, and the macro is in OpenOffice.Org instead of in the documents, I need to go to only one place to change the dialog box.

Can you help me here?
Back to top
View user's profile Send private message
Villeroy
Super User
Super User


Joined: 04 Oct 2004
Posts: 10106
Location: Germany

PostPosted: Thu May 18, 2006 12:47 pm    Post subject: Reply with quote

Some copy and paste-programming:
Code:

REM  *****  BASIC  *****

Sub Main
Const sFamily$ = "ParagraphStyles"
' CharacterStyles  the container of style sheets for sequences of characters within a text
' ParagraphStyles  the container of style sheets for text paragraphs
' FrameStyles    the container of style sheets for text frames
' PageStyles     the container of style sheets for pages
' NumberingStyles the container for style sheets for numbering
' CellStyles the container for style sheets for cells
' ShapeStyles  the container for style sheets for shapes
styleList = getStyleNames(ThisComponent,sFamily,bUsed:=False,bUserDef:=False,bLocalized:=true)
if uBound(styleList) > -1 then
   sSelect = getUserSelectionFromListbox(styleList(),sFamily)
   msgbox sSelect
endif
End Sub
Sub getUserSelectionFromListbox(sArray(),sTitle$) as String
   'create a named, sized dialog from scratch with some controls
   REM make dialog model
   oDialogModel = CreateUnoService("com.sun.star.awt.UnoControlDialogModel")
   with oDialogModel
      .PositionX = 100
      .PositionY = 100
      .Width = 60
      .Height = 60
      .Title = sTitle
      .Sizeable = true 'this does not work with linux, how about windows?
   end with
   REM make listbox model
   oCtrlModel = oDialogModel.createInstance("com.sun.star.awt.UnoControlListBoxModel")
   with oCtrlModel
      .DropDown = true
      .PositionX = 2
      .PositionY = 2
      .Width = 50
      .Height = 13
      .MultiSelection = false
      .stringItemList = sArray()
   End With
   oDialogModel.insertByName("Listbox1",oCtrlModel)
   REM make OK button model
   oCtrlModel = oDialogModel.createInstance("com.sun.star.awt.UnoControlButtonModel")
   with oCtrlModel
      .PositionX = 2
      .PositionY = 45
      .Width = 25
      .Height = 13
      .Label = "OK"
      .DefaultButton = true
      .PushButtonType = com.sun.star.awt.PushButtonType.OK
   End With
   oDialogModel.insertByName("btnOK",oCtrlModel)
   REM make cancel button model
   oCtrlModel = oDialogModel.createInstance("com.sun.star.awt.UnoControlButtonModel")
   with oCtrlModel
      .PositionX = 32
      .PositionY = 45
      .Width = 25
      .Height = 13
      .Label = "Cancel"
      .PushButtonType = com.sun.star.awt.PushButtonType.CANCEL
   End With
   oDialogModel.insertByName("btnCancel",oCtrlModel)
   REM make dialog and set model
   oDialog = CreateUnoService("com.sun.star.awt.UnoControlDialog")
   oDialog.setModel(oDialogModel)
   oDialog.setVisible(True)
   If oDialog.execute > 0 then
      getUserSelectionFromListbox = oDialog.getCOntrol("Listbox1").getSelectedItem
   endif
End Sub
'returns a string-array of style-names
'oDoc:= any OOo-document
'sFamily:= appropriate name of a style family
'bUsed:= only styles, used in this document
'bUserDef:= no builtin styles
'bLocalized:= return localized names of the builtin styles (eg. builtin "Default" --> german "Standard")
Function getStyleNames(oDoc,sFamily$,bUsed as Boolean,bUserDef as Boolean,bLocalized as Boolean)
Dim oFamily,oStyle,i%,sNames$(),sName$
   If oDoc.StyleFamilies.hasByName(sFamily) then
      oFamily = oDoc.StyleFamilies.getByName(sFamily)
      for i = 0 to oFamily.getCount -1
         oStyle = oFamily.getByIndex(i)
         If (bUsed IMP oStyle.isInUse)AND(bUserDef IMP oStyle.isUserDefined) then
            sName = oStyle.getName
            if bLocalized then sName = oStyle.DisplayName
            bas_Pusharray sNames(),sName
         endif
      next
   endif
   getStyleNames = sNames()
End Function
'very simple routine appending some element to an array which can be undimensioned (LBound > UBound)
Sub bas_PushArray(xArray(),vNextElement)
Dim iUB%,iLB%
   iLB = lBound(xArray())
   iUB = uBound(xArray())
   If iLB > iUB then
      iUB = iLB
      redim xArray(iLB To iUB)
   else
      iUB = iUB +1
      redim preserve xArray(iLB To iUB)
   endif
   xArray(iUB) = vNextElement
End Sub
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API 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