开发者

Adding a custom Authors category to Gtk::AboutDialog class

I was wondering if there was a way to set a custom Authors category in a Gtk::Ab开发者_如何学GooutDialog class via gtkmm. I know there are the following methods:

  • set_artists()
  • set_authors()
  • set_documenters()
  • set_translator_credits()

But I wanted to add a custom category. Right now I have a program that accepts a bunch of plugins, so on startup when it scans for plugins I would like to populate a "Plugins" page on the about screen once you click credits that shows all of the plugin authors' names (removing duplicates of course). The logic is already there, but it looks quite odd adding them to the artists or documenters categories where they certainly do not belong.

Is there an easy way to add a new category besides rolling my own?


Nice question! In GTK 3, this is fairly easy. You have to do some manipulation of the About dialog's internal children, which may change in future releases, so be warned!

I've written a quick-n-dirty example in Vala that does what you want. That was faster for me because I almost never use Gtkmm. It shouldn't be too hard to translate though.

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;
}

In GTK 2, this is much more difficult, although probably not impossible. You have to connect to the Credits button's clicked signal, with a handler that runs after the normal handler, and then get a list of toplevel windows and look for the new window that opens. Then you can add another page to that window's GtkNotebook.

I would suggest doing it a little differently: add a Plugins button to the action area which opens its own window. Then you don't have to go messing around with internal children. Here's another Vala sample:

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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜