Initial commit.
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / CursesDialog / cmCursesStringWidget.cxx
blob40136f1136c1e70776dc459e9faf6101042105cb
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmCursesStringWidget.cxx,v $
5 Language: C++
6 Date: $Date: 2006/03/16 15:44:55 $
7 Version: $Revision: 1.13 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmCursesStringWidget.h"
18 #include "cmCursesMainForm.h"
20 inline int ctrl(int z)
22 return (z&037);
25 cmCursesStringWidget::cmCursesStringWidget(int width, int height,
26 int left, int top) :
27 cmCursesWidget(width, height, left, top)
29 this->InEdit = false;
30 this->Type = cmCacheManager::STRING;
31 set_field_fore(this->Field, A_NORMAL);
32 set_field_back(this->Field, A_STANDOUT);
33 field_opts_off(this->Field, O_STATIC);
36 void cmCursesStringWidget::OnTab(cmCursesMainForm*, WINDOW*)
38 //FORM* form = fm->GetForm();
41 void cmCursesStringWidget::OnReturn(cmCursesMainForm* fm, WINDOW*)
43 FORM* form = fm->GetForm();
44 if (this->InEdit)
46 cmCursesForm::LogMessage("String widget leaving edit.");
47 this->InEdit = false;
48 fm->PrintKeys();
49 delete[] this->OriginalString;
50 // trick to force forms to update the field buffer
51 form_driver(form, REQ_NEXT_FIELD);
52 form_driver(form, REQ_PREV_FIELD);
53 this->Done = true;
55 else
57 cmCursesForm::LogMessage("String widget entering edit.");
58 this->InEdit = true;
59 fm->PrintKeys();
60 char* buf = field_buffer(this->Field, 0);
61 this->OriginalString = new char[strlen(buf)+1];
62 strcpy(this->OriginalString, buf);
66 void cmCursesStringWidget::OnType(int& key, cmCursesMainForm* fm, WINDOW*)
68 form_driver(fm->GetForm(), key);
71 bool cmCursesStringWidget::HandleInput(int& key, cmCursesMainForm* fm,
72 WINDOW* w)
74 int x,y;
76 FORM* form = fm->GetForm();
77 // 10 == enter
78 if (!this->InEdit && ( key != 10 && key != KEY_ENTER ) )
80 return false;
83 this->OriginalString=0;
84 this->Done = false;
86 char debugMessage[128];
88 // <Enter> is used to change edit mode (like <Esc> in vi).
89 while(!this->Done)
91 sprintf(debugMessage, "String widget handling input, key: %d", key);
92 cmCursesForm::LogMessage(debugMessage);
94 fm->PrintKeys();
96 getmaxyx(stdscr, y, x);
97 // If window too small, handle 'q' only
98 if ( x < cmCursesMainForm::MIN_WIDTH ||
99 y < cmCursesMainForm::MIN_HEIGHT )
101 // quit
102 if ( key == 'q' )
104 return false;
106 else
108 key=getch();
109 continue;
113 // If resize occured during edit, move out of edit mode
114 if (!this->InEdit && ( key != 10 && key != KEY_ENTER ) )
116 return false;
118 // 10 == enter
119 if (key == 10 || key == KEY_ENTER)
121 this->OnReturn(fm, w);
123 else if ( key == KEY_DOWN || key == ctrl('n') ||
124 key == KEY_UP || key == ctrl('p') ||
125 key == KEY_NPAGE || key == ctrl('d') ||
126 key == KEY_PPAGE || key == ctrl('u'))
128 this->InEdit = false;
129 delete[] this->OriginalString;
130 // trick to force forms to update the field buffer
131 form_driver(form, REQ_NEXT_FIELD);
132 form_driver(form, REQ_PREV_FIELD);
133 return false;
135 // esc
136 else if (key == 27)
138 if (this->InEdit)
140 this->InEdit = false;
141 fm->PrintKeys();
142 this->SetString(this->OriginalString);
143 delete[] this->OriginalString;
144 touchwin(w);
145 wrefresh(w);
146 return true;
149 else if ( key == 9 )
151 this->OnTab(fm, w);
153 else if ( key == KEY_LEFT || key == ctrl('b') )
155 form_driver(form, REQ_PREV_CHAR);
157 else if ( key == KEY_RIGHT || key == ctrl('f') )
159 form_driver(form, REQ_NEXT_CHAR);
161 else if ( key == ctrl('k') )
163 form_driver(form, REQ_CLR_EOL);
165 else if ( key == ctrl('a') || key == KEY_HOME )
167 form_driver(form, REQ_BEG_FIELD);
169 else if ( key == ctrl('e') || key == KEY_END )
171 form_driver(form, REQ_END_FIELD);
173 else if ( key == ctrl('d') || key == 127 ||
174 key == KEY_BACKSPACE || key == KEY_DC )
176 if ( form->curcol > 0 )
178 form_driver(form, REQ_DEL_PREV);
181 else
183 this->OnType(key, fm, w);
185 if ( !this->Done )
187 touchwin(w);
188 wrefresh(w);
190 key=getch();
193 return true;
196 void cmCursesStringWidget::SetString(const char* value)
198 this->SetValue(value);
201 const char* cmCursesStringWidget::GetString()
203 return this->GetValue();
206 const char* cmCursesStringWidget::GetValue()
208 return field_buffer(this->Field, 0);
211 bool cmCursesStringWidget::PrintKeys()
213 int x,y;
214 getmaxyx(stdscr, y, x);
215 if ( x < cmCursesMainForm::MIN_WIDTH ||
216 y < cmCursesMainForm::MIN_HEIGHT )
218 return false;
220 if (this->InEdit)
222 char firstLine[512];
223 // Clean the toolbar
224 for(int i=0; i<512; i++)
226 firstLine[i] = ' ';
228 firstLine[511] = '\0';
229 curses_move(y-4,0);
230 printw(firstLine);
231 curses_move(y-3,0);
232 printw(firstLine);
233 curses_move(y-2,0);
234 printw(firstLine);
235 curses_move(y-1,0);
236 printw(firstLine);
238 sprintf(firstLine, "Editing option, press [enter] to leave edit.");
239 curses_move(y-3,0);
240 printw(firstLine);
241 return true;
243 else
245 return false;