File: DemoActions.d

package info (click to toggle)
gtk-d 3.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 20,152 kB
  • sloc: javascript: 565; sh: 71; makefile: 25
file content (141 lines) | stat: -rw-r--r-- 5,413 bytes parent folder | download | duplicates (4)
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
/*
 * This file is part of gtkD.
 *
 * gtkD is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * gtkD 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
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with gtkD; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 */

/*****************************************************************************

Authors: Gerald Nunn <gerald.b.nunn@gmail.com>

This is a demo of using GIO actions with GTK PopOver. It demonstrates
stateless and stateful actions displayed as normal, checkbox and radiobutton
menu items

See here for information about GIO actions: https://wiki.gnome.org/HowDoI/GAction

*****************************************************************************/

import std.stdio;

import gtk.Application : Application;
import gio.Application : GioApplication = Application;
import gtk.ApplicationWindow : ApplicationWindow;
import gtkc.giotypes : GApplicationFlags;

import gdk.Event;

import gio.Menu;
import gio.MenuItem;
import gio.SimpleAction;

import glib.Variant;
import glib.VariantType;

import gtk.HeaderBar;
import gtk.Image;
import gtk.MenuButton;
import gtk.MessageDialog;
import gtk.Popover;
import gtk.Widget;

class MainWindow : ApplicationWindow {

    this(Application application) {
        super(application);
        initUI();
        showAll();
    }

    /**
     * Create and initialize the GTK widgets
     */
    private void initUI() {
        this.setSizeRequest(1024, 640);

        //HeaderBar
        HeaderBar hb = new HeaderBar();
        hb.setShowCloseButton(true);
        hb.setTitle("Actions Demo, Click the menu button >>>");
        this.setTitlebar(hb);

        //Normal stateless action
        SimpleAction saNormal = new SimpleAction("normal", null);
        saNormal.addOnActivate(delegate(Variant, SimpleAction) {
            MessageDialog dialog = new MessageDialog(this, DialogFlags.MODAL, MessageType.ERROR, ButtonsType.OK, "Normal action clicked!", null);
            scope (exit) {
                dialog.destroy();
            }
            dialog.run();
        });
        addAction(saNormal);

        //Stateful action which generates a Checkbox in popover
        SimpleAction saCheck = new SimpleAction("check", null, new Variant(false));
        saCheck.addOnActivate(delegate(Variant value, SimpleAction sa) { bool newState = !sa.getState().getBoolean(); sa.setState(new Variant(newState)); });
        addAction(saCheck);

        // Stateful action which uses targets. A target is essentially
        // one of a set of actions that an action can hold as state. When
        // using this type of action there is only one action for it however
        // when creating the menu model there is one menu item for each target.
        //
        // When using this type of action, you need to create the SimpleAction telling
        // it what type of variant the state represents. The parameter "new VariantType("s")" 
        // says that it is a string in this example. See GtkD documentation on VariantType
        // for more information.
        SimpleAction saRadio = new SimpleAction("radio", new VariantType("s"), new Variant("left"));
        saRadio.addOnActivate(delegate(Variant value, SimpleAction sa) {
            //The selected target is passed as the value.
            sa.setState(value);
        });
        addAction(saRadio);

        //Menu Model
        Menu model = new Menu();
        // Note that action names consist of a prefix and an id which when 
        // combined make a detailed name. The prefix comes from the container (ActionMap)
        // that the action is inserted against. GTK Application has a prefix of "app"
        // while GTK ApplicationWindow has a prefix of "win". You can create your own
        // prefixes by creating SimpleActionGroup to hold a set of actions and then call
        // Widget.insertActionGroup with your own prefix. 
        model.append("Normal", "win.normal");
        model.append("Checkbox", "win.check");

        Menu section = new Menu();
        //Note the target of the action is represented by the portion after the double colon
        section.append("Left", "win.radio::left");
        section.append("Center", "win.radio::center");
        section.append("Left", "win.radio::right");
        model.appendSection(null, section);

        //MenuButton
        MenuButton mb = new MenuButton();
        mb.setFocusOnClick(false);
        Image imgHamburger = new Image("open-menu-symbolic", IconSize.MENU);
        mb.add(imgHamburger);
        hb.packEnd(mb);

        //Popover
        Popover po = new Popover(mb, model);
        mb.setPopover(po);
    }
}

int main(string[] args) {
    auto application = new Application("demo.gtkd.Actions", GApplicationFlags.FLAGS_NONE);
    application.addOnActivate(delegate void(GioApplication app) { MainWindow mainWindow = new MainWindow(application); });
    return application.run(args);
}