简体   繁体   English

在gtkmm中使用Window :: set_title时窗口标题截断

[英]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. 点击取消,在标题栏中看到默认的应用程序名称,但是如果选择一个文件,它将显示文件的第一行作为标题,但是将其截断...字符串的末尾总是3个字符,并添加三个点“ ...”。

What am I doing wrong?? 我究竟做错了什么?? or is it a bug with my gtkmm version or something? 还是我的gtkmm版本有问题? I use gtkmm-2.4 Thanks in advance. 我使用gtkmm-2.4预先感谢。

#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? 也许您的文件不是UTF-8编码的?
  • 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 '...'. 我不知道这是一个错误还是什么,但是GTK似乎将'\\ n'之前的最后三个字符替换为'...'。 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'). 换句话说,用于重命名窗口的字符串不能包含任何“ \\ n”,否则set_title将不会显示全名(它将在“ \\ n”之前停止三个字符)。

Therefore, In my case, since I used 'getline()', I simply removed the '\\n' from the end of the string. 因此,就我而言,由于我使用了“ getline()”,因此我只是从字符串末尾删除了“ \\ n”。

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. 请注意,我必须使用'std :: string'而不是'Gtk :: ustring',因为它不能处理'end()'函数上的'operator-'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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