1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmCursesStringWidget.cxx,v $
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
)
25 cmCursesStringWidget::cmCursesStringWidget(int width
, int height
,
27 cmCursesWidget(width
, height
, left
, top
)
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();
46 cmCursesForm::LogMessage("String widget leaving edit.");
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
);
57 cmCursesForm::LogMessage("String widget entering edit.");
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
,
76 FORM
* form
= fm
->GetForm();
78 if (!this->InEdit
&& ( key
!= 10 && key
!= KEY_ENTER
) )
83 this->OriginalString
=0;
86 char debugMessage
[128];
88 // <Enter> is used to change edit mode (like <Esc> in vi).
91 sprintf(debugMessage
, "String widget handling input, key: %d", key
);
92 cmCursesForm::LogMessage(debugMessage
);
96 getmaxyx(stdscr
, y
, x
);
97 // If window too small, handle 'q' only
98 if ( x
< cmCursesMainForm::MIN_WIDTH
||
99 y
< cmCursesMainForm::MIN_HEIGHT
)
113 // If resize occured during edit, move out of edit mode
114 if (!this->InEdit
&& ( key
!= 10 && key
!= KEY_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
);
140 this->InEdit
= false;
142 this->SetString(this->OriginalString
);
143 delete[] this->OriginalString
;
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
);
183 this->OnType(key
, fm
, w
);
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()
214 getmaxyx(stdscr
, y
, x
);
215 if ( x
< cmCursesMainForm::MIN_WIDTH
||
216 y
< cmCursesMainForm::MIN_HEIGHT
)
224 for(int i
=0; i
<512; i
++)
228 firstLine
[511] = '\0';
238 sprintf(firstLine
, "Editing option, press [enter] to leave edit.");