forked from limetext/lime
/
window.go
106 lines (90 loc) · 2.07 KB
/
window.go
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
// Copyright 2013 The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package backend
import (
"code.google.com/p/log4go"
"github.com/quarnster/util/text"
"io/ioutil"
"runtime/debug"
"sync"
)
type Window struct {
text.HasId
text.HasSettings
views []*View
active_view *View
lock sync.Mutex
}
func (w *Window) NewFile() *View {
w.lock.Lock()
defer w.lock.Unlock()
w.views = append(w.views, newView(w))
v := w.views[len(w.views)-1]
v.Settings().SetParent(w)
v.setBuffer(text.NewBuffer())
v.selection.Clear()
v.selection.Add(text.Region{0, 0})
OnNew.Call(v)
w.SetActiveView(v)
return v
}
func (w *Window) Views() []*View {
w.lock.Lock()
defer w.lock.Unlock()
ret := make([]*View, len(w.views))
copy(ret, w.views)
return ret
}
func (w *Window) remove(v *View) {
w.lock.Lock()
defer w.lock.Unlock()
for i, vv := range w.views {
if v == vv {
end := len(w.views) - 1
if i != end {
copy(w.views[i:], w.views[i+1:])
}
w.views = w.views[:end]
return
}
}
log4go.Error("Wanted to remove view %+v, but it doesn't appear to be a child of this window", v)
}
func (w *Window) OpenFile(filename string, flags int) *View {
v := w.NewFile()
v.SetScratch(true)
e := v.BeginEdit()
v.Buffer().SetFileName(filename)
if d, err := ioutil.ReadFile(filename); err != nil {
log4go.Error("Couldn't load file %s: %s", filename, err)
} else {
v.Insert(e, 0, string(d))
}
v.EndEdit(e)
v.SetScratch(false)
OnLoad.Call(v)
return v
}
func (w *Window) SetActiveView(v *View) {
// w.lock.Lock()
// defer w.lock.Unlock()
if w.active_view != nil {
OnDeactivated.Call(w.active_view)
}
w.active_view = v
if w.active_view != nil {
OnActivated.Call(w.active_view)
}
}
func (w *Window) ActiveView() *View {
return w.active_view
}
func (w *Window) runCommand(c WindowCommand, name string) error {
defer func() {
if r := recover(); r != nil {
log4go.Error("Paniced while running window command %s %v: %v\n%s", name, c, r, string(debug.Stack()))
}
}()
return c.Run(w)
}