Width of GRID units does not match width of COLUMNS

I have a grid, created by a BASIC macro, that needs to contain 4 columns. I fail to match the width of the grid with the width of the columns :

const kWidthCol1 = 30
const kWidthCol2 = 50
const kWidthCol3 = 10
const kWidthCol4 = 75
...
	hEventGrid = makeGrid (hDialog, "ttt", 20, 20, kWidthCol1+kWidthCol2+kWidthCol3+kWidthCol4, 150)
	hColumn1 = makeColumn (hEventGrid, "Datum", kWidthCol1)
	... etc

function makeGrid (oDlg, Title, x, y, w, h) as object
	dim oGridControl as object
	dim oGridModel as object
	oGridControl = createUnoService ("com.sun.star.awt.grid.UnoControlGrid")
'	oGridModel = oDlg.createInstance ("com.sun.star.awt.grid.UnoControlGridModel")
	oGridModel = createUnoService ("com.sun.star.awt.grid.UnoControlGridModel")
	oGridModel.useGridLines = true
	oGridControl.setModel(oGridModel)
	oGridControl.setPosSize (x, y, w, h, com.sun.star.awt.PosSize.POSSIZE)
	oDlg.addControl (Title, oGridControl)
	makeGrid = oGridControl
end function

function makeColumn (table, title, width) as object
	dim col as object
	col = createUnoService ("com.sun.star.awt.grid.GridColumn")
	col.Title = title
	col.columnWidth = width
	col.maxWidth = width
	col.minWidth = width
	col.Resizeable = false
	table.model.ColumnModel.addColumn (col)
	makeColumn = col
end function

Resulting in a grid that is far too small :
Screenshot 2025-02-04 at 15.54.26

This suggests to me that the width of the grid is using a different unit than the width of the columns. If so, what is the relation between these two units ?

possible clue from Service UnoControlGridModel
image

I finally managed to find a workaround :

  • “createUnoService (“com.sun.star.awt.grid.UnoControlGrid”)” uses SCREEN coordinates
  • "createUnoService (“com.sun.star.awt.grid.GridColumn”) uses coordinates from the DIALOG editor
    The relationship between those coordinates (which is dependant on the screen resolution) can be obtained as follows :
  1. Make a grid in the dialog editor, sized 100x100. Location is not important, size can actually be anything. Give it a name so it can be picked up the code… I used “ScaleFactors”.
  2. To find the relationship, pick up “ScaleFactors”. grid.model.width uses dialog coordinates, grid.PosSize.Width uses screen coordinates. The same is valid for height.
  3. The scale factor, which is different for width and height, is found by dividing the two coordinates.
  4. Since dialog editor does not allow for setting “ScaleFactors” to hidden, it must be done from withing the code.

The code I used :

const kWidthCol1 = 40
const kWidthCol2 = 100
const kWidthCol3 = 200
const kWidthCol4 = 50

' make the grid using dialog coordinates
	hEventGrid = makeGrid (hDialog, "EventGrid", 10, 10, kWidthCol1+kWidthCol2+kWidthCol3+kWidthCol4, 70)
' and make the columns, see below

function makeGrid (oDlg, Name, x, y, w, h) as object
	dim oGridScaleFactors
	dim scalex, scaley
	oGridScaleFactors = hdialog.getcontrol ("ScaleFactors")
	scalex = oGridScaleFactors.PosSize.Width / oGridScaleFactors.model.width
	scaley = oGridScaleFactors.PosSize.Height / oGridScaleFactors.model.height
	oGridScaleFactors.visible = false
	
	dim oGridControl as object
	dim oGridModel as object
	oGridControl = createUnoService ("com.sun.star.awt.grid.UnoControlGrid")
	oGridModel = oDlg.model.createInstance ("com.sun.star.awt.grid.UnoControlGridModel")
	oGridControl.setModel(oGridModel)
	oGridControl.setPosSize (x*scalex, y*scaley, w*scalex, h*scaley, com.sun.star.awt.PosSize.POSSIZE)
	oGridModel.name = Name
	makeGrid = oGridControl
end function

For fitting the columns in the grid, set the columns to a fixed width using dialog coordinates.
The last column is not fixed, but has MIN and MAX widths, so it adjusts in the remaining spaces left over, taking into account if there is a vertical scrollbar. It also needs to be resizable.

hColumn1 = makeColumn (hEventGrid, "Datum", kWidthCol1)
hColumn1.HorizontalAlign = com.sun.star.style.HorizontalAlignment.CENTER
hColumn2 = makeColumn (hEventGrid, "Gebeurtenis", kWidthCol2)
hColumn3 = makeColumn (hEventGrid, "VT", kWidthCol3)
hColumn3.HorizontalAlign = com.sun.star.style.HorizontalAlignment.CENTER

hColumn4 = makeColumn (hEventGrid, "Opmerking", 0)
hColumn4.columnWidth = 0
hColumn4.maxWidth = kWidthCol4 + 100
hColumn4.minWidth = 10
hColumn4.Resizeable = true

function makeColumn (grid, title, width) as object
	dim col as object
	col = createUnoService ("com.sun.star.awt.grid.GridColumn")
	col.Title = title
	col.Resizeable = false
	grid.model.ColumnModel.addColumn (col)
	makeColumn = col
end function