Skip to content

Commit f16deca

Browse files
committed
+ Widget::Contains()
1 parent 60ec919 commit f16deca

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

src/Widget.cpp

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,29 @@ void Widget::BringToFront()
496496
PtWidgetToFront(widget());
497497
}
498498

499+
bool Widget::Contains(const Widget &widget)
500+
{
501+
PtWidget_t *this_widget = this->widget();
502+
503+
if(false == PtIsContainer(this_widget))
504+
return false;
505+
506+
PtWidget_t *front = PtWidgetChildFront(this_widget);
507+
508+
if(NULL == front)
509+
return false;
510+
511+
PtWidget_t *other_widget = widget.widget();
512+
513+
for(PtWidget_t *next = front; next != NULL; next = PtWidgetBrotherBehind(next))
514+
{
515+
if(next == other_widget)
516+
return true;
517+
}
518+
519+
return false;
520+
}
521+
499522
void Widget::SetBounds(short x, short y, unsigned short width, unsigned short height)
500523
{
501524
PhDim_t size;
@@ -525,18 +548,25 @@ bool Widget::Focus()
525548
return PtGiveFocus(widget(), nullptr) != nullptr;
526549
}
527550

528-
Widget Widget::GetNextWidget(Widget widget, bool forward) const
551+
Widget Widget::GetNextWidget(const Widget &widget, bool forward) const
529552
{
530553
PtWidget_t *this_widget = this->widget();
531554

532555
PtWidget_t *result =
533556
forward ?
534-
PtWidgetChildFront(this_widget):
535-
PtWidgetChildBack(this_widget);
557+
PtWidgetBrotherInFront(this_widget):
558+
PtWidgetBrotherBehind(this_widget);
536559

537560
if(NULL == result)
538-
throw(
539-
std::out_of_range(std::string("PhWidgets::Widget::GetNextWidget(): \'") + WidgetClassName(this_widget) + "\' widget has no children"));
561+
{
562+
result =
563+
forward ?
564+
PtWidgetBrotherBehind(this_widget):
565+
PtWidgetBrotherInFront(this_widget);
566+
}
567+
568+
if(NULL == result)
569+
return *this;
540570

541571
return Widget(result);
542572
}

src/Widget.h

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,36 @@ namespace PhWidgets
10921092
*/
10931093
void BringToFront();
10941094

1095+
//! Retrieves a value indicating whether the specified widget is a child of the widget.
1096+
/*!
1097+
@param[in] widget The Widget to evaluate.
1098+
@returns `true` if the specified widget is a child of the widget; otherwise, `false`.
1099+
1100+
### Examples ###
1101+
1102+
The following code example ensures that a Label is visible by calling its BringToFront() method.
1103+
This example requires that you have a Window `main_window` with a Label named `label1`.
1104+
1105+
@code
1106+
void MakeLabelVisible()
1107+
{
1108+
// If the window contains label1, bring it
1109+
// to the front to make sure it is visible.
1110+
1111+
if(main_window.Contains(label1))
1112+
{
1113+
label1.BringToFront();
1114+
}
1115+
}
1116+
@endcode
1117+
1118+
@see
1119+
- BringToFront()
1120+
- SendToBack()
1121+
- Widgets
1122+
*/
1123+
bool Contains(const Widget &widget);
1124+
10951125
//! Sets input focus to the widget.
10961126
/*!
10971127
@return `true` if the input focus request was successful; otherwise, `false`.
@@ -1145,7 +1175,7 @@ namespace PhWidgets
11451175
- Container::ActiveWidget
11461176
- Widgets
11471177
*/
1148-
Widget GetNextWidget(Widget widget, bool forward = true) const;
1178+
Widget GetNextWidget(const Widget &widget, bool forward = true) const;
11491179

11501180
//! Sends the widget to the back of the z-order.
11511181
/*!

0 commit comments

Comments
 (0)