ncursesw-morphos/c++/cursesf.cc

455 lines
12 KiB
C++
Raw Normal View History

1998-03-01 12:21:12 +08:00
// * this is for making emacs happy: -*-Mode: C++;-*-
/****************************************************************************
2005-10-10 02:41:57 +08:00
* Copyright (c) 1998-2003,2005 Free Software Foundation, Inc. *
1998-03-01 12:21:12 +08:00
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the *
* "Software"), to deal in the Software without restriction, including *
* without limitation the rights to use, copy, modify, merge, publish, *
* distribute, distribute with modifications, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included *
* in all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
* IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
* THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
* *
* Except as contained in this notice, the name(s) of the above copyright *
* holders shall not be used in advertising or otherwise to promote the *
* sale, use or other dealings in this Software without prior written *
* authorization. *
****************************************************************************/
/****************************************************************************
2002-10-13 11:35:53 +08:00
* Author: Juergen Pfeifer, 1997 *
1998-03-01 12:21:12 +08:00
****************************************************************************/
2002-10-13 11:35:53 +08:00
#include "internal.h"
1998-03-01 12:21:12 +08:00
#include "cursesf.h"
#include "cursesapp.h"
2005-10-10 02:41:57 +08:00
MODULE_ID("$Id: cursesf.cc,v 1.21 2005/08/13 18:09:06 tom Exp $")
2002-10-13 11:35:53 +08:00
2005-10-10 02:41:57 +08:00
NCursesFormField::~NCursesFormField ()
{
1998-03-01 12:21:12 +08:00
if (field)
OnError(::free_field (field));
}
/* Construct a FIELD* array from an array of NCursesFormField
* objects.
*/
FIELD**
2005-10-10 02:41:57 +08:00
NCursesForm::mapFields(NCursesFormField* nfields[])
{
1998-03-01 12:21:12 +08:00
int fieldCount = 0,lcv;
2000-10-21 12:42:11 +08:00
FIELD** old_fields;
2000-07-09 10:46:08 +08:00
assert(nfields != 0);
1998-03-01 12:21:12 +08:00
for (lcv=0; nfields[lcv]->field; ++lcv)
++fieldCount;
2002-10-13 11:35:53 +08:00
1998-03-01 12:21:12 +08:00
FIELD** fields = new FIELD*[fieldCount + 1];
2002-10-13 11:35:53 +08:00
1998-03-01 12:21:12 +08:00
for (lcv=0;nfields[lcv]->field;++lcv) {
fields[lcv] = nfields[lcv]->field;
}
fields[lcv] = NULL;
2002-10-13 11:35:53 +08:00
1998-03-01 12:21:12 +08:00
my_fields = nfields;
2002-10-13 11:35:53 +08:00
2000-10-21 12:42:11 +08:00
if (form && (old_fields = ::form_fields(form))) {
2005-10-10 02:41:57 +08:00
::set_form_fields(form, static_cast<FIELD**>(0));
2000-10-21 12:42:11 +08:00
delete[] old_fields;
}
1998-03-01 12:21:12 +08:00
return fields;
}
2005-10-10 02:41:57 +08:00
void NCursesForm::setDefaultAttributes()
{
1998-03-01 12:21:12 +08:00
NCursesApplication* S = NCursesApplication::getApplication();
int n = count();
if (n > 0) {
for(int i=0; i<n; i++) {
NCursesFormField* f = (*this)[i];
if ((f->options() & (O_EDIT|O_ACTIVE))==(O_EDIT|O_ACTIVE)) {
if (S) {
f->set_foreground(S->foregrounds());
f->set_background(S->backgrounds());
}
f->set_pad_character('_');
}
else {
if (S)
f->set_background(S->labels());
}
}
}
if (S) {
bkgd(' '|S->dialog_backgrounds());
if (sub)
sub->bkgd(' '|S->dialog_backgrounds());
}
}
void
NCursesForm::InitForm(NCursesFormField* nfields[],
bool with_frame,
2005-10-10 02:41:57 +08:00
bool autoDelete_Fields)
{
1998-03-01 12:21:12 +08:00
int mrows, mcols;
2002-10-13 11:35:53 +08:00
1998-03-01 12:21:12 +08:00
keypad(TRUE);
meta(TRUE);
b_framed = with_frame;
b_autoDelete = autoDelete_Fields;
2005-10-10 02:41:57 +08:00
form = static_cast<FORM*>(0);
1998-03-01 12:21:12 +08:00
form = ::new_form(mapFields(nfields));
if (!form)
OnError (E_SYSTEM_ERROR);
2002-10-13 11:35:53 +08:00
1998-03-01 12:21:12 +08:00
UserHook* hook = new UserHook;
hook->m_user = NULL;
hook->m_back = this;
hook->m_owner = form;
2005-10-10 02:41:57 +08:00
::set_form_userptr(form, reinterpret_cast<void*>(hook));
2002-10-13 11:35:53 +08:00
2005-10-10 02:41:57 +08:00
::set_form_init (form, _nc_xx_frm_init);
::set_form_term (form, _nc_xx_frm_term);
::set_field_init (form, _nc_xx_fld_init);
::set_field_term (form, _nc_xx_fld_term);
2002-10-13 11:35:53 +08:00
1998-03-01 12:21:12 +08:00
scale(mrows, mcols);
::set_form_win(form, w);
2002-10-13 11:35:53 +08:00
1998-03-01 12:21:12 +08:00
if (with_frame) {
if ((mrows > height()-2) || (mcols > width()-2))
2002-10-13 11:35:53 +08:00
OnError(E_NO_ROOM);
1998-03-01 12:21:12 +08:00
sub = new NCursesWindow(*this,mrows,mcols,1,1,'r');
::set_form_sub(form, sub->w);
b_sub_owner = TRUE;
}
else {
2005-10-10 02:41:57 +08:00
sub = static_cast<NCursesWindow*>(0);
1998-03-01 12:21:12 +08:00
b_sub_owner = FALSE;
}
options_on(O_NL_OVERLOAD);
setDefaultAttributes();
}
2005-10-10 02:41:57 +08:00
NCursesForm::~NCursesForm()
{
UserHook* hook = reinterpret_cast<UserHook*>(::form_userptr(form));
1998-03-01 12:21:12 +08:00
delete hook;
if (b_sub_owner) {
delete sub;
2005-10-10 02:41:57 +08:00
::set_form_sub(form, static_cast<WINDOW *>(0));
1998-03-01 12:21:12 +08:00
}
if (form) {
FIELD** fields = ::form_fields(form);
int cnt = count();
2005-10-10 02:41:57 +08:00
OnError(::set_form_fields(form, static_cast<FIELD**>(0)));
1998-03-01 12:21:12 +08:00
if (b_autoDelete) {
if (cnt>0) {
for (int i=0; i <= cnt; i++)
2002-10-13 11:35:53 +08:00
delete my_fields[i];
1998-03-01 12:21:12 +08:00
}
delete[] my_fields;
}
::free_form(form);
// It's essential to do this after free_form()
2002-10-13 11:35:53 +08:00
delete[] fields;
1998-03-01 12:21:12 +08:00
}
}
void
2005-10-10 02:41:57 +08:00
NCursesForm::setSubWindow(NCursesWindow& nsub)
{
1998-03-01 12:21:12 +08:00
if (!isDescendant(nsub))
OnError(E_SYSTEM_ERROR);
else {
if (b_sub_owner)
delete sub;
sub = &nsub;
::set_form_sub(form,sub->w);
}
}
/* Internal hook functions. They will route the hook
* calls to virtual methods of the NCursesForm class,
2002-10-13 11:35:53 +08:00
* so in C++ providing a hook is done simply by
1998-03-01 12:21:12 +08:00
* implementing a virtual method in a derived class
*/
void
2005-10-10 02:41:57 +08:00
_nc_xx_frm_init(FORM *f)
{
NCursesForm::getHook(f)->On_Form_Init();
1998-03-01 12:21:12 +08:00
}
void
2005-10-10 02:41:57 +08:00
_nc_xx_frm_term(FORM *f)
{
NCursesForm::getHook(f)->On_Form_Termination();
1998-03-01 12:21:12 +08:00
}
void
2005-10-10 02:41:57 +08:00
_nc_xx_fld_init(FORM *f)
{
NCursesForm* F = NCursesForm::getHook(f);
1998-03-01 12:21:12 +08:00
F->On_Field_Init (*(F->current_field ()));
}
void
2005-10-10 02:41:57 +08:00
_nc_xx_fld_term(FORM *f)
{
NCursesForm* F = NCursesForm::getHook(f);
1998-03-01 12:21:12 +08:00
F->On_Field_Termination (*(F->current_field ()));
}
void
2005-10-10 02:41:57 +08:00
NCursesForm::On_Form_Init()
{
1998-03-01 12:21:12 +08:00
}
void
2005-10-10 02:41:57 +08:00
NCursesForm::On_Form_Termination()
{
1998-03-01 12:21:12 +08:00
}
void
2005-10-10 02:41:57 +08:00
NCursesForm::On_Field_Init(NCursesFormField& field)
{
1998-03-01 12:21:12 +08:00
}
void
2005-10-10 02:41:57 +08:00
NCursesForm::On_Field_Termination(NCursesFormField& field)
{
1998-03-01 12:21:12 +08:00
}
// call the form driver and do basic error checking.
2002-10-13 11:35:53 +08:00
int
2005-10-10 02:41:57 +08:00
NCursesForm::driver (int c)
{
1998-03-01 12:21:12 +08:00
int res = ::form_driver (form, c);
switch (res) {
case E_OK:
case E_REQUEST_DENIED:
case E_INVALID_FIELD:
case E_UNKNOWN_COMMAND:
break;
default:
OnError (res);
}
return (res);
}
2005-10-10 02:41:57 +08:00
void NCursesForm::On_Request_Denied(int c) const
{
2002-10-13 11:35:53 +08:00
::beep();
1998-03-01 12:21:12 +08:00
}
2005-10-10 02:41:57 +08:00
void NCursesForm::On_Invalid_Field(int c) const
{
2002-10-13 11:35:53 +08:00
::beep();
1998-03-01 12:21:12 +08:00
}
2005-10-10 02:41:57 +08:00
void NCursesForm::On_Unknown_Command(int c) const
{
2002-10-13 11:35:53 +08:00
::beep();
1998-03-01 12:21:12 +08:00
}
static const int CMD_QUIT = MAX_COMMAND + 1;
NCursesFormField*
2005-10-10 02:41:57 +08:00
NCursesForm::operator()(void)
{
1998-03-01 12:21:12 +08:00
int drvCmnd;
int err;
int c;
post();
show();
refresh();
2002-10-13 11:35:53 +08:00
while (((drvCmnd = virtualize((c=getKey()))) != CMD_QUIT)) {
1998-03-01 12:21:12 +08:00
switch((err=driver(drvCmnd))) {
case E_REQUEST_DENIED:
On_Request_Denied(c);
break;
case E_INVALID_FIELD:
On_Invalid_Field(c);
break;
case E_UNKNOWN_COMMAND:
On_Unknown_Command(c);
break;
case E_OK:
break;
default:
OnError(err);
}
}
unpost();
hide();
refresh();
return my_fields[::field_index (::current_field (form))];
}
// Provide a default key virtualization. Translate the keyboard
// code c into a form request code.
// The default implementation provides a hopefully straightforward
// mapping for the most common keystrokes and form requests.
2002-10-13 11:35:53 +08:00
int
2005-10-10 02:41:57 +08:00
NCursesForm::virtualize(int c)
{
1998-03-01 12:21:12 +08:00
switch(c) {
case KEY_HOME : return(REQ_FIRST_FIELD);
case KEY_END : return(REQ_LAST_FIELD);
case KEY_DOWN : return(REQ_DOWN_CHAR);
case KEY_UP : return(REQ_UP_CHAR);
case KEY_LEFT : return(REQ_PREV_CHAR);
case KEY_RIGHT : return(REQ_NEXT_CHAR);
case KEY_NPAGE : return(REQ_NEXT_PAGE);
case KEY_PPAGE : return(REQ_PREV_PAGE);
case KEY_BACKSPACE : return(REQ_DEL_PREV);
case KEY_ENTER : return(REQ_NEW_LINE);
case KEY_CLEAR : return(REQ_CLR_FIELD);
case CTRL('X') : return(CMD_QUIT); // eXit
case CTRL('F') : return(REQ_NEXT_FIELD); // Forward
case CTRL('B') : return(REQ_PREV_FIELD); // Backward
2002-10-13 11:35:53 +08:00
case CTRL('L') : return(REQ_LEFT_FIELD); // Left
1998-03-01 12:21:12 +08:00
case CTRL('R') : return(REQ_RIGHT_FIELD); // Right
case CTRL('U') : return(REQ_UP_FIELD); // Up
case CTRL('D') : return(REQ_DOWN_FIELD); // Down
case CTRL('W') : return(REQ_NEXT_WORD);
case CTRL('T') : return(REQ_PREV_WORD);
case CTRL('A') : return(REQ_BEG_FIELD);
case CTRL('E') : return(REQ_END_FIELD);
case CTRL('I') : return(REQ_INS_CHAR);
case CTRL('M') :
case CTRL('J') : return(REQ_NEW_LINE);
case CTRL('O') : return(REQ_INS_LINE);
case CTRL('V') : return(REQ_DEL_CHAR);
case CTRL('H') : return(REQ_DEL_PREV);
case CTRL('Y') : return(REQ_DEL_LINE);
case CTRL('G') : return(REQ_DEL_WORD);
case CTRL('K') : return(REQ_CLR_EOF);
case CTRL('N') : return(REQ_NEXT_CHOICE);
case CTRL('P') : return(REQ_PREV_CHOICE);
2002-10-13 11:35:53 +08:00
1998-03-01 12:21:12 +08:00
default:
return(c);
}
}
//
// -------------------------------------------------------------------------
// User Defined Fieldtypes
// -------------------------------------------------------------------------
//
2005-10-10 02:41:57 +08:00
bool _nc_xx_fld_fcheck(FIELD *f, const void *u)
{
NCursesFormField* F = reinterpret_cast<NCursesFormField*>(const_cast<void *>(u));
2000-07-09 10:46:08 +08:00
assert(F != 0);
2005-10-10 02:41:57 +08:00
UserDefinedFieldType* udf = reinterpret_cast<UserDefinedFieldType*>(F->fieldtype());
2000-07-09 10:46:08 +08:00
assert(udf != 0);
1998-03-01 12:21:12 +08:00
return udf->field_check(*F);
}
2005-10-10 02:41:57 +08:00
bool _nc_xx_fld_ccheck(int c, const void *u)
{
NCursesFormField* F = reinterpret_cast<NCursesFormField*>(const_cast<void *>(u));
2000-07-09 10:46:08 +08:00
assert(F != 0);
2002-10-13 11:35:53 +08:00
UserDefinedFieldType* udf =
2005-10-10 02:41:57 +08:00
reinterpret_cast<UserDefinedFieldType*>(F->fieldtype());
2000-07-09 10:46:08 +08:00
assert(udf != 0);
1998-03-01 12:21:12 +08:00
return udf->char_check(c);
}
2005-10-10 02:41:57 +08:00
void* _nc_xx_fld_makearg(va_list* va)
{
1998-03-01 12:21:12 +08:00
return va_arg(*va,NCursesFormField*);
}
FIELDTYPE* UserDefinedFieldType::generic_fieldtype =
2005-10-10 02:41:57 +08:00
::new_fieldtype(_nc_xx_fld_fcheck,
_nc_xx_fld_ccheck);
1998-03-01 12:21:12 +08:00
FIELDTYPE* UserDefinedFieldType_With_Choice::generic_fieldtype_with_choice =
2005-10-10 02:41:57 +08:00
::new_fieldtype(_nc_xx_fld_fcheck,
_nc_xx_fld_ccheck);
1998-03-01 12:21:12 +08:00
2005-10-10 02:41:57 +08:00
bool _nc_xx_next_choice(FIELD *f, const void *u)
{
NCursesFormField* F = reinterpret_cast<NCursesFormField*>(const_cast<void *>(u));
2000-07-09 10:46:08 +08:00
assert(F != 0);
2002-10-13 11:35:53 +08:00
UserDefinedFieldType_With_Choice* udf =
2005-10-10 02:41:57 +08:00
reinterpret_cast<UserDefinedFieldType_With_Choice*>(F->fieldtype());
2000-07-09 10:46:08 +08:00
assert(udf != 0);
1998-03-01 12:21:12 +08:00
return udf->next(*F);
}
2005-10-10 02:41:57 +08:00
bool _nc_xx_prev_choice(FIELD *f, const void *u)
{
NCursesFormField* F = reinterpret_cast<NCursesFormField*>(const_cast<void *>(u));
2000-07-09 10:46:08 +08:00
assert(F != 0);
2002-10-13 11:35:53 +08:00
UserDefinedFieldType_With_Choice* udf =
2005-10-10 02:41:57 +08:00
reinterpret_cast<UserDefinedFieldType_With_Choice*>(F->fieldtype());
2000-07-09 10:46:08 +08:00
assert(udf != 0);
1998-03-01 12:21:12 +08:00
return udf->previous(*F);
}
2005-10-10 02:41:57 +08:00
class UDF_Init
{
1998-03-01 12:21:12 +08:00
private:
int code;
static UDF_Init* I;
2005-10-10 02:41:57 +08:00
1998-03-01 12:21:12 +08:00
public:
2005-10-10 02:41:57 +08:00
UDF_Init()
: code(0)
{
1998-03-01 12:21:12 +08:00
code = ::set_fieldtype_arg(UserDefinedFieldType::generic_fieldtype,
2005-10-10 02:41:57 +08:00
_nc_xx_fld_makearg,
1998-03-01 12:21:12 +08:00
NULL,
NULL);
2002-10-13 11:35:53 +08:00
if (code==E_OK)
1998-03-01 12:21:12 +08:00
code = ::set_fieldtype_arg
(UserDefinedFieldType_With_Choice::generic_fieldtype_with_choice,
2005-10-10 02:41:57 +08:00
_nc_xx_fld_makearg,
1998-03-01 12:21:12 +08:00
NULL,
NULL);
if (code==E_OK)
code = ::set_fieldtype_choice
(UserDefinedFieldType_With_Choice::generic_fieldtype_with_choice,
2005-10-10 02:41:57 +08:00
_nc_xx_next_choice,
_nc_xx_prev_choice);
1998-03-01 12:21:12 +08:00
}
};
UDF_Init* UDF_Init::I = new UDF_Init();