-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f70c8d9
commit c19b3e5
Showing
15 changed files
with
363 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
#include "LineEdit.h" | ||
#include "KeyCodes.h" | ||
|
||
TLineEdit *LineEditCreate(int Flags) | ||
{ | ||
TLineEdit *LE; | ||
|
||
LE=(TLineEdit *) calloc(1, sizeof(TLineEdit)); | ||
LE->Flags = Flags; | ||
if (Flags & LINE_EDIT_HISTORY) LE->History=ListCreate(); | ||
LE->MaxHistory=30; | ||
|
||
return(LE); | ||
} | ||
|
||
void LineEditDestroy(TLineEdit *LE) | ||
{ | ||
if (LE) | ||
{ | ||
if (LE->History) ListDestroy(LE->History, Destroy); | ||
Destroy(LE->Line); | ||
free(LE); | ||
} | ||
} | ||
|
||
|
||
void LineEditSetText(TLineEdit *LE, const char *Text) | ||
{ | ||
LE->Line=CopyStr(LE->Line, Text); | ||
LE->Len=StrLen(LE->Line); | ||
LE->Cursor=LE->Len; | ||
} | ||
|
||
|
||
void LineEditClearHistory(TLineEdit *LE) | ||
{ | ||
if (LE->History) ListClear(LE->History, Destroy); | ||
} | ||
|
||
void LineEditAddToHistory(TLineEdit *LE, const char *Text) | ||
{ | ||
ListNode *Node; | ||
|
||
if (StrValid(Text)) | ||
{ | ||
if (! LE->History) LE->History=ListCreate(); | ||
|
||
if (ListSize(LE->History) > LE->MaxHistory) | ||
{ | ||
Node=ListGetNext(LE->History); | ||
ListDeleteNode(Node); | ||
} | ||
|
||
Node=ListGetLast(LE->History); | ||
|
||
if (Node && Node->Tag) | ||
{ | ||
if (strcmp(Node->Tag, Text) !=0) ListAddNamedItem(LE->History, Text, NULL); | ||
} | ||
else ListAddNamedItem(LE->History, Text, NULL); | ||
} | ||
|
||
} | ||
|
||
int LineEditHandleChar(TLineEdit *LE, int Char) | ||
{ | ||
char *Tempstr=NULL; | ||
char *ptr; | ||
|
||
switch (Char) | ||
{ | ||
//seems like control-c sends this | ||
case STREAM_NODATA: | ||
case ESCAPE: | ||
return(LINE_EDIT_CANCEL); | ||
break; | ||
|
||
case STREAM_TIMEOUT: | ||
break; | ||
|
||
case TKEY_UP: | ||
if (LE->History) | ||
{ | ||
if (LE->History->Side) | ||
{ | ||
if (LE->History->Side != ListGetNext(LE->History)) LE->History->Side=ListGetPrev(LE->History->Side); | ||
} | ||
else LE->History->Side=ListGetLast(LE->History); | ||
|
||
if (LE->History->Side) LineEditSetText(LE, LE->History->Side->Tag); | ||
} | ||
break; | ||
|
||
case TKEY_DOWN: | ||
if (LE->History) | ||
{ | ||
if (LE->History->Side) | ||
{ | ||
LE->History->Side=ListGetNext(LE->History->Side); | ||
if (LE->History->Side) LineEditSetText(LE, LE->History->Side->Tag); | ||
} | ||
} | ||
break; | ||
|
||
case TKEY_LEFT: | ||
if ((! (LE->Flags & LINE_EDIT_NOMOVE)) && (LE->Cursor > 0)) LE->Cursor--; | ||
break; | ||
|
||
case TKEY_RIGHT: | ||
if ((! (LE->Flags & LINE_EDIT_NOMOVE)) && (LE->Cursor < LE->Len)) LE->Cursor++; | ||
break; | ||
|
||
case TKEY_HOME: | ||
if ((! (LE->Flags & LINE_EDIT_NOMOVE)) && (LE->Cursor < LE->Len)) LE->Cursor=0; | ||
break; | ||
|
||
case TKEY_END: | ||
if ((! (LE->Flags & LINE_EDIT_NOMOVE)) && (LE->Cursor < LE->Len)) LE->Cursor=LE->Len; | ||
break; | ||
|
||
//'backspace' key on keyboard will send the 'del' character in some cases! | ||
case 0x7f: //this is 'del' | ||
if (LE->Cursor < LE->Len) | ||
{ | ||
ptr=LE->Line + LE->Cursor; | ||
if (LE->Cursor < LE->Len) memmove(ptr, ptr +1, LE->Len - LE->Cursor); | ||
LE->Len--; | ||
StrTrunc(LE->Line, LE->Len); | ||
} | ||
break; | ||
|
||
case TKEY_BACKSPACE: | ||
if (LE->Cursor > 0) | ||
{ | ||
LE->Cursor--; | ||
ptr=LE->Line + LE->Cursor; | ||
if (LE->Cursor < LE->Len) memmove(ptr, ptr +1, LE->Len - LE->Cursor); | ||
LE->Len--; | ||
StrTrunc(LE->Line, LE->Len); | ||
} | ||
break; | ||
|
||
case TKEY_ENTER: | ||
if (LE->History) LE->History->Side=NULL; | ||
return(LINE_EDIT_ENTER); | ||
break; | ||
|
||
default: | ||
if (LE->Cursor == LE->Len) LE->Line=AddCharToBuffer(LE->Line, LE->Cursor, Char); | ||
else | ||
{ | ||
Tempstr=CopyStrLen(Tempstr, LE->Line, LE->Cursor); | ||
Tempstr=AddCharToBuffer(Tempstr, LE->Cursor, Char); | ||
Tempstr=CatStr(Tempstr, LE->Line + LE->Cursor); | ||
LE->Line=CopyStr(LE->Line, Tempstr); | ||
} | ||
LE->Cursor++; | ||
LE->Len++; | ||
break; | ||
} | ||
|
||
Destroy(Tempstr); | ||
|
||
return(LE->Cursor); | ||
} | ||
|
||
|
||
char *LineEditDone(char *RetStr, TLineEdit *LE) | ||
{ | ||
RetStr=CopyStr(RetStr, LE->Line); | ||
if (LE->Flags & LINE_EDIT_HISTORY) LineEditAddToHistory(LE, LE->Line); | ||
LE->Line=CopyStr(LE->Line, ""); | ||
LE->Len=0; | ||
LE->Cursor=0; | ||
|
||
return(RetStr); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#ifndef LIBUSEFUL_LINEEDIT_H | ||
#define LIBUSEFUL_LINEEDIT_H | ||
|
||
/* | ||
This module supplies a line-editor with a history. | ||
Example code using this can be found in Terminal.h in 'TerminalReadText' or in examples/EditLine.c | ||
The 'LineEditCreate' command can be passed the flags: | ||
LINE_EDIT_HISTORY - provide an recallable history of entered lines | ||
LINE_EDIT_NOMOVE - don't allow use of left/right to move back into the text | ||
this is used for password entry text that is invisible and | ||
thus we cannot allow the user to use full editing, as they | ||
can become 'lost'. | ||
The 'EditLineHandleChar' function does most of the work, it expects to receive | ||
the following characters (defined in KeyCodes.h) | ||
TKEY_LEFT - move cursor left within text | ||
TKEY_RIGHT - move cursor right within text | ||
TKEY_UP - if history mode is active, recall previous entry | ||
TKEY_DOWN - if moving through history goto next entry | ||
TKEY_HOME - move cursor to start of text | ||
TKEY_END - move cursor to end of text | ||
TKEY_BACKSPACE - backspace delete previous character | ||
TKEY_ERASE - erase curr character | ||
TKEY_ENTER - return/enter line | ||
TKEY_ESCAPE - cancel line editing | ||
The 'EditLineHandleChar' function returns the cursor position within the edited line, or if 'enter' is pressed it will return the value LINE_EDIT_ENTER, if 'escape' is pressed then it will return LINE_EDIT_CANCEL. | ||
When LINE_EDIT_ENTER is called 'LineEditDone' should be used to collect the typed line, as it will reset the LineEdit for a new line to be typed in, and if history mode is active, it will add the typed line to the history. | ||
*/ | ||
|
||
|
||
#include "includes.h" | ||
#include "List.h" | ||
|
||
|
||
#define LINE_EDIT_ENTER -1 | ||
#define LINE_EDIT_CANCEL -2 | ||
|
||
#define LINE_EDIT_HISTORY 1 | ||
#define LINE_EDIT_NOMOVE 2 | ||
|
||
typedef struct | ||
{ | ||
char *Line; | ||
int Flags; | ||
int Len; | ||
int MaxHistory; | ||
int Cursor; | ||
ListNode *History; | ||
} TLineEdit; | ||
|
||
|
||
#define LineEditSetMaxHistory(LE, Max) ((LE)->MaxHistory = (Max)) | ||
|
||
TLineEdit *LineEditCreate(int Flags); | ||
void LineEditSetText(TLineEdit *LE, const char *Text); | ||
void LineEditDestroy(TLineEdit *LE); | ||
int LineEditHandleChar(TLineEdit *LE, int Char); | ||
char *LineEditDone(char *RetStr, TLineEdit *LE); | ||
void LineEditClearHistory(TLineEdit *LE); | ||
void LineEditAddToHistory(TLineEdit *LE, const char *Text); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.