Widget alignment based on external label

15 views
Skip to first unread message

supsm17

unread,
Apr 10, 2024, 6:15:23 PMApr 10
to fltk.general
Let's say I have a Fl_Input with an external label "Input". How can I align the widget such that the labels are aligned with other widgets, rather than just the box?

Matthias Melcher

unread,
Apr 10, 2024, 7:46:46 PMApr 10
to fltk.general
I am not entirely sure I understand. Labels can be inside or outside the widget box (inside makes little sense on Fl_Input. They can be aligned around the box in 12 positions, top-left, top-cent, -top-right, right-top, right-center, etc. . They don't "see" what other widget are doing inside the same group. If you need more flexibility, it's probably best to have no label on your Fl_Input and instead position an Fl_Box with the label text next to the input. You can of course set the box's boxtype to FLAT_BOX to make it look like a regular label.

Lastly, you can use FLUID to do all this visually if you like. The FLUID alignment hints may be very helpful here.

supsm17

unread,
Apr 10, 2024, 7:51:51 PMApr 10
to fltk.general
Sorry if the question wasn't clear. I would like the positioning of the widgets to be based off the top left corner of the label box, rather than the actual widget box. So, specifying (0, 0) as (x, y) places the label along with the input box flush with the top and left, rather than only the input box (with the label cut off).
I've tried fluid, but it seems that all the alignment and positioning is based off the main widget box (alignment "snapping" does not work with the label box), and the best I can do is just carefully align the label box to the desired location.

It does seem that maybe having a Fl_Box as a separate "label" is the best move here.

Matthias Melcher

unread,
Apr 10, 2024, 8:03:22 PMApr 10
to fltk.general
Ok, understood. There is no API to the label box within the Widget class. You can use fl_measure to get the label size. For example (untested code). I

// Assumes alignment is on the left and only adjusts the left side and width.
// After call, widget *and* label will fit within the box.
void resize_w_label(Fl_Widget *wgt, int xa, int ya, int wa, int ha) {
  int label_wdt = 0, label_hgt = 0;
  fl_font(wgt->labelfont(), wgt->labelsize());
  fl_measure(wgt->label(), label_wdt, label_hgt);
  wgt->resize(xa + label_wdt, ya, wa - label_wdt, ha);
}

Gonzalo Garramuño

unread,
Apr 10, 2024, 8:17:14 PMApr 10
to fltkg...@googlegroups.com
There are several ways, depending on what you prefer:

1) Create an Fl_Group and place two widgets Box for label, Input without
label. and position them using X and W coordinates.  If you use fluid,
it can help.
2) Create a Fl_Pack, with similar widgets as Fl_Group.  Fl_Pack will
pack the two widgets next to each other if you set the pack
horizontally.  You just need to set the widget of the Fl_Group's widgets
and the Fl_Pack's height.
3) If you are using fltk1.4, use Fl_Flex.  Similar to Fl_Pack (it is its
default behavior).  But you can set the size of one widget (say the
label) and let the other resize as you will.

Gonzalo Garramuño
ggar...@gmail.com

Matthias Melcher

unread,
Apr 13, 2024, 7:00:10 AMApr 13
to fltk.general
@OP: did any of this solve your question?
Reply all
Reply to author
Forward
0 new messages