]> ncurses.scripts.mit.edu Git - ncurses.git/blob - c++/cursesf.cc
ncurses 6.1 - patch 20180217
[ncurses.git] / c++ / cursesf.cc
1 // * this is for making emacs happy: -*-Mode: C++;-*-
2 /****************************************************************************
3  * Copyright (c) 1998-2005,2011 Free Software Foundation, Inc.              *
4  *                                                                          *
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:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
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.                               *
23  *                                                                          *
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       *
27  * authorization.                                                           *
28  ****************************************************************************/
29
30 /****************************************************************************
31  *   Author: Juergen Pfeifer, 1997                                          *
32  ****************************************************************************/
33
34 #include "internal.h"
35 #include "cursesf.h"
36 #include "cursesapp.h"
37
38 MODULE_ID("$Id: cursesf.cc,v 1.22 2011/09/17 22:12:10 tom Exp $")
39
40 NCursesFormField::~NCursesFormField ()
41 {
42   if (field)
43     OnError(::free_field (field));
44 }
45
46 /* Construct a FIELD* array from an array of NCursesFormField
47  * objects.
48  */
49 FIELD**
50 NCursesForm::mapFields(NCursesFormField* nfields[])
51 {
52   int fieldCount = 0,lcv;
53   FIELD** old_fields;
54
55   assert(nfields != 0);
56
57   for (lcv=0; nfields[lcv]->field; ++lcv)
58     ++fieldCount;
59
60   FIELD** fields = new FIELD*[fieldCount + 1];
61
62   for (lcv=0;nfields[lcv]->field;++lcv) {
63     fields[lcv] = nfields[lcv]->field;
64   }
65   fields[lcv] = NULL;
66
67   my_fields = nfields;
68
69   if (form && (old_fields = ::form_fields(form))) {
70     ::set_form_fields(form, static_cast<FIELD**>(0));
71     delete[] old_fields;
72   }
73   return fields;
74 }
75
76 void NCursesForm::setDefaultAttributes()
77 {
78   NCursesApplication* S = NCursesApplication::getApplication();
79
80   int n = count();
81   if (n > 0) {
82     for(int i=0; i<n; i++) {
83       NCursesFormField* f = (*this)[i];
84       if ((f->options() & (O_EDIT|O_ACTIVE))==(O_EDIT|O_ACTIVE)) {
85         if (S) {
86           f->set_foreground(S->foregrounds());
87           f->set_background(S->backgrounds());
88         }
89         f->set_pad_character('_');
90       }
91       else {
92         if (S)
93           f->set_background(S->labels());
94       }
95     }
96   }
97
98   if (S) {
99     bkgd(' '|S->dialog_backgrounds());
100     if (sub)
101       sub->bkgd(' '|S->dialog_backgrounds());
102   }
103 }
104
105 void
106 NCursesForm::InitForm(NCursesFormField* nfields[],
107                       bool with_frame,
108                       bool autoDelete_Fields)
109 {
110   int mrows, mcols;
111
112   keypad(TRUE);
113   meta(TRUE);
114
115   b_framed = with_frame;
116   b_autoDelete = autoDelete_Fields;
117
118   form = static_cast<FORM*>(0);
119   form = ::new_form(mapFields(nfields));
120   if (!form)
121     OnError (E_SYSTEM_ERROR);
122
123   UserHook* hook = new UserHook;
124   hook->m_user   = NULL;
125   hook->m_back   = this;
126   hook->m_owner  = form;
127   ::set_form_userptr(form, reinterpret_cast<void*>(hook));
128
129   ::set_form_init  (form, _nc_xx_frm_init);
130   ::set_form_term  (form, _nc_xx_frm_term);
131   ::set_field_init (form, _nc_xx_fld_init);
132   ::set_field_term (form, _nc_xx_fld_term);
133
134   scale(mrows, mcols);
135   ::set_form_win(form, w);
136
137   if (with_frame) {
138     if ((mrows > height()-2) || (mcols > width()-2))
139       OnError(E_NO_ROOM);
140     sub = new NCursesWindow(*this,mrows,mcols,1,1,'r');
141     ::set_form_sub(form, sub->w);
142     b_sub_owner = TRUE;
143   }
144   else {
145     sub = static_cast<NCursesWindow*>(0);
146     b_sub_owner = FALSE;
147   }
148   options_on(O_NL_OVERLOAD);
149   setDefaultAttributes();
150 }
151
152 NCursesForm::~NCursesForm()
153 {
154   UserHook* hook = reinterpret_cast<UserHook*>(::form_userptr(form));
155   delete hook;
156   if (b_sub_owner) {
157     delete sub;
158     ::set_form_sub(form, static_cast<WINDOW *>(0));
159   }
160   if (form) {
161     FIELD** fields = ::form_fields(form);
162     int cnt = count();
163
164     OnError(::set_form_fields(form, static_cast<FIELD**>(0)));
165
166     if (b_autoDelete) {
167       if (cnt>0) {
168         for (int i=0; i <= cnt; i++)
169           delete my_fields[i];
170       }
171       delete[] my_fields;
172     }
173
174     ::free_form(form);
175     // It's essential to do this after free_form()
176     delete[] fields;
177   }
178 }
179
180 void
181 NCursesForm::setSubWindow(NCursesWindow& nsub)
182 {
183   if (!isDescendant(nsub))
184     OnError(E_SYSTEM_ERROR);
185   else {
186     if (b_sub_owner)
187       delete sub;
188     sub = &nsub;
189     ::set_form_sub(form,sub->w);
190   }
191 }
192
193 /* Internal hook functions. They will route the hook
194  * calls to virtual methods of the NCursesForm class,
195  * so in C++ providing a hook is done simply by
196  * implementing a virtual method in a derived class
197  */
198 void
199 _nc_xx_frm_init(FORM *f)
200 {
201   NCursesForm::getHook(f)->On_Form_Init();
202 }
203
204 void
205 _nc_xx_frm_term(FORM *f)
206 {
207   NCursesForm::getHook(f)->On_Form_Termination();
208 }
209
210 void
211 _nc_xx_fld_init(FORM *f)
212 {
213   NCursesForm* F = NCursesForm::getHook(f);
214   F->On_Field_Init (*(F->current_field ()));
215 }
216
217 void
218 _nc_xx_fld_term(FORM *f)
219 {
220   NCursesForm* F = NCursesForm::getHook(f);
221   F->On_Field_Termination (*(F->current_field ()));
222 }
223
224 void
225 NCursesForm::On_Form_Init()
226 {
227 }
228
229 void
230 NCursesForm::On_Form_Termination()
231 {
232 }
233
234 void
235 NCursesForm::On_Field_Init(NCursesFormField& field)
236 {
237   (void) field;
238 }
239
240 void
241 NCursesForm::On_Field_Termination(NCursesFormField& field)
242 {
243   (void) field;
244 }
245
246 // call the form driver and do basic error checking.
247 int
248 NCursesForm::driver (int c)
249 {
250   int res = ::form_driver (form, c);
251   switch (res) {
252   case E_OK:
253   case E_REQUEST_DENIED:
254   case E_INVALID_FIELD:
255   case E_UNKNOWN_COMMAND:
256     break;
257   default:
258     OnError (res);
259   }
260   return (res);
261 }
262
263 void NCursesForm::On_Request_Denied(int c) const
264 {
265   (void) c;
266   ::beep();
267 }
268
269 void NCursesForm::On_Invalid_Field(int c) const
270 {
271   (void) c;
272   ::beep();
273 }
274
275 void NCursesForm::On_Unknown_Command(int c) const
276 {
277   (void) c;
278   ::beep();
279 }
280
281 static const int CMD_QUIT = MAX_COMMAND + 1;
282
283 NCursesFormField*
284 NCursesForm::operator()(void)
285 {
286   int drvCmnd;
287   int err;
288   int c;
289
290   post();
291   show();
292   refresh();
293
294   while (((drvCmnd = virtualize((c=getKey()))) != CMD_QUIT)) {
295     switch((err=driver(drvCmnd))) {
296     case E_REQUEST_DENIED:
297       On_Request_Denied(c);
298       break;
299     case E_INVALID_FIELD:
300       On_Invalid_Field(c);
301       break;
302     case E_UNKNOWN_COMMAND:
303       On_Unknown_Command(c);
304       break;
305     case E_OK:
306       break;
307     default:
308       OnError(err);
309     }
310   }
311
312   unpost();
313   hide();
314   refresh();
315   return my_fields[::field_index (::current_field (form))];
316 }
317
318 // Provide a default key virtualization. Translate the keyboard
319 // code c into a form request code.
320 // The default implementation provides a hopefully straightforward
321 // mapping for the most common keystrokes and form requests.
322 int
323 NCursesForm::virtualize(int c)
324 {
325   switch(c) {
326
327   case KEY_HOME      : return(REQ_FIRST_FIELD);
328   case KEY_END       : return(REQ_LAST_FIELD);
329
330   case KEY_DOWN      : return(REQ_DOWN_CHAR);
331   case KEY_UP        : return(REQ_UP_CHAR);
332   case KEY_LEFT      : return(REQ_PREV_CHAR);
333   case KEY_RIGHT     : return(REQ_NEXT_CHAR);
334
335   case KEY_NPAGE     : return(REQ_NEXT_PAGE);
336   case KEY_PPAGE     : return(REQ_PREV_PAGE);
337
338   case KEY_BACKSPACE : return(REQ_DEL_PREV);
339   case KEY_ENTER     : return(REQ_NEW_LINE);
340   case KEY_CLEAR     : return(REQ_CLR_FIELD);
341
342   case CTRL('X')     : return(CMD_QUIT);        // eXit
343
344   case CTRL('F')     : return(REQ_NEXT_FIELD);  // Forward
345   case CTRL('B')     : return(REQ_PREV_FIELD);  // Backward
346   case CTRL('L')     : return(REQ_LEFT_FIELD);  // Left
347   case CTRL('R')     : return(REQ_RIGHT_FIELD); // Right
348   case CTRL('U')     : return(REQ_UP_FIELD);    // Up
349   case CTRL('D')     : return(REQ_DOWN_FIELD);  // Down
350
351   case CTRL('W')     : return(REQ_NEXT_WORD);
352   case CTRL('T')     : return(REQ_PREV_WORD);
353
354   case CTRL('A')     : return(REQ_BEG_FIELD);
355   case CTRL('E')     : return(REQ_END_FIELD);
356
357   case CTRL('I')     : return(REQ_INS_CHAR);
358   case CTRL('M')     :
359   case CTRL('J')     : return(REQ_NEW_LINE);
360   case CTRL('O')     : return(REQ_INS_LINE);
361   case CTRL('V')     : return(REQ_DEL_CHAR);
362   case CTRL('H')     : return(REQ_DEL_PREV);
363   case CTRL('Y')     : return(REQ_DEL_LINE);
364   case CTRL('G')     : return(REQ_DEL_WORD);
365   case CTRL('K')     : return(REQ_CLR_EOF);
366
367   case CTRL('N')     : return(REQ_NEXT_CHOICE);
368   case CTRL('P')     : return(REQ_PREV_CHOICE);
369
370   default:
371     return(c);
372   }
373 }
374 //
375 // -------------------------------------------------------------------------
376 // User Defined Fieldtypes
377 // -------------------------------------------------------------------------
378 //
379 bool _nc_xx_fld_fcheck(FIELD *f, const void *u)
380 {
381   (void) f;
382   NCursesFormField* F = reinterpret_cast<NCursesFormField*>(const_cast<void *>(u));
383   assert(F != 0);
384   UserDefinedFieldType* udf = reinterpret_cast<UserDefinedFieldType*>(F->fieldtype());
385   assert(udf != 0);
386   return udf->field_check(*F);
387 }
388
389 bool _nc_xx_fld_ccheck(int c, const void *u)
390 {
391   NCursesFormField* F = reinterpret_cast<NCursesFormField*>(const_cast<void *>(u));
392   assert(F != 0);
393   UserDefinedFieldType* udf =
394     reinterpret_cast<UserDefinedFieldType*>(F->fieldtype());
395   assert(udf != 0);
396   return udf->char_check(c);
397 }
398
399 void* _nc_xx_fld_makearg(va_list* va)
400 {
401   return va_arg(*va,NCursesFormField*);
402 }
403
404 FIELDTYPE* UserDefinedFieldType::generic_fieldtype =
405   ::new_fieldtype(_nc_xx_fld_fcheck,
406                   _nc_xx_fld_ccheck);
407
408 FIELDTYPE* UserDefinedFieldType_With_Choice::generic_fieldtype_with_choice =
409   ::new_fieldtype(_nc_xx_fld_fcheck,
410                   _nc_xx_fld_ccheck);
411
412 bool _nc_xx_next_choice(FIELD *f, const void *u)
413 {
414   (void) f;
415   NCursesFormField* F = reinterpret_cast<NCursesFormField*>(const_cast<void *>(u));
416   assert(F != 0);
417   UserDefinedFieldType_With_Choice* udf =
418     reinterpret_cast<UserDefinedFieldType_With_Choice*>(F->fieldtype());
419   assert(udf != 0);
420   return udf->next(*F);
421 }
422
423 bool _nc_xx_prev_choice(FIELD *f, const void *u)
424 {
425   (void) f;
426   NCursesFormField* F = reinterpret_cast<NCursesFormField*>(const_cast<void *>(u));
427   assert(F != 0);
428   UserDefinedFieldType_With_Choice* udf =
429     reinterpret_cast<UserDefinedFieldType_With_Choice*>(F->fieldtype());
430   assert(udf != 0);
431   return udf->previous(*F);
432 }
433
434 class UDF_Init
435 {
436 private:
437   int code;
438   static UDF_Init* I;
439
440 public:
441   UDF_Init()
442     : code(0)
443   {
444     code = ::set_fieldtype_arg(UserDefinedFieldType::generic_fieldtype,
445                                _nc_xx_fld_makearg,
446                                NULL,
447                                NULL);
448     if (code==E_OK)
449       code = ::set_fieldtype_arg
450         (UserDefinedFieldType_With_Choice::generic_fieldtype_with_choice,
451          _nc_xx_fld_makearg,
452          NULL,
453          NULL);
454     if (code==E_OK)
455       code = ::set_fieldtype_choice
456         (UserDefinedFieldType_With_Choice::generic_fieldtype_with_choice,
457          _nc_xx_next_choice,
458          _nc_xx_prev_choice);
459   }
460 };
461
462 UDF_Init* UDF_Init::I = new UDF_Init();