07 February 2013

SetFont 0 or Null?

In 2002 Peter Harder wrote:  "SetFont 0" oder "SetFont ungueltiges_handle%" sorgen im Gegensatz zu GFA16 bei GB32 für einen Programmabbruch. Tückisch vor allem  dann, wenn dieses Problem beim Umschreiben eines GFA16-Programms ' erst beim Kunden auffällt; ein Hinweis in der Onlinehilfe auf  diesen Unterschied fehlt.

What is this about SetFont?
In previous versions of GB the statement SetFont 0  was often used to select a default stock object font in the current active device context. Peter’s remark suggests that this functionality is missing from GFA-BASIC 32. However, it isn’t actually missing, but the data type of the argument is a Variant, rather then an integer. The 0 value is automatically converted to a Numeric Variant (data subtype = basInt) with value 0. This means: 0 is a valid numeric value and is handled as if the font-handle with value 0 is passed to SetFont.

It throws an error
The SetFont library routine checks for a numeric value in the Variant and uses that value to select a stock object. However, the valid value 0 references the WHITE_BRUSH stock object and not a font. SetFont stops and throws an error. ( The error “Illegal Function call”, which, of course, should have been “Invalid Parameter”.)

Use Null instead
To have SetFont select a stock object, to be precise, the DEFAULT_GUI_FONT, the Variant argument of SetFont must be set to the VT_NULL variant subtype. This is done by using the Null constant.

SetFont Null

The Null keyword is used with a Variant to indicate that it intentionally contains no valid data.

But what if you have a variable that is holding a font handle? Well, in case the font handle is 0, you would want to pass a VT_NULL variant subtype, so you must convert it to Null. You could use the explicit Null constant as above, but you can also use an undocumented function Null(value), which is replaced by a simple Null.

Global font%
If font%==
SetFont Null(font%