-
Notifications
You must be signed in to change notification settings - Fork 20
/
menu_wrap.c
1145 lines (1013 loc) · 34.6 KB
/
menu_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 menu wrapper as part of ncurses-ruby.
* It borrows heavily from form_wrap.c.
* Contributed by Earle Clubb <eclubb@valcom.com>
* Valcom Inc. <http://www.valcom.com>
* Copyright 2007
*
* Modifications
* (C) 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_MENU_H
#include "menu_wrap.h"
#include "ncurses_wrap.h"
#include "compat.h"
VALUE mMenu;
VALUE cITEM;
VALUE cMENU;
void init_menu_err_codes(void)
{
/* The routine succeeded. */
MENU_DEF_CONST(E_OK);
/* System error occurred (see errno). */
MENU_DEF_CONST(E_SYSTEM_ERROR);
/* Routine detected an incorrect or out-of-range argument. */
MENU_DEF_CONST(E_BAD_ARGUMENT);
/* The menu is already posted. */
MENU_DEF_CONST(E_POSTED);
/* Routine was called from an initialization or termination function. */
MENU_DEF_CONST(E_BAD_STATE);
/* Menu is too large for its window. */
MENU_DEF_CONST(E_NO_ROOM);
/* The menu has not been posted. */
MENU_DEF_CONST(E_NOT_POSTED);
/* The menu driver code saw an unknown request code. */
MENU_DEF_CONST(E_UNKNOWN_COMMAND);
/* Character failed to match. */
MENU_DEF_CONST(E_NO_MATCH);
/* The designated item cannot be selected. */
MENU_DEF_CONST(E_NOT_SELECTABLE);
/* No items are connected to the menu. */
MENU_DEF_CONST(E_NOT_CONNECTED);
/* The menu driver could not process the request.} */
MENU_DEF_CONST(E_REQUEST_DENIED);
}
/*
* Menu driver request characters - menu_driver(3x) man page
*/
void init_menu_req_constants(void)
{
/* Move left to an item. */
MENU_DEF_CONST(REQ_LEFT_ITEM);
/* Move right to an item. */
MENU_DEF_CONST(REQ_RIGHT_ITEM);
/* Move up to an item. */
MENU_DEF_CONST(REQ_UP_ITEM);
/* Move down to an item. */
MENU_DEF_CONST(REQ_DOWN_ITEM);
/* Scroll up a line. */
MENU_DEF_CONST(REQ_SCR_ULINE);
/* Scroll down a line. */
MENU_DEF_CONST(REQ_SCR_DLINE);
/* Scroll up a page. */
MENU_DEF_CONST(REQ_SCR_UPAGE);
/* Scroll down a page. */
MENU_DEF_CONST(REQ_SCR_DPAGE);
/* Move to the first item. */
MENU_DEF_CONST(REQ_FIRST_ITEM);
/* Move to the last item. */
MENU_DEF_CONST(REQ_LAST_ITEM);
/* Move to the next item. */
MENU_DEF_CONST(REQ_NEXT_ITEM);
/* Move to the previous item. */
MENU_DEF_CONST(REQ_PREV_ITEM);
/* Select/deselect an item. */
MENU_DEF_CONST(REQ_TOGGLE_ITEM);
/* Clear the menu pattern buffer. */
MENU_DEF_CONST(REQ_CLEAR_PATTERN);
/* Delete the previous character from the pattern buffer. */
MENU_DEF_CONST(REQ_BACK_PATTERN);
/* Move to the next item matching the pattern match. */
MENU_DEF_CONST(REQ_NEXT_MATCH);
/* Move to the previous item matching the pattern match. */
MENU_DEF_CONST(REQ_PREV_MATCH);
}
/*
* Item options - mitem_opts(3x) man page
*/
void init_item_opts_constants(void)
{
/* Item may be selected during menu processing. */
MENU_DEF_CONST(O_SELECTABLE);
}
/*
* Menu options - menu_opts(3x) man page
*/
void init_menu_opts_constants(void)
{
/* Only one item can be selected for this menu. */
MENU_DEF_CONST(O_ONEVALUE);
/* Display the item descriptions when the menu is posted. */
MENU_DEF_CONST(O_SHOWDESC);
/* Display the menu in row-major order. */
MENU_DEF_CONST(O_ROWMAJOR);
/* Ignore the case when pattern-matching. */
MENU_DEF_CONST(O_IGNORECASE);
/* Move the cursor to within the item name while pattern-matching. */
MENU_DEF_CONST(O_SHOWMATCH);
/* Don’t wrap around next-item and previous-item, requests to the other end of the menu. */
MENU_DEF_CONST(O_NONCYCLIC);
}
/*
* ITEM wrapper
*/
static VALUE wrap_item(ITEM *item)
{
if (item == 0) return Qnil;
{
VALUE items_hash = rb_iv_get(mMenu, "@items_hash");
VALUE item_address = INT2NUM((long)(item));
VALUE rb_item = rb_hash_aref(items_hash, item_address);
if (rb_item == Qnil)
{
rb_item = Data_Wrap_Struct(cITEM, 0, 0, item);
rb_iv_set(rb_item, "@destroyed", Qfalse);
rb_hash_aset(items_hash, item_address, rb_item);
}
return rb_item;
}
}
static ITEM *get_item(VALUE rb_item)
{
ITEM *item;
if (rb_item == Qnil) return 0;
if (rb_iv_get(rb_item, "@destroyed") == Qtrue)
{
rb_raise(rb_eRuntimeError, "Attempt to access a destroyed item");
return 0;
}
Data_Get_Struct(rb_item, ITEM, item);
return item;
}
/*
* MENU wrapper
*/
static VALUE wrap_menu(MENU *menu)
{
if (menu == 0) return Qnil;
{
VALUE menus_hash = rb_iv_get(mMenu, "@menus_hash");
VALUE menu_address = INT2NUM((long)(menu));
VALUE rb_menu = rb_hash_aref(menus_hash, menu_address);
if (rb_menu == Qnil)
{
rb_menu = Data_Wrap_Struct(cMENU, 0, 0, menu);
rb_iv_set(rb_menu, "@destroyed", Qfalse);
rb_hash_aset(menus_hash, menu_address, rb_menu);
}
return rb_menu;
}
}
static MENU *get_menu(VALUE rb_menu)
{
MENU *menu;
if (rb_menu == Qnil) return 0;
if (rb_iv_get(rb_menu, "@destroyed") == Qtrue)
{
rb_raise(rb_eRuntimeError, "Attempt to access a destroyed menu");
return 0;
}
Data_Get_Struct(rb_menu, MENU, menu);
return menu;
}
/*
* 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 ITEM_INIT_HOOK 0
#define ITEM_TERM_HOOK 1
#define MENU_INIT_HOOK 2
#define MENU_TERM_HOOK 3
#define PROC_HASHES_COUNT 4
static VALUE get_proc_hash(int hook)
{
VALUE arr = rb_iv_get(mMenu, "@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_address = INT2NUM((long)(owner));
VALUE proc_hash = get_proc_hash(hook);
VALUE proc = rb_hash_aref(proc_hash, owner_address);
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);
}
}
/*
* Menu creation/destruction functions - menu_new(3X) man page
*/
static VALUE rbncurs_m_new_menu(VALUE dummy, VALUE rb_item_array)
{
long n = rbncurs_array_length(rb_item_array);
/* Will ncurses free this array? If not, must do it after calling free_menu(). */
ITEM **items = ALLOC_N(ITEM*, (n+1));
long i;
for (i=0; i<n; i++)
items[i] = get_item(rb_ary_entry(rb_item_array, i));
items[n] = NULL;
return wrap_menu(new_menu(items));
}
static VALUE rbncurs_c_free_menu(VALUE rb_menu)
{
VALUE menus_hash = rb_iv_get(mMenu, "@menus_hash");
MENU *menu = get_menu(rb_menu);
VALUE menu_address = INT2NUM((long)(menu));
rb_funcall(menus_hash, rb_intern("delete"), 1, menu_address);
rb_iv_set(rb_menu, "@destroyed", Qtrue);
return INT2NUM(free_menu(menu));
}
static VALUE rbncurs_m_free_menu(VALUE dummy, VALUE rb_menu)
{ return rbncurs_c_free_menu(rb_menu); }
/*
* Menu post/unpost functions - menu_post(3X) man page
*/
static VALUE rbncurs_c_post_menu(VALUE rb_menu)
{
MENU *menu = get_menu(rb_menu);
return INT2NUM(post_menu(menu));
}
static VALUE rbncurs_m_post_menu(VALUE dummy, VALUE rb_menu)
{ return rbncurs_c_post_menu(rb_menu); }
static VALUE rbncurs_c_unpost_menu(VALUE rb_menu)
{
MENU *menu = get_menu(rb_menu);
return INT2NUM(unpost_menu(menu));
}
static VALUE rbncurs_m_unpost_menu(VALUE dummy, VALUE rb_menu)
{ return rbncurs_c_unpost_menu(rb_menu); }
/*
* Menu driver - menu_driver(3X) man page
*/
static VALUE rbncurs_c_menu_driver(VALUE rb_menu, VALUE c)
{
MENU *menu = get_menu(rb_menu);
return INT2NUM(menu_driver(menu, NUM2INT(c)));
}
static VALUE rbncurs_m_menu_driver(VALUE dummy, VALUE rb_menu, VALUE c)
{ return rbncurs_c_menu_driver(rb_menu, c); }
/*
* Current menu item get/set functions - mitem_current(3X) man page
*/
static VALUE rbncurs_c_current_item(VALUE rb_menu)
{
MENU *menu = get_menu(rb_menu);
return wrap_item(current_item(menu));
}
static VALUE rbncurs_m_current_item(VALUE dummy, VALUE rb_menu)
{ return rbncurs_c_current_item(rb_menu); }
static VALUE rbncurs_c_set_current_item(VALUE rb_menu, VALUE rb_item)
{
MENU *menu = get_menu(rb_menu);
ITEM *item = get_item(rb_item);
return INT2NUM(set_current_item(menu, item));
}
static VALUE rbncurs_m_set_current_item(VALUE dummy, VALUE rb_menu, VALUE rb_item)
{ return rbncurs_c_set_current_item(rb_menu, rb_item); }
static VALUE rbncurs_c_top_row(VALUE rb_menu)
{
MENU *menu = get_menu(rb_menu);
return INT2NUM(top_row(menu));
}
static VALUE rbncurs_m_top_row(VALUE dummy, VALUE rb_menu)
{ return rbncurs_c_top_row(rb_menu); }
static VALUE rbncurs_c_set_top_row(VALUE rb_menu, VALUE n)
{
MENU *menu = get_menu(rb_menu);
return INT2NUM(set_top_row(menu, NUM2INT(n)));
}
static VALUE rbncurs_m_set_top_row(VALUE dummy, VALUE rb_menu, VALUE rb_item)
{ return rbncurs_c_set_top_row(rb_menu, rb_item); }
static VALUE rbncurs_c_item_index(VALUE rb_item)
{
ITEM *item = get_item(rb_item);
return INT2NUM(item_index(item));
}
static VALUE rbncurs_m_item_index(VALUE dummy, VALUE rb_item)
{ return rbncurs_c_item_index(rb_item); }
/*
* Item creation/destruction functions - mitem_new(3X) man page
*/
static VALUE rbncurs_m_new_item(VALUE dummy, VALUE name, VALUE description)
{ return wrap_item(new_item(STR2CSTR(name), STR2CSTR(description))); }
static VALUE rbncurs_c_free_item(VALUE rb_item)
{
VALUE items_hash = rb_iv_get(mMenu, "@items_hash");
ITEM *item = get_item(rb_item);
VALUE item_address = INT2NUM((long)(item));
rb_funcall(items_hash, rb_intern("delete"), 1, item_address);
rb_iv_set(rb_item, "@destroyed", Qtrue);
return INT2NUM(free_item(item));
}
static VALUE rbncurs_m_free_item(VALUE dummy, VALUE rb_item)
{ return rbncurs_c_free_item(rb_item); }
/*
* Item-menu connection make/break functions - menu_items(3X) man page
*/
static VALUE rbncurs_c_set_menu_items(VALUE rb_menu, VALUE rb_item_array)
{
long n = rbncurs_array_length(rb_item_array);
/* If ncurses does not free memory used by the previous array of strings, */
/* we will have to do it now. */
ITEM **items = ALLOC_N(ITEM*, (n+1));
long i;
MENU *menu = NULL;
for (i=0; i<n; i++)
items[i] = get_item(rb_ary_entry(rb_item_array, i));
items[n] = NULL;
menu = get_menu(rb_menu);
return INT2NUM(set_menu_items(menu, items));
}
static VALUE rbncurs_m_set_menu_items(VALUE dummy, VALUE rb_menu, VALUE rb_item_array)
{ return rbncurs_c_set_menu_items(rb_menu, rb_item_array); }
static VALUE rbncurs_c_menu_items(VALUE rb_menu)
{
MENU *menu = get_menu(rb_menu);
ITEM **items = menu_items(menu);
VALUE arr = Qundef;
int i;
if (items == NULL)
rb_raise(rb_eRuntimeError, "Error retrieving menu items");
arr = rb_ary_new();
i=0;
while (items[i] != NULL)
rb_ary_push(arr, wrap_item(items[i++]));
return arr;
}
static VALUE rbncurs_m_menu_items(VALUE dummy, VALUE rb_menu)
{ return rbncurs_c_menu_items(rb_menu); }
static VALUE rbncurs_c_item_count(VALUE rb_menu)
{
MENU *menu = get_menu(rb_menu);
return INT2NUM(item_count(menu));
}
static VALUE rbncurs_m_item_count(VALUE dummy, VALUE rb_menu)
{ return rbncurs_c_item_count(rb_menu); }
/*
* Item/menu hook get/set functions - menu_hook(3X) man page
*/
static void item_init_hook(MENU *menu)
{
/* Find the Proc object associated with this menu */
VALUE proc = get_proc(menu, ITEM_INIT_HOOK);
if (proc != Qnil)
{
VALUE rb_menu = wrap_menu(menu);
rb_funcall(proc, rb_intern("call"), 1, rb_menu);
}
}
static VALUE rbncurs_c_set_item_init(VALUE rb_menu, VALUE proc)
{
MENU *menu = NULL;
if (!rb_obj_is_kind_of(rb_menu, cMENU))
rb_raise(rb_eArgError, "arg1 must be a MENU object");
if (!rb_obj_is_kind_of(proc, rb_cProc))
rb_raise(rb_eArgError, "arg2 must be a Proc object");
menu = get_menu(rb_menu);
reg_proc(menu, ITEM_INIT_HOOK, proc);
if (proc != Qnil)
return INT2NUM(set_item_init(menu, item_init_hook));
else
return INT2NUM(set_item_init(menu, NULL));
}
static VALUE rbncurs_m_set_item_init(VALUE dummy, VALUE rb_menu, VALUE proc)
{ return rbncurs_c_set_item_init(rb_menu, proc); }
static VALUE rbncurs_c_item_init(VALUE rb_menu)
{
MENU *menu = get_menu(rb_menu);
return get_proc(menu, ITEM_INIT_HOOK);
}
static VALUE rbncurs_m_item_init(VALUE dummy, VALUE rb_menu)
{ return rbncurs_c_item_init(rb_menu); }
static void item_term_hook(MENU *menu)
{
/* Find the Proc object associated with this menu */
VALUE proc = get_proc(menu, ITEM_TERM_HOOK);
if (proc != Qnil)
{
VALUE rb_menu = wrap_menu(menu);
rb_funcall(proc, rb_intern("call"), 1, rb_menu);
}
}
static VALUE rbncurs_c_set_item_term(VALUE rb_menu, VALUE proc)
{
MENU *menu = NULL;
if (!rb_obj_is_kind_of(rb_menu, cMENU))
rb_raise(rb_eArgError, "arg1 must be a MENU object");
if (!rb_obj_is_kind_of(proc, rb_cProc))
rb_raise(rb_eArgError, "arg2 must be a Proc object");
menu = get_menu(rb_menu);
reg_proc(menu, ITEM_TERM_HOOK, proc);
if (proc != Qnil)
return INT2NUM(set_item_term(menu, item_term_hook));
else
return INT2NUM(set_item_term(menu, NULL));
}
static VALUE rbncurs_m_set_item_term(VALUE dummy, VALUE rb_menu, VALUE proc)
{ return rbncurs_c_set_item_term(rb_menu, proc); }
static VALUE rbncurs_c_item_term(VALUE rb_menu)
{
MENU *menu = get_menu(rb_menu);
return get_proc(menu, ITEM_TERM_HOOK);
}
static VALUE rbncurs_m_item_term(VALUE dummy, VALUE rb_menu)
{ return rbncurs_c_item_term(rb_menu); }
static void menu_init_hook(MENU *menu)
{
/* Find the Proc object associated with this menu */
VALUE proc = get_proc(menu, MENU_INIT_HOOK);
if (proc != Qnil)
{
VALUE rb_menu = wrap_menu(menu);
rb_funcall(proc, rb_intern("call"), 1, rb_menu);
}
}
static VALUE rbncurs_c_set_menu_init(VALUE rb_menu, VALUE proc) {
MENU *menu = NULL;
if (!rb_obj_is_kind_of(rb_menu, cMENU))
rb_raise(rb_eArgError, "arg1 must be a MENU object");
if (!rb_obj_is_kind_of(proc, rb_cProc))
rb_raise(rb_eArgError, "arg2 must be a Proc object");
menu = get_menu(rb_menu);
reg_proc(menu, MENU_INIT_HOOK, proc);
if (proc != Qnil)
return INT2NUM(set_menu_init(menu, menu_init_hook));
else
return INT2NUM(set_menu_init(menu, NULL));
}
static VALUE rbncurs_m_set_menu_init(VALUE dummy, VALUE rb_menu, VALUE proc)
{ return rbncurs_c_set_menu_init(rb_menu, proc); }
static VALUE rbncurs_c_menu_init(VALUE rb_menu)
{
MENU *menu = get_menu(rb_menu);
return get_proc(menu, MENU_INIT_HOOK);
}
static VALUE rbncurs_m_menu_init(VALUE dummy, VALUE rb_menu)
{ return rbncurs_c_menu_init(rb_menu); }
static void menu_term_hook(MENU *menu)
{
/* Find the Proc object associated with this menu */
VALUE proc = get_proc(menu, MENU_TERM_HOOK);
if (proc != Qnil)
{
VALUE rb_menu = wrap_menu(menu);
rb_funcall(proc, rb_intern("call"), 1, rb_menu);
}
}
static VALUE rbncurs_c_set_menu_term(VALUE rb_menu, VALUE proc)
{
MENU *menu = NULL;
if (!rb_obj_is_kind_of(rb_menu, cMENU))
rb_raise(rb_eArgError, "arg1 must be a MENU object");
if (!rb_obj_is_kind_of(proc, rb_cProc))
rb_raise(rb_eArgError, "arg2 must be a Proc object");
menu = get_menu(rb_menu);
reg_proc(menu, MENU_TERM_HOOK, proc);
if (proc != Qnil)
return INT2NUM(set_menu_term(menu, menu_term_hook));
else
return INT2NUM(set_menu_term(menu, NULL));
}
static VALUE rbncurs_m_set_menu_term(VALUE dummy, VALUE rb_menu, VALUE proc)
{ return rbncurs_c_set_menu_term(rb_menu, proc); }
static VALUE rbncurs_c_menu_term(VALUE rb_menu)
{
MENU *menu = get_menu(rb_menu);
return get_proc(menu, MENU_TERM_HOOK);
}
static VALUE rbncurs_m_menu_term(VALUE dummy, VALUE rb_menu)
{ return rbncurs_c_menu_term(rb_menu); }
/*
* Item option get/set functions - mitem_opts(3X) man page
*/
static VALUE rbncurs_c_set_item_opts(VALUE rb_item, VALUE opts)
{
ITEM *item = get_item(rb_item);
return INT2NUM(set_item_opts(item, NUM2INT(opts)));
}
static VALUE rbncurs_m_set_item_opts(VALUE dummy, VALUE rb_item, VALUE opts)
{ return rbncurs_c_set_item_opts(rb_item, opts); }
static VALUE rbncurs_c_item_opts_on(VALUE rb_item, VALUE opts)
{
ITEM *item = get_item(rb_item);
return INT2NUM(item_opts_on(item, NUM2INT(opts)));
}
static VALUE rbncurs_m_item_opts_on(VALUE dummy, VALUE rb_item, VALUE opts)
{ return rbncurs_c_item_opts_on(rb_item, opts); }
static VALUE rbncurs_c_item_opts_off(VALUE rb_item, VALUE opts)
{
ITEM *item = get_item(rb_item);
return INT2NUM(item_opts_off(item, NUM2INT(opts)));
}
static VALUE rbncurs_m_item_opts_off(VALUE dummy, VALUE rb_item, VALUE opts)
{ return rbncurs_c_item_opts_off(rb_item, opts); }
static VALUE rbncurs_c_item_opts(VALUE rb_item)
{
ITEM *item = get_item(rb_item);
return INT2NUM(item_opts(item));
}
static VALUE rbncurs_m_item_opts(VALUE dummy, VALUE rb_item)
{ return rbncurs_c_item_opts(rb_item); }
/*
* Menu option get/set functions - menu_opts(3X) man page
*/
static VALUE rbncurs_c_set_menu_opts(VALUE rb_menu, VALUE opts)
{
MENU *menu = get_menu(rb_menu);
return INT2NUM(set_menu_opts(menu, NUM2INT(opts)));
}
static VALUE rbncurs_m_set_menu_opts(VALUE dummy, VALUE rb_menu, VALUE opts)
{ return rbncurs_c_set_menu_opts(rb_menu, opts); }
static VALUE rbncurs_c_menu_opts_on(VALUE rb_menu, VALUE opts)
{
MENU *menu = get_menu(rb_menu);
return INT2NUM(menu_opts_on(menu, NUM2INT(opts)));
}
static VALUE rbncurs_m_menu_opts_on(VALUE dummy, VALUE rb_menu, VALUE opts)
{ return rbncurs_c_menu_opts_on(rb_menu, opts); }
static VALUE rbncurs_c_menu_opts_off(VALUE rb_menu, VALUE opts)
{
MENU *menu = get_menu(rb_menu);
return INT2NUM(menu_opts_off(menu, NUM2INT(opts)));
}
static VALUE rbncurs_m_menu_opts_off(VALUE dummy, VALUE rb_menu, VALUE opts)
{ return rbncurs_c_menu_opts_off(rb_menu, opts); }
static VALUE rbncurs_c_menu_opts(VALUE rb_menu)
{
MENU *menu = get_menu(rb_menu);
return INT2NUM(menu_opts(menu));
}
static VALUE rbncurs_m_menu_opts(VALUE dummy, VALUE rb_menu)
{ return rbncurs_c_menu_opts(rb_menu); }
/*
* Printable menu request name handling functions - menu_requestname(3X) man page
*/
static VALUE rbncurs_c_menu_request_name(VALUE request)
{
return rb_str_new2(menu_request_name(NUM2INT(request)));
}
static VALUE rbncurs_m_menu_request_name(VALUE dummy, VALUE request)
{ return rbncurs_c_menu_request_name(request); }
static VALUE rbncurs_c_menu_request_by_name(VALUE name)
{
return INT2NUM(menu_request_by_name(STR2CSTR(name)));
}
static VALUE rbncurs_m_menu_request_by_name(VALUE dummy, VALUE name)
{ return rbncurs_c_menu_request_by_name(name); }
/*
* (Sub)window association make/break functions - menu_win(3X) man page
*/
static VALUE rbncurs_c_set_menu_win(VALUE rb_menu, VALUE rb_win)
{
MENU *menu = get_menu(rb_menu);
WINDOW *win = get_window(rb_win);
return INT2NUM(set_menu_win(menu, win));
}
static VALUE rbncurs_m_set_menu_win(VALUE dummy, VALUE rb_menu, VALUE rb_win)
{ return rbncurs_c_set_menu_win(rb_menu, rb_win); }
static VALUE rbncurs_c_menu_win(VALUE rb_menu)
{
MENU *menu = get_menu(rb_menu);
return wrap_window(menu_win(menu));
}
static VALUE rbncurs_m_menu_win(VALUE dummy, VALUE rb_menu)
{ return rbncurs_c_menu_win(rb_menu); }
static VALUE rbncurs_c_set_menu_sub(VALUE rb_menu, VALUE rb_sub)
{
MENU *menu = get_menu(rb_menu);
WINDOW *win = get_window(rb_sub);
return INT2NUM(set_menu_sub(menu, win));
}
static VALUE rbncurs_m_set_menu_sub(VALUE dummy, VALUE rb_menu, VALUE rb_sub)
{ return rbncurs_c_set_menu_sub(rb_menu, rb_sub); }
static VALUE rbncurs_c_menu_sub(VALUE rb_menu)
{
MENU *menu = get_menu(rb_menu);
return wrap_window(menu_sub(menu));
}
static VALUE rbncurs_m_menu_sub(VALUE dummy, VALUE rb_menu)
{ return rbncurs_c_menu_sub(rb_menu); }
static VALUE rbncurs_c_scale_menu(VALUE rb_menu, VALUE rows, VALUE columns)
{
MENU *menu = get_menu(rb_menu);
if (rb_obj_is_instance_of(rows, rb_cArray) != Qtrue ||
rb_obj_is_instance_of(columns, rb_cArray) != Qtrue)
{
rb_raise(rb_eArgError, "rows and columns arguments must be empty Arrays");
return Qnil;
}
else
{
int vals[2] = {0,0};
int result = scale_menu(menu, &vals[0],&vals[1]);
rb_ary_push(rows, INT2NUM(vals[0]));
rb_ary_push(columns, INT2NUM(vals[1]));
return INT2NUM(result);
}
}
static VALUE rbncurs_m_scale_menu(VALUE dummy, VALUE rb_menu, VALUE rows, VALUE columns)
{ return rbncurs_c_scale_menu(rb_menu, rows, columns); }
/*
* Menu cursor positioning functions - menu_cursor(3X) man page
*/
static VALUE rbncurs_c_pos_menu_cursor(VALUE rb_menu)
{
MENU *menu = get_menu(rb_menu);
return INT2NUM(pos_menu_cursor(menu));
}
static VALUE rbncurs_m_pos_menu_cursor(VALUE dummy, VALUE rb_menu)
{ return rbncurs_c_pos_menu_cursor(rb_menu); }
/*
* Item name/description retrieval functions - mitem_name(3X) man page
*/
static VALUE rbncurs_c_item_name(VALUE rb_item)
{
ITEM *item = get_item(rb_item);
return rb_str_new2(item_name(item));
}
static VALUE rbncurs_m_item_name(VALUE dummy, VALUE rb_item)
{ return rbncurs_c_item_name(rb_item); }
static VALUE rbncurs_c_item_description(VALUE rb_item)
{
ITEM *item = get_item(rb_item);
return rb_str_new2(item_description(item));
}
static VALUE rbncurs_m_item_description(VALUE dummy, VALUE rb_item)
{ return rbncurs_c_item_description(rb_item); }
/*
* Item value get/set functions - mitem_value(3X) man page
*/
static VALUE rbncurs_c_set_item_value(VALUE rb_item, VALUE value)
{
ITEM *item = get_item(rb_item);
return INT2NUM(set_item_value(item, RTEST(value)));
}
static VALUE rbncurs_m_set_item_value(VALUE dummy, VALUE rb_item, VALUE value)
{ return rbncurs_c_set_item_value(rb_item, value); }
static VALUE rbncurs_c_item_value(VALUE rb_item)
{
ITEM *item = get_item(rb_item);
return (item_value(item)) ? Qtrue: Qfalse;
}
static VALUE rbncurs_m_item_value(VALUE dummy, VALUE rb_item)
{ return rbncurs_c_item_value(rb_item); }
/*
* Item visibility retrieval function - mitem_visible(3X) man page
*/
static VALUE rbncurs_c_item_visible(VALUE rb_item)
{
ITEM *item = get_item(rb_item);
return (item_visible(item)) ? Qtrue: Qfalse;
}
static VALUE rbncurs_m_item_visible(VALUE dummy, VALUE rb_item)
{ return rbncurs_c_item_visible(rb_item); }
/*
* Menu attribute get/set functions - menu_attributes(3X) man page
*/
static VALUE rbncurs_c_set_menu_fore(VALUE rb_menu, VALUE attr)
{
MENU *menu = get_menu(rb_menu);
return INT2NUM(set_menu_fore(menu, NUM2ULONG(attr)));
}
static VALUE rbncurs_m_set_menu_fore(VALUE dummy, VALUE rb_menu, VALUE attr)
{ return rbncurs_c_set_menu_fore(rb_menu, attr); }
static VALUE rbncurs_c_menu_fore(VALUE rb_menu)
{
MENU *menu = get_menu(rb_menu);
return ULONG2NUM(menu_fore(menu));
}
static VALUE rbncurs_m_menu_fore(VALUE dummy, VALUE rb_menu)
{ return rbncurs_c_menu_fore(rb_menu); }
static VALUE rbncurs_c_set_menu_back(VALUE rb_menu, VALUE attr)
{
MENU *menu = get_menu(rb_menu);
return INT2NUM(set_menu_back(menu, NUM2ULONG(attr)));
}
static VALUE rbncurs_m_set_menu_back(VALUE dummy, VALUE rb_menu, VALUE attr)
{ return rbncurs_c_set_menu_back(rb_menu, attr); }
static VALUE rbncurs_c_menu_back(VALUE rb_menu)
{
MENU *menu = get_menu(rb_menu);
return ULONG2NUM(menu_back(menu));
}
static VALUE rbncurs_m_menu_back(VALUE dummy, VALUE rb_menu)
{ return rbncurs_c_menu_back(rb_menu); }
static VALUE rbncurs_c_set_menu_grey(VALUE rb_menu, VALUE attr)
{
MENU *menu = get_menu(rb_menu);
return INT2NUM(set_menu_grey(menu, NUM2ULONG(attr)));
}
static VALUE rbncurs_m_set_menu_grey(VALUE dummy, VALUE rb_menu, VALUE attr)
{ return rbncurs_c_set_menu_grey(rb_menu, attr); }
static VALUE rbncurs_c_menu_grey(VALUE rb_menu)
{
MENU *menu = get_menu(rb_menu);
return ULONG2NUM(menu_grey(menu));
}
static VALUE rbncurs_m_menu_grey(VALUE dummy, VALUE rb_menu)
{ return rbncurs_c_menu_grey(rb_menu); }
static VALUE rbncurs_c_set_menu_pad(VALUE rb_menu, VALUE pad)
{
MENU *menu = get_menu(rb_menu);
return INT2NUM(set_menu_pad(menu, NUM2INT(pad)));
}
static VALUE rbncurs_m_set_menu_pad(VALUE dummy, VALUE rb_menu, VALUE pad)
{ return rbncurs_c_set_menu_pad(rb_menu, pad); }
static VALUE rbncurs_c_menu_pad(VALUE rb_menu)
{
MENU *menu = get_menu(rb_menu);
return INT2NUM(menu_pad(menu));
}
static VALUE rbncurs_m_menu_pad(VALUE dummy, VALUE rb_menu)
{ return rbncurs_c_menu_pad(rb_menu); }
/*
* Menu format get/set functions - menu_format(3X) man page
*/
static VALUE rbncurs_c_set_menu_format(VALUE rb_menu, VALUE rows, VALUE cols)
{
MENU *menu = get_menu(rb_menu);
return INT2NUM(set_menu_format(menu, NUM2INT(rows), NUM2INT(cols)));
}
static VALUE rbncurs_m_set_menu_format(VALUE dummy, VALUE rb_menu, VALUE rows, VALUE cols)
{ return rbncurs_c_set_menu_format(rb_menu, rows, cols); }
static VALUE rbncurs_c_menu_format(VALUE rb_menu, VALUE rows, VALUE cols)
{
if (rb_obj_is_instance_of(rows, rb_cArray) != Qtrue ||
rb_obj_is_instance_of(cols, rb_cArray) != Qtrue)
{
rb_raise(rb_eArgError, "rows and cols arguments must be empty Arrays");
return Qnil;
}
else
{
MENU *menu = get_menu(rb_menu);
int vals[2] = {0,0};
menu_format(menu, &vals[0], &vals[1]);
rb_ary_push(rows, INT2NUM(vals[0]));
rb_ary_push(cols, INT2NUM(vals[1]));
return Qnil;
}
}
static VALUE rbncurs_m_menu_format(VALUE dummy, VALUE rb_menu, VALUE rows, VALUE cols)
{ return rbncurs_c_menu_format(rb_menu, rows, cols); }
/*
* Menu mark get/set functions - menu_mark(3X) man page
*/
static VALUE rbncurs_c_set_menu_mark(VALUE rb_menu, VALUE value)
{
MENU *menu = get_menu(rb_menu);
return INT2NUM(set_menu_mark(menu, STR2CSTR(value)));
}
static VALUE rbncurs_m_set_menu_mark(VALUE dummy, VALUE rb_field, VALUE value)
{ return rbncurs_c_set_menu_mark(rb_field, value); }
static VALUE rbncurs_c_menu_mark(VALUE rb_menu)
{
MENU *menu = get_menu(rb_menu);
return rb_str_new2(menu_mark(menu));
}
static VALUE rbncurs_m_menu_mark(VALUE dummy, VALUE rb_menu)
{ return rbncurs_c_menu_mark(rb_menu); }
/*
* Menu pattern get/set functions - menu_pattern(3X) man page
*/
static VALUE rbncurs_c_set_menu_pattern(VALUE rb_menu, VALUE pattern)
{
MENU *menu = get_menu(rb_menu);
return INT2NUM(set_menu_pattern(menu, STR2CSTR(pattern)));
}
static VALUE rbncurs_m_set_menu_pattern(VALUE dummy, VALUE rb_menu, VALUE pattern)
{ return rbncurs_c_set_menu_pattern(rb_menu, pattern); }
static VALUE rbncurs_c_menu_pattern(VALUE rb_menu)
{
MENU *menu = get_menu(rb_menu);
return rb_str_new2(menu_pattern(menu));
}
static VALUE rbncurs_m_menu_pattern(VALUE dummy, VALUE rb_menu)
{ return rbncurs_c_menu_pattern(rb_menu); }
/*
* Menu spacing get/set functions - menu_spacing(3X) man page
*/
static VALUE rbncurs_c_set_menu_spacing(VALUE rb_menu, VALUE spc_description,
VALUE spc_rows, VALUE spc_cols)
{
MENU *menu = get_menu(rb_menu);
return INT2NUM(set_menu_spacing(menu, NUM2INT(spc_description),
NUM2INT(spc_rows), NUM2INT(spc_cols)));
}
static VALUE rbncurs_m_set_menu_spacing(VALUE dummy, VALUE rb_menu, VALUE spc_description,
VALUE spc_rows, VALUE spc_cols)
{ return rbncurs_c_set_menu_spacing(rb_menu, spc_description, spc_rows, spc_cols); }
static VALUE rbncurs_c_menu_spacing(VALUE rb_menu, VALUE spc_description,
VALUE spc_rows, VALUE spc_cols)
{
if (rb_obj_is_instance_of(spc_description, rb_cArray) != Qtrue ||
rb_obj_is_instance_of(spc_rows, rb_cArray) != Qtrue ||
rb_obj_is_instance_of(spc_cols, rb_cArray) != Qtrue)
{
rb_raise(rb_eArgError, "spc_description, spc_rows, and spc_cols arguments must be empty Arrays");
return Qnil;
}
else
{
MENU *menu = get_menu(rb_menu);
int vals[3] = {0,0,0};
int result = menu_spacing(menu, &vals[0], &vals[1], &vals[2]);
rb_ary_push(spc_description, INT2NUM(vals[0]));
rb_ary_push(spc_rows, INT2NUM(vals[1]));
rb_ary_push(spc_cols, INT2NUM(vals[2]));
return INT2NUM(result);
}
}
static VALUE rbncurs_m_menu_spacing(VALUE dummy, VALUE rb_menu, VALUE spc_description,
VALUE spc_rows, VALUE spc_cols)
{ return rbncurs_c_menu_spacing(rb_menu, spc_description, spc_rows, spc_cols); }
/*
* Menu initialization function
*/
void init_menu(void)
{
mMenu = rb_define_module_under(mNcurses, "Menu");
MENU_SNG_FUNC(current_item, 1);
MENU_SNG_FUNC(free_item, 1);
MENU_SNG_FUNC(free_menu, 1);
MENU_SNG_FUNC(item_count, 1);
MENU_SNG_FUNC(item_description, 1);
MENU_SNG_FUNC(item_index, 1);
MENU_SNG_FUNC(item_init, 1);
MENU_SNG_FUNC(item_name, 1);