Skip to content

Commit

Permalink
Fluid: Rename Stratgy constants to comply with CMP.
Browse files Browse the repository at this point in the history
Capitalized constants.
Added flag to indicate creation by user or file.
Removed global variable 'reading_file'.
  • Loading branch information
MatthiasWM committed Dec 28, 2024
1 parent 2100655 commit 050cbab
Show file tree
Hide file tree
Showing 16 changed files with 114 additions and 90 deletions.
41 changes: 24 additions & 17 deletions fluid/Fl_Function_Type.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,16 @@ Fl_Function_Type::~Fl_Function_Type() {

/**
Create a new function for the widget tree.
\param[in] strategy new function add after current or as last child
\param[in] strategy add new function after current or as last child
\return the new node
*/
Fl_Type *Fl_Function_Type::make(Strategy strategy) {
Fl_Type *anchor = Fl_Type::current, *p = anchor;
if (p && (strategy == kAddAfterCurrent)) p = p->parent;
if (p && (strategy.placement() == Strategy::AFTER_CURRENT))
p = p->parent;
while (p && !p->is_decl_block()) {
anchor = p;
strategy = kAddAfterCurrent;
strategy.placement(Strategy::AFTER_CURRENT);
p = p->parent;
}
Fl_Function_Type *o = new Fl_Function_Type();
Expand Down Expand Up @@ -596,10 +597,11 @@ Fl_Code_Type::Fl_Code_Type() :
*/
Fl_Type *Fl_Code_Type::make(Strategy strategy) {
Fl_Type *anchor = Fl_Type::current, *p = anchor;
if (p && (strategy == kAddAfterCurrent)) p = p->parent;
if (p && (strategy.placement() == Strategy::AFTER_CURRENT))
p = p->parent;
while (p && !p->is_code_block()) {
anchor = p;
strategy = kAddAfterCurrent;
strategy.placement(Strategy::AFTER_CURRENT);
p = p->parent;
}
if (!p) {
Expand Down Expand Up @@ -763,10 +765,11 @@ Fl_CodeBlock_Type::~Fl_CodeBlock_Type() {
*/
Fl_Type *Fl_CodeBlock_Type::make(Strategy strategy) {
Fl_Type *anchor = Fl_Type::current, *p = anchor;
if (p && (strategy == kAddAfterCurrent)) p = p->parent;
if (p && (strategy.placement() == Strategy::AFTER_CURRENT))
p = p->parent;
while (p && !p->is_code_block()) {
anchor = p;
strategy = kAddAfterCurrent;
strategy.placement(Strategy::AFTER_CURRENT);
p = p->parent;
}
if (!p) {
Expand Down Expand Up @@ -904,10 +907,11 @@ int Fl_Decl_Type::is_public() const
*/
Fl_Type *Fl_Decl_Type::make(Strategy strategy) {
Fl_Type *anchor = Fl_Type::current, *p = anchor;
if (p && (strategy == kAddAfterCurrent)) p = p->parent;
if (p && (strategy.placement() == Strategy::AFTER_CURRENT))
p = p->parent;
while (p && !p->is_decl_block()) {
anchor = p;
strategy = kAddAfterCurrent;
strategy.placement(Strategy::AFTER_CURRENT);
p = p->parent;
}
Fl_Decl_Type *o = new Fl_Decl_Type();
Expand Down Expand Up @@ -1131,10 +1135,11 @@ Fl_Data_Type::~Fl_Data_Type() {
*/
Fl_Type *Fl_Data_Type::make(Strategy strategy) {
Fl_Type *anchor = Fl_Type::current, *p = anchor;
if (p && (strategy == kAddAfterCurrent)) p = p->parent;
if (p && (strategy.placement() == Strategy::AFTER_CURRENT))
p = p->parent;
while (p && !p->is_decl_block()) {
anchor = p;
strategy = kAddAfterCurrent;
strategy.placement(Strategy::AFTER_CURRENT);
p = p->parent;
}
Fl_Data_Type *o = new Fl_Data_Type();
Expand Down Expand Up @@ -1474,10 +1479,10 @@ int Fl_DeclBlock_Type::is_public() const {
*/
Fl_Type *Fl_DeclBlock_Type::make(Strategy strategy) {
Fl_Type *anchor = Fl_Type::current, *p = anchor;
if (p && (strategy == kAddAfterCurrent)) p = p->parent;
if (p && (strategy.placement() == Strategy::AFTER_CURRENT)) p = p->parent;
while (p && !p->is_decl_block()) {
anchor = p;
strategy = kAddAfterCurrent;
strategy.placement(Strategy::AFTER_CURRENT);
p = p->parent;
}
Fl_DeclBlock_Type *o = new Fl_DeclBlock_Type();
Expand Down Expand Up @@ -1706,10 +1711,11 @@ Fl_Comment_Type::Fl_Comment_Type() :
*/
Fl_Type *Fl_Comment_Type::make(Strategy strategy) {
Fl_Type *anchor = Fl_Type::current, *p = anchor;
if (p && (strategy == kAddAfterCurrent)) p = p->parent;
if (p && (strategy.placement() == Strategy::AFTER_CURRENT))
p = p->parent;
while (p && !p->is_code_block()) {
anchor = p;
strategy = kAddAfterCurrent;
strategy.placement(Strategy::AFTER_CURRENT);
p = p->parent;
}
Fl_Comment_Type *o = new Fl_Comment_Type();
Expand Down Expand Up @@ -1985,10 +1991,11 @@ void Fl_Class_Type::prefix(const char*p) {
*/
Fl_Type *Fl_Class_Type::make(Strategy strategy) {
Fl_Type *anchor = Fl_Type::current, *p = anchor;
if (p && (strategy == kAddAfterCurrent)) p = p->parent;
if (p && (strategy.placement() == Strategy::AFTER_CURRENT))
p = p->parent;
while (p && !p->is_decl_block()) {
anchor = p;
strategy = kAddAfterCurrent;
strategy.placement(Strategy::AFTER_CURRENT);
p = p->parent;
}
Fl_Class_Type *o = new Fl_Class_Type();
Expand Down
4 changes: 2 additions & 2 deletions fluid/Fl_Group_Type.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void group_cb(Fl_Widget *, void *) {
undo_checkpoint();
undo_suspend();
Fl_Type::current = qq;
Fl_Group_Type *n = (Fl_Group_Type*)(Fl_Group_type.make(kAddAsLastChild));
Fl_Group_Type *n = (Fl_Group_Type*)(Fl_Group_type.make(Strategy::AS_LAST_CHILD));
n->move_before(q);
n->o->resize(q->o->x(),q->o->y(),q->o->w(),q->o->h());
for (Fl_Type *t = qq->next; t && (t->level > qq->level);) {
Expand All @@ -126,7 +126,7 @@ void group_cb(Fl_Widget *, void *) {
continue;
}
Fl_Type *nxt = t->remove();
t->add(n, kAddAsLastChild);
t->add(n, Strategy::AS_LAST_CHILD);
t = nxt;
}
fix_group_size(n);
Expand Down
11 changes: 6 additions & 5 deletions fluid/Fl_Menu_Type.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,11 @@ Fl_Type *Fl_Menu_Item_Type::make(Strategy strategy) {
Fl_Type* Fl_Menu_Item_Type::make(int flags, Strategy strategy) {
// Find a good insert position based on the current marked node
Fl_Type *anchor = Fl_Type::current, *p = anchor;
if (p && (strategy == kAddAfterCurrent)) p = p->parent;
if (p && (strategy.placement() == Strategy::AFTER_CURRENT))
p = p->parent;
while (p && !(p->is_a(ID_Menu_Manager_) || p->is_a(ID_Submenu))) {
anchor = p;
strategy = kAddAfterCurrent;
strategy.placement(Strategy::AFTER_CURRENT);
p = p->parent;
}
if (!p) {
Expand All @@ -186,7 +187,7 @@ Fl_Type* Fl_Menu_Item_Type::make(int flags, Strategy strategy) {
t->o->type(flags);
t->factory = this;
t->add(anchor, strategy);
if (!reading_file) {
if (strategy.source() == Strategy::FROM_USER) {
if (flags==FL_SUBMENU) {
t->label("submenu");
} else {
Expand All @@ -209,14 +210,14 @@ void group_selected_menuitems() {
}
undo_checkpoint();
undo_suspend();
Fl_Widget_Type *n = (Fl_Widget_Type*)(q->make(FL_SUBMENU, kAddAfterCurrent));
Fl_Widget_Type *n = (Fl_Widget_Type*)(q->make(FL_SUBMENU, Strategy::AFTER_CURRENT));
for (Fl_Type *t = qq->next; t && (t->level > qq->level);) {
if (t->level != n->level || t == n || !t->selected) {
t = t->next;
continue;
}
Fl_Type *nxt = t->remove();
t->add(n, kAddAsLastChild);
t->add(n, Strategy::AS_LAST_CHILD);
t = nxt;
}
widget_browser->rebuild();
Expand Down
11 changes: 6 additions & 5 deletions fluid/Fl_Type.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ Fl_Group_Type *Fl_Type::group() {
This methods updates the widget_browser.
\param[in] p insert \c this tree as a child of \c p
\param[in] strategy is kAddAsLastChild or kAddAfterCurrent
\param[in] strategy is Strategy::AS_LAST_CHILD or Strategy::AFTER_CURRENT
*/
void Fl_Type::add(Fl_Type *anchor, Strategy strategy) {
#if 0
Expand All @@ -637,8 +637,9 @@ void Fl_Type::add(Fl_Type *anchor, Strategy strategy) {
int target_level = 0; // adjust self to this new level

// Find the node after our insertion position
switch (strategy) {
case kAddAsFirstChild:
switch (strategy.placement()) {
case Strategy::AS_FIRST_CHILD:
default:
if (anchor == NULL) {
target = Fl_Type::first;
} else {
Expand All @@ -647,7 +648,7 @@ void Fl_Type::add(Fl_Type *anchor, Strategy strategy) {
target_parent = anchor;
}
break;
case kAddAsLastChild:
case Strategy::AS_LAST_CHILD:
if (anchor == NULL) {
/* empty */
} else {
Expand All @@ -656,7 +657,7 @@ void Fl_Type::add(Fl_Type *anchor, Strategy strategy) {
target_parent = anchor;
}
break;
case kAddAfterCurrent:
case Strategy::AFTER_CURRENT:
if (anchor == NULL) {
target = Fl_Type::first;
} else {
Expand Down
32 changes: 26 additions & 6 deletions fluid/Fl_Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ class Fd_Project_Reader;
class Fd_Project_Writer;

/**
Declare where a new type is placed in the hierarchy.
Declare where a new type is placed and how to create it.
Note that a type can also be the start of a hierarchy of types. In that case,
Placement can be as the first or last child of the anchor, or right after the
anchor. In most cases, the anchor is the last selected type node.
If the source is FROM_USER, widgets may be created with default titles and
labels. Type created FROM_FILE will start with no label, so the label is set
correctly later.
\see Fl_Type *Fl_..._Type::make(Strategy strategy) calls `add()`
Add single Type:
Expand All @@ -45,10 +50,25 @@ class Fd_Project_Writer;
Fl_Type *Fd_Project_Reader::read_children(Fl_Type *p, int merge, Strategy strategy, char skip_options)
int Fd_Project_Reader::read_project(const char *filename, int merge, Strategy strategy)
*/
typedef enum {
kAddAsFirstChild = 0,
kAddAsLastChild,
kAddAfterCurrent
typedef struct Strategy {
enum Flags {
AS_FIRST_CHILD = 0x0000,
AS_LAST_CHILD = 0x0001,
AFTER_CURRENT = 0x0002,
PLACEMENT_MASK = 0x000f,
FROM_USER = 0x0000,
FROM_FILE = 0x0010,
SOURCE_MASK = 0x00f0,
FROM_FILE_AS_FIRST_CHILD = 0x0010,
FROM_FILE_AS_LAST_CHILD = 0x0011,
FROM_FILE_AFTER_CURRENT = 0x0012,
};
Flags flags;
Strategy(Flags f) { flags = f; }
void placement(Flags f) { flags = (Flags)((flags & ~PLACEMENT_MASK) | (f & PLACEMENT_MASK)); }
Flags placement() { return (Flags)(flags & PLACEMENT_MASK); }
void source(Flags f) { flags = (Flags)((flags & ~SOURCE_MASK) | (f & SOURCE_MASK)); }
Flags source() { return (Flags)(flags & SOURCE_MASK); }
} Strategy;

enum ID {
Expand Down
10 changes: 6 additions & 4 deletions fluid/Fl_Widget_Type.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,16 @@ Fl_Widget_Type::ideal_size(int &w, int &h) {

/**
Make a new Widget node.
\param[in] strategy is kAddAsLastChild or kAddAfterCurrent
\param[in] strategy is Strategy::AS_LAST_CHILD or Strategy::AFTER_CURRENT
\return new node
*/
Fl_Type *Fl_Widget_Type::make(Strategy strategy) {
Fl_Type *anchor = Fl_Type::current, *pp = anchor;
if (pp && (strategy == kAddAfterCurrent)) pp = pp->parent;
if (pp && (strategy.placement() == Strategy::AFTER_CURRENT))
pp = pp->parent;
while (pp && !pp->is_a(ID_Group)) {
anchor = pp;
strategy = kAddAfterCurrent;
strategy.placement(Strategy::AFTER_CURRENT);
pp = pp->parent;
}
if (!pp || !pp->is_true_widget() || !anchor->is_true_widget()) {
Expand Down Expand Up @@ -141,7 +142,8 @@ Fl_Type *Fl_Widget_Type::make(Strategy strategy) {
t->factory = this;
// Construct the Fl_Widget:
t->o = widget(X,Y,W,H);
if (reading_file) t->o->label(0);
if (strategy.source() == Strategy::FROM_FILE)
t->o->label(0);
else if (t->o->label()) t->label(t->o->label()); // allow editing
t->o->user_data((void*)t);
// Put it in the parent:
Expand Down
14 changes: 7 additions & 7 deletions fluid/Fl_Window_Type.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,15 @@ int Overlay_Window::handle(int e) {

/**
Make and add a new Window node.
\param[in] strategy is kAddAsLastChild or kAddAfterCurrent
\param[in] strategy is Strategy::AS_LAST_CHILD or Strategy::AFTER_CURRENT
\return new node
*/
Fl_Type *Fl_Window_Type::make(Strategy strategy) {
Fl_Type *anchor = Fl_Type::current, *p = anchor;
if (p && (strategy == kAddAfterCurrent)) p = p->parent;
if (p && (strategy.placement() == Strategy::AFTER_CURRENT)) p = p->parent;
while (p && (!p->is_code_block() || p->is_a(ID_Widget_Class))) {
anchor = p;
strategy = kAddAfterCurrent;
strategy.placement(Strategy::AFTER_CURRENT);
p = p->parent;
}
if (!p) {
Expand Down Expand Up @@ -1073,10 +1073,10 @@ int Fl_Window_Type::handle(int event) {
{
Fl_Type *cc = Fl_Type::current;
Fl_Type::current = Fl_Type::current_dnd;
add_new_widget_from_user(prototype, kAddAsLastChild);
add_new_widget_from_user(prototype, Strategy::AS_LAST_CHILD);
Fl_Type::current = cc;
} else {
add_new_widget_from_user(prototype, kAddAsLastChild);
add_new_widget_from_user(prototype, Strategy::AS_LAST_CHILD);
}
popupx = 0x7FFFFFFF;
popupy = 0x7FFFFFFF; // mark as invalid (MAXINT)
Expand Down Expand Up @@ -1376,10 +1376,10 @@ Fl_Widget_Class_Type *current_widget_class = 0;
*/
Fl_Type *Fl_Widget_Class_Type::make(Strategy strategy) {
Fl_Type *anchor = Fl_Type::current, *p = anchor;
if (p && (strategy == kAddAfterCurrent)) p = p->parent;
if (p && (strategy.placement() == Strategy::AFTER_CURRENT)) p = p->parent;
while (p && (!p->is_decl_block() || (p->is_widget() && p->is_class()))) {
anchor = p;
strategy = kAddAfterCurrent;
strategy.placement(Strategy::AFTER_CURRENT);
p = p->parent;
}
Fl_Widget_Class_Type *myo = new Fl_Widget_Class_Type();
Expand Down
20 changes: 10 additions & 10 deletions fluid/autodoc.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -383,22 +383,22 @@ void run_autodoc(const Fl_String &target_dir) {
// Create a silly project that contains all widgets that we want to document
new_project(false);

/*Fl_Type *t_func = */ add_new_widget_from_user("Function", kAddAsLastChild, false);
Fl_Window_Type *t_win = (Fl_Window_Type*)add_new_widget_from_user("Fl_Window", kAddAsLastChild, false);
/*Fl_Type *t_func = */ add_new_widget_from_user("Function", Strategy::AS_LAST_CHILD, false);
Fl_Window_Type *t_win = (Fl_Window_Type*)add_new_widget_from_user("Fl_Window", Strategy::AS_LAST_CHILD, false);
t_win->label("My Main Window");
Fl_Widget_Type *t_grp = (Fl_Widget_Type*)add_new_widget_from_user("Fl_Group", kAddAsLastChild, false);
Fl_Widget_Type *t_grp = (Fl_Widget_Type*)add_new_widget_from_user("Fl_Group", Strategy::AS_LAST_CHILD, false);
t_grp->public_ = 0;
Fl_Widget_Type *t_btn = (Fl_Widget_Type*)add_new_widget_from_user("Fl_Button", kAddAsLastChild, false);
Fl_Widget_Type *t_btn = (Fl_Widget_Type*)add_new_widget_from_user("Fl_Button", Strategy::AS_LAST_CHILD, false);
t_btn->comment("Don't press this button!");
t_btn->name("emergency_btn");
((Fl_Button*)t_btn->o)->shortcut(FL_COMMAND|'g');
Fl_Type *t_sldr = add_new_widget_from_user("Fl_Slider", kAddAsLastChild, false);
Fl_Type *t_inp = add_new_widget_from_user("Fl_Input", kAddAsLastChild, false);
Fl_Type *t_flx = add_new_widget_from_user("Fl_Flex", kAddAsLastChild, false);
Fl_Type *t_flxc = add_new_widget_from_user("Fl_Button", kAddAsLastChild, false);
Fl_Type *t_sldr = add_new_widget_from_user("Fl_Slider", Strategy::AS_LAST_CHILD, false);
Fl_Type *t_inp = add_new_widget_from_user("Fl_Input", Strategy::AS_LAST_CHILD, false);
Fl_Type *t_flx = add_new_widget_from_user("Fl_Flex", Strategy::AS_LAST_CHILD, false);
Fl_Type *t_flxc = add_new_widget_from_user("Fl_Button", Strategy::AS_LAST_CHILD, false);
select_only(t_grp);
Fl_Type *t_grd = add_new_widget_from_user("Fl_Grid", kAddAsLastChild, false);
Fl_Type *t_grdc = add_new_widget_from_user("Fl_Button", kAddAsLastChild, false);
Fl_Type *t_grd = add_new_widget_from_user("Fl_Grid", Strategy::AS_LAST_CHILD, false);
Fl_Type *t_grdc = add_new_widget_from_user("Fl_Button", Strategy::AS_LAST_CHILD, false);

widget_browser->rebuild();
g_project.update_settings_dialog();
Expand Down
2 changes: 1 addition & 1 deletion fluid/custom_widgets.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ int Widget_Bin_Window_Button::handle(int inEvent)
// 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, kAddAfterCurrent);
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;
Expand Down
Loading

0 comments on commit 050cbab

Please sign in to comment.