To be honest: I am a little bit frustrated. In "Beginners" http://www.freebasic.net/forum/viewtopi ... =2&t=23186 there is a long discussion about GUI programming in FreeBasic, and RockTheSchock points out that GUI coding in FreeBasic is (or can be) very cryptical. I understand your code, but it looks very tricky to avoid global variable names in FLTK. I was also stumbling over the callback function of your example in the thread in "Beginners":
Code: Select all
#include once "fltk-c.bi"
sub InputCB cdecl (byval self as FL_WIDGET ptr,byval arg as any ptr)
dim as Fl_Input ptr ptr in = arg
dim as double a = val(*Fl_Input_GetValue(in[0]))
dim as double b = val(*Fl_Input_GetValue(in[1]))
Fl_Input_SetValue in[2], str(a+b)
end sub
'
' main
'
var win = Fl_WindowNew(250,100,"add")
dim as any ptr in(...) = {Fl_Float_InputNew(40, 10, 200, 20,"a"), _
Fl_Float_InputNew(40, 40, 200, 20,"b"), _
Fl_Float_InputNew(40, 70, 200, 20,"a+b=") }
Fl_WidgetSetCallbackArg in(0),@InputCB,@in(0)
Fl_WidgetSetCallbackArg in(1),@InputCB,@in(0)
Fl_Input_SetReadonly in(2),1
Fl_WindowShow win
Fl_Run
I cannot see in the moment how a complex GUI code with many different widget types can be written in a clearly readable way without using global variables. In this example, you can only pass widgets of the same type as argument to the callback function within an array, not widgets of different types. Using an array means that you cannot give individual names to these widgets, but you have to use indexes which makes the code more difficult to write and to read if you have a larger number of widgets.
In the other example above you can handle only three widgets in the callback function, using "byval self as Fl_Widget Ptr", "byval other as any ptr" and "Fl_WidgetSetUserData / Fl_WidgetGetUserData" (self and other - a very special trick!).
Is there a general and simple method to avoid global variables?