简体   繁体   中英

Window title truncation when using Window::set_title with gtkmm

I am trying to rename the title of my application's main window, but when trying so, the name get truncated. I tried to see if it was a conversion problem but I realy can't find why this happen. Try this little program. Hit cancel to see the default application name in the title bar, but if you choose a file, it should display the first line of the file as title but instead truncated it... The trucation is always 3 caracters before the end of the string, and three dots "..." is added.

What am I doing wrong?? or is it a bug with my gtkmm version or something? I use gtkmm-2.4 Thanks in advance.

#include <iostream>
#include <gtkmm.h>

using namespace std;
using namespace Gtk;
using namespace Glib;

class AppWindow : public Window {
 public:
    AppWindow();
 protected:
    void onMenuFileOpen();
 private:
    ustring app_name;
    void OpenScript(const ustring sScriptFile);
};

AppWindow::AppWindow() {
    app_name = "default app_name, very long name, with !!^spectal caractères à afficher, and there is no name truncation";
    //set_title(app_name);  
    set_default_size(600, 600);

    onMenuFileOpen();

}

void AppWindow::onMenuFileOpen() {
    FileChooserDialog dialog("Choose a file", FILE_CHOOSER_ACTION_OPEN);
    dialog.set_transient_for(*this);

    //Add response buttons the the dialog:
    dialog.add_button(Stock::CANCEL, RESPONSE_CANCEL);
    dialog.add_button(Stock::OPEN, RESPONSE_OK);

    //Plain text filter
    FileFilter filter_text;
    filter_text.set_name("plain text");
    filter_text.add_mime_type("text/plain");
    dialog.add_filter(filter_text);

    //Show the dialog and wait for a user response:
    if(dialog.run() == RESPONSE_OK) {
        OpenScript(dialog.get_filename());
    }
    //HERE, I RENAME THE WINDOW
    set_title(app_name);
    cout << app_name << endl;
}

void AppWindow::OpenScript(const ustring sScriptFile) {
    RefPtr<IOChannel> file = IOChannel::create_from_file(sScriptFile,"r");
    IOStatus status;
    ustring one_line;

    if(file->get_flags() & IO_FLAG_IS_READABLE) {
        status = file->read_line(one_line);
        app_name=one_line;
    }
    file->close();
}

int main(int argc, char *argv[]) {
    Main kit(argc, argv);

    AppWindow window;
    //Shows the window and returns when it is closed.
    Main::run(window);

    return 0;
}

Works fine here.

  • maybe your file is not in UTF-8 encoding?
  • it's normal for the title to be truncated if it's longer than the space in the titlebar?

OK I finally found the solution myself. I write it here in case someone else gets the same issue. I don't know if it is a bug or what, but seems like GTK substitutes the three last characters before a '\\n' to '...'. In other words, the string used to rename the window must not contain any '\\n' or else set_title will not show the full name (it will stop three characters before '\\n').

Therefore, In my case, since I used 'getline()', I simply removed the '\\n' from the end of the string.

app_name.erase(app_name.end()-1);
Note that I had to use a 'std::string' instead of a 'Gtk::ustring' since it doesn't handle the 'operator-' on 'end()' function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM