LibreOffice Calc: Hieman edistyneempi versio, vuorovaikutteinen valintaikkunan taulukko objekti - päivittyvä laskentataulukon kaavio valintaikkunan kuvaobjektiin
Käyttö: Valitse valikkoriviltä ChartDialog -> OpenDialog, tuplaklikkaa mitä tahansa numeerisen arvon sisältävää taulukon solua, kirjoita jokin uusi arvo ja paina Enrter -näppäintä.
' Valintaikkunan (Dialog1) ohjausobjektit:
' Taulukko (GridControl1)
' Sidottu tapahtumiin:
' Kun kohdistus saavutetaan -> SetFocusOnGrid
' Näppäintä painettu -> GridKeyDown(oEvent)
' Hiiren painike vapautettu -> GridMouseUp(oEvent)
' Kuvaobjekti (ImageControl1)
' Tekstiruutu (TextField1)
' Tekstin tasaus -> oikea
' Suurin tekstinpituus 4
' Ei käytössä
' Sidottu tapahtumiin:
' Näppäintä painettu -> editBoxKeyDown(oEvent)
' Kun kohdistus menetetään -> editBoxLostFocus
' Module1
REM ***** BASIC *****
Private oDlg As Object
Private oImg As Object
Private oGrid As Object
Private oDataModel As Object
Private oGridModel As Object
Private oSheet As Object
Private Continue As Boolean
Private done As Boolean
Private msgLabel As Object
Private editBox As Object
Private editBoxOriginalX As Long
Private editBoxOriginalY As Long
Private colWidths() As Long
Private colcnt As Integer
Private rowcnt As Integer
Private xrow As Integer
Private xcolumn As Integer
Private ReInit As Boolean
Private EditMode As Boolean
Private done As Boolean
Sub ShowDialog
If Continue Then Exit Sub
DialogLibraries.LoadLibrary("Standard")
oDlg = CreateUnoDialog(DialogLibraries.Standard.Dialog1)
oImg = oDlg.getControl("ImageControl1")
Dim oListenerTop As Object
oListenerTop=createUnoListener("TopListen_", "com.sun.star.awt.XTopWindowListener")
oDlg.addTopWindowlistener(oListenerTop)
CenterDialog
oSheet = ThisComponent.Sheets.getByIndex(0)
Initialize
ExportChartToImage
Continue = True
done = False
DoEvents
Do while Continue
Wait 20
oDlg.setVisible(true)
If Not done Then
done = True
oGrid.gotoCell(0,1)
End If
Loop
oDlg.dispose()
End Sub
Sub TopListen_WindowClosing
Continue = False
ThisComponent.setModified(False)
End Sub
Sub TopListen_windowOpened
End Sub
Sub TopListen_windowClosed
End Sub
Sub TopListen_windowMinimized
End Sub
Sub TopListen_windowNormalized
End Sub
Sub TopListen_windowActivated
End Sub
Sub TopListen_windowDeactivated
End Sub
Sub TopListen_disposing
End Sub
Sub CenterDialog
Dim oSize As New com.sun.star.awt.Size, factor As Double
Dim oCC, oComponentWindow, oTopWindowPosSize
oCC = ThisComponent.getCurrentController()
oComponentWindow = oCC.ComponentWindow
oTopWindowPosSize = oComponentWindow.Toolkit.ActiveTopWindow.getPosSize()
oSize.Width = oDlg.Model.Width
oSize.Height = oDlg.Model.Height
factor = oSize.Width / oDlg.convertSizeToPixel(oSize, com.sun.star.util.MeasureUnit.APPFONT).Width
With oDlg.Model
.PositionX = (factor * oTopWindowPosSize.Width - .Width) / 2
.PositionY = (factor * oTopWindowPosSize.Height - .Height) / 2
End With
End Sub
Sub Initialize
oGrid = oDlg.getControl("GridControl1")
oDataModel = CreateUnoService("com.sun.star.awt.grid.DefaultGridDataModel")
oGridModel = oDlg.Model.getByName("GridControl1")
oGridModel.ColumnHeaderHeight = 10
Dim oDoc As Object
Dim oCell As Object
oDoc = ThisComponent
Dim oCursor As Object
oCursor = oSheet.createCursor()
oCursor.gotoEndOfUsedArea(False)
colcnt = oCursor.RangeAddress.EndColumn
rowcnt = oCursor.RangeAddress.EndRow
oCursor = Nothing
Dim columnModel As Object
columnModel = oGridModel.ColumnModel
If ReInit Then Goto jmp
Dim columns(0 To colcnt) As Object
For i = 0 to colcnt
columns(i) = columnModel.createColumn()
oCell = oSheet.getCellByPosition(i, 0)
columns(i).Title = String(5, " ") & oCell.getString()
columnModel.addColumn(columns(i))
Next i
jmp:
Dim rows(rowcnt) As Variant, values As String
For i = 1 To rowcnt
values = ""
For j = 0 To colcnt
pad = ""
oCell = oSheet.getCellByPosition(j, i)
If IsNumeric(oCell.getString()) Then
lgt = 38 - Len(oCell.getString())
pad = String(lgt, " ")
ElseIf i < 2 Then
pad = String(13, " ")
End If
values = values & pad & oCell.getString() & "¤"
Next j
rows(i) = Split(values, "¤")
oDataModel.addRow(oDataModel.RowCount, rows(i))
Next i
oGridModel.GridDataModel = oDataModel
If ReInit Then Goto jmp2
Redim colWidths(colcnt)
Dim maxLen As Integer
Dim charWidthFactor As Integer
charWidthFactor = oSheet.getCellByPosition(0, 0).CharHeight * 0.35
For i = 0 To colcnt
columns(i).ColumnWidth = 16 * charWidthFactor
colWidths(i) = columns(i).ColumnWidth
Next i
editBox = oDlg.getControl("TextField1")
editBoxOriginalX = editBox.Model.PositionX
editBoxOriginalY = editBox.Model.PositionY
jmp2:
ReInit = False
End Sub
Sub ExportChartToImage
Dim oDoc As Object
Dim oDrawPage As Object
Dim oShape As Object
oDoc = ThisComponent
oDrawPage = oDoc.getSheets().getByIndex(0).getDrawPage()
oShape = oDrawPage.getByIndex(0)
If IsNull(oShape) Then Exit Sub
If Not IsNull(oShape.Graphic) Then
oImg.Model.Graphic = oShape.Graphic
End If
End Sub
Sub ActivateEditBox
Dim xlocation As Long, i As Integer
xrow = oGrid.CurrentRow : xcolumn = oGrid.CurrentColumn
If Not IsNumeric(Replace(oDataModel.getCellData(xcolumn,xrow)," ",""))Then Exit Sub
If oGrid.CurrentColumn > 0 Then
For i = 0 To oGrid.CurrentColumn - 1
xlocation = xlocation + colWidths(i)
Next i
End If
Dim editBoxM As Object
editBoxM = editBox.Model
editBoxM.Enabled = True
editBoxM.Width = colWidths(oGrid.CurrentColumn) - 2
editBoxM.PositionY = oGrid.Model.PositionY + oGridModel.ColumnHeaderHeight + 1 + (oGrid.CurrentRow * 10)
editBoxM.PositionX = oGrid.Model.PositionX + 1 + xlocation
editBoxM.Text = Replace(oDataModel.getRowData(oGrid.CurrentRow)(oGrid.CurrentColumn), " ", "")
editBoxM.BackGroundColor = &Hffffff
oSelection = New com.sun.star.awt.Selection
oView = editBox.getView()
oView.setFocus()
textLen = Len(editBoxM.Text)
oSelection.Min = textLen
oSelection.Max = textLen
oView.setSelection(oSelection)
oSelection = Nothing
oView = Nothing
End Sub
Sub GridMouseUp(oEvent)
If oEvent.ClickCount = 2 And oEvent.Buttons = 1 Then
Dim colsW As Long, i As Integer
Dim gridX1 As Long, gridX2 As Long, gridY1 As Long, gridY2 As Long
For i = Lbound(colWidths) To Ubound(colWidths)
colsW = colsW + colWidths(i)
Next i
gridX1 = oGrid.Model.PositionX + oGridModel.RowHeaderWidth + 1
gridX2 = oGrid.Model.PositionX + oGridModel.RowHeaderWidth + 1 + colsW * 2
gridY1 = oGrid.Model.PositionY + oGridModel.ColumnHeaderHeight + 1
gridY2 = oGrid.Model.PositionY + oGridModel.ColumnHeaderHeight + 1 + (rowcnt * 20 )
If oEvent.X >= gridX1 And oEvent.X <= gridX2 Then
If oEvent.Y >= gridY1 And oEvent.Y <= gridY2 Then
ActivateEditBox
End If
End If
End If
End Sub
Sub GridKeyDown(oEvent)
If oEvent.Modifiers AND com.sun.star.awt.KeyModifier.MOD2 And oEvent.KeyCode = com.sun.star.awt.Key.X Then
ActivateEditBox
End If
End Sub
Sub SetFocusOnGrid
oSelection = New com.sun.star.awt.Selection
oView = oGrid.getView()
oView.setFocus()
oView = Nothing
oSelection = Nothing
End Sub
Sub Update
For i = rowcnt - 1 To 0 Step -1
oDataModel.removeRow(i)
Next i
editBox.Model.PositionX = editBoxOriginalX
editBox.Model.PositionY = editBoxOriginalY
ReInit = True
Initialize
ExportChartToImage
ThisComponent.store()
SetFocusOnGrid
End Sub
Sub editBoxKeyDown(oEvent)
If oEvent.KeyCode = com.sun.star.awt.Key.RETURN Then
Dim editedText As String
editedText = editBox.Model.Text
oSheet.getCellByPosition(xcolumn, xrow + 1).Value = Val(Replace(editedText, " ", ""))
ThisComponent.calculateAll()
Wait 50
Update
Else
Select Case oEvent.KeyCode
Case 256 To 265
Case Else
If Len(editBox.Model.Text) < 2 Then
editBox.Model.Text = ""
Else
editBox.Model.Text = Left(editBox.Model.Text, Len(editBox.Model.Text) - 1)
oView = editBox.getView()
oSelection = New com.sun.star.awt.Selection
textLen = Len(editBox.Model.Text)
oSelection.Min = textLen
oSelection.Max = textLen
oView.setSelection(oSelection)
oSelection = Nothing
oView = Nothing
End If
End Select
End If
End Sub
Sub editBoxLostFocus
editBox.Model.PositionX = editBoxOriginalX
editBox.Model.PositionY = editBoxOriginalY
editBox.Model.Enabled = False
End SubHalutessaan valmiin testiprojektin voi imaista täältä
HV (hyvää vappua jo etukäteen)