396. How to change the font of menus and menuitems - Part 2?

Problem

You have set up and changed the fonts of the menuitems in Part 1.

However, as noted in Part 1, the fonts of the radio menuitems are not changed as shown below:

We will fix this in this Part 2 as shown below:

How to change the font of menus and menuitems - Part 2?


Solution

  • For standard menuitems, we can get the pointer to the underlyiing GtkLabel using $menu_item->child.
  • For radio menuitems, we get the poitner to the label corresponding to each radio menuitem with $radio[$i]->child.
  • Once we have the label to the radio menuitems, we set the font as usual with GtkWidget::modify_font().

Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
40   
41   
42   
43   
44   
45   
47   
48   
49   
50   
51   
53   
54   
55   
56   
57   
58   
59   
60   
61   
62   
63   
67   
68   
69   
70   
71   
72   
73   
74   
75   
76   
77   
78   
80   
81   
82   
83   
84   
86   
92   
93   
94   
95   
99   
100   
104   
105   
106   
107   
108   
109   
110   
111   
112   
113   
114   
115   
116   
117   
119   
120   
121   
122   
123   
124   
125   
126   
127   
128   
129   
130   
131   
134   
136   
137   
138   
<?php
$window = new GtkWindow();
$window->set_title($argv[0]);
$window->set_size_request(400, 150);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());
$accel_group = new GtkAccelGroup();
$window->add_accel_group($accel_group);

// define menu definition
$menu_definition = array(
    '_File' => array('_New|N', '_Open|O', '_Close|C', '<hr>',
                    '_Save|S', 'Save _As','<hr>', '_Quit'),
    '_Edit' => array('Cut|X', 'Copy|C', '_Paste|V', '<hr>',
                    'Select All|A', '<hr>', '_Undo|Z','_Redo|Y'),
    '_Test' => array('Test_1|1', 'Test_2|2', 'Test_3|3', '<hr>',
                array('Selection 1', 'Selection 2', 'Selection 3'),
                '<hr>', 'Test_4|4')
);
setup_menu($vbox, $menu_definition);

// display title
$title = new GtkLabel("   Change the font and fontsize\n".
"of menu and menuitems - Part 2");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$vbox->pack_start($title);
$vbox->pack_start(new GtkLabel(''));

$window->show_all();
Gtk::main();

// setup menu
function setup_menu($vbox, $menus) {
    global $accel_group;
    $menubar = new GtkMenuBar();
    $vbox->pack_start($menubar, 0, 0);
    foreach($menus as $toplevel => $sublevels) {
        $menubar->append($top_menu = new GtkMenuItem($toplevel));
        $label = $top_menu->child;
        set_menuitem_font($label);
        $menu = new GtkMenu();
        $top_menu->set_submenu($menu);
        foreach($sublevels as $submenu) {
            if (strpos("$submenu", '|') === false) {
                $accel_key = '';
            } else {
                list($submenu, $accel_key) = explode('|', $submenu);
            }

            if (is_array($submenu)) { // set up radio menus
                $i=0;
                $radio[0] = null;
                foreach($submenu as $radio_item) {
                    $radio[$i] = new GtkRadioMenuItem($radio[0], $radio_item);
                    $radio[$i]->connect('toggled', "on_toggle");
                    $menu->append($radio[$i]);

                    $label = $radio[$i]->child; // note 1
                    set_menuitem_font($label); // note 2

                    ++$i;
                }
                $radio[0]->set_active(1); // select the first item

            } else {
                if ($submenu=='<hr>') {
                    $menu->append(new GtkSeparatorMenuItem());
                } else {
                    $submenu2 = str_replace('_', '', $submenu);
                    $submenu2 = str_replace(' ', '_', $submenu2);
                    $stock_image_name = 'Gtk::STOCK_'.strtoupper($submenu2);
                    if (defined($stock_image_name)) {
                        $menu_item = new GtkImageMenuItem(
                                        constant($stock_image_name));
                    } else {
                        $menu_item = new GtkMenuItem($submenu);
                    }
                    if ($accel_key!='') {
                        $menu_item->add_accelerator("activate", $accel_group,
                        ord($accel_key), Gdk::CONTROL_MASK, 1);
                    } 

                    $label = $menu_item->child;
                    set_menuitem_font($label);

                    $menu->append($menu_item);
                    $menu_item->connect('activate', 'on_menu_select');
                }
            }
        }
    }
}

// process radio menu selection
function on_toggle($radio) {
    $label = $radio->child->get_label();
    $active = $radio->get_active();
    echo("radio menu selected: $label\n");
}

// process menu item selection
function on_menu_select($menu_item) {
    $item = $menu_item->child->get_label();
    echo "menu selected: $item\n";
    if ($item=='_Quit') Gtk::main_quit();
}

// sets the label of a menuitem
function set_menuitem_font($label) {
    $label_text = $label->get_text();
    $label->modify_font(new PangoFontDescription("Times New Roman 12"));
}

?>

Output

As shown above.
 

Explanation

We make use of the code from How to change the font of menus and menuitems - Part 2? to set up the standard menu and menuitems.

What's new here:

  1. Get the pointer to the GtkLabel of each radio menuitem.
  2. Change the font.

Related Links

Add comment


Security code
Refresh