File: ExampleToolbar.java

package info (click to toggle)
java-gnome 4.1.3-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 9,840 kB
  • sloc: java: 27,002; ansic: 4,517; perl: 1,651; python: 1,187; makefile: 136
file content (187 lines) | stat: -rw-r--r-- 6,018 bytes parent folder | download | duplicates (5)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * java-gnome, a UI library for writing GTK and GNOME programs from Java!
 *
 * Copyright © 2007-2011 Operational Dynamics Consulting, Pty Ltd
 * Copyright © 2007      Vreixo Formoso
 *
 * The code in this file, and the program it is a part of, is made available
 * to you by its authors as open source software: you can redistribute it
 * and/or modify it under the terms of the GNU General Public License version
 * 2 ("GPL") as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GPL for more details.
 *
 * You should have received a copy of the GPL along with this program. If not,
 * see http://www.gnu.org/licenses/. The authors of this program may be
 * contacted through http://java-gnome.sourceforge.net/.
 */
package menu;

import org.gnome.gdk.Event;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Label;
import org.gnome.gtk.Menu;
import org.gnome.gtk.MenuItem;
import org.gnome.gtk.MenuToolButton;
import org.gnome.gtk.SeparatorToolItem;
import org.gnome.gtk.Stock;
import org.gnome.gtk.ToggleToolButton;
import org.gnome.gtk.ToolButton;
import org.gnome.gtk.ToolItem;
import org.gnome.gtk.Toolbar;
import org.gnome.gtk.VBox;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;

/**
 * How to use {@link Toolbar} and related Widgets.
 * 
 * @author Vreixo Formoso
 */
public class ExampleToolbar
{
    public ExampleToolbar() {
        final Window w;
        final VBox x;
        final Label l;
        final Toolbar toolbar;
        final ToolButton buttonNew;
        final MenuToolButton mtb;
        final Menu openMenu;
        final ToolItem item;
        final ToggleToolButton boldButton, italicButton;

        w = new Window();
        w.setDefaultSize(400, 150);

        x = new VBox(false, 3);
        w.add(x);

        /*
         * Create a Toolbar, and add it at the beginning of the Window. Note
         * that usually you also want a MenuBar, that is located at the top of
         * the Window, with the Toolbar just under it.
         */
        toolbar = new Toolbar();
        x.packStart(toolbar, false, false, 0);

        l = new Label("Select an action in a menu");
        x.add(l);

        /*
         * Usually you will want to add several ToolButtons to your Toolbar.
         * You could create ToolButtons from a Stock id. That way, both Icon
         * and Label are automatically set.
         */
        buttonNew = new ToolButton(Stock.NEW);
        toolbar.add(buttonNew);

        /*
         * You can respond to user clicks in the ToolButton connecting to the
         * ToolButton.Clicked signal.
         */
        buttonNew.connect(new ToolButton.Clicked() {
            public void onClicked(ToolButton source) {
                l.setLabel("You have clicked NEW ToolButton");
            }
        });

        /*
         * Sometimes you need a ToolButton that also has a dropdown Menu, to
         * allow the user select alternative actions. You can do that with a
         * MenuToolButton.
         */
        mtb = new MenuToolButton(Stock.OPEN);
        toolbar.add(mtb);

        /*
         * You can add your Menu to this kind of ToolButtons
         */
        openMenu = new Menu();
        openMenu.append(new MenuItem("File _A", new MenuItem.Activate() {
            public void onActivate(MenuItem source) {
                l.setLabel("You have selected File A in the Menu");
            }
        }));
        openMenu.append(new MenuItem("File _B", new MenuItem.Activate() {
            public void onActivate(MenuItem source) {
                l.setLabel("You have selected File B in the Menu");
            }
        }));
        openMenu.showAll();
        mtb.setMenu(openMenu);

        /*
         * You also can respond to user clicks in the MenuToolButton itself
         */
        mtb.connect(new ToolButton.Clicked() {
            public void onClicked(ToolButton source) {
                l.setLabel("You have clicked the Open MenuToolButton");
            }
        });

        /*
         * To group together related items, you put a SeparatorToolItem
         * between then
         */
        toolbar.add(new SeparatorToolItem());

        /*
         * ToggleToolButtons can be used to let users enable/disable some
         * options.
         */
        boldButton = new ToggleToolButton(Stock.BOLD);
        italicButton = new ToggleToolButton(Stock.ITALIC);

        toolbar.add(boldButton);
        toolbar.add(italicButton);

        /* In some cases you want the Button in a active state */
        boldButton.setActive(true);

        /*
         * the changes in the active state can be tracked by connecting to the
         * ToggleToolButton.Toggled signal.
         */
        boldButton.connect(new ToggleToolButton.Toggled() {
            public void onToggled(ToggleToolButton source) {
                /* You can get the state with the getActive() method */
                if (source.getActive()) {
                    l.setLabel("Bold has been enabled");
                } else {
                    l.setLabel("Bold has been disabled");
                }
            }
        });

        toolbar.add(new SeparatorToolItem());

        /*
         * Finally, you can add another kind of Widgets to your Toolbar, after
         * adding them to a ToolItem
         */
        item = new ToolItem();

        // TODO replace the Label with a more useful Widget
        item.add(new Label("This is a label"));
        toolbar.add(item);

        w.connect(new Window.DeleteEvent() {
            public boolean onDeleteEvent(Widget source, Event event) {
                Gtk.mainQuit();
                return false;
            }
        });
        w.showAll();
    }

    public static void main(String[] args) {
        Gtk.init(args);

        new ExampleToolbar();

        Gtk.main();
    }
}