Skip to content

Browse panels with user pointers

Adapted from NCURSES Programming HOWTO, by Pradeep Padala.

This program creates three windows which can be cycled through using the Tab key.

Example: Panel Window Browsing Example

#include <stdlib.h>
#include <string.h>
#include <panel.h>
#define NLINES 10
#define NCOLS 40
static void
print_in_middle(WINDOW *win, int starty, int startx,
int width, const char *string, chtype color)
{
int length, x, y;
float temp;
if (win == NULL)
win = stdscr;
getyx(win, y, x);
if (startx != 0)
x = startx;
if (starty != 0)
y = starty;
if (width == 0)
width = 80;
length = (int) strlen(string);
temp = (float) (width - length) / 2;
x = startx + (int) temp;
wattron(win, color);
mvwprintw(win, y, x, "%s", string);
wattroff(win, color);
}
/* Show the window with a border and a label */
static void
win_show(WINDOW *win, const char *label, int label_color)
{
int height, width;
getmaxyx(win, height, width);
(void) height;
box(win, 0, 0);
mvwaddch(win, 2, 0, ACS_LTEE);
mvwhline(win, 2, 1, ACS_HLINE, width - 2);
mvwaddch(win, 2, width - 1, ACS_RTEE);
print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color));
}
/* Put all the windows */
static void
init_wins(WINDOW **wins, int n)
{
int x, y, i;
char label[80];
y = 2;
x = 10;
for (i = 0; i < n; ++i) {
wins[i] = newwin(NLINES, NCOLS, y, x);
sprintf(label, "Window Number %d", i + 1);
win_show(wins[i], label, i + 1);
y += 3;
x += 7;
}
}
int
main(void)
{
WINDOW *my_wins[3];
PANEL *my_panels[3];
PANEL *top;
int ch;
/* Initialize curses */
cbreak();
noecho();
/* Initialize all the colors */
init_wins(my_wins, 3);
/* Attach a panel to each window */
/* Order is bottom up */
/* Push 0, order: stdscr-0 */
my_panels[0] = new_panel(my_wins[0]);
/* Push 1, order: stdscr-0-1 */
my_panels[1] = new_panel(my_wins[1]);
/* Push 2, order: stdscr-0-1-2 */
my_panels[2] = new_panel(my_wins[2]);
/* Set up the user pointers to the next panel */
set_panel_userptr(my_panels[0], my_panels[1]);
set_panel_userptr(my_panels[1], my_panels[2]);
set_panel_userptr(my_panels[2], my_panels[0]);
/* Update the stacking order. The last panel created is on top */
/* Show it on the screen */
mvprintw(LINES - 2, 0,
"Use tab to browse through the windows (F1 to Exit)");
top = my_panels[2];
while ((ch = getch()) != KEY_F(1)) {
switch (ch) {
case 9: /* Tab */
top = (PANEL *) panel_userptr(top);
top_panel(top);
break;
}
}
endwin();
return EXIT_SUCCESS;
}

The example uses user pointers to find the next window in the cycle. Every panel has an associated user pointer, not used by the panel code, to which you can attach application data. The pointer can point to any information you want to store. In this case each panel stores a pointer to the next panel in the cycle.

The user pointer for a panel is set with set_panel_userptr(). It can be read with panel_userptr(), which returns the user pointer for the panel given as argument. After finding the next panel in the cycle, the program brings it to the top with top_panel(), which brings the panel given as argument to the top of the deck.