使用带有gtkmm的Window :: set_title时窗口标题截断

时间:2011-02-20 22:42:09

标签: c++ cross-platform gtk truncate gtkmm

我正在尝试重命名应用程序主窗口的标题,但在尝试时,名称会被截断。我试着看看它是否是转换问题,但我真的无法找到为什么会发生这种情况。试试这个小程序。 点击取消以查看标题栏中的默认应用程序名称,但是如果您选择一个文件,它应该将文件的第一行显示为标题,而是截断它...在字符串结束之前,trucation始终是3个字符,并添加了三个点“......”。

我做错了什么?或者它是我的gtkmm版本或其他什么的错误?我用的是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;
}

2 个答案:

答案 0 :(得分:3)

在这里工作正常。

  • 也许您的文件不是UTF-8编码?
  • 如果标题长于标题栏中的空格,标题会被截断是正常的吗?

答案 1 :(得分:0)

好的,我终于找到了解决方案。我在这里写,以防其他人得到同样的问题。我不知道这是一个bug还是什么,但似乎GTK将'\ n'之前的三个最后一个字符替换为'...'。换句话说,用于重命名窗口的字符串不得包含任何'\ n',否则set_title将不显示全名(它将在'\ n'之前停止三个字符)。

因此,在我的情况下,由于我使用'getline()',我只是从字符串的末尾删除'\ n'。

app_name.erase(app_name.end()-1);
注意我必须使用'' std :: string'而不是'Gtk :: ustring',因为它不处理'end()'函数的'operator-'。