forked from raduprv/Eternal-Lands
-
Notifications
You must be signed in to change notification settings - Fork 3
/
chat.c
2523 lines (2254 loc) · 72 KB
/
chat.c
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <libxml/parser.h>
#include "chat.h"
#include "asc.h"
#include "colors.h"
#include "console.h"
#include "consolewin.h"
#include "elconfig.h"
#include "errors.h"
#include "gamewin.h"
#include "global.h"
#include "hud.h"
#include "init.h"
#include "interface.h"
#include "mapwin.h"
#include "multiplayer.h"
#include "queue.h"
#include "text.h"
#include "translate.h"
#ifdef OPENGL_TRACE
#include "gl_init.h"
#endif
#include "io/elfilewrapper.h"
#include "io/elpathwrapper.h"
#include "sound.h"
int chat_win = -1;
void remove_chat_tab (Uint8 channel);
int add_chat_tab (int nlines, Uint8 channel);
void clear_chat_tabs_updated();
void update_chat_tab_idx (Uint8 old_ix, Uint8 new_idx);
void remove_tab_button (Uint8 channel);
int add_tab_button (Uint8 channel);
void clear_tab_buttons_updated();
void update_tab_button_idx (Uint8 old_idx, Uint8 new_idx);
void convert_tabs (int new_wc);
int display_channel_color_win(Uint32 channel_number);
Uint32 active_channels[MAX_ACTIVE_CHANNELS] = {0};
Uint32 tmp_active_channels[MAX_ACTIVE_CHANNELS] = {0};
int tmp_channels_updating = 0;
Uint8 current_channel = 0;
queue_t *chan_name_queue;
chan_name * pseudo_chans[SPEC_CHANS];
widget_list *input_widget = NULL;
void input_widget_move_to_win(int window_id)
{
window_info *win = NULL;
if ((window_id >= 0) && (window_id < windows_list.num_windows))
win = &windows_list.window[window_id];
if ((input_widget == NULL) || (win == NULL))
return;
widget_move_win(input_widget->window_id, input_widget->id, window_id);
if(window_id == chat_win) {
widget_set_flags(input_widget->window_id, input_widget->id, TEXT_FIELD_BORDER|TEXT_FIELD_EDITABLE|TEXT_FIELD_NO_KEYPRESS);
input_widget->OnResize = NULL;
resize_chat_handler(win, win->len_x, win->len_y);
} else {
text_field *tf = input_widget->widget_info;
Uint32 flags;
input_widget->OnResize = input_field_resize;
if(window_id == console_root_win) {
flags = (TEXT_FIELD_BORDER|INPUT_DEFAULT_FLAGS)^WIDGET_CLICK_TRANSPARENT;
} else if(window_id == game_root_win && input_text_line.len == 0) {
flags = INPUT_DEFAULT_FLAGS|WIDGET_DISABLED;
} else if(window_id == map_root_win) {
flags = INPUT_DEFAULT_FLAGS|WIDGET_INVISIBLE;
} else {
flags = INPUT_DEFAULT_FLAGS;
}
widget_set_flags(input_widget->window_id, input_widget->id, flags);
widget_resize(input_widget->window_id, input_widget->id, win->len_x-HUD_MARGIN_X, tf->y_space*2+ceilf(DEFAULT_FONT_Y_LEN*input_widget->size*tf->nr_lines));
widget_move(input_widget->window_id, input_widget->id, 0, win->len_y-input_widget->len_y-HUD_MARGIN_Y);
}
}
void add_tab (Uint8 channel)
{
if (tab_bar_win != -1) add_tab_button (channel);
if (chat_win != -1) add_chat_tab (0, channel);
}
void remove_tab (Uint8 channel)
{
recolour_messages(display_text_buffer);
if (tab_bar_win != -1) remove_tab_button (channel);
if (chat_win != -1) remove_chat_tab (channel);
}
void clear_tabs_updated ()
{
if (tab_bar_win != -1) clear_tab_buttons_updated ();
if (chat_win != -1) clear_chat_tabs_updated ();
}
void update_tab_idx (Uint8 old_idx, Uint8 new_idx)
{
// XXX: CAUTION
// Since this function simply replaces old_idx y new_idx, it could
// potentially cause trouble when new_idx is already in use by
// another tab. However, as the code is now, successive calls to
// update_tab_idx are in increasing order of old_idx, and new_idx
// is lower than old_idx, so we should be safe.
if (tab_bar_win != -1) update_tab_button_idx (old_idx, new_idx);
if (chat_win != -1) update_chat_tab_idx (old_idx, new_idx);
}
void set_channel_tabs (const Uint32 *chans)
{
int nmax = loadsofchannels; //MAX_ACTIVE_CHANNELS;
Uint32 chan;
Uint8 chan_nr, chan_nrp;
for (chan_nr = 0; chan_nr < nmax; chan_nr++)
{
chan = chans[chan_nr];
if (chan == 0) continue;
for (chan_nrp = 0; chan_nrp < nmax; chan_nrp++)
{
if (active_channels[chan_nrp] == chan) break;
}
if (chan_nrp >= nmax)
{
// we left this channel
remove_tab (chan_nr + firstchannel);
}
else
{
update_tab_idx (chan_nr + firstchannel, chan_nrp + firstchannel);
}
}
for (chan_nrp = 0; chan_nrp < nmax; chan_nrp++)
{
chan = active_channels[chan_nrp];
if (chan == 0) continue;
for (chan_nr = 0; chan_nr < nmax; chan_nr++)
{
if (chans[chan_nr] == chan){
break;
}
}
if (chan_nr >= nmax)
{
// we have a new channel
add_tab (chan_nrp + firstchannel);
}
}
clear_tabs_updated();
}
void set_active_channels (Uint8 active, const Uint32 *channels, int nchan)
{
int i;
if(tmp_channels_updating)
return;
tmp_channels_updating = 1;
for (i = 0; i < MAX_ACTIVE_CHANNELS; i++)
tmp_active_channels[i] = active_channels[i];
for (i = 0; i < nchan; i++)
active_channels[i] = SDL_SwapLE32(channels[i]);
for ( ; i < MAX_ACTIVE_CHANNELS; i++)
active_channels[i] = 0;
set_channel_tabs (tmp_active_channels);
current_channel = active;
tmp_channels_updating = 0;
}
void send_active_channel (Uint8 chan)
{
Uint8 msg[2];
if (chan >= firstchannel && chan <= lastchannel)
{
msg[0] = SET_ACTIVE_CHANNEL;
msg[1] = chan;
my_tcp_send (my_socket, msg, 2);
current_channel = chan - firstchannel;
}
}
Uint32 get_active_channel (Uint8 idx)
{
if (idx >= firstchannel && idx <= lastchannel)
return active_channels[idx - firstchannel];
return 0;
}
#define CHAT_WIN_SPACE 4
#define CHAT_WIN_TAG_HEIGHT 20
#define CHAT_WIN_TAG_SPACE 3
#define CHAT_WIN_TEXT_WIDTH 500
#define CHAT_OUT_TEXT_HEIGHT (18*8)
#define CHAT_IN_TEXT_HEIGHT (18*3)
#define CHAT_WIN_SCROLL_WIDTH 20
int local_chat_separate = 0;
int personal_chat_separate = 0;
int guild_chat_separate = 1;
int server_chat_separate = 0;
int mod_chat_separate = 0;
/*
* use_windowed_chat == 0: old behaviour, all text is printed
* use_windowed_chat == 1: channel selection bar
* use_windowed_chat == 2: chat window
*/
int use_windowed_chat = 1;
int highlight_tab_on_nick = 1;
////////////////////////////////////////////////////////////////////////
// Chat window variables
int chat_scroll_id = 15;
int chat_tabcollection_id = 20;
int chat_out_start_id = 21;
int chat_win_x = 0; // upper left corner by default
int chat_win_y = 0;
int chat_win_text_width = CHAT_WIN_TEXT_WIDTH;
int chat_out_text_height = CHAT_OUT_TEXT_HEIGHT;
int current_line = 0;
int text_changed = 1;
int nr_displayed_lines;
chat_channel channels[MAX_CHAT_TABS];
int active_tab = -1;
chan_name *tab_label (Uint8 chan);//Forward declaration
void clear_chat_wins (void)
{
int i = 0;
if(use_windowed_chat != 2){return;}
for (;i < MAX_CHAT_TABS; ++i){
channels[i].nr_lines = 0;
}
vscrollbar_set_bar_len (chat_win, chat_scroll_id, 0);
vscrollbar_set_pos (chat_win, chat_scroll_id, 0);
current_line = 0;
text_changed = 1;
}
void init_chat_channels(void)
{
int itab;
for (itab = 0; itab < MAX_CHAT_TABS; itab++)
{
channels[itab].tab_id = -1;
channels[itab].out_id = chat_out_start_id + itab;
channels[itab].chan_nr = CHAT_ALL;
channels[itab].nr_lines = 0;
channels[itab].open = 0;
channels[itab].newchan = 0;
}
}
void clear_input_line (void)
{
input_text_line.data[0] = '\0';
input_text_line.len = 0;
if(input_widget != NULL) {
text_field *field = input_widget->widget_info;
field->cursor = 0;
field->cursor_line = 0;
field->nr_lines = 1;
if(use_windowed_chat != 2) {
widget_resize(input_widget->window_id, input_widget->id, input_widget->len_x, field->y_space*2+DEFAULT_FONT_Y_LEN*input_widget->size);
}
}
/* Hide the game win input widget */
if(input_widget->window_id == game_root_win) {
widget_set_flags(game_root_win, input_widget->id, INPUT_DEFAULT_FLAGS|WIDGET_DISABLED);
}
history_reset();
}
int close_channel (window_info *win)
{
int id = win->window_id;
int ichan;
for (ichan = 0; ichan < MAX_CHAT_TABS; ichan++)
{
if (channels[ichan].tab_id == id)
{
int idx = channels[ichan].chan_nr - firstchannel;
if (idx >= 0 && idx < MAX_ACTIVE_CHANNELS && 0)
{
char str[256];
safe_snprintf(str, sizeof(str), "%c#lc %d", RAW_TEXT, active_channels[idx]);
my_tcp_send(my_socket, (Uint8*)str, strlen(str+1)+1);
}
// Safe to remove?
if (tab_bar_win != -1) remove_tab_button(channels[ichan].chan_nr);
return 1;
}
}
// we shouldn't get here
LOG_ERROR ("Trying to close non-existant channel\n");
return 0;
}
void remove_chat_tab (Uint8 channel)
{
int ichan;
for (ichan = 0; ichan < MAX_CHAT_TABS; ichan++)
{
if (channels[ichan].chan_nr == channel && channels[ichan].open)
{
int nr = tab_collection_get_tab_nr (chat_win, chat_tabcollection_id, channels[ichan].tab_id);
tab_collection_close_tab (chat_win, chat_tabcollection_id, nr);
channels[ichan].tab_id = -1;
channels[ichan].chan_nr = CHAT_ALL;
channels[ichan].nr_lines = 0;
channels[ichan].open = 0;
channels[ichan].newchan = 0;
channels[ichan].highlighted = 0;
return;
}
}
}
int add_chat_tab(int nlines, Uint8 channel)
{
int ichan;
for (ichan = 0; ichan < MAX_CHAT_TABS; ichan++)
{
if (!channels[ichan].open)
{
// yay, found an empty slot
char title[64];
int inout_width = chat_win_text_width + 2 * CHAT_WIN_SPACE;
int output_height = chat_out_text_height + 2 * CHAT_WIN_SPACE;
channels[ichan].chan_nr = channel;
channels[ichan].nr_lines = nlines;
channels[ichan].open = 1;
channels[ichan].newchan = 1;
channels[ichan].highlighted = 0;
my_strncp(title,(tab_label(channel))->name, sizeof(title));
channels[ichan].tab_id = tab_add (chat_win, chat_tabcollection_id, title, 0, 1, 0);
set_window_flag (channels[ichan].tab_id, ELW_CLICK_TRANSPARENT);
set_window_min_size (channels[ichan].tab_id, 0, 0);
channels[ichan].out_id = text_field_add_extended (channels[ichan].tab_id, channels[ichan].out_id, NULL, 0, 0, inout_width, output_height, 0, chat_zoom, newcol_r, newcol_g, newcol_b, display_text_buffer, DISPLAY_TEXT_BUFFER_SIZE, channel, CHAT_WIN_SPACE, CHAT_WIN_SPACE);
set_window_handler (channels[ichan].tab_id, ELW_HANDLER_DESTROY, close_channel);
if(!channels[ichan].highlighted && channels[active_tab].chan_nr != CHAT_ALL)
{
tab_set_label_color_by_id (chat_win, chat_tabcollection_id, channels[ichan].tab_id, 1.0, 1.0, 0.0);
}
return ichan;
}
}
//no empty slot found
return -1;
}
void clear_chat_tabs_updated ()
{
int itab;
for (itab = 0; itab < MAX_CHAT_TABS; itab++)
{
channels[itab].updated = 0;
}
}
void update_chat_tab_idx (Uint8 old_idx, Uint8 new_idx)
{
int itab;
if (old_idx == new_idx) return;
for (itab = 0; itab < MAX_CHAT_TABS; itab++)
{
if (channels[itab].chan_nr == old_idx && channels[itab].open && channels[itab].updated ==0)
{
channels[itab].chan_nr = new_idx;
channels[itab].updated = 1;
return;
}
}
}
Uint8 get_tab_channel (Uint8 channel)
{
switch (channel)
{
case CHAT_LOCAL:
if (!local_chat_separate) return CHAT_ALL;
break;
case CHAT_PERSONAL:
if (!personal_chat_separate) return CHAT_ALL;
break;
case CHAT_GM:
if (!guild_chat_separate) return CHAT_ALL;
break;
case CHAT_SERVER:
if (!server_chat_separate) return CHAT_ALL;
break;
case CHAT_MOD:
if (!mod_chat_separate) return CHAT_ALL;
break;
case CHAT_MODPM:
// always display moderator PMs in all tabs
return CHAT_ALL;
}
return channel;
}
void update_chat_window (text_message *msg, char highlight)
{
int ichan, len, nlines, width, channel;
char found;
// don't bother if there's no chat window
if (chat_win < 0) return;
// rewrap message to get correct # of lines
width = windows_list.window[chat_win].len_x;
nlines = rewrap_message(msg, chat_zoom, width, NULL);
// first check if we need to display in all open channels
channel = get_tab_channel (msg->chan_idx);
if (channel == CHAT_ALL || channel == CHAT_MODPM)
{
for (ichan = 0; ichan < MAX_CHAT_TABS; ichan++)
{
if (channels[ichan].open) {
if (msg->deleted) {
channels[ichan].nr_lines -= nlines;
} else {
channels[ichan].nr_lines += nlines;
}
}
}
len = channels[active_tab].nr_lines - nr_displayed_lines;
if (len < 0) len = 0;
vscrollbar_set_bar_len (chat_win, chat_scroll_id, len);
vscrollbar_set_pos (chat_win, chat_scroll_id, len);
current_line = channels[active_tab].nr_lines;
text_changed = 1;
return;
}
// message not for all channels, see if this channel is already open
found = 0;
for (ichan = 0; ichan < MAX_CHAT_TABS; ichan++)
{
if (channels[ichan].open && (channels[ichan].chan_nr == channel || channels[ichan].chan_nr == CHAT_ALL))
{
if (msg->deleted) {
channels[ichan].nr_lines -= nlines;
} else {
channels[ichan].nr_lines += nlines;
}
channels[ichan].newchan = 1;
if (ichan == active_tab)
{
len = channels[ichan].nr_lines - nr_displayed_lines;
if (len < 0) len = 0;
vscrollbar_set_bar_len (chat_win, chat_scroll_id, len);
vscrollbar_set_pos (chat_win, chat_scroll_id, len);
current_line = channels[ichan].nr_lines;
text_changed = 1;
}
else if (highlight && !channels[ichan].highlighted && channels[active_tab].chan_nr != CHAT_ALL && channels[ichan].chan_nr != CHAT_ALL && !get_show_window(console_root_win)) //Make sure we don't change the color of a highlighted tab
{
tab_set_label_color_by_id (chat_win, chat_tabcollection_id, channels[ichan].tab_id, 1.0, 1.0, 0.0);
}
if (found) return; // we found the respective tab and the "all" tab now
found++;
}
}
// nothing to delete from
if (msg->deleted) return;
// channel not found, try to create a new one
if(add_chat_tab(nlines, channel) == -1)
{
// uh oh, no empty slot found. this shouldn't really be happening...
// log in general channel
channels[0].nr_lines += nlines;
channels[0].newchan = 1;
if (0 == active_tab)
{
len = channels[0].nr_lines - nr_displayed_lines;
if (len < 0) len = 0;
vscrollbar_set_bar_len (chat_win, chat_scroll_id, len);
vscrollbar_set_pos (chat_win, chat_scroll_id, len);
current_line = channels[active_tab].nr_lines;
text_changed = 1;
}
else if (highlight && !channels[0].highlighted) //Make sure we don't change the color of a highlighted tab
{
tab_set_label_color_by_id (chat_win, chat_tabcollection_id, channels[0].tab_id, 1.0, 1.0, 0.0);
}
}
}
int display_chat_handler (window_info *win)
{
static int msg_start = 0, offset_start = 0;
if (text_changed)
{
int line = vscrollbar_get_pos (chat_win, chat_scroll_id);
find_line_nr (channels[active_tab].nr_lines, line, channels[active_tab].chan_nr, &msg_start, &offset_start, chat_zoom, chat_win_text_width);
text_field_set_buf_pos (channels[active_tab].tab_id, channels[active_tab].out_id, msg_start, offset_start);
text_changed = 0;
}
if ((input_widget!= NULL) && (input_widget->window_id != win->window_id))
input_widget_move_to_win(win->window_id);
return 1;
}
void switch_to_chat_tab(int id, char click)
{
if(!click)
{
int itab;
//Do what a mouse click would do
widget_list *widget = widget_find(chat_win, chat_tabcollection_id);
tab_collection *collection = widget->widget_info;
for(itab = 0; itab < collection->nr_tabs; itab++)
{
if(collection->tabs[itab].content_id == id)
{
break;
}
}
tab_collection_select_tab(chat_win, chat_tabcollection_id, itab);
}
tab_set_label_color_by_id (chat_win, chat_tabcollection_id, id, -1.0, -1.0, -1.0);
//set active_tab
for (active_tab = 0; active_tab < MAX_CHAT_TABS; active_tab++)
{
if (channels[active_tab].tab_id == id && channels[active_tab].open)
{
break;
}
}
if (active_tab >= MAX_CHAT_TABS)
{
// This shouldn't be happening
LOG_ERROR ("Trying to switch to non-existant channel");
active_tab = 0;
}
current_line = channels[active_tab].nr_lines - nr_displayed_lines;
if (current_line < 0)
{
current_line = 0;
}
vscrollbar_set_bar_len(chat_win, chat_scroll_id, current_line);
vscrollbar_set_pos(chat_win, chat_scroll_id, current_line);
text_changed = 1;
channels[active_tab].highlighted = 0;
if (channels[active_tab].chan_nr >= firstchannel && channels[active_tab].chan_nr <= lastchannel)
send_active_channel (channels[active_tab].chan_nr);
recolour_messages(display_text_buffer);
}
void change_to_current_chat_tab(const char *input)
{
Uint8 channel;
int ichan;
int itab;
int input_len = strlen(input);
if(input[0] == '@' || input[0] == char_at_str[0])
{
channel = firstchannel + current_channel;
}
else if(my_strncompare(input, "#gm ", 4) || (my_strncompare(input, gm_cmd_str, strlen(gm_cmd_str)) && input_len > strlen(gm_cmd_str)+1 && input[strlen(gm_cmd_str)] == ' '))
{
channel = CHAT_GM;
}
else if(my_strncompare(input, "#mod ", 5) || (my_strncompare(input, mod_cmd_str, strlen(mod_cmd_str)) && input_len > strlen(mod_cmd_str)+1 && input[strlen(mod_cmd_str)] == ' '))
{
channel = CHAT_MOD;
}
else if(my_strncompare(input, "#bc ", 4) || (my_strncompare(input, bc_cmd_str, strlen(bc_cmd_str)) && input_len > strlen(bc_cmd_str)+1 && input[strlen(bc_cmd_str)] == ' '))
{
channel = CHAT_SERVER;
}
else if(input[0] == '/' || input[0] == char_slash_str[0])
{
channel = CHAT_PERSONAL;
}
else if(input[0] == '#' || input[0] == char_cmd_str[0]) {
//We don't want to switch tab on commands.
channel = CHAT_ALL;
}
else
{
channel = CHAT_LOCAL;
}
channel = get_tab_channel (channel);
if(channel != CHAT_ALL)
{
for(ichan = 0; ichan < MAX_CHAT_TABS; ichan++)
{
if(channels[ichan].chan_nr == channel && channels[ichan].open)
{
if(ichan != active_tab) //We don't want to switch to the tab we're already in
{
switch_to_chat_tab(channels[ichan].tab_id, 0);
}
return;
}
}
//We didn't find any tab to switch to, create new
itab = add_chat_tab(0, channel);
if(itab == -1)
{
//Eek, it failed, switch to general
switch_to_chat_tab(channels[0].tab_id, 0);
}
else
{
switch_to_chat_tab(channels[itab].tab_id, 0);
}
}
}
int chat_tabs_click (widget_list *widget, int mx, int my, Uint32 flags)
{
int id;
id = tab_collection_get_tab_id (chat_win, widget->id);
if (flags&ELW_RIGHT_MOUSE)
{
int i;
for(i=0; i < MAX_CHAT_TABS; i++)
{
if(channels[i].tab_id == id)
{
display_channel_color_win(get_active_channel(channels[i].chan_nr));
return 1;
}
}
}
else
{
if (id != channels[active_tab].tab_id)
{
//We're not looking at the tab we clicked
switch_to_chat_tab(id, 1);
return 1;
}
}
return 0;
}
int chat_scroll_drag (widget_list *widget, int mx, int my, Uint32 flags, int dx, int dy)
{
int line = vscrollbar_get_pos (chat_win, widget->id);
if (line != current_line)
{
text_changed = 1;
current_line = line;
}
return 0;
}
int chat_scroll_click (widget_list *widget, int mx, int my, Uint32 flags)
{
int line = vscrollbar_get_pos (chat_win, widget->id);
if (line != current_line)
{
text_changed = 1;
current_line = line;
}
return 0;
}
int chat_input_key (widget_list *widget, int mx, int my, Uint32 key, Uint32 unikey)
{
Uint16 keysym = key & 0xffff;
text_field *tf;
text_message *msg;
if (widget == NULL) {
return 0;
}
tf = (text_field *) widget->widget_info;
msg = tf->buffer;
if ( (!(key & ELW_CTRL) && ( (keysym == SDLK_UP) || (keysym == SDLK_DOWN) ) ) ||
(keysym == SDLK_LEFT) || (keysym == SDLK_RIGHT) || (keysym == SDLK_HOME) ||
(keysym == SDLK_END) || (keysym == SDLK_DELETE && tf->cursor < msg->len) ) {
//pass it along. the defaults are good enough
widget->Flags &= ~TEXT_FIELD_NO_KEYPRESS;
text_field_keypress (widget, mx, my, key, unikey);
widget->Flags |= TEXT_FIELD_NO_KEYPRESS;
}
else
{
return 0;
}
/* Key was handled, stop blinking on input */
tf->next_blink = cur_time + TF_BLINK_DELAY;
return 1;
}
int resize_chat_handler(window_info *win, int width, int height)
{
int itab;
int scroll_x = width - CHAT_WIN_SCROLL_WIDTH;
int scroll_height = height - 2*ELW_BOX_SIZE;
int inout_width = width - CHAT_WIN_SCROLL_WIDTH - 2 * CHAT_WIN_SPACE;
int input_height = CHAT_IN_TEXT_HEIGHT + 2 * CHAT_WIN_SPACE;
int input_y = height - input_height - CHAT_WIN_SPACE;
int tabcol_height = input_y - 2 * CHAT_WIN_SPACE;
int output_height = tabcol_height - CHAT_WIN_TAG_HEIGHT;
int line_height = DEFAULT_FONT_Y_LEN*chat_zoom;
if (output_height < 5*line_height + 2 * CHAT_WIN_SPACE && input_height > 3*line_height + 2 * CHAT_WIN_SPACE)
{
input_height -= 2*line_height;
input_y += 2*line_height;
output_height += 2*line_height;
tabcol_height += 2*line_height;
}
else if (output_height < 8*line_height + 2 * CHAT_WIN_SPACE && input_height > 2*line_height + 2 * CHAT_WIN_SPACE)
{
input_height -= line_height;
input_y += line_height;
output_height += line_height;
tabcol_height += line_height;
}
chat_win_text_width = inout_width - 2 * CHAT_WIN_SPACE;
chat_out_text_height = output_height - 2 * CHAT_WIN_SPACE;
widget_resize (chat_win, chat_scroll_id, CHAT_WIN_SCROLL_WIDTH, scroll_height);
widget_move (chat_win, chat_scroll_id, scroll_x, ELW_BOX_SIZE);
widget_resize (chat_win, chat_tabcollection_id, inout_width, tabcol_height);
for (itab = 0; itab < MAX_CHAT_TABS; itab++)
if (channels[itab].tab_id >= 0)
widget_resize (channels[itab].tab_id, channels[itab].out_id, inout_width, output_height);
widget_resize (chat_win, input_widget->id, inout_width, input_height);
widget_move (chat_win, input_widget->id, CHAT_WIN_SPACE, input_y);
update_chat_win_buffers();
return 0;
}
void update_chat_win_buffers(void)
{
int itab, imsg;
// recompute line breaks
for (itab = 0; itab < MAX_CHAT_TABS; itab++)
channels[itab].nr_lines = 0;
imsg = 0;
while (1)
{
update_chat_window (&display_text_buffer[imsg], 0);
if (imsg == last_message || last_message < 0)
break;
if (++imsg >= DISPLAY_TEXT_BUFFER_SIZE)
imsg = 0;
}
// adjust the text position and scroll bar
nr_displayed_lines = (int) (chat_out_text_height / (18.0f * chat_zoom));
current_line = channels[active_tab].nr_lines - nr_displayed_lines;
if (current_line < 0)
current_line = 0;
vscrollbar_set_bar_len (chat_win, chat_scroll_id, current_line);
vscrollbar_set_pos (chat_win, chat_scroll_id, current_line);
text_changed = 1;
}
void parse_input(char *data, int len)
{
if (len > MAX_TEXT_MESSAGE_LENGTH)
{
LOG_TO_CONSOLE(c_red2, command_too_long_str);
return;
}
if (data[0] == '%' && len > 1)
{
if ( (check_var ((char*)&(data[1]), IN_GAME_VAR) ) < 0)
{
send_input_text_line ((char*)data, len);
}
}
else if ( data[0] == '#' || data[0] == char_cmd_str[0] )
{
test_for_console_command ((char*)data, len);
}
else if (data[0] == '/' && len > 1)
{
// Forum #58898: Please the server when sending player messages;
// remove all but one space between name and message start.
// do not assume data is null terminated
size_t dx = 0;
char *rebuf = (char *)malloc(len+1);
for (dx=0; (dx < len) && (data[dx] != ' '); dx++)
rebuf[dx] = data[dx];
rebuf[dx] = '\0';
while ((dx < len) && (data[dx] == ' '))
dx++;
if (dx < len)
{
size_t rebuf_len = 0;
safe_strcat(rebuf, " ", len+1);
rebuf_len = strlen(rebuf);
safe_strncpy2(&rebuf[rebuf_len], &data[dx], len+1-rebuf_len, len-dx);
}
send_input_text_line (rebuf, strlen(rebuf));
free(rebuf);
}
else
{
if(data[0] == char_at_str[0])
data[0] = '@';
send_input_text_line ((char*)data, len);
}
}
int root_key_to_input_field (Uint32 key, Uint32 unikey)
{
Uint16 keysym = key & 0xffff;
Uint8 ch = key_to_char (unikey);
text_field *tf;
text_message *msg;
int alt_on = key & ELW_ALT, ctrl_on = key & ELW_CTRL;
if(input_widget == NULL || (input_widget->Flags & TEXT_FIELD_EDITABLE) == 0) {
return 0;
}
tf = input_widget->widget_info;
msg = &(tf->buffer[tf->msg]);
if (keysym == SDLK_ESCAPE)
{
clear_input_line();
}
else if (ch == SDLK_RETURN && msg->len > 0)
{
parse_input(msg->data, msg->len);
add_line_to_history((char*)msg->data, msg->len);
clear_input_line();
}
else if (tf->cursor == 1 && (ch == '/' || ch == char_slash_str[0])
&& (msg->data[0] == '/' || msg->data[0]==char_slash_str[0])
&& last_pm_from[0])
{
// watch for the '//' shortcut
tf->cursor += put_string_in_buffer (msg, (unsigned char*)last_pm_from, 1);
tf->cursor += put_char_in_buffer (msg, ' ', tf->cursor);
// set invalid width to force rewrap
msg->wrap_width = 0;
tf->nr_lines = rewrap_message (msg, input_widget->size, input_widget->len_x - 2 * tf->x_space, &tf->cursor);
}
else if (ch == SDLK_BACKSPACE || ch == SDLK_DELETE
#ifdef OSX
|| ch == 127
#endif
|| (!alt_on && !ctrl_on && is_printable (ch) && ch != '`')
)
{
if (is_printable (ch) && !get_show_window(map_root_win)) {
//Make sure the widget is visible.
widget_unset_flags (input_widget->window_id, input_widget->id, WIDGET_DISABLED);
widget_unset_flags (input_widget->window_id, input_widget->id, WIDGET_INVISIBLE);
}
// XXX FIXME: we've set the input widget with the
// TEXT_FIELD_NO_KEYPRESS flag so that the default key
// handler for a text field isn't called, but now
// we do want to call it directly. So we clear the flag,
// and reset it afterwards.
input_widget->Flags &= ~TEXT_FIELD_NO_KEYPRESS;
text_field_keypress (input_widget, 0, 0, key, unikey);
input_widget->Flags |= TEXT_FIELD_NO_KEYPRESS;
}
else if (key == K_TABCOMPLETE && input_text_line.len > 0)
{
do_tab_complete(&input_text_line);
}
else if (get_show_window(console_root_win))
{
if (!chat_input_key (input_widget, 0, 0, key, unikey))
return 0;
}
else
{
return 0;
}
tf->next_blink = cur_time + TF_BLINK_DELAY;
if(input_widget->window_id != chat_win && tf->nr_lines != floorf((input_widget->len_y-2*tf->y_space)/(DEFAULT_FONT_Y_LEN*input_widget->size))) {
/* Resize the input widget if needed */
widget_resize(input_widget->window_id, input_widget->id, input_widget->len_x, tf->y_space*2 + ceilf(DEFAULT_FONT_Y_LEN*input_widget->size*tf->nr_lines));
}
while(tf->buffer->data[tf->cursor] == '\r' && tf->cursor < tf->buffer->len)
{
tf->cursor++;
}
return 1;
}
void paste_in_input_field (const Uint8 *text)
{
text_field *tf;
text_message *msg;
if (input_widget == NULL) {
return;
} else if (input_widget->window_id == game_root_win) {
widget_unset_flags (game_root_win, input_widget->id, WIDGET_DISABLED);
}
tf = input_widget->widget_info;
msg = &(tf->buffer[tf->msg]);
tf->cursor += put_string_in_buffer(msg, text, tf->cursor);
// set invalid width to force rewrap
msg->wrap_width = 0;
tf->nr_lines = rewrap_message(msg, input_widget->size, input_widget->len_x - 2 * tf->x_space, &tf->cursor);
if(use_windowed_chat != 2) {
widget_resize(input_widget->window_id, input_widget->id, input_widget->len_x, tf->y_space*2 + ceilf(DEFAULT_FONT_Y_LEN*input_widget->size*tf->nr_lines));
}
}
void put_string_in_input_field(const Uint8 *text)
{
text_field *tf = input_widget->widget_info;
text_message *msg = &(tf->buffer[tf->msg]);
if(text != NULL) {
tf->cursor = msg->len = safe_snprintf((char*)msg->data, msg->size, "%s", text);