diff options
| author | Albrecht Schlosser <albrechts.fltk@online.de> | 2022-01-22 18:36:58 +0100 |
|---|---|---|
| committer | Albrecht Schlosser <albrechts.fltk@online.de> | 2022-11-22 19:01:39 +0100 |
| commit | fa6d95a7932e4192671b5d1ca294131380e08374 (patch) | |
| tree | 5b55ceced4b9f750e741969dbcc77b0656c924f1 /FL/Fl_Rect.H | |
| parent | 495b2395c14607c030a0270ed7341bf04be7fa56 (diff) | |
Enhance Fl_Rect: add inset() and new constructor
These new features can be used by widgets that draw inside a rectangle
taking the border width into account.
Diffstat (limited to 'FL/Fl_Rect.H')
| -rw-r--r-- | FL/Fl_Rect.H | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/FL/Fl_Rect.H b/FL/Fl_Rect.H index 32ce01ffb..a73a0e732 100644 --- a/FL/Fl_Rect.H +++ b/FL/Fl_Rect.H @@ -1,7 +1,7 @@ // // Fl_Rect header file for the Fast Light Tool Kit (FLTK). // -// Copyright 1998-2018 by Bill Spitzak and others. +// Copyright 1998-2022 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 @@ -50,6 +50,17 @@ public: Fl_Rect(int X, int Y, int W, int H) : x_(X), y_(Y), w_(W), h_(H) {} + /** This constructor creates a rectangle with the given x,y coordinates + and the given width and height reduced by the box frame size. + + This is the same as using the constructor w/o \p bt and subsequently + calling inset(\p bt). + */ + Fl_Rect(int X, int Y, int W, int H, Fl_Boxtype bt) + : x_(X), y_(Y), w_(W), h_(H) { + inset(bt); + } + /** This constructor creates a rectangle based on a widget's position and size. */ Fl_Rect (const Fl_Widget& widget) : x_(widget.x()), y_(widget.y()), w_(widget.w()), h_(widget.h()) {} @@ -77,6 +88,43 @@ public: void w(int W) { w_ = W; } ///< sets the width void h(int H) { h_ = H; } ///< sets the height + /** Move all edges in by \p d. + + Shrinks the rectangle by \p d at all sides keeping the center of the + rectangle at the same spot. + + If \p d is negative, the rectangle is enlarged. + + If \p d \>= w() or h() the result is undefined, i.e. an + invalid or empty rectangle. + */ + void inset(int d) { + x_ += d; + y_ += d; + w_ -= 2 * d; + h_ -= 2 * d; + } + + /** Move all edges in by the frame size of box type \p bt. + + Shrinks the rectangle at all sides by the frame width or height of the + given box type \p bt. + + This method uses the frame sizes given by the box type \p bt using + - Fl::box_dx(bt) + - Fl::box_dy(bt) + - Fl::box_dw(bt) + - Fl::box_dh(bt) + + If the rectangle is smaller than the frame sizes the result is undefined, + i.e. an invalid or empty rectangle. + */ + void inset(Fl_Boxtype bt) { + x_ += Fl::box_dx(bt); + y_ += Fl::box_dy(bt); + w_ -= Fl::box_dw(bt); + h_ -= Fl::box_dh(bt); + } }; // class Fl_Rect #endif // Fl_Rect_H |
