Re: [fltk.general] bit of callback help needed

86 views
Skip to first unread message

Ian MacArthur

unread,
May 20, 2013, 4:47:06 PM5/20/13
to fltkg...@googlegroups.com
On 20 May 2013, at 16:52, bat wrote:

> Now my mind-numbingly stupid question is.....


There are no stupid questions... (Well, unless you ask the same thing multiple times, then it starts to get stupid I guess, but you know what I mean, anyway...)


> is there a way to send the result(s) of a callback function back to 'main' in a way that is independent of these widget things so that the info can be used by another callback function? I have tried a few things, but nothing gets transferred to the second callback function. Sorry, I don't really understand what I am doing, I just copy other peoples' code and spot patterns in it, etc, but any clues at all on this or pointers to other examples I can copy would be awesomely useful!


Can you describe what it is you want to achieve in a broader sense?
Maybe folks here will be able to suggest something.

If possible, avoid the temptation to describe a particular implementation, as it is quite possible that a better way will occur to someone.
Similarly, try to avoid using any jargon words unless you are *sure* the meaning you assign to them matches what others here will expect - try and use "plain" language to describe your objective. Then we'll pitch in with some options!

Cheers, and have fun with fltk!



Greg Ercolano

unread,
May 20, 2013, 5:16:03 PM5/20/13
to fltkg...@googlegroups.com
On 05/20/13 08:52, bat wrote:
> is there a way to send the result(s) of a callback function back
> to 'main' in a way that is independent of these widget things so
> that the info can be used by another callback function?

There's a few ways to go.

The easiest is to use globals that both the callbacks and main()
have access to. But globals are considered bad programming practice,
since it makes modules dependent on globals, and thereby less 'modular'.

The more common way (but involves some work on your part)
is to set up your callbacks to have userdata that points to
this data you want main() and the widgets to have access to.
This way the widgets can access the data through the userdata
instead of through a global.. this makes the widget more modular
(so that it can be used in other applications, or can be instanced
so as to refer to different data, instead of just of fixed globals)

A more flexible approach (for very large apps) is to send messages
between widgets, avoiding callback data and pointers entirely.
I like to use text messages, which allows one to easily see messages
flying back and forth, and lets one make 'scripts' to trigger GUI events,
and also to have ascii config files that allow users to customize menus
and buttons, and make custom text messages send when those buttons/menus are hit.

It's common to make an "Application" class in which all widgets and windows
exist, and has all global data inside of it. This way you can have several
instances of the application class, so that main() can access data through
the application class, as well as the widgets.

Michael Surette

unread,
May 20, 2013, 7:21:26 PM5/20/13
to fltkg...@googlegroups.com
If your application has any significant size, I'm a big fan of the messages that Greg mentioned.  If you check out the Wikipedia entry for "signals and slots", you'll find a number of libraries which may help.



--
You received this message because you are subscribed to the Google Groups "fltk.general" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkgeneral...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.





--
Mike

Michael Werner

unread,
May 22, 2013, 1:41:58 PM5/22/13
to fltkg...@googlegroups.com

> my question is..... is there a way to send the result(s) of a callback function back to 'main' in a way that is
> independent of these widget things so that the info can be used by another callback function?

I started out just like you. All of my FLTK stuff was originally based on sample codes.
But I've been doing it for a while now and am slowly getting away from that approach.

Concerning your question, I agree with the other poster who suggested having your widgets talk directly to each other.  That way you bypass the need to send values back to your main program, then back to FLTK, (which is what I would have done a few years ago). I've included some code below demonstrating the syntax. It is the callback for a widget called 'Reset_Nit_All'.  As you can see it get and sends values directly to other widgets ('Select_Vertebra' and 'Reset_Nit_Vert').

By the way, I use FLUID exclusively to design my GUIs.  Callbacks can be defined in FLUID which is where the code below appears. The more I use FLTK the more I define callbacks within it instead of within my application code. I'm also finding that you really don't need global variables for GUI related values. Instead, you simply query a FLTK widget for it's value whenever it's needed by the application. The widgets themselves become your global variables.  Good luck.

---------------------------------------------------------------------------------------------------------------------------------------------
Callback routine for widget 'Reset_Nit_All'
---------------------------------------------------------------------------------------------------------------------------------------------

   int vv, qq;

   qq = Select_Vertebra->value();

   for (vv = 0; vv < 25; vv++)  {

      Select_Vertebra->value(vv);
      Reset_Nit_Vert->do_callback();
   }

    Select_Vertebra->value(qq);

-------------------------------------------------------------------------------------------------------------------------------------

bat

unread,
Aug 5, 2013, 2:32:34 PM8/5/13
to fltkg...@googlegroups.com
A belated thank you to all who replied to my post in May.... finally trying to sort this out with the simplest solution of using a global variable. When I can do this, I will try one of the better methods! I gather FLUID can probably do all this automatically but I'd rather understand the fundamentals first!!

What I need is a *very* simple beginner's C++ example of how a call-back function can set a global variable to a certain value and send the result back to 'main'. As a C++ beginner, I have a rough idea of how functions can get data from main and send results back to it, but the syntax of the current FLTK examples I have is a bit too complicated and the 'text-book' examples of function-calls don't seem to work in the context of 'my' current program.

Georg Potthast

unread,
Aug 5, 2013, 5:15:43 PM8/5/13
to fltkg...@googlegroups.com
I guess my tutorial can help you, it is written for absolute beginners and the examples in there are very simple: FLTK tutorial

Chapter 6 discusses callback functions.

Georg

bat

unread,
Aug 30, 2013, 10:55:32 AM8/30/13
to fltkg...@googlegroups.com
Thanks again for that. I finally found out what I was doing wrong... in each function, I was initialising the global (duh, yes really). Thought this might help in case someone else ever has a similar mindless problem. Incidentally my 'globals' are declared within a class (as in one of the other tutorials on the web), which I gather is a lesser sin than making them completely global. 
Reply all
Reply to author
Forward
0 new messages