how can i declare & define Browser callback?

16 views
Skip to first unread message

Dave Jordan

unread,
Jun 18, 2021, 1:18:15 PM6/18/21
to fltkg...@googlegroups.com
I haven't found any examples that use a subclassed Browser with .h & .cpp files.
brwsr.h
#include <FL/Fl.H>
#include <FL/Fl_Hold_Browser.H>
struct Brwsr : Fl_Hold_Browser {
callback(brwsr_cb);
// more callback-related decls?
}

brwsr.cpp
#include "brwsr.h"
Brwsr::Brwsr() {
void brwsr_cb2();
static void brwsr_cb(Fl_Widget*,void *v) {
((Brwsr*)v)->brwsr_cb();
}
}
Brwsr::brwsr_cb2() { ... }

Ian MacArthur

unread,
Jun 18, 2021, 1:37:56 PM6/18/21
to Fltk General
On 18 Jun 2021, at 18:18, Dave Jordan wrote:
>
> I haven't found any examples that use a subclassed Browser with .h & .cpp files.

Ummm, OK... What are you wanting to achieve? Your sample code looks a wee bit... odd.

Have you looked at examples/howto-browser-with-icons.cxx - does that not show what you need?
And of course test/browser.cxx.


> brwsr.h
> #include <FL/Fl.H>
> #include <FL/Fl_Hold_Browser.H>
> struct Brwsr : Fl_Hold_Browser {

That’s “unconventional", I would suggest, subclassing a class object as a struct. That would not be considered idiomatic C++ code, generally.


> callback(brwsr_cb);
> // more callback-related decls?
> }


What are you planning on doing, with a subclassed callback method? That’s not what folks usually do - the callback method is pretty generic, so you can pass it pretty much any callback function to call.


>
> brwsr.cpp
> #include "brwsr.h"
> Brwsr::Brwsr() {
> void brwsr_cb2();
> static void brwsr_cb(Fl_Widget*,void *v) {
> ((Brwsr*)v)->brwsr_cb();
> }
> }
> Brwsr::brwsr_cb2() { ... }
>
> --
> 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.
> To view this discussion on the web visit https://groups.google.com/d/msgid/fltkgeneral/CAPdW6bLwnBPWGWOJCrnvg6QdQbiZYk3cZ0btb5Teh3huwNv8UQ%40mail.gmail.com.

Dave Jordan

unread,
Jun 18, 2021, 5:51:18 PM6/18/21
to fltkg...@googlegroups.com
I have my entire program working but the code is not organized so I am redoing it all to come into some sort of sensible order.
In this refactoring I'm splitting out all the classes into .cpp and .h files and changing them to structs for now b/c most of the objects / variables in my code need to be referenced in multiple other classes. Thing is, the examples that ship with FLTK do not use subclass header files. I think I have figured out just now how to declare & define the callback; the last compile has no errors. I will have to wait and see if it works the same as the old.
-dave

 

Greg Ercolano

unread,
Jun 18, 2021, 7:10:11 PM6/18/21
to fltkg...@googlegroups.com
On 6/18/21 10:18 AM, Dave Jordan wrote:
Subject: how can i declare & define Browser callback?

I haven't found any examples that use a subclassed Browser with .h & .cpp files.

    It sounds like you're looking for an example that subclasses Fl_Hold_Browser
    and uses callbacks, so an example of that might be:


#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Hold_Browser.H>

// Demonstrate subclassing Fl_Hold_Browser and using callback -erco 6/18/21

class MyBrowser : public Fl_Hold_Browser {
    void MyCallback2(Fl_Widget *w) {
        void *i = selection();
        if ( i ) printf("item selected is '%s'..\n", item_text(i));
    }

    static void MyCallback(Fl_Widget *w, void *data) {
        MyBrowser *brow = static_cast<MyBrowser*>(data);
	brow->MyCallback2(w);
    }
public:
    MyBrowser(int X,int Y,int W,int H) : Fl_Hold_Browser(X,Y,W,H) {
        when(FL_WHEN_CHANGED);
        callback(MyCallback, (void*)this);
    }
};

int main() {
    Fl_Window win(300,500);
    MyBrowser brow(10,10,300-20,500-20);
    brow.add("aaa"); brow.add("bbb"); brow.add("ccc");
    win.show();
    return Fl::run();
}


    Also, my FLTK cheat page has a few examples that subclass Fl_Browser, so to see those
    you can search that page for 'public Fl_Browser' and find those examples too.

  

Dave Jordan

unread,
Jun 18, 2021, 9:10:32 PM6/18/21
to fltkg...@googlegroups.com
On Fri, Jun 18, 2021 at 7:10 PM Greg Ercolano <er...@seriss.com> wrote:
On 6/18/21 10:18 AM, Dave Jordan wrote:
Subject: how can i declare & define Browser callback?
I haven't found any examples that use a subclassed Browser with .h & .cpp files.

    It sounds like you're looking for an example that subclasses Fl_Hold_Browser
    and uses callbacks, so an example of that might be:


#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Hold_Browser.H>

// Demonstrate subclassing Fl_Hold_Browser and using callback -erco 6/18/21

class MyBrowser : public Fl_Hold_Browser {
right, i did see examples in various places including your cheat page and the examples dir, but again, no "MyBrowser.h" or the like was in evidence, so I wasn't sure about the decl.
I had some simple compile error to start with, i dont even remember what, and then i got too complicated trying to fix it.
this is how I have it now:

#ifndef BRWSR_H
#define BRWSR_H
#include "glbl.h"         // include various FL widget headers

struct Brwsr : Fl_Hold_Browser {
Brwsr(int x, int y, int w1, int w2, int h);
void brwsr_cb(Fl_Widget*, void *v);
};
#endif // BRWSR_H
**********************************
#include yada yada.h
Brwsr::Brwsr(int x, int y, int w1, int w2, int h) : Fl_Hold_Browser(x, y, w1 + w2, h) {
// set appearance properties
}
void Brwsr::brwsr_cb(Fl_Widget *w, void *v) {     // use browser widget to control which game is displayed (what did i see it called? vertical tabbed dialog?)
Brwsr *b = (Brwsr*) w;
if(b->value() > 0) {
Game *gm = (Game*) v;
// unload app->cur.game
// load Game* gm
// assign gm to app->cur.game
// etc...
}
}

Greg Ercolano

unread,
Jun 18, 2021, 10:07:41 PM6/18/21
to fltkg...@googlegroups.com

    
right, i did see examples in various places including your cheat page and the examples dir,
but again, no "MyBrowser.h" or the like was in evidence, so I wasn't sure about the decl.

    For the cheat sheet and examples, the code normally broken up over separate .cxx / .H files
    are all kept in one file just to make it easy to copy/paste.

    But yeah, typically the class MyWhatever is declared in the MyWhatever.H file,
    and the methods are defined in the MyWhatever.cxx file


struct Brwsr : Fl_Hold_Browser {
Brwsr(int x, int y, int w1, int w2, int h);
void brwsr_cb(Fl_Widget*, void *v);
};

    Most of your example looks fine, but why are you using "struct" instead of "class" in the above?

    Use class so you can use all the normal keywords to control method visibility such as
    public: and protected: and private: and so it's clearly a class.

pvr...@btinternet.com

unread,
Jun 19, 2021, 3:23:22 AM6/19/21
to fltk.general
On Saturday, June 19, 2021 at 3:07:41 AM UTC+1 er...@seriss.com wrote:

    Most of your example looks fine, but why are you using "struct" instead of "class" in the above?

    Use class so you can use all the normal keywords to control method visibility such as
    public: and protected: and private: and so it's clearly a class.

struct is effectively the same as class except that the default access is public rather than private. As the OP stated that's why he's using struct rather than class.

It's bad practice to have default access as public as that increases the risk of coding errors.

Phil.

Dave Jordan

unread,
Jun 19, 2021, 4:54:50 AM6/19/21
to fltkg...@googlegroups.com
As soon as I wrap my head around all these other new C++ concepts I will go back and add appropriate access specifiers.
For now there is little chance i will misapply my own functions/methods and i need to follow KISS.

imm

unread,
Jun 19, 2021, 5:23:59 AM6/19/21
to General FLTK
On Sat, 19 Jun 2021, 09:55 Dave Jordan wrote:

As soon as I wrap my head around all these other new C++ concepts I will go back and add appropriate access specifiers.
For now there is little chance i will misapply my own functions/methods and i need to follow KISS.

Indeed, it sounds like a lot of the issue here is really just to do with the syntax of C/C++ and what constitutes "normal" practice in that syntax.

If it's unfamiliar to you, then anything can be tricky...

That said, most of the ALGOL style languages have broadly the same concept of scope and encapsulation and it's certainly the case that restricting the scope of elements usually prevents more problems than it causes, so it's generally a good idea to do that from the outset (and not try to shoehorn it in later!)

In terms of how that is done, fltk is pretty much like any simple C++ code, so general C++ examples are usually relevant.

We tend to show our samples packed into a single file for simplicity; the implicit assumption is that folk picking up fltk already know C++, of course, and will understand how to extend from there.

But if C++ is unfamiliar, then our examples are probably not the best teaching resource available for idiomatic C++ coding!
--
Ian
From my Fairphone FP3

Dave Jordan

unread,
Jun 19, 2021, 4:15:59 PM6/19/21
to fltkg...@googlegroups.com
I hasten to add the samples & whatnot *are* great for learning FLTK.


--
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.

Preston

unread,
Jun 19, 2021, 9:28:37 PM6/19/21
to fltk.general

As soon as I wrap my head around all these other new C++ concepts I will go back and add appropriate access specifiers.
For now there is little chance i will misapply my own functions/methods and i need to follow KISS.

The two books I have read so far are Programming Principles and Practice using C++   2nd Edition, Bjarne Stroustrup.
     Chapter 12 goes over FLTK libraries, but his code is broken now. Use the one from github instead.
     Its 1000 pages, but does a good job doing basic features and best practices.

The second book that I am on is A Tour of C++ 2nd edition, Bjarne Stroustrup.
     It is ~200 pages and is a lot more technical. I Needed to have read PPP2 before this one.
     It has a lot more explanation than  https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines that it references often. 
     The book is from 2018 but has some C++ 20 features that are not fully implemented in compilers like modules. 

These plus youtube have helped me a lot.
Hope they can help you too. 

pk

Dave Jordan

unread,
Jun 19, 2021, 11:08:16 PM6/19/21
to fltkg...@googlegroups.com
I'm sure they will! Thank you for the refs!
I never would have thought that Stroustrup's publications would discuss any libs except the C++ ones.

--
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.

Preston

unread,
Jun 20, 2021, 1:04:30 AM6/20/21
to fltk.general

I'm sure they will! Thank you for the refs!
I never would have thought that Stroustrup's publications would discuss any libs except the C++ ones.

It's a general programming book that goes over the basics. Encapsulation, Polymorphism, RAII, Scope, Virtual Functions, OOP, Generic Programming, etc  (It just so happens to use C++, and FLTK for GUI)
It goes over how to write intermediate libraries to do basic things like, Buttons/Callbacks, Drawing shapes, Windows, Images/Jpegs.

I wouldn't use it to make programs, due to having to recompile to move a button 20 pixels to the left, or to resize it. I would use FLUID if starting a project today.
The biggest thing is it teaches you to try to program first (with good structure planned) and code second

I have written too much spaghetti code that is hard to build on top off instead of programming with structure at the beginning. 
It compiles and runs, but is not pretty or practical. 
He does good examples of what not to do, and why. (Like use Classes instead of Structs)

The book stands out in Two ways:
1) It has nice forward and backward chapter/concept references. (In chapter 12 it will reference a concept in 4.2 and list where it is to re-read if not fully understood. I went back to referenced chapters often)
2) The book has a large amount of supporting documentation
          They discuss the broken GUI.h code and fix it.
 
I want to start coding that people can actually build off of and use, and this is just my start. 
pk
Reply all
Reply to author
Forward
0 new messages