forked from QW-Group/ezquake-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ez_label.c
1351 lines (1150 loc) · 35.2 KB
/
ez_label.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
/*
Copyright (C) 2007 ezQuake team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
$Id: ez_scrollpane.c,v 1.78 2007/10/27 14:51:15 cokeman1982 Exp $
*/
#include "quakedef.h"
#include "keys.h"
#include "utils.h"
#include "common_draw.h"
#include "ez_controls.h"
#include "ez_label.h"
#ifdef _MSC_VER
#pragma warning( disable : 4189 )
#endif
// =========================================================================================
// Label
// =========================================================================================
//
// Label - Creates a label control and initializes it.
//
ez_label_t *EZ_label_Create(ez_tree_t *tree, ez_control_t *parent,
char *name, char *description,
int x, int y, int width, int height,
ez_control_flags_t flags, ez_label_flags_t text_flags,
const char *text)
{
ez_label_t *label = NULL;
// We have to have a tree to add the control to.
if (!tree)
{
return NULL;
}
label = (ez_label_t *)Q_malloc(sizeof(ez_label_t));
memset(label, 0, sizeof(ez_label_t));
EZ_label_Init(label, tree, parent, name, description, x, y, width, height, flags, text_flags, text);
return label;
}
//
// Label - Initializes a label control.
//
void EZ_label_Init(ez_label_t *label, ez_tree_t *tree, ez_control_t *parent,
char *name, char *description,
int x, int y, int width, int height,
ez_control_flags_t flags, ez_label_flags_t text_flags,
const char *text)
{
// Initialize the inherited class first.
EZ_control_Init(&label->super, tree, parent, name, description, x, y, width, height, flags);
label->super.CLASS_ID = EZ_LABEL_ID;
// Overriden events.
CONTROL_REGISTER_EVENT(label, EZ_label_OnDraw, OnDraw, ez_control_t);
CONTROL_REGISTER_EVENT(label, EZ_label_OnKeyDown, OnKeyDown, ez_control_t);
CONTROL_REGISTER_EVENT(label, EZ_label_OnKeyUp, OnKeyUp, ez_control_t);
CONTROL_REGISTER_EVENT(label, EZ_label_OnMouseDown, OnMouseDown, ez_control_t);
CONTROL_REGISTER_EVENT(label, EZ_label_OnMouseUp, OnMouseUp, ez_control_t);
CONTROL_REGISTER_EVENT(label, EZ_label_OnMouseHover, OnMouseHover, ez_control_t);
CONTROL_REGISTER_EVENT(label, EZ_label_OnResize, OnResize, ez_control_t);
CONTROL_REGISTER_EVENT(label, EZ_label_Destroy, OnDestroy, ez_control_t);
// Label specific events.
CONTROL_REGISTER_EVENT(label, EZ_label_OnTextChanged, OnTextChanged, ez_label_t);
CONTROL_REGISTER_EVENT(label, EZ_label_OnCaretMoved, OnCaretMoved, ez_label_t);
CONTROL_REGISTER_EVENT(label, EZ_label_OnTextScaleChanged, OnTextScaleChanged, ez_label_t);
CONTROL_REGISTER_EVENT(label, EZ_label_OnTextFlagsChanged, OnTextFlagsChanged, ez_label_t);
((ez_control_t *)label)->ext_flags |= control_contained;
EZ_label_SetTextFlags(label, text_flags | label_selectable);
EZ_label_SetTextScale(label, 1.0);
EZ_label_DeselectText(label);
if (text)
{
EZ_label_SetText(label, text);
}
}
//
// Label - Destroys a label control.
//
int EZ_label_Destroy(ez_control_t *self, qbool destroy_children)
{
ez_label_t *label = (ez_label_t *)self;
CONTROL_EVENT_HANDLER_CALL(NULL, self, ez_control_t, OnDestroy, destroy_children);
Q_free(label->text);
EZ_eventhandler_Remove(label->event_handlers.OnCaretMoved, NULL, true);
EZ_eventhandler_Remove(label->event_handlers.OnTextChanged, NULL, true);
EZ_eventhandler_Remove(label->event_handlers.OnTextFlagsChanged, NULL, true);
EZ_eventhandler_Remove(label->event_handlers.OnTextScaleChanged, NULL, true);
// TODO: Can we just free a part like this here? How about children, will they be properly destroyed?
EZ_control_Destroy(&label->super, destroy_children);
return 0;
}
//
// Label - Sets the event handler for the OnTextChanged event.
//
void EZ_label_AddOnTextChanged(ez_label_t *label, ez_eventhandler_fp OnTextChanged, void *payload)
{
CONTROL_ADD_EVENTHANDLER(label, EZ_CONTROL_HANDLER, OnTextChanged, ez_label_t, OnTextChanged, payload);
CONTROL_RAISE_EVENT(NULL, label, ez_control_t, OnEventHandlerChanged, NULL);
}
//
// Label - Sets the event handler for the OnTextScaleChanged event.
//
void EZ_label_AddOnTextScaleChanged(ez_label_t *label, ez_eventhandler_fp OnTextScaleChanged, void *payload)
{
CONTROL_ADD_EVENTHANDLER(label, EZ_CONTROL_HANDLER, OnTextScaleChanged, ez_label_t, OnTextScaleChanged, payload);
CONTROL_RAISE_EVENT(NULL, label, ez_control_t, OnEventHandlerChanged, NULL);
}
//
// Label - Sets the event handler for the OnCaretMoved event.
//
void EZ_label_AddOnTextOnCaretMoved(ez_label_t *label, ez_eventhandler_fp OnCaretMoved, void *payload)
{
CONTROL_ADD_EVENTHANDLER(label, EZ_CONTROL_HANDLER, OnCaretMoved, ez_label_t, OnCaretMoved, payload);
CONTROL_RAISE_EVENT(NULL, label, ez_control_t, OnEventHandlerChanged, NULL);
}
//
// Label - Calculates where in the label text that the wordwraps will be done.
//
static void EZ_label_CalculateWordwraps(ez_label_t *label)
{
int i = 0;
int current_index = -1;
int last_index = -1;
// int current_col = 0;
int scaled_char_size = label->scaled_char_size;
label->num_rows = 1;
label->num_cols = 0;
if (label->text && (label->ext_flags & label_wraptext))
{
// Wordwrap the string to the virtual size of the control and save the
// indexes where each row ends in an array.
while ((i < LABEL_MAX_WRAPS) && Util_GetNextWordwrapString(label->text, NULL, (current_index + 1), ¤t_index, LABEL_LINE_SIZE, label->super.virtual_width, scaled_char_size))
{
label->wordwraps[i].index = current_index;
label->wordwraps[i].col = current_index - last_index;
label->wordwraps[i].row = label->num_rows - 1;
i++;
// Find the number of rows and columns.
label->num_cols = max(label->num_cols, label->wordwraps[i].col);
label->num_rows++;
last_index = current_index;
}
}
else if (label->text)
{
// Normal non-wrapped text, still save new line locations.
current_index = 0; // TODO : Will this be ok? Otherwise it will be -1, which definantly is bad :p
while ((i < LABEL_MAX_WRAPS) && label->text[current_index])
{
if (label->text[current_index] == '\n')
{
label->wordwraps[i].index = current_index;
label->wordwraps[i].col = current_index - last_index;
label->wordwraps[i].row = label->num_rows - 1;
i++;
// Find the number of rows and columns.
label->num_cols = max(label->num_cols, label->wordwraps[i].col);
label->num_rows++;
}
current_index++;
}
}
// Save the row/col information for the last row also.
if (label->text)
{
label->wordwraps[i].index = -1;
label->wordwraps[i].row = label->num_rows - 1;
label->wordwraps[i].col = label->text_length - label->wordwraps[max(0, i - 1)].index;
label->num_cols = max(label->num_cols, label->wordwraps[i].col);
}
else
{
// No text.
label->wordwraps[0].index = -1;
label->wordwraps[0].row = -1;
label->wordwraps[0].col = -1;
label->num_cols = 0;
label->num_rows = 0;
}
// Change the virtual height of the control to fit the text when wrapping.
if (label->ext_flags & label_wraptext)
{
EZ_control_SetMinVirtualSize((ez_control_t *)label, label->super.virtual_width_min, scaled_char_size * (label->num_rows + 1));
}
else
{
EZ_control_SetMinVirtualSize((ez_control_t *)label, scaled_char_size * (label->num_cols + 1), scaled_char_size * (label->num_rows + 1));
}
}
//
// Label - Sets if the label should use the large charset or the normal one.
//
void EZ_label_SetLargeFont(ez_label_t *label, qbool large_font)
{
SET_FLAG(label->ext_flags, label_largefont, large_font);
CONTROL_RAISE_EVENT(NULL, label, ez_label_t, OnTextFlagsChanged, NULL);
}
//
// Label - Sets if the label should autosize itself to fit the text in the label.
//
void EZ_label_SetAutoSize(ez_label_t *label, qbool auto_size)
{
SET_FLAG(label->ext_flags, label_autosize, auto_size);
CONTROL_RAISE_EVENT(NULL, label, ez_label_t, OnTextFlagsChanged, NULL);
}
//
// Label - Sets if the label should automatically add "..." at the end of the string when it doesn't fit in the label.
//
void EZ_label_SetAutoEllipsis(ez_label_t *label, qbool auto_ellipsis)
{
SET_FLAG(label->ext_flags, label_autoellipsis, auto_ellipsis);
CONTROL_RAISE_EVENT(NULL, label, ez_label_t, OnTextFlagsChanged, NULL);
}
//
// Label - Sets if the text in the label should wrap to fit the width of the label.
//
void EZ_label_SetWrapText(ez_label_t *label, qbool wrap_text)
{
SET_FLAG(label->ext_flags, label_wraptext, wrap_text);
CONTROL_RAISE_EVENT(NULL, label, ez_label_t, OnTextFlagsChanged, NULL);
}
//
// Label - Sets if the text in the label should be selectable.
//
void EZ_label_SetTextSelectable(ez_label_t *label, qbool selectable)
{
SET_FLAG(label->ext_flags, label_selectable, selectable);
CONTROL_RAISE_EVENT(NULL, label, ez_label_t, OnTextFlagsChanged, NULL);
}
//
// Label - Sets if the label should be read only.
//
void EZ_label_SetReadOnly(ez_label_t *label, qbool read_only)
{
SET_FLAG(label->ext_flags, label_readonly, read_only);
CONTROL_RAISE_EVENT(NULL, label, ez_label_t, OnTextFlagsChanged, NULL);
}
//
// Label - Sets the text flags for the label.
//
void EZ_label_SetTextFlags(ez_label_t *label, ez_label_flags_t flags)
{
label->ext_flags = flags;
CONTROL_RAISE_EVENT(NULL, label, ez_label_t, OnTextFlagsChanged, NULL);
}
//
// Label - Gets the text flags for the label.
//
ez_label_flags_t EZ_label_GetTextFlags(ez_label_t *label)
{
return label->ext_flags;
}
//
// Label - Set the text scale for the label.
//
void EZ_label_SetTextScale(ez_label_t *label, float scale)
{
label->scale = scale;
CONTROL_RAISE_EVENT(NULL, label, ez_label_t, OnTextScaleChanged, NULL);
}
//
// Label - Sets the text color of a label.
//
void EZ_label_SetTextColor(ez_label_t *label, byte r, byte g, byte b, byte alpha)
{
label->color.c = RGBA_TO_COLOR(r, g, b, alpha);
}
//
// Label - Hides the caret.
//
void EZ_label_HideCaret(ez_label_t *label)
{
label->caret_pos.index = -1;
}
//
// Label - Deselects the text in the label.
//
void EZ_label_DeselectText(ez_label_t *label)
{
label->int_flags &= ~label_selecting;
label->select_start = -1;
label->select_end = -1;
}
//
// Label - Gets the size of the selected text.
//
int EZ_label_GetSelectedTextSize(ez_label_t *label)
{
if ((label->select_start > -1) && (label->select_end > -1))
{
return abs(label->select_end - label->select_start) + 1;
}
return 0;
}
//
// Label - Gets the selected text.
//
void EZ_label_GetSelectedText(ez_label_t *label, char *target, int target_size)
{
if ((label->select_start > -1) && (label->select_end > -1))
{
int sublen = abs(label->select_end - label->select_start) + 1;
if (sublen > 0)
{
snprintf(target, min(sublen, target_size), "%s", label->text + min(label->select_start, label->select_end));
}
}
}
//
// Label - Appends text at the given position in the text.
//
void EZ_label_AppendText(ez_label_t *label, int position, const char *append_text)
{
int text_len = label->text_length;
int append_text_len = strlen(append_text);
clamp(position, 0, text_len);
if (!label->text)
{
return;
}
// Reallocate the string to fit the new text.
label->text = (char *)Q_realloc((void *)label->text, (text_len + append_text_len + 1) * sizeof(char));
// Move the part of the string after the specified position.
memmove(
label->text + position + append_text_len, // Destination, make room for the append text.
label->text + position, // Move everything after the specified position.
(text_len + 1 - position) * sizeof(char));
// Copy the append text into the newly created space in the string.
memcpy(label->text + position, append_text, append_text_len * sizeof(char));
CONTROL_RAISE_EVENT(NULL, label, ez_label_t, OnTextChanged, NULL);
}
//
// Label - Removes text between two given indexes.
//
void EZ_label_RemoveText(ez_label_t *label, int start_index, int end_index)
{
clamp(start_index, 0, label->text_length);
clamp(end_index, start_index, label->text_length);
if (!label->text)
{
return;
}
memmove(
label->text + start_index, // Destination.
label->text + end_index, // Source.
(label->text_length - end_index + 1) * sizeof(char)); // Size.
label->text = Q_realloc(label->text, (strlen(label->text) + 1) * sizeof(char));
CONTROL_RAISE_EVENT(NULL, label, ez_label_t, OnTextChanged, NULL);
}
//
// Label - Sets the text of a label.
//
void EZ_label_SetText(ez_label_t *label, const char *text)
{
int text_len = strlen(text) + 1;
Q_free(label->text);
if (text)
{
label->text = Q_calloc(text_len, sizeof(char));
strlcpy(label->text, text, text_len);
}
CONTROL_RAISE_EVENT(NULL, label, ez_label_t, OnTextChanged, NULL);
}
//
// Label - The text flags for the label changed.
//
int EZ_label_OnTextFlagsChanged(ez_control_t *self, void *ext_event_info)
{
ez_label_t *label = (ez_label_t *)self;
// Deselect anything selected and hide the caret.
if (!(label->ext_flags & label_selectable))
{
EZ_label_HideCaret(label);
EZ_label_DeselectText(label);
}
// Make sure we recalculate the char size if we changed to large font.
if (label->ext_flags & label_largefont)
{
CONTROL_RAISE_EVENT(NULL, label, ez_label_t, OnTextScaleChanged, NULL);
}
// The label will autosize on a text change, so trigger an event for that.
if (label->ext_flags & label_autosize)
{
CONTROL_RAISE_EVENT(NULL, label, ez_label_t, OnTextChanged, NULL);
}
// Recalculate the line ends.
if (label->ext_flags & label_wraptext)
{
CONTROL_RAISE_EVENT(NULL, self, ez_control_t, OnResize, NULL);
}
CONTROL_EVENT_HANDLER_CALL(NULL, label, ez_label_t, OnTextFlagsChanged, NULL);
return 0;
}
//
// Label - The scale of the text changed.
//
int EZ_label_OnTextScaleChanged(ez_control_t *self, void *ext_event_info)
{
ez_label_t *label = (ez_label_t *)self;
int char_size = (label->ext_flags & label_largefont) ? 64 : 8;
label->scaled_char_size = Q_rint(char_size * label->scale);
// We need to recalculate the wordwrap stuff since the size changed.
EZ_label_CalculateWordwraps(label);
CONTROL_EVENT_HANDLER_CALL(NULL, label, ez_label_t, OnTextScaleChanged, NULL);
return 0;
}
//
// Label - Happens when the control has resized.
//
int EZ_label_OnResize(ez_control_t *self, void *ext_event_info)
{
ez_label_t *label = (ez_label_t *)self;
// Let the super class try first.
EZ_control_OnResize(self, NULL);
// Calculate where to wrap the text.
EZ_label_CalculateWordwraps(label);
CONTROL_EVENT_HANDLER_CALL(NULL, self, ez_control_t, OnResize, NULL);
return 0;
}
//
// Label - Draws a label control.
//
int EZ_label_OnDraw(ez_control_t *self, void *ext_event_info)
{
int x, y, i = 0;
char line[LABEL_LINE_SIZE];
ez_label_t *label = (ez_label_t *)self;
int scaled_char_size = label->scaled_char_size;
int last_index = -1;
int curr_row = 0;
int curr_col = 0;
clrinfo_t text_color = {RGBA_TO_COLOR(255, 255, 255, 255), 0}; // TODO : Set this in the struct instead.
color_t selection_color = RGBA_TO_COLOR(178, 0, 255, 125);
color_t caret_color = RGBA_TO_COLOR(255, 0, 0, 125);
// Let the super class draw first.
EZ_control_OnDraw(self, NULL);
// Get the position we're drawing at.
EZ_control_GetDrawingPosition(self, &x, &y);
// Find any newlines and draw line by line.
for (i = 0; i <= label->text_length; i++)
{
// Draw selection markers.
if (label->ext_flags & label_selectable)
{
if (((label->select_start > -1) && (label->select_end > -1) // Is something selected at all?
&& (label->select_end != label->select_start)) // Only highlight if we have at least 1 char selected.
&& (((i >= label->select_start) && (i < label->select_end)) // Is this index selected?
|| ((i >= label->select_end) && (i < label->select_start)))
)
{
// If this is one of the selected letters, draw a selection thingie behind it.
// TODO : Make this an XOR drawing ontop of the text instead?
Draw_AlphaFillRGB(
x + (curr_col * scaled_char_size),
y + (curr_row * scaled_char_size),
scaled_char_size, scaled_char_size,
selection_color);
}
if (i == label->caret_pos.index)
{
// Draw the caret.
Draw_AlphaFillRGB(
x + (curr_col * scaled_char_size),
y + (curr_row * scaled_char_size),
max(5, Q_rint(scaled_char_size * 0.1)), scaled_char_size,
caret_color);
}
}
if (i == label->wordwraps[curr_row].index || label->text[i] == '\0')
{
// We found the end of a line, copy the contents of the line to the line buffer.
snprintf(line, min(LABEL_LINE_SIZE, (i - last_index) + 1), "%s", (label->text + last_index + 1));
last_index = i; // Skip the newline character
if (label->ext_flags & label_largefont)
{
Draw_BigString(x, y + (curr_row * scaled_char_size), line, &text_color, 1, label->scale, 1, 0);
}
else
{
Draw_SColoredString(x, y + (curr_row * scaled_char_size), str2wcs(line), &text_color, 1, false, label->scale);
}
curr_row++;
curr_col = 0;
}
else
{
curr_col++;
}
}
// TODO : Remove this test stuff.
{
char tmp[1024];
int sublen = abs(label->select_end - label->select_start);
if (sublen > 0)
{
snprintf(tmp, sublen + 1, "%s", label->text + min(label->select_start, label->select_end));
Draw_String(x + (self->width / 2), y + (self->height / 2) + 8, tmp);
}
}
CONTROL_EVENT_HANDLER_CALL(NULL, self, ez_control_t, OnDraw, NULL);
return 0;
}
//
// Label - Finds the row and column for a specified index. The proper wordwrap indexes must've been calculated first.
//
static void EZ_label_FindRowColumnByIndex(ez_label_t *label, int index, int *row, int *column)
{
int i = 0;
// Find the row the index is on.
if (row)
{
// Find the wordwrap that's closest to the index but still larger
// that will be the end of the row that the index is on.
while ((label->wordwraps[i].index != -1) && (label->wordwraps[i].index < index))
{
i++;
}
// Save the row. (The last row as -1 as index, but still has the correct row saved).
(*row) = label->wordwraps[i].row;
}
if (column)
{
// We don't have the last rows end index saved explicitly, so get it here.
int row_end_index = (label->wordwraps[i].index < 0) ? strlen(label->text) : label->wordwraps[i].index;
// The column at the end of the row minus the difference between
// the given index and the index at the end of the row gives us
// what column the index is in.
(*column) = label->wordwraps[i].col - (row_end_index - index);
}
}
//
// Label - Finds the index in the label text at the specified row and column.
//
static int EZ_label_FindIndexByRowColumn(ez_label_t *label, int row, int column)
{
int row_end_index = 0;
// Make sure we don't do anythin stupid.
if ((row < 0) || (row > (label->num_rows - 1)) || (column < 0)) // || (label->wordwraps[row].index < 0))
{
return -1;
}
// We don't have the last rows end index saved explicitly, so get it here.
row_end_index = (label->wordwraps[row].index < 0) ? strlen(label->text) : label->wordwraps[row].index;
if (column > label->wordwraps[row].col)
{
// There is no such column on the specified row
// return the index of the last character on that row instead.
return row_end_index;
}
// Return the index of the specified column.
return row_end_index - (label->wordwraps[row].col - column);
}
//
// Label - Finds at what text index the mouse was clicked.
//
static int EZ_label_FindMouseTextIndex(ez_label_t *label, mouse_state_t *ms)
{
// TODO : Support big font gaps?
int x, y;
int row, col;
// Get the position we're drawing at.
EZ_control_GetDrawingPosition((ez_control_t *)label, &x, &y);
// Find the row and column where the mouse is.
row = ((ms->y - y) / label->scaled_char_size);
col = ((ms->x - x) / label->scaled_char_size);
// Give the last index if the mouse is past the last row.
if (row > (label->num_rows - 1))
{
return label->text_length;
}
return EZ_label_FindIndexByRowColumn(label, row, col) + 1;
}
//
// Label - Set the caret position.
//
void EZ_label_SetCaretPosition(ez_label_t *label, int caret_pos)
{
if (label->ext_flags & label_selectable)
{
label->caret_pos.index = caret_pos;
clamp(label->caret_pos.index, -1, label->text_length);
}
else
{
label->caret_pos.index = -1;
EZ_label_DeselectText(label);
}
CONTROL_RAISE_EVENT(NULL, label, ez_label_t, OnCaretMoved, NULL);
}
//
// Label - Moves the caret up or down.
//
static void EZ_label_MoveCaretVertically(ez_label_t *label, int amount)
{
// Find the index of the character above/below that position,
// and set the caret to that position, if there is no char
// exactly above that point, jump to the end of the line
// of the previous/next row instead.
int new_caret_index = EZ_label_FindIndexByRowColumn(label, max(0, label->caret_pos.row + amount), label->caret_pos.col);
if (new_caret_index >= 0)
{
EZ_label_SetCaretPosition(label, new_caret_index);
}
}
//
// Label - The caret was moved.
//
int EZ_label_OnCaretMoved(ez_control_t *self, void *ext_event_info)
{
ez_label_t *label = (ez_label_t *)self;
int scaled_char_size = label->scaled_char_size;
int row_visible_start = 0;
int row_visible_end = 0;
int col_visible_start = 0;
int col_visible_end = 0;
int prev_caret_row = label->caret_pos.row;
int prev_caret_col = label->caret_pos.col;
int new_scroll_x = self->virtual_x;
int new_scroll_y = self->virtual_y;
int num_visible_rows = Q_rint((float)self->height / scaled_char_size - 1); // The number of currently visible rows.
int num_visible_cols = Q_rint((float)self->width / scaled_char_size - 1);
// Find the new row and column of the caret.
EZ_label_FindRowColumnByIndex(label, label->caret_pos.index, &label->caret_pos.row, &label->caret_pos.col);
row_visible_start = (self->virtual_y / scaled_char_size);
row_visible_end = (row_visible_start + num_visible_rows);
// Vertical scrolling.
if (label->caret_pos.row <= row_visible_start)
{
// Caret at the top of the visible part of the text.
new_scroll_y = label->caret_pos.row * scaled_char_size;
}
else if (prev_caret_row >= label->caret_pos.row)
{
// Caret in the middle.
}
else if (label->caret_pos.row >= (row_visible_end - 1))
{
// Caret at the bottom of the visible part.
new_scroll_y = (label->caret_pos.row - num_visible_rows) * scaled_char_size;
// Special case if it's the last line. Make sure we're allowed to
// see the entire row, by scrolling a bit extra at the end.
if ((label->caret_pos.row == (label->num_rows - 1)) && (new_scroll_y < label->super.virtual_height_min))
{
new_scroll_y += Q_rint(0.2 * scaled_char_size);
}
}
// Only scroll horizontally if the text is not wrapped.
if (!(label->ext_flags & label_wraptext))
{
col_visible_start = (self->virtual_x / scaled_char_size);
col_visible_end = (col_visible_start + num_visible_cols);
if (label->caret_pos.col <= (col_visible_start + 1))
{
// At the start of the visible text (columns).
new_scroll_x = (label->caret_pos.col - 1) * scaled_char_size;
}
else if (prev_caret_col >= label->caret_pos.col)
{
// In the middle of the visible text.
}
else if (label->caret_pos.col >= (col_visible_end - 1))
{
// At the end of the visible text.
new_scroll_x = (label->caret_pos.col - num_visible_cols) * scaled_char_size;
}
}
// Only set a new scroll if it changed.
if ((new_scroll_x != self->virtual_x) || (new_scroll_y != self->virtual_y))
{
EZ_control_SetScrollPosition(self, new_scroll_x, new_scroll_y);
}
// Select while holding down shift.
if (keydown[K_SHIFT] && (label->select_start > -1))
{
label->int_flags |= label_selecting;
label->select_end = label->caret_pos.index;
}
// Deselect any selected text if we're not in selection mode.
if (!(label->int_flags & label_selecting))
{
EZ_label_DeselectText(label);
}
CONTROL_EVENT_HANDLER_CALL(NULL, label, ez_label_t, OnCaretMoved, NULL);
return 0;
}
//
// Label - The text changed in the label.
//
int EZ_label_OnTextChanged(ez_control_t *self, void *ext_event_info)
{
ez_label_t *label = (ez_label_t *)self;
// Make sure we have the correct text length saved.
label->text_length = label->text ? strlen(label->text) : 0;
// Find the new places to wrap on.
EZ_label_CalculateWordwraps(label);
if (label->ext_flags & label_autosize)
{
// Only resize after the number of rows if we're wrapping the text.
if (label->ext_flags & label_wraptext)
{
EZ_control_SetSize(self, self->width, (label->scaled_char_size * label->num_rows));
}
else
{
EZ_control_SetSize(self, (label->scaled_char_size * label->num_cols), (label->scaled_char_size * label->num_rows));
}
}
CONTROL_EVENT_HANDLER_CALL(NULL, label, ez_label_t, OnTextChanged, NULL);
return 0;
}
//
// Label - Handle a page up/dn key press.
//
static void EZ_label_PageUpDnKeyDown(ez_label_t *label, int key)
{
int num_visible_rows = Q_rint((float)label->super.height / label->scaled_char_size);
switch (key)
{
case K_PGUP :
{
EZ_label_MoveCaretVertically(label, -num_visible_rows);
break;
}
case K_PGDN :
{
EZ_label_MoveCaretVertically(label, num_visible_rows);
break;
}
default :
{
break;
}
}
}
//
// Label - Gets the next word boundary.
//
static int EZ_label_GetNextWordBoundary(ez_label_t *label, int cur_pos, qbool forward)
{
char *txt = label->text;
int dir = forward ? 1 : -1;
// Make sure we're not out of bounds.
clamp(cur_pos, 0, label->text_length);
// Eat all the whitespaces.
while ((cur_pos >= 0) && (cur_pos <= label->text_length) && ((txt[cur_pos] == ' ') || (txt[cur_pos] == '\n')))
{
cur_pos += dir;
}
// Find the next word boundary.
while ((cur_pos >= 0) && (cur_pos <= label->text_length) && (txt[cur_pos] != ' ') && (txt[cur_pos] != '\n'))
{
cur_pos += dir;
}
return clamp(cur_pos, 0, label->text_length);
}
//
// Label - Handle a arrow key press.
//
static void EZ_label_ArrowKeyDown(ez_label_t *label, int key)
{
switch(key)
{
case K_LEFTARROW :
{
if (keydown[K_CTRL])
{
EZ_label_SetCaretPosition(label, EZ_label_GetNextWordBoundary(label, label->caret_pos.index, false));
}
else
{
EZ_label_SetCaretPosition(label, max(0, label->caret_pos.index - 1));
}
break;
}
case K_RIGHTARROW :
{
if (keydown[K_CTRL])
{
EZ_label_SetCaretPosition(label, EZ_label_GetNextWordBoundary(label, label->caret_pos.index, true));
}
else
{
EZ_label_SetCaretPosition(label, label->caret_pos.index + 1);
}
break;
}
case K_UPARROW :
{
EZ_label_MoveCaretVertically(label, -1);
break;
}
case K_DOWNARROW :
{
EZ_label_MoveCaretVertically(label, 1);
break;
}
default :
{
break;
}
}
}
//
// Label - Handle a HOME/END key press.
//
static void EZ_label_EndHomeKeyDown(ez_label_t *label, int key)
{
switch(key)
{
case K_HOME :
{
if (keydown[K_CTRL])
{
// Move the caret to the start of the text.
EZ_label_SetCaretPosition(label, 0);
}
else
{
// Move the caret to the start of the current line.
EZ_label_SetCaretPosition(label, label->caret_pos.index - label->caret_pos.col + 1);
}
break;
}
case K_END :
{
if (keydown[K_CTRL])
{
// Move the caret to the end of the text.
EZ_label_SetCaretPosition(label, label->text_length);
}
else
{
// Move the caret to the end of the line.
int i = 0;
int line_end_index = 0;
// Find the last index of the current row.
while ((label->wordwraps[i].index > 0) && (label->wordwraps[i].index < label->caret_pos.index))
{
i++;
}
// Special case for last line.
line_end_index = (label->wordwraps[i].index < 0) ? label->text_length : label->wordwraps[i].index;