diff options
author | Rodrigo Arias Mallo <rodarima@gmail.com> | 2024-08-16 13:19:26 +0200 |
---|---|---|
committer | Rodrigo Arias Mallo <rodarima@gmail.com> | 2024-10-17 20:38:16 +0200 |
commit | 49623838743ad69811dd8348c57ca0baed46934c (patch) | |
tree | 25f38db754a740e0880e6c7c1cc455e618033e03 | |
parent | b6c8599a5acdaddccca102bae4370316beaa1915 (diff) |
Allow widgets to adjust new requisition
When a widget calls the parent to correct its own requisition, let the
child widget perform adjustments on the requisition. This allows images
to control the height when the parent changes the width, so the image
can preserve its own aspect ratio.
-rw-r--r-- | dw/image.cc | 16 | ||||
-rw-r--r-- | dw/image.hh | 1 | ||||
-rw-r--r-- | dw/widget.cc | 19 | ||||
-rw-r--r-- | dw/widget.hh | 1 |
4 files changed, 36 insertions, 1 deletions
diff --git a/dw/image.cc b/dw/image.cc index 44a1ff45..603f2964 100644 --- a/dw/image.cc +++ b/dw/image.cc @@ -402,6 +402,22 @@ void Image::sizeRequestSimpl (core::Requisition *requisition) DBG_OBJ_LEAVE (); } +void Image::setReqWidth(core::Requisition *requisition, int width) +{ + /* If we have the image buffer, try to set the height to preserve the image + * ratio */ + if (buffer) { + int w = buffer->getRootWidth(); + int h = buffer->getRootHeight(); + float ratio = (float) h / (float) w; + int height = (float) width * ratio; + /* Preserve descent */ + requisition->ascent = height - requisition->descent; + } + + requisition->width = width; +} + void Image::getExtremesSimpl (core::Extremes *extremes) { int contentWidth; diff --git a/dw/image.hh b/dw/image.hh index ae47f307..5d890c4e 100644 --- a/dw/image.hh +++ b/dw/image.hh @@ -130,6 +130,7 @@ private: bool isMap; protected: + void setReqWidth(core::Requisition *requisition, int width); void sizeRequestSimpl (core::Requisition *requisition); void getExtremesSimpl (core::Extremes *extremes); void sizeAllocateImpl (core::Allocation *allocation); diff --git a/dw/widget.cc b/dw/widget.cc index 1099ee77..ce5286b9 100644 --- a/dw/widget.cc +++ b/dw/widget.cc @@ -1873,6 +1873,17 @@ void Widget::correctRequisitionOfChild (Widget *child, Requisition *requisition, requisition->descent); } +void Widget::setReqWidth (Requisition *requisition, int width) +{ + /* Naive implementation */ + requisition->width = width; +} + +/** Correct a child requisition to fit the parent. + * + * The \param requisition is adjusted in width so it fits in the current widget + * (parent). + */ void Widget::correctReqWidthOfChild (Widget *child, Requisition *requisition, bool allowDecreaseWidth) { @@ -1887,8 +1898,14 @@ void Widget::correctReqWidthOfChild (Widget *child, Requisition *requisition, if (!allowDecreaseWidth && limitMinWidth < requisition->width) limitMinWidth = requisition->width; + int width = requisition->width; child->calcFinalWidth (child->getStyle(), -1, this, limitMinWidth, true, - &requisition->width); + &width); + + + /* Ask the child widget to adjust its own requisition in case it has to + * modify the height too (like images to try to preserve the aspect ratio) */ + child->setReqWidth(requisition, width); DBG_OBJ_LEAVE_VAL ("%d * (%d + %d)", requisition->width, requisition->ascent, requisition->descent); diff --git a/dw/widget.hh b/dw/widget.hh index 1787be1f..a7daa91d 100644 --- a/dw/widget.hh +++ b/dw/widget.hh @@ -351,6 +351,7 @@ protected: virtual int getAvailWidthOfChild (Widget *child, bool forceValue); virtual int getAvailHeightOfChild (Widget *child, bool forceValue); + virtual void setReqWidth (Requisition *requisition, int width); virtual void correctRequisitionOfChild (Widget *child, Requisition *requisition, void (*splitHeightFun) (int, int*, |