summaryrefslogtreecommitdiff
path: root/extensions/gjs/Gtk.js
blob: 8a239770345668787e2538a0ffa1e6593ba6864a (plain)
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
// application/javascript;version=1.8
// Copyright 2013 Giovanni Campagna
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

const Lang = imports.lang;
const GObject = imports.gi.GObject;

var GjsPrivate = imports.gi.GjsPrivate;

let Gtk;

const GtkWidgetClass = new Lang.Class({
    Name: 'GtkWidgetClass',
    Extends: GObject.Class,

    _init: function(params) {
        let template = params.Template;
        delete params.Template;

        let children = params.Children;
        delete params.Children;

        let internalChildren = params.InternalChildren;
        delete params.InternalChildren;

        if (template) {
            params._instance_init = function() {
                this.init_template();
            };
        }

        this.parent(params);

        if (template) {
            if (typeof template == 'string' &&
                template.startsWith('resource:///'))
                Gtk.Widget.set_template_from_resource.call(this, template.slice(11));
            else
                Gtk.Widget.set_template.call(this, template);
        }

        this.Template = template;
        this.Children = children;
        this.InternalChildren = internalChildren;

        if (children) {
            for (let i = 0; i < children.length; i++)
                Gtk.Widget.bind_template_child_full.call(this, children[i], false, 0);
        }

        if (internalChildren) {
            for (let i = 0; i < internalChildren.length; i++)
                Gtk.Widget.bind_template_child_full.call(this, internalChildren[i], true, 0);
        }
    },

    _isValidClass: function(klass) {
        let proto = klass.prototype;

        if (!proto)
            return false;

        // If proto == Gtk.Widget.prototype, then
        // proto.__proto__ is GObject.InitiallyUnowned, so
        // "proto instanceof Gtk.Widget"
        // will return false.
        return proto == Gtk.Widget.prototype ||
            proto instanceof Gtk.Widget;
    },
});

function _init() {

    Gtk = this;

    Gtk.Widget.prototype.__metaclass__ = GtkWidgetClass;
    if (GjsPrivate.gtk_container_child_set_property) {
        Gtk.Container.prototype.child_set_property = function(child, property, value) {
            GjsPrivate.gtk_container_child_set_property(this, child, property, value);
        };
    }

    Gtk.Widget.prototype._init = function(params) {
        GObject.Object.prototype._init.call(this, params);

        if (this.constructor.Template) {
            let children = this.constructor.Children || [];
            for (let child of children)
                this[child.replace('-', '_', 'g')] = this.get_template_child(this.constructor, child);

            let internalChildren = this.constructor.InternalChildren || [];
            for (let child of internalChildren)
                this['_' + child.replace('-', '_', 'g')] = this.get_template_child(this.constructor, child);
        }
    };
}