summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAlbrecht Schlosser <albrechts.fltk@online.de>2021-03-18 15:52:33 +0100
committerAlbrecht Schlosser <albrechts.fltk@online.de>2021-03-19 13:48:41 +0100
commitedd52ca1e84dde61f98d8525a53b0541281c6b20 (patch)
tree4d2999731f69e0d83871cdb07d7cb44382c73d19 /examples
parent19ae897553eb38e432a52359b25f0d2c65e7753f (diff)
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.
Diffstat (limited to 'examples')
-rw-r--r--examples/CMakeLists.txt16
-rw-r--r--examples/fluid-callback.fl85
2 files changed, 101 insertions, 0 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index a81a5695b..fc6c13237 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -67,6 +67,14 @@ set (SIMPLE_SOURCES
)
############################################################
+# simple FLUID examples w/o extra libs
+############################################################
+
+set (FLUID_SOURCES
+ fluid-callback
+)
+
+############################################################
# examples requiring fltk_images
############################################################
@@ -93,6 +101,14 @@ foreach (src ${SIMPLE_SOURCES})
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 <FL/Fl_Window.H>} {public global
+}
+
+decl {\#include <FL/Fl_Button.H>} {public global
+}
+
+decl {\#include <stdio.h>} {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");} {}
+}