1 |
# (c) Tuomas J. Lukka |
# (c) Tuomas J. Lukka |
2 |
|
|
3 |
|
# The key event handlers do not have the same restrictions |
4 |
|
# as the event handlers for mouse events -- no need to implement |
5 |
|
# any particular interface. |
6 |
|
|
7 |
|
# Therefore, we'll just use a single class. |
8 |
|
|
9 |
|
from org.fenfire.swamp import Nodes |
10 |
|
from org import fenfire as ff |
11 |
|
from org.fenfire.util import RDFUtil |
12 |
|
|
13 |
|
dbg = 0 |
14 |
|
|
15 |
|
def p(*s): |
16 |
|
print 'ff.fenpdf.events.key::', s |
17 |
|
|
18 |
|
class KeyActions: |
19 |
|
def __init__(self, context): |
20 |
|
self.context = context |
21 |
|
def insertChar(self, main, char): |
22 |
|
"""Insert a character to the accursed node - no creation. |
23 |
|
|
24 |
|
If no node is accursed, will do nothing. |
25 |
|
""" |
26 |
|
cur = self.context.states.cursor |
27 |
|
pp = self.context.states.ppActions |
28 |
|
|
29 |
|
acc = Nodes.toString(cur.getAccursed()) |
30 |
|
|
31 |
|
if dbg: p( "Typing: ", cur, cur.getAccursed()) |
32 |
|
|
33 |
|
offs = cur.getCursorOffset() |
34 |
|
|
35 |
|
pp.insertText(acc, offs, char) |
36 |
|
cur.setCursorOffset(offs + 1) |
37 |
|
|
38 |
|
|
39 |
|
def insertCharOrCreate(self, main, char): |
40 |
|
"""Insert a character to the accursed node. |
41 |
|
|
42 |
|
If no node is accursed, create a new node at cursor. |
43 |
|
""" |
44 |
|
cur = self.context.states.cursor |
45 |
|
pp = self.context.states.ppActions |
46 |
|
|
47 |
|
if cur.getAccursed() == None: |
48 |
|
paper = Nodes.toString(main.getPlane()) |
49 |
|
note = pp.newNote(paper, int(main.getFocus().getPanX()), |
50 |
|
int(main.getFocus().getPanY()), "") |
51 |
|
cur.setAccursed(Nodes.get(note)) |
52 |
|
|
53 |
|
self.insertChar(main, char) |
54 |
|
|
55 |
|
def moveInsertionCursor(self, main, n): |
56 |
|
"""Move the text insertion cursor by n. |
57 |
|
|
58 |
|
The number n may be positive or negative. |
59 |
|
If no node is accursed, will do nothing. |
60 |
|
""" |
61 |
|
cur = self.context.states.cursor |
62 |
|
acc = cur.getAccursed() |
63 |
|
if acc == None: return |
64 |
|
|
65 |
|
# XXX !!! |
66 |
|
alphContent = ff.util.AlphContent(self.context.states.fen) |
67 |
|
|
68 |
|
offs = cur.getCursorOffset() + n |
69 |
|
if offs < 0: offs = 0 |
70 |
|
l = len(alphContent.getText(acc)) |
71 |
|
|
72 |
|
if offs > l: offs = l |
73 |
|
|
74 |
|
cur.setCursorOffset(offs) |
75 |
|
|
76 |
|
def backspace(self, main): |
77 |
|
cur = self.context.states.cursor |
78 |
|
pp = self.context.states.ppActions |
79 |
|
|
80 |
|
|
81 |
|
acc = cur.getAccursed() |
82 |
|
if acc == None: return |
83 |
|
offs = cur.getCursorOffset() |
84 |
|
|
85 |
|
note = Nodes.toString(acc) |
86 |
|
|
87 |
|
# XXX !!! |
88 |
|
alphContent = ff.util.AlphContent(self.fenPDF.fen) |
89 |
|
|
90 |
|
if offs >= 1: |
91 |
|
p('delete:', offs) |
92 |
|
pp.deleteText(note, offs - 1, offs) |
93 |
|
offs = cur.setCursorOffset(offs - 1) |
94 |
|
text = alphContent.getText(self.fenPDF.fen.constgraph, acc) |
95 |
|
if offs == 0 and len(text) == 0: |
96 |
|
# If text went to nothingness |
97 |
|
if RDFUtil.isLinked(self.fenPDF.fen.graph, acc): |
98 |
|
# Kludge workaround: insert # if it is linked |
99 |
|
pp.insertText(note, 0, "#") |
100 |
|
else: |
101 |
|
# Delete the note |
102 |
|
pp.deleteNote(note) |
103 |
|
cur.setAccursed(None) |
104 |
|
|
105 |
|
|
106 |
|
# XXX Not implemented |
107 |
|
def toggleBgTextureUse(self, value = -1): |
108 |
|
p("Not implemented") |
109 |
|
pass |
110 |
|
|
111 |
|
def changeBgPaperMaker(self): |
112 |
|
p("Not implemented") |
113 |
|
pass |