2016-09-27 22:01:08 +03:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2001-11-05 02:05:21 +03:00
|
|
|
#include "cmCursesStringWidget.h"
|
2016-04-29 16:40:20 +03:00
|
|
|
|
2016-09-01 22:55:09 +03:00
|
|
|
#include "cmCursesForm.h"
|
2001-12-13 21:28:41 +03:00
|
|
|
#include "cmCursesMainForm.h"
|
2016-09-01 22:55:09 +03:00
|
|
|
#include "cmCursesStandardIncludes.h"
|
|
|
|
#include "cmCursesWidget.h"
|
|
|
|
#include "cmState.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2001-12-04 19:16:04 +03:00
|
|
|
|
2001-11-05 02:05:21 +03:00
|
|
|
inline int ctrl(int z)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
return (z & 037);
|
2012-08-13 21:42:58 +04:00
|
|
|
}
|
2001-11-05 02:05:21 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmCursesStringWidget::cmCursesStringWidget(int width, int height, int left,
|
|
|
|
int top)
|
|
|
|
: cmCursesWidget(width, height, left, top)
|
2001-11-05 02:05:21 +03:00
|
|
|
{
|
2006-03-16 18:44:55 +03:00
|
|
|
this->InEdit = false;
|
2015-04-07 23:45:54 +03:00
|
|
|
this->Type = cmState::STRING;
|
2016-05-16 17:34:04 +03:00
|
|
|
set_field_fore(this->Field, A_NORMAL);
|
|
|
|
set_field_back(this->Field, A_STANDOUT);
|
|
|
|
field_opts_off(this->Field, O_STATIC);
|
2001-11-05 02:05:21 +03:00
|
|
|
}
|
|
|
|
|
2016-08-17 02:49:57 +03:00
|
|
|
void cmCursesStringWidget::OnTab(cmCursesMainForm* /*unused*/,
|
|
|
|
WINDOW* /*unused*/)
|
2002-11-05 01:37:21 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
// FORM* form = fm->GetForm();
|
2002-11-05 01:37:21 +03:00
|
|
|
}
|
|
|
|
|
2016-08-17 02:49:57 +03:00
|
|
|
void cmCursesStringWidget::OnReturn(cmCursesMainForm* fm, WINDOW* /*unused*/)
|
2002-11-05 01:37:21 +03:00
|
|
|
{
|
|
|
|
FORM* form = fm->GetForm();
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->InEdit) {
|
2002-11-05 01:37:21 +03:00
|
|
|
cmCursesForm::LogMessage("String widget leaving edit.");
|
2006-03-16 18:44:55 +03:00
|
|
|
this->InEdit = false;
|
2002-11-05 01:37:21 +03:00
|
|
|
fm->PrintKeys();
|
2006-03-16 18:44:55 +03:00
|
|
|
delete[] this->OriginalString;
|
2002-11-05 01:37:21 +03:00
|
|
|
// trick to force forms to update the field buffer
|
|
|
|
form_driver(form, REQ_NEXT_FIELD);
|
|
|
|
form_driver(form, REQ_PREV_FIELD);
|
2006-03-16 18:44:55 +03:00
|
|
|
this->Done = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2002-11-05 01:37:21 +03:00
|
|
|
cmCursesForm::LogMessage("String widget entering edit.");
|
2006-03-16 18:44:55 +03:00
|
|
|
this->InEdit = true;
|
2002-11-05 01:37:21 +03:00
|
|
|
fm->PrintKeys();
|
2006-03-16 18:44:55 +03:00
|
|
|
char* buf = field_buffer(this->Field, 0);
|
2016-05-16 17:34:04 +03:00
|
|
|
this->OriginalString = new char[strlen(buf) + 1];
|
2006-03-16 18:44:55 +03:00
|
|
|
strcpy(this->OriginalString, buf);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-11-05 01:37:21 +03:00
|
|
|
}
|
|
|
|
|
2016-08-17 02:49:57 +03:00
|
|
|
void cmCursesStringWidget::OnType(int& key, cmCursesMainForm* fm,
|
|
|
|
WINDOW* /*unused*/)
|
2002-11-05 01:37:21 +03:00
|
|
|
{
|
|
|
|
form_driver(fm->GetForm(), key);
|
|
|
|
}
|
2001-11-05 02:05:21 +03:00
|
|
|
|
2012-08-13 21:42:58 +04:00
|
|
|
bool cmCursesStringWidget::HandleInput(int& key, cmCursesMainForm* fm,
|
2002-10-24 02:03:27 +04:00
|
|
|
WINDOW* w)
|
2001-11-05 02:05:21 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
int x, y;
|
2001-12-13 21:28:41 +03:00
|
|
|
|
|
|
|
FORM* form = fm->GetForm();
|
2016-07-21 22:30:50 +03:00
|
|
|
// when not in edit mode, edit mode is entered by pressing enter or i (vim
|
|
|
|
// binding)
|
2001-11-05 02:05:21 +03:00
|
|
|
// 10 == enter
|
2016-07-21 22:30:50 +03:00
|
|
|
if (!this->InEdit && (key != 10 && key != KEY_ENTER && key != 'i')) {
|
2001-11-05 02:05:21 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2001-11-05 02:05:21 +03:00
|
|
|
|
2016-06-27 23:44:16 +03:00
|
|
|
this->OriginalString = CM_NULLPTR;
|
2006-03-16 18:44:55 +03:00
|
|
|
this->Done = false;
|
2001-11-05 02:05:21 +03:00
|
|
|
|
2001-12-04 19:16:04 +03:00
|
|
|
char debugMessage[128];
|
|
|
|
|
2001-11-05 02:05:21 +03:00
|
|
|
// <Enter> is used to change edit mode (like <Esc> in vi).
|
2016-05-16 17:34:04 +03:00
|
|
|
while (!this->Done) {
|
2001-12-04 19:16:04 +03:00
|
|
|
sprintf(debugMessage, "String widget handling input, key: %d", key);
|
|
|
|
cmCursesForm::LogMessage(debugMessage);
|
2001-12-13 21:28:41 +03:00
|
|
|
|
|
|
|
fm->PrintKeys();
|
|
|
|
|
|
|
|
getmaxyx(stdscr, y, x);
|
|
|
|
// If window too small, handle 'q' only
|
2016-05-16 17:34:04 +03:00
|
|
|
if (x < cmCursesMainForm::MIN_WIDTH || y < cmCursesMainForm::MIN_HEIGHT) {
|
2001-12-13 21:28:41 +03:00
|
|
|
// quit
|
2016-05-16 17:34:04 +03:00
|
|
|
if (key == 'q') {
|
2002-10-24 02:03:27 +04:00
|
|
|
return false;
|
2001-12-13 21:28:41 +03:00
|
|
|
}
|
2016-08-18 20:39:54 +03:00
|
|
|
key = getch();
|
|
|
|
continue;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2001-12-13 21:28:41 +03:00
|
|
|
|
2016-03-29 21:24:20 +03:00
|
|
|
// If resize occurred during edit, move out of edit mode
|
2016-07-21 22:30:50 +03:00
|
|
|
if (!this->InEdit && (key != 10 && key != KEY_ENTER && key != 'i')) {
|
2001-11-05 02:05:21 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2016-07-21 22:30:50 +03:00
|
|
|
// enter edit with return and i (vim binding)
|
|
|
|
if (!this->InEdit && (key == 10 || key == KEY_ENTER || key == 'i')) {
|
|
|
|
this->OnReturn(fm, w);
|
|
|
|
}
|
|
|
|
// leave edit with return (but not i -- not a toggle)
|
|
|
|
else if (this->InEdit && (key == 10 || key == KEY_ENTER)) {
|
2002-11-05 01:37:21 +03:00
|
|
|
this->OnReturn(fm, w);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (key == KEY_DOWN || key == ctrl('n') || key == KEY_UP ||
|
|
|
|
key == ctrl('p') || key == KEY_NPAGE || key == ctrl('d') ||
|
|
|
|
key == KEY_PPAGE || key == ctrl('u')) {
|
2006-03-16 18:44:55 +03:00
|
|
|
this->InEdit = false;
|
2012-08-13 21:42:58 +04:00
|
|
|
delete[] this->OriginalString;
|
2001-11-30 23:04:25 +03:00
|
|
|
// trick to force forms to update the field buffer
|
|
|
|
form_driver(form, REQ_NEXT_FIELD);
|
|
|
|
form_driver(form, REQ_PREV_FIELD);
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2001-11-06 06:10:52 +03:00
|
|
|
// esc
|
2016-05-16 17:34:04 +03:00
|
|
|
else if (key == 27) {
|
|
|
|
if (this->InEdit) {
|
2006-03-16 18:44:55 +03:00
|
|
|
this->InEdit = false;
|
2002-10-24 02:03:27 +04:00
|
|
|
fm->PrintKeys();
|
2006-03-16 18:44:55 +03:00
|
|
|
this->SetString(this->OriginalString);
|
|
|
|
delete[] this->OriginalString;
|
2012-08-13 21:42:58 +04:00
|
|
|
touchwin(w);
|
|
|
|
wrefresh(w);
|
2002-10-24 02:03:27 +04:00
|
|
|
return true;
|
2001-11-05 02:05:21 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (key == 9) {
|
2002-11-05 01:37:21 +03:00
|
|
|
this->OnTab(fm, w);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (key == KEY_LEFT || key == ctrl('b')) {
|
2001-11-05 02:05:21 +03:00
|
|
|
form_driver(form, REQ_PREV_CHAR);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (key == KEY_RIGHT || key == ctrl('f')) {
|
2001-11-05 02:05:21 +03:00
|
|
|
form_driver(form, REQ_NEXT_CHAR);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (key == ctrl('k')) {
|
2001-11-06 06:10:52 +03:00
|
|
|
form_driver(form, REQ_CLR_EOL);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (key == ctrl('a') || key == KEY_HOME) {
|
2001-11-06 06:10:52 +03:00
|
|
|
form_driver(form, REQ_BEG_FIELD);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (key == ctrl('e') || key == KEY_END) {
|
2001-11-06 06:10:52 +03:00
|
|
|
form_driver(form, REQ_END_FIELD);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (key == 127 || key == KEY_BACKSPACE) {
|
|
|
|
FIELD* cur = current_field(form);
|
|
|
|
form_driver(form, REQ_DEL_PREV);
|
|
|
|
if (current_field(form) != cur) {
|
|
|
|
set_current_field(form, cur);
|
2001-11-06 06:10:52 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (key == ctrl('d') || key == KEY_DC) {
|
2015-09-12 17:37:35 +03:00
|
|
|
form_driver(form, REQ_DEL_CHAR);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2002-11-05 01:37:21 +03:00
|
|
|
this->OnType(key, fm, w);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (!this->Done) {
|
2012-08-13 21:42:58 +04:00
|
|
|
touchwin(w);
|
|
|
|
wrefresh(w);
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
key = getch();
|
2001-11-05 02:05:21 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-11-05 01:37:21 +03:00
|
|
|
return true;
|
2001-11-05 02:05:21 +03:00
|
|
|
}
|
|
|
|
|
2014-02-22 04:05:55 +04:00
|
|
|
void cmCursesStringWidget::SetString(const std::string& value)
|
2001-11-05 02:05:21 +03:00
|
|
|
{
|
|
|
|
this->SetValue(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* cmCursesStringWidget::GetString()
|
|
|
|
{
|
|
|
|
return this->GetValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* cmCursesStringWidget::GetValue()
|
|
|
|
{
|
2006-03-16 18:44:55 +03:00
|
|
|
return field_buffer(this->Field, 0);
|
2001-11-05 02:05:21 +03:00
|
|
|
}
|
2001-12-13 21:28:41 +03:00
|
|
|
|
|
|
|
bool cmCursesStringWidget::PrintKeys()
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
int x, y;
|
2001-12-13 21:28:41 +03:00
|
|
|
getmaxyx(stdscr, y, x);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (x < cmCursesMainForm::MIN_WIDTH || y < cmCursesMainForm::MIN_HEIGHT) {
|
2001-12-13 21:28:41 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (this->InEdit) {
|
2015-09-12 17:35:36 +03:00
|
|
|
char fmt_s[] = "%s";
|
2001-12-13 21:28:41 +03:00
|
|
|
char firstLine[512];
|
|
|
|
// Clean the toolbar
|
2016-05-16 17:34:04 +03:00
|
|
|
for (int i = 0; i < 512; i++) {
|
2001-12-13 21:28:41 +03:00
|
|
|
firstLine[i] = ' ';
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2001-12-13 21:28:41 +03:00
|
|
|
firstLine[511] = '\0';
|
2016-05-16 17:34:04 +03:00
|
|
|
curses_move(y - 4, 0);
|
2015-09-12 17:35:36 +03:00
|
|
|
printw(fmt_s, firstLine);
|
2016-05-16 17:34:04 +03:00
|
|
|
curses_move(y - 3, 0);
|
2015-09-12 17:35:36 +03:00
|
|
|
printw(fmt_s, firstLine);
|
2016-05-16 17:34:04 +03:00
|
|
|
curses_move(y - 2, 0);
|
2015-09-12 17:35:36 +03:00
|
|
|
printw(fmt_s, firstLine);
|
2016-05-16 17:34:04 +03:00
|
|
|
curses_move(y - 1, 0);
|
2015-09-12 17:35:36 +03:00
|
|
|
printw(fmt_s, firstLine);
|
2001-12-13 21:28:41 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
curses_move(y - 3, 0);
|
2015-09-12 17:35:36 +03:00
|
|
|
printw(fmt_s, "Editing option, press [enter] to leave edit.");
|
2001-12-13 21:28:41 +03:00
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2016-08-18 20:39:54 +03:00
|
|
|
return false;
|
2001-12-13 21:28:41 +03:00
|
|
|
}
|