Fl_Multi_Label example seems broken

26 views
Skip to first unread message

Robert Arkiletian

unread,
Jun 15, 2021, 12:34:19 PM6/15/21
to fltk.general
In the examples directory file 

seems broken. If lines 159, 163, 167 are commented out

item->image(L_document_pixmap); // note: this clobbers the item's label()
item->image(L_folder_pixmap);
item->image(L_redx_pixmap);
 
then none of the Fl_Multi_Label images are displayed. Even the label images under File menu are not displayed. *BUT* if even one of the above item->image lines is executed then all the images under File menu are displayed.  Looking at the original example file 
created by Greg E. I noticed that there are a couple lines removed in function AddItemToMenu in the current example in the git repo. 

Specifically, the original code has these lines which were removed.
line 118
const char *itemtext = item->label();
line 121
item->image(*pixmap);  
and the edit to line 132
ml->labelb = itemtext;

If I apply these changes, restoring the original code, then the example seems to work properly again. Wondering why those lines were removed from the original example?

The reason for my query is that we are trying to add Fl_Multi_Label support to pyFltk but we are trying to hide the implementation of function AddItemToMenu by adding 
void add_multi_label(Fl_Pixmap* pixmap)
method to Fl_Menu_Item 

Greg Ercolano

unread,
Jun 15, 2021, 1:34:04 PM6/15/21
to fltkg...@googlegroups.com


On 6/15/21 9:34 AM, Robert Arkiletian wrote:
In the examples directory file 

seems broken. If lines 159, 163, 167 are commented out

item->image(L_document_pixmap); // note: this clobbers the item's label()
item->image(L_folder_pixmap);
item->image(L_redx_pixmap);
 
then none of the Fl_Multi_Label images are displayed. Even the label images under File menu are not displayed. *BUT* if even one of the above item->image lines is executed then all the images under File menu are displayed.  Looking at the original example file 
created by Greg E. I noticed that there are a couple lines removed in function AddItemToMenu in the current example in the git repo. 

Specifically, the original code has these lines which were removed.
line 118
const char *itemtext = item->label();
line 121
item->image(*pixmap);  
and the edit to line 132
ml->labelb = itemtext;

If I apply these changes, restoring the original code, then the example seems to work properly again. Wondering why those lines were removed from the original example?

I'm not quite awake yet, but I'm guessing this has to do with changes made on Mon Sep 11 18:12:58 2017 +0000
as per a thread on fltk.coredev entitled "Fl_Multi_Label: leading underbars in public API"

To read this thread which evolved over the period Sep 9 - Sep 15, you can go here:
https://www.fltk.org/newsgroups.php?gfltk.coredev
..and then in the Search: bar type:

    subject: Fl_Multi_Label:

..and that should show the interchange leading to the modification which can be seen in git:

$ git diff 2a41af 88204a

@@ -117,6 +117,10 @@ int AddItemToMenu(Fl_Menu_   *menu,                   // menu to add item to
 
   if ( !pixmap ) return i;
   Fl_Menu_Item *item = (Fl_Menu_Item*)&(menu->menu()[i]);
+  const char *itemtext = item->label();         // keep item's label() -- item->image() clobbers it!
+
+  // Assign image to menu item
+  item->image(*pixmap);                  // note: clobbers item->label()
 
   // Create a multi label, assign it an image + text
   Fl_Multi_Label *ml = new Fl_Multi_Label;
@@ -127,7 +131,7 @@ int AddItemToMenu(Fl_Menu_   *menu,                   // menu to add item to
 
   // Right side of label is text
   ml->typeb  = FL_NORMAL_LABEL;
-  ml->labelb = item->label();
+  ml->labelb = itemtext;
 
   // Assign multilabel to item
   ml->label(item);

..and here's the log entry for that change:

    $ git log 88204a5..2a41af1

    Applied Manolo's recommendation (fltk.coredev), removing unnecessary item->image(*pixmap) call.

I don't know if I'll have time to pick through that thread to figure out a reduced answer
to your question, but perhaps you can put the pieces together before I do..

Robert Arkiletian

unread,
Jun 15, 2021, 2:51:42 PM6/15/21
to fltk.general
Yes, you found it. That is the git commit in question.  Reading through the thread, you pointed me at, I found the post by Manolo here
where he mentions

===
The source code of Fl_Multi_Label::label(Fl_Menu_Item* o) is
    Fl::set_labeltype(_FL_MULTI_LABEL, multi_labeltype, multi_measure);
    o->label(_FL_MULTI_LABEL, (const char*)this);
Thus, the difference between the 1st and the 2nd approaches listed above
is the call to Fl::set_labeltype(). Indeed using the 1st approach once
and then the 2nd approach produces correct multi-labelled menu items.

The API inconsistency could thus be solved adding an Fl_Multi_Label
constructor:
Fl_Multi_Label::Fl_Multi_Label() {
  static unsigned count = 0;
  if (!count++) Fl::set_labeltype(_FL_MULTI_LABEL, multi_labeltype, multi_measure);
}
===

The nagging question is why does calling item.image *just once* allow all the images to be displayed?
Digging into the docs. item.image  is actually a fluid compatibility
code which just calls Fl_Pixmap's label method.
So therefore you can replace

item->image(L_redx_pixmap);
with
Fl_Pixmap* pm = &L_redx_pixmap;
pm->label(item);

and the other menu items now display images. Therefore, it must be
something in the label method.

Just as Manolo concluded, I looked at the implementation of label method for pixmap.
void Fl_Pixmap::label(Fl_Menu_Item* m) {
 Fl::set_labeltype(_FL_IMAGE_LABEL, labeltype, Fl_Image::measure);
 m->label(_FL_IMAGE_LABEL, (const char*)this);
}
I agree with Manolo that Fl::set_labeltype  line is allowing all the menu item
images to be displayed. It only needs to be called once.
It is defined as
"Sets the functions to call to draw and measure a specific labeltype."
So I tried to call this line

Fl::set_labeltype(_FL_IMAGE_LABEL, Fl_Image::labeltype, Fl_Image::measure);

but unfortunately labeltype and measure functions are protected, not public.
I'm not sure how to solve this issue. An easy fix would be to revert the example code to the original.  But would that just be hiding the real bug. I'm just not sure.

Robert Arkiletian

unread,
Jun 15, 2021, 4:18:52 PM6/15/21
to fltk.general
My apologies to the list, this issue is fixed in 1.4. 

I was trying the example code using 1.3.6, and FL_IMAGE_LABEL was not defined so I changed it to _FL_IMAGE_LABEL. 

The issue is only present with 1.3.6.
The example code works perfectly with 1.4. Sorry for all the noise.



Reply all
Reply to author
Forward
0 new messages