Search code examples
c#monodevelopgtk#

GTKSharp, Increasing the text size of a Menu Bar using Mondevelop,Stetic GUI Designer


I am wondering if anyone can help me. I am trying to figure out, how to increase the text inside a menu bar using mono develop. I can increase the size of text in labels and such using

public static FontDescription Font(string Family, int Size, Pango.Style Sty = Pango.Style.Normal)
{
    var F = new FontDescription
    {
        Family = Family,
        Size = Convert.ToInt32(Size * Pango.Scale.PangoScale),
        Style = Sty
    };
    return F;
}

And then:

 var test = Font("Verdana", 24);
 label1.ModifyFont(test);

But when I do this it wont work

MainMenuBar.ModifyFont(test);

Cant Increase the size of the text with the menu bar


Solution

  • You can use Widget.ModifyFont(Pango.FontDescription fd). This method is available in all classes, since it derives from Widget.

    However, you cannot change the font of a MenuBar and expect all child MenuItem's fonts. This does not even work for a MenuItem: you have to change the Label inside it, normally accessible by MenuItem.Child.

    I have prepared a pair of functions that do the trick:

    public static void ChangeContainerFont(Gtk.Container container, string fontDesc)
    {
        ChangeWidgetFont( container, fontDesc );
    
        foreach(Gtk.Widget subw in container.Children) {
            ChangeWidgetFont( subw, fontDesc );
    
            if ( subw is Gtk.MenuItem menuItem ) {
                var subMenu = menuItem.Submenu;
                ChangeContainerFont( menuItem, fontDesc );
    
                if ( subMenu is Gtk.Container subContainer ) {
                    ChangeContainerFont( subContainer, fontDesc );
                } else {
                    if ( subMenu != null ) {
                        ChangeWidgetFont( subMenu, fontDesc );
                    }
                }
            } else
            if ( subw is Gtk.Container subContainer ) {
                ChangeContainerFont( subContainer, fontDesc );
            }
        }
    }
    
    public static void ChangeWidgetFont(Gtk.Widget w, string fontDesc)
    {
        w.ModifyFont( Pango.FontDescription.FromString( fontDesc ) );
    }
    

    You can call ChangeContainerFont(c, s) programmatically after the UI has been built (give your menu bar an intuitive name such as menuBar):

    ChangeContainerFont( menuBar, "Times 22" );
    

    This will run through all MenuItem's inside, stopping once it reaches a wiget which is not a container. For example, MenuBar or MenuItem are containers (the former contains MenuItem's, the latter contains at least a Label), while a Label is not.

    Hope this helps.