Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/mmm_modules/borderframe.py
blob: b97920f1a73695e74a5612a3bd403617741dd659 (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
# Copyright 2007 World Wide Workshop Foundation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# If you find this activity useful or end up using parts of it in one of your
# own creations we would love to hear from you at info@WorldWideWorkshop.org !
#

from gi.repository import Gtk, GObject

BORDER_LEFT = 1
BORDER_RIGHT = 2
BORDER_TOP = 4
BORDER_BOTTOM = 8
BORDER_VERTICAL = BORDER_TOP | BORDER_BOTTOM
BORDER_HORIZONTAL = BORDER_LEFT | BORDER_RIGHT
BORDER_ALL = BORDER_VERTICAL | BORDER_HORIZONTAL
BORDER_ALL_BUT_BOTTOM = BORDER_HORIZONTAL | BORDER_TOP
BORDER_ALL_BUT_TOP = BORDER_HORIZONTAL | BORDER_BOTTOM
BORDER_ALL_BUT_LEFT = BORDER_VERTICAL | BORDER_RIGHT

class BorderFrame (Gtk.EventBox):
    def __init__ (self, border=BORDER_ALL, size=5, bg_color=None, border_color=None):
        Gtk.EventBox.__init__(self)
        if border_color is not None:
            self.set_border_color(Gdk.color_parse(border_color))
        self.inner = Gtk.EventBox()
        if bg_color is not None:
            self.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse(bg_color))
        align = Gtk.Alignment(1.0,1.0,1.0,1.0)
        self.padding = [0,0,0,0]
        if (border & BORDER_TOP) != 0:
            self.padding[0] = size
        if (border & BORDER_BOTTOM) != 0:
            self.padding[1] = size
        if (border & BORDER_LEFT) != 0:
            self.padding[2] = size
        if (border & BORDER_RIGHT) != 0:
            self.padding[3] = size
        align.set_padding(*self.padding)
        align.add(self.inner)
        align.show()
        self.inner.show()
        Gtk.EventBox.add(self, align)
        self.stack = []

    def set_border_color (self, color):
        Gtk.EventBox.modify_bg(self, Gtk.StateType.NORMAL, color)

    def modify_bg (self, state, color):
        self.inner.modify_bg(state, color)

    def add (self, widget):
        self.stack.append(widget)
        self.inner.add(widget)
        self.inner.child.show_now()

    def push (self, widget):
        widget.set_size_request(*self.inner.child.get_size_request())
        self.inner.remove(self.inner.child)
        self.add(widget)

    def pop (self):
        if len(self.stack) > 1:
            self.inner.remove(self.inner.child)
            del self.stack[-1]
            self.inner.add(self.stack[-1])

    def get_child (self):
        return self.inner.child

    def set_size_request (self, w, h):
        self.inner.set_size_request(w,h)
        super(BorderFrame, self).set_size_request(w+self.padding[0]+self.padding[2], h+self.padding[1]+self.padding[3])

    def show (self):
        self.show_all()

#    def get_allocation (self):
#        return self.inner.get_allocation()