/usr/lib/gdesklets/layout/LayoutObject.py is in gdesklets 0.36.1-5+b1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 | import Unit
from HashPQueue import HashPQueue
class LayoutObject(object):
"""
gDesklets Geometry Engine
(c) 2003 - 2005 Martin Grimme <martin@gdesklets.org>
This engine is licensed under the terms of the GNU LGPL.
This class represents a layout object for handling geometry calculations
for visual elements. It is based on the highly flexible and sophisticated
geometry engine of gDesklets 0.34.3, but is completely decoupled from the
rendering backend now and optimized even further.
"""
__slots__ = ("__margin_right", "__margin_bottom", "__margin_ids",
"__enabled",
"__children", "__parent", "__relative", "__is_relative",
"__relatives", "__anchor",
"__real_geometry", "__old_real_geometry", "__geometry",
"__border_width", "__bbox",
"__geometry_callback", "__action_callback")
def __init__(self, parent = None):
"""
Creates a new layout object. You normally only call this for the root
object of the hierarchy of layout objects, without specifying any parent
object.
"""
# right margin
self.__margin_right = HashPQueue(HashPQueue.GREATER_THAN)
# bottom margin
self.__margin_bottom = HashPQueue(HashPQueue.GREATER_THAN)
# the IDs of the margin entries
self.__margin_ids = (-1, -1)
# whether the element is enabled
self.__enabled = True
# the children of a layout object
self.__children = []
# the parent object
self.__parent = parent
# the object to which this object is placed relative to
self.__relative = None
self.__is_relative = (False, False)
self.__relatives = []
# the position of the positioning anchor given as a percentage of the
# object's size
self.__anchor = (0, 0)
# the layout object's geometry
self.__real_geometry = (Unit.ZERO, Unit.ZERO, Unit.ZERO, Unit.ZERO)
self.__old_real_geometry = (Unit.ZERO, Unit.ZERO, Unit.ZERO, Unit.ZERO)
self.__geometry = (Unit.Unit(), Unit.Unit(), Unit.Unit(), Unit.Unit())
# the widths of the border
self.__border_width = (Unit.ZERO, Unit.ZERO, Unit.ZERO, Unit.ZERO)
# the children bounding box
self.__bbox = (Unit.ZERO, Unit.ZERO, Unit.ZERO, Unit.ZERO)
# the geometry callback handler
self.__geometry_callback = None
# the action callback handler
self.__action_callback = None
def new_child(self):
"""
Creates and returns a new layout object as a child of this layout
object. Use this method to add new objects.
"""
child = LayoutObject(self)
self.__children.append(child)
return child
def remove_child(self, child):
"""
Removes the given child layout object. Raises a ValueError if the object
does not exist.
"""
self.__children.remove(child)
rx, ry, rw, rh = child.get_real_geometry()
self.__margin_right.remove(rx + rw)
self.__margin_bottom.remove(ry + rh)
def set_callback(self, handler):
"""
Sets the callback handler for geometry changes. The callback handler
will be invoked whenever the geometry of the object changes.
The handler has the signature:
handler(src, x, y, width, height)
The callback notifies you about geometry changes of an object.
"""
self.__geometry_callback = handler
def set_action_callback(self, handler):
self.__action_callback = handler
def set_enabled(self, value):
"""
Enables or disables the layout object. Disabled objects don't contribute
to the layout and act as if they did not exist.
"""
self.__enabled = value
self._update_all()
def set_geometry(self, x = None, y = None, width = None, height = None):
"""
Sets the geometry values. The values have to be valid Unit objects.
"""
gx, gy, gw, gh = self.__geometry
if (x): gx = x
if (y): gy = y
if (width): gw = width
if (height): gh = height
self.__geometry = (gx, gy, gw, gh)
self._update_all()
def get_geometry(self):
"""
Returns the user-given geometry in Unit values.
"""
return self.__geometry
def get_real_geometry(self):
"""
Returns the real geometry of the object, with anchors, and relative and
percentual positioning resolved.
"""
return self.__real_geometry
def _update_bbox(self, right_id, bottom_id, x, y, w, h, ignore_x, ignore_y):
"""
Updates the bounding box with the given values.
"""
self.__margin_right.remove(right_id)
self.__margin_bottom.remove(bottom_id)
right_id, bottom_id = -1, -1
if (not ignore_x):
right_id = self.__margin_right.insert(x.copy() + w.copy())
if (not ignore_y):
bottom_id = self.__margin_bottom.insert(y.copy() + h.copy())
return (right_id, bottom_id)
def __get_children_bbox(self):
"""
Returns the bounding box of the child objects.
"""
bx1 = Unit.ZERO
by1 = Unit.ZERO
bx2 = self.__margin_right.top() or Unit.ZERO
by2 = self.__margin_bottom.top() or Unit.ZERO
return (bx1, by1, bx2, by2)
def __get_parent_size(self):
"""
Returns the size of the parent widget.
"""
parent_width, parent_height = \
self.__parent.get_real_geometry()[2:]
# don't allow dangerous values for the parent's size
parent_width = max(Unit.ONE, parent_width)
parent_height = max(Unit.ONE, parent_height)
return (parent_width, parent_height)
def set_anchor(self, x, y):
"""
Sets the positioning anchor to the given place. The coordinates are
given in percentages of the object's size with values between 0.0
and 1.0.
"""
self.__anchor = (x, y)
self._update_all()
def get_anchor(self):
"""
Returns the position of the anchor.
"""
return self.__anchor
def __is_percentual(self):
"""
Returns whether the object has some percentual values.
"""
x, y, w, h = self.__geometry
return Unit.UNIT_PERCENT in (x.get_unit(), y.get_unit(),
w.get_unit(), h.get_unit())
def __resolve_anchor(self, x, y, w, h):
"""
Resolves the anchored position and returns the position of the top-left
corner of the object.
"""
ax, ay = self.get_anchor()
ax = w * ax
ay = h * ay
return (x - ax, y - ay)
def _update_all(self):
"""
Updates the geometry of the layout object and adjusts depending objects
as well.
"""
if (self._update_geometry()):
self._update_relatives()
self._update_children()
self._update_parent()
# objects with percentual geometry sometimes need special treatment
if (self.__is_percentual()):
if (self._update_geometry()):
self._update_relatives()
else:
self._update_children()
def _update_geometry(self):
"""
Updates the geometry of the layout object.
This does not include notifying the parent object or relative objects
about the change.
Returns True if something changed, False otherwise.
"""
x, y, w, h = self.__real_geometry
ux, uy, uw, uh = self.__geometry
# adjust percentage stuff
if (self.__parent):
parent_width, parent_height = self.__get_parent_size()
w1, h1, w2, h2 = self.__parent.get_border_width()
pw = parent_width - w1 - w2
ph = parent_height - h1 - h2
parent_width = (pw >= Unit.ZERO) and pw or Unit.ZERO
parent_height = (ph >= Unit.ZERO) and ph or Unit.ZERO
if (parent_width.is_unset()): parent_width = w
if (parent_height.is_unset()): parent_height = h
ux.set_100_percent(parent_width.as_px())
uw.set_100_percent(parent_width.as_px())
uy.set_100_percent(parent_height.as_px())
uh.set_100_percent(parent_height.as_px())
# compute geometry
if (ux.is_unset()): new_x = x
else: new_x = ux
if (uy.is_unset()): new_y = y
else: new_y = uy
if (self.__enabled):
if (uw.is_unset()): new_w = self.__get_value_for_unset(width = w)
else: new_w = uw
if (uh.is_unset()): new_h = self.__get_value_for_unset(height = h)
else: new_h = uh
else:
new_w = Unit.ZERO
new_h = Unit.ZERO
# handle relative positioning
if (self.__relative):
is_rel_x, is_rel_y = self.__is_relative
# get the coords of the anchor of the relative
rx, ry, rw, rh = self.__relative.get_real_geometry()
anchor_x, anchor_y = self.__relative.get_anchor()
anchor_x = rw * anchor_x
anchor_y = rh * anchor_y
# get the remaining amount of pixels from the anchor to the
# bottom/right edge of the relative
tw = rw - anchor_x
th = rh - anchor_y
if (not ux.is_unset()): new_x += rx + anchor_x
else: new_x = rx + anchor_x
if (not uy.is_unset()): new_y += ry + anchor_y
else: new_y = ry + anchor_y
if (is_rel_x): new_x += tw
if (is_rel_y): new_y += th
#end if
# get coords of the top left corner, i.e. resolve the anchor
new_x, new_y = self.__resolve_anchor(new_x, new_y, new_w, new_h)
old_x, old_y, old_w, old_h = self.__old_real_geometry
if ((new_x, new_y, new_w, new_h) != self.__old_real_geometry):
self.__real_geometry = (new_x.copy(), new_y.copy(),
new_w.copy(), new_h.copy())
self.__old_real_geometry = (new_x.copy(), new_y.copy(),
new_w.copy(), new_h.copy())
# update the parent's children bounding box
if (self.__parent):
right_id, bottom_id = self.__margin_ids
ignore_x = (Unit.UNIT_PERCENT in
(ux.get_unit(), uw.get_unit()))
ignore_y = (Unit.UNIT_PERCENT in
(uy.get_unit(), uh.get_unit()))
self.__margin_ids = \
self.__parent._update_bbox(right_id, bottom_id,
new_x, new_y, new_w, new_h,
ignore_x, ignore_y)
# call the geometry callback
if (self.__geometry_callback):
self.__geometry_callback(self, new_x, new_y, new_w, new_h)
return True
else:
return False
def _update_relatives(self):
"""
Updates the geometry of the objects placed relative to this object.
"""
for r in self.__relatives:
r._update_geometry()
r._update_relatives()
def _update_children(self):
"""
Updates the geometry of the child objects of this object. This only
affects children with percentual geometry values.
"""
if (not self.__enabled): return
x, y, w, h = self.get_geometry()
for c in self.__children:
ux, uy, uw, uh = c.get_geometry()
if (Unit.UNIT_PERCENT in (ux.get_unit(), uy.get_unit(),
uw.get_unit(), uh.get_unit())):
c._update_geometry()
c._update_relatives()
c._update_children()
#end for
def _update_parent(self):
"""
Updates the geometry of the parent object. This only affects the parent
if it does not have a fixed size.
"""
if (self.__parent):
x, y, w, h = self.__parent.get_geometry()
if (Unit.Unit() in (w, h)):
self.__parent._update_all()
def set_relative_to(self, other, is_rel_x, is_rel_y):
"""
Places the object relative to another one.
"""
self.__relative = other
self.__is_relative = (is_rel_x, is_rel_y)
other._add_relative(self)
self._update_all()
def _add_relative(self, r):
"""
Adds a new relative object.
"""
self.__relatives.append(r)
def _remove_relative(self, r):
"""
Removes a relative object.
"""
try:
self.__relatives.remove(r)
except:
pass
def set_border_width(self, left, top, right, bottom):
"""
Sets the widths of the four borders of the layout object.
A border-width > 0 results in inner padding.
"""
self.__border_width = (left, top, right, bottom)
self._update_all()
def get_border_width(self):
"""
Returns the widths of the four borders as a 4-tuple.
"""
return self.__border_width
def __get_value_for_unset(self, x = None, y = None,
width = None, height = None):
"""
Returns the geometry value that should be used for unset values.
"""
bx1, by1, bx2, by2 = self.__get_children_bbox()
bw1, bh1, bw2, bh2 = self.__border_width
if (x): return x
elif (y): return y
elif (width): return bx2 + bw1 + bw2
elif (height): return by2 + bh1 + bh2
def send_action(self, x, y, *args):
if (self.__action_callback):
self.__action_callback(self, x, y, *args)
bw, bh, nil, nil = self.__border_width
x -= bw
y -= bh
for c in self.__children:
cx, cy, cw, ch = c.get_real_geometry()
if (cx <= x < cx + cw and cy <= y < cy + ch):
c.send_action(x - cx, y - cy, *args)
#end for
def dump(self, indent = 0):
"""
For debugging:
Dumps the geometry values of the layout object together with all
child-objects to stdout.
"""
print " " * indent,
print self.get_geometry(), "---", \
map(lambda f: f.as_px(), self.get_real_geometry())
for c in self.__children:
c.dump(indent + 2)
if (__name__ == "__main__"):
help(LayoutObject)
def on_action(src, x, y, arg):
print x, y, arg
def on_change(src, x, y, w, h):
print src
print " ", x, y, w, h
r = LayoutObject()
r.set_geometry(Unit.ZERO, Unit.ZERO,
Unit.Unit(1280, Unit.UNIT_PT), Unit.Unit(1024, Unit.UNIT_PT))
a = r.new_child()
a.set_geometry(Unit.Unit(50, Unit.UNIT_PERCENT),
Unit.Unit(10, Unit.UNIT_PERCENT),
Unit.Unit(), Unit.Unit())
a.set_border_width(Unit.Unit(10, Unit.UNIT_PX),
Unit.Unit(10, Unit.UNIT_PX),
Unit.Unit(10, Unit.UNIT_PX),
Unit.Unit(10, Unit.UNIT_PX))
for x in range(0, 7):
for y in range(0, 5):
new = a.new_child()
new.set_callback(on_change)
new.set_action_callback(on_action)
new.set_geometry(Unit.Unit(x, Unit.UNIT_PT),
Unit.Unit(y, Unit.UNIT_PT),
Unit.Unit(50, Unit.UNIT_PT),
Unit.Unit(10, Unit.UNIT_PT))
new.set_anchor(1.0, 0.2)
print a.get_real_geometry()
a.send_action(Unit.Unit(5, Unit.UNIT_PT),
Unit.Unit(4, Unit.UNIT_PT), "test")
|