c++ - 将自定义作者类别添加到 Gtk::AboutDialog 类

标签 c++ gtkmm

我想知道是否有办法通过 gtkmm 在 Gtk::AboutDialog 类中设置自定义作者类别。我知道有以下几种方法:

  • set_artists()
  • 设置作者()
  • set_documenters()
  • set_translator_credits()

但我想添加一个自定义类别。现在我有一个接受一堆插件的程序,所以在启动时扫描插件时,我想在你点击显示所有插件作者姓名的致谢名单后,在关于屏幕上填充一个“插件”页面(删除当然是重复)。逻辑已经存在,但将他们添加到他们肯定不属于的艺术家或记录者类别看起来很奇怪。

除了滚动我自己的类别之外,是否有添加新类别的简单方法?

最佳答案

好问题!在 GTK 3 中,这相当容易。您必须对“关于”对话框的内部子项进行一些操作,这在未来的版本中可能会发生变化,因此请注意!

我已经在 Vala 中编写了一个简单的示例,它可以执行您想要的操作。这对我来说更快,因为我几乎从不使用 Gtkmm。不过翻译应该不难。

using Gtk;

int main(string[] args)
{
    Gtk.init(ref args);

    var dialog = new AboutDialog();

    // Fetch internal children, using trickery
    var box = dialog.get_child() as Box;
    Box? box2 = null;
    ButtonBox? buttons = null;
    Notebook? notebook = null;
    box.forall( (child) => {
        if(child.name == "GtkBox")
            box2 = child as Box;
        else if(child.name == "GtkButtonBox")
            buttons = child as ButtonBox;
    });
    box2.forall( (child) => { 
        if(child.name == "GtkNotebook")
            notebook = child as Notebook;
    });

    // Add a new page to the notebook (put whatever widgets you want in it)
    var plugin_page_index = notebook.append_page(new Label("Plugin 1\nPlugin 2"),
        new Label("Plugins"));

    // Add a button that toggles whether the page is visible
    var button = new ToggleButton.with_label("Plugins");
    button.clicked.connect( (button) => {
        notebook.page = (button as ToggleButton).active? plugin_page_index : 0;
    });
    buttons.pack_start(button);
    buttons.set_child_secondary(button, true);

    // Set some other parameters
    dialog.program_name = "Test Program";
    dialog.logo_icon_name = Gtk.Stock.ABOUT;
    dialog.version = "0.1";
    dialog.authors = { "Author 1", "Author 2" };
    dialog.show_all(); // otherwise the new widgets are invisible
    dialog.run();

    return 0;
}

在 GTK 2 中,这要困难得多,尽管可能并非不可能。您必须使用在正常处理程序之后运行的处理程序连接到 Credits 按钮的 clicked 信号,然后获取顶层窗口列表并查找打开的新窗口。然后您可以将另一个页面添加到该窗口的 GtkNotebook

我建议做一些不同的事情:在操作区域添加一个插件按钮,它会打开自己的窗口。这样你就不必去搞乱内部 child 了。这是另一个 Vala 示例:

using Gtk;

class PluginsAboutDialog : AboutDialog {

    private Dialog _plugins_window;
    private Widget _plugins_widget;

    public Widget plugins_widget { get {
        return _plugins_widget;
    }
    set {
        var content_area = _plugins_window.get_content_area() as VBox;
        if(_plugins_widget != null)
            content_area.remove(_plugins_widget);
        _plugins_widget = value;
        content_area.pack_start(value);
    }}

    public PluginsAboutDialog() {
        _plugins_window = new Dialog();
        _plugins_window.title = "Plugins";
        _plugins_window.add_buttons(Stock.CLOSE, ResponseType.CLOSE, null);
        _plugins_window.response.connect((widget, response) => { widget.hide(); });

        var buttons = get_action_area() as HButtonBox;

        // Add a button that opens a plugins window
        var button = new Button.with_label("Plugins");
        button.clicked.connect( (button) => {
            _plugins_window.show_all();
            _plugins_window.run();
        });
        button.show();
        buttons.pack_start(button);
        buttons.set_child_secondary(button, true);
    }

    public static int main(string[] args) {

        Gtk.init(ref args);
        var dialog = new PluginsAboutDialog();

        // Make a widget for the plugins window
        var can_be_any_widget = new Label("Plugin 1\nPlugin 2");
        dialog.plugins_widget = can_be_any_widget;

        // Set some other parameters
        dialog.program_name = "Test Program";
        dialog.logo_icon_name = Gtk.Stock.ABOUT;
        dialog.version = "0.1";
        dialog.authors = { "Author 1", "Author 2" };
        dialog.run();

        return 0;
    }
}

关于c++ - 将自定义作者类别添加到 Gtk::AboutDialog 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7562429/

相关文章:

c++ - std::mutex 如何在不同的线程中解锁?

c++ - 我的 C++ 程序应该支持 IA64 还是只支持 x64?

PHP exec 和 C++ 程序无限运行

c++ - 是否可以在 C++ 中使用作用域类型作为宏名称?

c++ - 如何在textbuffer gtkmmTextView中写字母ñ?

c++ - 创建一个独立的 gtkmm 对话框

c++ - 使用已删除的指针地址

c++ - GTKMM::在 GTk::Drawing 区域中使用单个 Gdk::PixBuf 绘制多个图像

c++ - 如何等待 Glib::Dispatcher 的连接函数完成?

c++ - Gtk 3.10 小部件是否向后兼容?