Label set_markup

Hi there, @gwillems

Actually this topic is related to the Gnome Issue#5900
I have text like this:

cText += "|                                                                                                     |\n";
cText += "|Su Mo Tu We Th Fr Sa      Su Mo Tu We Th Fr Sa      Su Mo Tu We Th Fr Sa      Su Mo Tu We Th Fr Sa   |\n";
cText += "|    1  2  3  4  5  6 00                1  2  3 04                   1  2 08       1  2  3  4  5  6 13|\n";

When this text is set to the Label through set_markup it’s looks awkward

So I have no idea how come… 3 lines with the same width are printed in wrong way

Here an example is written in c++

testWin.hpp

#pragma once

#include <gtkmm/applicationwindow.h>
#include <gtkmm/label.h>
#include <gtkmm/box.h>

class testWin : public Gtk::ApplicationWindow {
 public:
  testWin();

 private:
  void onMap(GdkEventAny*);

  Gtk::Label lLabel,cLabel,rLabel;
  Gtk::Box box;
  Glib::ustring cText;
};

testApp.hpp

#pragma once

#include <gtkmm/application.h>
#include "testWin.hpp"

class testApp : public Gtk::Application {
 protected:
  testApp();
  // Signals
  void on_activate() override;

 public:
  static Glib::RefPtr<Gtk::Application> create();

 private:
  testWin* createAppWin();
};

testApp.cpp

#include "testApp.hpp"

testApp::testApp() :
  Gtk::Application("org.gtkmm.examples.application") {
}

Glib::RefPtr<Gtk::Application> testApp::create() {
  static Glib::RefPtr<Gtk::Application> app{new testApp()};
  return app;
}

void testApp::on_activate()
{
  // The application has been started, so let's show a window.
  auto appWin{createAppWin()};
  appWin->present();
}

testWin* testApp::createAppWin() {
  auto appWin{new testWin()};

  add_window(*appWin);
  appWin->signal_hide().connect([appWin](){ delete appWin; });

  return appWin;
}

testWin.cpp

#include "testWin.hpp"

testWin::testWin()
: Gtk::ApplicationWindow(),
  lLabel{"Left Label"},
  cLabel{"Center Label"},
  rLabel{"Right Label"},

  box{Gtk::ORIENTATION_HORIZONTAL, 0} {
  box.pack_start(lLabel);
  box.set_center_widget(cLabel);
  box.pack_end(rLabel);

  cLabel.set_justify(Gtk::Justification::JUSTIFY_RIGHT);
  cLabel.set_line_wrap(true);
  cLabel.set_line_wrap_mode(Pango::WrapMode::WRAP_WORD_CHAR);
  bool tf{false};
  cLabel.get_layout()->set_justify(tf);
  cLabel.get_layout()->set_single_paragraph_mode(tf);
  cLabel.get_layout()->set_indent(tf);
  cLabel.get_layout()->set_auto_dir(tf);
  cLabel.get_layout()->set_alignment(Pango::Alignment::ALIGN_LEFT);

  this->set_title("test app");
  this->set_name("test app");
  this->set_size_request(1908, 39);
  this->add(box);
  cText += "|                                                                                                     |\n";
  cText += "|Su Mo Tu We Th Fr Sa      Su Mo Tu We Th Fr Sa      Su Mo Tu We Th Fr Sa      Su Mo Tu We Th Fr Sa   |\n";
  cText += "|    1  2  3  4  5  6 00                1  2  3 04                   1  2 08       1  2  3  4  5  6 13|\n";

  cLabel.set_markup(cText);

  this->signal_map_event().connect_notify(sigc::mem_fun(*this, &testWin::onMap));
}

void testWin::onMap(GdkEventAny*) {
  this->show_all();
}

test.cpp

#include "testApp.hpp"

int main(int argc, char* argv[]) {
  auto app{testApp::create()};

  return app->run(argc, argv);
}

Are you sure the font your app is using is monospace? The characters wont have a uniform size if it isnt.

Even when I’m using <span font-family=‘’> it doesn’t work

My apologies. With <span font-family=‘monospace’> works correct.
Thank you @HighKingofMelons

@HighKingofMelons even set_markup with monospace works correct, here the problem with set_tooltip_markup

Seems like there isnt enough horizontal space so the end of each line flows into a new one.

Is there a particular reason you are trying to squeeze a whole calender into a tooltip?

In the current example I’m using the same variable labelTxt which is used for both method set_markup and set_label_markup without any differences . So it’s strange set_markup works great wheres set_tooltip_markup is not. And at least means that both API are working in a different way

The reason: in Waybar we have clock module which can print month/year calendar for user purposes. Waybar Issue#2240 So user complains that if they set more than 2-3 columns(where each column is repesented by the separate Month) tooltip with the calendar is looking awkward


So temporary I switched tooltip for the Label to custom tooltip widget with TextView which solved the problem. But to me it seems not so performant solution … due to I have to set tooltip widget each signal time.

I see.

It does seem that the label that is built into GtkTooltip indeed has been set to have a max width of 50. Which does at least make sense for it’s primary purpose as a tooltip.

I can’t at a glance identify a better current solution than the one you have already. Though i think you might be overestimating the performance impact a little bit.

It migth be good to open an issue about letting you specify the width of the label in the tooltip though.

No, it’s not a good idea.

Don’t use a tooltip.

If you want to display something more complex than a simple line of text, you should have your own GtkPopover with a custom content.

1 Like

Thank you both, @ebassi and @HighKingofMelons . Let me check GtkPopover.

You can probably close the issue you linked, since it’s expected behavior.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.