-
Notifications
You must be signed in to change notification settings - Fork 20
/
form_wrap.c
1450 lines (1343 loc) · 48.5 KB
/
form_wrap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This is a curses forms wrapper as part of ncurses-ruby
* Contributed by Simon Kaczor <skaczor@cox.net>
* Prognosoft Inc. <http://www.prognosoft.biz>
* Copyright 2004
*
* Changes:
* (C) 2004 Tobias Peters
* (C) 2005 2009 Tobias Herzke
*
* This module is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This module is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this module; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifdef HAVE_FORM_H
#include "form_wrap.h"
#include "ncurses_wrap.h"
#include "compat.h"
VALUE mForm;
VALUE cFIELD;
VALUE cFIELDTYPE;
VALUE cFORM;
void init_err_codes()
{
/* The routine succeeded. */
FORM_DEF_CONST(E_OK);
/* The field is already connected to a form. */
FORM_DEF_CONST(E_CONNECTED);
/* System error occurred (see errno). */
FORM_DEF_CONST(E_SYSTEM_ERROR);
/* Routine detected an incorrect or out-of-range argument. */
FORM_DEF_CONST(E_BAD_ARGUMENT);
/* The form is already posted. */
FORM_DEF_CONST(E_POSTED);
/* Routine was called from an initialization or termination function. */
FORM_DEF_CONST(E_BAD_STATE);
/* Form is too large for its window. */
FORM_DEF_CONST(E_NO_ROOM);
/* The form has not been posted. */
FORM_DEF_CONST(E_NOT_POSTED);
/* The form driver code saw an unknown request code. */
FORM_DEF_CONST(E_UNKNOWN_COMMAND);
/* Contents of a field are not valid. */
FORM_DEF_CONST(E_INVALID_FIELD);
/* No fields are connected to the form. */
FORM_DEF_CONST(E_NOT_CONNECTED);
/* The form driver could not process the request.} */
FORM_DEF_CONST(E_REQUEST_DENIED);
}
/*
* Form driver request characters listed in form_driver(3x) man page
*/
void init_req_constants() {
/* Move to the next page */
FORM_DEF_CONST(REQ_NEXT_PAGE);
/* Move to the previous page. */
FORM_DEF_CONST(REQ_PREV_PAGE);
/* Move to the first page. */
FORM_DEF_CONST(REQ_FIRST_PAGE);
/* Move to the last field. */
FORM_DEF_CONST(REQ_LAST_PAGE);
/* Move to the next field. */
FORM_DEF_CONST(REQ_NEXT_FIELD);
/* Move to the previous field. */
FORM_DEF_CONST(REQ_PREV_FIELD);
/* Move to the first field. */
FORM_DEF_CONST(REQ_FIRST_FIELD);
/* Move to the last field. */
FORM_DEF_CONST(REQ_LAST_FIELD);
/* Move to the sorted next field. */
FORM_DEF_CONST(REQ_SNEXT_FIELD);
/* Move to the sorted previous field. */
FORM_DEF_CONST(REQ_SPREV_FIELD);
/* Move to the sorted first field. */
FORM_DEF_CONST(REQ_SFIRST_FIELD);
/* Move to the sorted last field. */
FORM_DEF_CONST(REQ_SLAST_FIELD);
/* Move left to a field. */
FORM_DEF_CONST(REQ_LEFT_FIELD);
/* Move right to a field. */
FORM_DEF_CONST(REQ_RIGHT_FIELD);
/* Move up to a field. */
FORM_DEF_CONST(REQ_UP_FIELD);
/* Move down to a field. */
FORM_DEF_CONST(REQ_DOWN_FIELD);
/* Move to the next char. */
FORM_DEF_CONST(REQ_NEXT_CHAR);
/* Move to the previous char. */
FORM_DEF_CONST(REQ_PREV_CHAR);
/* Move to the next line. */
FORM_DEF_CONST(REQ_NEXT_LINE);
/* Move to the previous line. */
FORM_DEF_CONST(REQ_PREV_LINE);
/* Move to the next word. */
FORM_DEF_CONST(REQ_NEXT_WORD);
/* Move to the previous word. */
FORM_DEF_CONST(REQ_PREV_WORD);
/* Move to the beginning of the field. */
FORM_DEF_CONST(REQ_BEG_FIELD);
/* Move to the end of the field. */
FORM_DEF_CONST(REQ_END_FIELD);
/* Move to the beginning of the line. */
FORM_DEF_CONST(REQ_BEG_LINE);
/* Move to the end of the line. */
FORM_DEF_CONST(REQ_END_LINE);
/* Move left in the field. */
FORM_DEF_CONST(REQ_LEFT_CHAR);
/* Move right in the field. */
FORM_DEF_CONST(REQ_RIGHT_CHAR);
/* Move up in the field. */
FORM_DEF_CONST(REQ_UP_CHAR);
/* Move down in the field. */
FORM_DEF_CONST(REQ_DOWN_CHAR);
/* Insert or overlay a new line. */
FORM_DEF_CONST(REQ_NEW_LINE);
/* Insert a blank at the cursor. */
FORM_DEF_CONST(REQ_INS_CHAR);
/* Insert a blank line at the cursor. */
FORM_DEF_CONST(REQ_INS_LINE);
/* Delete character at the cursor. */
FORM_DEF_CONST(REQ_DEL_CHAR);
/* Delete character before the cursor. */
FORM_DEF_CONST(REQ_DEL_PREV);
/* Delete line at the cursor. */
FORM_DEF_CONST(REQ_DEL_LINE);
/* Delete blank-delimited word at the cursor. */
FORM_DEF_CONST(REQ_DEL_WORD);
/* Clear to end of line from cursor. */
FORM_DEF_CONST(REQ_CLR_EOL);
/* Clear to end of field from cursor. */
FORM_DEF_CONST(REQ_CLR_EOF);
/* Clear the entire field. */
FORM_DEF_CONST(REQ_CLR_FIELD);
/* Enter overlay mode. */
FORM_DEF_CONST(REQ_OVL_MODE);
/* Enter insert mode. */
FORM_DEF_CONST(REQ_INS_MODE);
/* Scroll the field forward a line. */
FORM_DEF_CONST(REQ_SCR_FLINE);
/* Scroll the field backward a line. */
FORM_DEF_CONST(REQ_SCR_BLINE);
/* Scroll the field forward a page. */
FORM_DEF_CONST(REQ_SCR_FPAGE);
/* Scroll the field backward a page. */
FORM_DEF_CONST(REQ_SCR_BPAGE);
/* Scroll the field forward half a page. */
FORM_DEF_CONST(REQ_SCR_FHPAGE);
/* Scroll the field backward half a page. */
FORM_DEF_CONST(REQ_SCR_BHPAGE);
/* Scroll the field forward a character. */
FORM_DEF_CONST(REQ_SCR_FCHAR);
/* Scroll the field backward a character. */
FORM_DEF_CONST(REQ_SCR_BCHAR);
/* Horizontal scroll the field forward a line. */
FORM_DEF_CONST(REQ_SCR_HFLINE);
/* Horizontal scroll the field backward a line. */
FORM_DEF_CONST(REQ_SCR_HBLINE);
/* Horizontal scroll the field forward half a line. */
FORM_DEF_CONST(REQ_SCR_HFHALF);
/* Horizontal scroll the field backward half a line. */
FORM_DEF_CONST(REQ_SCR_HBHALF);
/* Validate field. */
FORM_DEF_CONST(REQ_VALIDATION);
/* Display next field choice. */
FORM_DEF_CONST(REQ_NEXT_CHOICE);
/* Display previous field choice. */
FORM_DEF_CONST(REQ_PREV_CHOICE);
}
/*
* field justification constants
*/
void init_just_constants() {
FORM_DEF_CONST(NO_JUSTIFICATION);
FORM_DEF_CONST(JUSTIFY_RIGHT);
FORM_DEF_CONST(JUSTIFY_LEFT);
FORM_DEF_CONST(JUSTIFY_CENTER);
}
/*
* field options constants
*/
void init_opts_constants() {
FORM_DEF_CONST(O_VISIBLE);
FORM_DEF_CONST(O_ACTIVE);
FORM_DEF_CONST(O_PUBLIC);
FORM_DEF_CONST(O_EDIT);
FORM_DEF_CONST(O_WRAP);
FORM_DEF_CONST(O_BLANK);
FORM_DEF_CONST(O_AUTOSKIP);
FORM_DEF_CONST(O_NULLOK);
FORM_DEF_CONST(O_STATIC);
FORM_DEF_CONST(O_PASSOK);
}
void init_form_opts_constants() {
FORM_DEF_CONST(O_NL_OVERLOAD);
FORM_DEF_CONST(O_BS_OVERLOAD);
}
/*
* _PAGE wrapper
*/
/* static VALUE wrap_page(_PAGE* page) */
/* { */
/* if (page == 0) return Qnil; */
/* { */
/* VALUE pages_hash = rb_iv_get(mForm, "@pages_hash"); */
/* VALUE page_adress = INT2NUM((long)(page)); */
/* VALUE rb_page = rb_hash_aref(pages_hash, page_adress); */
/* if (rb_page == Qnil) { */
/* rb_page = Data_Wrap_Struct(cPAGE, 0, 0, page); */
/* rb_iv_set(rb_page, "@destroyed", Qfalse); */
/* rb_hash_aset(pages_hash, page_adress, rb_page); */
/* } */
/* return rb_page; */
/* } */
/* } */
/* static _PAGE* get_page(VALUE rb_page) */
/* { */
/* _PAGE* page; */
/* if (rb_page == Qnil) return 0; */
/* if (rb_iv_get(rb_page, "@destroyed") == Qtrue) { */
/* rb_raise(rb_eRuntimeError, "Attempt to access a destroyed page"); */
/* return 0; */
/* } */
/* Data_Get_Struct(rb_page, _PAGE, page); */
/* return page; */
/* } */
/*
* FIELD wrapper
*/
static VALUE wrap_field(FIELD* field)
{
if (field == 0) return Qnil;
{
VALUE fields_hash = rb_iv_get(mForm, "@fields_hash");
VALUE field_adress = INT2NUM((long)(field));
VALUE rb_field = rb_hash_aref(fields_hash, field_adress);
if (rb_field == Qnil) {
rb_field = Data_Wrap_Struct(cFIELD, 0, 0, field);
rb_iv_set(rb_field, "@destroyed", Qfalse);
rb_hash_aset(fields_hash, field_adress, rb_field);
}
return rb_field;
}
}
static FIELD* get_field(VALUE rb_field)
{
FIELD* field;
if (rb_field == Qnil) return 0;
if (rb_iv_get(rb_field, "@destroyed") == Qtrue) {
rb_raise(rb_eRuntimeError, "Attempt to access a destroyed field");
return 0;
}
Data_Get_Struct(rb_field, FIELD, field);
return field;
}
/*
* FIELDTYPE wrapper
*/
static VALUE wrap_fieldtype(FIELDTYPE* fieldtype)
{
if (fieldtype == NULL) return Qnil;
{
VALUE fieldtypes_hash = rb_iv_get(mForm, "@fieldtypes_hash");
VALUE fieldtype_adress = INT2NUM((long)(fieldtype));
VALUE rb_fieldtype = rb_hash_aref(fieldtypes_hash, fieldtype_adress);
if (rb_fieldtype == Qnil) {
rb_fieldtype = Data_Wrap_Struct(cFIELDTYPE, 0, 0, fieldtype);
rb_iv_set(rb_fieldtype, "@destroyed", Qfalse);
rb_hash_aset(fieldtypes_hash, fieldtype_adress, rb_fieldtype);
}
return rb_fieldtype;
}
}
static FIELDTYPE* get_fieldtype(VALUE rb_fieldtype)
{
FIELDTYPE* fieldtype;
if (rb_fieldtype == Qnil) return 0;
if (rb_iv_get(rb_fieldtype, "@destroyed") == Qtrue) {
rb_raise(rb_eRuntimeError, "Attempt to access a destroyed fieldtype");
return 0;
}
Data_Get_Struct(rb_fieldtype, FIELDTYPE, fieldtype);
return fieldtype;
}
/*
* FORM wrapper
*/
static VALUE wrap_form(FORM* form)
{
if (form == 0) return Qnil;
{
VALUE forms_hash = rb_iv_get(mForm, "@forms_hash");
VALUE form_adress = INT2NUM((long)(form));
VALUE rb_form = rb_hash_aref(forms_hash, form_adress);
if (rb_form == Qnil) {
rb_form = Data_Wrap_Struct(cFORM, 0, 0, form);
rb_iv_set(rb_form, "@destroyed", Qfalse);
rb_hash_aset(forms_hash, form_adress, rb_form);
}
return rb_form;
}
}
static FORM* get_form(VALUE rb_form)
{
FORM* form;
if (rb_form == Qnil) return 0;
if (rb_iv_get(rb_form, "@destroyed") == Qtrue) {
rb_raise(rb_eRuntimeError, "Attempt to access a destroyed form");
return 0;
}
Data_Get_Struct(rb_form, FORM, form);
return form;
}
/*
* Proc objects are registered using hashes (one for each type of hook)
* The key in the hash is the address of the ncurses "object" and the value is
* the Proc object.
*/
#define FIELD_INIT_HOOK 0
#define FIELD_TERM_HOOK 1
#define FORM_INIT_HOOK 2
#define FORM_TERM_HOOK 3
#define FIELDTYPE_FIELD_CHECK_HOOK 4
#define FIELDTYPE_CHAR_CHECK_HOOK 5
#define FIELDTYPE_NEXT_CHOICE_HOOK 6
#define FIELDTYPE_PREV_CHOICE_HOOK 7
#define FIELDTYPE_ARGS 8
#define PROC_HASHES_COUNT 9
static VALUE get_proc_hash(int hook) {
VALUE arr = rb_iv_get(mForm, "@proc_hashes");
VALUE hash = rb_ary_entry(arr, (long)hook);
if (hash == Qnil) {
rb_raise(rb_eRuntimeError, "Invalid proc hash.");
}
return hash;
}
/*
* Returns an existing Ruby Proc for a given owning "object" and hook type.
* Qnil will be returned if no Proc was associated with the owner
*/
static VALUE get_proc(void* owner, int hook) {
if (owner == 0) return Qnil;
{
VALUE owner_adress = INT2NUM((long)(owner));
VALUE proc_hash = get_proc_hash(hook);
VALUE proc = rb_hash_aref(proc_hash, owner_adress);
return proc;
}
}
/*
* Registers the Proc object with a given owner "object" and hook type.
* If proc is Qnil, the hook is unregistered instead.
*/
static void reg_proc(void* owner, int hook, VALUE proc) {
if (owner == NULL) return;
{
VALUE proc_hash = get_proc_hash(hook);
VALUE owner_address = INT2NUM((long)(owner));
if (proc == Qnil) {
rb_hash_delete(proc_hash, owner_address);
}
else {
rb_hash_aset(proc_hash, owner_address, proc);
}
}
}
/*
* Form creation/destruction functions
*/
static VALUE rbncurs_m_new_form(VALUE dummy, VALUE rb_field_array)
{
long n = rbncurs_array_length(rb_field_array);
/* Will ncurses free this array? If not, must do it after calling free_form(). */
FIELD** fields = ALLOC_N(FIELD*, (n+1));
long i;
for (i=0; i<n; i++){
fields[i] = get_field(rb_ary_entry(rb_field_array, i));
}
fields[n] = NULL;
return wrap_form(new_form(fields));
}
static VALUE rbncurs_c_free_form(VALUE rb_form) {
VALUE forms_hash = rb_iv_get(mForm, "@forms_hash");
FORM* form = get_form(rb_form);
VALUE form_adress = INT2NUM((long)(form));
rb_funcall(forms_hash, rb_intern("delete"), 1, form_adress);
rb_iv_set(rb_form, "@destroyed", Qtrue);
return INT2NUM(free_form(form));
}
static VALUE rbncurs_m_free_form(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_free_form(rb_form); }
/*
* form_post
*/
static VALUE rbncurs_c_post_form(VALUE rb_form) {
FORM* form = get_form(rb_form);
return INT2NUM(post_form(form));
}
static VALUE rbncurs_m_post_form(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_post_form(rb_form); }
static VALUE rbncurs_c_unpost_form(VALUE rb_form) {
FORM* form = get_form(rb_form);
return INT2NUM(unpost_form(form));
}
static VALUE rbncurs_m_unpost_form(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_unpost_form(rb_form); }
/*
* Form driver
*/
static VALUE rbncurs_c_form_driver(VALUE rb_form, VALUE c) {
FORM* form = get_form(rb_form);
return INT2NUM(form_driver(form, NUM2INT(c)));
}
static VALUE rbncurs_m_form_driver(VALUE dummy, VALUE rb_form, VALUE c)
{ return rbncurs_c_form_driver(rb_form, c); }
/*
* form_page(3x)
*/
static VALUE rbncurs_c_set_current_field(VALUE rb_form, VALUE rb_field) {
FORM* form = get_form(rb_form);
FIELD* field = get_field(rb_field);
return INT2NUM(set_current_field(form, field));
}
static VALUE rbncurs_m_set_current_field(VALUE dummy, VALUE rb_form, VALUE rb_field)
{ return rbncurs_c_set_current_field(rb_form, rb_field); }
static VALUE rbncurs_c_current_field(VALUE rb_form) {
FORM* form = get_form(rb_form);
return wrap_field(current_field(form));
}
static VALUE rbncurs_m_current_field(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_current_field(rb_form); }
static VALUE rbncurs_c_set_form_page(VALUE rb_form, VALUE n) {
FORM* form = get_form(rb_form);
return INT2NUM(set_form_page(form, NUM2INT(n)));
}
static VALUE rbncurs_m_set_form_page(VALUE dummy, VALUE rb_form, VALUE n)
{ return rbncurs_c_set_form_page(rb_form, n); }
static VALUE rbncurs_c_form_page(VALUE rb_form) {
FORM* form = get_form(rb_form);
return INT2NUM(form_page(form));
}
static VALUE rbncurs_m_form_page(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_form_page(rb_form); }
static VALUE rbncurs_c_field_index(VALUE rb_field) {
FIELD* field = get_field(rb_field);
return INT2NUM(field_index(field));
}
static VALUE rbncurs_m_field_index(VALUE dummy, VALUE rb_field)
{ return rbncurs_c_field_index(rb_field); }
/*
* form_data(3x)
*/
static VALUE rbncurs_c_data_ahead(VALUE rb_form) {
FORM* form = get_form(rb_form);
return (data_ahead(form)) ? Qtrue: Qfalse;
}
static VALUE rbncurs_m_data_ahead(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_data_ahead(rb_form); }
static VALUE rbncurs_c_data_behind(VALUE rb_form) {
FORM* form = get_form(rb_form);
return (data_behind(form)) ? Qtrue: Qfalse;
}
static VALUE rbncurs_m_data_behind(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_data_behind(rb_form); }
/*
* Field functions (form_field_new(3x))
*/
static VALUE rbncurs_m_new_field(VALUE dummy,
VALUE height, VALUE width,
VALUE toprow, VALUE leftcol,
VALUE offscreen, VALUE nbuffers) {
return wrap_field(new_field(NUM2INT(height), NUM2INT(width),
NUM2INT(toprow), NUM2INT(leftcol),
NUM2INT(offscreen), NUM2INT(nbuffers)));
}
static VALUE rbncurs_c_dup_field(VALUE rb_field, VALUE toprow, VALUE leftcol) {
FIELD* field = get_field(rb_field);
return wrap_field(dup_field(field, NUM2INT(toprow),NUM2INT(leftcol)));
}
static VALUE rbncurs_m_dup_field(VALUE dummy, VALUE rb_field, VALUE toprow, VALUE leftcol)
{ return rbncurs_c_dup_field(rb_field, toprow, leftcol); }
static VALUE rbncurs_c_link_field(VALUE rb_field, VALUE toprow, VALUE leftcol) {
FIELD* field = get_field(rb_field);
return wrap_field(link_field(field, NUM2INT(toprow),NUM2INT(leftcol)));
}
static VALUE rbncurs_m_link_field(VALUE dummy, VALUE rb_field, VALUE toprow, VALUE leftcol)
{ return rbncurs_c_link_field(rb_field, toprow, leftcol); }
static VALUE rbncurs_c_free_field(VALUE rb_field) {
VALUE fields_hash = rb_iv_get(mForm, "@fields_hash");
FIELD* field = get_field(rb_field);
VALUE field_adress = INT2NUM((long)(field));
rb_funcall(fields_hash, rb_intern("delete"), 1, field_adress);
rb_iv_set(rb_field, "@destroyed", Qtrue);
return INT2NUM(free_field(field));
}
static VALUE rbncurs_m_free_field(VALUE dummy, VALUE rb_field)
{ return rbncurs_c_free_field(rb_field); }
/*
* form_field_info(3x)
*/
static VALUE rbncurs_c_field_info(VALUE rb_field, VALUE rows, VALUE cols,
VALUE frow, VALUE fcol, VALUE nrow, VALUE nbuf) {
if (rb_obj_is_instance_of(rows, rb_cArray) != Qtrue
|| rb_obj_is_instance_of(cols, rb_cArray) != Qtrue
|| rb_obj_is_instance_of(frow, rb_cArray) != Qtrue
|| rb_obj_is_instance_of(fcol, rb_cArray) != Qtrue
|| rb_obj_is_instance_of(nrow, rb_cArray) != Qtrue
|| rb_obj_is_instance_of(nbuf, rb_cArray) != Qtrue) {
rb_raise(rb_eArgError,
"rows, cols, frow, fcol, nrow and nbuf arguments must be empty Arrays");
return Qnil;
}
else {
FIELD* field = get_field(rb_field);
int vals[6] = {0,0,0,0,0,0};
int result = field_info(field, &vals[0],&vals[1],&vals[2],&vals[3],&vals[4],&vals[5]);
rb_ary_push(rows, INT2NUM(vals[0]));
rb_ary_push(cols, INT2NUM(vals[1]));
rb_ary_push(frow, INT2NUM(vals[2]));
rb_ary_push(fcol, INT2NUM(vals[3]));
rb_ary_push(nrow, INT2NUM(vals[4]));
rb_ary_push(nbuf, INT2NUM(vals[5]));
return INT2NUM(result);
}
}
static VALUE rbncurs_m_field_info(VALUE dummy, VALUE rb_field, VALUE rows, VALUE cols,
VALUE frow, VALUE fcol, VALUE nrow, VALUE nbuf)
{ return rbncurs_c_field_info(rb_field, rows, cols, frow, fcol, nrow, nbuf); }
static VALUE rbncurs_c_dynamic_field_info(VALUE rb_field, VALUE rows, VALUE cols,
VALUE max) {
if (rb_obj_is_instance_of(rows, rb_cArray) != Qtrue
|| rb_obj_is_instance_of(cols, rb_cArray) != Qtrue
|| rb_obj_is_instance_of(max, rb_cArray) != Qtrue) {
rb_raise(rb_eArgError,
"rows, cols and max arguments must be empty Arrays");
return Qnil;
}
else {
FIELD* field = get_field(rb_field);
int vals[3] = {0,0,0};
int result = dynamic_field_info(field, &vals[0],&vals[1],&vals[2]);
rb_ary_push(rows, INT2NUM(vals[0]));
rb_ary_push(cols, INT2NUM(vals[1]));
rb_ary_push(max, INT2NUM(vals[2]));
return INT2NUM(result);
}
}
static VALUE rbncurs_m_dynamic_field_info(VALUE dummy, VALUE rb_field, VALUE rows, VALUE cols,
VALUE max)
{ return rbncurs_c_dynamic_field_info(rb_field, rows, cols, max); }
/*
* field_validation
*/
static VALUE rbncurs_c_set_field_type(int argc, VALUE* argv, VALUE rb_field) {
VALUE rb_fieldtype, arg3, arg4, arg5;
FIELD* field = get_field(rb_field);
FIELDTYPE* ftype = NULL;
rb_scan_args(argc, argv, "13", &rb_fieldtype, &arg3, &arg4, &arg5);
ftype = get_fieldtype(rb_fieldtype);
if (ftype == TYPE_ALNUM ||
ftype == TYPE_ALPHA) {
if (argc != 2)
rb_raise(rb_eArgError, "TYPE_ALNUM and TYPE_ALPHA require one additional argument");
return INT2NUM(set_field_type(field, ftype,
NUM2INT(arg3)));
}
if (ftype == TYPE_ENUM) {
if (argc != 4) {
rb_raise(rb_eArgError, "TYPE_ENUM requires three additional arguments");
}
else {
int n = rbncurs_array_length(arg3);
/* Will ncurses free this array of strings in free_field()? */
char** list = ALLOC_N(char*, n+1);
int i;
for (i = 0; i < n; i++) {
list[i] = STR2CSTR(rb_ary_entry(arg3, (long)i));
}
list[n] = NULL;
return INT2NUM(set_field_type(field, ftype,
list,
RTEST(arg4),
RTEST(arg5)));
}
}
else if (ftype == TYPE_INTEGER) {
if (argc != 4)
rb_raise(rb_eArgError, "TYPE_INTEGER requires three additional arguments");
return INT2NUM(set_field_type(field, ftype,
NUM2INT(arg3),
NUM2LONG(arg4),
NUM2LONG(arg5)));
}
else if (ftype == TYPE_NUMERIC) {
if (argc != 4)
rb_raise(rb_eArgError, "TYPE_NUMERIC requires three additional arguments");
return INT2NUM(set_field_type(field, ftype,
NUM2INT(arg3),
NUM2DBL(arg4),
NUM2DBL(arg5)));
}
else if (ftype == TYPE_REGEXP){
if (argc != 2)
rb_raise(rb_eArgError, "TYPE_REGEXP requires one additional argument");
return INT2NUM(set_field_type(field, ftype,
STR2CSTR(arg3)));
}
else if (ftype == TYPE_IPV4){
if (argc != 1)
rb_raise(rb_eArgError, "TYPE_IPV4 has no additional arguments");
return INT2NUM(set_field_type(field, ftype));
}
else {
/* It is a user-defined field type. */
/* Will store the arguments associated with this field */
/* for use in the callback function. */
VALUE rest;
rb_scan_args(argc, argv, "1*", &rb_fieldtype, &rest);
reg_proc(field, FIELDTYPE_ARGS, rest);
/* Pass field as an optional parameter so that make_arg can create */
/* the block-argument used in finding the appropriate Ruby Proc */
return INT2NUM(set_field_type(field, ftype, field));
}
}
static VALUE rbncurs_m_set_field_type(int argc, VALUE* argv, VALUE dummy)
{ return rbncurs_c_set_field_type(argc-1, argv+1, argv[0]); }
static VALUE rbncurs_c_field_type(VALUE rb_field) {
FIELD* field = get_field(rb_field);
return wrap_fieldtype(field_type(field));
}
static VALUE rbncurs_m_field_type(VALUE dummy, VALUE rb_field)
{ return rbncurs_c_field_type(rb_field); }
/* What is this function doing??? */
static VALUE rbncurs_c_field_arg(VALUE rb_field) {
FIELD* field = get_field(rb_field);
field_arg(field);
return Qfalse;
}
static VALUE rbncurs_m_field_arg(VALUE dummy, VALUE rb_field)
{ return rbncurs_c_field_arg(rb_field); }
/*
* field_attributes
*/
static VALUE rbncurs_c_set_field_fore(VALUE rb_field, VALUE attr) {
FIELD* field = get_field(rb_field);
return INT2NUM(set_field_fore(field, NUM2ULONG(attr)));
}
static VALUE rbncurs_m_set_field_fore(VALUE dummy, VALUE rb_field, VALUE attr)
{ return rbncurs_c_set_field_fore(rb_field, attr); }
static VALUE rbncurs_c_field_fore(VALUE rb_field) {
FIELD* field = get_field(rb_field);
return ULONG2NUM(field_fore(field));
}
static VALUE rbncurs_m_field_fore(VALUE dummy, VALUE rb_field)
{ return rbncurs_c_field_fore(rb_field); }
static VALUE rbncurs_c_set_field_back(VALUE rb_field, VALUE attr) {
FIELD* field = get_field(rb_field);
return INT2NUM(set_field_back(field, NUM2ULONG(attr)));
}
static VALUE rbncurs_m_set_field_back(VALUE dummy, VALUE rb_field, VALUE attr)
{ return rbncurs_c_set_field_back(rb_field, attr); }
static VALUE rbncurs_c_field_back(VALUE rb_field) {
FIELD* field = get_field(rb_field);
return ULONG2NUM(field_back(field));
}
static VALUE rbncurs_m_field_back(VALUE dummy, VALUE rb_field)
{ return rbncurs_c_field_back(rb_field); }
static VALUE rbncurs_c_set_field_pad(VALUE rb_field, VALUE pad) {
FIELD* field = get_field(rb_field);
return INT2NUM(set_field_pad(field, NUM2INT(pad)));
}
static VALUE rbncurs_m_set_field_pad(VALUE dummy, VALUE rb_field, VALUE pad)
{ return rbncurs_c_set_field_pad(rb_field, pad); }
static VALUE rbncurs_c_field_pad(VALUE rb_field) {
FIELD* field = get_field(rb_field);
return INT2NUM(field_pad(field));
}
static VALUE rbncurs_m_field_pad(VALUE dummy, VALUE rb_field)
{ return rbncurs_c_field_pad(rb_field); }
/*
* field_buffer
*/
static VALUE rbncurs_c_set_field_buffer(VALUE rb_field, VALUE buf, VALUE value) {
FIELD* field = get_field(rb_field);
return INT2NUM(set_field_buffer(field, NUM2INT(buf), STR2CSTR(value)));
}
static VALUE rbncurs_m_set_field_buffer(VALUE dummy, VALUE rb_field, VALUE buf, VALUE value)
{ return rbncurs_c_set_field_buffer(rb_field, buf, value); }
static VALUE rbncurs_c_field_buffer(VALUE rb_field, VALUE buffer) {
FIELD* field = get_field(rb_field);
return rb_str_new2(field_buffer(field, NUM2INT(buffer)));
}
static VALUE rbncurs_m_field_buffer(VALUE dummy, VALUE rb_field, VALUE buffer)
{ return rbncurs_c_field_buffer(rb_field, buffer); }
static VALUE rbncurs_c_set_field_status(VALUE rb_field, VALUE status) {
FIELD* field = get_field(rb_field);
return INT2NUM(set_field_status(field, RTEST(status)));
}
static VALUE rbncurs_m_set_field_status(VALUE dummy, VALUE rb_field, VALUE status)
{ return rbncurs_c_set_field_status(rb_field, status); }
static VALUE rbncurs_c_field_status(VALUE rb_field) {
FIELD* field = get_field(rb_field);
return (field_status(field)) ? Qtrue: Qfalse;
}
static VALUE rbncurs_m_field_status(VALUE dummy, VALUE rb_field)
{ return rbncurs_c_field_status(rb_field); }
static VALUE rbncurs_c_set_max_field(VALUE rb_field, VALUE max) {
FIELD* field = get_field(rb_field);
return INT2NUM(set_max_field(field, NUM2INT(max)));
}
static VALUE rbncurs_m_set_max_field(VALUE dummy, VALUE rb_field, VALUE max)
{ return rbncurs_c_set_max_field(rb_field, max); }
/*
* form_field
*/
static VALUE rbncurs_c_set_form_fields(VALUE rb_form, VALUE rb_field_array) {
long n = rbncurs_array_length(rb_field_array);
/* If ncurses does not free memory used by the previous array of strings, */
/* we will have to do it now. */
FIELD** fields = ALLOC_N(FIELD*, (n+1));
long i;
FORM* form = NULL;
for (i=0; i<n; i++){
fields[i] = get_field(rb_ary_entry(rb_field_array, i));
}
fields[n] = NULL;
form = get_form(rb_form);
return INT2NUM(set_form_fields(form, fields));
}
static VALUE rbncurs_m_set_form_fields(VALUE dummy, VALUE rb_form, VALUE rb_field_array)
{ return rbncurs_c_set_form_fields(rb_form, rb_field_array); }
static VALUE rbncurs_c_form_fields(VALUE rb_form) {
FORM* form = get_form(rb_form);
FIELD** fields = form_fields(form);
VALUE arr = Qundef;
int i;
if (fields == NULL)
rb_raise(rb_eRuntimeError, "Error retrieving form fields");
arr = rb_ary_new();
i=0;
while (fields[i] != NULL)
{
rb_ary_push(arr, wrap_field(fields[i++]));
}
return arr;
}
static VALUE rbncurs_m_form_fields(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_form_fields(rb_form); }
static VALUE rbncurs_c_field_count(VALUE rb_form) {
FORM* form = get_form(rb_form);
return INT2NUM(field_count(form));
}
static VALUE rbncurs_m_field_count(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_field_count(rb_form); }
static VALUE rbncurs_c_move_field(VALUE rb_field, VALUE frow, VALUE fcol) {
FIELD* field = get_field(rb_field);
return INT2NUM(move_field(field, NUM2INT(frow), NUM2INT(fcol)));
}
static VALUE rbncurs_m_move_field(VALUE dummy, VALUE rb_field, VALUE frow, VALUE fcol)
{ return rbncurs_c_move_field(rb_field, frow, fcol); }
/*
* form_hook
*/
static void field_init_hook(FORM* form) {
/* Find the Proc object associated with this form */
VALUE proc = get_proc(form, FIELD_INIT_HOOK);
if (proc != Qnil) {
VALUE rb_form = wrap_form(form);
rb_funcall(proc, rb_intern("call"), 1, rb_form);
}
}
static VALUE rbncurs_c_set_field_init(VALUE rb_form, VALUE proc) {
FORM* form = NULL;
if (!rb_obj_is_kind_of(rb_form, cFORM))
rb_raise(rb_eArgError, "arg1 must be a FORM object");
if (!rb_obj_is_kind_of(proc, rb_cProc))
rb_raise(rb_eArgError, "arg2 must be a Proc object");
form = get_form(rb_form);
reg_proc(form, FIELD_INIT_HOOK, proc);
if (proc != Qnil) {
return INT2NUM(set_field_init(form, field_init_hook));
}
else {
return INT2NUM(set_field_init(form, NULL));
}
}
static VALUE rbncurs_m_set_field_init(VALUE dummy, VALUE rb_form, VALUE proc)
{ return rbncurs_c_set_field_init(rb_form, proc); }
static VALUE rbncurs_c_field_init(VALUE rb_form) {
FORM* form = get_form(rb_form);
return get_proc(form, FIELD_INIT_HOOK);
}
static VALUE rbncurs_m_field_init(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_field_init(rb_form); }
static void field_term_hook(FORM* form) {
/* Find the Proc object associated with this form */
VALUE proc = get_proc(form, FIELD_TERM_HOOK);
if (proc != Qnil) {
VALUE rb_form = wrap_form(form);
rb_funcall(proc, rb_intern("call"), 1, rb_form);
}
}
static VALUE rbncurs_c_set_field_term(VALUE rb_form, VALUE proc) {
FORM * form = NULL;
if (!rb_obj_is_kind_of(rb_form, cFORM))
rb_raise(rb_eArgError, "arg1 must be a FORM object");
if (!rb_obj_is_kind_of(proc, rb_cProc))
rb_raise(rb_eArgError, "arg2 must be a Proc object");
form = get_form(rb_form);
reg_proc(form, FIELD_TERM_HOOK, proc);
if (proc != Qnil) {
return INT2NUM(set_field_term(form, field_term_hook));
}
else {
return INT2NUM(set_field_term(form, NULL));
}
}
static VALUE rbncurs_m_set_field_term(VALUE dummy, VALUE rb_form, VALUE proc)
{ return rbncurs_c_set_field_term(rb_form, proc); }
static VALUE rbncurs_c_field_term(VALUE rb_form) {
FORM* form = get_form(rb_form);
return get_proc(form, FIELD_TERM_HOOK);
}
static VALUE rbncurs_m_field_term(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_field_term(rb_form); }
static void form_init_hook(FORM* form) {
/* Find the Proc object associated with this form */
VALUE proc = get_proc(form, FORM_INIT_HOOK);
if (proc != Qnil) {
VALUE rb_form = wrap_form(form);
rb_funcall(proc, rb_intern("call"), 1, rb_form);
}
}
static VALUE rbncurs_c_set_form_init(VALUE rb_form, VALUE proc) {
FORM * form = NULL;
if (!rb_obj_is_kind_of(rb_form, cFORM))
rb_raise(rb_eArgError, "arg1 must be a FORM object");
if (!rb_obj_is_kind_of(proc, rb_cProc))
rb_raise(rb_eArgError, "arg2 must be a Proc object");
form = get_form(rb_form);
reg_proc(form, FORM_INIT_HOOK, proc);
if (proc != Qnil) {
return INT2NUM(set_form_init(form, form_init_hook));
}
else {
return INT2NUM(set_form_init(form, NULL));
}
}
static VALUE rbncurs_m_set_form_init(VALUE dummy, VALUE rb_form, VALUE proc)
{ return rbncurs_c_set_form_init(rb_form, proc); }
static VALUE rbncurs_c_form_init(VALUE rb_form) {
FORM* form = get_form(rb_form);
return get_proc(form, FORM_INIT_HOOK);
}
static VALUE rbncurs_m_form_init(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_form_init(rb_form); }
static void form_term_hook(FORM* form) {
/* Find the Proc object associated with this form */
VALUE proc = get_proc(form, FORM_TERM_HOOK);
if (proc != Qnil) {
VALUE rb_form = wrap_form(form);
rb_funcall(proc, rb_intern("call"), 1, rb_form);
}
}
static VALUE rbncurs_c_set_form_term(VALUE rb_form, VALUE proc) {
FORM * form = NULL;
if (!rb_obj_is_kind_of(rb_form, cFORM))
rb_raise(rb_eArgError, "arg1 must be a FORM object");
if (!rb_obj_is_kind_of(proc, rb_cProc))
rb_raise(rb_eArgError, "arg2 must be a Proc object");
form = get_form(rb_form);
reg_proc(form, FORM_TERM_HOOK, proc);
if (proc != Qnil) {
return INT2NUM(set_form_term(form, form_term_hook));
}
else {
return INT2NUM(set_form_term(form, NULL));
}
}
static VALUE rbncurs_m_set_form_term(VALUE dummy, VALUE rb_form, VALUE proc)
{ return rbncurs_c_set_form_term(rb_form, proc); }
static VALUE rbncurs_c_form_term(VALUE rb_form) {
FORM* form = get_form(rb_form);
return get_proc(form, FORM_TERM_HOOK);
}
static VALUE rbncurs_m_form_term(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_form_term(rb_form); }
/*
* form_field_just
*/
static VALUE rbncurs_c_set_field_just(VALUE rb_field, VALUE justification) {
FIELD* field = get_field(rb_field);
return INT2NUM(set_field_just(field, NUM2INT(justification)));
}
static VALUE rbncurs_m_set_field_just(VALUE dummy, VALUE rb_field, VALUE justification)
{ return rbncurs_c_set_field_just(rb_field, justification); }
static VALUE rbncurs_c_field_just(VALUE rb_field) {
FIELD* field = get_field(rb_field);
return INT2NUM(field_just(field));
}
static VALUE rbncurs_m_field_just(VALUE dummy, VALUE rb_field)
{ return rbncurs_c_field_just(rb_field); }
/*
* form_field_opts
*/
static VALUE rbncurs_c_set_field_opts(VALUE rb_field, VALUE opts) {
FIELD* field = get_field(rb_field);
return INT2NUM(set_field_opts(field, NUM2INT(opts)));
}
static VALUE rbncurs_m_set_field_opts(VALUE dummy, VALUE rb_field, VALUE opts)
{ return rbncurs_c_set_field_opts(rb_field, opts); }
static VALUE rbncurs_c_field_opts_on(VALUE rb_field, VALUE opts) {
FIELD* field = get_field(rb_field);
return INT2NUM(field_opts_on(field, NUM2INT(opts)));
}
static VALUE rbncurs_m_field_opts_on(VALUE dummy, VALUE rb_field, VALUE opts)
{ return rbncurs_c_field_opts_on(rb_field, opts); }
static VALUE rbncurs_c_field_opts_off(VALUE rb_field, VALUE opts) {
FIELD* field = get_field(rb_field);