From a41d0b067d5907eb9a0540f8cfb02fba84c28f3dd84912cb161ee58e74b1e00e Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Thu, 21 Aug 2025 19:19:28 -0400 Subject: [PATCH] Partially done. --- dw/layout.cc | 21 ++++++++------------- dw/layout.hh | 12 +++++------- dw/textblock.cc | 8 ++++---- dw/textblock.hh | 4 ++-- dw/widget.hh | 8 ++++---- 5 files changed, 23 insertions(+), 30 deletions(-) diff --git a/dw/layout.cc b/dw/layout.cc index 69f3a7d..875636a 100644 --- a/dw/layout.cc +++ b/dw/layout.cc @@ -248,13 +248,6 @@ bool Layout::LinkEmitter::emitClick (Widget *widget, int link, int img, // --------------------------------------------------------------------- -Layout::Anchor::~Anchor () -{ - free(name); -} - -// --------------------------------------------------------------------- - Layout::Layout (std::unique_ptr< Platform > platform, bool limit) { this->platform = std::move( platform ); @@ -731,18 +724,20 @@ void Layout::setAnchor (const std::optional< std::string_view > anchor) /** * Used, when the widget is not allocated yet. */ -char *Layout::addAnchor (Widget *widget, const char* name) +std::optional< std::string > +Layout::addAnchor (Widget *widget, const std::string &name) { return addAnchor (widget, name, -1); } -char *Layout::addAnchor (Widget *widget, const char* name, int y) +std::optional< std::string > +Layout::addAnchor (Widget *widget, const std::string &name, int y) { if (anchorsTable.contains (name)) - return NULL; + return std::nullopt; else { auto anchor = std::make_unique< Anchor >(); - char *const rv= anchor->name = dStrdup (name); + std::string rv= anchor->name= name; anchor->widget = widget; anchor->y = y; @@ -753,7 +748,7 @@ char *Layout::addAnchor (Widget *widget, const char* name, int y) } } -void Layout::changeAnchor (Widget *widget, char* name, int y) +void Layout::changeAnchor (Widget *widget, const std::string &name, int y) { Anchor *const anchor = anchorsTable.at( name ).get(); assert (anchor); @@ -762,7 +757,7 @@ void Layout::changeAnchor (Widget *widget, char* name, int y) updateAnchor (); } -void Layout::removeAnchor (Widget *widget, char* name) +void Layout::removeAnchor (Widget *widget, const std::string &name) { anchorsTable.erase( name ); } diff --git a/dw/layout.hh b/dw/layout.hh index 2c33f5b..2dc5a3e 100644 --- a/dw/layout.hh +++ b/dw/layout.hh @@ -150,11 +150,9 @@ private: class Anchor: public lout::object::Object { public: - char *name; + std::string name; Widget *widget; int y; - - ~Anchor (); }; std::unique_ptr< Platform > platform; @@ -236,10 +234,10 @@ private: /* Widget */ - char *addAnchor (Widget *widget, const char* name); - char *addAnchor (Widget *widget, const char* name, int y); - void changeAnchor (Widget *widget, char* name, int y); - void removeAnchor (Widget *widget, char* name); + std::optional< std::string > addAnchor (Widget *widget, const std::string &name); + std::optional< std::string > addAnchor (Widget *widget, const std::string &name, int y); + void changeAnchor (Widget *widget, const std::string &name, int y); + void removeAnchor (Widget *widget, const std::string &name); void setCursor (style::Cursor cursor); void updateCursor (); void queueDraw (int x, int y, int width, int height); diff --git a/dw/textblock.cc b/dw/textblock.cc index c4ce215..532faa0 100644 --- a/dw/textblock.cc +++ b/dw/textblock.cc @@ -2555,11 +2555,11 @@ void Textblock::addWidget (core::Widget *widget, core::style::Style *style) * Return true on success, and false, when this anchor had already been * added to the widget tree. */ -bool Textblock::addAnchor (const char *name, core::style::Style *style) +bool Textblock::addAnchor (const std::string &name, core::style::Style *style) { - DBG_OBJ_ENTER ("construct.word", 0, "addAnchor", "\"%s\", %p", name, style); + DBG_OBJ_ENTER ("construct.word", 0, "addAnchor", "\"%s\", %p", name.c_str(), style); - char *copy; + std::optional< std::string > copy; int y; bool result; @@ -2574,7 +2574,7 @@ bool Textblock::addAnchor (const char *name, core::style::Style *style) } else copy = Widget::addAnchor (name); - if (copy == NULL) + if (not copy.has_value()) /** * \todo It may be necessary for future uses to save the anchor in * some way, e.g. when parts of the widget tree change. diff --git a/dw/textblock.hh b/dw/textblock.hh index 328ff0f..ee7882e 100644 --- a/dw/textblock.hh +++ b/dw/textblock.hh @@ -505,7 +505,7 @@ protected: struct Anchor { - char *name; + std::string_view name; int wordIndex; }; @@ -890,7 +890,7 @@ public: inline void addText (const char *text, core::style::Style *style) { addText (text, strlen(text), style); } void addWidget (core::Widget *widget, core::style::Style *style); - bool addAnchor (const char *name, core::style::Style *style); + bool addAnchor (const std::string &name, core::style::Style *style); void addSpace (core::style::Style *style); void addBreakOption (core::style::Style *style, bool forceBreak); void addParbreak (int space, core::style::Style *style); diff --git a/dw/widget.hh b/dw/widget.hh index d05d2de..a5a12c7 100644 --- a/dw/widget.hh +++ b/dw/widget.hh @@ -394,16 +394,16 @@ protected: virtual void enterNotifyImpl (EventCrossing *event); virtual void leaveNotifyImpl (EventCrossing *event); - inline char *addAnchor (const char* name) + inline std::optional< std::string > addAnchor (const std::string &name) { return layout->addAnchor (this, name); } - inline char *addAnchor (const char* name, int y) + inline std::optional< std::string > addAnchor (const std::string &name, int y) { return layout->addAnchor (this, name, y); } - inline void changeAnchor (char* name, int y) + inline void changeAnchor (const std::string &name, int y) { layout->changeAnchor (this, name, y); } - inline void removeAnchor (char* name) + inline void removeAnchor (const std::string &name) { if (layout) layout->removeAnchor (this, name); } //inline void updateBgColor () { layout->updateBgColor (); }