jpayne@69: // * This makes emacs happy -*-Mode: C++;-*- jpayne@69: // vile:cppmode jpayne@69: /**************************************************************************** jpayne@69: * Copyright 2019-2021,2022 Thomas E. Dickey * jpayne@69: * Copyright 1998-2012,2014 Free Software Foundation, Inc. * jpayne@69: * * jpayne@69: * Permission is hereby granted, free of charge, to any person obtaining a * jpayne@69: * copy of this software and associated documentation files (the * jpayne@69: * "Software"), to deal in the Software without restriction, including * jpayne@69: * without limitation the rights to use, copy, modify, merge, publish, * jpayne@69: * distribute, distribute with modifications, sublicense, and/or sell * jpayne@69: * copies of the Software, and to permit persons to whom the Software is * jpayne@69: * furnished to do so, subject to the following conditions: * jpayne@69: * * jpayne@69: * The above copyright notice and this permission notice shall be included * jpayne@69: * in all copies or substantial portions of the Software. * jpayne@69: * * jpayne@69: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * jpayne@69: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * jpayne@69: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * jpayne@69: * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * jpayne@69: * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * jpayne@69: * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * jpayne@69: * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * jpayne@69: * * jpayne@69: * Except as contained in this notice, the name(s) of the above copyright * jpayne@69: * holders shall not be used in advertising or otherwise to promote the * jpayne@69: * sale, use or other dealings in this Software without prior written * jpayne@69: * authorization. * jpayne@69: ****************************************************************************/ jpayne@69: jpayne@69: /**************************************************************************** jpayne@69: * Author: Juergen Pfeifer, 1997 * jpayne@69: ****************************************************************************/ jpayne@69: jpayne@69: // $Id: cursesf.h,v 1.39 2022/08/20 20:52:15 tom Exp $ jpayne@69: jpayne@69: #ifndef NCURSES_CURSESF_H_incl jpayne@69: #define NCURSES_CURSESF_H_incl 1 jpayne@69: jpayne@69: #include jpayne@69: jpayne@69: #ifndef __EXT_QNX jpayne@69: #include jpayne@69: #endif jpayne@69: jpayne@69: extern "C" { jpayne@69: # include jpayne@69: } jpayne@69: // jpayne@69: // ------------------------------------------------------------------------- jpayne@69: // The abstract base class for builtin and user defined Fieldtypes. jpayne@69: // ------------------------------------------------------------------------- jpayne@69: // jpayne@69: class NCURSES_CXX_IMPEXP NCursesFormField; // forward declaration jpayne@69: jpayne@69: // Class to represent builtin field types as well as C++ written new jpayne@69: // fieldtypes (see classes UserDefineFieldType... jpayne@69: class NCURSES_CXX_IMPEXP NCursesFieldType jpayne@69: { jpayne@69: friend class NCursesFormField; jpayne@69: jpayne@69: protected: jpayne@69: FIELDTYPE* fieldtype; jpayne@69: jpayne@69: inline void OnError(int err) const THROW2(NCursesException const, NCursesFormException) { jpayne@69: if (err!=E_OK) jpayne@69: THROW(new NCursesFormException (err)); jpayne@69: } jpayne@69: jpayne@69: NCursesFieldType(FIELDTYPE *f) : fieldtype(f) { jpayne@69: } jpayne@69: jpayne@69: virtual ~NCursesFieldType() {} jpayne@69: jpayne@69: // Set the fields f fieldtype to this one. jpayne@69: virtual void set(NCursesFormField& f) = 0; jpayne@69: jpayne@69: public: jpayne@69: NCursesFieldType() jpayne@69: : fieldtype(STATIC_CAST(FIELDTYPE*)(0)) jpayne@69: { jpayne@69: } jpayne@69: jpayne@69: NCursesFieldType& operator=(const NCursesFieldType& rhs) jpayne@69: { jpayne@69: if (this != &rhs) { jpayne@69: *this = rhs; jpayne@69: } jpayne@69: return *this; jpayne@69: } jpayne@69: jpayne@69: NCursesFieldType(const NCursesFieldType& rhs) jpayne@69: : fieldtype(rhs.fieldtype) jpayne@69: { jpayne@69: } jpayne@69: jpayne@69: }; jpayne@69: jpayne@69: // jpayne@69: // ------------------------------------------------------------------------- jpayne@69: // The class representing a forms field, wrapping the lowlevel FIELD struct jpayne@69: // ------------------------------------------------------------------------- jpayne@69: // jpayne@69: class NCURSES_CXX_IMPEXP NCursesFormField jpayne@69: { jpayne@69: friend class NCursesForm; jpayne@69: jpayne@69: protected: jpayne@69: FIELD *field; // lowlevel structure jpayne@69: NCursesFieldType* ftype; // Associated field type jpayne@69: jpayne@69: // Error handler jpayne@69: inline void OnError (int err) const THROW2(NCursesException const, NCursesFormException) { jpayne@69: if (err != E_OK) jpayne@69: THROW(new NCursesFormException (err)); jpayne@69: } jpayne@69: jpayne@69: public: jpayne@69: // Create a 'Null' field. Can be used to delimit a field list jpayne@69: NCursesFormField() jpayne@69: : field(STATIC_CAST(FIELD*)(0)), jpayne@69: ftype(STATIC_CAST(NCursesFieldType*)(0)) jpayne@69: { jpayne@69: } jpayne@69: jpayne@69: // Create a new field jpayne@69: NCursesFormField (int rows, jpayne@69: int ncols, jpayne@69: int first_row = 0, jpayne@69: int first_col = 0, jpayne@69: int offscreen_rows = 0, jpayne@69: int additional_buffers = 0) jpayne@69: : field(0), jpayne@69: ftype(STATIC_CAST(NCursesFieldType*)(0)) jpayne@69: { jpayne@69: field = ::new_field(rows, ncols, first_row, first_col, jpayne@69: offscreen_rows, additional_buffers); jpayne@69: if (!field) jpayne@69: OnError(errno); jpayne@69: } jpayne@69: jpayne@69: NCursesFormField& operator=(const NCursesFormField& rhs) jpayne@69: { jpayne@69: if (this != &rhs) { jpayne@69: *this = rhs; jpayne@69: } jpayne@69: return *this; jpayne@69: } jpayne@69: jpayne@69: NCursesFormField(const NCursesFormField& rhs) jpayne@69: : field(rhs.field), ftype(rhs.ftype) jpayne@69: { jpayne@69: } jpayne@69: jpayne@69: virtual ~NCursesFormField () THROWS(NCursesException); jpayne@69: jpayne@69: // Duplicate the field at a new position jpayne@69: inline NCursesFormField* dup(int first_row, int first_col) jpayne@69: { jpayne@69: NCursesFormField* f = new NCursesFormField(); jpayne@69: if (!f) jpayne@69: OnError(E_SYSTEM_ERROR); jpayne@69: else { jpayne@69: f->ftype = ftype; jpayne@69: f->field = ::dup_field(field,first_row,first_col); jpayne@69: if (!f->field) jpayne@69: OnError(errno); jpayne@69: } jpayne@69: return f; jpayne@69: } jpayne@69: jpayne@69: // Link the field to a new location jpayne@69: inline NCursesFormField* link(int first_row, int first_col) { jpayne@69: NCursesFormField* f = new NCursesFormField(); jpayne@69: if (!f) jpayne@69: OnError(E_SYSTEM_ERROR); jpayne@69: else { jpayne@69: f->ftype = ftype; jpayne@69: f->field = ::link_field(field,first_row,first_col); jpayne@69: if (!f->field) jpayne@69: OnError(errno); jpayne@69: } jpayne@69: return f; jpayne@69: } jpayne@69: jpayne@69: // Get the lowlevel field representation jpayne@69: inline FIELD* get_field() const { jpayne@69: return field; jpayne@69: } jpayne@69: jpayne@69: // Retrieve info about the field jpayne@69: inline void info(int& rows, int& ncols, jpayne@69: int& first_row, int& first_col, jpayne@69: int& offscreen_rows, int& additional_buffers) const { jpayne@69: OnError(::field_info(field, &rows, &ncols, jpayne@69: &first_row, &first_col, jpayne@69: &offscreen_rows, &additional_buffers)); jpayne@69: } jpayne@69: jpayne@69: // Retrieve info about the fields dynamic properties. jpayne@69: inline void dynamic_info(int& dynamic_rows, int& dynamic_cols, jpayne@69: int& max_growth) const { jpayne@69: OnError(::dynamic_field_info(field, &dynamic_rows, &dynamic_cols, jpayne@69: &max_growth)); jpayne@69: } jpayne@69: jpayne@69: // For a dynamic field you may set the maximum growth limit. jpayne@69: // A zero means unlimited growth. jpayne@69: inline void set_maximum_growth(int growth = 0) { jpayne@69: OnError(::set_max_field(field,growth)); jpayne@69: } jpayne@69: jpayne@69: // Move the field to a new position jpayne@69: inline void move(int row, int col) { jpayne@69: OnError(::move_field(field,row,col)); jpayne@69: } jpayne@69: jpayne@69: // Mark the field to start a new page jpayne@69: inline void new_page(bool pageFlag = FALSE) { jpayne@69: OnError(::set_new_page(field,pageFlag)); jpayne@69: } jpayne@69: jpayne@69: // Retrieve whether or not the field starts a new page. jpayne@69: inline bool is_new_page() const { jpayne@69: return ::new_page(field); jpayne@69: } jpayne@69: jpayne@69: // Set the justification for the field jpayne@69: inline void set_justification(int just) { jpayne@69: OnError(::set_field_just(field,just)); jpayne@69: } jpayne@69: jpayne@69: // Retrieve the fields justification jpayne@69: inline int justification() const { jpayne@69: return ::field_just(field); jpayne@69: } jpayne@69: // Set the foreground attribute for the field jpayne@69: inline void set_foreground(chtype foreground) { jpayne@69: OnError(::set_field_fore(field,foreground)); jpayne@69: } jpayne@69: jpayne@69: // Retrieve the fields foreground attribute jpayne@69: inline chtype fore() const { jpayne@69: return ::field_fore(field); jpayne@69: } jpayne@69: jpayne@69: // Set the background attribute for the field jpayne@69: inline void set_background(chtype background) { jpayne@69: OnError(::set_field_back(field,background)); jpayne@69: } jpayne@69: jpayne@69: // Retrieve the fields background attribute jpayne@69: inline chtype back() const { jpayne@69: return ::field_back(field); jpayne@69: } jpayne@69: jpayne@69: // Set the padding character for the field jpayne@69: inline void set_pad_character(int padding) { jpayne@69: OnError(::set_field_pad(field, padding)); jpayne@69: } jpayne@69: jpayne@69: // Retrieve the fields padding character jpayne@69: inline int pad() const { jpayne@69: return ::field_pad(field); jpayne@69: } jpayne@69: jpayne@69: // Switch on the fields options jpayne@69: inline void options_on (Field_Options opts) { jpayne@69: OnError (::field_opts_on (field, opts)); jpayne@69: } jpayne@69: jpayne@69: // Switch off the fields options jpayne@69: inline void options_off (Field_Options opts) { jpayne@69: OnError (::field_opts_off (field, opts)); jpayne@69: } jpayne@69: jpayne@69: // Retrieve the fields options jpayne@69: inline Field_Options options () const { jpayne@69: return ::field_opts (field); jpayne@69: } jpayne@69: jpayne@69: // Set the fields options jpayne@69: inline void set_options (Field_Options opts) { jpayne@69: OnError (::set_field_opts (field, opts)); jpayne@69: } jpayne@69: jpayne@69: // Mark the field as changed jpayne@69: inline void set_changed(bool changeFlag = TRUE) { jpayne@69: OnError(::set_field_status(field,changeFlag)); jpayne@69: } jpayne@69: jpayne@69: // Test whether or not the field is marked as changed jpayne@69: inline bool changed() const { jpayne@69: return ::field_status(field); jpayne@69: } jpayne@69: jpayne@69: // Return the index of the field in the field array of a form jpayne@69: // or -1 if the field is not associated to a form jpayne@69: inline int (index)() const { jpayne@69: return ::field_index(field); jpayne@69: } jpayne@69: jpayne@69: // Store a value in a fields buffer. The default buffer is nr. 0 jpayne@69: inline void set_value(const char *val, int buffer = 0) { jpayne@69: OnError(::set_field_buffer(field,buffer,val)); jpayne@69: } jpayne@69: jpayne@69: // Retrieve the value of a fields buffer. The default buffer is nr. 0 jpayne@69: inline char* value(int buffer = 0) const { jpayne@69: return ::field_buffer(field,buffer); jpayne@69: } jpayne@69: jpayne@69: // Set the validation type of the field. jpayne@69: inline void set_fieldtype(NCursesFieldType& f) { jpayne@69: ftype = &f; jpayne@69: f.set(*this); // A good friend may do that... jpayne@69: } jpayne@69: jpayne@69: // Retrieve the validation type of the field. jpayne@69: inline NCursesFieldType* fieldtype() const { jpayne@69: return ftype; jpayne@69: } jpayne@69: jpayne@69: }; jpayne@69: jpayne@69: // This are the built-in hook functions in this C++ binding. In C++ we use jpayne@69: // virtual member functions (see below On_..._Init and On_..._Termination) jpayne@69: // to provide this functionality in an object oriented manner. jpayne@69: extern "C" { jpayne@69: void _nc_xx_frm_init(FORM *); jpayne@69: void _nc_xx_frm_term(FORM *); jpayne@69: void _nc_xx_fld_init(FORM *); jpayne@69: void _nc_xx_fld_term(FORM *); jpayne@69: } jpayne@69: jpayne@69: // jpayne@69: // ------------------------------------------------------------------------- jpayne@69: // The class representing a form, wrapping the lowlevel FORM struct jpayne@69: // ------------------------------------------------------------------------- jpayne@69: // jpayne@69: class NCURSES_CXX_IMPEXP NCursesForm : public NCursesPanel jpayne@69: { jpayne@69: protected: jpayne@69: FORM* form; // the lowlevel structure jpayne@69: jpayne@69: private: jpayne@69: NCursesWindow* sub; // the subwindow object jpayne@69: bool b_sub_owner; // is this our own subwindow? jpayne@69: bool b_framed; // has the form a border? jpayne@69: bool b_autoDelete; // Delete fields when deleting form? jpayne@69: jpayne@69: NCursesFormField** my_fields; // The array of fields for this form jpayne@69: jpayne@69: // This structure is used for the form's user data field to link the jpayne@69: // FORM* to the C++ object and to provide extra space for a user pointer. jpayne@69: typedef struct { jpayne@69: void* m_user; // the pointer for the user's data jpayne@69: const NCursesForm* m_back; // backward pointer to C++ object jpayne@69: const FORM* m_owner; jpayne@69: } UserHook; jpayne@69: jpayne@69: // Get the backward pointer to the C++ object from a FORM jpayne@69: static inline NCursesForm* getHook(const FORM *f) { jpayne@69: UserHook* hook = reinterpret_cast(::form_userptr(f)); jpayne@69: assert(hook != 0 && hook->m_owner==f); jpayne@69: return const_cast(hook->m_back); jpayne@69: } jpayne@69: jpayne@69: friend void _nc_xx_frm_init(FORM *); jpayne@69: friend void _nc_xx_frm_term(FORM *); jpayne@69: friend void _nc_xx_fld_init(FORM *); jpayne@69: friend void _nc_xx_fld_term(FORM *); jpayne@69: jpayne@69: // Calculate FIELD* array for the menu jpayne@69: FIELD** mapFields(NCursesFormField* nfields[]); jpayne@69: jpayne@69: protected: jpayne@69: // internal routines jpayne@69: inline void set_user(void *user) { jpayne@69: UserHook* uptr = reinterpret_cast(::form_userptr (form)); jpayne@69: assert (uptr != 0 && uptr->m_back==this && uptr->m_owner==form); jpayne@69: uptr->m_user = user; jpayne@69: } jpayne@69: jpayne@69: inline void *get_user() { jpayne@69: UserHook* uptr = reinterpret_cast(::form_userptr (form)); jpayne@69: assert (uptr != 0 && uptr->m_back==this && uptr->m_owner==form); jpayne@69: return uptr->m_user; jpayne@69: } jpayne@69: jpayne@69: void InitForm (NCursesFormField* Fields[], jpayne@69: bool with_frame, jpayne@69: bool autoDeleteFields); jpayne@69: jpayne@69: inline void OnError (int err) const THROW2(NCursesException const, NCursesFormException) { jpayne@69: if (err != E_OK) jpayne@69: THROW(new NCursesFormException (err)); jpayne@69: } jpayne@69: jpayne@69: // this wraps the form_driver call. jpayne@69: virtual int driver (int c) ; jpayne@69: jpayne@69: // 'Internal' constructor, builds an object without association to a jpayne@69: // field array. jpayne@69: NCursesForm( int nlines, jpayne@69: int ncols, jpayne@69: int begin_y = 0, jpayne@69: int begin_x = 0) jpayne@69: : NCursesPanel(nlines, ncols, begin_y, begin_x), jpayne@69: form (STATIC_CAST(FORM*)(0)), jpayne@69: sub(0), jpayne@69: b_sub_owner(0), jpayne@69: b_framed(0), jpayne@69: b_autoDelete(0), jpayne@69: my_fields(0) jpayne@69: { jpayne@69: } jpayne@69: jpayne@69: public: jpayne@69: // Create form for the default panel. jpayne@69: NCursesForm (NCursesFormField* Fields[], jpayne@69: bool with_frame=FALSE, // reserve space for a frame? jpayne@69: bool autoDelete_Fields=FALSE) // do automatic cleanup? jpayne@69: : NCursesPanel(), jpayne@69: form(0), jpayne@69: sub(0), jpayne@69: b_sub_owner(0), jpayne@69: b_framed(0), jpayne@69: b_autoDelete(0), jpayne@69: my_fields(0) jpayne@69: { jpayne@69: InitForm(Fields, with_frame, autoDelete_Fields); jpayne@69: } jpayne@69: jpayne@69: // Create a form in a panel with the given position and size. jpayne@69: NCursesForm (NCursesFormField* Fields[], jpayne@69: int nlines, jpayne@69: int ncols, jpayne@69: int begin_y, jpayne@69: int begin_x, jpayne@69: bool with_frame=FALSE, // reserve space for a frame? jpayne@69: bool autoDelete_Fields=FALSE) // do automatic cleanup? jpayne@69: : NCursesPanel(nlines, ncols, begin_y, begin_x), jpayne@69: form(0), jpayne@69: sub(0), jpayne@69: b_sub_owner(0), jpayne@69: b_framed(0), jpayne@69: b_autoDelete(0), jpayne@69: my_fields(0) jpayne@69: { jpayne@69: InitForm(Fields, with_frame, autoDelete_Fields); jpayne@69: } jpayne@69: jpayne@69: NCursesForm& operator=(const NCursesForm& rhs) jpayne@69: { jpayne@69: if (this != &rhs) { jpayne@69: *this = rhs; jpayne@69: NCursesPanel::operator=(rhs); jpayne@69: } jpayne@69: return *this; jpayne@69: } jpayne@69: jpayne@69: NCursesForm(const NCursesForm& rhs) jpayne@69: : NCursesPanel(rhs), jpayne@69: form(rhs.form), jpayne@69: sub(rhs.sub), jpayne@69: b_sub_owner(rhs.b_sub_owner), jpayne@69: b_framed(rhs.b_framed), jpayne@69: b_autoDelete(rhs.b_autoDelete), jpayne@69: my_fields(rhs.my_fields) jpayne@69: { jpayne@69: } jpayne@69: jpayne@69: virtual ~NCursesForm() THROWS(NCursesException); jpayne@69: jpayne@69: // Set the default attributes for the form jpayne@69: virtual void setDefaultAttributes(); jpayne@69: jpayne@69: // Retrieve current field of the form. jpayne@69: inline NCursesFormField* current_field() const { jpayne@69: return my_fields[::field_index(::current_field(form))]; jpayne@69: } jpayne@69: jpayne@69: // Set the forms subwindow jpayne@69: void setSubWindow(NCursesWindow& sub); jpayne@69: jpayne@69: // Set these fields for the form jpayne@69: inline void setFields(NCursesFormField* Fields[]) { jpayne@69: OnError(::set_form_fields(form,mapFields(Fields))); jpayne@69: } jpayne@69: jpayne@69: // Remove the form from the screen jpayne@69: inline void unpost (void) { jpayne@69: OnError (::unpost_form (form)); jpayne@69: } jpayne@69: jpayne@69: // Post the form to the screen if flag is true, unpost it otherwise jpayne@69: inline void post(bool flag = TRUE) { jpayne@69: OnError (flag ? ::post_form(form) : ::unpost_form (form)); jpayne@69: } jpayne@69: jpayne@69: // Decorations jpayne@69: inline void frame(const char *title=NULL, const char* btitle=NULL) NCURSES_OVERRIDE { jpayne@69: if (b_framed) jpayne@69: NCursesPanel::frame(title,btitle); jpayne@69: else jpayne@69: OnError(E_SYSTEM_ERROR); jpayne@69: } jpayne@69: jpayne@69: inline void boldframe(const char *title=NULL, const char* btitle=NULL) NCURSES_OVERRIDE { jpayne@69: if (b_framed) jpayne@69: NCursesPanel::boldframe(title,btitle); jpayne@69: else jpayne@69: OnError(E_SYSTEM_ERROR); jpayne@69: } jpayne@69: jpayne@69: inline void label(const char *topLabel, const char *bottomLabel) NCURSES_OVERRIDE { jpayne@69: if (b_framed) jpayne@69: NCursesPanel::label(topLabel,bottomLabel); jpayne@69: else jpayne@69: OnError(E_SYSTEM_ERROR); jpayne@69: } jpayne@69: jpayne@69: // ----- jpayne@69: // Hooks jpayne@69: // ----- jpayne@69: jpayne@69: // Called after the form gets repositioned in its window. jpayne@69: // This is especially true if the form is posted. jpayne@69: virtual void On_Form_Init(); jpayne@69: jpayne@69: // Called before the form gets repositioned in its window. jpayne@69: // This is especially true if the form is unposted. jpayne@69: virtual void On_Form_Termination(); jpayne@69: jpayne@69: // Called after the field became the current field jpayne@69: virtual void On_Field_Init(NCursesFormField& field); jpayne@69: jpayne@69: // Called before this field is left as current field. jpayne@69: virtual void On_Field_Termination(NCursesFormField& field); jpayne@69: jpayne@69: // Calculate required window size for the form. jpayne@69: void scale(int& rows, int& ncols) const { jpayne@69: OnError(::scale_form(form,&rows,&ncols)); jpayne@69: } jpayne@69: jpayne@69: // Retrieve number of fields in the form. jpayne@69: int count() const { jpayne@69: return ::field_count(form); jpayne@69: } jpayne@69: jpayne@69: // Make the page the current page of the form. jpayne@69: void set_page(int pageNum) { jpayne@69: OnError(::set_form_page(form, pageNum)); jpayne@69: } jpayne@69: jpayne@69: // Retrieve current page number jpayne@69: int page() const { jpayne@69: return ::form_page(form); jpayne@69: } jpayne@69: jpayne@69: // Switch on the forms options jpayne@69: inline void options_on (Form_Options opts) { jpayne@69: OnError (::form_opts_on (form, opts)); jpayne@69: } jpayne@69: jpayne@69: // Switch off the forms options jpayne@69: inline void options_off (Form_Options opts) { jpayne@69: OnError (::form_opts_off (form, opts)); jpayne@69: } jpayne@69: jpayne@69: // Retrieve the forms options jpayne@69: inline Form_Options options () const { jpayne@69: return ::form_opts (form); jpayne@69: } jpayne@69: jpayne@69: // Set the forms options jpayne@69: inline void set_options (Form_Options opts) { jpayne@69: OnError (::set_form_opts (form, opts)); jpayne@69: } jpayne@69: jpayne@69: // Are there more data in the current field after the data shown jpayne@69: inline bool data_ahead() const { jpayne@69: return ::data_ahead(form); jpayne@69: } jpayne@69: jpayne@69: // Are there more data in the current field before the data shown jpayne@69: inline bool data_behind() const { jpayne@69: return ::data_behind(form); jpayne@69: } jpayne@69: jpayne@69: // Position the cursor to the current field jpayne@69: inline void position_cursor () { jpayne@69: OnError (::pos_form_cursor (form)); jpayne@69: } jpayne@69: // Set the current field jpayne@69: inline void set_current(NCursesFormField& F) { jpayne@69: OnError (::set_current_field(form, F.field)); jpayne@69: } jpayne@69: jpayne@69: // Provide a default key virtualization. Translate the keyboard jpayne@69: // code c into a form request code. jpayne@69: // The default implementation provides a hopefully straightforward jpayne@69: // mapping for the most common keystrokes and form requests. jpayne@69: virtual int virtualize(int c); jpayne@69: jpayne@69: // Operators jpayne@69: inline NCursesFormField* operator[](int i) const { jpayne@69: if ( (i < 0) || (i >= ::field_count (form)) ) jpayne@69: OnError (E_BAD_ARGUMENT); jpayne@69: return my_fields[i]; jpayne@69: } jpayne@69: jpayne@69: // Perform the menu's operation jpayne@69: // Return the field where you left the form. jpayne@69: virtual NCursesFormField* operator()(void); jpayne@69: jpayne@69: // Exception handlers. The default is a Beep. jpayne@69: virtual void On_Request_Denied(int c) const; jpayne@69: virtual void On_Invalid_Field(int c) const; jpayne@69: virtual void On_Unknown_Command(int c) const; jpayne@69: jpayne@69: }; jpayne@69: jpayne@69: // jpayne@69: // ------------------------------------------------------------------------- jpayne@69: // This is the typical C++ typesafe way to allow to attach jpayne@69: // user data to a field of a form. Its assumed that the user jpayne@69: // data belongs to some class T. Use T as template argument jpayne@69: // to create a UserField. jpayne@69: // ------------------------------------------------------------------------- jpayne@69: template class NCURSES_CXX_IMPEXP NCursesUserField : public NCursesFormField jpayne@69: { jpayne@69: public: jpayne@69: NCursesUserField (int rows, jpayne@69: int ncols, jpayne@69: int first_row = 0, jpayne@69: int first_col = 0, jpayne@69: const T* p_UserData = STATIC_CAST(T*)(0), jpayne@69: int offscreen_rows = 0, jpayne@69: int additional_buffers = 0) jpayne@69: : NCursesFormField (rows, ncols, jpayne@69: first_row, first_col, jpayne@69: offscreen_rows, additional_buffers) { jpayne@69: if (field) jpayne@69: OnError(::set_field_userptr(field, STATIC_CAST(void *)(p_UserData))); jpayne@69: } jpayne@69: jpayne@69: virtual ~NCursesUserField() THROWS(NCursesException) {}; jpayne@69: jpayne@69: inline const T* UserData (void) const { jpayne@69: return reinterpret_cast(::field_userptr (field)); jpayne@69: } jpayne@69: jpayne@69: inline virtual void setUserData(const T* p_UserData) { jpayne@69: if (field) jpayne@69: OnError (::set_field_userptr (field, STATIC_CAST(void *)(p_UserData))); jpayne@69: } jpayne@69: }; jpayne@69: // jpayne@69: // ------------------------------------------------------------------------- jpayne@69: // The same mechanism is used to attach user data to a form jpayne@69: // ------------------------------------------------------------------------- jpayne@69: // jpayne@69: template class NCURSES_CXX_IMPEXP NCursesUserForm : public NCursesForm jpayne@69: { jpayne@69: protected: jpayne@69: // 'Internal' constructor, builds an object without association to a jpayne@69: // field array. jpayne@69: NCursesUserForm( int nlines, jpayne@69: int ncols, jpayne@69: int begin_y = 0, jpayne@69: int begin_x = 0, jpayne@69: const T* p_UserData = STATIC_CAST(T*)(0)) jpayne@69: : NCursesForm(nlines,ncols,begin_y,begin_x) { jpayne@69: if (form) jpayne@69: set_user (const_cast(reinterpret_cast jpayne@69: (p_UserData))); jpayne@69: } jpayne@69: jpayne@69: public: jpayne@69: NCursesUserForm (NCursesFormField* Fields[], jpayne@69: const T* p_UserData = STATIC_CAST(T*)(0), jpayne@69: bool with_frame=FALSE, jpayne@69: bool autoDelete_Fields=FALSE) jpayne@69: : NCursesForm (Fields, with_frame, autoDelete_Fields) { jpayne@69: if (form) jpayne@69: set_user (const_cast(reinterpret_cast(p_UserData))); jpayne@69: }; jpayne@69: jpayne@69: NCursesUserForm (NCursesFormField* Fields[], jpayne@69: int nlines, jpayne@69: int ncols, jpayne@69: int begin_y = 0, jpayne@69: int begin_x = 0, jpayne@69: const T* p_UserData = STATIC_CAST(T*)(0), jpayne@69: bool with_frame=FALSE, jpayne@69: bool autoDelete_Fields=FALSE) jpayne@69: : NCursesForm (Fields, nlines, ncols, begin_y, begin_x, jpayne@69: with_frame, autoDelete_Fields) { jpayne@69: if (form) jpayne@69: set_user (const_cast(reinterpret_cast jpayne@69: (p_UserData))); jpayne@69: }; jpayne@69: jpayne@69: virtual ~NCursesUserForm() THROWS(NCursesException) { jpayne@69: }; jpayne@69: jpayne@69: inline T* UserData (void) { jpayne@69: return reinterpret_cast(get_user ()); jpayne@69: }; jpayne@69: jpayne@69: inline virtual void setUserData (const T* p_UserData) { jpayne@69: if (form) jpayne@69: set_user (const_cast(reinterpret_cast(p_UserData))); jpayne@69: } jpayne@69: jpayne@69: }; jpayne@69: // jpayne@69: // ------------------------------------------------------------------------- jpayne@69: // Builtin Fieldtypes jpayne@69: // ------------------------------------------------------------------------- jpayne@69: // jpayne@69: class NCURSES_CXX_IMPEXP Alpha_Field : public NCursesFieldType jpayne@69: { jpayne@69: private: jpayne@69: int min_field_width; jpayne@69: jpayne@69: void set(NCursesFormField& f) NCURSES_OVERRIDE { jpayne@69: OnError(::set_field_type(f.get_field(),fieldtype,min_field_width)); jpayne@69: } jpayne@69: jpayne@69: public: jpayne@69: explicit Alpha_Field(int width) jpayne@69: : NCursesFieldType(TYPE_ALPHA), jpayne@69: min_field_width(width) { jpayne@69: } jpayne@69: }; jpayne@69: jpayne@69: class NCURSES_CXX_IMPEXP Alphanumeric_Field : public NCursesFieldType jpayne@69: { jpayne@69: private: jpayne@69: int min_field_width; jpayne@69: jpayne@69: void set(NCursesFormField& f) NCURSES_OVERRIDE { jpayne@69: OnError(::set_field_type(f.get_field(),fieldtype,min_field_width)); jpayne@69: } jpayne@69: jpayne@69: public: jpayne@69: explicit Alphanumeric_Field(int width) jpayne@69: : NCursesFieldType(TYPE_ALNUM), jpayne@69: min_field_width(width) { jpayne@69: } jpayne@69: }; jpayne@69: jpayne@69: class NCURSES_CXX_IMPEXP Integer_Field : public NCursesFieldType jpayne@69: { jpayne@69: private: jpayne@69: int precision; jpayne@69: long lower_limit, upper_limit; jpayne@69: jpayne@69: void set(NCursesFormField& f) NCURSES_OVERRIDE { jpayne@69: OnError(::set_field_type(f.get_field(),fieldtype, jpayne@69: precision,lower_limit,upper_limit)); jpayne@69: } jpayne@69: jpayne@69: public: jpayne@69: Integer_Field(int prec, long low=0L, long high=0L) jpayne@69: : NCursesFieldType(TYPE_INTEGER), jpayne@69: precision(prec), lower_limit(low), upper_limit(high) { jpayne@69: } jpayne@69: }; jpayne@69: jpayne@69: class NCURSES_CXX_IMPEXP Numeric_Field : public NCursesFieldType jpayne@69: { jpayne@69: private: jpayne@69: int precision; jpayne@69: double lower_limit, upper_limit; jpayne@69: jpayne@69: void set(NCursesFormField& f) NCURSES_OVERRIDE { jpayne@69: OnError(::set_field_type(f.get_field(),fieldtype, jpayne@69: precision,lower_limit,upper_limit)); jpayne@69: } jpayne@69: jpayne@69: public: jpayne@69: Numeric_Field(int prec, double low=0.0, double high=0.0) jpayne@69: : NCursesFieldType(TYPE_NUMERIC), jpayne@69: precision(prec), lower_limit(low), upper_limit(high) { jpayne@69: } jpayne@69: }; jpayne@69: jpayne@69: class NCURSES_CXX_IMPEXP Regular_Expression_Field : public NCursesFieldType jpayne@69: { jpayne@69: private: jpayne@69: char* regex; jpayne@69: jpayne@69: void set(NCursesFormField& f) NCURSES_OVERRIDE { jpayne@69: OnError(::set_field_type(f.get_field(),fieldtype,regex)); jpayne@69: } jpayne@69: jpayne@69: void copy_regex(const char *source) jpayne@69: { jpayne@69: regex = new char[1 + ::strlen(source)]; jpayne@69: (::strcpy)(regex, source); jpayne@69: } jpayne@69: jpayne@69: public: jpayne@69: explicit Regular_Expression_Field(const char *expr) jpayne@69: : NCursesFieldType(TYPE_REGEXP), jpayne@69: regex(NULL) jpayne@69: { jpayne@69: copy_regex(expr); jpayne@69: } jpayne@69: jpayne@69: Regular_Expression_Field& operator=(const Regular_Expression_Field& rhs) jpayne@69: { jpayne@69: if (this != &rhs) { jpayne@69: *this = rhs; jpayne@69: copy_regex(rhs.regex); jpayne@69: NCursesFieldType::operator=(rhs); jpayne@69: } jpayne@69: return *this; jpayne@69: } jpayne@69: jpayne@69: Regular_Expression_Field(const Regular_Expression_Field& rhs) jpayne@69: : NCursesFieldType(rhs), jpayne@69: regex(NULL) jpayne@69: { jpayne@69: copy_regex(rhs.regex); jpayne@69: } jpayne@69: jpayne@69: ~Regular_Expression_Field() { jpayne@69: delete[] regex; jpayne@69: } jpayne@69: }; jpayne@69: jpayne@69: class NCURSES_CXX_IMPEXP Enumeration_Field : public NCursesFieldType jpayne@69: { jpayne@69: private: jpayne@69: const char** list; jpayne@69: int case_sensitive; jpayne@69: int non_unique_matches; jpayne@69: jpayne@69: void set(NCursesFormField& f) NCURSES_OVERRIDE { jpayne@69: OnError(::set_field_type(f.get_field(),fieldtype, jpayne@69: list,case_sensitive,non_unique_matches)); jpayne@69: } jpayne@69: public: jpayne@69: Enumeration_Field(const char* enums[], jpayne@69: bool case_sens=FALSE, jpayne@69: bool non_unique=FALSE) jpayne@69: : NCursesFieldType(TYPE_ENUM), jpayne@69: list(enums), jpayne@69: case_sensitive(case_sens ? -1 : 0), jpayne@69: non_unique_matches(non_unique ? -1 : 0) { jpayne@69: } jpayne@69: jpayne@69: Enumeration_Field& operator=(const Enumeration_Field& rhs) jpayne@69: { jpayne@69: if (this != &rhs) { jpayne@69: *this = rhs; jpayne@69: NCursesFieldType::operator=(rhs); jpayne@69: } jpayne@69: return *this; jpayne@69: } jpayne@69: jpayne@69: Enumeration_Field(const Enumeration_Field& rhs) jpayne@69: : NCursesFieldType(rhs), jpayne@69: list(rhs.list), jpayne@69: case_sensitive(rhs.case_sensitive), jpayne@69: non_unique_matches(rhs.non_unique_matches) jpayne@69: { jpayne@69: } jpayne@69: }; jpayne@69: jpayne@69: class NCURSES_CXX_IMPEXP IPV4_Address_Field : public NCursesFieldType jpayne@69: { jpayne@69: private: jpayne@69: void set(NCursesFormField& f) NCURSES_OVERRIDE { jpayne@69: OnError(::set_field_type(f.get_field(),fieldtype)); jpayne@69: } jpayne@69: jpayne@69: public: jpayne@69: IPV4_Address_Field() : NCursesFieldType(TYPE_IPV4) { jpayne@69: } jpayne@69: }; jpayne@69: jpayne@69: extern "C" { jpayne@69: bool _nc_xx_fld_fcheck(FIELD *, const void*); jpayne@69: bool _nc_xx_fld_ccheck(int c, const void *); jpayne@69: void* _nc_xx_fld_makearg(va_list*); jpayne@69: } jpayne@69: jpayne@69: // jpayne@69: // ------------------------------------------------------------------------- jpayne@69: // Abstract base class for User-Defined Fieldtypes jpayne@69: // ------------------------------------------------------------------------- jpayne@69: // jpayne@69: class NCURSES_CXX_IMPEXP UserDefinedFieldType : public NCursesFieldType jpayne@69: { jpayne@69: friend class UDF_Init; // Internal helper to set up statics jpayne@69: private: jpayne@69: // For all C++ defined fieldtypes we need only one generic lowlevel jpayne@69: // FIELDTYPE* element. jpayne@69: static FIELDTYPE* generic_fieldtype; jpayne@69: jpayne@69: protected: jpayne@69: // This are the functions required by the low level libforms functions jpayne@69: // to construct a fieldtype. jpayne@69: friend bool _nc_xx_fld_fcheck(FIELD *, const void*); jpayne@69: friend bool _nc_xx_fld_ccheck(int c, const void *); jpayne@69: friend void* _nc_xx_fld_makearg(va_list*); jpayne@69: jpayne@69: void set(NCursesFormField& f) NCURSES_OVERRIDE { jpayne@69: OnError(::set_field_type(f.get_field(),fieldtype,&f)); jpayne@69: } jpayne@69: jpayne@69: protected: jpayne@69: // Redefine this function to do a field validation. The argument jpayne@69: // is a reference to the field you should validate. jpayne@69: virtual bool field_check(NCursesFormField& f) = 0; jpayne@69: jpayne@69: // Redefine this function to do a character validation. The argument jpayne@69: // is the character to be validated. jpayne@69: virtual bool char_check (int c) = 0; jpayne@69: jpayne@69: public: jpayne@69: UserDefinedFieldType(); jpayne@69: }; jpayne@69: jpayne@69: extern "C" { jpayne@69: bool _nc_xx_next_choice(FIELD*, const void *); jpayne@69: bool _nc_xx_prev_choice(FIELD*, const void *); jpayne@69: } jpayne@69: jpayne@69: // jpayne@69: // ------------------------------------------------------------------------- jpayne@69: // Abstract base class for User-Defined Fieldtypes with Choice functions jpayne@69: // ------------------------------------------------------------------------- jpayne@69: // jpayne@69: class NCURSES_CXX_IMPEXP UserDefinedFieldType_With_Choice : public UserDefinedFieldType jpayne@69: { jpayne@69: friend class UDF_Init; // Internal helper to set up statics jpayne@69: private: jpayne@69: // For all C++ defined fieldtypes with choice functions we need only one jpayne@69: // generic lowlevel FIELDTYPE* element. jpayne@69: static FIELDTYPE* generic_fieldtype_with_choice; jpayne@69: jpayne@69: // This are the functions required by the low level libforms functions jpayne@69: // to construct a fieldtype with choice functions. jpayne@69: friend bool _nc_xx_next_choice(FIELD*, const void *); jpayne@69: friend bool _nc_xx_prev_choice(FIELD*, const void *); jpayne@69: jpayne@69: protected: jpayne@69: // Redefine this function to do the retrieval of the next choice value. jpayne@69: // The argument is a reference to the field tobe examined. jpayne@69: virtual bool next (NCursesFormField& f) = 0; jpayne@69: jpayne@69: // Redefine this function to do the retrieval of the previous choice value. jpayne@69: // The argument is a reference to the field tobe examined. jpayne@69: virtual bool previous(NCursesFormField& f) = 0; jpayne@69: jpayne@69: public: jpayne@69: UserDefinedFieldType_With_Choice(); jpayne@69: }; jpayne@69: jpayne@69: #endif /* NCURSES_CURSESF_H_incl */