Something like an Fl_Output in a menubar

47 views
Skip to first unread message

david allen

unread,
May 7, 2026, 4:41:40 PMMay 7
to fltk.general
I would like to put something like an Fl_Output in a menubar just to show some status info. Can someone give me guidance for this?

I looked at the menubar example from the test directory. It has entry that looks like an instance of an Fl_Toggle_Button that I could mimic. But I couldn't find an instance of an Fl_Toggle_Button being created. 

Brian Larsen

unread,
May 8, 2026, 1:48:12 AMMay 8
to fltk.general
Here's a simple example that might help. I put an FL_Box on the right of the menubar, but you should be able to put whatever you want. 

// clang++ -Werror -Wall -Wextra -std=c++20  `fltk-config --cxxflags` -o menubartest menubartest.cpp `fltk-config --ldflags`
#include <stdio.h>
#include <string>
#include <time.h>

#include <FL/Fl.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Sys_Menu_Bar.H>
#include <FL/Fl_Window.H>

int main() {
  int mainX = 500;
  int mainY = 250;
  int mainW = 700;
  int mainH = 400;

  Fl_Window* winMain = new Fl_Window(mainX, mainY, mainW, mainH, "Menubar Test");
  winMain->color(FL_BLUE);

  int winMenuH = 25;
  Fl_Window* winMenu = new Fl_Window(0, 0, mainW, winMenuH, 0);

  // Limit the menuBar width so something can go on its right side.
  int mbW = 200;
  Fl_Sys_Menu_Bar* menuBar = new Fl_Sys_Menu_Bar(0, 0, mbW, winMenuH, 0);
  menuBar->box(FL_NO_BOX);

  menuBar->add("Abc", 0, 0, 0, FL_SUBMENU);
  menuBar->add("Abc/1", 0, 0, 0, 0);

  menuBar->add("Def", 0, 0, 0, FL_SUBMENU);
  menuBar->add("Def/2", 0, 0, 0, 0);

  // This goes to the right of the menuBar. It could be anything,
  // but this example displays the current time as the box label.
  Fl_Box* status = new Fl_Box(mbW, 0, mainW - mbW, winMenuH, 0);

  winMenu->end();
  winMain->end();

  winMain->show();

  char timBuf[100];

  while(winMain->visible()) {
    time_t now = time(0);
    strftime(timBuf, sizeof(timBuf), "%Y-%m-%d %H:%M:%S %Z %z", localtime(&now));
    status->copy_label(&timBuf[0]);

    Fl::wait(0.01);
  }
}

Ian MacArthur

unread,
May 8, 2026, 2:52:02 AMMay 8
to fltk.general
On Thursday, 7 May 2026 at 21:41:40 UTC+1 David wrote:
I would like to put something like an Fl_Output in a menubar just to show some status info. Can someone give me guidance for this?

I looked at the menubar example from the test directory. It has entry that looks like an instance of an Fl_Toggle_Button that I could mimic. But I couldn't find an instance of an Fl_Toggle_Button being created. 

That toggle button is a "special case" of Fl_Menu_Item - specifically it's an FL_MENU_TOGGLE, so that's something that the FL_MENU system "knows" how to draw.
The FL_MENU system is odd in that regard, it's not using the regular fltk widget mechanics (something we keep talking about changing...) so it can really only draw the things it knows about, not all the "regular" widgets.

You could possibly just make the last menu item have no callback, and then update its label on the fly to show your status?
Or do what Brian L. suggests and just make the menubar a bit short and put the status output beside (possibly with the help of one of the layout widgets, if you need resizing and so forth...)

david allen

unread,
May 8, 2026, 8:06:27 AMMay 8
to fltk.general
Thanks Ian for the explanation. Thanks Brian for the code --- it is perfect for my needs.

Brian Larsen

unread,
May 8, 2026, 8:50:33 AMMay 8
to fltk.general
You're welcome. By amazing coincidence I put that same idea on my todo list about 2 days ago, so your message motivated me to create that example and then enhance it for use in my app.
For reference, I decided that Fl_Group was a better container than the winMenu in the example. Also, I calculate the width of the menubar so that it can be as narrow as possible since my "status" area shows an absolute file name, which could be a bit long.

Albrecht Schlosser

unread,
May 8, 2026, 9:06:04 AMMay 8
to fltkg...@googlegroups.com
On 5/8/26 08:52 Ian MacArthur wrote:
You could possibly just make the last menu item have no callback, and then update its label on the fly to show your status?
Or do what Brian L. suggests and just make the menubar a bit short and put the status output beside (possibly with the help of one of the layout widgets, if you need resizing and so forth...)

I recommend the latter. For use with a one-liner of widgets like the menubar plus one or even more widgets I suggest to use Fl_Flex as the container widget so the resizing of the menubar is independent of the resizable() widget of the window.

Brian Larsen

unread,
May 8, 2026, 9:25:44 AMMay 8
to fltk.general
On Friday, May 8, 2026 at 9:06:04 PM UTC+8 Albrecht-S wrote:
On 5/8/26 08:52 Ian MacArthur wrote:

I recommend the latter. For use with a one-liner of widgets like the menubar plus one or even more widgets I suggest to use Fl_Flex as the container widget so the resizing of the menubar is independent of the resizable() widget of the window.

Thanks for that suggestion. I haven't used Fl_Flex before, but that sounds like it simplifies things, for example, it wouldn't be necessary to calculate the width of the menubar. I'll probably experiment with that in the next day or two.

Albrecht Schlosser

unread,
May 8, 2026, 11:25:21 AMMay 8
to fltkg...@googlegroups.com
On 5/8/26 15:25 Brian Larsen wrote:
On Friday, May 8, 2026 at 9:06:04 PM UTC+8 Albrecht-S wrote:
On 5/8/26 08:52 Ian MacArthur wrote:

I recommend the latter. For use with a one-liner of widgets like the menubar plus one or even more widgets I suggest to use Fl_Flex as the container widget so the resizing of the menubar is independent of the resizable() widget of the window.

Thanks for that suggestion.

You're welcome.


I haven't used Fl_Flex before, but that sounds like it simplifies things, for example, it wouldn't be necessary to calculate the width of the menubar.

That's correct. You can set the additional widget to fixed size, and the menu bar will fill the rest automatically.


I'll probably experiment with that in the next day or two.

Further info: my statement that this makes you independent of the resizable() of the window is not exactly true in all cases. If you find unexpected resize effects I suggest to use the Fl_Flex at the top and another group (Fl_Group or any other container widget) below (full width and height of the rest of the window) and set that group widget as the resizable() of the window. That group widget could be an Fl_Grid widget if you like that. This way you can create very flexible layouts (since 1.4.x).

Reply all
Reply to author
Forward
0 new messages