Adding a BButton to a BTextControl, how?

Hi there!

I have a class “DateBox” that inherits from another class, which in turn is a BTextControl.
Now, I want to add a BButton to the right of the BTextControl widget. How to do that?

In my naivety, I tried this inside the DateBox ctor:

BGroupLayout* textcontrolGroup;
BLayoutBuilder::Group<>(this, B_HORIZONTAL, 0)
	.GetLayout(&textcontrolGroup)
	.Add(fCalenderButton)
	.End();

This results in this (though I have the same result without the .GetLayout(&textcontrolGroup) line…):

datebox

The window creates this layout with:

	.Add(fStartDate->CreateLabelLayoutItem(), 0, 1)
	.Add(fStartDate->CreateTextViewLayoutItem(), 1, 1, 3)

fStartDate being the DateBox.
So, the label of the BTextControl isn’t found and the TextView is drawn without borders.

Did I explain it clearly enough? I’m a selftaught (well, more like continuously screwing up and sometimes learning…) coder, so I might lack the right vernacular.
Be gentle with me… :smiley:

1 Like

It’s a matter of experimentation and testing, thinking about solutions and applying them, whether they work or not, it’s always good to study similar codes to understand the logic.

Until someone knowledgeable comes and say what The Right Way to do it is, a workaround may be to not do it at all. That is, to handle the button independently:

	.Add(fStartDate->CreateLabelLayoutItem(), 0, 1)
	.Add(fStartDate->CreateTextViewLayoutItem(), 1, 1, 2)
	.Add(fStartDate->CalendarButton(), 3, 1)

In my very simple test it made the component taller, though.

From the point of view of someone who does not know about this, there’s quite some magic in BTextControl. The text box is a child view, with a smaller frame to account for the border you are missing, which is drawn by the BTextControl, not by the view with the text. You may need to override Draw(), and even more layout stuff.

Yeah, I came to the same conclusion.

It’s not if you do a SetExplicitSize() for the button with the height of the BTextControl:

	float height;
	fDateBox->GetPreferredSize(NULL, &height);
	BSize size(height + 2, height);
	SetExplicitSize(size);

See:

calbutton

1 Like