-
Notifications
You must be signed in to change notification settings - Fork 40
/
dockx
executable file
·1430 lines (1332 loc) · 56.7 KB
/
dockx
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python2
# DockX
#
# Copyright 2011 Matias Sars
#
# DockbarX 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 3 of the License, or
# (at your option) any later version.
#
# DockbarX 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 dockbar. If not, see <http://www.gnu.org/licenses/>.
from dockbarx.log import *
import sys
log_to_file()
sys.stderr = StdErrWrapper()
sys.stdout = StdOutWrapper()
import pygtk
pygtk.require("2.0")
import gtk
import cairo
import dockbarx.dockbar
import wnck
import gobject
from dockbarx.common import Globals
from dockbarx.theme import DockTheme
from dockbarx.applets import DockXApplets, DockXApplet
from Xlib import display
from math import pi, sin, cos, tan, atan
import weakref
import time
import dbus
import dbus.service
WNCK_WINDOW_STATE_MINIMIZED = 1
class CairoDockX(gtk.Window):
"""The Dock Window for running DockbarX as a standalone dock"""
__gsignals__ = {"expose-event": "override"}
def __init__(self):
gtk.Window.__init__(self)
self.globals = Globals()
self.theme = DockTheme()
self.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DOCK)
self.stick()
self.set_accept_focus(False)
self.set_decorated(False)
self.set_resizable(False)
gtk_screen = gtk.gdk.screen_get_default()
colormap = gtk_screen.get_rgba_colormap()
if colormap is None:
colormap = gtk_screen.get_rgb_colormap()
self.set_colormap(colormap)
self.set_app_paintable(1)
self.button_area = gtk.Alignment(0,0,1,1)
gtk.Window.add(self, self.button_area)
self.compute_padding()
self.globals.connect("dock-color-changed", self.__on_color_changed)
def do_expose_event(self, event):
self.window.set_back_pixmap(None, False)
self.set_shape_mask()
w,h = self.get_size()
self.ctx = self.window.cairo_create()
if self.is_composited():
self.ctx.set_source_rgba(1, 1, 1, 0)
else:
self.ctx.set_source_rgb(0.8, 0.8, 0.8)
self.ctx.set_operator(cairo.OPERATOR_SOURCE)
self.ctx.paint()
self.ctx.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
self.ctx.clip()
self.draw_frame(self.ctx, w, h)
gtk.Window.do_expose_event(self, event)
if self.get_child():
self.propagate_expose(self.get_child(), event)
def set_shape_mask(self):
# Set window shape from alpha mask of background image
pos = self.globals.settings["dock/position"]
w, h = self.get_size()
if w==0 or h==0:
return
pixmap = gtk.gdk.Pixmap (None, w, h, 1)
ctx = pixmap.cairo_create()
ctx.set_source_rgba(0, 0, 0, 0)
ctx.set_operator (cairo.OPERATOR_SOURCE)
ctx.paint()
if self.is_composited():
if self.dockbar is not None:
ctx.set_source_rgba(1, 1, 1, 1)
a = self.dockbar.get_container().get_allocation()
ctx.rectangle(*a)
ctx.fill()
w, h = self.__set_rotation(pos, ctx, w, h)
stroke_path, fill_path = self.__make_path(ctx, w, h, bar2=False)
ctx.new_path()
ctx.set_source_rgba(1, 1, 1, 1)
ctx.append_path(fill_path)
ctx.fill()
ctx.set_source_rgba(1, 1, 1, 1)
ctx.append_path(stroke_path)
ctx.set_line_width(0.8)
ctx.stroke()
if int(self.theme.get("use_bar2", False)):
stroke_path, fill_path = self.__make_path(ctx, w, h, bar2=True)
ctx.new_path()
ctx.set_source_rgba(1, 1, 1, 1)
ctx.append_path(fill_path)
ctx.fill()
ctx.set_source_rgba(1, 1, 1, 1)
ctx.append_path(stroke_path)
ctx.set_line_width(0.8)
ctx.stroke()
if self.is_composited():
self.window.shape_combine_mask(None, 0, 0)
self.window.input_shape_combine_mask(pixmap, 0, 0)
else:
self.window.shape_combine_mask(pixmap, 0, 0)
def draw_frame(self, ctx, w, h):
pos = self.globals.settings["dock/position"]
w, h = self.__set_rotation(pos, ctx, w, h)
self.__make_path(ctx, w, h, bar2=False)
color = self.globals.dock_colors["bg_color"]
red, green, blue = self.__parse_color(color)
alpha = self.globals.dock_colors["bg_alpha"] / 255.0
#~ ctx.new_path()
#~ ctx.append_path(fill_path)
if self.is_composited():
ctx.set_source_rgba(red, green, blue, alpha)
else:
ctx.set_source_rgb(red, green, blue)
ctx.fill_preserve()
ctx.set_operator(cairo.OPERATOR_OVER)
# Linear gradients
for n in (1, 2, 3):
name = "linear_gradient%s" % n
if not int(self.theme.get("use_%s" % name, 0)):
continue
pattern = self.__make_linear_pattern(w, h, n)
rpc1 = self.theme.get("%s_start_color" % name, "#FFFFFF")
if not rpc1[0] == "#":
rpc1 = "#%s" % rpc1
red, green, blue = self.__parse_color(rpc1)
alpha = self.theme.get("%s_start_alpha" % name, 20)
alpha = float(alpha) / 100
pattern.add_color_stop_rgba(0.0, red, green, blue, alpha)
rpc2 = self.theme.get("%s_stop_color" % name, "#FFFFFF")
if not rpc2[0] == "#":
rpc2 = "#%s" % rpc2
red, green, blue = self.__parse_color(rpc2)
alpha = self.theme.get("%s_stop_alpha" % name, 0)
alpha = float(alpha) / 100
pattern.add_color_stop_rgba(1.0, red, green, blue, alpha)
ctx.set_source(pattern)
ctx.fill_preserve()
# Background picture
if self.theme.bg[1] is not None:
if self.theme.get("strech_bg_img", False):
pattern = cairo.SurfacePattern(self.theme.get_bg(1, h))
else:
pattern = cairo.SurfacePattern(self.theme.get_bg(1))
pattern.set_extend(cairo.EXTEND_REPEAT)
ctx.set_source(pattern)
ctx.fill_preserve()
# Stroke the outer border
ctx.set_operator(cairo.OPERATOR_SOURCE)
#~ ctx.append_path(stroke_path)
# Since copy_path/append_path puts the path in the wrong position
# in cairo 1.12 we have to call make path again to make the stroke path
self.__make_path(ctx, w, h, bar2=False, stroke_only=True)
color = self.theme.get("border_color", "#000000")
red, green, blue = self.__parse_color(color)
ctx.set_source_rgb(red, green, blue)
bw = float(self.theme.get("border_width", 0.8))
ctx.set_line_width(bw)
ctx.stroke()
if not int(self.theme.get("use_bar2", False)):
return
if self.theme.get("bar2_type") == "layer":
ctx.set_operator(cairo.OPERATOR_OVER)
self.__make_path(ctx, w, h, bar2=True)
color = self.globals.dock_colors["bar2_bg_color"]
red, green, blue = self.__parse_color(color)
alpha= self.globals.dock_colors["bar2_bg_alpha"] / 255.0
#~ ctx.new_path()
#~ ctx.append_path(fill_path)
if self.is_composited():
ctx.set_source_rgba(red, green, blue, alpha)
else:
ctx.set_source_rgb(red, green, blue)
ctx.fill_preserve()
ctx.set_operator(cairo.OPERATOR_OVER)
# Linear gradients
for n in (1, 2, 3):
name = "bar2_linear_gradient%s" % n
if not int(self.theme.get("bar2_use_linear_gradient%s" % n, 0)):
continue
pattern = self.__make_linear_pattern(w, h, n, bar2=True)
rpc1 = self.theme.get("%s_start_color" % name, "#FFFFFF")
if not rpc1[0] == "#":
rpc1 = "#%s" % rpc1
red, green, blue = self.__parse_color(rpc1)
alpha = self.theme.get("%s_start_alpha" % name, 20)
alpha = float(alpha) / 100
pattern.add_color_stop_rgba(0.0, red, green, blue, alpha)
rpc2 = self.theme.get("%s_stop_color" % name, "#FFFFFF")
if not rpc2[0] == "#":
rpc2 = "#%s" % rpc2
red, green, blue = self.__parse_color(rpc2)
alpha = self.theme.get("%s_stop_alpha" % name, 0)
alpha = float(alpha) / 100
pattern.add_color_stop_rgba(1.0, red, green, blue, alpha)
ctx.set_source(pattern)
ctx.fill_preserve()
# Background picture
if self.theme.bg[2] is not None:
if self.theme.get("bar2_strech_bg_img", False):
pattern = cairo.SurfacePattern(self.theme.get_bg(2, h))
else:
pattern = cairo.SurfacePattern(self.theme.get_bg(2))
pattern.set_extend(cairo.EXTEND_REPEAT)
ctx.set_source(pattern)
ctx.fill_preserve()
# Stroke the outer border
ctx.set_operator(cairo.OPERATOR_SOURCE)
color = self.theme.get("bar2_border_color", "#000000")
red, green, blue = self.__parse_color(color)
# Since copy_path/append_path puts the path in the wrong position
# in cairo 1.12 we have to call make path again to make the stroke path
self.__make_path(ctx, w, h, bar2=True, stroke_only=True)
#~ ctx.append_path(stroke_path)
ctx.set_source_rgb(red, green, blue)
bw = float(self.theme.get("bar2_border_width", 0.8))
ctx.set_line_width(bw)
ctx.stroke()
def __set_rotation(self, pos, ctx, w, h):
# Changes the user space so that the dock can be drawn as if
# it's at the bottom no matter where it's really placed.
if pos == "left":
old = h
h = w
w = old
del old
ctx.rotate(pi/2)
ctx.translate(0, -h)
elif pos == "right":
old = h
h = w
w = old
del old
ctx.rotate(pi/2)
ctx.scale(1, -1)
elif pos == "top":
ctx.scale(1, -1)
ctx.translate(0, -h)
return w, h
def __make_path(self, ctx, w, h, bar2=False, stroke_only=False):
# Create the stroke and fill paths
mode = self.globals.settings["dock/mode"]
ed = self.globals.settings["dock/end_decorations"]
if bar2:
# slope is recalculated in such a way that 0 means a straight
# angle, positive numbers means outwards slope and negative
# numbers inwards. In radians.
slope = -(float(self.theme.get("bar2_slope", 90)) - 90) / 180.0*pi
bend = float(self.theme.get("bar2_bend", 0))
r = float(self.theme.get("bar2_roundness", 6))
sp = int(self.theme.get("bar2_side_padding", 6))
rel_size = float(self.theme.get("bar2_rel_size", 100.0))
rel_size2 = float(self.theme.get("rel_size", 100.0))
slope2 = -(float(self.theme.get("slope", 90)) - 90) / 180.0 * pi
sp2 = int(self.theme.get("side_padding", 6))
if not self.is_composited() and rel_size2 < 100:
rel_size2 = 100.0
else:
slope = -(float(self.theme.get("slope", 90)) - 90) / 180.0 * pi
bend = float(self.theme.get("bend", 0))
r = float(self.theme.get("roundness", 6))
sp = int(self.theme.get("side_padding", 6))
rel_size = float(self.theme.get("rel_size", 100.0))
if int(self.theme.get("use_bar2", False)):
rel_size2 = float(self.theme.get("bar2_rel_size", 100.0))
slope2 = -(float(self.theme.get("bar2_slope", 90)) - 90)
slope2 = slope2 / 180.0 * pi
sp2 = int(self.theme.get("bar2_side_padding", 6))
else:
rel_size2 = 0
slope2 = 0
sp2 = 0
if not self.is_composited() and rel_size < 100:
rel_size = 100.0
size = int(self.globals.settings["dock/size"] * rel_size / 100)
size2 = int(self.globals.settings["dock/size"] * rel_size2 / 100)
t = h - size
d1 = int(tan(abs(slope)) * size)
d2 = int(bend / 100 * d1)
p = max(int(tan(abs(slope2)) * size2) + sp2 - d1 - sp, 0)
if mode == "centered" or ed:
y0 = h
y3 = t + 0.5
if slope < 0:
x0 = p + 0.5 + d1
x1 = x0 - d2
y1 = y0
x3 = p + 0.5
rad = atan((x1-x3)/(y1-y3))
x2 = x3 + sin(rad) * r
y2 = y3 + cos(rad) * r
else:
x0 = p + 0.5
x1 = x0 + d2
y1 = y0
x3 = p + 0.5 + d1
rad = atan((x3-x1)/(y1-y3))
x2 = x3 - sin(rad) * r
y2 = y3 + cos(rad) * r
ctx.move_to(x0, y0)
ctx.curve_to(x1, y1, x1, y1, x2, y2)
ctx.curve_to(x3, y3, x3, y3, x3 + r, y3)
else:
ctx.move_to(0, t + 0.5)
if mode == "centered" or mode == "corner" or ed:
y3 = h
y0 = t + 0.5
if slope < 0:
x3 = w - p - 0.5 - d1
x2 = x3 + d2
y2 = y3
x0 = w - p - 0.5
rad = atan((x0-x2)/(y2-y0))
x1 = w - p - 0.5 - sin(rad) * r
y1 = y0 + cos(rad) * r
else:
x3 = w - p - 0.5
x2 = x3 - d2
y2 = y3
x0 = w - p - 0.5 - d1
rad = atan((x2-x0)/(y2-y0))
x1 = x0 + sin(rad) * r
y1 = y0 + cos(rad) * r
ctx.line_to(x0 - r, y0)
ctx.curve_to(x0, y0, x0, y0, x1, y1)
ctx.curve_to(x2, y2, x2, y2, x3, y3)
else:
ctx.line_to(w, t + 0.5)
if stroke_only:
# Only stroke path is interesting and it is used right away.
return
stroke_path = ctx.copy_path()
ctx.line_to(w, h)
ctx.line_to(0, h)
ctx.close_path()
fill_path = ctx.copy_path()
return stroke_path, fill_path
def __parse_color(self, color):
if color[0] != "#":
color = "#%s" % color
red = float(int(color[1:3], 16))/255
green = float(int(color[3:5], 16))/255
blue = float(int(color[5:7], 16))/255
return red, green, blue
def __make_linear_pattern(self, w, h, n, bar2=False):
if bar2:
name = "bar2_linear_gradient%s" % n
rel_size = float(self.theme.get("bar2_rel_size", 100.0))
else:
name = "linear_gradient%s" % n
rel_size = float(self.theme.get("rel_size", 100.0))
angle = int(self.theme.get("%s_angle" % name, 0))
start = float(self.theme.get("%s_start" % name, 0))
stop = float(self.theme.get("%s_stop" % name, 100))
size = int(self.globals.settings["dock/size"] * rel_size / 100)
t = h - size
start_x = None
angle = angle % 360
if angle < 0:
angle += 360
if angle == 0:
start_x = start * w / 100.0
start_y = 0
stop_x = stop * w / 100.0
stop_y = 0
if angle == 180:
start_x = w - (start * w / 100.0)
start_y = 0
stop_x = w - (stop * w / 100.0)
stop_y = 0
elif angle == 270:
start_x = 0
start_y = t + start * size / 100.0
stop_x = 0
stop_y = t + stop * size / 100.0
elif angle == 90:
start_x = 0
start_y = h - (start * size / 100.0)
stop_x = 0
stop_y = h - (stop * size / 100.0)
elif angle < 90:
x1 = w * start / 100.0
y1 = h - size * start / 100.0
x2 = w * stop / 100.0
y2 = h - size * stop / 100.0
elif 90 < angle and angle < 180:
x1 = w - (w * start / 100.0)
y1 = h - size * start / 100.0
x2 = w - (w * stop / 100.0)
y2 = h - size * stop / 100.0
elif 180 < angle and angle < 270:
x1 = w - (w * start / 100.0)
y1 = t + size * start / 100.0
x2 = w - (w * stop / 100.0)
y2 = t + size * stop / 100.0
elif 270 < angle:
x1 = w * start / 100.0
y1 = t + size * start / 100.0
x2 = w * stop / 100.0
y2 = t + size * stop / 100.0
if start_x is None:
k1 = -tan(angle * pi / 180.0 )
k2 = -1 / k1
start_x = x1
start_y = y1
stop_x = (k1 * x1 - k2 * x2 + y2 - y1) / (k1 - k2)
stop_y = k1 * (stop_x - x1) + y1
return cairo.LinearGradient(start_x, start_y, stop_x, stop_y)
def compute_padding(self):
pos = self.globals.settings["dock/position"]
size = self.globals.settings["dock/size"]
rel_size = float(self.theme.get("rel_size", 100))
if int(self.theme.get("use_bar2", False)):
b2_rel_size = rel_size = float(self.theme.get("bar2_rel_size",
100))
else:
b2_rel_size = 0
p = max(int(size*rel_size/100) - size,
int(size*b2_rel_size/100) - size,
0)
# Todo: Is rel_bottom_padding really a good idea? Decide.
rb = float(self.theme.get("rel_bottom_padding", 0))
b = int(size * rb / 100)
if pos == "left":
padding = (0, 0, b, p)
elif pos == "right":
padding = (0, 0, p, b)
elif pos == "top":
padding = (b, p, 0, 0)
else:
padding = (p, b, 0, 0)
self.button_area.set_padding(*padding)
def add_box(self, widget):
self.button_area.add(widget)
def remove_box(self):
self.button_area.remove(widget)
def __on_color_changed(self, *args):
self.queue_draw()
class DockX(CairoDockX):
__gsignals__ = {"destroy": "override",
"size-allocate": "override"}
def __init__(self, monitor=0):
self.globals = Globals()
self.empty_space = {"left": 1000, "right": 1000,
"top": 1000, "bottom":1000}
self.geometry_time = 0
self.last_geometry_window = lambda: None
if self.globals.settings["dock/behavior"] in ("panel", "standard"):
self.autohide = False
else:
self.autohide = True
self.screen = wnck.screen_get_default()
self.monitor = monitor
self.windows = weakref.WeakKeyDictionary()
self.border_distances = weakref.WeakKeyDictionary()
self.db_loaded = False
self.dockbar = dockbarx.dockbar.DockBar(parent=self)
self.dockbar.set_parent_handles_menu(True)
self.dockbar.set_parent_window_reporting(True)
self.dockbar.set_no_theme_change_reload(True)
self.dockbar.set_no_dbus_reload(True)
self.dockbar.set_keyboard_show_dock(True)
self.dockbar.set_expose_on_clear(True)
self.dockbar.load()
self.db_loaded = True
CairoDockX.__init__(self)
self.set_keep_above(True)
self.autohide_sid = None
self.autounhide_sid = None
self.autounhide_sid = None
self.dockbar_max_size = None
self.old_x = 0
self.old_y = 0
self.old_width = 0
self.old_height = 0
self.box = gtk.HBox()
self.position_boxes = None
self.applet_sids = {}
self.padding1 = EventPadding(self)
self.padding2 = EventPadding(self)
self.spacer1 = EventPadding(self)
self.spacer2 = EventPadding(self)
self.add_box(self.box)
self.applets = DockXApplets()
self.position_dock()
self.globals.connect("dock-size-changed", self.__on_dock_size_changed)
self.globals.connect("dock-position-changed",
self.__on_position_changed)
self.globals.connect("dock-mode-changed", self.__on_mode_changed)
self.globals.connect("dock-offset-changed", self.__on_offset_changed)
self.globals.connect("dock-behavior-changed",
self.__on_behavior_changed)
self.__compute_should_autohide()
self.screen.connect("active-window-changed",
self.__on_active_window_changed)
self.globals.connect("theme-changed", self.reload)
self.globals.connect("dock-end-decorations-changed",
self.__on_end_decorations_changed)
self.theme.connect("dock-theme-reloaded",
self.__on_dock_theme_reloaded)
screen = gtk.gdk.screen_get_default()
screen.connect("size-changed", self.__on_screen_size_changed)
if self.autohide:
self.show_dock()
self.dbus_obj = DockXDBus(self)
def position_dock(self, rebuild=False):
centered = self.globals.settings["dock/mode"] == "centered"
cornered = self.globals.settings["dock/mode"] == "corner"
ed = self.globals.settings["dock/end_decorations"]
pos = self.globals.settings["dock/position"]
orient = {"top": "up", "left": "left",
"right": "right", "bottom": "down"}[pos]
if self.dockbar.get_orient() != orient or not self.box.get_children():
self.dockbar.set_orient(orient)
rebuild = True
if rebuild:
self.__rebuild()
else:
self.__update_applets()
mx, my, mw, mh = self.get_monitor_geometry()
l, r, t, b = self.__get_monitor_and_strut_borders()
sw = self.get_screen().get_width()
sh = self.get_screen().get_height()
rel_size = float(self.theme.get("rel_size", 100))
db_size = self.globals.settings["dock/size"]
size = max(db_size, int(db_size * rel_size / 100))
o = self.globals.settings["dock/offset"]
if pos == "left":
x,y, w, h = (l, t + o, size, sh - t - b - o)
strut = [x + w, 0, 0, 0, y, y + h - 1, 0, 0, 0, 0, 0, 0]
elif pos == "right":
x,y, w, h = (sw - size - r, t + o, size, sh - t - b - o)
strut = [0, sw - x, 0, 0, 0, 0, y, y + h - 1, 0, 0, 0, 0]
elif pos == "top":
x,y, w, h = (l + o, t, sw - l - r - o, size)
strut = [0, 0, y + h, 0, 0, 0, 0, 0, x, x + w - 1, 0, 0]
else:
x,y, w, h = (l + o, sh - size - b, sw - l - r - o, size)
strut = [0, 0, 0, sh - y, 0, 0, 0, 0, 0, 0, x, x + w - 1]
if (centered or cornered) and pos in ("left", "right"):
self.set_size_request(w, -1)
elif (centered or cornered):
self.set_size_request(-1, h)
else:
self.set_size_request(w, h)
self.__set_dock_strut(x, y, w, h)
if centered and pos in ("left", "right"):
a = self.get_allocation()
self.move(x, my + mh / 2 - a.height / 2)
elif centered:
a = self.get_allocation()
self.move(mx + mw / 2 - a.width / 2, y)
else:
self.move(x, y)
self.old_x = x
self.old_y = y
self.dockbar.set_size(db_size)
def __rebuild(self):
self.__destroy_old()
centered = self.globals.settings["dock/mode"] == "centered"
cornered = self.globals.settings["dock/mode"] == "corner"
panel = self.globals.settings["dock/mode"] == "panel"
ed = self.globals.settings["dock/end_decorations"]
pos = self.globals.settings["dock/position"]
if pos in ("left", "right"):
gtk_box = gtk.VBox
else:
gtk_box = gtk.HBox
self.box = gtk_box()
self.add_box(self.box)
# Padding/End decorations before applets
self.box.pack_start(self.padding1, False, False)
self.padding1.compute_size(centered or ed)
# Add the applets to the dock
alist = self.applets.get_list()
self.__add_applets(alist, pos, panel)
# Padding/End decorations after applets
self.box.pack_start(self.padding2, False, False)
self.padding2.compute_size(centered or cornered or ed)
# Define if the paddings has a position right
# before or after the dockbarx applet.
# Needed for drag and drop behavior.
p1 = p2 = None
if alist[0] == "DockbarX":
p1 = "before"
if alist[-1] == "DockbarX":
p2 = "after"
self.padding1.define_position(p1)
self.padding2.define_position(p2)
self.show_all()
self.connect_applet_signals()
# Do a first calculation of dockbarx max size after everything has been realised.
gobject.idle_add(self.__calulate_db_max_size_on_realized)
def __destroy_old(self):
self.disconnect_applet_signals()
# Clean up the old box
for child in self.box.get_children():
self.box.remove(child)
self.box.destroy()
if self.position_boxes:
for box in self.position_boxes:
for child in box.get_children():
box.remove(child)
box.destroy()
def __add_applets(self, alist, pos, panel):
# Add the applets to the dock
self.position_boxes = self.__setup_boxes(alist, pos, panel)
if self.position_boxes:
box = self.position_boxes[0]
else:
box = self.box
expanded = False
spacer = self.spacer1
for i in range(len(alist)):
name = alist[i]
if name == "Spacer" and panel:
if self.position_boxes and box == self.position_boxes[1]:
# Put the second spacer in the right/bottom box.
# Start putting applets in the right/bottom box.
box = self.position_boxes[2]
box.pack_start(spacer, True, True)
# Define spacer posistion relative to dbx
# Needed for correct drag and drop behavior.
if i != 0 and alist[i - 1] == "DockbarX":
if not self.position_boxes:
# Instead of adding a spacer after the dockbar
# we set dockbar to expand.
self.__dockbar_set_expand(box, True)
expanded = True
continue
spacer.define_position("after")
elif i + 1 != len(alist) and alist[i + 1] == "DockbarX":
spacer.define_position("before")
if self.position_boxes and box == self.position_boxes[0]:
# The first spacer is in the left/top box.
# Start putting applets in the middle box.
box = self.position_boxes[1]
spacer = self.spacer2
expanded = True
if name == "Spacer":
continue
if name == "DockbarX":
applet = self.dockbar.get_container()
expand = False
else:
appletscr = self.applets.get(name)
if appletscr is None:
continue
applet = appletscr.get_dbx_applet({"name":name, "dock":self})
expand = applet.get_expand() and panel
if expand:
expanded = True
box.pack_start(applet, expand, expand)
if panel and not expanded:
# The dockbar should expand if nothing else on the panel does.
self.__dockbar_set_expand(box, True)
expanded = True
return expanded
def __setup_boxes(self, alist, pos, panel):
# Three boxes (left/center/right or top/middle/bottom)
# are needed if the dock should contain centered applets,
# which happens if two spacers are used and panel mode is selected.
if alist.count("Spacer") == 2 and panel:
# Make three boxes and prepare them
if pos in ("left", "right"):
gtk_box = gtk.VBox
else:
gtk_box = gtk.HBox
boxes = [gtk_box(), gtk_box(),gtk_box()]
self.box.pack_start(boxes[0], True, True)
self.box.pack_start(boxes[1], False, False)
self.box.pack_start(boxes[2], True, True)
# Use hugh size requests so that the boxes get equaly large.
if pos in ("left", "right"):
boxes[0].set_size_request(-1, 3000)
boxes[2].set_size_request(-1, 3000)
else:
boxes[0].set_size_request(3000, -1)
boxes[2].set_size_request(3000, -1)
else:
boxes = None
return boxes
def __dockbar_set_expand(self, box, expand):
cp = box.query_child_packing(self.dockbar.get_container())
cp = (expand, expand, cp[2], cp[3])
box.set_child_packing(self.dockbar.get_container(), *cp)
def connect_applet_signals(self):
# Adds size-allocate signals for all applets.
self.applet_sids = {}
applets = self.__get_applets()
for app in applets:
if isinstance(app, DockXApplet) and not app.get_expand():
sid = app.connect("size-allocate", self.__on_applet_size_allocate)
self.applet_sids[app]=sid
def disconnect_applet_signals(self):
while self.applet_sids:
app, sid = self.applet_sids.popitem()
app.disconnect(sid)
def __update_applets(self):
applets = self.__get_applets()
for app in applets:
if isinstance(app, DockXApplet):
app.update()
def __get_applets(self):
# Returns a list of all applets on the dock.
applets = []
if self.position_boxes:
for box in self.position_boxes:
applets.extend(box)
else:
applets.extend(self.box)
return applets
def __set_dock_strut(self, x, y, w, h):
if not self.window:
return
set_strut = self.globals.settings["dock/behavior"] == "panel"
if not set_strut:
self.window.property_delete("_NET_WM_STRUT")
self.window.property_delete("_NET_WM_STRUT_PARTIAL")
return
s = self.get_screen()
sw = s.get_width()
sh = s.get_height()
mx, my, mw, mh = s.get_monitor_geometry(self.monitor)
if self.globals.settings["dock/position"] == "left":
strut = [x + w, 0, 0, 0, y, y + h - 1, 0, 0, 0, 0, 0, 0]
elif self.globals.settings["dock/position"] == "right":
strut = [0, sw - x, 0, 0, 0, 0, y, y + h - 1, 0, 0, 0, 0]
elif self.globals.settings["dock/position"] == "top":
strut = [0, 0, y + h, 0, 0, 0, 0, 0, x, x + w - 1, 0, 0]
else:
strut = [0, 0, 0, sh - y, 0, 0, 0, 0, 0, 0, x, x + w - 1]
self.window.property_change("_NET_WM_STRUT", "CARDINAL", 32,
gtk.gdk.PROP_MODE_REPLACE, strut[:4])
self.window.property_change("_NET_WM_STRUT_PARTIAL", "CARDINAL",
32, gtk.gdk.PROP_MODE_REPLACE, strut)
def __get_monitor_and_strut_borders(self):
# This function returns the distance from screen edges to
# the monitor edges including strut.
size = self.globals.settings["dock/size"]
mx, my, mw, mh = self.get_monitor_geometry()
sw = self.get_screen().get_width()
sh = self.get_screen().get_height()
strut = [mx, sw - (mx + mw), my, sh - (my + mh)]
d = display.Display()
strut_atom = d.get_atom('_NET_WM_STRUT')
strut_partial_atom = d.get_atom('_NET_WM_STRUT_PARTIAL')
root = d.screen().root
windows = root.query_tree()._data['children']
for w in windows:
try:
prop1 = w.get_full_property(strut_partial_atom, 0)
prop2 = w.get_full_property(strut_atom, 0)
except:
continue
if prop1 is not None:
cl = w.get_wm_class()
if cl and cl[0] in ("dockx", "dockbarx_factory"):
continue
if self.globals.settings["dock/position"] == "left":
if prop1.value[6] < my + mh and \
prop1.value[7] >= my:
strut[1] = max(strut[1], prop1.value[1])
if prop1.value[8] <= mx + size and \
prop1.value[9] >= mx:
strut[2] = max(strut[2], prop1.value[2])
if prop1.value[10] <= mx + size and \
prop1.value[11] >= mx:
strut[3] = max(strut[3], prop1.value[3])
elif self.globals.settings["dock/position"] == "right":
if prop1.value[4] < my + mh and \
prop1.value[5] >= my:
strut[0] = max(strut[0], prop1.value[0])
if prop1.value[8] < mx + mw and \
prop1.value[9] >= mx + mw - size:
strut[2] = max(strut[2], prop1.value[2])
if prop1.value[10] < mx + mw and \
prop1.value[11] >= mx + mw - size:
strut[3] = max(strut[3], prop1.value[3])
elif self.globals.settings["dock/position"] == "top":
if prop1.value[4] <= my + size and \
prop1.value[5] >= my:
strut[0] = max(strut[0], prop1.value[0])
if prop1.value[6] <= my + size and \
prop1.value[7] >= my:
strut[1] = max(strut[1], prop1.value[1])
if prop1.value[10] < mx + mw and \
prop1.value[11] >= mx:
strut[3] = max(strut[3], prop1.value[3])
else:
if prop1.value[4] < my + mh and \
prop1.value[5] >= my + mh - size:
strut[0] = max(strut[0], prop1.value[0])
if prop1.value[6] < my + mh and \
prop1.value[7] >= my + mh - size:
strut[1] = max(strut[1], prop1.value[1])
if prop1.value[8] < mx + mw and \
prop1.value[9] >= mx:
strut[2] = max(strut[2], prop1.value[2])
continue
if prop2 is not None:
# Hopefully this one is never needed because
# it won't work well with dualscreens.
cl = w.get_wm_class()
if cl and cl[0] in ("dockx", "dockbarx_factory"):
continue
for i in range(4):
strut[i] = max(strut[i], prop2.value[i])
return strut
def do_size_allocate(self, allocation):
CairoDockX.do_size_allocate(self, allocation)
w = allocation.width
h = allocation.height
if self.globals.settings["dock/mode"] == "centered":
mx, my, mw, mh = self.get_monitor_geometry()
if self.globals.settings["dock/position"] == "left":
x = mx
y = my + mh / 2 - h / 2
elif self.globals.settings["dock/position"] == "right":
x = mx + mw - w
y = my + mh / 2 - h / 2
elif self.globals.settings["dock/position"] == "top":
x = mx + mw / 2 - w / 2
y = my
else:
x = mx + mw / 2 - w / 2
y = my + mh - h
else:
x = self.old_x
y = self.old_y
self.move(x, y)
if (w, h) != (self.old_width, self.old_height):
self.queue_draw()
self.old_width, self.old_height = w, h
if self.globals.settings["dock/mode"] != "panel":
self.__set_dock_strut(x, y, w, h)
def show_dock(self):
self.show()
if self.globals.settings["dock/mode"] != "centered":
self.move(self.old_x, self.old_y)
if self.autohide_sid is not None:
gobject.source_remove(self.autohide_sid)
if self.autounhide_sid is not None:
gobject.source_remove(self.autounhide_sid)
self.autounhide_sid = None
self.autohide_sid = gobject.timeout_add(200, self.__hide_check)
def hide_dock(self):
self.hide()
if self.autohide_sid is not None:
gobject.source_remove(self.autohide_sid)
self.autohide_sid = None
if self.autounhide_sid is not None:
gobject.source_remove(self.autounhide_sid)
display = gtk.gdk.display_get_default()
mx, my, mw, mh = self.get_monitor_geometry()
if self.globals.settings["dock/position"] == "left":
x1 = x2 = mx
y1 = my
y2 = my + mh -1
elif self.globals.settings["dock/position"] == "right":
x1 = x2 = mx + mw - 1
y1 = my
y2 = my + mh - 1
elif self.globals.settings["dock/position"] == "top":
x1 = mx
x2 = mx + mw - 1
y1 = y2 = my
else:
x1 = mx
x2 = mx + mw - 1
y1 = y2 = my + mh - 1
self.autounhide_sid = gobject.timeout_add(200, self.__unhide_check,
display, x1, x2, y1, y2)
def __unhide_check(self, display, x1, x2, y1, y2):
if not self.should_autohide:
self.show_dock()
return False
s, x, y, mod = display.get_pointer()
if y >= y1 and y <= y2 and x >= x1 and x <= x2:
self.show_dock()
return False
return True
def __hide_check(self):
if self.globals.shown_popup() is not None or not self.should_autohide:
return True
pos = self.globals.settings["dock/position"]
x, y = self.get_pointer()
a = self.get_allocation()
if x >= 0 and x < a.width and y >= 0 and y < a.height:
return True
if (pos == "left" and x == 0) or \
(pos == "right" and x == a.width - 1) or \
(pos == "top" and y == 0) or \
(pos == "bottom" and y == a.height - 1):
return True
self.hide_dock()
return False
def add_window(self, window, reset_should_autohide=True):
if not self.autohide:
return
geo_sid = window.connect("geometry-changed",
self.__on_window_geometry_changed)
state_sid = window.connect("state-changed",
self.__on_window_state_changed)
self.windows[window] = (geo_sid, state_sid)
self.__calc_border_distance(window)
if self.db_loaded and reset_should_autohide:
self.__compute_should_autohide()
def remove_window(self, window, reset_should_autohide=True, forced=False):
if not self.autohide and not forced:
return
try:
del self.border_distances[window]
except KeyError:
pass
if window in self.windows:
sids = self.windows.pop(window)
if sids is not None:
window.disconnect(sids[0])
window.disconnect(sids[1])
if reset_should_autohide:
self.__compute_should_autohide()
def __on_window_state_changed(self, wnck_window,changed_mask, new_state):
if WNCK_WINDOW_STATE_MINIMIZED & changed_mask:
self.__compute_should_autohide()
def __on_window_geometry_changed(self, window):
if time.time() - self.geometry_time < 0.12 and \
window == self.last_geometry_window():
# Same window get multiple calls when the geometry changes
# In that case, just return.
return
self.last_geometry_window = weakref.ref(window)
self.geometry_time = time.time()