*メモ [#a53be209] フォーム関連メモ。行き先が決まれば移動されるかもしれません。 #contents **タブっぽくコントロールの表示切り替え [#pcfecb73] フォームにタブコントロールはありません。ボタンを押してコントロールの表示/非表示を切り替えることで同じ付近にコントロールを切り替えてみます。 コントロールの表示/非表示を切り替えるにはフォームコントロールのコントローラを利用する必要があります。フォームコントロールのコントローラはデザインモードでは動作しないのでテストするときには注意。 &ref(TabLike_Form-1.odt); ボタンを配置してそれぞれのボタンをページ切り替えとして利用。ボタンを押すとそれぞれのボタンに対応したフォームコントロールを表示する。 また、ドキュメントを開いたときなどのイベントにあるページ群を表示するように設定しておく。 -コントロールが重なっているため、フォーム編集時にはフォームナビゲータ必須。 -フォームが完成するまではコントロールをずらして作成するべき。 あとは -グリッドを表示したままだった。 -ページを切り替えたときにフォーカスを移すとか…。 -ボタンの色がいまいち。 -グループボックスにタイトルを設定してみたが…。 **タブっぽくコントロールの表示切り替え2 [#e91a149c] [[Simulating tabs in a form.>http://user.services.openoffice.org/en/forum/viewtopic.php?f=39&t=7102&st=0&sk=t&sd=a]] ではセクションの切り替えでタブ風にしているようです。こっちのほうが楽ですね。 セクションは取り扱いが面倒、ということでフレームにセクションを入れて……と。セクションがフレーム内にあるときには IsVisible を切り替えても非表示になりませんね…。 書いてみたマクロが上記のコードとほとんど同じだったので上記リンク参照。 **フォームコントロールからのコピー [#id4cc213] 一行もののフォームコントロールの内容を、ボタンなどを押すだけでコピーする。accessibility API を利用する。 ボタンなどに下記を割り当てます。ボタンを押すと TextBox のテキストをコピーします。複数行にしたコントロールでは利用できません。 Sub CopyButtonPush( ev ) oForm = ev.Source.getModel().getParent() oTextBox = oForm.getByName("TextBox") oDoc = oForm.getParent().getParent() oController = oDoc.getCurrentController() oControl = oController.getControl(oTextBox) oAccControl = oControl.getAccessibleContext() nCount = oAccControl.getCharacterCount() oAccControl.copyText(0, nCount) End Sub **ボタンを押す [#e9980366] フォーム上に配置したボタンを押します (押したのと同じ状態を起こします)。 PushButton1 をマウスでクリックしたときの動作を起こします。 Sub ButtonPushed( ev ) oForm = ev.Source.getModel().getParent() oButton = oForm.getByName("PushButton1") oDoc = oForm.getParent().getParent() oController = oDoc.getCurrentController() oControl = oController.getControl(oButton) oAccControl = oControl.getAccessibleContext() oAccControl.doAccessibleAction(0) End Sub 押したときに実行されるプロシージャを呼び出せばいいじゃないかと思われますが、実行時にイベントオブジェクトに頼っている場合には呼び出し時にイベントオブジェクトを渡す必要があります。Source などの設定はコントロールのコントローラを取得する必要があるので面倒だったりします。 ** 画像サイズに合わせてイメージコントロールのサイズを変更する [#b73c236f] 画像のサイズに合わせてイメージコントロールのサイズを変更します。最大値を決めて比率を一定にするときなどの参考用。 コントロールの XControlModel からシェープを探し出す必要があります。それが分かれば後はサイズを計算します。 #code(ob){{ Sub Main oDoc = ThisComponent oDrawPage = oDoc.getDrawpage() oController = oDoc.getCurrentController() oForm = oDrawPage.getForms().getByIndex(0) oImageCtrl = oForm.getByName("ImageControl") oGraphic = oImageCtrl.Graphic oShape = FindShape( oDrawPage, oImageCtrl ) aBaseSize = oShape.Size aSize = CalculateSize( aBaseSize, oGraphic.Size ) oShape.Size = aSize End Sub Function FindShape( oContainer As Object, oModel ) As Object Dim oFound As Object For i = 0 To oContainer.getCount() -1 step 1 oShape = oContainer.getByIndex(i) If oShape.supportsService("com.sun.star.drawing.ControlShape") Then If EqualUNOObjects( oShape.Control, oModel ) Then oFound = oShape Exit For End If End If Next FindShape = oFound End Function Function CalculateSize( aShapeSize, aGraphicSize ) Dim aSize As New com.sun.star.awt.Size aSize.Width = aShapeSize.Height * ( aGraphicSize.Width / aGraphicSize.Height) aSize.Height = aShapeSize.Height CalculateSize = aSize End Function }} ** フォームコントロールを MRI で簡単に開く [#tbaa11d0] 実行するとドキュメント中にあるフォームコントロール一覧のツリー表示が表示され、選択して MRI ボタンを押すと MRI で開いてくれるマクロ。 #code(basic){{ 'Dim oTree As Object Dim oDoc As Object Dim oForms As Object Dim oDialog As Object Sub Main oDoc = ThisComponent If oDoc.supportsService("com.sun.star.text.TextDocument") Then oForms = oDoc.getDrawPage().getForms() ElseIf oDoc.supportsService("com.sun.star.sheet.SpreadsheetDocument") Then ' Spreadsheet needs problem about active sheet changing oForms = oDoc.getCurrentController().getActiveSheet().getDrawPage().getForms() ElseIf oDoc.supportsService("com.sun.star.drawing.DrawingDocument") Then oForms = oDoc.getCurrentController().getCurrentPage().getForms() Else Exit Sub End If ' create dialog oDialog = CreateUnoService("com.sun.star.awt.UnoControlDialog") oDialogModel = CreateUnoService("com.sun.star.awt.UnoControlDialogModel") oDialogModel.setPropertyValues( _ Array("Height", "PositionX", "PositionY", "Width"), _ Array(200, 50, 50, 130) ) oDialog.setModel(oDialogModel) ' AutoClose check box oCheckModel = oDialogModel.createInstance("com.sun.star.awt.UnoControlCheckBoxModel") oCheckModel.setPropertyValues(_ Array("Height", "Label", "PositionX", "PositionY", "State", "Width"), _ Array(13, "Auto Close", 1, 187, 1, 40)) oDialogModel.insertByName("AutoClose", oCheckModel) ' Close Button oCloseBtnModel = oDialogModel.createInstance("com.sun.star.awt.UnoControlButtonModel") oCloseBtnModel.setPropertyValues(_ Array("Height", "Label", "PositionX", "PositionY", "PushButtonType", "Width"), _ Array(13, "~Close", 94, 187, com.sun.star.awt.PushButtonType.CANCEL, 35)) oDialogModel.insertByName("CloseBtn", oCloseBtnModel) ' MRI Button oMRIBtnModel = oDialogModel.createInstance("com.sun.star.awt.UnoControlButtonModel") oMRIBtnModel.setPropertyValues(_ Array("Height", "Label", "PositionX", "PositionY", "PushButtonType", "Width"), _ Array(13, "~MRI", 58, 187, com.sun.star.awt.PushButtonType.STANDARD, 35)) oDialogModel.insertByName("MRIBtn", oMRIBtnModel) ' create tree control oTreeModel = oDialogModel.createInstance("com.sun.star.awt.tree.TreeControlModel") oTreeModel.setPropertyValues( _ Array("Height", "PositionX", "PositionY", "SelectionType", "ShowsRootHandles", "Width"), _ Array(186, 1, 1, com.sun.star.view.SelectionType.SINGLE, False, 128) ) oDialogModel.insertByName("Tree", oTreeModel) oTree = oDialog.getControl("Tree") oTreeDataModel = CreateUnoService("com.sun.star.awt.tree.MutableTreeDataModel") ' make new tree model structure oTreeDataModel = CreateUnoService( _ "com.sun.star.awt.tree.MutableTreeDataModel") ' top node oRootNode = oTreeDataModel.createNode("Forms", true) oRootNode.setCollapsedGraphicURL("private:graphicrepository/res/sx18013.png") oRootNode.setExpandedGraphicURL("private:graphicrepository/res/sx18013.png") ' set data to the root oTreeDataModel.setRoot(oRootNode) oTreeModel.DataModel = oTreeDataModel AddChildren(oForms, oTreeDataModel, oRootNode) oDialog.setVisible(True) oButtonListener = CreateUnoListener("ButtonPush_", "com.sun.star.awt.XActionListener") oMRIBtn = oDialog.getControl("MRIBtn") oMRIBtn.addActionListener(oButtonListener) oTree.expandNode(oRootNode) ' expand after dialog visible ExpandAllNodes(oTree, oRootNode) oMouseListener = CreateUnoListener("Mouse_", "com.sun.star.awt.XMouseListener") oTree.addMouseListener(oMouseListener) oDialog.execute() oTree.removeMouseListener(oMouseListener) oMRIBtn.removeActionListener(oButtonListener) 'oDialog.getControl("MRIBtn").removeActionListener(oButtonListener) 'oDialog.dispose() End Sub Sub CloseDialog() If oDialog.getControl("AutoClose").State = 1 Then oDialog.endExecute() End Sub Function GetFormControl(oForms As Object, oNode As Object) As Object Dim oControl As Object Dim sNames() As String While NOT IsNull(oNode) n = UBound(sNames) +1 ReDim Preserve sNames(n) sNames(n) = CStr(oNode.getDisplayValue()) oNode = oNode.getParent() WEnd ' find control oControl = oForms For i = UBound(sNames) -1 to 0 step -1 oControl = oControl.getByName(sNames(i)) Next GetFormControl = oControl End Function Sub SelectControl(oForms As Object, oNode As Object) As Object oControl = GetFormControl(oForms, oNode) If IsNull(oControl) Then Exit Sub oDoc.getCurrentController().select(oControl) End Sub Sub OpenWithMRI(oForms As Object, oControl As Object) oControl = GetFormControl(oForms, oControl) If IsNull(oControl) Then Exit Sub oController = ThisComponent.getCurrentController().getControl(oControl) Globalscope.BasicLibraries.loadLibrary("MRILib") MRILib.Mri(oController) End Sub Sub Mouse_mousePressed(oEv As com.sun.star.awt.MouseEvent) If oEv.Buttons = com.sun.star.awt.MouseButton.RIGHT Then oTree = oEv.Source.getContext().getControl("Tree") oNearNode = oTree.getClosestNodeForLocation(oEv.X,oEv.Y) If IsNull(oNearNode) Then Exit Sub aRect = CreateUnoStruct("com.sun.star.awt.Rectangle") aRect.X = oEv.X + oTree.PosSize.X aRect.Y = oEv.Y + oTree.PosSize.Y oPopup = CreateUnoService("stardiv.vcl.PopupMenu")'"com.sun.star.awt.PopupMenu") oPopup.insertItem(1, "Select", 0, 0) oPopup.insertItem(2, "MRI", 0, 1) oPopup.setCommand(1, "select") oPopup.setCommand(2, "mri") n = oPopup.execute(oTree.getContext().getPeer(), aRect, _ com.sun.star.awt.PopupMenuDirection.EXECUTE_DEFAULT) If n > 0 Then sCommand = oPopup.getCommand(n) Select Case sCommand Case "select" SelectControl(oForms, oNearNode) Case "mri" OpenWithMRI(oForms, oNearNode) End Select 'oEv.Source.getContext().endExecute() ' close dialog CloseDialog() End If End If End Sub Sub Mouse_mouseReleased(oEv) If oEv.Buttons = com.sun.star.awt.MouseButton.LEFT and oEv.ClickCount = 2 Then oTree = oEv.Source.getContext().getControl("Tree") oNearNode = oTree.getClosestNodeForLocation(oEv.X,oEv.Y) If IsNull(oNearNode) Then Exit Sub OpenWithMRI(oForms, oNearNode) CloseDialog End If End Sub Sub Mouse_mouseEntered(ev) End Sub Sub Mouse_mouseExited(ev) End Sub Sub Mouse_disposing(ev) End Sub Sub ButtonPush_actionPerformed(oEv As com.sun.star.awt.ActionEvent) ' get selected item Dim oSelected() As Object oTree = oEv.Source.getContext().getControl("Tree") oSelected = oTree.getSelection() If IsArray(oSelected) Then Exit Sub Else OpenWithMRI(oForms, oSelected(0)) CloseDialog End If End Sub Sub ButtonPush_dispose(oEv) End Sub Sub AddChildren( oContainer As Object, oDataModel As Object, oParent As Object ) ' index is better about their order in the tree 'sNames = oContainer.getElementNames() 'For i = 0 To UBound(sNames) step 1 'sName = sNames(i) For i = 0 To oContainer.getCount() - 1 step 1 'If oContainer.hasByName(sName) Then 'oModel = oContainer.getByName(sName) oModel = oContainer.getByIndex(i) sName = oModel.getName() sImageURL = GetImageURL(oModel.getServiceName()) If HasUnoInterfaces(oModel, _ "com.sun.star.container.XContainer") Then oChild = oDataModel.createNode(sName, true) AddChildren(oContainer.getByName(sName), oDataModel, oChild) Else oChild = oDataModel.createNode(sName, false) End If oChild.setExpandedGraphicURL(sImageURL) oChild.setCollapsedGraphicURL(sImageURL) oParent.appendChild(oChild) 'End If Next End Sub Sub ExpandAllNodes(oTree As Object, oParent) For i = 0 To oParent.getChildCount() - 1 step 1 oChild = oParent.getChildAt(i) oTree.expandNode(oChild) If oChild.getChildCount() > 0 Then ExpandAllNodes(oTree, oChild) End If Next End Sub Function GetImageURL(sServiceName As String) As String Dim sName As String sControlService = Mid(sServiceName, 28) 'stardiv.one.form.component.Form sServices = Array(_ "CheckBox", "ComboBox", "CommandButton", _ "CurrencyField", "DateField", "Edit", "FileControl", _ "FixedText", "Form", "FormattedField", _ "Grid", "GroupBox", "HiddenControl", _ "ImageButton", "ImageControl", "ListBox", _ "NumericField", "PatternField", "RadioButton", _ "TextField", "TimeField", _ ".NavigationToolBar", ".SpinButton", ".ScrollBar") sNames = Array(_ "sx10596", "sx10601", "sx10594", _ "sx10707", "sx10704", "sx10599", "sx10605", _ "sx10597", "sx10593", "sx10728", _ "sx10603", "sx10598", "sx18022", _ "sx10604", "sx10710", "sx10600", _ "sx10706", "sx10708", "sx10595", _ "sx10599", "sx10705", _ "sx10607", "sx10769", "sx10768") For i = 0 To UBound(sServices) step 1 If sServices(i) = sControlService Then sName = sNames(i) Exit For End If Next GetImageURL = "private:graphicrepository/res/" & sName & ".png" End Function }} 以前 MRI を使うツール公開していいか?ってメールがきてアイデアみたいなのが書いてあったからそれを適当にマクロにしたもの。向こうが作ったのを送ってきた後でこっちの送ったけど、なんとかかんとかって返ってきた、その後は音沙汰なし。これも置いておいていいかな・・・。 色々機能を付け加えてウィンドウに変更してフォームナビゲータみたいにしようかと思ったけど面倒なので辞め。 |