82 lines
1.4 KiB
C
82 lines
1.4 KiB
C
#include "init.h"
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include "Input.h"
|
|
|
|
Input Input_create(Window window)
|
|
{
|
|
Input this;
|
|
|
|
this = malloc(sizeof(struct Input_struct));
|
|
if (this == NULL) {
|
|
fprintf(stderr, "malloc failed\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
this->window = window;
|
|
this->line = Line_create();
|
|
|
|
return this;
|
|
}
|
|
|
|
void Input_destroy(Input this)
|
|
{
|
|
if (this == NULL) {
|
|
return;
|
|
}
|
|
|
|
Line_destroy(this->line);
|
|
free(this);
|
|
}
|
|
|
|
void Input_clear(Input this)
|
|
{
|
|
Line_clear(this->line);
|
|
}
|
|
|
|
void Input_updateCursorPosition(Input this)
|
|
{
|
|
int width;
|
|
int i;
|
|
|
|
width = 0;
|
|
for (i = 0; i < Line_size(this->line); i++) {
|
|
width += Character_width(Line_getCharacter(this->line, i));
|
|
}
|
|
Window_setCursorPosition(this->window, width, 0);
|
|
}
|
|
|
|
void Input_paint(Input this)
|
|
{
|
|
int width;
|
|
int i;
|
|
|
|
Window_setCursorPosition(this->window, 0, 0);
|
|
width = 0;
|
|
for (i = 0; i < Line_size(this->line); i++) {
|
|
Character c;
|
|
|
|
c = Line_getCharacter(this->line, i);
|
|
Character_write(c, stdout);
|
|
width += Character_width(c);
|
|
}
|
|
for (i = width; i < Window_width(this->window); i++) {
|
|
fputc(' ', stdout);
|
|
}
|
|
}
|
|
|
|
void Input_keyTyped(Input this, int keyChar)
|
|
{
|
|
if (keyChar == 0x08 || keyChar == 0x7f) {
|
|
int size;
|
|
|
|
size = Line_size(this->line);
|
|
if (size > 0) {
|
|
Line_removeCharacter(this->line, size - 1);
|
|
}
|
|
} else {
|
|
Line_addCharacter(this->line, Line_size(this->line), keyChar);
|
|
}
|
|
}
|