Blame platform-demos/C/samples/toolbar_builder.py

Packit 1470ea
from gi.repository import Gtk
Packit 1470ea
from gi.repository import Gdk
Packit 1470ea
from gi.repository import Gio
Packit 1470ea
import sys
Packit 1470ea
Packit 1470ea
Packit 1470ea
class MyWindow(Gtk.ApplicationWindow):
Packit 1470ea
Packit 1470ea
    def __init__(self, app):
Packit 1470ea
        Gtk.Window.__init__(self, title="Toolbar Example", application=app)
Packit 1470ea
        self.set_default_size(400, 200)
Packit 1470ea
Packit 1470ea
        # a grid to attach the toolbar (see below)
Packit 1470ea
        grid = Gtk.Grid()
Packit 1470ea
        self.add(grid)
Packit 1470ea
        # we have to show the grid (and therefore the toolbar) with show(),
Packit 1470ea
        # as show_all() would show also the buttons in the toolbar that we want to
Packit 1470ea
        # be hidden (such as the leave_fullscreen button)
Packit 1470ea
        grid.show()
Packit 1470ea
Packit 1470ea
        # a builder to add the UI designed with Glade to the grid:
Packit 1470ea
        builder = Gtk.Builder()
Packit 1470ea
        # get the file (if it is there)
Packit 1470ea
        try:
Packit 1470ea
            builder.add_from_file("toolbar_builder.ui")
Packit 1470ea
        except:
Packit 1470ea
            print("file not found")
Packit 1470ea
            sys.exit()
Packit 1470ea
        # and attach it to the grid
Packit 1470ea
        grid.attach(builder.get_object("toolbar"), 0, 0, 1, 1)
Packit 1470ea
Packit 1470ea
        # two buttons that will be used later in a method
Packit 1470ea
        self.fullscreen_button = builder.get_object("fullscreen_button")
Packit 1470ea
        self.leave_fullscreen_button = builder.get_object(
Packit 1470ea
            "leave_fullscreen_button")
Packit 1470ea
Packit 1470ea
        # create the actions that control the window, connect their signal to a
Packit 1470ea
        # callback method (see below), add the action to the window:
Packit 1470ea
Packit 1470ea
        # undo
Packit 1470ea
        undo_action = Gio.SimpleAction.new("undo", None)
Packit 1470ea
        undo_action.connect("activate", self.undo_callback)
Packit 1470ea
        self.add_action(undo_action)
Packit 1470ea
Packit 1470ea
        # and fullscreen
Packit 1470ea
        fullscreen_action = Gio.SimpleAction.new("fullscreen", None)
Packit 1470ea
        fullscreen_action.connect("activate", self.fullscreen_callback)
Packit 1470ea
        self.add_action(fullscreen_action)
Packit 1470ea
Packit 1470ea
    # callback for undo
Packit 1470ea
    def undo_callback(self, action, parameter):
Packit 1470ea
        print("You clicked \"Undo\".")
Packit 1470ea
Packit 1470ea
    # callback for fullscreen
Packit 1470ea
    def fullscreen_callback(self, action, parameter):
Packit 1470ea
        # check if the state is the same as Gdk.WindowState.FULLSCREEN, which
Packit 1470ea
        # is a bit flag
Packit 1470ea
        is_fullscreen = self.get_window().get_state(
Packit 1470ea
        ) & Gdk.WindowState.FULLSCREEN != 0
Packit 1470ea
        if is_fullscreen:
Packit 1470ea
            self.unfullscreen()
Packit 1470ea
            self.leave_fullscreen_button.hide()
Packit 1470ea
            self.fullscreen_button.show()
Packit 1470ea
        else:
Packit 1470ea
            self.fullscreen()
Packit 1470ea
            self.fullscreen_button.hide()
Packit 1470ea
            self.leave_fullscreen_button.show()
Packit 1470ea
Packit 1470ea
Packit 1470ea
class MyApplication(Gtk.Application):
Packit 1470ea
Packit 1470ea
    def __init__(self):
Packit 1470ea
        Gtk.Application.__init__(self)
Packit 1470ea
Packit 1470ea
    def do_activate(self):
Packit 1470ea
        win = MyWindow(self)
Packit 1470ea
        # show the window - with show() not show_all() because that would show also
Packit 1470ea
        # the leave_fullscreen button
Packit 1470ea
        win.show()
Packit 1470ea
Packit 1470ea
    def do_startup(self):
Packit 1470ea
        Gtk.Application.do_startup(self)
Packit 1470ea
Packit 1470ea
        # actions that control the application: create, connect their signal to a
Packit 1470ea
        # callback method (see below), add the action to the application
Packit 1470ea
Packit 1470ea
        # new
Packit 1470ea
        new_action = Gio.SimpleAction.new("new", None)
Packit 1470ea
        new_action.connect("activate", self.new_callback)
Packit 1470ea
        app.add_action(new_action)
Packit 1470ea
Packit 1470ea
        # open
Packit 1470ea
        open_action = Gio.SimpleAction.new("open", None)
Packit 1470ea
        open_action.connect("activate", self.open_callback)
Packit 1470ea
        app.add_action(open_action)
Packit 1470ea
Packit 1470ea
    # callback for new
Packit 1470ea
    def new_callback(self, action, parameter):
Packit 1470ea
        print("You clicked \"New\".")
Packit 1470ea
Packit 1470ea
    # callback for open
Packit 1470ea
    def open_callback(self, action, parameter):
Packit 1470ea
        print("You clicked \"Open\".")
Packit 1470ea
Packit 1470ea
app = MyApplication()
Packit 1470ea
exit_status = app.run(sys.argv)
Packit 1470ea
sys.exit(exit_status)