1 // * this is for making emacs happy: -*-Mode: C++;-*-
2 /****************************************************************************
3 * Copyright (c) 1998 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 <juergen.pfeifer@gmx.net> 1997 *
32 ****************************************************************************/
35 #include "cursesapp.h"
38 MODULE_ID("$Id: cursesf.cc,v 1.10 1999/10/30 23:49:28 tom Exp $")
40 NCursesFormField::~NCursesFormField () {
42 OnError(::free_field (field));
45 /* Construct a FIELD* array from an array of NCursesFormField
49 NCursesForm::mapFields(NCursesFormField* nfields[]) {
50 int fieldCount = 0,lcv;
54 for (lcv=0; nfields[lcv]->field; ++lcv)
57 FIELD** fields = new FIELD*[fieldCount + 1];
59 for (lcv=0;nfields[lcv]->field;++lcv) {
60 fields[lcv] = nfields[lcv]->field;
67 delete[] ::form_fields(form);
71 void NCursesForm::setDefaultAttributes() {
72 NCursesApplication* S = NCursesApplication::getApplication();
76 for(int i=0; i<n; i++) {
77 NCursesFormField* f = (*this)[i];
78 if ((f->options() & (O_EDIT|O_ACTIVE))==(O_EDIT|O_ACTIVE)) {
80 f->set_foreground(S->foregrounds());
81 f->set_background(S->backgrounds());
83 f->set_pad_character('_');
87 f->set_background(S->labels());
93 bkgd(' '|S->dialog_backgrounds());
95 sub->bkgd(' '|S->dialog_backgrounds());
100 NCursesForm::InitForm(NCursesFormField* nfields[],
102 bool autoDelete_Fields) {
108 b_framed = with_frame;
109 b_autoDelete = autoDelete_Fields;
112 form = ::new_form(mapFields(nfields));
114 OnError (E_SYSTEM_ERROR);
116 UserHook* hook = new UserHook;
119 hook->m_owner = form;
120 ::set_form_userptr(form,(void*)hook);
122 ::set_form_init (form, NCursesForm::frm_init);
123 ::set_form_term (form, NCursesForm::frm_term);
124 ::set_field_init (form, NCursesForm::fld_init);
125 ::set_field_term (form, NCursesForm::fld_term);
128 ::set_form_win(form, w);
131 if ((mrows > height()-2) || (mcols > width()-2))
133 sub = new NCursesWindow(*this,mrows,mcols,1,1,'r');
134 ::set_form_sub(form, sub->w);
138 sub = (NCursesWindow*)0;
141 options_on(O_NL_OVERLOAD);
142 setDefaultAttributes();
145 NCursesForm::~NCursesForm() {
146 UserHook* hook = (UserHook*)::form_userptr(form);
150 ::set_form_sub(form,(WINDOW *)0);
153 FIELD** fields = ::form_fields(form);
156 OnError(::set_form_fields(form,(FIELD**)0));
160 for (int i=0; i <= cnt; i++)
167 // It's essential to do this after free_form()
173 NCursesForm::setSubWindow(NCursesWindow& nsub) {
174 if (!isDescendant(nsub))
175 OnError(E_SYSTEM_ERROR);
180 ::set_form_sub(form,sub->w);
184 /* Internal hook functions. They will route the hook
185 * calls to virtual methods of the NCursesForm class,
186 * so in C++ providing a hook is done simply by
187 * implementing a virtual method in a derived class
190 NCursesForm::frm_init(FORM *f) {
191 getHook(f)->On_Form_Init();
195 NCursesForm::frm_term(FORM *f) {
196 getHook(f)->On_Form_Termination();
200 NCursesForm::fld_init(FORM *f) {
201 NCursesForm* F = getHook(f);
202 F->On_Field_Init (*(F->current_field ()));
206 NCursesForm::fld_term(FORM *f) {
207 NCursesForm* F = getHook(f);
208 F->On_Field_Termination (*(F->current_field ()));
212 NCursesForm::On_Form_Init() {
216 NCursesForm::On_Form_Termination() {
220 NCursesForm::On_Field_Init(NCursesFormField& field) {
224 NCursesForm::On_Field_Termination(NCursesFormField& field) {
227 // call the form driver and do basic error checking.
229 NCursesForm::driver (int c) {
230 int res = ::form_driver (form, c);
233 case E_REQUEST_DENIED:
234 case E_INVALID_FIELD:
235 case E_UNKNOWN_COMMAND:
243 void NCursesForm::On_Request_Denied(int c) const {
247 void NCursesForm::On_Invalid_Field(int c) const {
251 void NCursesForm::On_Unknown_Command(int c) const {
255 static const int CMD_QUIT = MAX_COMMAND + 1;
258 NCursesForm::operator()(void) {
267 while (((drvCmnd = virtualize((c=getch()))) != CMD_QUIT)) {
268 switch((err=driver(drvCmnd))) {
269 case E_REQUEST_DENIED:
270 On_Request_Denied(c);
272 case E_INVALID_FIELD:
275 case E_UNKNOWN_COMMAND:
276 On_Unknown_Command(c);
288 return my_fields[::field_index (::current_field (form))];
291 // Provide a default key virtualization. Translate the keyboard
292 // code c into a form request code.
293 // The default implementation provides a hopefully straightforward
294 // mapping for the most common keystrokes and form requests.
296 NCursesForm::virtualize(int c) {
299 case KEY_HOME : return(REQ_FIRST_FIELD);
300 case KEY_END : return(REQ_LAST_FIELD);
302 case KEY_DOWN : return(REQ_DOWN_CHAR);
303 case KEY_UP : return(REQ_UP_CHAR);
304 case KEY_LEFT : return(REQ_PREV_CHAR);
305 case KEY_RIGHT : return(REQ_NEXT_CHAR);
307 case KEY_NPAGE : return(REQ_NEXT_PAGE);
308 case KEY_PPAGE : return(REQ_PREV_PAGE);
310 case KEY_BACKSPACE : return(REQ_DEL_PREV);
311 case KEY_ENTER : return(REQ_NEW_LINE);
312 case KEY_CLEAR : return(REQ_CLR_FIELD);
314 case CTRL('X') : return(CMD_QUIT); // eXit
316 case CTRL('F') : return(REQ_NEXT_FIELD); // Forward
317 case CTRL('B') : return(REQ_PREV_FIELD); // Backward
318 case CTRL('L') : return(REQ_LEFT_FIELD); // Left
319 case CTRL('R') : return(REQ_RIGHT_FIELD); // Right
320 case CTRL('U') : return(REQ_UP_FIELD); // Up
321 case CTRL('D') : return(REQ_DOWN_FIELD); // Down
323 case CTRL('W') : return(REQ_NEXT_WORD);
324 case CTRL('T') : return(REQ_PREV_WORD);
326 case CTRL('A') : return(REQ_BEG_FIELD);
327 case CTRL('E') : return(REQ_END_FIELD);
329 case CTRL('I') : return(REQ_INS_CHAR);
331 case CTRL('J') : return(REQ_NEW_LINE);
332 case CTRL('O') : return(REQ_INS_LINE);
333 case CTRL('V') : return(REQ_DEL_CHAR);
334 case CTRL('H') : return(REQ_DEL_PREV);
335 case CTRL('Y') : return(REQ_DEL_LINE);
336 case CTRL('G') : return(REQ_DEL_WORD);
337 case CTRL('K') : return(REQ_CLR_EOF);
339 case CTRL('N') : return(REQ_NEXT_CHOICE);
340 case CTRL('P') : return(REQ_PREV_CHOICE);
347 // -------------------------------------------------------------------------
348 // User Defined Fieldtypes
349 // -------------------------------------------------------------------------
351 bool UserDefinedFieldType::fcheck(FIELD *f, const void *u) {
352 NCursesFormField* F = (NCursesFormField*)u;
354 UserDefinedFieldType* udf = (UserDefinedFieldType*)(F->fieldtype());
356 return udf->field_check(*F);
359 bool UserDefinedFieldType::ccheck(int c, const void *u) {
360 NCursesFormField* F = (NCursesFormField*)u;
362 UserDefinedFieldType* udf =
363 (UserDefinedFieldType*)(F->fieldtype());
365 return udf->char_check(c);
368 void* UserDefinedFieldType::makearg(va_list* va) {
369 return va_arg(*va,NCursesFormField*);
372 FIELDTYPE* UserDefinedFieldType::generic_fieldtype =
373 ::new_fieldtype(UserDefinedFieldType::fcheck,
374 UserDefinedFieldType::ccheck);
376 FIELDTYPE* UserDefinedFieldType_With_Choice::generic_fieldtype_with_choice =
377 ::new_fieldtype(UserDefinedFieldType::fcheck,
378 UserDefinedFieldType::ccheck);
380 bool UserDefinedFieldType_With_Choice::next_choice(FIELD *f, const void *u) {
381 NCursesFormField* F = (NCursesFormField*)u;
383 UserDefinedFieldType_With_Choice* udf =
384 (UserDefinedFieldType_With_Choice*)(F->fieldtype());
386 return udf->next(*F);
389 bool UserDefinedFieldType_With_Choice::prev_choice(FIELD *f, const void *u) {
390 NCursesFormField* F = (NCursesFormField*)u;
392 UserDefinedFieldType_With_Choice* udf =
393 (UserDefinedFieldType_With_Choice*)(F->fieldtype());
395 return udf->previous(*F);
404 code = ::set_fieldtype_arg(UserDefinedFieldType::generic_fieldtype,
405 UserDefinedFieldType::makearg,
409 code = ::set_fieldtype_arg
410 (UserDefinedFieldType_With_Choice::generic_fieldtype_with_choice,
411 UserDefinedFieldType::makearg,
415 code = ::set_fieldtype_choice
416 (UserDefinedFieldType_With_Choice::generic_fieldtype_with_choice,
417 UserDefinedFieldType_With_Choice::next_choice,
418 UserDefinedFieldType_With_Choice::prev_choice);
422 UDF_Init* UDF_Init::I = new UDF_Init();