8
$\begingroup$

I've often wanted to set the size of an interface element to something like Full - 50, so as to not over-constrain box sizes.

One approach to this would be to simply use a Dynamic[boxsize - 50] as my ImageSize.

Unfortunately, for boxes that information isn't possible to extract by normal means.

I've always resorted to hacks and workarounds, but I don't see what that info shouldn't be possible to extract from the FE.

In digging I found FrontEnd`GetBoundingBoxSizePacket, but it always returns one of two things. Either it gives:

FrontEndExecute[FrontEnd`GetBoundingBoxSizePacket@EvaluationCell[]]

{{34., 13., 7.}}

Or

FrontEndExecute[FrontEnd`GetBoundingBoxSizePacket@1]

$Failed

No matter what FE object I pass to it, it only ever returns one size.

Does anyone know can I get the size of a BoxObject?

$\endgroup$

1 Answer 1

4
$\begingroup$

GetBoundingBoxSizePacket seems to only operate on Cell expressions, so we can do:

getBoxSize[c_Cell] := {#[[1]], Total@#[[2 ;;]]} &@
   First@FrontEndExecute@GetBoundingBoxSizePacket[c];
getBoxSize[c_CellObject] := getBoxSize[NotebookRead[c]];
getBoxSize[b_BoxObject] :=
  getBoxSize[
   Cell[BoxData@NotebookRead[b],
    "Output",
    PageWidth -> Infinity,
    ShowCellBracket -> False,
    CellMargins -> {{0, 0}, {0, 0}}
    ]
   ];
getBoxSize[e_] :=
  getBoxSize[
   Cell[BoxData@ToBoxes[e],
    "Output",
    PageWidth -> Infinity,
    ShowCellBracket -> False,
    CellMargins -> {{0, 0}, {0, 0}}
    ]
   ];

And we get what we expect:

c = EvaluationCell[];
getBoxSize@EvaluationCell[]
Rasterize[c, "RasterSize"]

{215., 60.}

{215, 60}

Dynamic[boxo = EvaluationBox[]]

Rasterize[Dynamic[EvaluationBox[]], "RasterSize"]
getBoxSize@boxo

{136, 26}

{135., 26.}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.