1 // * this is for making emacs happy: -*-Mode: C++;-*-
2 /****************************************************************************
3 * Copyright 2019-2020,2021 Thomas E. Dickey *
4 * Copyright 1998-2005,2011 Free Software Foundation, Inc. *
6 * Permission is hereby granted, free of charge, to any person obtaining a *
7 * copy of this software and associated documentation files (the *
8 * "Software"), to deal in the Software without restriction, including *
9 * without limitation the rights to use, copy, modify, merge, publish, *
10 * distribute, distribute with modifications, sublicense, and/or sell *
11 * copies of the Software, and to permit persons to whom the Software is *
12 * furnished to do so, subject to the following conditions: *
14 * The above copyright notice and this permission notice shall be included *
15 * in all copies or substantial portions of the Software. *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
25 * Except as contained in this notice, the name(s) of the above copyright *
26 * holders shall not be used in advertising or otherwise to promote the *
27 * sale, use or other dealings in this Software without prior written *
29 ****************************************************************************/
31 /****************************************************************************
32 * Author: Juergen Pfeifer, 1997 *
33 ****************************************************************************/
37 #include "cursesapp.h"
39 MODULE_ID("$Id: cursesf.cc,v 1.26 2021/04/17 18:11:08 tom Exp $")
41 NCursesFormField::~NCursesFormField () THROWS(NCursesException)
44 OnError(::free_field (field));
47 /* Construct a FIELD* array from an array of NCursesFormField
51 NCursesForm::mapFields(NCursesFormField* nfields[])
53 int fieldCount = 0,lcv;
58 for (lcv=0; nfields[lcv]->field; ++lcv)
61 FIELD** fields = new FIELD*[fieldCount + 1];
63 for (lcv=0;nfields[lcv]->field;++lcv) {
64 fields[lcv] = nfields[lcv]->field;
70 if (form && (old_fields = ::form_fields(form))) {
71 ::set_form_fields(form, static_cast<FIELD**>(0));
77 void NCursesForm::setDefaultAttributes()
79 NCursesApplication* S = NCursesApplication::getApplication();
83 for(int i=0; i<n; i++) {
84 NCursesFormField* f = (*this)[i];
85 if ((f->options() & (O_EDIT|O_ACTIVE))==(O_EDIT|O_ACTIVE)) {
87 f->set_foreground(S->foregrounds());
88 f->set_background(S->backgrounds());
90 f->set_pad_character('_');
94 f->set_background(S->labels());
100 bkgd(' '|S->dialog_backgrounds());
102 sub->bkgd(' '|S->dialog_backgrounds());
107 NCursesForm::InitForm(NCursesFormField* nfields[],
109 bool autoDelete_Fields)
116 b_framed = with_frame;
117 b_autoDelete = autoDelete_Fields;
119 form = static_cast<FORM*>(0);
120 form = ::new_form(mapFields(nfields));
122 OnError (E_SYSTEM_ERROR);
124 UserHook* hook = new UserHook;
127 hook->m_owner = form;
128 ::set_form_userptr(form, reinterpret_cast<void*>(hook));
130 ::set_form_init (form, _nc_xx_frm_init);
131 ::set_form_term (form, _nc_xx_frm_term);
132 ::set_field_init (form, _nc_xx_fld_init);
133 ::set_field_term (form, _nc_xx_fld_term);
136 ::set_form_win(form, w);
139 if ((mrows > height()-2) || (mcols > width()-2))
141 sub = new NCursesWindow(*this,mrows,mcols,1,1,'r');
142 ::set_form_sub(form, sub->w);
146 sub = static_cast<NCursesWindow*>(0);
149 options_on(O_NL_OVERLOAD);
150 setDefaultAttributes();
153 NCursesForm::~NCursesForm() THROWS(NCursesException)
155 UserHook* hook = reinterpret_cast<UserHook*>(::form_userptr(form));
159 ::set_form_sub(form, static_cast<WINDOW *>(0));
162 FIELD** fields = ::form_fields(form);
165 OnError(::set_form_fields(form, static_cast<FIELD**>(0)));
169 for (int i=0; i <= cnt; i++)
176 // It's essential to do this after free_form()
182 NCursesForm::setSubWindow(NCursesWindow& nsub)
184 if (!isDescendant(nsub))
185 OnError(E_SYSTEM_ERROR);
190 ::set_form_sub(form,sub->w);
194 /* Internal hook functions. They will route the hook
195 * calls to virtual methods of the NCursesForm class,
196 * so in C++ providing a hook is done simply by
197 * implementing a virtual method in a derived class
200 _nc_xx_frm_init(FORM *f)
202 NCursesForm::getHook(f)->On_Form_Init();
206 _nc_xx_frm_term(FORM *f)
208 NCursesForm::getHook(f)->On_Form_Termination();
212 _nc_xx_fld_init(FORM *f)
214 NCursesForm* F = NCursesForm::getHook(f);
215 F->On_Field_Init (*(F->current_field ()));
219 _nc_xx_fld_term(FORM *f)
221 NCursesForm* F = NCursesForm::getHook(f);
222 F->On_Field_Termination (*(F->current_field ()));
226 NCursesForm::On_Form_Init()
231 NCursesForm::On_Form_Termination()
236 NCursesForm::On_Field_Init(NCursesFormField& field)
242 NCursesForm::On_Field_Termination(NCursesFormField& field)
247 // call the form driver and do basic error checking.
249 NCursesForm::driver (int c)
251 int res = ::form_driver (form, c);
254 case E_REQUEST_DENIED:
255 case E_INVALID_FIELD:
256 case E_UNKNOWN_COMMAND:
264 void NCursesForm::On_Request_Denied(int c) const
270 void NCursesForm::On_Invalid_Field(int c) const
276 void NCursesForm::On_Unknown_Command(int c) const
282 static const int CMD_QUIT = MAX_COMMAND + 1;
285 NCursesForm::operator()(void)
294 while (((drvCmnd = virtualize((c = getKey()))) != CMD_QUIT)) {
296 switch((err = driver(drvCmnd))) {
297 case E_REQUEST_DENIED:
298 On_Request_Denied(c);
300 case E_INVALID_FIELD:
303 case E_UNKNOWN_COMMAND:
304 On_Unknown_Command(c);
316 return my_fields[::field_index (::current_field (form))];
319 // Provide a default key virtualization. Translate the keyboard
320 // code c into a form request code.
321 // The default implementation provides a hopefully straightforward
322 // mapping for the most common keystrokes and form requests.
324 NCursesForm::virtualize(int c)
328 case KEY_HOME : return(REQ_FIRST_FIELD);
329 case KEY_END : return(REQ_LAST_FIELD);
331 case KEY_DOWN : return(REQ_DOWN_CHAR);
332 case KEY_UP : return(REQ_UP_CHAR);
333 case KEY_LEFT : return(REQ_PREV_CHAR);
334 case KEY_RIGHT : return(REQ_NEXT_CHAR);
336 case KEY_NPAGE : return(REQ_NEXT_PAGE);
337 case KEY_PPAGE : return(REQ_PREV_PAGE);
339 case KEY_BACKSPACE : return(REQ_DEL_PREV);
340 case KEY_ENTER : return(REQ_NEW_LINE);
341 case KEY_CLEAR : return(REQ_CLR_FIELD);
343 case CTRL('X') : return(CMD_QUIT); // eXit
345 case CTRL('F') : return(REQ_NEXT_FIELD); // Forward
346 case CTRL('B') : return(REQ_PREV_FIELD); // Backward
347 case CTRL('L') : return(REQ_LEFT_FIELD); // Left
348 case CTRL('R') : return(REQ_RIGHT_FIELD); // Right
349 case CTRL('U') : return(REQ_UP_FIELD); // Up
350 case CTRL('D') : return(REQ_DOWN_FIELD); // Down
352 case CTRL('W') : return(REQ_NEXT_WORD);
353 case CTRL('T') : return(REQ_PREV_WORD);
355 case CTRL('A') : return(REQ_BEG_FIELD);
356 case CTRL('E') : return(REQ_END_FIELD);
358 case CTRL('I') : return(REQ_INS_CHAR);
360 case CTRL('J') : return(REQ_NEW_LINE);
361 case CTRL('O') : return(REQ_INS_LINE);
362 case CTRL('V') : return(REQ_DEL_CHAR);
363 case CTRL('H') : return(REQ_DEL_PREV);
364 case CTRL('Y') : return(REQ_DEL_LINE);
365 case CTRL('G') : return(REQ_DEL_WORD);
366 case CTRL('K') : return(REQ_CLR_EOF);
368 case CTRL('N') : return(REQ_NEXT_CHOICE);
369 case CTRL('P') : return(REQ_PREV_CHOICE);
376 // -------------------------------------------------------------------------
377 // User Defined Fieldtypes
378 // -------------------------------------------------------------------------
380 bool _nc_xx_fld_fcheck(FIELD *f, const void *u)
383 NCursesFormField* F = reinterpret_cast<NCursesFormField*>(const_cast<void *>(u));
385 UserDefinedFieldType* udf = reinterpret_cast<UserDefinedFieldType*>(F->fieldtype());
387 return udf->field_check(*F);
390 bool _nc_xx_fld_ccheck(int c, const void *u)
392 NCursesFormField* F = reinterpret_cast<NCursesFormField*>(const_cast<void *>(u));
394 UserDefinedFieldType* udf =
395 reinterpret_cast<UserDefinedFieldType*>(F->fieldtype());
397 return udf->char_check(c);
400 void* _nc_xx_fld_makearg(va_list* va)
402 return va_arg(*va,NCursesFormField*);
405 FIELDTYPE* UserDefinedFieldType::generic_fieldtype =
406 ::new_fieldtype(_nc_xx_fld_fcheck,
410 UserDefinedFieldType::UserDefinedFieldType() : NCursesFieldType(generic_fieldtype) {
413 FIELDTYPE* UserDefinedFieldType_With_Choice::generic_fieldtype_with_choice =
414 ::new_fieldtype(_nc_xx_fld_fcheck,
418 UserDefinedFieldType_With_Choice::UserDefinedFieldType_With_Choice() {
419 fieldtype = generic_fieldtype_with_choice;
422 bool _nc_xx_next_choice(FIELD *f, const void *u)
425 NCursesFormField* F = reinterpret_cast<NCursesFormField*>(const_cast<void *>(u));
427 UserDefinedFieldType_With_Choice* udf =
428 reinterpret_cast<UserDefinedFieldType_With_Choice*>(F->fieldtype());
430 return udf->next(*F);
433 bool _nc_xx_prev_choice(FIELD *f, const void *u)
436 NCursesFormField* F = reinterpret_cast<NCursesFormField*>(const_cast<void *>(u));
438 UserDefinedFieldType_With_Choice* udf =
439 reinterpret_cast<UserDefinedFieldType_With_Choice*>(F->fieldtype());
441 return udf->previous(*F);
454 code = ::set_fieldtype_arg(UserDefinedFieldType::generic_fieldtype,
459 code = ::set_fieldtype_arg
460 (UserDefinedFieldType_With_Choice::generic_fieldtype_with_choice,
465 code = ::set_fieldtype_choice
466 (UserDefinedFieldType_With_Choice::generic_fieldtype_with_choice,
472 UDF_Init* UDF_Init::I = new UDF_Init();