| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- /*=========================================================================
- Program: Insight Segmentation & Registration Toolkit
- Module: $RCSfile$
- Language: C++
- Date: $Date$
- Version: $Revision$
- Copyright (c) 2002 Insight Consortium. All rights reserved.
- See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
- This software is distributed WITHOUT ANY WARRANTY; without even
- the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- PURPOSE. See the above copyright notices for more information.
- =========================================================================*/
- #include "cmCursesStringWidget.h"
- #include "cmCursesMainForm.h"
- inline int ctrl(int z)
- {
- return (z&037);
- }
- cmCursesStringWidget::cmCursesStringWidget(int width, int height,
- int left, int top) :
- cmCursesWidget(width, height, left, top)
- {
- m_InEdit = false;
- m_Type = cmCacheManager::STRING;
- set_field_fore(m_Field, A_NORMAL);
- set_field_back(m_Field, A_STANDOUT);
- field_opts_off(m_Field, O_STATIC);
- }
- bool cmCursesStringWidget::HandleInput(int& key, cmCursesMainForm* fm,
- WINDOW* w)
- {
- int x,y;
- FORM* form = fm->GetForm();
- // 10 == enter
- if (!m_InEdit && ( key != 10 ) )
- {
- return false;
- }
- char* originalStr=0;
- char debugMessage[128];
- // <Enter> is used to change edit mode (like <Esc> in vi).
- while(1)
- {
- sprintf(debugMessage, "String widget handling input, key: %d", key);
- cmCursesForm::LogMessage(debugMessage);
- fm->PrintKeys();
- getmaxyx(stdscr, y, x);
- // If window too small, handle 'q' only
- if ( x < cmCursesMainForm::MIN_WIDTH ||
- y < cmCursesMainForm::MIN_HEIGHT )
- {
- // quit
- if ( key == 'q' )
- {
- return false;
- }
- else
- {
- key=getch();
- continue;
- }
- }
- // If resize occured during edit, move out of edit mode
- if (!m_InEdit && ( key != 10 && key != KEY_ENTER ) )
- {
- return false;
- }
- // 10 == enter
- if (key == 10 || key == KEY_ENTER)
- {
- if (m_InEdit)
- {
- cmCursesForm::LogMessage("String widget leaving edit.");
- m_InEdit = false;
- fm->PrintKeys();
- delete[] originalStr;
- // trick to force forms to update the field buffer
- form_driver(form, REQ_NEXT_FIELD);
- form_driver(form, REQ_PREV_FIELD);
- return true;
- }
- else
- {
- cmCursesForm::LogMessage("String widget entering edit.");
- m_InEdit = true;
- fm->PrintKeys();
- char* buf = field_buffer(m_Field, 0);
- originalStr = new char[strlen(buf)+1];
- strcpy(originalStr, buf);
- }
- }
- 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'))
- {
- m_InEdit = false;
- delete[] originalStr;
- // trick to force forms to update the field buffer
- form_driver(form, REQ_NEXT_FIELD);
- form_driver(form, REQ_PREV_FIELD);
- return false;
- }
- // esc
- else if (key == 27)
- {
- if (m_InEdit)
- {
- m_InEdit = false;
- fm->PrintKeys();
- this->SetString(originalStr);
- delete[] originalStr;
- touchwin(w);
- wrefresh(w);
- return true;
- }
- }
- else if ( key == KEY_LEFT || key == ctrl('b') )
- {
- form_driver(form, REQ_PREV_CHAR);
- }
- else if ( key == KEY_RIGHT || key == ctrl('f') )
- {
- form_driver(form, REQ_NEXT_CHAR);
- }
- else if ( key == ctrl('k') )
- {
- form_driver(form, REQ_CLR_EOL);
- }
- else if ( key == ctrl('a') )
- {
- form_driver(form, REQ_BEG_FIELD);
- }
- else if ( key == ctrl('e') )
- {
- form_driver(form, REQ_END_FIELD);
- }
- else if ( key == ctrl('d') || key == 127 ||
- key == KEY_BACKSPACE || key == KEY_DC )
- {
- form_driver(form, REQ_DEL_PREV);
- }
- else
- {
- form_driver(form, key);
- }
- touchwin(w);
- wrefresh(w);
- key=getch();
- }
- }
- void cmCursesStringWidget::SetString(const char* value)
- {
- this->SetValue(value);
- }
- const char* cmCursesStringWidget::GetString()
- {
- return this->GetValue();
- }
- const char* cmCursesStringWidget::GetValue()
- {
- return field_buffer(m_Field, 0);
- }
- bool cmCursesStringWidget::PrintKeys()
- {
- int x,y;
- getmaxyx(stdscr, y, x);
- if ( x < cmCursesMainForm::MIN_WIDTH ||
- y < cmCursesMainForm::MIN_HEIGHT )
- {
- return false;
- }
- if (m_InEdit)
- {
- char firstLine[512];
- // Clean the toolbar
- for(int i=0; i<512; i++)
- {
- firstLine[i] = ' ';
- }
- firstLine[511] = '\0';
- curses_move(y-4,0);
- printw(firstLine);
- curses_move(y-3,0);
- printw(firstLine);
- curses_move(y-2,0);
- printw(firstLine);
- curses_move(y-1,0);
- printw(firstLine);
- sprintf(firstLine, "Editing option, press [enter] to leave edit.");
- curses_move(y-3,0);
- printw(firstLine);
- return true;
- }
- else
- {
- return false;
- }
- }
|