Class: Curses::Menu

Inherits:
Object
  • Object
show all
Defined in:
ext/curses/curses.c,
lib/curses.rb

Instance Method Summary collapse

Constructor Details

#initialize(items) ⇒ Object

call-seq:

new(items)

Construct a new Curses::Menu.



3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
# File 'ext/curses/curses.c', line 3331

static VALUE
menu_initialize(VALUE obj, VALUE items)
{
    struct menudata *menup;
    ITEM **menu_items;
    int i;
    ID id_new;

    Check_Type(items, T_ARRAY);
    curses_init_screen(Qnil);
    TypedData_Get_Struct(obj, struct menudata, &menudata_type, menup);
    if (menup->menu) {
	rb_raise(rb_eRuntimeError, "already initialized menu");
    }
    menup->items = rb_ary_new();
    menu_items = ALLOC_N(ITEM *, RARRAY_LEN(items) + 1);
    CONST_ID(id_new, "new");
    for (i = 0; i < RARRAY_LEN(items); i++) {
	VALUE item = RARRAY_AREF(items, i);
	struct itemdata *itemp;

	if (RB_TYPE_P(item, T_ARRAY)) {
	    item = rb_apply(cItem, id_new, item);
	}
	GetITEM(item, itemp);
	menu_items[i] = itemp->item;
	rb_ary_push(menup->items, item);
    }
    menu_items[RARRAY_LEN(items)] = NULL;
    menup->menu = new_menu(menu_items);
    if (menup->menu == NULL) {
	check_curses_error(errno);
    }

    return obj;
}

Instance Method Details

#backObject

call-seq:

back

Get the background attribute of menu.



3740
3741
3742
3743
3744
3745
3746
3747
3748
# File 'ext/curses/curses.c', line 3740

static VALUE
menu_get_back(VALUE obj)
{
    struct menudata *menup;

    GetMENU(obj, menup);

    return CHTYPE2NUM(menu_back(menup->menu));
}

#back=(attr) ⇒ Object

call-seq:

set_back(attr)

Get the background attribute of menu.



3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
# File 'ext/curses/curses.c', line 3721

static VALUE
menu_set_back(VALUE obj, VALUE attr)
{
    struct menudata *menup;

    GetMENU(obj, menup);
    CHTYPE2NUM(set_menu_back(menup->menu, NUM2CHTYPE(attr)));

    return attr;
}

#back_patternObject



76
77
78
# File 'lib/curses.rb', line 76

def back_pattern
  driver(Curses::REQ_BACK_PATTERN)
end

#clear_patternObject



72
73
74
# File 'lib/curses.rb', line 72

def clear_pattern
  driver(Curses::REQ_CLEAR_PATTERN)
end

#current_itemObject

call-seq:

current_item

Returns the current item.



3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
# File 'ext/curses/curses.c', line 3521

static VALUE
menu_get_current_item(VALUE obj)
{
    struct menudata *menup;
    ITEM *item;

    GetMENU(obj, menup);
    item = current_item(menup->menu);
    if (item == NULL) {
	return Qnil;
    }
    return item_new(item);
}

#current_item=(item) ⇒ Object

call-seq:

current_item=(item)

Sets the current item.



3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
# File 'ext/curses/curses.c', line 3543

static VALUE
menu_set_current_item(VALUE obj, VALUE item)
{
    struct menudata *menup;
    struct itemdata *itemp;

    GetMENU(obj, menup);
    GetITEM(item, itemp);
    set_current_item(menup->menu, itemp->item);
    return item;
}

#down_itemObject



32
33
34
# File 'lib/curses.rb', line 32

def down_item
  driver(Curses::REQ_DOWN_ITEM)
end

#driver(command) ⇒ Object

call-seq:

driver(command)

Perform the command on the menu.



3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
# File 'ext/curses/curses.c', line 3418

static VALUE
menu_driver_m(VALUE obj, VALUE command)
{
    struct menudata *menup;
    int error;

    GetMENU(obj, menup);
    error = menu_driver(menup->menu, NUM2INT(command));
    check_curses_error(error);

    return obj;
}

#first_itemObject



52
53
54
# File 'lib/curses.rb', line 52

def first_item
  driver(Curses::REQ_FIRST_ITEM)
end

#foreObject

call-seq:

fore

Sets the foreground attribute of menu. This is the highlight used for selected menu items.



3703
3704
3705
3706
3707
3708
3709
3710
3711
# File 'ext/curses/curses.c', line 3703

static VALUE
menu_get_fore(VALUE obj)
{
    struct menudata *menup;

    GetMENU(obj, menup);

    return CHTYPE2NUM(menu_fore(menup->menu));
}

#fore=(attr) ⇒ Object

call-seq:

fore=(attr)

Sets the foreground attribute of menu. This is the highlight used for selected menu items.



3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
# File 'ext/curses/curses.c', line 3683

static VALUE
menu_set_fore(VALUE obj, VALUE attr)
{
    struct menudata *menup;

    GetMENU(obj, menup);
    set_menu_fore(menup->menu, NUM2CHTYPE(attr));

    return attr;
}

#formatObject

call-seq:

format

Get the maximum size of the menu.



3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
# File 'ext/curses/curses.c', line 3758

static VALUE
menu_format_m(VALUE obj)
{
    struct menudata *menup;
    int rows, cols;

    GetMENU(obj, menup);
    menu_format(menup->menu, &rows, &cols);
    return rb_assoc_new(INT2NUM(rows), INT2NUM(cols));
}

#item_countObject

call-seq:

item_count

Returns the count of items in the menu.



3439
3440
3441
3442
3443
3444
3445
3446
# File 'ext/curses/curses.c', line 3439

static VALUE
menu_item_count(VALUE obj)
{
    struct menudata *menup;

    GetMENU(obj, menup);
    return INT2NUM(item_count(menup->menu));
}

#itemsObject

call-seq:

items

Returns the items of the menu.



3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
# File 'ext/curses/curses.c', line 3456

static VALUE
menu_get_items(VALUE obj)
{
    struct menudata *menup;
    ITEM **items;
    int count, i;
    VALUE ary;

    GetMENU(obj, menup);
    items = menu_items(menup->menu);
    if (items == NULL) {
	return Qnil;
    }
    count = item_count(menup->menu);
    ary = rb_ary_new();
    for (i = 0; i < count; i++) {
	rb_ary_push(ary, item_new(items[i]));
    }
    return ary;
}

#items=(items) ⇒ Object

call-seq:

items=(items)

Returns the items of the menu.



3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
# File 'ext/curses/curses.c', line 3485

static VALUE
menu_set_items(VALUE obj, VALUE items)
{
    struct menudata *menup;
    ITEM **old_items, **new_items;
    int i, error;

    Check_Type(items, T_ARRAY);
    GetMENU(obj, menup);
    old_items = menu_items(menup->menu);
    new_items = ALLOC_N(ITEM*, RARRAY_LEN(items) + 1);
    for (i = 0; i < RARRAY_LEN(items); i++) {
	struct itemdata *itemp;
	GetITEM(RARRAY_AREF(items, i), itemp);
	new_items[i] = itemp->item;
    }
    new_items[RARRAY_LEN(items)] = NULL;
    error = set_menu_items(menup->menu, new_items);
    if (error != E_OK) {
	xfree(new_items);
	check_curses_error(error);
	return items;
    }
    xfree(old_items);
    menup->items = rb_ary_dup(items);
    return items;
}

#last_itemObject



56
57
58
# File 'lib/curses.rb', line 56

def last_item
  driver(Curses::REQ_LAST_ITEM)
end

#left_itemObject



20
21
22
# File 'lib/curses.rb', line 20

def left_item
  driver(Curses::REQ_LEFT_ITEM)
end

#markObject

call-seq:

mark

Get the Menu’s mark string



3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
# File 'ext/curses/curses.c', line 3662

static VALUE
menu_get_mark(VALUE obj)
{
    struct menudata *menup;
    const char *mark;

    GetMENU(obj, menup);
    mark = menu_mark(menup->menu);

    return rb_external_str_new_with_enc(mark, strlen(mark), terminal_encoding);
}

#mark=(mark) ⇒ Object

call-seq:

mark=(str)

Set the mark string to distinguish the selected items



3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
# File 'ext/curses/curses.c', line 3643

static VALUE
menu_set_mark(VALUE obj, VALUE mark)
{
    struct menudata *menup;

    GetMENU(obj, menup);
    set_menu_mark(menup->menu, StringValueCStr(mark));

    return obj;
}

#next_itemObject



60
61
62
# File 'lib/curses.rb', line 60

def next_item
  driver(Curses::REQ_NEXT_ITEM)
end

#next_matchObject



80
81
82
# File 'lib/curses.rb', line 80

def next_match
  driver(Curses::REQ_NEXT_MATCH)
end

#optsObject

call-seq:

opts

Get the current option bits of the menu.



3837
3838
3839
3840
3841
3842
3843
3844
# File 'ext/curses/curses.c', line 3837

static VALUE
menu_opts_m(VALUE obj)
{
    struct menudata *menup;

    GetMENU(obj, menup);
    return INT2NUM(menu_opts(menup->menu));
}

#opts_off(opts) ⇒ Object

call-seq:

opts_off(opts)

Turn off the option bits of the menu.



3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
# File 'ext/curses/curses.c', line 3817

static VALUE
menu_opts_off_m(VALUE obj, VALUE opts)
{
    struct menudata *menup;
    int error;

    GetMENU(obj, menup);
    error = menu_opts_off(menup->menu, NUM2INT(opts));
    check_curses_error(error);
    return obj;
}

#opts_on(opts) ⇒ Object

call-seq:

opts_on(opts)

Turn on the option bits of the menu.



3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
# File 'ext/curses/curses.c', line 3797

static VALUE
menu_opts_on_m(VALUE obj, VALUE opts)
{
    struct menudata *menup;
    int error;

    GetMENU(obj, menup);
    error = menu_opts_on(menup->menu, NUM2INT(opts));
    check_curses_error(error);
    return obj;
}

#postObject

call-seq:

post

Post the menu.



3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
# File 'ext/curses/curses.c', line 3376

static VALUE
menu_post(VALUE obj)
{
    struct menudata *menup;
    int error;

    GetMENU(obj, menup);
    error = post_menu(menup->menu);
    check_curses_error(error);

    return obj;
}

#prev_itemObject



64
65
66
# File 'lib/curses.rb', line 64

def prev_item
  driver(Curses::REQ_PREV_ITEM)
end

#prev_matchObject



84
85
86
# File 'lib/curses.rb', line 84

def prev_match
  driver(Curses::REQ_PREV_MATCH)
end

#right_itemObject



24
25
26
# File 'lib/curses.rb', line 24

def right_item
  driver(Curses::REQ_RIGHT_ITEM)
end

#scaleObject

call-seq:

scale

Return the minimum rows and columns required for the subwindow of the menu.



3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
# File 'ext/curses/curses.c', line 3603

static VALUE
menu_scale(VALUE obj)
{
    struct menudata *menup;
    int error, rows, columns;

    GetMENU(obj, menup);
    error = scale_menu(menup->menu, &rows, &columns);
    check_curses_error(error);
    return rb_assoc_new(INT2NUM(rows), INT2NUM(columns));
}

#scroll_down_lineObject



40
41
42
# File 'lib/curses.rb', line 40

def scroll_down_line
  driver(Curses::REQ_SCR_DLINE)
end

#scroll_down_pageObject



48
49
50
# File 'lib/curses.rb', line 48

def scroll_down_page
  driver(Curses::REQ_SCR_DPAGE)
end

#scroll_up_lineObject



36
37
38
# File 'lib/curses.rb', line 36

def scroll_up_line
  driver(Curses::REQ_SCR_ULINE)
end

#scroll_up_pageObject



44
45
46
# File 'lib/curses.rb', line 44

def scroll_up_page
  driver(Curses::REQ_SCR_UPAGE)
end

#set_format(rows, cols) ⇒ Object

call-seq:

set_format(rows, cols)

Set the maximum size of the menu.



3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
# File 'ext/curses/curses.c', line 3623

static VALUE
menu_set_format(VALUE obj, VALUE rows, VALUE cols)
{
    struct menudata *menup;
    int error;

    GetMENU(obj, menup);
    error = set_menu_format(menup->menu, NUM2INT(rows), NUM2INT(cols));
    check_curses_error(error);
    return obj;
}

#set_opts(opts) ⇒ Object

call-seq:

set_opts(opts)

Set the option bits of the menu.



3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
# File 'ext/curses/curses.c', line 3777

static VALUE
menu_set_opts(VALUE obj, VALUE opts)
{
    struct menudata *menup;
    int error;

    GetMENU(obj, menup);
    error = set_menu_opts(menup->menu, NUM2INT(opts));
    check_curses_error(error);
    return obj;
}

#set_sub(win) ⇒ Object

call-seq:

set_sub=(win)

Set the subwindow of the menu.



3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
# File 'ext/curses/curses.c', line 3583

static VALUE
menu_set_sub(VALUE obj, VALUE win)
{
    struct menudata *menup;
    struct windata *winp;

    GetMENU(obj, menup);
    GetWINDOW(win, winp);
    set_menu_sub(menup->menu, winp->window);
    return win;
}

#set_win(win) ⇒ Object

call-seq:

set_win=(win)

Set the window of the menu.



3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
# File 'ext/curses/curses.c', line 3563

static VALUE
menu_set_win(VALUE obj, VALUE win)
{
    struct menudata *menup;
    struct windata *winp;

    GetMENU(obj, menup);
    GetWINDOW(win, winp);
    set_menu_win(menup->menu, winp->window);
    return win;
}

#toggle_itemObject



68
69
70
# File 'lib/curses.rb', line 68

def toggle_item
  driver(Curses::REQ_TOGGLE_ITEM)
end

#unpostObject

call-seq:

unpost

Unpost the menu.



3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
# File 'ext/curses/curses.c', line 3397

static VALUE
menu_unpost(VALUE obj)
{
    struct menudata *menup;
    int error;

    GetMENU(obj, menup);
    error = unpost_menu(menup->menu);
    check_curses_error(error);

    return obj;
}

#up_itemObject



28
29
30
# File 'lib/curses.rb', line 28

def up_item
  driver(Curses::REQ_UP_ITEM)
end