+void Renderer_RichText_HandleKeyFire(tWindow *Window, tRichText_Window *Info, const struct sWndMsg_KeyAction *Msg)
+{
+ tRichText_Line *line = Info->CursorLine;
+ size_t len = WriteUTF8(NULL, Msg->UCS32);
+ switch(Msg->UCS32)
+ {
+ case 0:
+ switch(Msg->KeySym)
+ {
+ case KEYSYM_RIGHTARROW:
+ if( Info->CursorBytePos == line->ByteLength )
+ break;
+ Info->CursorBytePos += ReadUTF8(line->Data + Info->CursorBytePos, NULL);
+ Info->CursorCol ++;
+ break;
+ case KEYSYM_LEFTARROW:
+ if( Info->CursorBytePos == 0 )
+ break;
+ Info->CursorBytePos -= ReadUTF8Rev(line->Data, Info->CursorBytePos, NULL);
+ Info->CursorCol --;
+ break;
+ case KEYSYM_UPARROW:
+ _SysDebug("TODO: RichText edit up line");
+ break;
+ case KEYSYM_DOWNARROW:
+ _SysDebug("TODO: RichText edit down line");
+ break;
+ default:
+ // No effect
+ return ;
+ }
+ break;
+ case '\n': // Newline
+ _SysDebug("TODO: RichText edit newline");
+ break;
+ case '\b': // Backspace
+ if( Info->CursorBytePos == 0 )
+ return ;
+ len = ReadUTF8Rev(line->Data, Info->CursorBytePos, NULL);
+ Info->CursorBytePos -= len;
+ Info->CursorCol --;
+ if(0)
+ case '\x7f': // Delete
+ len = ReadUTF8(line->Data + Info->CursorBytePos, NULL);
+ if( Info->CursorBytePos == line->ByteLength )
+ return ;
+ memmove(line->Data + Info->CursorBytePos, line->Data + Info->CursorBytePos + len,
+ line->ByteLength - Info->CursorBytePos - len);
+ line->ByteLength -= len;
+ _SysDebug("RichText: %p Backspace/Delete '%.*s'", Window,
+ line->ByteLength, line->Data);
+ break;
+ default:
+ // Increase buffer size
+ if( line->ByteLength + len > line->Space ) {
+ line->Space += LINE_SPACE_UNIT;
+ tRichText_Line *nl = realloc(line, sizeof(*line) + line->Space);
+ if( nl == NULL )
+ return ;
+ if( nl != line ) {
+ *(line->Prev ? &line->Prev->Next : &Info->FirstLine) = nl;
+ if(line->Next)
+ line->Next->Prev = nl;
+ if(Info->FirstVisLine == line)
+ Info->FirstVisLine = nl;
+ Info->CursorLine = nl;
+ line = nl;
+ }
+ }
+ // Shift data
+ memmove(line->Data + Info->CursorBytePos + len, line->Data + Info->CursorBytePos,
+ line->ByteLength - Info->CursorBytePos);
+ // Encode
+ WriteUTF8(line->Data + Info->CursorBytePos, Msg->UCS32);
+ Info->CursorBytePos += len;
+ Info->CursorCol ++;
+ line->ByteLength += len;
+
+ _SysDebug("RichText: %p Appended %X '%.*s' to line %i", Window,
+ Msg->UCS32, len, line->Data + Info->CursorBytePos - len,
+ line->Num);
+ break;
+ }
+ // Invalidate line
+ line->bIsClean = 0;
+ WM_Invalidate(Window, 1);
+}
+