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