c++markupgtkmmpangomessagedialog

From a MessageDialog how to read variables when i use pango markup?


how to use pango markup in messagedialog text using variable

For example this code

void usb_boot::creation(){
//Gtk::MessageDialog dialogue(*this, listeDeroulante.get_active_text());
std::string message("Type de formatage : " + type), type1, chemin1;
Gtk::MessageDialog *dialogue = new Gtk::MessageDialog("Résumé", true, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO);
dialogue->set_title("Résumé");
dialogue->set_message("<span weight='bold'>message</span>",true);
dialogue->set_secondary_text("<b>listeDeroulante.get_active_text()</b>", true);
dialogue->set_default_response(Gtk::RESPONSE_YES);
int result = dialogue->run();

set_message and set_secondary_text have to print variables but just "see" word. Is there a way to read like variables ?


Solution

  • While the std::stringstream solution works, I would suggest using simple string concatenation, through std::string's operator+:

    #include <gtkmm.h>
    
    int main(int argc, char **argv)
    {
        auto app = Gtk::Application::create(argc, argv, "so.question.q63886899");
        
        Gtk::Window w;
        w.show_all();
    
        {
            // Unformatted messages:
            std::string primaryMessage = "Some message...";
            std::string secondaryMessage = "Some more message details...";
    
            Gtk::MessageDialog dialog(w, "Message dialog", true, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO);
            dialog.set_title("Title");
    
            // Add pango markup tags through string concatenation:
            dialog.set_message("<span weight='bold'>" + primaryMessage + "</span>", true);
            dialog.set_secondary_text("<b>" + secondaryMessage + "</b>", true);
    
            dialog.run();
        }
    
        return app->run(w);
    }
    

    With this solution, no need to introduce an extra type.