1 // * This makes 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 ****************************************************************************/
34 // $Id: cursesf.h,v 1.10 1999/05/16 17:31:42 juergen Exp $
45 // -------------------------------------------------------------------------
46 // The abstract base class for buitin and user defined Fieldtypes.
47 // -------------------------------------------------------------------------
49 class NCursesFormField; // forward declaration
51 // Class to represent builtin field types as well as C++ written new
52 // fieldtypes (see classes UserDefineFieldType...
53 class NCursesFieldType {
54 friend class NCursesFormField;
59 inline void OnError(int err) const THROWS(NCursesFormException) {
61 THROW(new NCursesFormException (err));
64 NCursesFieldType(FIELDTYPE *f) : fieldtype(f) {
67 virtual ~NCursesFieldType() {}
69 // Set the fields f fieldtype to this one.
70 virtual void set(NCursesFormField& f) = 0;
73 NCursesFieldType() : fieldtype((FIELDTYPE*)0) {
78 // -------------------------------------------------------------------------
79 // The class representing a forms field, wrapping the lowlevel FIELD struct
80 // -------------------------------------------------------------------------
82 class NCursesFormField {
83 friend class NCursesForm;
86 FIELD *field; // lowlevel structure
87 NCursesFieldType* ftype; // Associated field type
90 inline void OnError (int err) const THROWS(NCursesFormException) {
92 THROW(new NCursesFormException (err));
96 // Create a 'Null' field. Can be used to delimit a field list
98 : field((FIELD*)0), ftype((NCursesFieldType*)0) {
101 // Create a new field
102 NCursesFormField (int rows,
106 int offscreen_rows = 0,
107 int additional_buffers = 0)
108 : ftype((NCursesFieldType*)0) {
109 field = ::new_field(rows,cols,first_row,first_col,
110 offscreen_rows, additional_buffers);
115 virtual ~NCursesFormField ();
117 // Duplicate the field at a new position
118 inline NCursesFormField* dup(int first_row, int first_col) {
119 NCursesFormField* f = new NCursesFormField();
121 OnError(E_SYSTEM_ERROR);
124 f->field = ::dup_field(field,first_row,first_col);
131 // Link the field to a new location
132 inline NCursesFormField* link(int first_row, int first_col) {
133 NCursesFormField* f = new NCursesFormField();
135 OnError(E_SYSTEM_ERROR);
138 f->field = ::link_field(field,first_row,first_col);
145 // Get the lowlevel field representation
146 inline FIELD* get_field() const {
150 // Retrieve info about the field
151 inline void info(int& rows, int& cols,
152 int& first_row, int& first_col,
153 int& offscreen_rows, int& additional_buffers) const {
154 OnError(::field_info(field, &rows, &cols,
155 &first_row, &first_col,
156 &offscreen_rows, &additional_buffers));
159 // Retrieve info about the fields dynamic properties.
160 inline void dynamic_info(int& dynamic_rows, int& dynamic_cols,
161 int& max_growth) const {
162 OnError(::dynamic_field_info(field, &dynamic_rows, &dynamic_cols,
166 // For a dynamic field you may set the maximum growth limit.
167 // A zero means unlimited growth.
168 inline void set_maximum_growth(int growth = 0) {
169 OnError(::set_max_field(field,growth));
172 // Move the field to a new position
173 inline void move(int row, int col) {
174 OnError(::move_field(field,row,col));
177 // Mark the field to start a new page
178 inline void new_page(bool pageFlag = FALSE) {
179 OnError(::set_new_page(field,pageFlag));
182 // Retrieve whether or not the field starts a new page.
183 inline bool is_new_page() const {
184 return ::new_page(field);
187 // Set the justification for the field
188 inline void set_justification(int just) {
189 OnError(::set_field_just(field,just));
192 // Retrieve the fields justification
193 inline int justification() const {
194 return ::field_just(field);
196 // Set the foreground attribute for the field
197 inline void set_foreground(chtype fore) {
198 OnError(::set_field_fore(field,fore));
201 // Retrieve the fields foreground attribute
202 inline chtype fore() const {
203 return ::field_fore(field);
206 // Set the background attribute for the field
207 inline void set_background(chtype back) {
208 OnError(::set_field_back(field,back));
211 // Retrieve the fields background attribute
212 inline chtype back() const {
213 return ::field_back(field);
216 // Set the padding character for the field
217 inline void set_pad_character(int pad) {
218 OnError(::set_field_pad(field,pad));
221 // Retrieve the fields padding character
222 inline int pad() const {
223 return ::field_pad(field);
226 // Switch on the fields options
227 inline void options_on (Field_Options options) {
228 OnError (::field_opts_on (field, options));
231 // Switch off the fields options
232 inline void options_off (Field_Options options) {
233 OnError (::field_opts_off (field, options));
236 // Retrieve the fields options
237 inline Field_Options options () const {
238 return ::field_opts (field);
241 // Set the fields options
242 inline void set_options (Field_Options options) {
243 OnError (::set_field_opts (field, options));
246 // Mark the field as changed
247 inline void set_changed(bool changeFlag = TRUE) {
248 OnError(::set_field_status(field,changeFlag));
251 // Test whether or not the field is marked as changed
252 inline bool changed() const {
253 return ::field_status(field);
256 // Return the index of the field in the field array of a form
257 // or -1 if the field is not associated to a form
258 inline int (index)() const {
259 return ::field_index(field);
262 // Store a value in a fields buffer. The default buffer is nr. 0
263 inline void set_value(const char *val, int buffer = 0) {
264 OnError(::set_field_buffer(field,buffer,val));
267 // Retrieve the value of a fields buffer. The defaukt buffer is nr. 0
268 inline char* value(int buffer = 0) const {
269 return ::field_buffer(field,buffer);
272 // Set the validation type of the field.
273 inline void set_fieldtype(NCursesFieldType& f) {
275 f.set(*this); // A good friend may do that...
278 // Retrieve the validation type of the field.
279 inline NCursesFieldType* fieldtype() const {
286 // -------------------------------------------------------------------------
287 // The class representing a form, wrapping the lowlevel FORM struct
288 // -------------------------------------------------------------------------
290 class NCursesForm : public NCursesPanel {
292 FORM* form; // the lowlevel structure
295 NCursesWindow* sub; // the subwindow object
296 bool b_sub_owner; // is this our own subwindow?
297 bool b_framed; // has the form a border?
298 bool b_autoDelete; // Delete fields when deleting form?
300 NCursesFormField** my_fields; // The array of fields for this form
302 // This structure is used for the form's user data field to link the
303 // FORM* to the C++ object and to provide extra space for a user pointer.
305 void* m_user; // the pointer for the user's data
306 const NCursesForm* m_back; // backward pointer to C++ object
310 // Get the backward pointer to the C++ object from a FORM
311 static inline NCursesForm* getHook(const FORM *f) {
312 UserHook* hook = (UserHook*)::form_userptr(f);
313 assert(hook && hook->m_owner==f);
314 return (NCursesForm*)(hook->m_back);
317 // This are the built-in hook functions in this C++ binding. In C++ we use
318 // virtual member functions (see below On_..._Init and On_..._Termination)
319 // to provide this functionality in an object oriented manner.
320 static void frm_init(FORM *);
321 static void frm_term(FORM *);
322 static void fld_init(FORM *);
323 static void fld_term(FORM *);
325 // Calculate FIELD* array for the menu
326 FIELD** mapFields(NCursesFormField* nfields[]);
330 inline void set_user(void *user) {
331 UserHook* uptr = (UserHook*)::form_userptr (form);
332 assert (uptr && uptr->m_back==this && uptr->m_owner==form);
336 inline void *get_user() {
337 UserHook* uptr = (UserHook*)::form_userptr (form);
338 assert (uptr && uptr->m_back==this && uptr->m_owner==form);
342 void InitForm (NCursesFormField* Fields[],
344 bool autoDeleteFields);
346 inline void OnError (int err) const THROWS(NCursesFormException) {
348 THROW(new NCursesFormException (err));
351 // this wraps the form_driver call.
352 virtual int driver (int c) ;
354 // 'Internal' constructor, builds an object without association to a
356 NCursesForm( int lines,
360 : NCursesPanel(lines,cols,begin_y,begin_x),
365 // Create form for the default panel.
366 NCursesForm (NCursesFormField* Fields[],
367 bool with_frame=FALSE, // reserve space for a frame?
368 bool autoDelete_Fields=FALSE) // do automatic cleanup?
370 InitForm(Fields, with_frame, autoDelete_Fields);
373 // Create a form in a panel with the given position and size.
374 NCursesForm (NCursesFormField* Fields[],
379 bool with_frame=FALSE, // reserve space for a frame?
380 bool autoDelete_Fields=FALSE) // do automatic cleanup?
381 : NCursesPanel(lines, cols, begin_y, begin_x) {
382 InitForm(Fields, with_frame, autoDelete_Fields);
385 virtual ~NCursesForm();
387 // Set the default attributes for the form
388 virtual void setDefaultAttributes();
390 // Retrieve current field of the form.
391 inline NCursesFormField* current_field() const {
392 return my_fields[::field_index(::current_field(form))];
395 // Set the forms subwindow
396 void setSubWindow(NCursesWindow& sub);
398 // Set these fields for the form
399 inline void setFields(NCursesFormField* Fields[]) {
400 OnError(::set_form_fields(form,mapFields(Fields)));
403 // Remove the form from the screen
404 inline void unpost (void) {
405 OnError (::unpost_form (form));
408 // Post the form to the screen if flag is true, unpost it otherwise
409 inline void post(bool flag = TRUE) {
410 OnError (flag ? ::post_form(form) : ::unpost_form (form));
414 inline void frame(const char *title=NULL, const char* btitle=NULL) {
416 NCursesPanel::frame(title,btitle);
418 OnError(E_SYSTEM_ERROR);
421 inline void boldframe(const char *title=NULL, const char* btitle=NULL) {
423 NCursesPanel::boldframe(title,btitle);
425 OnError(E_SYSTEM_ERROR);
428 inline void label(const char *topLabel, const char *bottomLabel) {
430 NCursesPanel::label(topLabel,bottomLabel);
432 OnError(E_SYSTEM_ERROR);
439 // Called after the form gets repositioned in its window.
440 // This is especially true if the form is posted.
441 virtual void On_Form_Init();
443 // Called before the form gets repositioned in its window.
444 // This is especially true if the form is unposted.
445 virtual void On_Form_Termination();
447 // Called after the field became the current field
448 virtual void On_Field_Init(NCursesFormField& field);
450 // Called before this field is left as current field.
451 virtual void On_Field_Termination(NCursesFormField& field);
453 // Calculate required window size for the form.
454 void scale(int& rows, int& cols) const {
455 OnError(::scale_form(form,&rows,&cols));
458 // Retrieve number of fields in the form.
460 return ::field_count(form);
463 // Make the page the current page of the form.
464 void set_page(int page) {
465 OnError(::set_form_page(form,page));
468 // Retrieve current page number
470 return ::form_page(form);
473 // Switch on the forms options
474 inline void options_on (Form_Options options) {
475 OnError (::form_opts_on (form, options));
478 // Switch off the forms options
479 inline void options_off (Form_Options options) {
480 OnError (::form_opts_off (form, options));
483 // Retrieve the forms options
484 inline Form_Options options () const {
485 return ::form_opts (form);
488 // Set the forms options
489 inline void set_options (Form_Options options) {
490 OnError (::set_form_opts (form, options));
493 // Are there more data in the current field after the data shown
494 inline bool data_ahead() const {
495 return ::data_ahead(form);
498 // Are there more data in the current field before the data shown
499 inline bool data_behind() const {
500 return ::data_behind(form);
503 // Position the cursor to the current field
504 inline void position_cursor () {
505 OnError (::pos_form_cursor (form));
507 // Set the current field
508 inline void set_current(NCursesFormField& F) {
509 OnError (::set_current_field(form, F.field));
512 // Provide a default key virtualization. Translate the keyboard
513 // code c into a form request code.
514 // The default implementation provides a hopefully straightforward
515 // mapping for the most common keystrokes and form requests.
516 virtual int virtualize(int c);
519 inline NCursesFormField* operator[](int i) const {
520 if ( (i < 0) || (i >= ::field_count (form)) )
521 OnError (E_BAD_ARGUMENT);
525 // Perform the menu's operation
526 // Return the field where you left the form.
527 virtual NCursesFormField* operator()(void);
529 // Exception handlers. The default is a Beep.
530 virtual void On_Request_Denied(int c) const;
531 virtual void On_Invalid_Field(int c) const;
532 virtual void On_Unknown_Command(int c) const;
537 // -------------------------------------------------------------------------
538 // This is the typical C++ typesafe way to allow to attach
539 // user data to a field of a form. Its assumed that the user
540 // data belongs to some class T. Use T as template argument
541 // to create a UserField.
542 // -------------------------------------------------------------------------
543 template<class T> class NCursesUserField : public NCursesFormField
546 NCursesUserField (int rows,
550 const T* p_UserData = (T*)0,
551 int offscreen_rows = 0,
552 int additional_buffers = 0)
553 : NCursesFormField (rows, cols,
554 first_row, first_col,
555 offscreen_rows, additional_buffers) {
557 OnError(::set_field_userptr(field,(void *)p_UserData));
560 virtual ~NCursesUserField() {};
562 inline const T* UserData (void) const {
563 return (const T*)::field_userptr (field);
566 inline virtual void setUserData(const T* p_UserData) {
568 OnError (::set_field_userptr (field, (void *)p_UserData));
572 // -------------------------------------------------------------------------
573 // The same mechanism is used to attach user data to a form
574 // -------------------------------------------------------------------------
576 template<class T> class NCursesUserForm : public NCursesForm
579 // 'Internal' constructor, builds an object without association to a
581 NCursesUserForm( int lines,
585 const T* p_UserData = (T*)0)
586 : NCursesForm(lines,cols,begin_y,begin_x) {
588 set_user ((void *)p_UserData);
592 NCursesUserForm (NCursesFormField Fields[],
593 bool with_frame=FALSE,
594 bool autoDelete_Fields=FALSE)
595 : NCursesForm (Fields, with_frame, autoDelete_Fields) {
598 NCursesUserForm (NCursesFormField Fields[],
599 const T* p_UserData = (T*)0,
600 bool with_frame=FALSE,
601 bool autoDelete_Fields=FALSE)
602 : NCursesForm (Fields, with_frame, autoDelete_Fields) {
604 set_user ((void *)p_UserData);
607 NCursesUserForm (NCursesFormField Fields[],
612 const T* p_UserData = (T*)0,
613 bool with_frame=FALSE,
614 bool autoDelete_Fields=FALSE)
615 : NCursesForm (Fields, lines, cols, begin_y, begin_x,
616 with_frame, autoDelete_Fields) {
618 set_user ((void *)p_UserData);
621 virtual ~NCursesUserForm() {
624 inline T* UserData (void) const {
625 return (T*)get_user ();
628 inline virtual void setUserData (const T* p_UserData) {
630 set_user ((void *)p_UserData);
635 // -------------------------------------------------------------------------
636 // Builtin Fieldtypes
637 // -------------------------------------------------------------------------
639 class Alpha_Field : public NCursesFieldType {
643 void set(NCursesFormField& f) {
644 OnError(::set_field_type(f.get_field(),fieldtype,min_field_width));
648 Alpha_Field(int width)
649 : NCursesFieldType(TYPE_ALPHA),
650 min_field_width(width) {
654 class Alphanumeric_Field : public NCursesFieldType {
658 void set(NCursesFormField& f) {
659 OnError(::set_field_type(f.get_field(),fieldtype,min_field_width));
663 Alphanumeric_Field(int width)
664 : NCursesFieldType(TYPE_ALNUM),
665 min_field_width(width) {
669 class Integer_Field : public NCursesFieldType {
672 long lower_limit, upper_limit;
674 void set(NCursesFormField& f) {
675 OnError(::set_field_type(f.get_field(),fieldtype,
676 precision,lower_limit,upper_limit));
680 Integer_Field(int prec, long low=0L, long high=0L)
681 : NCursesFieldType(TYPE_INTEGER),
682 precision(prec), lower_limit(low), upper_limit(high) {
686 class Numeric_Field : public NCursesFieldType {
689 double lower_limit, upper_limit;
691 void set(NCursesFormField& f) {
692 OnError(::set_field_type(f.get_field(),fieldtype,
693 precision,lower_limit,upper_limit));
697 Numeric_Field(int prec, double low=0.0, double high=0.0)
698 : NCursesFieldType(TYPE_NUMERIC),
699 precision(prec), lower_limit(low), upper_limit(high) {
703 class Regular_Expression_Field : public NCursesFieldType {
707 void set(NCursesFormField& f) {
708 OnError(::set_field_type(f.get_field(),fieldtype,regex));
712 Regular_Expression_Field(const char *expr)
713 : NCursesFieldType(TYPE_REGEXP) {
714 regex = new char[1+::strlen(expr)];
715 (strcpy)(regex,expr);
718 ~Regular_Expression_Field() {
723 class Enumeration_Field : public NCursesFieldType {
727 int non_unique_matches;
729 void set(NCursesFormField& f) {
730 OnError(::set_field_type(f.get_field(),fieldtype,
731 list,case_sensitive,non_unique_matches));
734 Enumeration_Field(char* enums[],
735 bool case_sens=FALSE,
736 bool non_unique=FALSE)
737 : NCursesFieldType(TYPE_ENUM),
739 case_sensitive(case_sens?-1:0),
740 non_unique_matches(non_unique?-1:0) {
744 class IPV4_Address_Field : public NCursesFieldType {
746 void set(NCursesFormField& f) {
747 OnError(::set_field_type(f.get_field(),fieldtype));
751 IPV4_Address_Field() : NCursesFieldType(TYPE_IPV4) {
755 // -------------------------------------------------------------------------
756 // Abstract base class for User-Defined Fieldtypes
757 // -------------------------------------------------------------------------
759 class UserDefinedFieldType : public NCursesFieldType {
760 friend class UDF_Init; // Internal helper to set up statics
762 // For all C++ defined fieldtypes we need only one generic lowlevel
763 // FIELDTYPE* element.
764 static FIELDTYPE* generic_fieldtype;
767 // This are the functions required by the low level libforms functions
768 // to construct a fieldtype.
769 static bool fcheck(FIELD *, const void*);
770 static bool ccheck(int c, const void *);
771 static void* makearg(va_list*);
773 void set(NCursesFormField& f) {
774 OnError(::set_field_type(f.get_field(),fieldtype,&f));
778 // Redefine this function to do a field validation. The argument
779 // is a reference to the field you should validate.
780 virtual bool field_check(NCursesFormField& f) = 0;
782 // Redefine this function to do a character validation. The argument
783 // is the character to be validated.
784 virtual bool char_check (int c) = 0;
787 UserDefinedFieldType() : NCursesFieldType(generic_fieldtype) {
791 // -------------------------------------------------------------------------
792 // Abstract base class for User-Defined Fieldtypes with Choice functions
793 // -------------------------------------------------------------------------
795 class UserDefinedFieldType_With_Choice : public UserDefinedFieldType {
796 friend class UDF_Init; // Internal helper to set up statics
798 // For all C++ defined fieldtypes with choice functions we need only one
799 // generic lowlevel FIELDTYPE* element.
800 static FIELDTYPE* generic_fieldtype_with_choice;
802 // This are the functions required by the low level libforms functions
803 // to construct a fieldtype with choice functions.
804 static bool next_choice(FIELD*, const void *);
805 static bool prev_choice(FIELD*, const void *);
808 // Redefine this function to do the retrieval of the next choice value.
809 // The argument is a reference to the field tobe examined.
810 virtual bool next (NCursesFormField& f) = 0;
812 // Redefine this function to do the retrieval of the previous choice value.
813 // The argument is a reference to the field tobe examined.
814 virtual bool previous(NCursesFormField& f) = 0;
817 UserDefinedFieldType_With_Choice() {
818 fieldtype = generic_fieldtype_with_choice;