~ubuntu-branches/ubuntu/natty/ncurses/natty

« back to all changes in this revision

Viewing changes to test/demo_menus.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-05-17 09:00:42 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070517090042-86fgxrr6j5jzagot
Tags: 5.6-0ubuntu1
* New upstream version.
  - Remove patches applied upstream: ncurses.upstream, signed-chars.
  - Update patches: debian-backspace.
* Build-depend on g++-multilib instead of lib{32,64}c*-dev-*.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
 * Copyright (c) 2005,2006 Free Software Foundation, Inc.                   *
 
3
 *                                                                          *
 
4
 * Permission is hereby granted, free of charge, to any person obtaining a  *
 
5
 * copy of this software and associated documentation files (the            *
 
6
 * "Software"), to deal in the Software without restriction, including      *
 
7
 * without limitation the rights to use, copy, modify, merge, publish,      *
 
8
 * distribute, distribute with modifications, sublicense, and/or sell       *
 
9
 * copies of the Software, and to permit persons to whom the Software is    *
 
10
 * furnished to do so, subject to the following conditions:                 *
 
11
 *                                                                          *
 
12
 * The above copyright notice and this permission notice shall be included  *
 
13
 * in all copies or substantial portions of the Software.                   *
 
14
 *                                                                          *
 
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
 
16
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
 
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
 
18
 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
 
19
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
 
20
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
 
21
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
 
22
 *                                                                          *
 
23
 * Except as contained in this notice, the name(s) of the above copyright   *
 
24
 * holders shall not be used in advertising or otherwise to promote the     *
 
25
 * sale, use or other dealings in this Software without prior written       *
 
26
 * authorization.                                                           *
 
27
 ****************************************************************************/
1
28
/*
2
 
 * $Id: demo_menus.c,v 1.13 2005/10/01 15:54:31 tom Exp $
 
29
 * $Id: demo_menus.c,v 1.20 2006/06/17 17:39:37 tom Exp $
3
30
 *
4
31
 * Demonstrate a variety of functions from the menu library.
5
32
 * Thomas Dickey - 2005/4/9
18
45
menu_format                     -
19
46
menu_grey                       -
20
47
menu_init                       -
21
 
menu_mark                       -
22
48
menu_opts                       -
23
49
menu_pad                        -
24
 
menu_pattern                    -
25
50
menu_request_by_name            -
26
51
menu_request_name               -
27
52
menu_sub                        -
65
90
#endif
66
91
 
67
92
typedef enum {
68
 
    eUnknown = -1
69
 
    ,eFile = 0
 
93
    eBanner = -1
 
94
    ,eFile
70
95
    ,eSelect
71
96
#ifdef TRACE
72
97
    ,eTrace
73
98
#endif
 
99
    ,eMAX
74
100
} MenuNo;
75
101
 
 
102
#define okMenuNo(n) (((n) > eBanner) && ((n) < eMAX))
 
103
 
76
104
#define MENU_Y  1
77
105
 
78
106
static MENU *mpBanner;
162
190
{
163
191
    int result = 0;
164
192
 
165
 
    if ((int) number >= 0) {
 
193
    if (okMenuNo(number)) {
166
194
        int spc_desc, spc_rows, spc_cols;
167
195
 
168
196
#ifdef NCURSES_VERSION
172
200
#endif
173
201
 
174
202
        /* FIXME: MENU.itemlen seems the only way to get actual width of items */
175
 
        result = number * (mpBanner->itemlen + spc_rows);
 
203
        result = (number - (eBanner + 1)) * (mpBanner->itemlen + spc_rows);
176
204
    }
177
205
    return result;
178
206
}
183
211
    MENU *result;
184
212
    WINDOW *menuwin;
185
213
    int mrows, mcols;
186
 
    int y = ((int) number >= 0) ? MENU_Y : 0;
 
214
    int y = okMenuNo(number) ? MENU_Y : 0;
187
215
    int x = menu_offset(number);
188
216
    int margin = (y == MENU_Y) ? 1 : 0;
189
217
    int maxcol = (ncols + x) < COLS ? ncols : (COLS - x - 1);
232
260
    int count;
233
261
 
234
262
    if (m != 0) {
 
263
        delwin(menu_sub(m));
235
264
        delwin(menu_win(m));
236
265
 
237
266
        ip = menu_items(m);
510
539
static int
511
540
menu_number(void)
512
541
{
513
 
    return item_index(current_item(mpBanner));
 
542
    return item_index(current_item(mpBanner)) - (eBanner + 1);
514
543
}
515
544
 
516
545
static MENU *
558
587
        *ip++ = new_item(*ap, "");
559
588
    *ip = (ITEM *) 0;
560
589
 
561
 
    mpBanner = menu_create(items, SIZEOF(labels) - 1, SIZEOF(labels) - 1, eUnknown);
 
590
    mpBanner = menu_create(items, SIZEOF(labels) - 1, SIZEOF(labels) - 1, eBanner);
562
591
    set_menu_mark(mpBanner, ">");
563
592
 
564
593
    build_file_menu(eFile);
568
597
#endif
569
598
}
570
599
 
 
600
static int
 
601
move_menu(MENU * menu, MENU * current, int by_y, int by_x)
 
602
{
 
603
    WINDOW *top_win = menu_win(menu);
 
604
    WINDOW *sub_win = menu_sub(menu);
 
605
    int y0, x0;
 
606
    int y1, x1;
 
607
    int result;
 
608
 
 
609
    getbegyx(top_win, y0, x0);
 
610
    y0 += by_y;
 
611
    x0 += by_x;
 
612
 
 
613
    getbegyx(sub_win, y1, x1);
 
614
    y1 += by_y;
 
615
    x1 += by_x;
 
616
 
 
617
    if ((result = mvwin(top_win, y0, x0)) != ERR) {
 
618
#if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH < 20060218)
 
619
        sub_win->_begy = y1;
 
620
        sub_win->_begx = x1;
 
621
#else
 
622
        mvwin(sub_win, y1, x1);
 
623
#endif
 
624
        if (menu == current) {
 
625
            touchwin(top_win);
 
626
            wnoutrefresh(top_win);
 
627
        }
 
628
    }
 
629
    return result;
 
630
}
 
631
 
 
632
/*
 
633
 * Move the menus around on the screen, to test mvwin().
 
634
 */
 
635
static void
 
636
move_menus(MENU * current, int by_y, int by_x)
 
637
{
 
638
    if (move_menu(mpBanner, current, by_y, by_x) != ERR) {
 
639
        erase();
 
640
        wnoutrefresh(stdscr);
 
641
        move_menu(mpFile, current, by_y, by_x);
 
642
        move_menu(mpSelect, current, by_y, by_x);
 
643
#ifdef TRACE
 
644
        move_menu(mpTrace, current, by_y, by_x);
 
645
#endif
 
646
        doupdate();
 
647
    }
 
648
}
 
649
 
 
650
static void
 
651
show_status(int ch, MENU * menu)
 
652
{
 
653
    move(LINES - 1, 0);
 
654
    printw("key %s, menu %d, mark %s, match %s",
 
655
           keyname(ch),
 
656
           menu_number(),
 
657
           menu_mark(menu),
 
658
           menu_pattern(menu));
 
659
    clrtoeol();
 
660
    refresh();
 
661
}
 
662
 
571
663
static void
572
664
perform_menus(void)
573
665
{
574
666
    MENU *this_menu;
575
667
    MENU *last_menu = mpFile;
576
 
    int code = E_UNKNOWN_COMMAND, cmd, ch;
 
668
    int code = E_UNKNOWN_COMMAND;
 
669
    int cmd;
 
670
    int ch = ERR;
577
671
 
578
672
#ifdef NCURSES_MOUSE_VERSION
579
673
    mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
582
676
    menu_display(last_menu);
583
677
 
584
678
    for (;;) {
 
679
 
 
680
        if (ch != ERR)
 
681
            show_status(ch, last_menu);
 
682
 
585
683
        ch = menu_getc(mpBanner);
 
684
 
 
685
        /*
 
686
         * Provide for moving the menu around in the screen using shifted
 
687
         * cursor keys.
 
688
         */
 
689
        switch (ch) {
 
690
        case KEY_SF:
 
691
            move_menus(last_menu, 1, 0);
 
692
            continue;
 
693
        case KEY_SR:
 
694
            move_menus(last_menu, -1, 0);
 
695
            continue;
 
696
        case KEY_SLEFT:
 
697
            move_menus(last_menu, 0, -1);
 
698
            continue;
 
699
        case KEY_SRIGHT:
 
700
            move_menus(last_menu, 0, 1);
 
701
            continue;
 
702
        }
586
703
        cmd = menu_virtualize(ch);
587
704
 
588
705
        switch (cmd) {
664
781
    menu_destroy(mpBanner);
665
782
}
666
783
 
 
784
#if HAVE_RIPOFFLINE
 
785
static int
 
786
rip_footer(WINDOW *win, int cols)
 
787
{
 
788
    wbkgd(win, A_REVERSE);
 
789
    werase(win);
 
790
    wmove(win, 0, 0);
 
791
    wprintw(win, "footer: %d columns", cols);
 
792
    wnoutrefresh(win);
 
793
    return OK;
 
794
}
 
795
 
 
796
static int
 
797
rip_header(WINDOW *win, int cols)
 
798
{
 
799
    wbkgd(win, A_REVERSE);
 
800
    werase(win);
 
801
    wmove(win, 0, 0);
 
802
    wprintw(win, "header: %d columns", cols);
 
803
    wnoutrefresh(win);
 
804
    return OK;
 
805
}
 
806
#endif /* HAVE_RIPOFFLINE */
 
807
 
 
808
static void
 
809
usage(void)
 
810
{
 
811
    static const char *const tbl[] =
 
812
    {
 
813
        "Usage: demo_menus [options]"
 
814
        ,""
 
815
        ,"Options:"
 
816
#if HAVE_RIPOFFLINE
 
817
        ,"  -f       rip-off footer line (can repeat)"
 
818
        ,"  -h       rip-off header line (can repeat)"
 
819
#endif
 
820
#ifdef TRACE
 
821
        ,"  -t mask  specify default trace-level (may toggle with ^T)"
 
822
#endif
 
823
    };
 
824
    size_t n;
 
825
    for (n = 0; n < SIZEOF(tbl); n++)
 
826
        fprintf(stderr, "%s\n", tbl[n]);
 
827
    ExitProgram(EXIT_FAILURE);
 
828
}
 
829
 
667
830
int
668
831
main(int argc, char *argv[])
669
832
{
 
833
    int c;
 
834
 
670
835
    setlocale(LC_ALL, "");
671
836
 
 
837
    while ((c = getopt(argc, argv, "a:de:fhmp:s:t:")) != EOF) {
 
838
        switch (c) {
 
839
#if HAVE_RIPOFFLINE
 
840
        case 'f':
 
841
            ripoffline(-1, rip_footer);
 
842
            break;
 
843
        case 'h':
 
844
            ripoffline(1, rip_header);
 
845
            break;
 
846
#endif /* HAVE_RIPOFFLINE */
 
847
#ifdef TRACE
 
848
        case 't':
 
849
            trace(strtoul(optarg, 0, 0));
 
850
            break;
 
851
#endif
 
852
        default:
 
853
            usage();
 
854
        }
 
855
    }
 
856
 
672
857
    initscr();
673
858
    noraw();
674
859
    cbreak();
684
869
    destroy_menus();
685
870
 
686
871
    endwin();
687
 
    return EXIT_SUCCESS;
 
872
    ExitProgram(EXIT_SUCCESS);
688
873
}
689
874
#else
690
875
int