summaryrefslogtreecommitdiffstats
path: root/Tools/idle/Separator.py
blob: 2645e57a37e4c5d6e9070f2c9a8ee843fc7006b6 (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
from Tkinter import *

class Separator:
    
    def __init__(self, master, orient, min=10, thickness=5, bg=None):
        self.min = max(1, min)
        self.thickness = max(1, thickness)
        if orient in ("h", "horizontal"):
            self.side = "left"
            self.dim = "width"
            self.dir = "x"
            self.cursor = "sb_h_double_arrow"
    	elif orient in ("v", "vertical"):
    	    self.side = "top"
    	    self.dim = "height"
    	    self.dir = "y"
            self.cursor = "sb_v_double_arrow"
    	else:
    	    raise ValueError, "Separator: orient should be h or v"
    	self.winfo_dim = "winfo_" + self.dim
        self.master = master = Frame(master)
        master.pack(expand=1, fill="both")
        self.f1 = Frame(master)
        self.f1.pack(expand=1, fill="both", side=self.side)
        self.div = Frame(master, cursor=self.cursor)
        self.div[self.dim] = self.thickness
        self.div.pack(fill="both", side=self.side)
        self.f2 = Frame(master)
        self.f2.pack(expand=1, fill="both", side=self.side)
        self.div.bind("<ButtonPress-1>", self.divider_press)
        if bg:
            ##self.f1["bg"] = bg
            ##self.f2["bg"] = bg
            self.div["bg"] = bg

    def parts(self):
        return self.f1, self.f2

    def divider_press(self, event):
        self.press_event = event
        self.f1.pack_propagate(0)
        self.f2.pack_propagate(0)
        for f in self.f1, self.f2:
            for dim in "width", "height":
                f[dim] = getattr(f, "winfo_"+dim)()
        self.div.bind("<Motion>", self.div_motion)
        self.div.bind("<ButtonRelease-1>", self.div_release)
        self.div.grab_set()

    def div_motion(self, event):
        delta = getattr(event, self.dir) - getattr(self.press_event, self.dir)
        if delta:
            dim1 = getattr(self.f1, self.winfo_dim)()
            dim2 = getattr(self.f2, self.winfo_dim)()
            delta = max(delta, self.min-dim1)
            delta = min(delta, dim2-self.min)
            dim1 = dim1 + delta
            dim2 = dim2 - delta
            self.f1[self.dim] = dim1
            self.f2[self.dim] = dim2

    def div_release(self, event):
        self.div_motion(event)
        self.div.unbind("<Motion>")
        self.div.grab_release()

class VSeparator(Separator):

    def __init__(self, master, min=10, thickness=5, bg=None):
        Separator.__init__(self, master, "v", min, thickness, bg)

class HSeparator(Separator):

    def __init__(self, master, min=10, thickness=5, bg=None):
        Separator.__init__(self, master, "h", min, thickness, bg)

def main():
    root = Tk()
    tlist = []
    outer = HSeparator(root, bg="red")
    for part in outer.parts():
        inner = VSeparator(part, bg="blue")
        for f in inner.parts():
            t = Text(f, width=40, height=10, borderwidth=0)
            t.pack(fill="both", expand=1)
            tlist.append(t)
    tlist[0].insert("1.0", "Make your own Mondrian!")
    tlist[1].insert("1.0", "Move the colored dividers...")
    root.mainloop()

if __name__ == '__main__':
    main()