Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: d8f395d85b
Fetching contributors…

Cannot retrieve contributors at this time

file 251 lines (214 sloc) 7.877 kb
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
# Copyright (c) 2008, Aldo Cortesi. All rights reserved.
#
# 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.

import manager, window, confreader, command, hook, drawer


class Gap(command.CommandObject):
    """
A gap, placed along one of the edges of the screen. If a gap has been
defined, Qtile will avoid covering it with windows. The most probable
reason for configuring a gap is to make space for a third-party bar or
other static window.
"""
    def __init__(self, size):
        """
size: The width of the gap.
"""
        self.size = size
        self.qtile, self.screen = None, None

    def _configure(self, qtile, screen):
        self.qtile, self.screen = qtile, screen

    @property
    def x(self):
        s = self.screen
        if s.right is self:
            return s.dx + s.dwidth
        else:
            return s.x

    @property
    def y(self):
        s = self.screen
        if s.top is self:
            return s.y
        elif s.bottom is self:
            return s.dy + s.dheight
        elif s.left is self:
            return s.dy
        elif s.right is self:
            return s.y + s.dy

    @property
    def width(self):
        s = self.screen
        if self in [s.top, s.bottom]:
            return s.width
        else:
            return self.size

    @property
    def height(self):
        s = self.screen
        if self in [s.top, s.bottom]:
            return self.size
        else:
            return s.dheight

    def geometry(self):
        return self.x, self.y, self.width, self.height

    def _items(self, name):
        if name == "screen":
            return True, None

    def _select(self, name, sel):
        if name == "screen":
            return self.screen

    @property
    def position(self):
        for i in ["top", "bottom", "left", "right"]:
            if getattr(self.screen, i) is self:
                return i

    def info(self):
        return dict(
            position=self.position
        )

    def cmd_info(self):
        """
Info for this object.
"""
        return self.info()


class Obj:
    def __init__(self, name):
        self.name = name

    def __str__(self):
        return self.name

    def __repr__(self):
        return self.name


STRETCH = Obj("STRETCH")
CALCULATED = Obj("CALCULATED")
STATIC = Obj("STATIC")

class Bar(Gap):
    """
A bar, which can contain widgets. Note that bars can only be placed at
the top or bottom of the screen.
"""
    defaults = manager.Defaults(
        ("background", "#000000", "Background colour."),
        ("opacity", 1, "Bar window opacity.")
    )
    def __init__(self, widgets, size, **config):
        """
- widgets: A list of widget objects.
- size: The height of the bar.
"""
        Gap.__init__(self, size)
        self.widgets = widgets
        self.defaults.load(self, config)
        self.saved_focus = None

    def _configure(self, qtile, screen):
        if not self in [screen.top, screen.bottom]:
            raise confreader.ConfigError(
                    "Bars must be at the top or the bottom of the screen."
                  )
        if len(filter(lambda w: w.width_type == STRETCH, self.widgets)) > 1:
            raise confreader.ConfigError("Only one STRETCH widget allowed!")

        Gap._configure(self, qtile, screen)
        self.window = window.Internal.create(
                        self.qtile,
                        self.x, self.y, self.width, self.height,
                        self.opacity
                     )

        self.drawer = drawer.Drawer(
                            self.qtile,
                            self.window.window.wid,
                            self.width,
                            self.height
                      )
        self.drawer.clear(self.background)

        self.window.handle_Expose = self.handle_Expose
        self.window.handle_ButtonPress = self.handle_ButtonPress
        qtile.windowMap[self.window.window.wid] = self.window
        self.window.unhide()

        for i in self.widgets:
            qtile.registerWidget(i)
            i._configure(qtile, self)

        # FIXME: These should be targeted better.
        hook.subscribe.setgroup(self.draw)
        hook.subscribe.delgroup(self.draw)
        hook.subscribe.addgroup(self.draw)

    def _resize(self, width, widgets):
        stretches = [i for i in widgets if i.width_type == STRETCH]
        if stretches:
            stretchspace = width - sum([i.width for i in widgets if i.width_type != STRETCH])
            stretchspace = max(stretchspace, 0)
            astretch = stretchspace/len(stretches)
            for i in stretches:
                i.width = astretch
            if astretch:
                i.width += stretchspace%astretch

        offset = 0
        for i in widgets:
            i.offset = offset
            offset += i.width

    def handle_Expose(self, e):
        self.draw()

    def handle_ButtonPress(self, e):
        for i in self.widgets:
            if e.event_x < i.offset + i.width:
                i.click(e.event_x - i.offset, e.event_y, e.detail)
                break

    def widget_grab_keyboard(self, widget):
        """
A widget can call this method to grab the keyboard focus
and receive keyboard messages. When done,
widget_ungrab_keyboard() must be called.
"""
        self.window.handle_KeyPress = widget.handle_KeyPress
        self.saved_focus = self.qtile.currentWindow
        self.window.window.set_input_focus()

    def widget_ungrab_keyboard(self):
        """
Removes the widget's keyboard handler.
"""
        del self.window.handle_KeyPress
        if not self.saved_focus == None:
            self.saved_focus.window.set_input_focus()

    def draw(self):
        self._resize(self.width, self.widgets)
        for i in self.widgets:
            i.draw()
        if self.widgets:
            end = i.offset + i.width
            if end < self.width:
                self.drawer.draw(end, self.width-end)

    def info(self):
        return dict(
            width = self.width,
            position = self.position,
            widgets = [i.info() for i in self.widgets],
            window = self.window.window.wid
        )

    def cmd_fake_click(self, screen, position, x, y, button=1):
        """
Fake a mouse-click on the bar. Co-ordinates are relative
to the top-left corner of the bar.

:screen The integer screen offset
:position One of "top", "bottom", "left", or "right"
"""
        class _fake:
            pass
        fake = _fake()
        fake.event_x = x
        fake.event_y = y
        fake.detail = button
        self.handle_ButtonPress(fake)
Something went wrong with that request. Please try again.