Giter Club home page Giter Club logo

cfltk's Introduction

cfltk

C89 bindings for the FLTK gui library, which can be used as basis for C applications or for other bindings in other languages.

  • The official fltk docs.
  • The official fltk repo.

Api

The cfltk api follows the FLTK api mostly, which would make it easier to refer to the FLTK docs. As an example:

Fl_Window *w = new Fl_Window(100, 100, 400, 300, "name");
w->end();
w->show();

becomes:

Fl_Window *w = Fl_Window_new(100, 100, 400, 300, "name");
Fl_Window_end(w);
Fl_Window_show(w);

The fltk-rs repo shows an example wrapper around cfltk.

Usage

To add to your project, you can add this project as a submodule:

$ git submodule add https://github.com/moalyousef/cfltk
$ git submodule update --init --recursive
$ cd cfltk/fltk

or by cloning the repo:

$ git clone https://github.com/moalyousef/cfltk
$ cd cfltk
$ git submodule update --init --recursive

You can build your project using cmake on the command line or gui:

$ cmake -B bin -S .
$ cmake --build bin --parallel

An example CMakeLists.txt file:

cmake_minimum_required(VERSION 3.14)
project(app)

add_subdirectory(cfltk)

add_executable(main main.c)
target_include_directories(main PRIVATE cfltk/include)
target_link_libraries(main PRIVATE cfltk fltk::fltk fltk::images fltk::png fltk::z) # as needed

# for windows, might be needed in some setups like creating a library
target_link_libraries(main PRIVATE ws2_32 comctl32 gdi32 gdiplus oleaut32 ole32 uuid shell32 advapi32 comdlg32 winspool user32 kernel32 odbc32)

# for apple, might be needed in some setups like creating a library
target_link_libraries(main PRIVATE -framework Cocoa)

# for linux, might be needed in some setups like creating a library
target_link_libraries(main PRIVATE pthread X11 Xext Xinerama Xcursor Xrender Xfixes Xft fontconfig pango-1.0 pangoxft-1.0 gobject-2.0 cairo pangocairo-1.0)

Options which can be used with cmake:

$ cmake -B bin -S . \
    -DCMAKE_BUILD_TYPE=Release \
    -DFLTK_USE_SYSTEM_LIBPNG=OFF \
    -DFLTK_USE_SYSTEM_LIBJPEG=OFF \
    -DFLTK_USE_SYSTEM_ZLIB=OFF \
    -DFLTK_BUILD_GL=OFF \
    -DFLTK_BUILD_EXAMPLES=OFF \
    -DFLTK_BUILD_TEST=OFF \
    -DFLTK_OPTION_LARGE_FILE=ON \
    -DFLTK_BUILD_HTML_DOCS=OFF \
    -DFLTK_BUILD_PDF_DOCS=OFF \

For pango support on linux (for rtl and cjk text), you can use -DFLTK_USE_PANGO=ON.

Otherwise, these options can be added to the CMakeLists.txt file:

    set(FLTK_USE_SYSTEM_LIBPNG OFF CACHE BOOL " " FORCE)
    set(FLTK_USE_SYSTEM_LIBJPEG OFF CACHE BOOL " " FORCE)
    set(FLTK_USE_SYSTEM_ZLIB OFF CACHE BOOL " " FORCE)
    set(FLTK_BUILD_GL OFF CACHE BOOL " " FORCE)
    set(FLTK_BUILD_EXAMPLES OFF CACHE BOOL " " FORCE)
    set(FLTK_BUILD_TEST OFF CACHE BOOL " " FORCE)
    set(OPTION_LARGE_FILE ON CACHE BOOL " " FORCE)
    set(FLTK_BUILD_HTML_DOCS OFF CACHE BOOL " " FORCE)
    set(FLTK_BUILD_PDF_DOCS OFF CACHE BOOL " " FORCE)

An example app:

#include <cfl.h>
#include <cfl_button.h>
#include <cfl_image.h>
#include <cfl_widget.h>
#include <cfl_window.h>
#include <stdlib.h>

void cb(Fl_Widget *w, void *data) { Fl_Widget_set_label(w, "Works!"); }

int main(void) {
    Fl_init_all(); // init all styles
    Fl_register_images(); // necessary for image support
    Fl_lock(); // necessary for multithreaded support
    Fl_Window *w = Fl_Window_new(100, 100, 400, 300, NULL);
    Fl_Button *b = Fl_Button_new(160, 210, 80, 40, "Click me");
    Fl_Window_end(w);
    Fl_Window_show(w);
    Fl_Button_set_callback(b, cb, NULL);
    return Fl_run();
}

Examples

More examples can be found in the examples directory.

Dependencies

CMake (version > 3.7), Git and a C++17 compiler need to be installed and in your PATH for a crossplatform build from source.

  • Windows:
    • MSVC: Windows SDK
    • Gnu: No dependencies
  • MacOS: No dependencies.
  • Linux: X11 and OpenGL development headers need to be installed for development. The libraries themselves are available on linux distros with a graphical user interface.

For Debian-based GUI distributions, that means running:

$ sudo apt-get install libx11-dev libxext-dev libxft-dev libxinerama-dev libxcursor-dev libxrender-dev libxfixes-dev libpango1.0-dev libgl1-mesa-dev libglu1-mesa-dev

For RHEL-based GUI distributions, that means running:

$ sudo yum groupinstall "X Software Development" && yum install pango-devel libXinerama-devel libstdc++-static

For Arch-based GUI distributions, that means running:

$ sudo pacman -S libx11 libxext libxft libxinerama libxcursor libxrender libxfixes pango cairo libgl mesa --needed

For Alpine linux:

$ apk add pango-dev fontconfig-dev libxinerama-dev libxfixes-dev libxcursor-dev mesa-gl

Safety

Both FLTK and the wrapper are exception safe. Widget, image and Fl_Text_Buffer mutating wrapper functions are thread-safe. FLTK manages the lifetime of widgets, which would delete any child widgets when the parent is deleted. The wrapper also provides an Fl_Widget_Tracker object which can be used to prevent use-after-free errors. The wrapper itself doesn't perform null checks on pointers returned from FLTK nor on pointers passed to it. Allocation failure should also be checked manually by the consuming user of the wrapper.

cfltk's People

Contributors

ashfordn avatar caseyb avatar hannesbraun avatar jgriffitts avatar jkl1337 avatar joseluis avatar moalyousef avatar tdryer avatar thechampagne avatar thorio avatar vstojkovic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

cfltk's Issues

[enhancement] Allow fully overriding widget draw routine

Currently the Widget_Derived draw callback unconditionally calls the super draw function first before calling a custom callback. This makes it not possible to, say, derive a new button type which gets state and mouse handling but with fully custom drawing. It would be nice if the super draw function is wrapped and explicitly callable and it isn't called if a draw callback is set.

Cairo support is missed in cfltk

Thanks for all the work done to make foreign language binding possible!

I see that most of the classes have been re-exported. However, special Cairo support for FLTK seems to be missed out. Since Cairo is enabled by default in most cases, is there any other reason for not exporting this part of the interface?

Cairo has more advanced drawing functions than FLTK without using OpenGL. Therefore, it can do some work that FLTK cannot do, such as rendering translucent shapes. Otherwise, sometimes only relatively low performance solutions can be used.

p.s. Since the maintainers of this code repository are basically the same as fltk-rs, I would also like to request here that this group of interfaces be integrated into fltk-rs in the future.

failed to build with pango libarary

In centos 6 with devtoolset-6 installed which contains gcc (GCC) 6.3.1 20170216 (Red Hat 6.3.1-3), the following command will not compile

cargo build --example spreadsheet --verbose

and the error is

gmake: warning: -jN forced in submake: disabling jobserver mode.
  /root/dev/fltk-rs/fltk-sys/cfltk/fltk/src/drivers/Cairo/Fl_Cairo_Graphics_Driver.cxx: In function ‘void draw_image_cb(void*, int, int, int, uchar*)’:
  /root/dev/fltk-rs/fltk-sys/cfltk/fltk/src/drivers/Cairo/Fl_Cairo_Graphics_Driver.cxx:58:26: error: invalid operands of types ‘uchar* {aka unsigned char*}’ and ‘__gnu_cxx::__enable_if<true, double>::__type {aka double}’ to binary ‘operator+’
       buf += abs(cb_data->D);
                            ^
  /root/dev/fltk-rs/fltk-sys/cfltk/fltk/src/drivers/Cairo/Fl_Cairo_Graphics_Driver.cxx:58:26: error:   in evaluation of ‘operator+=(uchar* {aka unsigned char*}, __gnu_cxx::__enable_if<true, double>::__type {aka double})’
  /root/dev/fltk-rs/fltk-sys/cfltk/fltk/src/drivers/Cairo/Fl_Cairo_Graphics_Driver.cxx: In member function ‘virtual void Fl_Cairo_Graphics_Driver::draw_image_mono(const uchar*, int, int, int, int, int, int)’:
  /root/dev/fltk-rs/fltk-sys/cfltk/fltk/src/drivers/Cairo/Fl_Cairo_Graphics_Driver.cxx:539:28: error: invalid operands of types ‘const uchar* {aka const unsigned char*}’ and ‘__gnu_cxx::__enable_if<true, double>::__type {aka double}’ to binary ‘operator+’
     if (D<0) data += iw*abs(D);
                              ^
  /root/dev/fltk-rs/fltk-sys/cfltk/fltk/src/drivers/Cairo/Fl_Cairo_Graphics_Driver.cxx:539:28: error:   in evaluation of ‘operator+=(const uchar* {aka const unsigned char*}, __gnu_cxx::__enable_if<true, double>::__type {aka double})’
  gmake[2]: *** [fltk/src/CMakeFiles/fltk.dir/drivers/Cairo/Fl_Cairo_Graphics_Driver.cxx.o] Error 1
  gmake[1]: *** [fltk/src/CMakeFiles/fltk.dir/all] Error 2
  gmake: *** [all] Error 2
  thread 'main' panicked at '
  command did not execute successfully, got: exit status: 2

It seems that abs function does not work properly, see stackoverflow here. By simply convert abs(xx) to int(abs(xx)), the problem is gone. Should we make a patch to Fl_Cairo_Graphics_Driver.cxx?

cfltk tries to link with libfltk.a when building shared libs

I built cfltk with CFLTK_BUILD_SHARED=ON and it tried to link with libfltk.a instead of libfltk.so. Here's the log:

/sbin/ld: /usr/local/lib64/libfltk.a(Fl_Message.cxx.o): warning: relocation against FL_NORMAL_SIZE' in read-only section .text'
/sbin/ld: /usr/local/lib64/libfltk.a(Fl.cxx.o): relocation R_X86_64_PC32 against symbol `_ZN16Fl_Screen_Driver13system_driverE' can not be used when making a shared object; recompile with -fPIC
/sbin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/cfltk.dir/build.make:485: libcfltk.so.1.4.5] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/cfltk.dir/all] Error 2
make: *** [Makefile:136: all] Error 2

Request to add more precompiled / bundled configurations

It would be great if win-x86 and win-arm64 were supported as fltk-bundled options as they are widely used.

Rationale:

  • win-x86: I know win-x86 is basically a dead platform in that no new computers ship with it, but I still have users who are on x86 versions of win7 and win10 and I expect it will be many more years before we see it disappear completely. Since one of fltk's missions is to support old platforms it seems sensible to compile for it. Additionally, x86 binaries are smaller and can run very efficiently on both x64 and arm64 variations of windows (thanks to WoW64) making it a good compilation target if you are building a binary that needs to run everywhere. While x64 recently received virtualization support in win-arm64 in Windows 11, it's performance is not that great so it is still better to compile for x86 if you require running on both.
  • win-arm64: This is becoming a more popular target recently in mobile windows computers due to performance and efficiency improvements. I expect that the market share of arm64 computers will continue to grow.

P.S. is there any reason this repo shouldn't move into the fltk-rs org? I went looking for it and struggled to find it before realising it was not a part of the org :)

Unable to compile on Windows with GCC.

Perhaps I'm just being stupid, but any time I run cmake, it attempts to execute the MSVC compilation path and utilize nmake. That's the jist of what I'm getting, anyways, I have no experience with cmake. It has made compiling this an impossibility, which is a shame because I really do need to use this on a non-git project. Below is the error I get trying to compile it.

F:\Software Development\Sources\cfltk-main\src>cmake .. -D CMAKE_C_COMPILER="F:\Software Development\mingw32\bin\gcc.exe" -D CMAKE_CXX_COMPILER="F:\Software Development\mingw32\bin\g++.exe"
CMake Error at CMakeLists.txt:3 (project):
  Running

   'nmake' '-?'

  failed with:

   The system cannot find the file specified


-- Configuring incomplete, errors occurred!```

[question]difference between cfltk and FLTK-C?

D.J.Peters supplied FLTK- C-1.3.3 for FreeBASIC, which is located on freebasic.net/forum/viewtopic.php?f=14&t=24547&hilit=fltk+c

Actually the lib can be used by other languages which support call functions in dynamic libraries on both windows and linux

so, what is the difference between cfltk and FLTK-C?

thanks

Including the library in a third-party project

I would like to understand how to properly connect the library to a third-party project, using the example of FreeRDP. I'm trying to build a program with a self-written GUI based on the FLTK library. Since FreeRDP is written in C, I want to build together with CFLTK.

I created a small form, wrote for its assembly CMakeLists.txt , and corrected the file by including his code there:

...
#include "../xfreerdp.h"

#include "../gfree/gfree.h"

int main(int argc, char* argv[])
{
    gfree();
    int rc = 1;
...

And corrected the FreeRDP build file CMakeLists.txt:

...

set(GFREE gfree)
add_subdirectory(${GFREE})
set(GFREE_FLAGS
    gfree
    cfltk
    fltk
    fltk_images
    fltk_jpeg
    fltk_z
    png16
    m
    X11
    Xinerama
    Xfixes
    Xcursor
    Xft
    Xrender
    fontconfig
)
ADD_LIBRARY(cfltk STATIC IMPORTED)
set_target_properties(cfltk PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/gfree_lib/libcfltk.a)
ADD_LIBRARY(fltk STATIC IMPORTED)
set_target_properties(fltk PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/gfree_lib/libfltk.a)
ADD_LIBRARY(fltk_images STATIC IMPORTED)
set_target_properties(fltk_images PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/gfree_lib/libfltk_images.a)
ADD_LIBRARY(fltk_jpeg STATIC IMPORTED)
set_target_properties(fltk_jpeg PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/gfree_lib/libfltk_jpeg.a)
ADD_LIBRARY(fltk_z STATIC IMPORTED)
set_target_properties(fltk_z PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/gfree_lib/libfltk_z.a)

....

set(${MODULE_PREFIX}_LIBS ${${MODULE_PREFIX}_LIBS} freerdp-client freerdp m ${GFREE_FLAGS})
....

Actually, the FreeRDP project is assembled without errors. It starts.

изображение

But when I connect to any server, I get errors:

[21:01:24:425] [4624:4625] [ERROR][com.freerdp.core.nego] - Protocol Security Negotiation Failure
[21:01:24:426] [4624:4625] [ERROR][com.freerdp.core] - rdp_client_connect:freerdp_set_last_error_ex ERRCONNECT_SECURITY_NEGO_CONNECT_FAILED [0x0002000C]
[21:01:24:426] [4624:4625] [ERROR][com.freerdp.core.connection] - Error: protocol security negotiation or connection failure

Without integrating my code into the FreeRDP project, my connection is performed without errors. I suspect that this is due to my introduction of third-party code into the FreeRDP project.

I understand that, most likely, I am connecting my code to the FreeRDP project as incorrectly as possible. I have never done this, and therefore I face problems.

Is it possible to implement all this somehow, taking into account that third-party code does not affect the performance of the main FreeRDP code?

Shared lib

Greetings, great job.
Any recommendations for building cfltk dynamically, currently build libcfltk.a, but would like a libcfltk.so instead
I tried with -DOPTION_BUILD_SHARED_LIBS=ON but it only works for fltk, cfltk stays the same.

Symbol 'Fl_Widget_set_label' not found, undifined reference to symbol

I don't know if I'm doing something wrong but I have compiled and installed both fltk and cfltk using GNU Make after I generated the Makefile using Cmake.

I'm using the example program and CMakeLists.txt that have been taken from "README.md" and I try to compile the program but I'm getting an error that the compiler cannot find the 'Fl_Widget_set_label' symbol and also I'm also getting an error about an undefined symbol called "dlsym@@GLIBC_2.2.5"

If you need more specific info:

OS: Linux Lite 5.6

Command used to compile: gcc ex.c -o ex -lcfltk -lfltk -lpthread -lX11 -lpango-1.0 -lpangoxft-1.0 -lgobject-2.0 -lcairo -lpangocairo-1.0 -lfltk_images -lfltk_jpeg // I also tried with "tcc" as the compiler

Exact error output::

ex.c: In function ‘cb’:
ex.c:7:37: warning: implicit declaration of function ‘Fl_Widget_set_label’; did you mean ‘Fl_Window_set_label’? [-Wimplicit-function-declaration]
    7 | void cb(Fl_Widget *w, void *data) { Fl_Widget_set_label(w, "Works!"); }
      |                                     ^~~~~~~~~~~~~~~~~~~
      |                                     Fl_Window_set_label
/usr/bin/ld: /usr/local/lib/libfltk.a(Fl_X11_Window_Driver.cxx.o): undefined reference to symbol 'dlsym@@GLIBC_2.2.5'
/usr/bin/ld: /lib/x86_64-linux-gnu/libdl.so.2: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.