diff options
| author | Matthias Melcher <github@matthiasm.com> | 2025-03-07 18:54:03 +0100 |
|---|---|---|
| committer | Matthias Melcher <github@matthiasm.com> | 2025-03-07 18:54:24 +0100 |
| commit | c3571838cb10133aa913efd7523b9543a65459c1 (patch) | |
| tree | 6820fd1e11ce523d76f4f0580c3fa7d8072dcec8 /fluid/widgets | |
| parent | 89f714cb4eac968c94925ee2e9629649033ef372 (diff) | |
Fluid: Rebuilding most of the widget directory.
One file per logical unit.
Namespaces.
Non-static data member initializers to never
get an uninitialized field again.
Diffstat (limited to 'fluid/widgets')
| -rw-r--r-- | fluid/widgets/Bin_Button.cxx | 128 | ||||
| -rw-r--r-- | fluid/widgets/Bin_Button.h | 44 | ||||
| -rw-r--r-- | fluid/widgets/CodeEditor.h | 105 | ||||
| -rw-r--r-- | fluid/widgets/Code_Editor.cxx (renamed from fluid/widgets/CodeEditor.cxx) | 94 | ||||
| -rw-r--r-- | fluid/widgets/Code_Editor.h | 70 | ||||
| -rw-r--r-- | fluid/widgets/Code_Viewer.cxx | 51 | ||||
| -rw-r--r-- | fluid/widgets/Code_Viewer.h | 51 | ||||
| -rw-r--r-- | fluid/widgets/Formula_Input.cxx (renamed from fluid/widgets/custom_widgets.cxx) | 152 | ||||
| -rw-r--r-- | fluid/widgets/Formula_Input.h | 73 | ||||
| -rw-r--r-- | fluid/widgets/Style_Parser.cxx (renamed from fluid/widgets/StyleParse.cxx) | 35 | ||||
| -rw-r--r-- | fluid/widgets/Style_Parser.h (renamed from fluid/widgets/StyleParse.h) | 46 | ||||
| -rw-r--r-- | fluid/widgets/Text_Viewer.cxx | 58 | ||||
| -rw-r--r-- | fluid/widgets/Text_Viewer.h | 46 | ||||
| -rw-r--r-- | fluid/widgets/custom_widgets.h | 90 |
14 files changed, 607 insertions, 436 deletions
diff --git a/fluid/widgets/Bin_Button.cxx b/fluid/widgets/Bin_Button.cxx new file mode 100644 index 000000000..cc351085d --- /dev/null +++ b/fluid/widgets/Bin_Button.cxx @@ -0,0 +1,128 @@ +// +// Widget Bin Button code for the Fast Light Tool Kit (FLTK). +// +// Copyright 1998-2025 by Bill Spitzak and others. +// +// This library is free software. Distribution and use rights are outlined in +// the file "COPYING" which should have been included with this file. If this +// file is missing or damaged, see the license at: +// +// https://www.fltk.org/COPYING.php +// +// Please see the following page on how to report bugs and issues: +// +// https://www.fltk.org/bugs.php +// + +#include "widgets/Bin_Button.h" + +#include "app/fluid.h" +#include "nodes/factory.h" +#include "nodes/Fl_Window_Type.h" +#include "widgets/widget_browser.h" + +#include <FL/Fl_Button.H> +#include <FL/Fl_Window.H> + +using namespace fld; +using namespace fld::widget; + + +/** \class fld::widget::Bin_Button + The Bin_Button button is a button that can be used in the widget bin to + allow the user to drag and drop widgets into a window or group. This feature + makes it easy for the user to position a widget at a specific location within + the window or group. + */ + +/** + Convert mouse dragging into a drag and drop event. + */ +int fld::widget::Bin_Button::handle(int inEvent) +{ + int ret = 0; + switch (inEvent) { + case FL_PUSH: + Fl_Button::handle(inEvent); + return 1; // make sure that we get drag events + case FL_DRAG: + ret = Fl_Button::handle(inEvent); + if (!user_data()) + return ret; + if (!Fl::event_is_click()) { // make it a dnd event + // fake a drag outside of the widget + Fl::e_x = x()-1; + Fl_Button::handle(inEvent); + // fake a button release + Fl_Button::handle(FL_RELEASE); + // make it into a dnd event + const char *type_name = (const char*)user_data(); + Fl_Type::current_dnd = Fl_Type::current; + Fl::copy(type_name, (int)strlen(type_name)+1, 0); + Fl::dnd(); + return 1; + } + return ret; + } + return Fl_Button::handle(inEvent); +} + +/** \class fld::widget::Bin_Window_Button + The Bin_Window_Button button is used in the widget bin to create new + windows by dragging and dropping. When the button is dragged and dropped onto + the desktop, a new window will be created at the drop location. + + This does not work in Wayland because Wayland does not allow client + applications to control window placement. + */ + +/** + Convert mouse dragging into a drag and drop event. + */ +int fld::widget::Bin_Window_Button::handle(int inEvent) +{ + static Fl_Window *drag_win = NULL; + int ret = 0; + switch (inEvent) { + case FL_PUSH: + Fl_Button::handle(inEvent); + return 1; // make sure that we get drag events + case FL_DRAG: + ret = Fl_Button::handle(inEvent); + if (!user_data()) + return ret; + if (!Fl::event_is_click()) { + if (!drag_win) { + drag_win = new Fl_Window(0, 0, 480, 320); + drag_win->border(0); + drag_win->set_non_modal(); + } + if (drag_win) { + drag_win->position(Fl::event_x_root()+1, Fl::event_y_root()+1); + drag_win->show(); + } + // Does not work outside window: fl_cursor(FL_CURSOR_HAND); + } + return ret; + case FL_RELEASE: + if (drag_win) { + Fl::delete_widget(drag_win); + drag_win = NULL; + // create a new window here + Fl_Type *prototype = typename_to_prototype((char*)user_data()); + if (prototype) { + Fl_Type *new_type = add_new_widget_from_user(prototype, Strategy::AFTER_CURRENT); + if (new_type && new_type->is_a(ID_Window)) { + Fl_Window_Type *new_window = (Fl_Window_Type*)new_type; + Fl_Window *w = (Fl_Window *)new_window->o; + w->position(Fl::event_x_root(), Fl::event_y_root()); + } + } + widget_browser->display(Fl_Type::current); + widget_browser->rebuild(); + } + return Fl_Button::handle(inEvent); + } + return Fl_Button::handle(inEvent); +} + diff --git a/fluid/widgets/Bin_Button.h b/fluid/widgets/Bin_Button.h new file mode 100644 index 000000000..342fe0eac --- /dev/null +++ b/fluid/widgets/Bin_Button.h @@ -0,0 +1,44 @@ +// +// Widget Bin Button header file for the Fast Light Tool Kit (FLTK). +// +// Copyright 1998-2025 by Bill Spitzak and others. +// +// This library is free software. Distribution and use rights are outlined in +// the file "COPYING" which should have been included with this file. If this +// file is missing or damaged, see the license at: +// +// https://www.fltk.org/COPYING.php +// +// Please see the following page on how to report bugs and issues: +// +// https://www.fltk.org/bugs.php +// + +#ifndef FLUID_WIDGETS_BIN_BUTTON_H +#define FLUID_WIDGETS_BIN_BUTTON_H + +#include <FL/Fl_Button.H> + +namespace fld { +namespace widget { + +// Adding drag and drop for dragging widgets into windows. +class Bin_Button : public Fl_Button { +public: + int handle(int) override; + Bin_Button(int X,int Y,int W,int H, const char* l = nullptr) : + Fl_Button(X,Y,W,H,l) { } +}; + +// Adding drag and drop functionality to drag window prototypes onto the desktop. +class Bin_Window_Button : public Fl_Button { +public: + int handle(int) override; + Bin_Window_Button(int X,int Y,int W,int H, const char* l = nullptr) : + Fl_Button(X,Y,W,H,l) { } +}; + +} // namespace widget +} // namespace fld + +#endif // FLUID_WIDGETS_BIN_BUTTON_H diff --git a/fluid/widgets/CodeEditor.h b/fluid/widgets/CodeEditor.h deleted file mode 100644 index cc720d618..000000000 --- a/fluid/widgets/CodeEditor.h +++ /dev/null @@ -1,105 +0,0 @@ -// -// Code editor widget for the Fast Light Tool Kit (FLTK). -// Syntax highlighting rewritten by erco@seriss.com 09/15/20. -// -// Copyright 1998-2025 by Bill Spitzak and others. -// -// This library is free software. Distribution and use rights are outlined in -// the file "COPYING" which should have been included with this file. If this -// file is missing or damaged, see the license at: -// -// https://www.fltk.org/COPYING.php -// -// Please see the following page on how to report bugs and issues: -// -// https://www.fltk.org/bugs.php -// - -#ifndef CodeEditor_h -#define CodeEditor_h - -// -// Include necessary headers... -// - -#include "StyleParse.h" - -#include <FL/Fl.H> -#include <FL/Fl_Text_Buffer.H> -#include <FL/Fl_Text_Editor.H> - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <ctype.h> - -// ---- CodeEditor declaration - -/** - A widget derived from Fl_Text_Editor that implements C++ code highlighting. - - CodeEditor is used in Fluid whenever the user can edit C++ source - code or header text. - */ -class CodeEditor : public Fl_Text_Editor { - friend class StyleParse; - - static Fl_Text_Display::Style_Table_Entry styletable[]; - static void style_parse(const char *tbuff, char *sbuff, int len, char style); - static void style_unfinished_cb(int, void*); - static void style_update(int pos, int nInserted, int nDeleted, - int /*nRestyled*/, const char * /*deletedText*/, - void *cbArg); - static int auto_indent(int, CodeEditor* e); - -public: - CodeEditor(int X, int Y, int W, int H, const char *L=0); - ~CodeEditor(); - void textsize(Fl_Fontsize s); - - /// access to protected member get_absolute_top_line_number() - int top_line() { return get_absolute_top_line_number(); } - - /// access to protected member mTopLineNum - int scroll_row() { return mTopLineNum; } - - /// access to protected member mHorizOffset - int scroll_col() { return mHorizOffset; } -}; - -// ---- CodeViewer declaration - -/** - A widget derived from CodeEditor with highlighting for code blocks. - - This widget is used by the codeview system to show the design's - source and header code. The secondary highlighting show the text - part that corresponds to the selected widget(s). - */ -class CodeViewer : public CodeEditor { -public: - CodeViewer(int X, int Y, int W, int H, const char *L=0); - -protected: - void draw() FL_OVERRIDE; - - /// Limit event handling to viewing, not editing - int handle(int ev) FL_OVERRIDE { return Fl_Text_Display::handle(ev); } -}; - -// ---- Project File Text Viewer declaration - -/** - A text viewer with an additional highlighting color scheme. - */ -class TextViewer : public Fl_Text_Display { -public: - TextViewer(int X, int Y, int W, int H, const char *L=0); - ~TextViewer(); - void draw() FL_OVERRIDE; - - /// access to protected member get_absolute_top_line_number() - int top_line() { return get_absolute_top_line_number(); } -}; - -#endif // !CodeEditor_h diff --git a/fluid/widgets/CodeEditor.cxx b/fluid/widgets/Code_Editor.cxx index ca114b577..a145b5055 100644 --- a/fluid/widgets/CodeEditor.cxx +++ b/fluid/widgets/Code_Editor.cxx @@ -19,20 +19,18 @@ // Include necessary headers... // -#include "widgets/CodeEditor.h" +#include "widgets/Code_Editor.h" -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <ctype.h> +using namespace fld; +using namespace fld::widget; -// ---- CodeEditor implementation +// ---- Code_Editor implementation /** Lookup table for all supported styles. Every table entry describes a rendering style for the corresponding text. */ -Fl_Text_Display::Style_Table_Entry CodeEditor::styletable[] = { // Style table +Fl_Text_Display::Style_Table_Entry Code_Editor::styletable[] = { // Style table { FL_FOREGROUND_COLOR, FL_COURIER, 11 }, // A - Plain { FL_DARK_GREEN, FL_COURIER_ITALIC, 11 }, // B - Line comments { FL_DARK_GREEN, FL_COURIER_ITALIC, 11 }, // C - Block comments @@ -50,7 +48,7 @@ Fl_Text_Display::Style_Table_Entry CodeEditor::styletable[] = { // Style table \param[in] in_len byte length to parse \param[in] in_style starting style letter */ -void CodeEditor::style_parse(const char *in_tbuff, // text buffer to parse +void Code_Editor::style_parse(const char *in_tbuff, // text buffer to parse char *in_sbuff, // style buffer we modify int in_len, // byte length to parse char in_style) { // starting style letter @@ -65,7 +63,7 @@ void CodeEditor::style_parse(const char *in_tbuff, // text buffer to par // 'G' - Keywords if, while.. // 'H' - Chars 'x' - StyleParse sp; + Style_Parser sp; sp.tbuff = in_tbuff; sp.sbuff = in_sbuff; sp.len = in_len; @@ -103,7 +101,7 @@ void CodeEditor::style_parse(const char *in_tbuff, // text buffer to par /** Update unfinished styles. */ -void CodeEditor::style_unfinished_cb(int, void*) { +void Code_Editor::style_unfinished_cb(int, void*) { } /** @@ -113,10 +111,10 @@ void CodeEditor::style_unfinished_cb(int, void*) { \param[in] nDeleted number of bytes deleted \param[in] cbArg pointer back to the code editor */ -void CodeEditor::style_update(int pos, int nInserted, int nDeleted, +void Code_Editor::style_update(int pos, int nInserted, int nDeleted, int /*nRestyled*/, const char * /*deletedText*/, void *cbArg) { - CodeEditor *editor = (CodeEditor*)cbArg; + Code_Editor *editor = (Code_Editor*)cbArg; char *style, // Style data *text; // Text data @@ -164,7 +162,7 @@ void CodeEditor::style_update(int pos, int nInserted, int nDeleted, Find the right indentation depth after pressing the Enter key. \param[in] e pointer back to the code editor */ -int CodeEditor::auto_indent(int, CodeEditor* e) { +int Code_Editor::auto_indent(int, Code_Editor* e) { if (e->buffer()->selected()) { e->insert_position(e->buffer()->primary_selection()->start()); e->buffer()->remove_selection(); @@ -198,11 +196,11 @@ int CodeEditor::auto_indent(int, CodeEditor* e) { } /** - Create a CodeEditor widget. + Create a Code_Editor widget. \param[in] X, Y, W, H position and size of the widget \param[in] L optional label */ -CodeEditor::CodeEditor(int X, int Y, int W, int H, const char *L) : +Code_Editor::Code_Editor(int X, int Y, int W, int H, const char *L) : Fl_Text_Editor(X, Y, W, H, L) { buffer(new Fl_Text_Buffer); @@ -228,9 +226,9 @@ CodeEditor::CodeEditor(int X, int Y, int W, int H, const char *L) : } /** - Destroy a CodeEditor widget. + Destroy a Code_Editor widget. */ -CodeEditor::~CodeEditor() { +Code_Editor::~Code_Editor() { Fl_Text_Buffer *buf = mStyleBuffer; mStyleBuffer = 0; delete buf; @@ -245,7 +243,7 @@ CodeEditor::~CodeEditor() { This works by updating the fontsizes in the style table. \param[in] s the new general height of the text font */ -void CodeEditor::textsize(Fl_Fontsize s) { +void Code_Editor::textsize(Fl_Fontsize s) { Fl_Text_Editor::textsize(s); // call base class method // now attempt to update our styletable to honor the new size... int entries = sizeof(styletable) / sizeof(styletable[0]); @@ -254,63 +252,3 @@ void CodeEditor::textsize(Fl_Fontsize s) { } } // textsize -// ---- CodeViewer implementation - -/** - Create a CodeViewer widget. - \param[in] X, Y, W, H position and size of the widget - \param[in] L optional label - */ -CodeViewer::CodeViewer(int X, int Y, int W, int H, const char *L) -: CodeEditor(X, Y, W, H, L) -{ - default_key_function(kf_ignore); - remove_all_key_bindings(&key_bindings); - cursor_style(CARET_CURSOR); -} - -/** - Tricking Fl_Text_Display into using bearable colors for this specific task. - */ -void CodeViewer::draw() -{ - Fl_Color c = Fl::get_color(FL_SELECTION_COLOR); - Fl::set_color(FL_SELECTION_COLOR, fl_color_average(FL_BACKGROUND_COLOR, FL_FOREGROUND_COLOR, 0.9f)); - CodeEditor::draw(); - Fl::set_color(FL_SELECTION_COLOR, c); -} - -// ---- TextViewer implementation - -/** - Create a TextViewer widget. - \param[in] X, Y, W, H position and size of the widget - \param[in] L optional label - */ -TextViewer::TextViewer(int X, int Y, int W, int H, const char *L) -: Fl_Text_Display(X, Y, W, H, L) -{ - buffer(new Fl_Text_Buffer); -} - -/** - Avoid memory leaks. - */ -TextViewer::~TextViewer() { - Fl_Text_Buffer *buf = mBuffer; - buffer(0); - delete buf; -} - -/** - Tricking Fl_Text_Display into using bearable colors for this specific task. - */ -void TextViewer::draw() -{ - Fl_Color c = Fl::get_color(FL_SELECTION_COLOR); - Fl::set_color(FL_SELECTION_COLOR, fl_color_average(FL_BACKGROUND_COLOR, FL_FOREGROUND_COLOR, 0.9f)); - Fl_Text_Display::draw(); - Fl::set_color(FL_SELECTION_COLOR, c); -} - - diff --git a/fluid/widgets/Code_Editor.h b/fluid/widgets/Code_Editor.h new file mode 100644 index 000000000..ef8df0e0d --- /dev/null +++ b/fluid/widgets/Code_Editor.h @@ -0,0 +1,70 @@ +// +// Code editor widget for the Fast Light Tool Kit (FLTK). +// Syntax highlighting rewritten by erco@seriss.com 09/15/20. +// +// Copyright 1998-2025 by Bill Spitzak and others. +// +// This library is free software. Distribution and use rights are outlined in +// the file "COPYING" which should have been included with this file. If this +// file is missing or damaged, see the license at: +// +// https://www.fltk.org/COPYING.php +// +// Please see the following page on how to report bugs and issues: +// +// https://www.fltk.org/bugs.php +// + +#ifndef FLUID_WIDGETS_CODE_EDITOR_H +#define FLUID_WIDGETS_CODE_EDITOR_H + +// +// Include necessary headers... +// + +#include "Style_Parser.h" + +#include <FL/Fl_Text_Editor.H> + +namespace fld { +namespace widget { + +// ---- Code_Editor declaration + +/** + A widget derived from Fl_Text_Editor that implements C++ code highlighting. + + Code_Editor is used in Fluid whenever the user can edit C++ source + code or header text. + */ +class Code_Editor : public Fl_Text_Editor { + friend class Style_Parser; + + static Fl_Text_Display::Style_Table_Entry styletable[]; + static void style_parse(const char *tbuff, char *sbuff, int len, char style); + static void style_unfinished_cb(int, void*); + static void style_update(int pos, int nInserted, int nDeleted, + int /*nRestyled*/, const char * /*deletedText*/, + void *cbArg); + static int auto_indent(int, Code_Editor* e); + +public: + Code_Editor(int X, int Y, int W, int H, const char *L=0); + ~Code_Editor(); + void textsize(Fl_Fontsize s); + + /// access to protected member get_absolute_top_line_number() + int top_line() { return get_absolute_top_line_number(); } + + /// access to protected member mTopLineNum + int scroll_row() { return mTopLineNum; } + + /// access to protected member mHorizOffset + int scroll_col() { return mHorizOffset; } +}; + +} // namespace widget +} // namespace fld + + +#endif // FLUID_WIDGETS_CODE_EDITOR_H diff --git a/fluid/widgets/Code_Viewer.cxx b/fluid/widgets/Code_Viewer.cxx new file mode 100644 index 000000000..82a71f4f6 --- /dev/null +++ b/fluid/widgets/Code_Viewer.cxx @@ -0,0 +1,51 @@ +// +// Code editor widget for the Fast Light Tool Kit (FLTK). +// Syntax highlighting rewritten by erco@seriss.com 09/15/20. +// +// Copyright 1998-2025 by Bill Spitzak and others. +// +// This library is free software. Distribution and use rights are outlined in +// the file "COPYING" which should have been included with this file. If this +// file is missing or damaged, see the license at: +// +// https://www.fltk.org/COPYING.php +// +// Please see the following page on how to report bugs and issues: +// +// https://www.fltk.org/bugs.php +// + +// +// Include necessary headers... +// + +#include "widgets/Code_Viewer.h" + +using namespace fld; +using namespace fld::widget; + +/** + Create a fld::widget::Code_Viewer widget. + \param[in] X, Y, W, H position and size of the widget + \param[in] L optional label + */ +Code_Viewer::Code_Viewer(int X, int Y, int W, int H, const char *L) +: Code_Editor(X, Y, W, H, L) +{ + default_key_function(kf_ignore); + remove_all_key_bindings(&key_bindings); + cursor_style(CARET_CURSOR); +} + +/** + Tricking Fl_Text_Display into using bearable colors for this specific task. + */ +void Code_Viewer::draw() +{ + Fl_Color c = Fl::get_color(FL_SELECTION_COLOR); + Fl::set_color(FL_SELECTION_COLOR, fl_color_average(FL_BACKGROUND_COLOR, FL_FOREGROUND_COLOR, 0.9f)); + Code_Editor::draw(); + Fl::set_color(FL_SELECTION_COLOR, c); +} + + diff --git a/fluid/widgets/Code_Viewer.h b/fluid/widgets/Code_Viewer.h new file mode 100644 index 000000000..5bf8b870a --- /dev/null +++ b/fluid/widgets/Code_Viewer.h @@ -0,0 +1,51 @@ +// +// Code editor widget for the Fast Light Tool Kit (FLTK). +// Syntax highlighting rewritten by erco@seriss.com 09/15/20. +// +// Copyright 1998-2025 by Bill Spitzak and others. +// +// This library is free software. Distribution and use rights are outlined in +// the file "COPYING" which should have been included with this file. If this +// file is missing or damaged, see the license at: +// +// https://www.fltk.org/COPYING.php +// +// Please see the following page on how to report bugs and issues: +// +// https://www.fltk.org/bugs.php +// + +#ifndef FLUID_WIDGETS_CODE_VIEWER_H +#define FLUID_WIDGETS_CODE_VIEWER_H + +// +// Include necessary headers... +// + +#include "widgets/Code_Editor.h" + +namespace fld { +namespace widget { + +/** + A widget derived from Code_Editor with highlighting for code blocks. + + This widget is used by the codeview system to show the design's + source and header code. The secondary highlighting show the text + part that corresponds to the selected widget(s). + */ +class Code_Viewer : public Code_Editor { +public: + Code_Viewer(int X, int Y, int W, int H, const char *L = nullptr); + +protected: + void draw() override; + + /// Limit event handling to viewing, not editing + int handle(int ev) override { return Fl_Text_Display::handle(ev); } +}; + +} // namespace widget +} // namespace fld + +#endif // FLUID_WIDGETS_CODE_VIEWER_H diff --git a/fluid/widgets/custom_widgets.cxx b/fluid/widgets/Formula_Input.cxx index 25a630783..b26bd83e3 100644 --- a/fluid/widgets/custom_widgets.cxx +++ b/fluid/widgets/Formula_Input.cxx @@ -1,7 +1,7 @@ // // Widget type code for the Fast Light Tool Kit (FLTK). // -// Copyright 1998-2023 by Bill Spitzak and others. +// Copyright 1998-2025 by Bill Spitzak and others. // // This library is free software. Distribution and use rights are outlined in // the file "COPYING" which should have been included with this file. If this @@ -14,119 +14,20 @@ // https://www.fltk.org/bugs.php // -#include "widgets/custom_widgets.h" +#include "widgets/Formula_Input.h" -#include "app/fluid.h" -#include "nodes/Fl_Window_Type.h" -#include "nodes/factory.h" -#include "panels/widget_panel.h" -#include "widgets/widget_browser.h" - -#include <FL/platform.H> -#include <FL/Fl_Button.H> -#include <FL/Fl_Window.H> -#include <FL/fl_draw.H> -#include <FL/Fl_Menu_.H> #include <FL/fl_string_functions.h> #include "../src/flstring.h" -/** \class Widget_Bin_Button - The Widget_Bin_Button button is a button that can be used in the widget bin to - allow the user to drag and drop widgets into a window or group. This feature - makes it easy for the user to position a widget at a specific location within - the window or group. - */ - -/** - Convert mouse dragging into a drag and drop event. - */ -int Widget_Bin_Button::handle(int inEvent) -{ - int ret = 0; - switch (inEvent) { - case FL_PUSH: - Fl_Button::handle(inEvent); - return 1; // make sure that we get drag events - case FL_DRAG: - ret = Fl_Button::handle(inEvent); - if (!user_data()) - return ret; - if (!Fl::event_is_click()) { // make it a dnd event - // fake a drag outside of the widget - Fl::e_x = x()-1; - Fl_Button::handle(inEvent); - // fake a button release - Fl_Button::handle(FL_RELEASE); - // make it into a dnd event - const char *type_name = (const char*)user_data(); - Fl_Type::current_dnd = Fl_Type::current; - Fl::copy(type_name, (int)strlen(type_name)+1, 0); - Fl::dnd(); - return 1; - } - return ret; - } - return Fl_Button::handle(inEvent); -} - -/** \class Widget_Bin_Window_Button - The Widget_Bin_Window_Button button is used in the widget bin to create new - windows by dragging and dropping. When the button is dragged and dropped onto - the desktop, a new window will be created at the drop location. - */ +#include <stdlib.h> +#include <ctype.h> +#include <string.h> -/** - Convert mouse dragging into a drag and drop event. - */ -int Widget_Bin_Window_Button::handle(int inEvent) -{ - static Fl_Window *drag_win = NULL; - int ret = 0; - switch (inEvent) { - case FL_PUSH: - Fl_Button::handle(inEvent); - return 1; // make sure that we get drag events - case FL_DRAG: - ret = Fl_Button::handle(inEvent); - if (!user_data()) - return ret; - if (!Fl::event_is_click()) { - if (!drag_win) { - drag_win = new Fl_Window(0, 0, 480, 320); - drag_win->border(0); - drag_win->set_non_modal(); - } - if (drag_win) { - drag_win->position(Fl::event_x_root()+1, Fl::event_y_root()+1); - drag_win->show(); - } - // Does not work outside window: fl_cursor(FL_CURSOR_HAND); - } - return ret; - case FL_RELEASE: - if (drag_win) { - Fl::delete_widget(drag_win); - drag_win = NULL; - // create a new window here - Fl_Type *prototype = typename_to_prototype((char*)user_data()); - if (prototype) { - Fl_Type *new_type = add_new_widget_from_user(prototype, Strategy::AFTER_CURRENT); - if (new_type && new_type->is_a(ID_Window)) { - Fl_Window_Type *new_window = (Fl_Window_Type*)new_type; - Fl_Window *w = (Fl_Window *)new_window->o; - w->position(Fl::event_x_root(), Fl::event_y_root()); - } - } - widget_browser->display(Fl_Type::current); - widget_browser->rebuild(); - } - return Fl_Button::handle(inEvent); - } - return Fl_Button::handle(inEvent); -} +using namespace fld; +using namespace fld::widget; -/** \class Fluid_Coord_Input - The Fluid_Coord_Input widget is an input field for entering widget coordinates +/** \class fld::widget::Formula_Input + The Formula_Input widget is an input field for entering widget coordinates and sizes. It includes basic math capabilities and allows the use of variables in formulas. This widget is useful for specifying precise positions and dimensions for widgets in a graphical user interface. @@ -135,21 +36,18 @@ int Widget_Bin_Window_Button::handle(int inEvent) /** Create an input field. */ -Fluid_Coord_Input::Fluid_Coord_Input(int x, int y, int w, int h, const char *l) : -Fl_Input(x, y, w, h, l), -user_callback_(0L), -vars_(0L), -vars_user_data_(0L) +Formula_Input::Formula_Input(int x, int y, int w, int h, const char *l) +: Fl_Input(x, y, w, h, l) { Fl_Input::callback((Fl_Callback*)callback_handler_cb); text("0"); } -void Fluid_Coord_Input::callback_handler_cb(Fluid_Coord_Input *This, void *v) { +void Formula_Input::callback_handler_cb(Formula_Input *This, void *v) { This->callback_handler(v); } -void Fluid_Coord_Input::callback_handler(void *v) { +void Formula_Input::callback_handler(void *v) { if (user_callback_) (*user_callback_)(this, v); // do *not* update the value to show the evaluated formula here, because the @@ -165,7 +63,7 @@ void Fluid_Coord_Input::callback_handler(void *v) { the last character of the variable name when returning. \return the integer value that was found or calculated */ -int Fluid_Coord_Input::eval_var(uchar *&s) const { +int Formula_Input::eval_var(uchar *&s) const { if (!vars_) return 0; // find the end of the variable name @@ -173,7 +71,7 @@ int Fluid_Coord_Input::eval_var(uchar *&s) const { while (isalpha(*s)) s++; int n = (int)(s-v); // find the variable in the list - for (Fluid_Coord_Input_Vars *vars = vars_; vars->name_; vars++) { + for (Formula_Input_Vars *vars = vars_; vars->name_; vars++) { if (strncmp((char*)v, vars->name_, n)==0 && vars->name_[n]==0) return vars->callback_(this, vars_user_data_); } @@ -187,7 +85,7 @@ int Fluid_Coord_Input::eval_var(uchar *&s) const { \param prio priority of current operation \return the value so far */ -int Fluid_Coord_Input::eval(uchar *&s, int prio) const { +int Formula_Input::eval(uchar *&s, int prio) const { int v = 0, sgn = 1; uchar c = *s++; @@ -248,7 +146,7 @@ int Fluid_Coord_Input::eval(uchar *&s, int prio) const { /** Evaluate a formula into an integer. - The Fluid_Coord_Input widget includes a formula interpreter that allows you + The Formula_Input widget includes a formula interpreter that allows you to evaluate a string containing a mathematical formula and obtain the result as an integer. The interpreter supports unary plus and minus, basic integer math operations (such as addition, subtraction, multiplication, and division), @@ -259,7 +157,7 @@ int Fluid_Coord_Input::eval(uchar *&s, int prio) const { \param s formula as a C string \return the calculated value */ -int Fluid_Coord_Input::eval(const char *s) const +int Formula_Input::eval(const char *s) const { // duplicate the text, so we can modify it uchar *buf = (uchar*)fl_strdup(s); @@ -281,14 +179,14 @@ int Fluid_Coord_Input::eval(const char *s) const /** Evaluate the formula and return the result. */ -int Fluid_Coord_Input::value() const { +int Formula_Input::value() const { return eval(text()); } /** Set the field to an integer value, replacing previous texts. */ -void Fluid_Coord_Input::value(int v) { +void Formula_Input::value(int v) { char buf[32]; fl_snprintf(buf, sizeof(buf), "%d", v); text(buf); @@ -297,7 +195,7 @@ void Fluid_Coord_Input::value(int v) { /** Allow vertical mouse dragging and mouse wheel to interactively change the value. */ -int Fluid_Coord_Input::handle(int event) { +int Formula_Input::handle(int event) { switch (event) { case FL_MOUSEWHEEL: if (Fl::event_dy()) { @@ -309,3 +207,11 @@ int Fluid_Coord_Input::handle(int event) { } return Fl_Input::handle(event); } + +/** Set the list of the available variables + \param vars array of variables, last entry `has name_` set to `NULL` + \param user_data is forwarded to the Variable callback */ +void Formula_Input::variables(Formula_Input_Vars *vars, void *user_data) { + vars_ = vars; + vars_user_data_ = user_data; +} diff --git a/fluid/widgets/Formula_Input.h b/fluid/widgets/Formula_Input.h new file mode 100644 index 000000000..5307cdc88 --- /dev/null +++ b/fluid/widgets/Formula_Input.h @@ -0,0 +1,73 @@ +// +// Formula_Input widget header file for the Fast Light Tool Kit (FLTK). +// +// Copyright 1998-2025 by Bill Spitzak and others. +// +// This library is free software. Distribution and use rights are outlined in +// the file "COPYING" which should have been included with this file. If this +// file is missing or damaged, see the license at: +// +// https://www.fltk.org/COPYING.php +// +// Please see the following page on how to report bugs and issues: +// +// https://www.fltk.org/bugs.php +// + +#ifndef FLUID_WIDGETS_FORMULA_INPUT_H +#define FLUID_WIDGETS_FORMULA_INPUT_H + +#include <FL/Fl_Input.H> + +namespace fld { +namespace widget { + +class Formula_Input; + +// Callback signature for function returning the value of a variable. +typedef int (Fluid_Coord_Callback)(Formula_Input const *, void*); + +// Entry for a list of variables available to an input field. +// Formula_Input::variables() expects an array of +// Formula_Input_Vars with the last entry's name_ set to NULL. +typedef struct Formula_Input_Vars { + const char *name_; + Fluid_Coord_Callback *callback_; +} Formula_Input_Vars; + +// A text input widget that understands simple math. +class Formula_Input : public Fl_Input +{ + Fl_Callback *user_callback_ { nullptr }; + Formula_Input_Vars *vars_ { nullptr }; + void *vars_user_data_ { nullptr }; + + static void callback_handler_cb(Formula_Input *This, void *v); + void callback_handler(void *v); + int eval_var(uchar *&s) const; + int eval(uchar *&s, int prio) const; + int eval(const char *s) const; + +public: + Formula_Input(int x, int y, int w, int h, const char *l = nullptr); + + /** Return the text in the widget text field. */ + const char *text() const { return Fl_Input::value(); } + + /** Set the text in the text field */ + void text(const char *v) { Fl_Input::value(v); } + + int value() const; + void value(int v); + + /** Set the general callback for this widget. */ + void callback(Fl_Callback *cb) { user_callback_ = cb; } + + void variables(fld::widget::Formula_Input_Vars *vars, void *user_data); + int handle(int) override; +}; + +} // namespace widget +} // namespace fld + +#endif // FLUID_WIDGETS_FORMULA_INPUT_H diff --git a/fluid/widgets/StyleParse.cxx b/fluid/widgets/Style_Parser.cxx index b8b8ff4f0..9929cc01a 100644 --- a/fluid/widgets/StyleParse.cxx +++ b/fluid/widgets/Style_Parser.cxx @@ -1,7 +1,7 @@ // // Syntax highlighting for the Fast Light Tool Kit (FLTK). // -// Copyright 1998-2020 by Bill Spitzak and others. +// Copyright 1998-2025 by Bill Spitzak and others. // Copyright 2020 Greg Ercolano. // // This library is free software. Distribution and use rights are outlined in @@ -15,13 +15,16 @@ // https://www.fltk.org/bugs.php // -#include "StyleParse.h" +#include "Style_Parser.h" #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> // bsearch() +using namespace fld; +using namespace fld::widget; + // Sorted list of C/C++ keywords... static const char * const code_keywords[] = { "and", @@ -130,7 +133,7 @@ static void* search_types(char *find) { // Applies the current style, advances to next text + style char. // Returns 0 if hit end of buffer, 1 otherwise. // -int StyleParse::parse_over_char(int handle_crlf) { +int Style_Parser::parse_over_char(int handle_crlf) { char c = *tbuff; // End of line? @@ -157,7 +160,7 @@ int StyleParse::parse_over_char(int handle_crlf) { // Parse over white space using current style // Returns 0 if hit end of buffer, 1 otherwise. // -int StyleParse::parse_over_white() { +int Style_Parser::parse_over_white() { while ( len > 0 && strchr(" \t", *tbuff)) { if ( !parse_over_char() ) return 0; } return 1; @@ -166,7 +169,7 @@ int StyleParse::parse_over_white() { // Parse over non-white alphabetic text // Returns 0 if hit end of buffer, 1 otherwise. // -int StyleParse::parse_over_alpha() { +int Style_Parser::parse_over_alpha() { while ( len > 0 && isalpha(*tbuff) ) { if ( !parse_over_char() ) return 0; } return 1; @@ -175,7 +178,7 @@ int StyleParse::parse_over_alpha() { // Parse to end of line in specified style. // Returns 0 if hit end of buffer, 1 otherwise. // -int StyleParse::parse_to_eol(char s) { +int Style_Parser::parse_to_eol(char s) { char save = style; style = s; while ( *tbuff != '\n' ) @@ -187,7 +190,7 @@ int StyleParse::parse_to_eol(char s) { // Parse a block comment until end of comment or buffer. // Returns 0 if hit end of buffer, 1 otherwise. // -int StyleParse::parse_block_comment() { +int Style_Parser::parse_block_comment() { char save = style; style = 'C'; // block comment style while ( len > 0 ) { @@ -203,7 +206,7 @@ int StyleParse::parse_block_comment() { } // Copy keyword from tbuff -> keyword[] buffer -void StyleParse::buffer_keyword() { +void Style_Parser::buffer_keyword() { char *key = keyword; char *kend = key + sizeof(keyword) - 1; // end of buffer for ( const char *s=tbuff; @@ -215,7 +218,7 @@ void StyleParse::buffer_keyword() { // Parse over specified 'key'word in specified style 's'. // Returns 0 if hit end of buffer, 1 otherwise. // -int StyleParse::parse_over_key(const char *key, char s) { +int Style_Parser::parse_over_key(const char *key, char s) { char save = style; style = s; // Parse over the keyword while applying style to sbuff @@ -229,7 +232,7 @@ int StyleParse::parse_over_key(const char *key, char s) { // Parse over angle brackets <..> in specified style. // Returns 0 if hit end of buffer, 1 otherwise. // -int StyleParse::parse_over_angles(char s) { +int Style_Parser::parse_over_angles(char s) { if ( *tbuff != '<' ) return 1; // not <..>, early exit char save = style; style = s; @@ -245,7 +248,7 @@ int StyleParse::parse_over_angles(char s) { // spi.keyword[] will contain parsed word. // Returns 0 if hit end of buffer, 1 otherwise. // -int StyleParse::parse_keyword() { +int Style_Parser::parse_keyword() { // Parse into 'keyword' buffer buffer_keyword(); char *key = keyword; @@ -262,7 +265,7 @@ int StyleParse::parse_keyword() { // Style parse a quoted string, either "" or ''. // Returns 0 if hit end of buffer, 1 otherwise. // -int StyleParse::parse_quoted_string(char quote_char, // e.g. '"' or '\'' +int Style_Parser::parse_quoted_string(char quote_char, // e.g. '"' or '\'' char in_style) { // style for quoted text style = in_style; // start string style if ( !parse_over_char() ) return 0; // parse over opening quote @@ -289,7 +292,7 @@ int StyleParse::parse_quoted_string(char quote_char, // e.g. '"' or '\'' // Style parse a directive (#include, #define..) // Returns 0 if hit end of buffer, 1 otherwise. // -int StyleParse::parse_directive() { +int Style_Parser::parse_directive() { style = 'E'; // start directive style if ( !parse_over_char() ) return 0; // Parse over '#' if ( !parse_over_white() ) return 0; // Parse over any whitespace after '#' @@ -303,7 +306,7 @@ int StyleParse::parse_directive() { // Style parse a line comment to end of line. // Returns 0 if hit end of buffer, 1 otherwise. // -int StyleParse::parse_line_comment() { +int Style_Parser::parse_line_comment() { return parse_to_eol('B'); } @@ -312,7 +315,7 @@ int StyleParse::parse_line_comment() { // a continuation of a line, such as in a multiline #directive. // Returns 0 if hit end of buffer, 1 otherwise. // -int StyleParse::parse_escape() { +int Style_Parser::parse_escape() { const char no_crlf = 0; if ( !parse_over_char(no_crlf) ) return 0; // backslash if ( !parse_over_char(no_crlf) ) return 0; // char escaped @@ -322,7 +325,7 @@ int StyleParse::parse_escape() { // Parse all other non-specific characters // Returns 0 if hit end of buffer, 1 otherwise. // -int StyleParse::parse_all_else() { +int Style_Parser::parse_all_else() { last = isalnum(*tbuff) || *tbuff == '_' || *tbuff == '.'; return parse_over_char(); } diff --git a/fluid/widgets/StyleParse.h b/fluid/widgets/Style_Parser.h index 2fcc4f4db..287ea6b21 100644 --- a/fluid/widgets/StyleParse.h +++ b/fluid/widgets/Style_Parser.h @@ -1,7 +1,7 @@ // // Syntax highlighting for the Fast Light Tool Kit (FLTK). // -// Copyright 1998-2020 by Bill Spitzak and others. +// Copyright 1998-2025 by Bill Spitzak and others. // Copyright 2020 Greg Ercolano. // // This library is free software. Distribution and use rights are outlined in @@ -15,30 +15,25 @@ // https://www.fltk.org/bugs.php // -#ifndef StyleParse_h -#define StyleParse_h +#ifndef FLUID_WIDGETS_STYLE_PARSER_H +#define FLUID_WIDGETS_STYLE_PARSER_H -// Class to manage style parsing, friend of CodeEditor -class StyleParse { +namespace fld { +namespace widget { + +// Class to manage style parsing, friend of Code_Editor +class Style_Parser { public: - const char *tbuff; // text buffer - char *sbuff; // style buffer - int len; // running length - char style; // current style - char lwhite; // leading white space (1=white, 0=past white) - int col; // line's column counter - char keyword[40]; // keyword parsing buffer - char last; // flag for keyword parsing + const char *tbuff { nullptr }; // text buffer + char *sbuff { nullptr }; // style buffer + int len { 0 }; // running length + char style { 0 }; // current style + char lwhite { 1 }; // leading white space (1=white, 0=past white) + int col { 0 }; // line's column counter + char keyword[40] { }; // keyword parsing buffer + char last { 0 }; // flag for keyword parsing - StyleParse() { - tbuff = 0; - sbuff = 0; - len = 0; - style = 0; - lwhite = 1; - col = 0; - last = 0; - } + Style_Parser() = default; // Methods to aid in parsing int parse_over_char(int handle_crlf=1); @@ -51,11 +46,14 @@ public: int parse_over_angles(char s); int parse_keyword(); // "switch" int parse_quoted_string(char quote_char, char in_style); - // "hello", 'x' + // "hello", 'x' int parse_directive(); // "#define" int parse_line_comment(); // "// text.." int parse_escape(); // "\'" int parse_all_else(); // all other code }; -#endif // StyleParse_h +} // namespace widget +} // namespace fld + +#endif // FLUID_WIDGETS_STYLE_PARSER_H diff --git a/fluid/widgets/Text_Viewer.cxx b/fluid/widgets/Text_Viewer.cxx new file mode 100644 index 000000000..ba2d145fe --- /dev/null +++ b/fluid/widgets/Text_Viewer.cxx @@ -0,0 +1,58 @@ +// +// Code editor widget for the Fast Light Tool Kit (FLTK). +// Syntax highlighting rewritten by erco@seriss.com 09/15/20. +// +// Copyright 1998-2025 by Bill Spitzak and others. +// +// This library is free software. Distribution and use rights are outlined in +// the file "COPYING" which should have been included with this file. If this +// file is missing or damaged, see the license at: +// +// https://www.fltk.org/COPYING.php +// +// Please see the following page on how to report bugs and issues: +// +// https://www.fltk.org/bugs.php +// + +// +// Include necessary headers... +// + +#include "widgets/Text_Viewer.h" + +using namespace fld; +using namespace fld::widget; + +/** + Create a fld::widget::Text_Viewer widget. + \param[in] X, Y, W, H position and size of the widget + \param[in] L optional label + */ +Text_Viewer::Text_Viewer(int X, int Y, int W, int H, const char *L) +: Fl_Text_Display(X, Y, W, H, L) +{ + buffer(new Fl_Text_Buffer); +} + +/** + Avoid memory leaks. + */ +Text_Viewer::~Text_Viewer() { + Fl_Text_Buffer *buf = mBuffer; + buffer(0); + delete buf; +} + +/** + Tricking Fl_Text_Display into using bearable colors for this specific task. + */ +void Text_Viewer::draw() +{ + Fl_Color c = Fl::get_color(FL_SELECTION_COLOR); + Fl::set_color(FL_SELECTION_COLOR, fl_color_average(FL_BACKGROUND_COLOR, FL_FOREGROUND_COLOR, 0.9f)); + Fl_Text_Display::draw(); + Fl::set_color(FL_SELECTION_COLOR, c); +} + + diff --git a/fluid/widgets/Text_Viewer.h b/fluid/widgets/Text_Viewer.h new file mode 100644 index 000000000..1e5810d50 --- /dev/null +++ b/fluid/widgets/Text_Viewer.h @@ -0,0 +1,46 @@ +// +// Code editor widget for the Fast Light Tool Kit (FLTK). +// Syntax highlighting rewritten by erco@seriss.com 09/15/20. +// +// Copyright 1998-2025 by Bill Spitzak and others. +// +// This library is free software. Distribution and use rights are outlined in +// the file "COPYING" which should have been included with this file. If this +// file is missing or damaged, see the license at: +// +// https://www.fltk.org/COPYING.php +// +// Please see the following page on how to report bugs and issues: +// +// https://www.fltk.org/bugs.php +// + +#ifndef FLUID_WIDGETS_TEXT_VIEWER_H +#define FLUID_WIDGETS_TEXT_VIEWER_H + +// +// Include necessary headers... +// + +#include <FL/Fl_Text_Display.H> + +namespace fld { +namespace widget { + +/** + A text viewer with an additional highlighting color scheme. + */ +class Text_Viewer : public Fl_Text_Display { +public: + Text_Viewer(int X, int Y, int W, int H, const char *L = nullptr); + ~Text_Viewer(); + void draw() override; + + /// access to protected member get_absolute_top_line_number() + int top_line() { return get_absolute_top_line_number(); } +}; + +} // namespace widget +} // namespace fld + +#endif // FLUID_WIDGETS_TEXT_VIEWER_H diff --git a/fluid/widgets/custom_widgets.h b/fluid/widgets/custom_widgets.h deleted file mode 100644 index 875496b8e..000000000 --- a/fluid/widgets/custom_widgets.h +++ /dev/null @@ -1,90 +0,0 @@ -// -// Shortcut header file for the Fast Light Tool Kit (FLTK). -// -// Copyright 1998-2023 by Bill Spitzak and others. -// -// This library is free software. Distribution and use rights are outlined in -// the file "COPYING" which should have been included with this file. If this -// file is missing or damaged, see the license at: -// -// https://www.fltk.org/COPYING.php -// -// Please see the following page on how to report bugs and issues: -// -// https://www.fltk.org/bugs.php -// - -#ifndef _FLUID_SHORTCUT_BUTTON_H -#define _FLUID_SHORTCUT_BUTTON_H - -#include <FL/Fl_Button.H> -#include <FL/Fl_Input.H> - -// Adding drag and drop for dragging widgets into windows. -class Widget_Bin_Button : public Fl_Button { -public: - int handle(int) FL_OVERRIDE; - Widget_Bin_Button(int X,int Y,int W,int H, const char* l = 0) : - Fl_Button(X,Y,W,H,l) { } -}; - -// Adding drag and drop functionality to drag window prototypes onto the desktop. -class Widget_Bin_Window_Button : public Fl_Button { -public: - int handle(int) FL_OVERRIDE; - Widget_Bin_Window_Button(int X,int Y,int W,int H, const char* l = 0) : - Fl_Button(X,Y,W,H,l) { } -}; - -// Callback signature for function returning the value of a variable. -typedef int (Fluid_Coord_Callback)(class Fluid_Coord_Input const *, void*); - -// Entry for a list of variables available to an input field. -// Fluid_Coord_Input::variables() expects an array of Fluid_Coord_Input_Vars -// with the last entry's name_ set to NULL. -typedef struct Fluid_Coord_Input_Vars { - const char *name_; - Fluid_Coord_Callback *callback_; -} Fluid_Coord_Input_Vars; - -// A text input widget that understands simple math. -class Fluid_Coord_Input : public Fl_Input -{ - Fl_Callback *user_callback_; - Fluid_Coord_Input_Vars *vars_; - void *vars_user_data_; - static void callback_handler_cb(Fluid_Coord_Input *This, void *v); - void callback_handler(void *v); - int eval_var(uchar *&s) const; - int eval(uchar *&s, int prio) const; - int eval(const char *s) const; - -public: - Fluid_Coord_Input(int x, int y, int w, int h, const char *l=0L); - - /** Return the text in the widget text field. */ - const char *text() const { return Fl_Input::value(); } - - /** Set the text in the text field */ - void text(const char *v) { Fl_Input::value(v); } - - int value() const; - void value(int v); - - /** Set the general callback for this widget. */ - void callback(Fl_Callback *cb) { - user_callback_ = cb; - } - - /** Set the list of the available variables - \param vars array of variables, last entry `has name_` set to `NULL` - \param user_data is forwarded to the Variable callback */ - void variables(Fluid_Coord_Input_Vars *vars, void *user_data) { - vars_ = vars; - vars_user_data_ = user_data; - } - - int handle(int) FL_OVERRIDE; -}; - -#endif |
