form_wrap.c (50951B) - raw
1 /*
2 * This is a curses forms wrapper as part of ncurses-ruby
3 * Contributed by Simon Kaczor <skaczor@cox.net>
4 * Prognosoft Inc. <http://www.prognosoft.biz>
5 * Copyright 2004
6 *
7 * Changes:
8 * (C) 2004 Tobias Peters
9 * (C) 2005 2009 Tobias Herzke
10 * (C) 2013 Gaute Hope <eg@gaute.vetsj.com>
11 *
12 * This module is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
16 *
17 * This module is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this module; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Changes
27 * (C) 2011 Tobias Herzke
28 */
29
30
31 #if defined(HAVE_FORM_H) || defined(HAVE_NCURSESW_FORM_H)
32
33 # include <wchar.h>
34
35 #include "form_wrap.h"
36 #include "ncurses_wrap.h"
37 #include "compat.h"
38
39 VALUE mForm;
40 VALUE cFIELD;
41 VALUE cFIELDTYPE;
42 VALUE cFORM;
43
44 void init_err_codes()
45 {
46 /* The routine succeeded. */
47 FORM_DEF_CONST(E_OK);
48 /* The field is already connected to a form. */
49 FORM_DEF_CONST(E_CONNECTED);
50 /* System error occurred (see errno). */
51 FORM_DEF_CONST(E_SYSTEM_ERROR);
52 /* Routine detected an incorrect or out-of-range argument. */
53 FORM_DEF_CONST(E_BAD_ARGUMENT);
54 /* The form is already posted. */
55 FORM_DEF_CONST(E_POSTED);
56 /* Routine was called from an initialization or termination function. */
57 FORM_DEF_CONST(E_BAD_STATE);
58 /* Form is too large for its window. */
59 FORM_DEF_CONST(E_NO_ROOM);
60 /* The form has not been posted. */
61 FORM_DEF_CONST(E_NOT_POSTED);
62 /* The form driver code saw an unknown request code. */
63 FORM_DEF_CONST(E_UNKNOWN_COMMAND);
64 /* Contents of a field are not valid. */
65 FORM_DEF_CONST(E_INVALID_FIELD);
66 /* No fields are connected to the form. */
67 FORM_DEF_CONST(E_NOT_CONNECTED);
68 /* The form driver could not process the request.} */
69 FORM_DEF_CONST(E_REQUEST_DENIED);
70 }
71
72 /*
73 * Form driver request characters listed in form_driver(3x) man page
74 */
75 void init_req_constants() {
76 /* Move to the next page */
77 FORM_DEF_CONST(REQ_NEXT_PAGE);
78 /* Move to the previous page. */
79 FORM_DEF_CONST(REQ_PREV_PAGE);
80 /* Move to the first page. */
81 FORM_DEF_CONST(REQ_FIRST_PAGE);
82 /* Move to the last field. */
83 FORM_DEF_CONST(REQ_LAST_PAGE);
84 /* Move to the next field. */
85 FORM_DEF_CONST(REQ_NEXT_FIELD);
86 /* Move to the previous field. */
87 FORM_DEF_CONST(REQ_PREV_FIELD);
88 /* Move to the first field. */
89 FORM_DEF_CONST(REQ_FIRST_FIELD);
90 /* Move to the last field. */
91 FORM_DEF_CONST(REQ_LAST_FIELD);
92 /* Move to the sorted next field. */
93 FORM_DEF_CONST(REQ_SNEXT_FIELD);
94 /* Move to the sorted previous field. */
95 FORM_DEF_CONST(REQ_SPREV_FIELD);
96 /* Move to the sorted first field. */
97 FORM_DEF_CONST(REQ_SFIRST_FIELD);
98 /* Move to the sorted last field. */
99 FORM_DEF_CONST(REQ_SLAST_FIELD);
100 /* Move left to a field. */
101 FORM_DEF_CONST(REQ_LEFT_FIELD);
102 /* Move right to a field. */
103 FORM_DEF_CONST(REQ_RIGHT_FIELD);
104 /* Move up to a field. */
105 FORM_DEF_CONST(REQ_UP_FIELD);
106 /* Move down to a field. */
107 FORM_DEF_CONST(REQ_DOWN_FIELD);
108 /* Move to the next char. */
109 FORM_DEF_CONST(REQ_NEXT_CHAR);
110 /* Move to the previous char. */
111 FORM_DEF_CONST(REQ_PREV_CHAR);
112 /* Move to the next line. */
113 FORM_DEF_CONST(REQ_NEXT_LINE);
114 /* Move to the previous line. */
115 FORM_DEF_CONST(REQ_PREV_LINE);
116 /* Move to the next word. */
117 FORM_DEF_CONST(REQ_NEXT_WORD);
118 /* Move to the previous word. */
119 FORM_DEF_CONST(REQ_PREV_WORD);
120 /* Move to the beginning of the field. */
121 FORM_DEF_CONST(REQ_BEG_FIELD);
122 /* Move to the end of the field. */
123 FORM_DEF_CONST(REQ_END_FIELD);
124 /* Move to the beginning of the line. */
125 FORM_DEF_CONST(REQ_BEG_LINE);
126 /* Move to the end of the line. */
127 FORM_DEF_CONST(REQ_END_LINE);
128 /* Move left in the field. */
129 FORM_DEF_CONST(REQ_LEFT_CHAR);
130 /* Move right in the field. */
131 FORM_DEF_CONST(REQ_RIGHT_CHAR);
132 /* Move up in the field. */
133 FORM_DEF_CONST(REQ_UP_CHAR);
134 /* Move down in the field. */
135 FORM_DEF_CONST(REQ_DOWN_CHAR);
136 /* Insert or overlay a new line. */
137 FORM_DEF_CONST(REQ_NEW_LINE);
138 /* Insert a blank at the cursor. */
139 FORM_DEF_CONST(REQ_INS_CHAR);
140 /* Insert a blank line at the cursor. */
141 FORM_DEF_CONST(REQ_INS_LINE);
142 /* Delete character at the cursor. */
143 FORM_DEF_CONST(REQ_DEL_CHAR);
144 /* Delete character before the cursor. */
145 FORM_DEF_CONST(REQ_DEL_PREV);
146 /* Delete line at the cursor. */
147 FORM_DEF_CONST(REQ_DEL_LINE);
148 /* Delete blank-delimited word at the cursor. */
149 FORM_DEF_CONST(REQ_DEL_WORD);
150 /* Clear to end of line from cursor. */
151 FORM_DEF_CONST(REQ_CLR_EOL);
152 /* Clear to end of field from cursor. */
153 FORM_DEF_CONST(REQ_CLR_EOF);
154 /* Clear the entire field. */
155 FORM_DEF_CONST(REQ_CLR_FIELD);
156 /* Enter overlay mode. */
157 FORM_DEF_CONST(REQ_OVL_MODE);
158 /* Enter insert mode. */
159 FORM_DEF_CONST(REQ_INS_MODE);
160 /* Scroll the field forward a line. */
161 FORM_DEF_CONST(REQ_SCR_FLINE);
162 /* Scroll the field backward a line. */
163 FORM_DEF_CONST(REQ_SCR_BLINE);
164 /* Scroll the field forward a page. */
165 FORM_DEF_CONST(REQ_SCR_FPAGE);
166 /* Scroll the field backward a page. */
167 FORM_DEF_CONST(REQ_SCR_BPAGE);
168 /* Scroll the field forward half a page. */
169 FORM_DEF_CONST(REQ_SCR_FHPAGE);
170 /* Scroll the field backward half a page. */
171 FORM_DEF_CONST(REQ_SCR_BHPAGE);
172 /* Scroll the field forward a character. */
173 FORM_DEF_CONST(REQ_SCR_FCHAR);
174 /* Scroll the field backward a character. */
175 FORM_DEF_CONST(REQ_SCR_BCHAR);
176 /* Horizontal scroll the field forward a line. */
177 FORM_DEF_CONST(REQ_SCR_HFLINE);
178 /* Horizontal scroll the field backward a line. */
179 FORM_DEF_CONST(REQ_SCR_HBLINE);
180 /* Horizontal scroll the field forward half a line. */
181 FORM_DEF_CONST(REQ_SCR_HFHALF);
182 /* Horizontal scroll the field backward half a line. */
183 FORM_DEF_CONST(REQ_SCR_HBHALF);
184 /* Validate field. */
185 FORM_DEF_CONST(REQ_VALIDATION);
186 /* Display next field choice. */
187 FORM_DEF_CONST(REQ_NEXT_CHOICE);
188 /* Display previous field choice. */
189 FORM_DEF_CONST(REQ_PREV_CHOICE);
190 }
191
192 /*
193 * field justification constants
194 */
195 void init_just_constants() {
196 FORM_DEF_CONST(NO_JUSTIFICATION);
197 FORM_DEF_CONST(JUSTIFY_RIGHT);
198 FORM_DEF_CONST(JUSTIFY_LEFT);
199 FORM_DEF_CONST(JUSTIFY_CENTER);
200 }
201
202 /*
203 * field options constants
204 */
205 void init_opts_constants() {
206 FORM_DEF_CONST(O_VISIBLE);
207 FORM_DEF_CONST(O_ACTIVE);
208 FORM_DEF_CONST(O_PUBLIC);
209 FORM_DEF_CONST(O_EDIT);
210 FORM_DEF_CONST(O_WRAP);
211 FORM_DEF_CONST(O_BLANK);
212 FORM_DEF_CONST(O_AUTOSKIP);
213 FORM_DEF_CONST(O_NULLOK);
214 FORM_DEF_CONST(O_STATIC);
215 FORM_DEF_CONST(O_PASSOK);
216 }
217
218 void init_form_opts_constants() {
219 FORM_DEF_CONST(O_NL_OVERLOAD);
220 FORM_DEF_CONST(O_BS_OVERLOAD);
221 }
222
223 /*
224 * _PAGE wrapper
225 */
226 /* static VALUE wrap_page(_PAGE* page) */
227 /* { */
228 /* if (page == 0) return Qnil; */
229 /* { */
230 /* VALUE pages_hash = rb_iv_get(mForm, "@pages_hash"); */
231 /* VALUE page_adress = INT2NUM((long)(page)); */
232 /* VALUE rb_page = rb_hash_aref(pages_hash, page_adress); */
233 /* if (rb_page == Qnil) { */
234 /* rb_page = Data_Wrap_Struct(cPAGE, 0, 0, page); */
235 /* rb_iv_set(rb_page, "@destroyed", Qfalse); */
236 /* rb_hash_aset(pages_hash, page_adress, rb_page); */
237 /* } */
238 /* return rb_page; */
239 /* } */
240 /* } */
241 /* static _PAGE* get_page(VALUE rb_page) */
242 /* { */
243 /* _PAGE* page; */
244 /* if (rb_page == Qnil) return 0; */
245 /* if (rb_iv_get(rb_page, "@destroyed") == Qtrue) { */
246 /* rb_raise(rb_eRuntimeError, "Attempt to access a destroyed page"); */
247 /* return 0; */
248 /* } */
249 /* Data_Get_Struct(rb_page, _PAGE, page); */
250 /* return page; */
251 /* } */
252
253 /*
254 * FIELD wrapper
255 */
256 static const struct rb_data_type_struct field_rbtype = {
257 .wrap_struct_name = "Ncurses::Form::FIELDTYPE",
258 };
259 static VALUE wrap_field(FIELD* field)
260 {
261 if (field == 0) return Qnil;
262 {
263 VALUE fields_hash = rb_iv_get(mForm, "@fields_hash");
264 VALUE field_adress = INT2NUM((long)(field));
265 VALUE rb_field = rb_hash_aref(fields_hash, field_adress);
266 if (rb_field == Qnil) {
267 rb_field = TypedData_Wrap_Struct(cFIELD, &field_rbtype, field);
268 rb_iv_set(rb_field, "@destroyed", Qfalse);
269 rb_hash_aset(fields_hash, field_adress, rb_field);
270 }
271 return rb_field;
272 }
273 }
274 static FIELD* get_field(VALUE rb_field)
275 {
276 FIELD* field;
277 if (rb_field == Qnil) return 0;
278 if (rb_iv_get(rb_field, "@destroyed") == Qtrue) {
279 rb_raise(rb_eRuntimeError, "Attempt to access a destroyed field");
280 return 0;
281 }
282 TypedData_Get_Struct(rb_field, FIELD, &field_rbtype, field);
283 return field;
284 }
285
286 /*
287 * FIELDTYPE wrapper
288 */
289 static const struct rb_data_type_struct fieldtype_rbtype = {
290 .wrap_struct_name = "Ncurses::Form::FIELDTYPE",
291 };
292 static VALUE wrap_fieldtype(FIELDTYPE* fieldtype)
293 {
294 if (fieldtype == NULL) return Qnil;
295 {
296 VALUE fieldtypes_hash = rb_iv_get(mForm, "@fieldtypes_hash");
297 VALUE fieldtype_adress = INT2NUM((long)(fieldtype));
298 VALUE rb_fieldtype = rb_hash_aref(fieldtypes_hash, fieldtype_adress);
299 if (rb_fieldtype == Qnil) {
300 rb_fieldtype = TypedData_Wrap_Struct(cFIELDTYPE, &fieldtype_rbtype, fieldtype);
301 rb_iv_set(rb_fieldtype, "@destroyed", Qfalse);
302 rb_hash_aset(fieldtypes_hash, fieldtype_adress, rb_fieldtype);
303 }
304 return rb_fieldtype;
305 }
306 }
307 static FIELDTYPE* get_fieldtype(VALUE rb_fieldtype)
308 {
309 FIELDTYPE* fieldtype;
310 if (rb_fieldtype == Qnil) return 0;
311 if (rb_iv_get(rb_fieldtype, "@destroyed") == Qtrue) {
312 rb_raise(rb_eRuntimeError, "Attempt to access a destroyed fieldtype");
313 return 0;
314 }
315 TypedData_Get_Struct(rb_fieldtype, FIELDTYPE, &fieldtype_rbtype, fieldtype);
316 return fieldtype;
317 }
318
319 /*
320 * FORM wrapper
321 */
322 static const struct rb_data_type_struct form_rbtype = {
323 .wrap_struct_name = "Ncurses::Form::FORM",
324 };
325 static VALUE wrap_form(FORM* form)
326 {
327 if (form == 0) return Qnil;
328 {
329 VALUE forms_hash = rb_iv_get(mForm, "@forms_hash");
330 VALUE form_adress = INT2NUM((long)(form));
331 VALUE rb_form = rb_hash_aref(forms_hash, form_adress);
332 if (rb_form == Qnil) {
333 rb_form = TypedData_Wrap_Struct(cFORM, &form_rbtype, form);
334 rb_iv_set(rb_form, "@destroyed", Qfalse);
335 rb_hash_aset(forms_hash, form_adress, rb_form);
336 }
337 return rb_form;
338 }
339 }
340 static FORM* get_form(VALUE rb_form)
341 {
342 FORM* form;
343 if (rb_form == Qnil) return 0;
344 if (rb_iv_get(rb_form, "@destroyed") == Qtrue) {
345 rb_raise(rb_eRuntimeError, "Attempt to access a destroyed form");
346 return 0;
347 }
348 TypedData_Get_Struct(rb_form, FORM, &form_rbtype, form);
349 return form;
350 }
351
352 /*
353 * Proc objects are registered using hashes (one for each type of hook)
354 * The key in the hash is the address of the ncurses "object" and the value is
355 * the Proc object.
356 */
357 #define FIELD_INIT_HOOK 0
358 #define FIELD_TERM_HOOK 1
359 #define FORM_INIT_HOOK 2
360 #define FORM_TERM_HOOK 3
361 #define FIELDTYPE_FIELD_CHECK_HOOK 4
362 #define FIELDTYPE_CHAR_CHECK_HOOK 5
363 #define FIELDTYPE_NEXT_CHOICE_HOOK 6
364 #define FIELDTYPE_PREV_CHOICE_HOOK 7
365 #define FIELDTYPE_ARGS 8
366 #define PROC_HASHES_COUNT 9
367 static VALUE get_proc_hash(int hook) {
368 VALUE arr = rb_iv_get(mForm, "@proc_hashes");
369 VALUE hash = rb_ary_entry(arr, (long)hook);
370 if (hash == Qnil) {
371 rb_raise(rb_eRuntimeError, "Invalid proc hash.");
372 }
373 return hash;
374 }
375
376 /*
377 * Returns an existing Ruby Proc for a given owning "object" and hook type.
378 * Qnil will be returned if no Proc was associated with the owner
379 */
380 static VALUE get_proc(void* owner, int hook) {
381 if (owner == 0) return Qnil;
382 {
383 VALUE owner_adress = INT2NUM((long)(owner));
384 VALUE proc_hash = get_proc_hash(hook);
385 VALUE proc = rb_hash_aref(proc_hash, owner_adress);
386 return proc;
387 }
388 }
389 /*
390 * Registers the Proc object with a given owner "object" and hook type.
391 * If proc is Qnil, the hook is unregistered instead.
392 */
393 static void reg_proc(void* owner, int hook, VALUE proc) {
394 if (owner == NULL) return;
395 {
396 VALUE proc_hash = get_proc_hash(hook);
397 VALUE owner_address = INT2NUM((long)(owner));
398 if (proc == Qnil) {
399 rb_hash_delete(proc_hash, owner_address);
400 }
401 else {
402 rb_hash_aset(proc_hash, owner_address, proc);
403 }
404 }
405 }
406
407 /*
408 * Form creation/destruction functions
409 */
410 static VALUE rbncurs_m_new_form(VALUE dummy, VALUE rb_field_array)
411 {
412 long n = rbncurs_array_length(rb_field_array);
413 /* Will ncurses free this array? If not, must do it after calling free_form(). */
414 FIELD** fields = ALLOC_N(FIELD*, (n+1));
415 long i;
416 for (i=0; i<n; i++){
417 fields[i] = get_field(rb_ary_entry(rb_field_array, i));
418 }
419 fields[n] = NULL;
420 return wrap_form(new_form(fields));
421 }
422
423 static VALUE rbncurs_c_free_form(VALUE rb_form) {
424 VALUE forms_hash = rb_iv_get(mForm, "@forms_hash");
425 FORM* form = get_form(rb_form);
426 VALUE form_adress = INT2NUM((long)(form));
427 rb_funcall(forms_hash, rb_intern("delete"), 1, form_adress);
428 rb_iv_set(rb_form, "@destroyed", Qtrue);
429 return INT2NUM(free_form(form));
430 }
431 static VALUE rbncurs_m_free_form(VALUE dummy, VALUE rb_form)
432 { return rbncurs_c_free_form(rb_form); }
433
434 /*
435 * form_post
436 */
437 static VALUE rbncurs_c_post_form(VALUE rb_form) {
438 FORM* form = get_form(rb_form);
439 return INT2NUM(post_form(form));
440 }
441 static VALUE rbncurs_m_post_form(VALUE dummy, VALUE rb_form)
442 { return rbncurs_c_post_form(rb_form); }
443
444 static VALUE rbncurs_c_unpost_form(VALUE rb_form) {
445 FORM* form = get_form(rb_form);
446 return INT2NUM(unpost_form(form));
447 }
448 static VALUE rbncurs_m_unpost_form(VALUE dummy, VALUE rb_form)
449 { return rbncurs_c_unpost_form(rb_form); }
450
451 /*
452 * Form driver
453 */
454 static VALUE rbncurs_c_form_driver(VALUE rb_form, VALUE c) {
455 FORM* form = get_form(rb_form);
456 return INT2NUM(form_driver(form, NUM2INT(c)));
457 }
458 static VALUE rbncurs_m_form_driver(VALUE dummy, VALUE rb_form, VALUE c)
459 { return rbncurs_c_form_driver(rb_form, c); }
460
461 # ifdef HAVE_FORM_DRIVER_W
462 /* Form driver W */
463 static VALUE rbncurs_c_form_driver_w(VALUE rb_form, VALUE type, VALUE c) {
464 FORM* form = get_form(rb_form);
465 return INT2NUM(form_driver_w(form, NUM2INT(type), NUM2INT(c)));
466 }
467 static VALUE rbncurs_m_form_driver_w(VALUE dummy, VALUE rb_form, VALUE type, VALUE c)
468 { return rbncurs_c_form_driver_w(rb_form, type, c); }
469 # endif
470
471 /*
472 * form_page(3x)
473 */
474 static VALUE rbncurs_c_set_current_field(VALUE rb_form, VALUE rb_field) {
475 FORM* form = get_form(rb_form);
476 FIELD* field = get_field(rb_field);
477 return INT2NUM(set_current_field(form, field));
478 }
479 static VALUE rbncurs_m_set_current_field(VALUE dummy, VALUE rb_form, VALUE rb_field)
480 { return rbncurs_c_set_current_field(rb_form, rb_field); }
481
482 static VALUE rbncurs_c_current_field(VALUE rb_form) {
483 FORM* form = get_form(rb_form);
484 return wrap_field(current_field(form));
485 }
486 static VALUE rbncurs_m_current_field(VALUE dummy, VALUE rb_form)
487 { return rbncurs_c_current_field(rb_form); }
488
489 static VALUE rbncurs_c_set_form_page(VALUE rb_form, VALUE n) {
490 FORM* form = get_form(rb_form);
491 return INT2NUM(set_form_page(form, NUM2INT(n)));
492 }
493 static VALUE rbncurs_m_set_form_page(VALUE dummy, VALUE rb_form, VALUE n)
494 { return rbncurs_c_set_form_page(rb_form, n); }
495
496 static VALUE rbncurs_c_form_page(VALUE rb_form) {
497 FORM* form = get_form(rb_form);
498 return INT2NUM(form_page(form));
499 }
500 static VALUE rbncurs_m_form_page(VALUE dummy, VALUE rb_form)
501 { return rbncurs_c_form_page(rb_form); }
502
503 static VALUE rbncurs_c_field_index(VALUE rb_field) {
504 FIELD* field = get_field(rb_field);
505 return INT2NUM(field_index(field));
506 }
507 static VALUE rbncurs_m_field_index(VALUE dummy, VALUE rb_field)
508 { return rbncurs_c_field_index(rb_field); }
509
510 /*
511 * form_data(3x)
512 */
513 static VALUE rbncurs_c_data_ahead(VALUE rb_form) {
514 FORM* form = get_form(rb_form);
515 return (data_ahead(form)) ? Qtrue: Qfalse;
516 }
517 static VALUE rbncurs_m_data_ahead(VALUE dummy, VALUE rb_form)
518 { return rbncurs_c_data_ahead(rb_form); }
519
520 static VALUE rbncurs_c_data_behind(VALUE rb_form) {
521 FORM* form = get_form(rb_form);
522 return (data_behind(form)) ? Qtrue: Qfalse;
523 }
524 static VALUE rbncurs_m_data_behind(VALUE dummy, VALUE rb_form)
525 { return rbncurs_c_data_behind(rb_form); }
526
527
528
529 /*
530 * Field functions (form_field_new(3x))
531 */
532 static VALUE rbncurs_m_new_field(VALUE dummy,
533 VALUE height, VALUE width,
534 VALUE toprow, VALUE leftcol,
535 VALUE offscreen, VALUE nbuffers) {
536 return wrap_field(new_field(NUM2INT(height), NUM2INT(width),
537 NUM2INT(toprow), NUM2INT(leftcol),
538 NUM2INT(offscreen), NUM2INT(nbuffers)));
539 }
540
541 static VALUE rbncurs_c_dup_field(VALUE rb_field, VALUE toprow, VALUE leftcol) {
542 FIELD* field = get_field(rb_field);
543 return wrap_field(dup_field(field, NUM2INT(toprow),NUM2INT(leftcol)));
544 }
545 static VALUE rbncurs_m_dup_field(VALUE dummy, VALUE rb_field, VALUE toprow, VALUE leftcol)
546 { return rbncurs_c_dup_field(rb_field, toprow, leftcol); }
547
548 static VALUE rbncurs_c_link_field(VALUE rb_field, VALUE toprow, VALUE leftcol) {
549 FIELD* field = get_field(rb_field);
550 return wrap_field(link_field(field, NUM2INT(toprow),NUM2INT(leftcol)));
551 }
552 static VALUE rbncurs_m_link_field(VALUE dummy, VALUE rb_field, VALUE toprow, VALUE leftcol)
553 { return rbncurs_c_link_field(rb_field, toprow, leftcol); }
554
555 static VALUE rbncurs_c_free_field(VALUE rb_field) {
556 VALUE fields_hash = rb_iv_get(mForm, "@fields_hash");
557 FIELD* field = get_field(rb_field);
558 VALUE field_adress = INT2NUM((long)(field));
559 rb_funcall(fields_hash, rb_intern("delete"), 1, field_adress);
560 rb_iv_set(rb_field, "@destroyed", Qtrue);
561 return INT2NUM(free_field(field));
562 }
563 static VALUE rbncurs_m_free_field(VALUE dummy, VALUE rb_field)
564 { return rbncurs_c_free_field(rb_field); }
565
566
567 /*
568 * form_field_info(3x)
569 */
570 static VALUE rbncurs_c_field_info(VALUE rb_field, VALUE rows, VALUE cols,
571 VALUE frow, VALUE fcol, VALUE nrow, VALUE nbuf) {
572 if (rb_obj_is_instance_of(rows, rb_cArray) != Qtrue
573 || rb_obj_is_instance_of(cols, rb_cArray) != Qtrue
574 || rb_obj_is_instance_of(frow, rb_cArray) != Qtrue
575 || rb_obj_is_instance_of(fcol, rb_cArray) != Qtrue
576 || rb_obj_is_instance_of(nrow, rb_cArray) != Qtrue
577 || rb_obj_is_instance_of(nbuf, rb_cArray) != Qtrue) {
578 rb_raise(rb_eArgError,
579 "rows, cols, frow, fcol, nrow and nbuf arguments must be empty Arrays");
580 return Qnil;
581 }
582 else {
583 FIELD* field = get_field(rb_field);
584 int vals[6] = {0,0,0,0,0,0};
585
586 int result = field_info(field, &vals[0],&vals[1],&vals[2],&vals[3],&vals[4],&vals[5]);
587 rb_ary_push(rows, INT2NUM(vals[0]));
588 rb_ary_push(cols, INT2NUM(vals[1]));
589 rb_ary_push(frow, INT2NUM(vals[2]));
590 rb_ary_push(fcol, INT2NUM(vals[3]));
591 rb_ary_push(nrow, INT2NUM(vals[4]));
592 rb_ary_push(nbuf, INT2NUM(vals[5]));
593 return INT2NUM(result);
594 }
595 }
596 static VALUE rbncurs_m_field_info(VALUE dummy, VALUE rb_field, VALUE rows, VALUE cols,
597 VALUE frow, VALUE fcol, VALUE nrow, VALUE nbuf)
598 { return rbncurs_c_field_info(rb_field, rows, cols, frow, fcol, nrow, nbuf); }
599
600 static VALUE rbncurs_c_dynamic_field_info(VALUE rb_field, VALUE rows, VALUE cols,
601 VALUE max) {
602 if (rb_obj_is_instance_of(rows, rb_cArray) != Qtrue
603 || rb_obj_is_instance_of(cols, rb_cArray) != Qtrue
604 || rb_obj_is_instance_of(max, rb_cArray) != Qtrue) {
605 rb_raise(rb_eArgError,
606 "rows, cols and max arguments must be empty Arrays");
607 return Qnil;
608 }
609 else {
610 FIELD* field = get_field(rb_field);
611 int vals[3] = {0,0,0};
612
613 int result = dynamic_field_info(field, &vals[0],&vals[1],&vals[2]);
614 rb_ary_push(rows, INT2NUM(vals[0]));
615 rb_ary_push(cols, INT2NUM(vals[1]));
616 rb_ary_push(max, INT2NUM(vals[2]));
617 return INT2NUM(result);
618 }
619 }
620 static VALUE rbncurs_m_dynamic_field_info(VALUE dummy, VALUE rb_field, VALUE rows, VALUE cols,
621 VALUE max)
622 { return rbncurs_c_dynamic_field_info(rb_field, rows, cols, max); }
623 /*
624 * field_validation
625 */
626 static VALUE rbncurs_c_set_field_type(int argc, VALUE* argv, VALUE rb_field) {
627 VALUE rb_fieldtype, arg3, arg4, arg5;
628 FIELD* field = get_field(rb_field);
629 FIELDTYPE* ftype = NULL;
630
631 rb_scan_args(argc, argv, "13", &rb_fieldtype, &arg3, &arg4, &arg5);
632 ftype = get_fieldtype(rb_fieldtype);
633
634 if (ftype == TYPE_ALNUM ||
635 ftype == TYPE_ALPHA) {
636 if (argc != 2)
637 rb_raise(rb_eArgError, "TYPE_ALNUM and TYPE_ALPHA require one additional argument");
638 return INT2NUM(set_field_type(field, ftype,
639 NUM2INT(arg3)));
640 }
641 if (ftype == TYPE_ENUM) {
642 if (argc != 4) {
643 rb_raise(rb_eArgError, "TYPE_ENUM requires three additional arguments");
644 }
645 else {
646 int n = (int) rbncurs_array_length(arg3);
647 /* Will ncurses free this array of strings in free_field()? */
648 char** list = ALLOC_N(char*, n+1);
649 int i;
650 for (i = 0; i < n; i++) {
651 list[i] = STR2CSTR(rb_ary_entry(arg3, (long)i));
652 }
653 list[n] = NULL;
654 return INT2NUM(set_field_type(field, ftype,
655 list,
656 RTEST(arg4),
657 RTEST(arg5)));
658 }
659 }
660 else if (ftype == TYPE_INTEGER) {
661 if (argc != 4)
662 rb_raise(rb_eArgError, "TYPE_INTEGER requires three additional arguments");
663 return INT2NUM(set_field_type(field, ftype,
664 NUM2INT(arg3),
665 NUM2LONG(arg4),
666 NUM2LONG(arg5)));
667 }
668 else if (ftype == TYPE_NUMERIC) {
669 if (argc != 4)
670 rb_raise(rb_eArgError, "TYPE_NUMERIC requires three additional arguments");
671 return INT2NUM(set_field_type(field, ftype,
672 NUM2INT(arg3),
673 NUM2DBL(arg4),
674 NUM2DBL(arg5)));
675 }
676 else if (ftype == TYPE_REGEXP){
677 if (argc != 2)
678 rb_raise(rb_eArgError, "TYPE_REGEXP requires one additional argument");
679 return INT2NUM(set_field_type(field, ftype,
680 STR2CSTR(arg3)));
681 }
682 else if (ftype == TYPE_IPV4){
683 if (argc != 1)
684 rb_raise(rb_eArgError, "TYPE_IPV4 has no additional arguments");
685 return INT2NUM(set_field_type(field, ftype));
686 }
687 else {
688 /* It is a user-defined field type. */
689 /* Will store the arguments associated with this field */
690 /* for use in the callback function. */
691 VALUE rest;
692 rb_scan_args(argc, argv, "1*", &rb_fieldtype, &rest);
693 reg_proc(field, FIELDTYPE_ARGS, rest);
694 /* Pass field as an optional parameter so that make_arg can create */
695 /* the block-argument used in finding the appropriate Ruby Proc */
696 return INT2NUM(set_field_type(field, ftype, field));
697 }
698
699 }
700 static VALUE rbncurs_m_set_field_type(int argc, VALUE* argv, VALUE dummy)
701 { return rbncurs_c_set_field_type(argc-1, argv+1, argv[0]); }
702
703 static VALUE rbncurs_c_field_type(VALUE rb_field) {
704 FIELD* field = get_field(rb_field);
705 return wrap_fieldtype(field_type(field));
706 }
707 static VALUE rbncurs_m_field_type(VALUE dummy, VALUE rb_field)
708 { return rbncurs_c_field_type(rb_field); }
709 /* What is this function doing??? */
710 static VALUE rbncurs_c_field_arg(VALUE rb_field) {
711 FIELD* field = get_field(rb_field);
712 field_arg(field);
713 return Qfalse;
714 }
715 static VALUE rbncurs_m_field_arg(VALUE dummy, VALUE rb_field)
716 { return rbncurs_c_field_arg(rb_field); }
717
718 /*
719 * field_attributes
720 */
721 static VALUE rbncurs_c_set_field_fore(VALUE rb_field, VALUE attr) {
722 FIELD* field = get_field(rb_field);
723 return INT2NUM(set_field_fore(field, (int) NUM2ULONG(attr)));
724 }
725 static VALUE rbncurs_m_set_field_fore(VALUE dummy, VALUE rb_field, VALUE attr)
726 { return rbncurs_c_set_field_fore(rb_field, attr); }
727
728 static VALUE rbncurs_c_field_fore(VALUE rb_field) {
729 FIELD* field = get_field(rb_field);
730 return ULONG2NUM(field_fore(field));
731 }
732 static VALUE rbncurs_m_field_fore(VALUE dummy, VALUE rb_field)
733 { return rbncurs_c_field_fore(rb_field); }
734
735 static VALUE rbncurs_c_set_field_back(VALUE rb_field, VALUE attr) {
736 FIELD* field = get_field(rb_field);
737 return INT2NUM(set_field_back(field, (int) NUM2ULONG(attr)));
738 }
739 static VALUE rbncurs_m_set_field_back(VALUE dummy, VALUE rb_field, VALUE attr)
740 { return rbncurs_c_set_field_back(rb_field, attr); }
741
742 static VALUE rbncurs_c_field_back(VALUE rb_field) {
743 FIELD* field = get_field(rb_field);
744 return ULONG2NUM(field_back(field));
745 }
746 static VALUE rbncurs_m_field_back(VALUE dummy, VALUE rb_field)
747 { return rbncurs_c_field_back(rb_field); }
748
749 static VALUE rbncurs_c_set_field_pad(VALUE rb_field, VALUE pad) {
750 FIELD* field = get_field(rb_field);
751 return INT2NUM(set_field_pad(field, NUM2INT(pad)));
752 }
753 static VALUE rbncurs_m_set_field_pad(VALUE dummy, VALUE rb_field, VALUE pad)
754 { return rbncurs_c_set_field_pad(rb_field, pad); }
755
756 static VALUE rbncurs_c_field_pad(VALUE rb_field) {
757 FIELD* field = get_field(rb_field);
758 return INT2NUM(field_pad(field));
759 }
760 static VALUE rbncurs_m_field_pad(VALUE dummy, VALUE rb_field)
761 { return rbncurs_c_field_pad(rb_field); }
762
763 /*
764 * field_buffer
765 */
766 static VALUE rbncurs_c_set_field_buffer(VALUE rb_field, VALUE buf, VALUE value) {
767 FIELD* field = get_field(rb_field);
768 return INT2NUM(set_field_buffer(field, NUM2INT(buf), STR2CSTR(value)));
769 }
770 static VALUE rbncurs_m_set_field_buffer(VALUE dummy, VALUE rb_field, VALUE buf, VALUE value)
771 { return rbncurs_c_set_field_buffer(rb_field, buf, value); }
772
773 static VALUE rbncurs_c_field_buffer(VALUE rb_field, VALUE buffer) {
774 FIELD* field = get_field(rb_field);
775 char * str = field_buffer(field, NUM2INT(buffer));
776 if (str == NULL) return Qnil;
777 return rb_str_new2(str);
778 }
779 static VALUE rbncurs_m_field_buffer(VALUE dummy, VALUE rb_field, VALUE buffer)
780 { return rbncurs_c_field_buffer(rb_field, buffer); }
781
782 static VALUE rbncurs_c_set_field_status(VALUE rb_field, VALUE status) {
783 FIELD* field = get_field(rb_field);
784 return INT2NUM(set_field_status(field, RTEST(status)));
785 }
786 static VALUE rbncurs_m_set_field_status(VALUE dummy, VALUE rb_field, VALUE status)
787 { return rbncurs_c_set_field_status(rb_field, status); }
788
789 static VALUE rbncurs_c_field_status(VALUE rb_field) {
790 FIELD* field = get_field(rb_field);
791 return (field_status(field)) ? Qtrue: Qfalse;
792 }
793 static VALUE rbncurs_m_field_status(VALUE dummy, VALUE rb_field)
794 { return rbncurs_c_field_status(rb_field); }
795
796 static VALUE rbncurs_c_set_max_field(VALUE rb_field, VALUE max) {
797 FIELD* field = get_field(rb_field);
798 return INT2NUM(set_max_field(field, NUM2INT(max)));
799 }
800 static VALUE rbncurs_m_set_max_field(VALUE dummy, VALUE rb_field, VALUE max)
801 { return rbncurs_c_set_max_field(rb_field, max); }
802
803 /*
804 * form_field
805 */
806 static VALUE rbncurs_c_set_form_fields(VALUE rb_form, VALUE rb_field_array) {
807 long n = rbncurs_array_length(rb_field_array);
808 /* If ncurses does not free memory used by the previous array of strings, */
809 /* we will have to do it now. */
810 FIELD** fields = ALLOC_N(FIELD*, (n+1));
811 long i;
812 FORM* form = NULL;
813 for (i=0; i<n; i++){
814 fields[i] = get_field(rb_ary_entry(rb_field_array, i));
815 }
816 fields[n] = NULL;
817 form = get_form(rb_form);
818 return INT2NUM(set_form_fields(form, fields));
819 }
820 static VALUE rbncurs_m_set_form_fields(VALUE dummy, VALUE rb_form, VALUE rb_field_array)
821 { return rbncurs_c_set_form_fields(rb_form, rb_field_array); }
822
823 static VALUE rbncurs_c_form_fields(VALUE rb_form) {
824 FORM* form = get_form(rb_form);
825 FIELD** fields = form_fields(form);
826 VALUE arr = Qundef;
827 int i;
828 if (fields == NULL)
829 rb_raise(rb_eRuntimeError, "Error retrieving form fields");
830 arr = rb_ary_new();
831 i=0;
832 while (fields[i] != NULL)
833 {
834 rb_ary_push(arr, wrap_field(fields[i++]));
835 }
836 return arr;
837 }
838 static VALUE rbncurs_m_form_fields(VALUE dummy, VALUE rb_form)
839 { return rbncurs_c_form_fields(rb_form); }
840
841 static VALUE rbncurs_c_field_count(VALUE rb_form) {
842 FORM* form = get_form(rb_form);
843 return INT2NUM(field_count(form));
844 }
845 static VALUE rbncurs_m_field_count(VALUE dummy, VALUE rb_form)
846 { return rbncurs_c_field_count(rb_form); }
847
848 static VALUE rbncurs_c_move_field(VALUE rb_field, VALUE frow, VALUE fcol) {
849 FIELD* field = get_field(rb_field);
850 return INT2NUM(move_field(field, NUM2INT(frow), NUM2INT(fcol)));
851 }
852 static VALUE rbncurs_m_move_field(VALUE dummy, VALUE rb_field, VALUE frow, VALUE fcol)
853 { return rbncurs_c_move_field(rb_field, frow, fcol); }
854
855
856 /*
857 * form_hook
858 */
859 static void field_init_hook(FORM* form) {
860 /* Find the Proc object associated with this form */
861 VALUE proc = get_proc(form, FIELD_INIT_HOOK);
862 if (proc != Qnil) {
863 VALUE rb_form = wrap_form(form);
864 rb_funcall(proc, rb_intern("call"), 1, rb_form);
865 }
866 }
867 static VALUE rbncurs_c_set_field_init(VALUE rb_form, VALUE proc) {
868 FORM* form = NULL;
869 if (!rb_obj_is_kind_of(rb_form, cFORM))
870 rb_raise(rb_eArgError, "arg1 must be a FORM object");
871 if (!rb_obj_is_kind_of(proc, rb_cProc))
872 rb_raise(rb_eArgError, "arg2 must be a Proc object");
873 form = get_form(rb_form);
874 reg_proc(form, FIELD_INIT_HOOK, proc);
875 if (proc != Qnil) {
876 return INT2NUM(set_field_init(form, field_init_hook));
877 }
878 else {
879 return INT2NUM(set_field_init(form, NULL));
880 }
881 }
882 static VALUE rbncurs_m_set_field_init(VALUE dummy, VALUE rb_form, VALUE proc)
883 { return rbncurs_c_set_field_init(rb_form, proc); }
884
885 static VALUE rbncurs_c_field_init(VALUE rb_form) {
886 FORM* form = get_form(rb_form);
887 return get_proc(form, FIELD_INIT_HOOK);
888 }
889 static VALUE rbncurs_m_field_init(VALUE dummy, VALUE rb_form)
890 { return rbncurs_c_field_init(rb_form); }
891
892 static void field_term_hook(FORM* form) {
893 /* Find the Proc object associated with this form */
894 VALUE proc = get_proc(form, FIELD_TERM_HOOK);
895 if (proc != Qnil) {
896 VALUE rb_form = wrap_form(form);
897 rb_funcall(proc, rb_intern("call"), 1, rb_form);
898 }
899 }
900 static VALUE rbncurs_c_set_field_term(VALUE rb_form, VALUE proc) {
901 FORM * form = NULL;
902 if (!rb_obj_is_kind_of(rb_form, cFORM))
903 rb_raise(rb_eArgError, "arg1 must be a FORM object");
904 if (!rb_obj_is_kind_of(proc, rb_cProc))
905 rb_raise(rb_eArgError, "arg2 must be a Proc object");
906 form = get_form(rb_form);
907 reg_proc(form, FIELD_TERM_HOOK, proc);
908 if (proc != Qnil) {
909 return INT2NUM(set_field_term(form, field_term_hook));
910 }
911 else {
912 return INT2NUM(set_field_term(form, NULL));
913 }
914 }
915 static VALUE rbncurs_m_set_field_term(VALUE dummy, VALUE rb_form, VALUE proc)
916 { return rbncurs_c_set_field_term(rb_form, proc); }
917
918 static VALUE rbncurs_c_field_term(VALUE rb_form) {
919 FORM* form = get_form(rb_form);
920 return get_proc(form, FIELD_TERM_HOOK);
921 }
922 static VALUE rbncurs_m_field_term(VALUE dummy, VALUE rb_form)
923 { return rbncurs_c_field_term(rb_form); }
924
925 static void form_init_hook(FORM* form) {
926 /* Find the Proc object associated with this form */
927 VALUE proc = get_proc(form, FORM_INIT_HOOK);
928 if (proc != Qnil) {
929 VALUE rb_form = wrap_form(form);
930 rb_funcall(proc, rb_intern("call"), 1, rb_form);
931 }
932 }
933 static VALUE rbncurs_c_set_form_init(VALUE rb_form, VALUE proc) {
934 FORM * form = NULL;
935 if (!rb_obj_is_kind_of(rb_form, cFORM))
936 rb_raise(rb_eArgError, "arg1 must be a FORM object");
937 if (!rb_obj_is_kind_of(proc, rb_cProc))
938 rb_raise(rb_eArgError, "arg2 must be a Proc object");
939 form = get_form(rb_form);
940 reg_proc(form, FORM_INIT_HOOK, proc);
941 if (proc != Qnil) {
942 return INT2NUM(set_form_init(form, form_init_hook));
943 }
944 else {
945 return INT2NUM(set_form_init(form, NULL));
946 }
947 }
948 static VALUE rbncurs_m_set_form_init(VALUE dummy, VALUE rb_form, VALUE proc)
949 { return rbncurs_c_set_form_init(rb_form, proc); }
950
951 static VALUE rbncurs_c_form_init(VALUE rb_form) {
952 FORM* form = get_form(rb_form);
953 return get_proc(form, FORM_INIT_HOOK);
954 }
955 static VALUE rbncurs_m_form_init(VALUE dummy, VALUE rb_form)
956 { return rbncurs_c_form_init(rb_form); }
957
958 static void form_term_hook(FORM* form) {
959 /* Find the Proc object associated with this form */
960 VALUE proc = get_proc(form, FORM_TERM_HOOK);
961 if (proc != Qnil) {
962 VALUE rb_form = wrap_form(form);
963 rb_funcall(proc, rb_intern("call"), 1, rb_form);
964 }
965 }
966 static VALUE rbncurs_c_set_form_term(VALUE rb_form, VALUE proc) {
967 FORM * form = NULL;
968 if (!rb_obj_is_kind_of(rb_form, cFORM))
969 rb_raise(rb_eArgError, "arg1 must be a FORM object");
970 if (!rb_obj_is_kind_of(proc, rb_cProc))
971 rb_raise(rb_eArgError, "arg2 must be a Proc object");
972 form = get_form(rb_form);
973 reg_proc(form, FORM_TERM_HOOK, proc);
974 if (proc != Qnil) {
975 return INT2NUM(set_form_term(form, form_term_hook));
976 }
977 else {
978 return INT2NUM(set_form_term(form, NULL));
979 }
980 }
981 static VALUE rbncurs_m_set_form_term(VALUE dummy, VALUE rb_form, VALUE proc)
982 { return rbncurs_c_set_form_term(rb_form, proc); }
983
984 static VALUE rbncurs_c_form_term(VALUE rb_form) {
985 FORM* form = get_form(rb_form);
986 return get_proc(form, FORM_TERM_HOOK);
987 }
988 static VALUE rbncurs_m_form_term(VALUE dummy, VALUE rb_form)
989 { return rbncurs_c_form_term(rb_form); }
990
991 /*
992 * form_field_just
993 */
994 static VALUE rbncurs_c_set_field_just(VALUE rb_field, VALUE justification) {
995 FIELD* field = get_field(rb_field);
996 return INT2NUM(set_field_just(field, NUM2INT(justification)));
997 }
998 static VALUE rbncurs_m_set_field_just(VALUE dummy, VALUE rb_field, VALUE justification)
999 { return rbncurs_c_set_field_just(rb_field, justification); }
1000
1001 static VALUE rbncurs_c_field_just(VALUE rb_field) {
1002 FIELD* field = get_field(rb_field);
1003 return INT2NUM(field_just(field));
1004 }
1005 static VALUE rbncurs_m_field_just(VALUE dummy, VALUE rb_field)
1006 { return rbncurs_c_field_just(rb_field); }
1007
1008 /*
1009 * form_field_opts
1010 */
1011 static VALUE rbncurs_c_set_field_opts(VALUE rb_field, VALUE opts) {
1012 FIELD* field = get_field(rb_field);
1013 return INT2NUM(set_field_opts(field, NUM2INT(opts)));
1014 }
1015 static VALUE rbncurs_m_set_field_opts(VALUE dummy, VALUE rb_field, VALUE opts)
1016 { return rbncurs_c_set_field_opts(rb_field, opts); }
1017
1018 static VALUE rbncurs_c_field_opts_on(VALUE rb_field, VALUE opts) {
1019 FIELD* field = get_field(rb_field);
1020 return INT2NUM(field_opts_on(field, NUM2INT(opts)));
1021 }
1022 static VALUE rbncurs_m_field_opts_on(VALUE dummy, VALUE rb_field, VALUE opts)
1023 { return rbncurs_c_field_opts_on(rb_field, opts); }
1024
1025 static VALUE rbncurs_c_field_opts_off(VALUE rb_field, VALUE opts) {
1026 FIELD* field = get_field(rb_field);
1027 return INT2NUM(field_opts_off(field, NUM2INT(opts)));
1028 }
1029 static VALUE rbncurs_m_field_opts_off(VALUE dummy, VALUE rb_field, VALUE opts)
1030 { return rbncurs_c_field_opts_off(rb_field, opts); }
1031
1032 static VALUE rbncurs_c_field_opts(VALUE rb_field) {
1033 FIELD* field = get_field(rb_field);
1034 return INT2NUM(field_opts(field));
1035 }
1036 static VALUE rbncurs_m_field_opts(VALUE dummy, VALUE rb_field)
1037 { return rbncurs_c_field_opts(rb_field); }
1038
1039 /*
1040 * form_opts
1041 */
1042 static VALUE rbncurs_c_set_form_opts(VALUE rb_form, VALUE opts) {
1043 FORM* form = get_form(rb_form);
1044 return INT2NUM(set_form_opts(form, NUM2INT(opts)));
1045 }
1046 static VALUE rbncurs_m_set_form_opts(VALUE dummy, VALUE rb_form, VALUE opts)
1047 { return rbncurs_c_set_form_opts(rb_form, opts); }
1048
1049 static VALUE rbncurs_c_form_opts_on(VALUE rb_form, VALUE opts) {
1050 FORM* form = get_form(rb_form);
1051 return INT2NUM(form_opts_on(form, NUM2INT(opts)));
1052 }
1053 static VALUE rbncurs_m_form_opts_on(VALUE dummy, VALUE rb_form, VALUE opts)
1054 { return rbncurs_c_form_opts_on(rb_form, opts); }
1055
1056 static VALUE rbncurs_c_form_opts_off(VALUE rb_form, VALUE opts) {
1057 FORM* form = get_form(rb_form);
1058 return INT2NUM(form_opts_off(form, NUM2INT(opts)));
1059 }
1060 static VALUE rbncurs_m_form_opts_off(VALUE dummy, VALUE rb_form, VALUE opts)
1061 { return rbncurs_c_form_opts_off(rb_form, opts); }
1062
1063 static VALUE rbncurs_c_form_opts(VALUE rb_form) {
1064 FORM* form = get_form(rb_form);
1065 return INT2NUM(form_opts(form));
1066 }
1067 static VALUE rbncurs_m_form_opts(VALUE dummy, VALUE rb_form)
1068 { return rbncurs_c_form_opts(rb_form); }
1069
1070 /*
1071 * form_requestname
1072 */
1073 static VALUE rbncurs_c_form_request_name(VALUE request) {
1074 char * str = form_request_name(NUM2INT(request));
1075 if (str == NULL) return Qnil;
1076 return rb_str_new2(str);
1077 }
1078 static VALUE rbncurs_m_form_request_name(VALUE dummy, VALUE request)
1079 { return rbncurs_c_form_request_name(request); }
1080
1081 static VALUE rbncurs_c_form_request_by_name(VALUE name) {
1082 return INT2NUM(form_request_by_name(STR2CSTR(name)));
1083 }
1084 static VALUE rbncurs_m_form_request_by_name(VALUE dummy, VALUE name)
1085 { return rbncurs_c_form_request_by_name(name); }
1086
1087 /*
1088 * form_win
1089 */
1090 static VALUE rbncurs_c_set_form_win(VALUE rb_form, VALUE rb_win) {
1091 FORM* form = get_form(rb_form);
1092 WINDOW* win = get_window(rb_win);
1093 return INT2NUM(set_form_win(form, win));
1094 }
1095 static VALUE rbncurs_m_set_form_win(VALUE dummy, VALUE rb_form, VALUE rb_win)
1096 { return rbncurs_c_set_form_win(rb_form, rb_win); }
1097
1098 static VALUE rbncurs_c_form_win(VALUE rb_form) {
1099 FORM* form = get_form(rb_form);
1100 return wrap_window(form_win(form));
1101 }
1102 static VALUE rbncurs_m_form_win(VALUE dummy, VALUE rb_form)
1103 { return rbncurs_c_form_win(rb_form); }
1104
1105 static VALUE rbncurs_c_set_form_sub(VALUE rb_form, VALUE rb_sub) {
1106 FORM* form = get_form(rb_form);
1107 WINDOW* win = get_window(rb_sub);
1108 return INT2NUM(set_form_sub(form, win));
1109 }
1110 static VALUE rbncurs_m_set_form_sub(VALUE dummy, VALUE rb_form, VALUE rb_sub)
1111 { return rbncurs_c_set_form_sub(rb_form, rb_sub); }
1112
1113 static VALUE rbncurs_c_form_sub(VALUE rb_form) {
1114 FORM* form = get_form(rb_form);
1115 return wrap_window(form_sub(form));
1116 }
1117 static VALUE rbncurs_m_form_sub(VALUE dummy, VALUE rb_form)
1118 { return rbncurs_c_form_sub(rb_form); }
1119
1120 static VALUE rbncurs_c_scale_form(VALUE rb_form, VALUE rows, VALUE columns) {
1121 FORM* form = get_form(rb_form);
1122 if (rb_obj_is_instance_of(rows, rb_cArray) != Qtrue
1123 || rb_obj_is_instance_of(columns, rb_cArray) != Qtrue) {
1124 rb_raise(rb_eArgError,
1125 "rows and columns arguments must be empty Arrays");
1126 return Qnil;
1127 }
1128 else {
1129 int vals[2] = {0,0};
1130 int result = scale_form(form, &vals[0],&vals[1]);
1131 rb_ary_push(rows, INT2NUM(vals[0]));
1132 rb_ary_push(columns, INT2NUM(vals[1]));
1133 return INT2NUM(result);
1134 }
1135 }
1136 static VALUE rbncurs_m_scale_form(VALUE dummy, VALUE rb_form, VALUE rows, VALUE columns)
1137 { return rbncurs_c_scale_form(rb_form, rows, columns); }
1138
1139 /*
1140 * form_fieldtype
1141 */
1142 static void* make_arg(va_list* ap) {
1143 /* This method creates a list of arguments to be passed */
1144 /* to the validation functions (char_check and field_check). */
1145 FIELD* field = va_arg(*ap, FIELD*);
1146 FIELDTYPE* fieldtype = field_type(field);
1147 VALUE proc = get_proc(fieldtype, FIELDTYPE_FIELD_CHECK_HOOK);
1148 if (proc == Qnil) {
1149 proc = get_proc(fieldtype, FIELDTYPE_CHAR_CHECK_HOOK);
1150 }
1151
1152 /* Compare number of arguments in Ruby Proc with that of set_field_type */
1153 if (proc != Qnil) {
1154 VALUE argc = rb_funcall(proc, rb_intern("arity"),0);
1155 VALUE args = get_proc(field, FIELDTYPE_ARGS);
1156 if (args != Qnil) {
1157 if (NUM2INT(argc)-1 != rbncurs_array_length(args)) {
1158 char msg[500];
1159 snprintf(msg, 500, "The validation functions for this field type need %d additional arguments.",(int)(NUM2INT(argc)-1));
1160 msg[499]=0;
1161 rb_raise(rb_eArgError, "%s", msg);
1162 }
1163 }
1164 }
1165 /* field will be the only argument in field_check/char_check callback */
1166 /* and will be used to locate the appropriate Ruby Proc */
1167 return field;
1168 }
1169 static bool field_check(FIELD* field, const void* argblock) {
1170 FIELDTYPE* fieldtype = field_type(field);
1171 VALUE proc = get_proc(fieldtype, FIELDTYPE_FIELD_CHECK_HOOK);
1172 if (proc != Qnil)
1173 {
1174 VALUE args = rb_ary_dup(get_proc(field, FIELDTYPE_ARGS));
1175 rb_ary_unshift(args, wrap_field(field));
1176 return RTEST(rb_apply(proc, rb_intern("call"), args));
1177 }
1178 return 1;
1179 }
1180 static bool char_check(int c, const void* argblock) {
1181 FIELD* field = (FIELD*)argblock;
1182 FIELDTYPE* fieldtype = field_type(field);
1183 VALUE proc = get_proc(fieldtype, FIELDTYPE_CHAR_CHECK_HOOK);
1184 if (proc != Qnil) {
1185 VALUE args = rb_ary_dup(get_proc(field, FIELDTYPE_ARGS));
1186 char str[2];
1187 str[0] = c;
1188 str[1] = 0;
1189 rb_ary_unshift(args, rb_str_new2(str));
1190 return RTEST(rb_apply(proc, rb_intern("call"), args));
1191 }
1192 return 1;
1193 }
1194 static VALUE rbncurs_m_new_fieldtype(VALUE dummy, VALUE field_check_proc, VALUE char_check_proc)
1195 {
1196 FIELDTYPE* fieldtype = new_fieldtype(field_check_proc == Qnil ? NULL : field_check,
1197 char_check_proc == Qnil ? NULL : char_check);
1198 set_fieldtype_arg(fieldtype, make_arg, NULL, NULL);
1199 if (field_check_proc != Qnil) {
1200 reg_proc(fieldtype, FIELDTYPE_FIELD_CHECK_HOOK, field_check_proc);
1201 }
1202 if (char_check_proc != Qnil) {
1203 reg_proc(fieldtype, FIELDTYPE_CHAR_CHECK_HOOK, char_check_proc);
1204 }
1205
1206 return wrap_fieldtype(fieldtype);
1207 }
1208
1209 static VALUE rbncurs_c_free_fieldtype(VALUE rb_fieldtype) {
1210 FIELDTYPE* fieldtype = get_fieldtype(rb_fieldtype);
1211 return INT2NUM(free_fieldtype(fieldtype));
1212 }
1213 static VALUE rbncurs_m_free_fieldtype(VALUE dummy, VALUE rb_field)
1214 { return rbncurs_c_free_fieldtype(rb_field); }
1215
1216 static bool next_choice(FIELD* field, const void* argblock) {
1217 FIELDTYPE* fieldtype = field_type(field);
1218 VALUE proc = get_proc(fieldtype, FIELDTYPE_NEXT_CHOICE_HOOK);
1219 if (proc != Qnil) {
1220 /* Need to find out what exactly is argblock about... */
1221 return RTEST(rb_funcall(proc, rb_intern("call"), 1, wrap_field(field)));
1222 }
1223 return 1;
1224 }
1225 static bool prev_choice(FIELD* field, const void* argblock) {
1226 FIELDTYPE* fieldtype = field_type(field);
1227 VALUE proc = get_proc(fieldtype, FIELDTYPE_PREV_CHOICE_HOOK);
1228 if (proc != Qnil) {
1229 /* Need to find out what exactly is argblock about... */
1230 return RTEST(rb_funcall(proc, rb_intern("call"), 1, wrap_field(field)));
1231 }
1232 return 1;
1233 }
1234
1235 static VALUE rbncurs_c_set_fieldtype_choice(VALUE rb_fieldtype, VALUE next_choice_proc, VALUE prev_choice_proc) {
1236 FIELDTYPE* fieldtype = get_fieldtype(rb_fieldtype);
1237 int result = set_fieldtype_choice(fieldtype,
1238 next_choice_proc == Qnil ? NULL : next_choice,
1239 prev_choice_proc == Qnil ? NULL : prev_choice);
1240 if (next_choice_proc != Qnil)
1241 reg_proc(fieldtype, FIELDTYPE_NEXT_CHOICE_HOOK, next_choice_proc);
1242 if (prev_choice_proc != Qnil)
1243 reg_proc(fieldtype, FIELDTYPE_PREV_CHOICE_HOOK, prev_choice_proc);
1244 return INT2NUM(result);
1245 }
1246 static VALUE rbncurs_m_set_fieldtype_choice(VALUE dummy, VALUE rb_fieldtype, VALUE next_choice_proc, VALUE prev_choice_proc)
1247 { return rbncurs_c_set_fieldtype_choice(rb_fieldtype, next_choice_proc, prev_choice_proc); }
1248
1249 static VALUE rbncurs_c_link_fieldtype(VALUE rb_fieldtype1, VALUE rb_fieldtype2) {
1250 FIELDTYPE* fieldtype1 = get_fieldtype(rb_fieldtype1);
1251 FIELDTYPE* fieldtype2 = get_fieldtype(rb_fieldtype2);
1252 return wrap_fieldtype(link_fieldtype(fieldtype1, fieldtype2));
1253 }
1254 static VALUE rbncurs_m_link_fieldtype(VALUE dummy, VALUE rb_fieldtype1, VALUE fieldtype2)
1255 { return rbncurs_c_link_fieldtype(rb_fieldtype1, fieldtype2); }
1256
1257 static VALUE rbncurs_c_set_new_page(VALUE rb_field, VALUE new_page_flag) {
1258 FIELD* field = get_field(rb_field);
1259 return INT2NUM(set_new_page(field, RTEST(new_page_flag)));
1260 }
1261 static VALUE rbncurs_m_set_new_page(VALUE dummy, VALUE rb_field, VALUE new_page_flag)
1262 { return rbncurs_c_set_new_page(rb_field, new_page_flag); }
1263
1264 static VALUE rbncurs_c_new_page(VALUE rb_field) {
1265 FIELD* field = get_field(rb_field);
1266 return (new_page(field))? Qtrue : Qfalse;
1267 }
1268 static VALUE rbncurs_m_new_page(VALUE dummy, VALUE rb_field)
1269 { return rbncurs_c_new_page(rb_field); }
1270
1271 /*
1272 * form_cursor
1273 */
1274 static VALUE rbncurs_c_pos_form_cursor(VALUE rb_form) {
1275 FORM* form = get_form(rb_form);
1276 return INT2NUM(pos_form_cursor(form));
1277 }
1278 static VALUE rbncurs_m_pos_form_cursor(VALUE dummy, VALUE rb_form)
1279 { return rbncurs_c_pos_form_cursor(rb_form); }
1280
1281 void init_form(void)
1282 {
1283
1284 mForm = rb_define_module_under(mNcurses, "Form");
1285
1286 FORM_SNG_FUNC(current_field,1);
1287 FORM_SNG_FUNC(data_ahead,1);
1288 FORM_SNG_FUNC(data_behind,1);
1289 FORM_SNG_FUNC(dup_field,3);
1290 FORM_SNG_FUNC(dynamic_field_info,4);
1291 FORM_SNG_FUNC(field_arg,1);
1292 FORM_SNG_FUNC(field_back,1);
1293 FORM_SNG_FUNC(field_buffer,2);
1294 FORM_SNG_FUNC(field_count,1);
1295 FORM_SNG_FUNC(field_fore,1);
1296 FORM_SNG_FUNC(field_index,1);
1297 FORM_SNG_FUNC(field_info,7);
1298 FORM_SNG_FUNC(field_init,1);
1299 FORM_SNG_FUNC(field_just,1);
1300 FORM_SNG_FUNC(field_opts,1);
1301 FORM_SNG_FUNC(field_opts_off,2);
1302 FORM_SNG_FUNC(field_opts_on,2);
1303 FORM_SNG_FUNC(field_pad,1);
1304 FORM_SNG_FUNC(field_status,1);
1305 FORM_SNG_FUNC(field_term,1);
1306 FORM_SNG_FUNC(field_type,1);
1307 /* FORM_SNG_FUNC(field_userptr,1); */
1308 FORM_SNG_FUNC(form_driver,2);
1309
1310 # ifdef HAVE_FORM_DRIVER_W
1311 FORM_SNG_FUNC(form_driver_w,3);
1312 #endif
1313
1314 FORM_SNG_FUNC(form_fields,1);
1315 FORM_SNG_FUNC(form_init,1);
1316 FORM_SNG_FUNC(form_opts,1);
1317 FORM_SNG_FUNC(form_opts_off,2);
1318 FORM_SNG_FUNC(form_opts_on,2);
1319 FORM_SNG_FUNC(form_page,1);
1320 FORM_SNG_FUNC(form_request_by_name,1);
1321 FORM_SNG_FUNC(form_request_name,1);
1322 FORM_SNG_FUNC(form_sub,1);
1323 FORM_SNG_FUNC(form_term,1);
1324 /* FORM_SNG_FUNC(form_userptr,1); */
1325 FORM_SNG_FUNC(form_win,1);
1326 FORM_SNG_FUNC(free_field,1);
1327 FORM_SNG_FUNC(free_fieldtype,1);
1328 FORM_SNG_FUNC(free_form,1);
1329 FORM_SNG_FUNC(link_field,3);
1330 FORM_SNG_FUNC(link_fieldtype,2);
1331 FORM_SNG_FUNC(move_field,3);
1332 FORM_SNG_FUNC(new_field,6);
1333 FORM_SNG_FUNC(new_fieldtype,2);
1334 FORM_SNG_FUNC(new_form,1);
1335 FORM_SNG_FUNC(new_page,1);
1336 FORM_SNG_FUNC(pos_form_cursor,1);
1337 FORM_SNG_FUNC(post_form,1);
1338 FORM_SNG_FUNC(scale_form,3);
1339 FORM_SNG_FUNC(set_current_field,2);
1340 FORM_SNG_FUNC(set_field_back,2);
1341 FORM_SNG_FUNC(set_field_buffer,3);
1342 FORM_SNG_FUNC(set_field_fore,2);
1343 FORM_SNG_FUNC(set_field_init,2);
1344 FORM_SNG_FUNC(set_field_just,2);
1345 FORM_SNG_FUNC(set_field_opts,2);
1346 FORM_SNG_FUNC(set_field_pad,2);
1347 FORM_SNG_FUNC(set_field_status,2);
1348 FORM_SNG_FUNC(set_field_term,2);
1349 FORM_SNG_FUNC(set_field_type,-1);
1350 /* FORM_SNG_FUNC(set_field_userptr,2); */
1351 FORM_SNG_FUNC(set_fieldtype_choice,3);
1352 FORM_SNG_FUNC(set_form_fields,2);
1353 FORM_SNG_FUNC(set_form_init,2);
1354 FORM_SNG_FUNC(set_form_opts,2);
1355 FORM_SNG_FUNC(set_form_page,2);
1356 FORM_SNG_FUNC(set_form_sub,2);
1357 FORM_SNG_FUNC(set_form_term,2);
1358 /* FORM_SNG_FUNC(set_form_userptr,2); */
1359 FORM_SNG_FUNC(set_form_win,2);
1360 FORM_SNG_FUNC(set_max_field,2);
1361 FORM_SNG_FUNC(set_new_page,2);
1362 FORM_SNG_FUNC(unpost_form,1);
1363
1364 init_err_codes();
1365 init_req_constants();
1366 init_opts_constants();
1367 init_just_constants();
1368 init_form_opts_constants();
1369
1370 /* Hashes to store registered blocks (Proc) */
1371 {
1372 VALUE hashes = rb_iv_set(mForm, "@proc_hashes", rb_ary_new());
1373 int i;
1374 for (i = 0; i < PROC_HASHES_COUNT; i++)
1375 {
1376 rb_ary_push(hashes, rb_hash_new());
1377 }
1378 }
1379
1380 /* Forms */
1381 rb_iv_set(mForm, "@forms_hash", rb_hash_new());
1382 cFORM = rb_define_class_under(mForm, "FORM", rb_cObject);
1383 rb_undef_alloc_func(cFORM);
1384 rb_define_singleton_method(cFORM, "new",
1385 (&rbncurs_m_new_form),
1386 1);
1387 RB_CLASS_METH(cFORM, NULL, current_field,0);
1388 RB_CLASS_METH(cFORM, NULL, data_ahead,0);
1389 RB_CLASS_METH(cFORM, NULL, data_behind,0);
1390 RB_CLASS_METH(cFORM, NULL, dup_field,2);
1391 RB_CLASS_METH(cFORM, NULL, field_count,0);
1392 RB_CLASS_METH(cFORM, NULL, field_init,0);
1393 RB_CLASS_METH(cFORM, NULL, field_term,0);
1394 RB_CLASS_METH(cFORM, "driver", form_driver,1);
1395
1396 # ifdef HAVE_FORM_DRIVER_W
1397 RB_CLASS_METH(cFORM, "driver_w", form_driver_w,2);
1398 #endif
1399
1400 RB_CLASS_METH(cFORM, "fields", form_fields,0);
1401 RB_CLASS_METH(cFORM, "init", form_init,0);
1402 RB_CLASS_METH(cFORM, "opts", form_opts,0);
1403 RB_CLASS_METH(cFORM, "opts_off", form_opts_off,1);
1404 RB_CLASS_METH(cFORM, "opts_on", form_opts_on,1);
1405 RB_CLASS_METH(cFORM, "page", form_page,0);
1406 RB_CLASS_METH(cFORM, "sub", form_sub,0);
1407 RB_CLASS_METH(cFORM, "term", form_term,0);
1408 /* RB_CLASS_METH(cFORM, "userptr", form_userptr,0); */
1409 RB_CLASS_METH(cFORM, "win", form_win,0);
1410 RB_CLASS_METH(cFORM, "free", free_form,0);
1411 RB_CLASS_METH(cFORM, "pos_cursor", pos_form_cursor,0);
1412 RB_CLASS_METH(cFORM, "post", post_form,0);
1413 RB_CLASS_METH(cFORM, "scale", scale_form,2);
1414 RB_CLASS_METH(cFORM, "current_field=", set_current_field,1);
1415 RB_CLASS_METH(cFORM, "field_init=", set_field_init,1);
1416 RB_CLASS_METH(cFORM, "field_term=", set_field_term,1);
1417 RB_CLASS_METH(cFORM, "fields=", set_form_fields,1);
1418 RB_CLASS_METH(cFORM, "init=", set_form_init,1);
1419 RB_CLASS_METH(cFORM, "opts=", set_form_opts,1);
1420 RB_CLASS_METH(cFORM, "page=", set_form_page,1);
1421 RB_CLASS_METH(cFORM, "sub=", set_form_sub,1);
1422 RB_CLASS_METH(cFORM, "term=", set_form_term,1);
1423 /* RB_CLASS_METH(cFORM, "userptr=", set_form_userptr,1); */
1424 RB_CLASS_METH(cFORM, "win=", set_form_win,1);
1425 RB_CLASS_METH(cFORM, "unpost", unpost_form,0);
1426
1427 /* Fields */
1428 rb_iv_set(mForm, "@fields_hash", rb_hash_new());
1429 cFIELD = rb_define_class_under(mForm, "FIELD", rb_cObject);
1430 rb_undef_alloc_func(cFIELD);
1431 rb_define_singleton_method(cFIELD, "new",
1432 (&rbncurs_m_new_field),
1433 6);
1434 RB_CLASS_METH(cFIELD, "dup", dup_field,2);
1435 RB_CLASS_METH(cFIELD, "dynamic_info", dynamic_field_info,3);
1436 RB_CLASS_METH(cFIELD, "arg", field_arg,0);
1437 RB_CLASS_METH(cFIELD, "back", field_back,0);
1438 RB_CLASS_METH(cFIELD, "buffer", field_buffer,1);
1439 RB_CLASS_METH(cFIELD, "fore", field_fore,0);
1440 RB_CLASS_METH(cFIELD, "index", field_index,0);
1441 RB_CLASS_METH(cFIELD, "info", field_info,6);
1442 RB_CLASS_METH(cFIELD, "just", field_just,0);
1443 RB_CLASS_METH(cFIELD, "opts", field_opts,0);
1444 RB_CLASS_METH(cFIELD, "opts_off", field_opts_off,1);
1445 RB_CLASS_METH(cFIELD, "opts_on", field_opts_on,1);
1446 RB_CLASS_METH(cFIELD, "pad", field_pad,0);
1447 RB_CLASS_METH(cFIELD, "status", field_status,0);
1448 RB_CLASS_METH(cFIELD, "type", field_type,0);
1449 /* RB_CLASS_METH(cFIELD, "userptr", field_userptr,0); */
1450 RB_CLASS_METH(cFIELD, "free", free_field,0);
1451 RB_CLASS_METH(cFIELD, "link", link_field,2);
1452 RB_CLASS_METH(cFIELD, "move", move_field,2);
1453 RB_CLASS_METH(cFIELD, NULL, new_page,0);
1454 RB_CLASS_METH(cFIELD, "back=", set_field_back,1);
1455 RB_CLASS_METH(cFIELD, "set_buffer", set_field_buffer,2);
1456 RB_CLASS_METH(cFIELD, "fore=", set_field_fore,1);
1457 RB_CLASS_METH(cFIELD, "just=", set_field_just,1);
1458 RB_CLASS_METH(cFIELD, "opts=", set_field_opts,1);
1459 RB_CLASS_METH(cFIELD, "pad=", set_field_pad,1);
1460 RB_CLASS_METH(cFIELD, "status=", set_field_status,1);
1461 RB_CLASS_METH(cFIELD, "set_type", set_field_type,-1);
1462 /* RB_CLASS_METH(cFIELD, "userptr=", set_field_userptr,1); */
1463 RB_CLASS_METH(cFIELD, "max_field=", set_max_field,1);
1464 RB_CLASS_METH(cFIELD, "new_page=", set_new_page,1);
1465
1466
1467
1468
1469 /* Field types */
1470 rb_iv_set(mForm, "@fieldtypes_hash", rb_hash_new());
1471 cFIELDTYPE = rb_define_class_under(mForm, "FIELDTYPE", rb_cObject);
1472 rb_undef_alloc_func(cFIELDTYPE);
1473 rb_define_singleton_method(cFIELDTYPE, "new",
1474 (&rbncurs_m_new_fieldtype),
1475 2);
1476 RB_CLASS_METH(cFIELDTYPE, "free", free_fieldtype,0);
1477 RB_CLASS_METH(cFIELDTYPE, "link", link_fieldtype,1);
1478 RB_CLASS_METH(cFIELDTYPE, "set_choice", set_fieldtype_choice,2);
1479
1480 /* Create predefined types */
1481 rb_define_const(mForm, "TYPE_ALNUM", wrap_fieldtype(TYPE_ALNUM));
1482 rb_define_const(mForm, "TYPE_ALPHA", wrap_fieldtype(TYPE_ALPHA));
1483 rb_define_const(mForm, "TYPE_ENUM", wrap_fieldtype(TYPE_ENUM));
1484 rb_define_const(mForm, "TYPE_INTEGER", wrap_fieldtype(TYPE_INTEGER));
1485 rb_define_const(mForm, "TYPE_NUMERIC", wrap_fieldtype(TYPE_NUMERIC));
1486 rb_define_const(mForm, "TYPE_REGEXP", wrap_fieldtype(TYPE_REGEXP));
1487 rb_define_const(mForm, "TYPE_IPV4", wrap_fieldtype(TYPE_IPV4));
1488
1489 }
1490
1491 #endif