From edd52ca1e84dde61f98d8525a53b0541281c6b20 Mon Sep 17 00:00:00 2001 From: Albrecht Schlosser Date: Thu, 18 Mar 2021 15:52:33 +0100 Subject: Add fluid callback demo program to examples folder This example demonstrates how to build an entire program using fluid and how to add static and virtual class methods as callbacks. --- examples/CMakeLists.txt | 16 +++++++++ examples/fluid-callback.fl | 85 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 examples/fluid-callback.fl (limited to 'examples') diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index a81a5695b..fc6c13237 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -66,6 +66,14 @@ set (SIMPLE_SOURCES wizard-simple ) +############################################################ +# simple FLUID examples w/o extra libs +############################################################ + +set (FLUID_SOURCES + fluid-callback +) + ############################################################ # examples requiring fltk_images ############################################################ @@ -92,6 +100,14 @@ foreach (src ${SIMPLE_SOURCES}) CREATE_EXAMPLE (${src} ${src}.cxx fltk) endforeach (src) +############################################################ +# create FLUID example programs +############################################################ + +foreach (src ${FLUID_SOURCES}) + CREATE_EXAMPLE (${src} ${src}.fl fltk) +endforeach (src) + ############################################################ # create example programs with fltk_images library ############################################################ diff --git a/examples/fluid-callback.fl b/examples/fluid-callback.fl new file mode 100644 index 000000000..42ce474c8 --- /dev/null +++ b/examples/fluid-callback.fl @@ -0,0 +1,85 @@ +# data file for the Fltk User Interface Designer (fluid) +version 1.0400 +header_name {.h} +code_name {.cxx} +comment {README FIRST +- - - - - - - +This fluid (.fl) file demonstrates how to create a complete +FLTK program with a button with a virtual callback method +and a virtual destructor. + +The code is as short as possible but demonstrates setting +virtual and static attributes on callbacks. + +Note that FLTK needs static functions as callbacks assigned +to widgets like Fl_Buttons. The static callback method can +eventually call other class methods like the virtual callback +method in this example. + +Use fluid interactively to open this fluid (.fl) file and +double-click on items to see how they are defined. + +Hint: Open the code view (Edit/Show Source Code) and select +items to see which code each item generates. Switch tabs +'Source' and 'Header' in the code view window to see code in +the source and header file, respectively. + +Use "File/Write Code" or 'fluid -c fluid-callback.fl' at +the command line to generate .cxx and .h files; compile +and build the demo program with + + fltk-config --compile fluid-callback.cxx +} {in_source not_in_header +} + +decl {\#include } {public global +} + +decl {\#include } {public global +} + +decl {\#include } {public global +} + +class App {open : Fl_Window +} { + Function {App(int X, int Y, const char *L) : Fl_Window(X, Y, L)} { + comment Constructor open + } { + code {// Code in constructor +printf("Creating new App\\n"); +Fl_Button *exit_cb = new Fl_Button(20, 20, 260, 60, "Exit: invokes virtual callback"); +exit_cb->callback(Cb_Bn_static, (void *)77); +show();} {} + } + Function {~App()} { + comment {Virtual destructor} open return_type virtual + } { + code {printf("Closing and deleting App\\n");} {} + } + Function {Cb_Bn_static(Fl_Widget *w, void *v)} { + comment {Static callback method} open return_type {static void} + } { + code {// code in static callback +int i = (int)(fl_intptr_t)v; +printf("Executing static callback Cb_Bn_static, value = %d\\n", i); +Fl_Window *win = w->window(); +App *app = (App *)win; +app->Cb_Bn_virtual(v);} {} + } + Function {Cb_Bn_virtual(void *v)} { + comment {// Virtual callback method} open return_type {virtual void} + } { + code {// code in virtual callback +int i = (int)(fl_intptr_t)v; +printf("Executing virtual callback Cb_Bn_virtual, value = %d\\n", i); +// note: operator delete calls hide() +delete this;} {} + } +} + +Function {} {open return_type int +} { + code {// Main program +App *app = new App(300, 100, "Virtual Callback Test");} {} +} -- cgit v1.2.3