-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgui.erl
371 lines (299 loc) · 13.1 KB
/
gui.erl
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
-module(gui).
-export([start/0, init/1, terminate/2, code_change/3,
handle_info/2, handle_call/3, handle_cast/2, handle_event/2]).
-behaviour(wx_object).
-include_lib("wx/include/wx.hrl").
%% File with records definitions to be used as state for the client
-include_lib("./defs.hrl").
-define(SYSTEM, 0).
-define(CMDLINE, 1).
-define(NOTEBOOK, 2).
-define(MAX_CONNECTIONS, 100000).
-define(SYSTEM_NAME, "System").
% This record defines the structure of the
% client process.
%
% It contains the fields:
%
% parent: it stores the top window (used with operations
% related to display widgets in the GUI.
% gui: it stores the name the GUI process.
% client: it stores the name of the client process.
-record(state,
{
parent,
client,
gui,
clientid % unique part of name, as integer
}).
start() ->
Server = wx:new(),
wx_object:start_link(?MODULE, Server, []).
init(Server) ->
wx:batch(fun () ->
do_init(Server) end ).
do_init(Server) ->
% It creates a unique name for the client and gui processes
{ClientName, ClientID} = find_unique_name("client_", ?MAX_CONNECTIONS),
{GUIName,_ } = find_unique_name("gui_", ?MAX_CONNECTIONS),
% If any of the name choosen above are taken at this point, everything crashes!
register(to_atom(GUIName), self()),
genserver:start(to_atom(ClientName), client:initial_state("user01", GUIName), fun client:loop/2),
%% Starting GUI
Frame = wxFrame:new(Server, -1, "Chat", []),
Parent = Frame,
Panel = wxPanel:new(Parent, []),
%% Widgets: command line and system tab
Cmd = wxTextCtrl:new(Panel, -1, [{value, ""}, {style, ?wxTE_PROCESS_ENTER}]),
label(ClientID, ?CMDLINE, Cmd),
Ntbk = wxAuiNotebook:new(Panel,[{style,?wxAUI_NB_DEFAULT_STYLE}]),
label(ClientID, ?NOTEBOOK, Ntbk),
Tab = create_tab(ClientName, ?SYSTEM_NAME, "Welcome to CCHAT v. 0.2"),
label(ClientID, ?SYSTEM, Tab),
%% Sizers
MainSizer = wxBoxSizer:new(?wxVERTICAL),
wxSizer:add(MainSizer, Ntbk, [{flag, ?wxEXPAND}, {proportion,1}]),
wxSizer:addSpacer(MainSizer,10),
wxSizer:add(MainSizer, Cmd, [{flag, ?wxEXPAND}]),
wxPanel:setSizer(Panel, MainSizer),
wxFrame:show(Frame),
focus(with_label(ClientName, ?CMDLINE)),
wxTextCtrl:connect(Cmd, command_text_enter),
wxAuiNotebook:connect(Ntbk, command_auinotebook_button),
trace(["Client:", ClientName]),
trace(["GUI:", GUIName]),
{Panel, #state{parent=Panel, client=ClientName, clientid=ClientID, gui=GUIName}}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Async Events are handled in handle_event as in handle_info
handle_event(#wx{ event = #wxCommand{type = command_text_enter, cmdString = Item} },
St = #state{ parent = Panel, client = ClientName, clientid = ClientID }) ->
clear_text(with_label(ClientName, ?CMDLINE)),
Cmd = lexgrm:parse_cmd(Item),
trace(["Command:", Cmd]),
case Cmd of
%% Connecting to the server (Specifying the remote machine)
{connect_remote, Server, Machine} ->
write_channel(with_label(ClientName, ?SYSTEM), "* "++"Trying to connect to "++Server++" in machine "++Machine++"..."),
Result = catch_fatal (ClientName, Panel, fun () -> request(ClientName, {connect, {Server, Machine}}) end ),
case Result of
ok -> write_channel(with_label(ClientName, ?SYSTEM), "+ Connected!") ;
error -> ok
end ;
%% Connecting to the server
{connect, Server} ->
write_channel(with_label(ClientName, ?SYSTEM), "* "++"Trying to connect to "++Server++"..."),
Result = catch_fatal (ClientName, Panel, fun () -> request(ClientName, {connect, Server}) end ),
case Result of
ok -> write_channel(with_label(ClientName, ?SYSTEM), "+ Connected!") ;
error -> ok
end ;
%% Disconnect from the server
disconnect ->
Result = catch_fatal (ClientName, Panel, fun () -> request(ClientName, disconnect) end ),
case Result of
ok -> write_channel(with_label(ClientName, ?SYSTEM), "+ Disconnected") ;
error -> ok
end ;
%% Joining a new channel
{join, Channel} ->
Result = catch_fatal(ClientName, Panel, fun () -> request(ClientName, {join, Channel}) end ),
case Result of
ok -> write_channel(with_label(ClientName, ?SYSTEM), "+ Joined "++Channel),
Tab = create_tab(ClientName, Channel, "* Channel "++Channel),
label(ClientID, channel_id(Channel), Tab) ;
error -> ok
end ;
% /leave
leave -> Channel = active_channel(with_label(ClientName,?NOTEBOOK)),
leave_channel(ClientName, Panel, Channel) ;
% /leave #channel
{leave, Channel} -> leave_channel(ClientName, Panel, Channel) ;
%% Sending a message
{msg, String} ->
Channel = active_channel(with_label(ClientName,?NOTEBOOK)),
case Channel of
?SYSTEM_NAME -> write_channel(with_label(ClientName, ?SYSTEM), "- "++"Command not recognized: "++String) ;
_ -> Result = catch_fatal(ClientName, Panel,
fun () -> request(ClientName, {msg_from_GUI, Channel, String}) end ),
case Result of
ok -> write_channel(with_label(ClientName, channel_id(Channel)), String) ;
error -> ok
end
end ;
%% Who am I?
whoami -> Result = catch_fatal(ClientName, Panel, fun () -> request(ClientName, whoami) end),
case Result of
error -> ok ;
Nick -> write_channel(with_label(ClientName, ?SYSTEM), "* "++"You are "++Nick)
end ;
%% Change nickname
{nick, Nick} -> Result = catch_fatal(ClientName, Panel, fun () -> request(ClientName,{nick, Nick}) end ),
case Result of
ok -> write_channel( with_label(ClientName, ?SYSTEM),
"* "++"You are known now as "++Nick) ;
error -> ok
end ;
%% Ping someone
{ping, Nick} -> Result = catch_fatal(ClientName, Panel, fun () -> request(ClientName,{ping, Nick}) end ),
case Result of
ok -> write_channel( with_label(ClientName, ?SYSTEM),
"* "++"Ping "++Nick) ;
error -> ok
end ;
%% The given command was wrong
{ignore, Line} ->
write_channel(with_label(ClientName, ?SYSTEM), "- "++"Command not recognized"),
write_channel(with_label(ClientName, ?SYSTEM), Line)
end,
focus(with_label(ClientName, ?CMDLINE)),
{noreply, St} ;
% Clicking the X on a tab
handle_event(#wx{ event = #wxAuiNotebook{type = command_auinotebook_button, selection = TabPos} },
St = #state{ parent = Panel, client = ClientName }) ->
Ntbk = typed_search(with_label(ClientName, ?NOTEBOOK), wxAuiNotebook),
Channel = wxAuiNotebook:getPageText(Ntbk,TabPos),
leave_channel(ClientName, Panel, Channel),
{noreply, St} ;
handle_event(WX = #wx{}, State = #state{}) ->
io:format("#wx: ~p~n",[WX]),
io:format("#state: ~p~n",[State]),
{noreply, State}.
%% Callbacks handled as normal gen_server callbacks (not used)
handle_info(_Msg, State) ->
{noreply, State}.
handle_call(shutdown, _From, State) ->
{stop, normal, ok, State};
%% Here, the GUI receives a message from the client process!
handle_call({msg_to_GUI, Channel, Msg}, _From, State = #state{ client = ClientName }) ->
write_channel( with_label(ClientName, channel_id(Channel)), Msg),
{reply, ok, State} ;
%% Here, the GUI receives a message to the system tab
handle_call({msg_to_SYSTEM, Msg}, _From, State = #state{ client = ClientName }) ->
write_channel(with_label(ClientName, ?SYSTEM), "* "++Msg),
{reply, ok, State} ;
handle_call(_Msg, _From, State) ->
{reply, {error,nyi}, State}.
handle_cast(_Msg, State) ->
{noreply,State}.
code_change(_, _, State) ->
{stop, ignore, State}.
terminate(_Reason, _State) ->
ok.
%% Auxiliary functions
%% Finding an unique name
find_unique_name(Prefix,N) ->
Num = random:uniform(N),
MStr = integer_to_list(Num),
Name = Prefix++MStr,
case whereis(to_atom(Name)) of
undefined -> {Name, Num} ;
_ -> find_unique_name(Prefix,N)
end.
%% Debugging
trace(Args) ->
io:format("~n~s"++lists:flatten(lists:duplicate(length(Args)-1,"~p~n")),Args).
%% GUI
clear_text(ID) ->
CmdLine = typed_search(ID, wxTextCtrl),
wxTextCtrl:setValue(CmdLine, "").
fatal_dialog(Parent, Error) ->
StrError = lists:flatten(io_lib:format("~p",[Error])),
Msg = "Something went very wrong!\n\n" ++ StrError,
WW = wxMessageDialog:new(Parent, Msg,
[{style, ?wxSTAY_ON_TOP bor ?wxICON_ERROR bor ?wxOK}]),
wxDialog:showModal(WW),
exit(StrError).
create_tab(ClientName, Title, Init) ->
Ntbk = typed_search(with_label(ClientName, ?NOTEBOOK), wxAuiNotebook),
NtbkPanel = wxPanel:new(Ntbk, []),
Msgs = wxTextCtrl:new(NtbkPanel, -1,
[{value, Init},
{style, ?wxTE_MULTILINE}]),
wxTextCtrl:setEditable(Msgs, false),
wxTextCtrl:setInsertionPointEnd(Msgs),
NtbkSizer = wxBoxSizer:new(?wxVERTICAL),
wxSizer:addSpacer(NtbkSizer, 10),
wxSizer:add(NtbkSizer, Msgs, [{flag, ?wxEXPAND}, {proportion,1}]),
wxPanel:setSizer(NtbkPanel, NtbkSizer),
wxAuiNotebook:addPage(Ntbk,NtbkPanel,Title),
wxWindow:setFocus(NtbkPanel),
Msgs.
active_channel(ID) ->
Ntbk = typed_search(ID, wxAuiNotebook),
PageNumber = wxAuiNotebook:getSelection(Ntbk),
Title = wxAuiNotebook:getPageText(Ntbk, PageNumber),
Title.
close_tab(NotebookID, TabName) ->
Ntbk = typed_search(NotebookID, wxAuiNotebook),
Max = wxAuiNotebook:getPageCount(Ntbk),
Tabs = [ {wxAuiNotebook:getPageText(Ntbk,N), N} || N <- lists:seq(0,Max-1) ],
{_, PageNumber} = lists:keyfind(TabName, 1, Tabs),
Page = wxAuiNotebook:getPage(Ntbk,PageNumber),
wxWindow:destroyChildren(Page),
wxAuiNotebook:removePage(Ntbk, PageNumber).
write_channel(ID, String) ->
DMesg = typed_search(ID,wxTextCtrl),
wxTextCtrl:writeText(DMesg, "\n"++String).
%% Labels
focus(ID) ->
W = wxWindow:findWindowById(ID),
wxWindow:setFocus(W).
typed_search(ID, no_cast) ->
Result = wxWindow:findWindowById(ID),
% io:format("Looking for label ~p and found ~p~n",[ID,Result]),
Result ;
typed_search(ID, Cast) ->
Result = wxWindow:findWindowById(ID),
{F1, F2, _, F3} = Result,
% io:format("Looking for label ~p and found ~p~n",[ID,{F1,F2,Cast,F3}]),
{F1,F2,Cast,F3}.
label(ClientID, ID, Widget) ->
Label = join_ids(ClientID, ID),
_Result = wxWindow:setId(Widget, Label),
% io:format("Setting label ~p to widget ~p, result ~p~n",[Label,Widget, Result]),
ok.
% with_label("client_123", 9) = 1239
with_label(ClientName, Id) ->
CIds = lists:sublist(ClientName,8,5),
S = lists:flatten(io_lib:format("~s~p", [CIds, Id])),
{N, _} = string:to_integer(S),
N.
% join_ids(123,456) = 123456
join_ids(ClientId, Id) ->
S = lists:flatten(io_lib:format("~p~p", [ClientId, Id])),
{N, _} = string:to_integer(S),
N.
% Concats ASCII codes for each character into a mega integer
% channel_id("AAA") = 656565
channel_id(ChannelName) ->
S = lists:foldl(fun(S, Acc) -> io_lib:format("~p", [S])++Acc end, "", ChannelName),
list_to_integer(lists:flatten(S)).
% client_id("client_1234") = 1234
% client_id(ClientName) ->
% {N, _} = string:to_integer(lists:sublist(ClientName,8,5)),
% N.
%% Requests
request(ClientName, Msg) ->
genserver:request(to_atom(ClientName), Msg, 100000). % must be greater than default timeout
%% Errors
catch_fatal(ClientName, Panel, Cmd) ->
case catch( Cmd() ) of
{'EXIT',Reason} -> fatal_dialog(Panel, Reason) ;
{error, _, Msg} -> write_channel(with_label(ClientName, ?SYSTEM), "- Error: "++Msg),
error ;
Result -> Result
end.
to_atom(String) ->
list_to_atom(String).
%% Leave a channel
leave_channel(ClientName, Panel, Channel) ->
case Channel of
?SYSTEM_NAME -> write_channel(with_label(ClientName, ?SYSTEM), "- "++"Cannot leave System tab") ;
Channel -> Result = catch_fatal(ClientName, Panel,
fun () -> request(ClientName, {leave, Channel}) end ),
case Result of
ok -> close_tab(with_label(ClientName, ?NOTEBOOK), Channel),
write_channel(with_label(ClientName, ?SYSTEM), "* "++"Left "++Channel) ;
error -> ok
end
end.