1 // * this is for making emacs happy: -*-Mode: C++;-*-
2 /****************************************************************************
3 * Copyright (c) 1998,2001 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Juergen Pfeifer, 1997 *
32 * Contact: http://www.familiepfeifer.de/Contact.aspx?Lang=en *
33 ****************************************************************************/
37 #include "cursesapp.h"
39 MODULE_ID("$Id: cursesf.cc,v 1.15 2002/07/06 15:47:52 juergen Exp $")
41 NCursesFormField::~NCursesFormField () {
43 OnError(::free_field (field));
46 /* Construct a FIELD* array from an array of NCursesFormField
50 NCursesForm::mapFields(NCursesFormField* nfields[]) {
51 int fieldCount = 0,lcv;
56 for (lcv=0; nfields[lcv]->field; ++lcv)
59 FIELD** fields = new FIELD*[fieldCount + 1];
61 for (lcv=0;nfields[lcv]->field;++lcv) {
62 fields[lcv] = nfields[lcv]->field;
68 if (form && (old_fields = ::form_fields(form))) {
69 ::set_form_fields(form,(FIELD**)0);
75 void NCursesForm::setDefaultAttributes() {
76 NCursesApplication* S = NCursesApplication::getApplication();
80 for(int i=0; i<n; i++) {
81 NCursesFormField* f = (*this)[i];
82 if ((f->options() & (O_EDIT|O_ACTIVE))==(O_EDIT|O_ACTIVE)) {
84 f->set_foreground(S->foregrounds());
85 f->set_background(S->backgrounds());
87 f->set_pad_character('_');
91 f->set_background(S->labels());
97 bkgd(' '|S->dialog_backgrounds());
99 sub->bkgd(' '|S->dialog_backgrounds());
104 NCursesForm::InitForm(NCursesFormField* nfields[],
106 bool autoDelete_Fields) {
112 b_framed = with_frame;
113 b_autoDelete = autoDelete_Fields;
116 form = ::new_form(mapFields(nfields));
118 OnError (E_SYSTEM_ERROR);
120 UserHook* hook = new UserHook;
123 hook->m_owner = form;
124 ::set_form_userptr(form,(void*)hook);
126 ::set_form_init (form, NCursesForm::frm_init);
127 ::set_form_term (form, NCursesForm::frm_term);
128 ::set_field_init (form, NCursesForm::fld_init);
129 ::set_field_term (form, NCursesForm::fld_term);
132 ::set_form_win(form, w);
135 if ((mrows > height()-2) || (mcols > width()-2))
137 sub = new NCursesWindow(*this,mrows,mcols,1,1,'r');
138 ::set_form_sub(form, sub->w);
142 sub = (NCursesWindow*)0;
145 options_on(O_NL_OVERLOAD);
146 setDefaultAttributes();
149 NCursesForm::~NCursesForm() {
150 UserHook* hook = (UserHook*)::form_userptr(form);
154 ::set_form_sub(form,(WINDOW *)0);
157 FIELD** fields = ::form_fields(form);
160 OnError(::set_form_fields(form,(FIELD**)0));
164 for (int i=0; i <= cnt; i++)
171 // It's essential to do this after free_form()
177 NCursesForm::setSubWindow(NCursesWindow& nsub) {
178 if (!isDescendant(nsub))
179 OnError(E_SYSTEM_ERROR);
184 ::set_form_sub(form,sub->w);
188 /* Internal hook functions. They will route the hook
189 * calls to virtual methods of the NCursesForm class,
190 * so in C++ providing a hook is done simply by
191 * implementing a virtual method in a derived class
194 NCursesForm::frm_init(FORM *f) {
195 getHook(f)->On_Form_Init();
199 NCursesForm::frm_term(FORM *f) {
200 getHook(f)->On_Form_Termination();
204 NCursesForm::fld_init(FORM *f) {
205 NCursesForm* F = getHook(f);
206 F->On_Field_Init (*(F->current_field ()));
210 NCursesForm::fld_term(FORM *f) {
211 NCursesForm* F = getHook(f);
212 F->On_Field_Termination (*(F->current_field ()));
216 NCursesForm::On_Form_Init() {
220 NCursesForm::On_Form_Termination() {
224 NCursesForm::On_Field_Init(NCursesFormField& field) {
228 NCursesForm::On_Field_Termination(NCursesFormField& field) {
231 // call the form driver and do basic error checking.
233 NCursesForm::driver (int c) {
234 int res = ::form_driver (form, c);
237 case E_REQUEST_DENIED:
238 case E_INVALID_FIELD:
239 case E_UNKNOWN_COMMAND:
247 void NCursesForm::On_Request_Denied(int c) const {
251 void NCursesForm::On_Invalid_Field(int c) const {
255 void NCursesForm::On_Unknown_Command(int c) const {
259 static const int CMD_QUIT = MAX_COMMAND + 1;
262 NCursesForm::operator()(void) {
271 while (((drvCmnd = virtualize((c=getKey()))) != CMD_QUIT)) {
272 switch((err=driver(drvCmnd))) {
273 case E_REQUEST_DENIED:
274 On_Request_Denied(c);
276 case E_INVALID_FIELD:
279 case E_UNKNOWN_COMMAND:
280 On_Unknown_Command(c);
292 return my_fields[::field_index (::current_field (form))];
295 // Provide a default key virtualization. Translate the keyboard
296 // code c into a form request code.
297 // The default implementation provides a hopefully straightforward
298 // mapping for the most common keystrokes and form requests.
300 NCursesForm::virtualize(int c) {
303 case KEY_HOME : return(REQ_FIRST_FIELD);
304 case KEY_END : return(REQ_LAST_FIELD);
306 case KEY_DOWN : return(REQ_DOWN_CHAR);
307 case KEY_UP : return(REQ_UP_CHAR);
308 case KEY_LEFT : return(REQ_PREV_CHAR);
309 case KEY_RIGHT : return(REQ_NEXT_CHAR);
311 case KEY_NPAGE : return(REQ_NEXT_PAGE);
312 case KEY_PPAGE : return(REQ_PREV_PAGE);
314 case KEY_BACKSPACE : return(REQ_DEL_PREV);
315 case KEY_ENTER : return(REQ_NEW_LINE);
316 case KEY_CLEAR : return(REQ_CLR_FIELD);
318 case CTRL('X') : return(CMD_QUIT); // eXit
320 case CTRL('F') : return(REQ_NEXT_FIELD); // Forward
321 case CTRL('B') : return(REQ_PREV_FIELD); // Backward
322 case CTRL('L') : return(REQ_LEFT_FIELD); // Left
323 case CTRL('R') : return(REQ_RIGHT_FIELD); // Right
324 case CTRL('U') : return(REQ_UP_FIELD); // Up
325 case CTRL('D') : return(REQ_DOWN_FIELD); // Down
327 case CTRL('W') : return(REQ_NEXT_WORD);
328 case CTRL('T') : return(REQ_PREV_WORD);
330 case CTRL('A') : return(REQ_BEG_FIELD);
331 case CTRL('E') : return(REQ_END_FIELD);
333 case CTRL('I') : return(REQ_INS_CHAR);
335 case CTRL('J') : return(REQ_NEW_LINE);
336 case CTRL('O') : return(REQ_INS_LINE);
337 case CTRL('V') : return(REQ_DEL_CHAR);
338 case CTRL('H') : return(REQ_DEL_PREV);
339 case CTRL('Y') : return(REQ_DEL_LINE);
340 case CTRL('G') : return(REQ_DEL_WORD);
341 case CTRL('K') : return(REQ_CLR_EOF);
343 case CTRL('N') : return(REQ_NEXT_CHOICE);
344 case CTRL('P') : return(REQ_PREV_CHOICE);
351 // -------------------------------------------------------------------------
352 // User Defined Fieldtypes
353 // -------------------------------------------------------------------------
355 bool UserDefinedFieldType::fcheck(FIELD *f, const void *u) {
356 NCursesFormField* F = (NCursesFormField*)u;
358 UserDefinedFieldType* udf = (UserDefinedFieldType*)(F->fieldtype());
360 return udf->field_check(*F);
363 bool UserDefinedFieldType::ccheck(int c, const void *u) {
364 NCursesFormField* F = (NCursesFormField*)u;
366 UserDefinedFieldType* udf =
367 (UserDefinedFieldType*)(F->fieldtype());
369 return udf->char_check(c);
372 void* UserDefinedFieldType::makearg(va_list* va) {
373 return va_arg(*va,NCursesFormField*);
376 FIELDTYPE* UserDefinedFieldType::generic_fieldtype =
377 ::new_fieldtype(UserDefinedFieldType::fcheck,
378 UserDefinedFieldType::ccheck);
380 FIELDTYPE* UserDefinedFieldType_With_Choice::generic_fieldtype_with_choice =
381 ::new_fieldtype(UserDefinedFieldType::fcheck,
382 UserDefinedFieldType::ccheck);
384 bool UserDefinedFieldType_With_Choice::next_choice(FIELD *f, const void *u) {
385 NCursesFormField* F = (NCursesFormField*)u;
387 UserDefinedFieldType_With_Choice* udf =
388 (UserDefinedFieldType_With_Choice*)(F->fieldtype());
390 return udf->next(*F);
393 bool UserDefinedFieldType_With_Choice::prev_choice(FIELD *f, const void *u) {
394 NCursesFormField* F = (NCursesFormField*)u;
396 UserDefinedFieldType_With_Choice* udf =
397 (UserDefinedFieldType_With_Choice*)(F->fieldtype());
399 return udf->previous(*F);
408 code = ::set_fieldtype_arg(UserDefinedFieldType::generic_fieldtype,
409 UserDefinedFieldType::makearg,
413 code = ::set_fieldtype_arg
414 (UserDefinedFieldType_With_Choice::generic_fieldtype_with_choice,
415 UserDefinedFieldType::makearg,
419 code = ::set_fieldtype_choice
420 (UserDefinedFieldType_With_Choice::generic_fieldtype_with_choice,
421 UserDefinedFieldType_With_Choice::next_choice,
422 UserDefinedFieldType_With_Choice::prev_choice);
426 UDF_Init* UDF_Init::I = new UDF_Init();