Permalink
Browse files

mark TrimStart and TrimEnd functions as unsafe, start porting launche…

…r input widget
  • Loading branch information...
UnknownShadow200 committed Dec 2, 2018
1 parent c735fa6 commit 1513a20cd345d5c2b5958954de832115a04ada70
Showing with 103 additions and 12 deletions.
  1. +68 −0 src/LWidgets.c
  2. +23 −0 src/LWidgets.h
  3. +1 −1 src/ServerConnection.c
  4. +4 −4 src/String.c
  5. +2 −2 src/String.h
  6. +2 −2 src/Utils.c
  7. +3 −3 src/Widgets.c
@@ -145,6 +145,74 @@ void LButton_SetText(struct LButton* w, const String* text, const FontDesc* font
*#########################################################################################################################*/
CC_NOINLINE void LInput_Init(struct LInput* w, int width, int height, const char* hintText, const FontDesc* hintFont);
CC_NOINLINE void LInput_SetText(struct LInput* w, const String* text, const FontDesc* font);
CC_NOINLINE Rect2D LInput_MeasureCaret(struct LInput* w);
void LInput_AdvanceCaretPos(struct LInput* w, bool forwards) {
if (forwards && w->CaretPos == -1) return;
if (!forwards && w->CaretPos == 0) return;
if (w->CaretPos == -1 && !forwards) /* caret after text */
w->CaretPos = w->Text.length;
w->CaretPos += (forwards ? 1 : -1);
if (w->CaretPos < 0 || w->CaretPos >= w->Text.length) w->CaretPos = -1;
}
CC_NOINLINE void LInput_SetCaretToCursor(struct LInput* w, int x, int y);
bool LInput_Append(struct LInput* w, char c) {
if (w->TextFilter && !w->TextFilter(c)) return false;
if (c >= ' ' && c <= '~' && c != '&' && w->Text.length < w->Text.capacity) {
if (w->CaretPos == -1) {
String_Append(&w->Text, c);
} else {
String_InsertAt(&w->Text, w->CaretPos, c);
w->CaretPos++;
}
if (w->TextChanged) w->TextChanged(w);
return true;
}
return false;
}
bool LInput_Backspace(struct LInput* w) {
if (w->Text.length == 0 || w->CaretPos == 0) return false;
if (w->CaretPos == -1) {
String_DeleteAt(&w->Text, w->Text.length - 1);
} else {
String_DeleteAt(&w->Text, w->CaretPos - 1);
w->CaretPos--;
if (w->CaretPos == -1) w->CaretPos = 0;
}
if (w->TextChanged) TextChanged(w);
if (w->CaretPos >= w->Text.length) w->CaretPos = -1;
return true;
}
bool LInput_Delete(struct LInput* w) {
if (w->Text.length == 0 || w->CaretPos == -1) return false;
String_DeleteAt(&w->Text, w->CaretPos);
if (w->CaretPos == -1) w->CaretPos = 0;
if (w->TextChanged) w->TextChanged(w);
if (w->CaretPos >= w->Text.length) w->CaretPos = -1;
return true;
}
bool LInput_Clear(struct LInput* w) {
if (w->Text.length == 0) return false;
w->Text.length = 0;
if (w->TextChanged) w->TextChanged(w);
w->CaretPos = -1;
return true;
}
/* Sets the currently entered text to the contents of the system clipboard. */
CC_NOINLINE bool LInput_CopyFromClipboard(struct LInput* w, const String* text);
/*########################################################################################################################*
@@ -33,21 +33,44 @@ struct LButton {
CC_NOINLINE void LButton_Init(struct LButton* w, int width, int height);
CC_NOINLINE void LButton_SetText(struct LButton* w, const String* text, const FontDesc* font);
struct LInput;
struct LInput {
LWidget_Layout
int BaseWidth, RealWidth;
/* Text displayed when the user has not entered anything in the text field. */
const char* HintText;
/* Whether all characters should be rendered as *. */
bool Password;
/* Filter applied to text received from the clipboard. Can be NULL. */
void (*ClipboardFilter)(String* text);
/* Callback invoked when the text is changed. Can be NULL. */
void (*TextChanged)(struct LInput* w);
/* Filter that only lets certain characters be entered. Can be NULL. */
bool (*TextFilter)(char c);
/* Specifies the position that characters are inserted/deleted from. */
/* NOTE: -1 to insert/delete characters at end of the text. */
int CaretPos;
FontDesc Font, HintFont;
String Text;
int __TextHeight;
char __TextBuffer[STRING_SIZE];
};
CC_NOINLINE void LInput_Init(struct LInput* w, int width, int height, const char* hintText, const FontDesc* hintFont);
CC_NOINLINE void LInput_SetText(struct LInput* w, const String* text, const FontDesc* font);
CC_NOINLINE Rect2D LInput_MeasureCaret(struct LInput* w);
CC_NOINLINE void LInput_AdvanceCaretPos(struct LInput* w, bool forwards);
CC_NOINLINE void LInput_SetCaretToCursor(struct LInput* w, int x, int y);
/* Appends a character to the end of the currently entered text. */
CC_NOINLINE bool LInput_Append(struct LInput* w, char c);
/* Removes the character preceding the caret in the currently entered text. */
CC_NOINLINE bool LInput_Backspace(struct LInput* w);
/* Removes the character at the caret in the currently entered text. */
CC_NOINLINE bool LInput_Delete(struct LInput* w);
/* Resets the currently entered text to an empty string. */
CC_NOINLINE bool LInput_Clear(struct LInput* w);
/* Sets the currently entered text to the contents of the system clipboard. */
CC_NOINLINE bool LInput_CopyFromClipboard(struct LInput* w, const String* text);
struct LLabel {
LWidget_Layout
@@ -194,7 +194,7 @@ static void SPConnection_AddPart(const String* text) {
for (i = 0; i < tmp.length; i++) {
if (tmp.buffer[i] == '%') tmp.buffer[i] = '&';
}
String_TrimEnd(&tmp);
String_UNSAFE_TrimEnd(&tmp);
col = Drawer2D_LastCol(&tmp, tmp.length);
if (col) SPConnection_LastCol = col;
@@ -92,8 +92,8 @@ bool String_UNSAFE_Separate(STRING_REF const String* str, char c, String* key, S
*value = String_UNSAFE_SubstringAt(str, idx);
/* Trim key [c] value to just key[c]value */
String_TrimEnd(key);
String_TrimStart(value);
String_UNSAFE_TrimEnd(key);
String_UNSAFE_TrimStart(value);
return key->length > 0 && value->length > 0;
}
@@ -338,7 +338,7 @@ void String_DeleteAt(String* str, int offset) {
str->length--;
}
void String_TrimStart(String* str) {
void String_UNSAFE_TrimStart(String* str) {
int i;
for (i = 0; i < str->length; i++) {
if (str->buffer[i] != ' ') break;
@@ -348,7 +348,7 @@ void String_TrimStart(String* str) {
}
}
void String_TrimEnd(String* str) {
void String_UNSAFE_TrimEnd(String* str) {
int i;
for (i = str->length - 1; i >= 0; i--) {
if (str->buffer[i] != ' ') break;
@@ -117,10 +117,10 @@ CC_NOINLINE void String_InsertAt(String* str, int offset, char c);
CC_NOINLINE void String_DeleteAt(String* str, int offset);
/* Trims leading spaces from the given string. */
/* NOTE: Works by adjusting buffer/length, the characters in memory are not shifted. */
CC_NOINLINE void String_TrimStart(String* str);
CC_NOINLINE void String_UNSAFE_TrimStart(String* str);
/* Trims trailing spaces from the given string. */
/* NOTE: Works by adjusting buffer/length, the characters in memory are not shifted. */
CC_NOINLINE void String_TrimEnd(String* str);
CC_NOINLINE void String_UNSAFE_TrimEnd(String* str);
/* Returns first index of the given substring in the given string, -1 if not found. */
/* e.g. index of "ab" within "cbabd" is 2 */
@@ -259,8 +259,8 @@ void EntryList_Load(struct EntryList* list, EntryList_Filter filter) {
if (res == ERR_END_OF_STREAM) break;
if (res) { Chat_LogError2(res, "reading from", &path); break; }
String_TrimStart(&entry);
String_TrimEnd(&entry);
String_UNSAFE_TrimStart(&entry);
String_UNSAFE_TrimEnd(&entry);
if (!entry.length) continue;
if (filter && !filter(&entry)) continue;
@@ -1161,8 +1161,8 @@ static bool InputWidget_OtherKey(struct InputWidget* w, Key key) {
String_InitArray(text, textBuffer);
Window_GetClipboardText(&text);
String_TrimStart(&text);
String_TrimEnd(&text);
String_UNSAFE_TrimStart(&text);
String_UNSAFE_TrimEnd(&text);
if (!text.length) return true;
InputWidget_AppendString(w, &text);
@@ -1654,7 +1654,7 @@ static void ChatInputWidget_OnPressedEnter(void* widget) {
struct ChatInputWidget* w = widget;
/* Don't want trailing spaces in output message */
String text = w->Base.Text;
String_TrimEnd(&text);
String_UNSAFE_TrimEnd(&text);
if (text.length) { Chat_Send(&text, true); }
w->OrigStr.length = 0;

0 comments on commit 1513a20

Please sign in to comment.