This repository has been archived by the owner on Jun 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HorizontalBoxView.py
93 lines (75 loc) · 2.8 KB
/
HorizontalBoxView.py
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
# Copyright (C) 2009-2011 AG Projects. See LICENSE for details.
#
from Foundation import (NSView,
NSWidth,
NSHeight,
NSTextField)
from AppKit import NSRectFill
class HorizontalBoxView(NSView):
def initWithFrame_(self, frame):
self = super(HorizontalBoxView, self).initWithFrame_(frame)
if self:
self.background_color = None
self.spacing = 0
self.border = 0
self.expandingViews = set()
return self
def setViewExpands(self, view, flag = True):
if flag and not view in self.expandingViews:
self.expandingViews.add(view)
elif not flag and view in self.expandingViews:
self.expandingViews.remove(view)
def setSpacing_(self, spacing):
self.spacing = spacing
def setBorderWidth_(self, border):
self.border = border
def didAddSubview_(self, subview):
minimumWidth = self.spacing * (self.subviews().count()-1) + self.border*2
expandCount = 0
for view in self.subviews():
if view in self.expandingViews:
expandCount += 1
else:
minimumWidth += NSWidth(view.frame())
frame = self.frame()
if NSWidth(frame) != minimumWidth:
frame.size.width = minimumWidth
self.setFrame_(frame)
def isFlipped(self):
return True
def setBackgroundColor_(self, color):
self.background_color = color
self.setNeedsDisplay_(True)
def drawRect_(self, rect):
if self.background_color:
self.background_color.set()
NSRectFill(rect)
#NSColor.redColor().set()
#NSFrameRect(rect)
def resizeSubviewsWithOldSize_(self, oldSize):
frame = self.frame()
height = NSHeight(frame) - 2 * self.border
expandCount = 0
minimumWidth = 2 * self.border
for view in self.subviews():
if view in self.expandingViews:
expandCount += 1
else:
minimumWidth += NSWidth(view.frame())
minimumWidth += self.spacing
expandedWidth= 0
if expandCount > 0:
expandedWidth = int((NSWidth(frame) - minimumWidth) / expandCount)
x = self.border
for view in self.subviews():
rect = view.frame()
if not view.isKindOfClass_(NSTextField.class__()):
rect.origin.y = self.border
rect.size.height = height
else:
rect.origin.y = self.border + (height - NSHeight(rect)) / 2
rect.origin.x = x
if view in self.expandingViews:
rect.size.width = expandedWidth
view.setFrame_(rect)
x += NSWidth(rect) + self.spacing