diff options
| author | Matthias Melcher <github@matthiasm.com> | 2026-01-05 01:29:08 +0100 |
|---|---|---|
| committer | Matthias Melcher <github@matthiasm.com> | 2026-01-05 11:52:34 +0100 |
| commit | 11325da073a78b5a012e64b9732e253b50289763 (patch) | |
| tree | 64d2614c4fd3a13b8f2f7392a7d1764eddf7002e /fluid/nodes | |
| parent | 48617a8075e86b6991030d34df1b31fe86cd01b3 (diff) | |
Fluid: Modernize Function Nodes
Diffstat (limited to 'fluid/nodes')
| -rw-r--r-- | fluid/nodes/Function_Node.cxx | 119 | ||||
| -rw-r--r-- | fluid/nodes/Function_Node.h | 58 |
2 files changed, 83 insertions, 94 deletions
diff --git a/fluid/nodes/Function_Node.cxx b/fluid/nodes/Function_Node.cxx index a05bba271..dc1e4958d 100644 --- a/fluid/nodes/Function_Node.cxx +++ b/fluid/nodes/Function_Node.cxx @@ -189,25 +189,6 @@ const char *c_check(const char *c, int type) { Function_Node Function_Node::prototype; /** - Create a new function. - */ -Function_Node::Function_Node() : - Node(), - return_type_(nullptr), - public_(0), - declare_c_(0), - constructor(0), - havewidgets(0) -{ } - -/** - Destructor. - */ -Function_Node::~Function_Node() { - if (return_type_) free((void*)return_type_); -} - -/** Create a new function for the widget tree. \param[in] strategy add new function after current or as last child \return the new node @@ -223,7 +204,7 @@ Node *Function_Node::make(Strategy strategy) { } Function_Node *o = new Function_Node(); o->name("make_window()"); - o->return_type_ = nullptr; + o->return_type_.clear(); o->add(anchor, strategy); o->factory = this; o->public_ = 1; @@ -244,9 +225,9 @@ void Function_Node::write_properties(fld::io::Project_Writer &f) { case 2: f.write_string("protected"); break; } if (declare_c_) f.write_string("C"); - if (return_type_) { + if (!return_type().empty()) { f.write_string("return_type"); - f.write_word(return_type_); + f.write_word(return_type().c_str()); } } @@ -262,7 +243,7 @@ void Function_Node::read_property(fld::io::Project_Reader &f, const char *c) { } else if (!strcmp(c,"C")) { declare_c_ = 1; } else if (!strcmp(c,"return_type")) { - storestring(f.read_word(),return_type_); + return_type(f.read_word()); } else { Node::read_property(f, c); } @@ -354,25 +335,37 @@ void Function_Node::write_code1(fld::io::Code_Writer& f) { if (havechildren) f.write_c("int main(int argc, char **argv) {\n"); } else { - const char* rtype = return_type_; - const char* star = ""; + std::string rtype = return_type(); + std::string star = ""; // from matt: let the user type "static " at the start of type // in order to declare a static method; int is_static = 0; int is_virtual = 0; - if (rtype) { - if (!strcmp(rtype,"static")) {is_static = 1; rtype = nullptr;} - else if (!strncmp(rtype, "static ",7)) {is_static = 1; rtype += 7;} + if (!rtype.empty()) { + if (rtype == "static") { + is_static = 1; + rtype.clear(); + } else if (rtype.compare(0, 7, "static ")==0) { + is_static = 1; + rtype.erase(0, 7); + } } - if (rtype) { - if (!strcmp(rtype, "virtual")) {is_virtual = 1; rtype = nullptr;} - else if (!strncmp(rtype, "virtual ",8)) {is_virtual = 1; rtype += 8;} + if (!rtype.empty()) { + if (rtype == "virtual") { + is_virtual = 1; + rtype.clear(); + } else if (rtype.compare(0, 8, "virtual ")==0) { + is_virtual = 1; + rtype.erase(0, 8); + } } - if (!rtype) { + if (rtype.empty()) { if (havewidgets) { rtype = subclassname(child); star = "*"; - } else rtype = "void"; + } else { + rtype = "void"; + } } const char* k = class_name(0); @@ -390,9 +383,9 @@ void Function_Node::write_code1(fld::io::Code_Writer& f) { if (is_static) f.write_h("static "); if (is_virtual) f.write_h("virtual "); if (!constructor) { - f.write_h("%s%s ", rtype, star); + f.write_h("%s%s ", rtype.c_str(), star.c_str()); if (havechildren) - f.write_c("%s%s ", rtype, star); + f.write_c("%s%s ", rtype.c_str(), star.c_str()); } // if this is a subclass, only f.write_h() the part before the ':' @@ -423,9 +416,9 @@ void Function_Node::write_code1(fld::io::Code_Writer& f) { write_comment_c(f); if (public_==1) { if (declare_c_) - f.write_h("extern \"C\" { %s%s %s; }\n", rtype, star, name()); + f.write_h("extern \"C\" { %s%s %s; }\n", rtype.c_str(), star.c_str(), name()); else - f.write_h("%s%s %s;\n", rtype, star, name()); + f.write_h("%s%s %s;\n", rtype.c_str(), star.c_str(), name()); } else if (public_==2) { // write neither the prototype nor static, the function may be declared elsewhere } else { @@ -437,7 +430,7 @@ void Function_Node::write_code1(fld::io::Code_Writer& f) { char s[1024]; if (havechildren) { clean_function_for_implementation(s, name()); - f.write_c("%s%s %s {\n", rtype, star, s); + f.write_c("%s%s %s {\n", rtype.c_str(), star.c_str(), s); } } } @@ -466,7 +459,7 @@ void Function_Node::write_code2(fld::io::Code_Writer& f) { f.write_c("%s%s->show(argc, argv);\n", f.indent(1), var); if (havechildren) f.write_c("%sreturn Fl::run();\n", f.indent(1)); - } else if (havewidgets && !constructor && !return_type_) { + } else if (havewidgets && !constructor && return_type().empty()) { f.write_c("%sreturn %s;\n", f.indent(1), var); } if (havechildren) @@ -481,10 +474,11 @@ void Function_Node::write_code2(fld::io::Code_Writer& f) { \return 1 if they match, 0 if not */ int Function_Node::has_signature(const char *rtype, const char *sig) const { - if (rtype && !return_type_) return 0; - if (!name()) return 0; - if ( (rtype==nullptr || strcmp(return_type_, rtype)==0) - && fl_filename_match(name(), sig)) { + if (rtype && return_type().empty()) + return 0; + if (!name()) + return 0; + if ( (rtype==nullptr || (return_type() == rtype)) && fl_filename_match(name(), sig)) { return 1; } return 0; @@ -504,15 +498,6 @@ int Function_Node::has_signature(const char *rtype, const char *sig) const { Code_Node Code_Node::prototype; /** - Constructor. - */ -Code_Node::Code_Node() : - cursor_position_(0), - code_input_scroll_row(0), - code_input_scroll_col(0) -{} - -/** Make a new code node. If the parent node is not a function, a message box will pop up and the request will be ignored. @@ -634,22 +619,6 @@ int Code_Node::handle_editor_changes() { CodeBlock_Node CodeBlock_Node::prototype; /** - Constructor. - */ -CodeBlock_Node::CodeBlock_Node() : - Node(), - after(nullptr) -{ } - -/** - Destructor. - */ -CodeBlock_Node::~CodeBlock_Node() { - if (after) - free((void*)after); -} - -/** Make a new code block. If the parent node is not a function or another codeblock, a message box will pop up and the request will be ignored. @@ -671,7 +640,7 @@ Node *CodeBlock_Node::make(Strategy strategy) { } CodeBlock_Node *o = new CodeBlock_Node(); o->name("if (test())"); - o->after = nullptr; + o->end_code_.clear(); o->add(anchor, strategy); o->factory = this; return o; @@ -684,9 +653,9 @@ Node *CodeBlock_Node::make(Strategy strategy) { */ void CodeBlock_Node::write_properties(fld::io::Project_Writer &f) { Node::write_properties(f); - if (after) { + if (!end_code().empty()) { f.write_string("after"); - f.write_word(after); + f.write_word(end_code().c_str()); } } @@ -695,7 +664,7 @@ void CodeBlock_Node::write_properties(fld::io::Project_Writer &f) { */ void CodeBlock_Node::read_property(fld::io::Project_Reader &f, const char *c) { if (!strcmp(c,"after")) { - storestring(f.read_word(),after); + end_code(f.read_word()); } else { Node::read_property(f, c); } @@ -722,8 +691,10 @@ void CodeBlock_Node::write_code1(fld::io::Code_Writer& f) { */ void CodeBlock_Node::write_code2(fld::io::Code_Writer& f) { f.indentation--; - if (after) f.write_c("%s} %s\n", f.indent(), after); - else f.write_c("%s}\n", f.indent()); + if (!end_code().empty()) + f.write_c("%s} %s\n", f.indent(), end_code().c_str()); + else + f.write_c("%s}\n", f.indent()); } // ---- Decl_Node declaration diff --git a/fluid/nodes/Function_Node.h b/fluid/nodes/Function_Node.h index 4196c202a..ec1aadbfe 100644 --- a/fluid/nodes/Function_Node.h +++ b/fluid/nodes/Function_Node.h @@ -1,7 +1,7 @@ // // C function Node header file for the Fast Light Tool Kit (FLTK). // -// Copyright 1998-2025 by Bill Spitzak and others. +// Copyright 1998-2026 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 @@ -49,21 +49,25 @@ class Function_Node : public Node public: typedef Node super; static Function_Node prototype; + private: - const char* return_type_; - char public_, declare_c_, constructor, havewidgets; + std::string return_type_; + char public_ = 0; + char declare_c_ = 0; + char constructor = 0; + char havewidgets = 0; + public: - Function_Node(); - ~Function_Node(); + Function_Node() = default; + ~Function_Node() override = default; + Node *make(Strategy strategy) override; void write_code1(fld::io::Code_Writer& f) override; void write_code2(fld::io::Code_Writer& f) override; void open() override; int ismain() {return name_ == nullptr;} const char *type_name() override {return "Function";} - const char *title() override { - return name() ? name() : "main()"; - } + const char *title() override { return name() ? name() : "main()"; } int can_have_children() const override {return 1;} int is_code_block() const override {return 1;} int is_public() const override; @@ -72,8 +76,8 @@ public: void write_properties(fld::io::Project_Writer &f) override; void read_property(fld::io::Project_Reader &f, const char *) override; int has_signature(const char *, const char*) const; - const char *return_type() { return return_type_; } - void return_type(const char *t) { storestring(t, return_type_); } + std::string return_type() const { return return_type_; } + void return_type(const std::string& t) { storestring(t, return_type_); } char visibility() { return public_; } void visibility(char v) { public_ = v; } char declare_c() { return declare_c_; } @@ -87,13 +91,17 @@ class Code_Node : public Node public: typedef Node super; static Code_Node prototype; - int cursor_position_; - int code_input_scroll_row; - int code_input_scroll_col; + private: + int cursor_position_ = 0; + int code_input_scroll_row_ = 0; + int code_input_scroll_col_ = 0; ExternalCodeEditor editor_; + public: - Code_Node(); + Code_Node() = default; + ~Code_Node() override = default; + Node *make(Strategy strategy) override; void write(fld::io::Project_Writer &f) override; void write_code1(fld::io::Code_Writer& f) override; @@ -107,6 +115,12 @@ public: int is_editing(); int reap_editor(); int handle_editor_changes(); + int cursor_position() { return cursor_position_; } + int code_input_scroll_row() { return code_input_scroll_row_; } + int code_input_scroll_col() { return code_input_scroll_col_; } + void save_editor_state(int pos, int row, int col) { + cursor_position_ = pos; code_input_scroll_row_ = row; code_input_scroll_col_ = col; + } }; // ---- CodeBlock_Node declaration @@ -116,11 +130,14 @@ class CodeBlock_Node : public Node public: typedef Node super; static CodeBlock_Node prototype; + private: - const char* after; + std::string end_code_; + public: - CodeBlock_Node(); - ~CodeBlock_Node(); + CodeBlock_Node() = default; + ~CodeBlock_Node() override = default; + Node *make(Strategy strategy) override; void write_code1(fld::io::Code_Writer& f) override; void write_code2(fld::io::Code_Writer& f) override; @@ -133,8 +150,8 @@ public: bool is_a(Type inType) const override { return (inType==Type::CodeBlock) ? true : super::is_a(inType); } void write_properties(fld::io::Project_Writer &f) override; void read_property(fld::io::Project_Reader &f, const char *) override; - const char *end_code() { return after; } - void end_code(const char *c) { storestring(c, after); } + std::string end_code() { return end_code_; } + void end_code(const std::string& c) { storestring(c, end_code_); } }; // ---- Decl_Node declaration @@ -176,9 +193,10 @@ class Data_Node : public Decl_Node public: typedef Decl_Node super; static Data_Node prototype; + private: std::string filename_; - int output_format_ { 0 }; + int output_format_ = 0; public: Data_Node() = default; |
