Tech Tip: Get the best display width and height for the given text data
PRODUCT: 4D | VERSION: 17 | PLATFORM: Mac & Win
Published On: May 15, 2019
The command OBJECT GET BEST SIZE returns the bestWidth and bestHeight parameters, the “optimal” width and height of the form object. However, this command only works in a context of a form execution.
With the introduction of the dynamic forms, it is possible to build a simple form definition and dynamically load as offscreen window to allow execution of the OBJECT GET BEST SIZE command. The following method is written as a self-contained utility method that obtains the bestWidth and bestHeight for a given text without having to rely on a pre-constructed of a form.
// ---------------------------------------------------- // Method: TEXT_GET_DISPLAY_BEST_SIZE // Description // Get the best display width and height for the given text data // // Parameters // $1 - Text // $2 - Font name // $3 - Font size // $4 - (Optional) Maximum display width // // Return // $0 - Object { "bestWidth": 100, "bestHeight": 24 } // ---------------------------------------------------- C_TEXT($1;$data_t;$2;$fontName_t) C_LONGINT($3;$fontSize_l;$4;$maxWidth_l;$win_l) If (Count parameters>=3) $data_t:=$1 $fontName_t:=$2 $fontSize_l:=$3 If (Count parameters>=4) $maxWidth_l:=$4 End if C_OBJECT($0;$formDef_o;$formData_o) $formDef_o:=New object("destination";"detailScreen";"rightMargin";19;\ "bottomMargin";20;"markerHeader";15;"markerBody";200;"markerBreak";\ 220;"markerFooter";240;"events";New collection("onLoad");"method";\ Current method name;"pages";New collection("Null";New object("objects";\ New object("Variable";New object("type";"input";"top";21;"left";16;\ "width";65;"height";17;"fontFamily";$fontName_t;"fontSize";\ $fontSize_l;"focusable";False;"enterable";False;"dragging";"none";\ "dropping";"custom"))))) $formData_o:=New object("data";$data_t;"maxWidth";$maxWidth_l) $win_l:=Open window(-10;-10;-10;-10) DIALOG($formDef_o;$formData_o) CLOSE WINDOW($win_l) $0:=New object("width";$formData_o.bestWidth;"height";$formData_o.bestHeight) Else If (Form event=On Load) // Execution occurs within the context of the Form method C_LONGINT($bw;$bh) OBJECT Get pointer(Object named;"Variable")->:=Form.data If (Form.maxWidth>0) OBJECT GET BEST SIZE(*;"Variable";$bw;$bh;Form.maxWidth) Else OBJECT GET BEST SIZE(*;"Variable";$bw;$bh) End if Form.bestWidth:=$bw Form.bestHeight:=$bh CANCEL End if End if |