Files
Test/ui.cpp

68 lines
2.0 KiB
C++
Raw Normal View History

2025-02-19 12:31:48 +01:00
// SPDX-FileCopyrightText: 2025 Krzysztof <email>
// SPDX-License-Identifier: Apache-2.0
#include "ui.h"
#include <ncurses.h>
#include <string>
#include <cstring>
Ui::Ui() {
initscr();
noecho();
cbreak();
curs_set(1); // Pokaż kursor
}
// Destruktor: zakończenie pracy z ncurses
Ui::~Ui() {
endwin();
}
// Rysowanie interfejsu logowania
void Ui::draw_ui(WINDOW *win, int width) const {
box(win, 0, 0);
mvwprintw(win, 2, (width - std::strlen("Login:")) / 2, "Login:");
mvwprintw(win, 4, (width - std::strlen("Password:")) / 2, "Password:");
mvwprintw(win, 7, (width - std::strlen("[Press ENTER to submit]")) / 2, "[Press ENTER to submit]");
wrefresh(win);
}
// Obsługa wprowadzania danych
void Ui::handle_input() const {
int height, width;
getmaxyx(stdscr, height, width);
int win_width = 40;
int win_height = 10;
int start_y = (height - win_height) / 2;
int start_x = (width - win_width) / 2;
// Tworzenie okna
WINDOW *win = newwin(win_height, win_width, start_y, start_x);
keypad(win, TRUE);
// Rysowanie interfejsu
draw_ui(win, win_width);
// Pola do przechowywania wprowadzonych danych
char login[30] = {0};
char password[30] = {0};
// Obsługa wprowadzania danych
mvwgetnstr(win, 3, (win_width - 30) / 2, login, sizeof(login) - 1); // Wprowadzenie loginu
mvwgetnstr(win, 5, (win_width - 30) / 2, password, sizeof(password) - 1); // Wprowadzenie hasła
// Czyszczenie ekranu i wyświetlenie wyników
clear();
mvprintw(height / 2 - 1, (width - std::strlen("Submitted Data")) / 2, "Submitted Data");
mvprintw(height / 2, (width - std::strlen("Login: ")) / 2, "Login: %s", login);
mvprintw(height / 2 + 1, (width - std::strlen("Password: ")) / 2, "Password: %s", password);
mvprintw(height / 2 + 3, (width - std::strlen("[Press any key to exit]")) / 2, "[Press any key to exit]");
refresh();
// Oczekiwanie na klawisz
getch();
// Sprzątanie
delwin(win);
}