2 |
from ScrolledText import * |
from ScrolledText import * |
3 |
from string import replace, find |
from string import replace, find |
4 |
|
|
5 |
class Chat(Frame): |
class Chat: |
6 |
|
|
7 |
|
inited = 0 |
8 |
|
|
9 |
def __init__(self, master, telltarget, tellhandler): |
def __init__(self, master, telltarget, tellhandler): |
|
Frame.__init__(self, master) |
|
10 |
|
|
11 |
self.telltarget = telltarget # room number or name of other person in chat |
self.telltarget = telltarget # room number or name of other person in chat |
12 |
self.tellhandler = tellhandler # Tell or RoomTell |
self.tellhandler = tellhandler # Tell or RoomTell |
13 |
|
|
14 |
|
self.taggedtext = [] |
15 |
|
|
16 |
|
self.sendhistory = [] |
17 |
|
self.historypos = -1 |
18 |
|
|
19 |
|
self.reparent(master) |
20 |
|
|
21 |
|
def reparent(self, master): |
22 |
|
self.master = master |
23 |
|
self.frame = Frame(self.master) |
24 |
|
|
25 |
self.output = ScrolledText(self, state=DISABLED) |
self.output = ScrolledText(self.frame, state=DISABLED) |
26 |
outputfont = "Arial 10" |
outputfont = "Arial 10" |
27 |
self.output.tag_config("normal", font=outputfont) |
self.output.tag_config("normal", font=outputfont) |
28 |
self.output.tag_config("name", font=outputfont + " bold") |
self.output.tag_config("name", font=outputfont + " bold") |
29 |
self.output.tag_config("action", font=outputfont + " italic") |
self.output.tag_config("action", font=outputfont + " italic") |
30 |
self.output.grid(row=0, sticky=NE+SW) |
self.output.grid(row=0, sticky=NE+SW) |
31 |
|
|
32 |
self.input = Entry(self) |
self.input = Entry(self.frame) |
33 |
self.input.bind("<Return>", self.send) |
self.input.bind("<Return>", self.send) |
34 |
|
self.input.bind("<Up>", self.scroll_history_back) |
35 |
|
self.input.bind("<Down>", self.scroll_history_forward) |
36 |
self.input.grid(row=1, sticky=E+W) |
self.input.grid(row=1, sticky=E+W) |
37 |
|
|
38 |
self.rowconfigure(0,weight=1) |
self.frame.rowconfigure(0,weight=1) |
39 |
self.columnconfigure(0,weight=1) |
self.frame.columnconfigure(0,weight=1) |
40 |
|
|
41 |
|
if self.inited: |
42 |
|
# copy old contents |
43 |
|
for text in self.taggedtext: |
44 |
|
self.add_chat(text[0], text[1], 0) |
45 |
|
else: |
46 |
|
self.inited = 1 |
47 |
|
|
48 |
|
def scroll_history_back(self, event): |
49 |
|
if self.historypos: |
50 |
|
if self.historypos == -1: |
51 |
|
self.historypos = len(self.sendhistory)-1 |
52 |
|
elif self.historypos > 0: |
53 |
|
self.historypos -= 1 |
54 |
|
self.input.delete(0, END) |
55 |
|
self.input.insert(0, self.sendhistory[self.historypos]) |
56 |
|
|
57 |
|
def scroll_history_forward(self, event): |
58 |
|
if self.historypos < 0: |
59 |
|
return |
60 |
|
if self.historypos < len(self.sendhistory)-1: |
61 |
|
self.historypos += 1 |
62 |
|
self.input.delete(0, END) |
63 |
|
self.input.insert(0, self.sendhistory[self.historypos]) |
64 |
|
elif self.historypos == len(self.sendhistory)-1: |
65 |
|
self.historypos = -1 |
66 |
|
self.input.delete(0, END) |
67 |
|
|
68 |
def send(self, event): |
def send(self, event): |
69 |
speech = self.input.get() |
speech = self.input.get() |
70 |
if speech != "": |
if speech != "": |
71 |
self.tellhandler(self.telltarget, speech) |
self.tellhandler(self.telltarget, speech) |
72 |
|
self.sendhistory.append(speech) |
73 |
|
self.historypos = -1 |
74 |
self.input.delete(0, END) |
self.input.delete(0, END) |
75 |
|
|
76 |
def receive(self, source, speech): |
def receive(self, source, speech): |
|
self.output.config(state=NORMAL) |
|
77 |
# check for action (string beginning with "/me") |
# check for action (string beginning with "/me") |
78 |
action = 0 |
action = 0 |
79 |
if find(speech, "/me", 0, 3) > -1: |
if find(speech, "/me", 0, 3) > -1: |
81 |
# replace "/me" with user's name |
# replace "/me" with user's name |
82 |
speech = replace(speech, "/me", source) |
speech = replace(speech, "/me", source) |
83 |
if not action: |
if not action: |
84 |
self.output.insert(END, source + ": ", "name") |
self.add_chat(source + ": ", "name") |
85 |
self.output.insert(END, speech + "\n", "normal") |
self.add_chat(speech + "\n", "normal") |
86 |
else: |
else: |
87 |
self.output.insert(END, "***" + speech + "\n", "action") |
self.add_chat("***" + speech + "\n", "action") |
88 |
|
|
89 |
|
def add_chat(self, text, tag, save=1): |
90 |
|
self.output.config(state=NORMAL) |
91 |
|
# save text and tag |
92 |
|
if save: |
93 |
|
self.taggedtext.append([text, tag]) |
94 |
|
# add to widget |
95 |
|
self.output.insert(END, text, tag) |
96 |
self.output.config(state=DISABLED) |
self.output.config(state=DISABLED) |
97 |
|
|
98 |
|
# grid manager widget methods (forward on to self.frame) |
99 |
|
|
100 |
|
def grid(self,column=0,columnspan=1,in_=None,ipadx=0,ipady=0, |
101 |
|
padx=0,pady=0,row=None,rowspan=1,sticky=None): |
102 |
|
if in_ is None: |
103 |
|
in_ = self.master |
104 |
|
self.frame.grid(column=column,columnspan=columnspan,in_=in_, |
105 |
|
ipadx=ipadx,ipady=ipady,padx=padx,pady=pady,row=row, |
106 |
|
rowspan=rowspan,sticky=sticky) |
107 |
|
|
108 |
|
def grid_configure(self,column=0,columnspan=1,in_=None,ipadx=0,ipady=0, |
109 |
|
padx=0,pady=0,row=None,rowspan=1,sticky=None): |
110 |
|
if in_ is None: |
111 |
|
in_ = self.master |
112 |
|
self.frame.grid_configure(column=column,columnspan=columnspan,in_=in_, |
113 |
|
ipadx=ipadx,ipady=ipady,padx=padx,pady=pady, |
114 |
|
row=row,rowspan=rowspan,sticky=sticky) |
115 |
|
|
116 |
|
def grid_forget(self): |
117 |
|
self.frame.grid_forget() |
118 |
|
|
119 |
|
def grid_info(self): |
120 |
|
return self.frame.grid_info() |
121 |
|
|
122 |
|
def grid_remove(self): |
123 |
|
self.frame.grid_remove() |