1from gi.repository import Gtk
2from gi.repository import Gdk
3from gi.repository import Gio
4import sys
5
6
7class MyWindow(Gtk.ApplicationWindow):
8
9    def __init__(self, app):
10        Gtk.Window.__init__(self, title="Toolbar Example", application=app)
11        self.set_default_size(400, 200)
12
13        # a grid to attach the toolbar
14        grid = Gtk.Grid()
15
16        # a toolbar created in the method create_toolbar (see below)
17        toolbar = self.create_toolbar()
18        # with extra horizontal space
19        toolbar.set_hexpand(True)
20        # show the toolbar
21        toolbar.show()
22
23        # attach the toolbar to the grid
24        grid.attach(toolbar, 0, 0, 1, 1)
25
26        # add the grid to the window
27        self.add(grid)
28
29        # create the actions that control the window and connect their signal to a
30        # callback method (see below):
31
32        # undo
33        undo_action = Gio.SimpleAction.new("undo", None)
34        undo_action.connect("activate", self.undo_callback)
35        self.add_action(undo_action)
36
37        # fullscreen
38        fullscreen_action = Gio.SimpleAction.new("fullscreen", None)
39        fullscreen_action.connect("activate", self.fullscreen_callback)
40        self.add_action(fullscreen_action)
41
42    # a method to create the toolbar
43    def create_toolbar(self):
44        # a toolbar
45        toolbar = Gtk.Toolbar()
46
47        # which is the primary toolbar of the application
48        toolbar.get_style_context().add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR)
49
50        # create a button for the "new" action, with a stock image
51        new_button = Gtk.ToolButton.new_from_stock(Gtk.STOCK_NEW)
52        # label is shown
53        new_button.set_is_important(True)
54        # insert the button at position in the toolbar
55        toolbar.insert(new_button, 0)
56        # show the button
57        new_button.show()
58        # set the name of the action associated with the button.
59        # The action controls the application (app)
60        new_button.set_action_name("app.new")
61
62        # button for the "open" action
63        open_button = Gtk.ToolButton.new_from_stock(Gtk.STOCK_OPEN)
64        open_button.set_is_important(True)
65        toolbar.insert(open_button, 1)
66        open_button.show()
67        open_button.set_action_name("app.open")
68
69        # button for the "undo" action
70        undo_button = Gtk.ToolButton.new_from_stock(Gtk.STOCK_UNDO)
71        undo_button.set_is_important(True)
72        toolbar.insert(undo_button, 2)
73        undo_button.show()
74        undo_button.set_action_name("win.undo")
75
76        # button for the "fullscreen/leave fullscreen" action
77        self.fullscreen_button = Gtk.ToolButton.new_from_stock(
78            Gtk.STOCK_FULLSCREEN)
79        self.fullscreen_button.set_is_important(True)
80        toolbar.insert(self.fullscreen_button, 3)
81        self.fullscreen_button.set_action_name("win.fullscreen")
82
83        # return the complete toolbar
84        return toolbar
85
86    # callback method for undo
87    def undo_callback(self, action, parameter):
88        print("You clicked \"Undo\".")
89
90    # callback method for fullscreen / leave fullscreen
91    def fullscreen_callback(self, action, parameter):
92        # check if the state is the same as Gdk.WindowState.FULLSCREEN, which
93        # is a bit flag
94        is_fullscreen = self.get_window().get_state(
95        ) & Gdk.WindowState.FULLSCREEN != 0
96        if not is_fullscreen:
97            self.fullscreen_button.set_stock_id(Gtk.STOCK_LEAVE_FULLSCREEN)
98            self.fullscreen()
99        else:
100            self.fullscreen_button.set_stock_id(Gtk.STOCK_FULLSCREEN)
101            self.unfullscreen()
102
103
104class MyApplication(Gtk.Application):
105
106    def __init__(self):
107        Gtk.Application.__init__(self)
108
109    def do_activate(self):
110        win = MyWindow(self)
111        win.show_all()
112
113    def do_startup(self):
114        Gtk.Application.do_startup(self)
115
116        # create the actions that control the window and connect their signal to a
117        # callback method (see below):
118
119        # new
120        new_action = Gio.SimpleAction.new("new", None)
121        new_action.connect("activate", self.new_callback)
122        app.add_action(new_action)
123
124        # open
125        open_action = Gio.SimpleAction.new("open", None)
126        open_action.connect("activate", self.open_callback)
127        app.add_action(open_action)
128
129    # callback method for new
130    def new_callback(self, action, parameter):
131        print("You clicked \"New\".")
132
133    # callback method for open
134    def open_callback(self, action, parameter):
135        print("You clicked \"Open\".")
136
137app = MyApplication()
138exit_status = app.run(sys.argv)
139sys.exit(exit_status)
140