/
GUI_textentry.c
122 lines (106 loc) · 3.25 KB
/
GUI_textentry.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
/*
This file is part of TableGUI.
Copyright (c) 2014 Felipe Ferreira da Silva
TableGUI is licensed under MIT license.
See the file "LICENSE" for license details.
*/
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>
#include <basicutils.h>
#include "GUI.h"
#include "GUI_font.h"
#include "GUI_widget.h"
#include "GUI_theme.h"
#include "GUI_textentry.h"
char * GUITextEntry_GetPrintableCharacters(TIterator *AIterator)
{
unsigned char *BText = (unsigned char *)malloc(sizeof(unsigned char));
BText[0] = '\0';
int BSize = 1;
while ((isdigit(*(unsigned char *)AIterator->FPointer)) &&
((unsigned char *)AIterator->FPointer != ' '))
{
BSize++;
BText = (unsigned char *)realloc(BText, sizeof(unsigned char *) * BSize);
AIterator = AIterator->FNext;
};
return BText;
};
void GUITextEntry_KeyDown(TGUIWidget *AWidget, int AKey, int AScanCode, int AMods)
{
TTextEntryProperties *BTextEntryProperties = Widget_GetPointer(AWidget, "text");
if (((AKey >= 32) && (AKey <= 96)) ||
(AKey == GLFW_KEY_ENTER) ||
(AKey == GLFW_KEY_TAB))
{
unsigned char *BChar = (unsigned char *)malloc(sizeof(unsigned char));
*BChar = AKey;
TIterator *BIterator = Alloc_Iterator(BChar);
Iterator_Insert(BTextEntryProperties->FText, BTextEntryProperties->FCursor, BIterator, ipBefore);
BTextEntryProperties->FCursor = BIterator;
} else
{
switch (AKey)
{
case GLFW_KEY_BACKSPACE:
{
if (BTextEntryProperties->FCursor->FPrevious)
{
Iterator_Remove(BTextEntryProperties->FText, BTextEntryProperties->FCursor->FPrevious);
};
break;
};
};
};
};
void GUITextEntry_MouseDown(TGUIWidget *AWidget, int AButton, int AMouseX, int AMouseY)
{
GUIWidget_MouseDown(AWidget, AButton, AMouseX, AMouseY);
};
void GUITextEntry_Draw(TGUIWidget *AWidget)
{
TTextEntryProperties *BTextEntryProperties = Widget_GetPointer(AWidget, "text");
TIterator *BIterator = BTextEntryProperties->FText->FFirstChild;
int BLine = 0;
int BLineHeight = glTextHeight(AWidget->FWindow->FFont, "Wg");
int BX = 0;
unsigned char *BWord = NULL;
while (BIterator)
{
// Draw only the text in the visible area.
if isdigit(*(unsigned char *)BIterator->FPointer)
{
BWord = GUITextEntry_GetPrintableCharacters(BIterator);
glPrint(AWidget->FWindow->FFont, BX, BLine * BLineHeight, BWord);
BX = BX + glTextWidth(AWidget->FWindow->FFont, BWord);
free(BWord);
} else
{
if (*(unsigned char *)BIterator->FPointer == '\n')
{
BX = 0;
} else
if (*(unsigned char *)BIterator->FPointer == ' ')
{
BX = BX + glTextWidth(AWidget->FWindow->FFont, " \0");
} else
if (*(unsigned char *)BIterator->FPointer == '\0')
{
break;
};
};
BIterator = BIterator->FNext;
};
};
void Alloc_GUITextEntry(TGUIWidget *AWidget)
{
Alloc_GUIWidget(AWidget);
TTextEntryProperties *BTextEntryProperties = (TTextEntryProperties *)malloc(sizeof(BTextEntryProperties));
Widget_AddPointer(AWidget, "text", BTextEntryProperties);
Widget_AddPointer(AWidget, "cur", NULL);
AWidget->FDraw = &GUITextEntry_Draw;
AWidget->FMouseDown = &GUITextEntry_MouseDown;
};