Skip to content

Commit

Permalink
Merge pull request #2 from jaydg/master
Browse files Browse the repository at this point in the history
Panel support
  • Loading branch information
unleashy committed Dec 22, 2018
2 parents afb41fd + 6a4f1cd commit 6c438fe
Show file tree
Hide file tree
Showing 4 changed files with 374 additions and 0 deletions.
13 changes: 13 additions & 0 deletions examples/ptest/dub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "ptest",
"description": "Example application for the D pdcurses bindings",
"authors": [
"Mark Hessling",
"William McBrine",
"Joachim de Groot"
],
"license": "ncurses",
"dependencies": {
"pdcurses-d": { "path": "../../" }
}
}
273 changes: 273 additions & 0 deletions examples/ptest/source/app.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
import core.stdc.stdlib : exit;
import std.conv : to;
import std.string : toStringz;

import pdcurses;
import pdcurses.panel;

string[] mod = ["test ", "TEST ", "(**) ", "*()* ", "<--> ", "LAST "];

void pflush()
{
update_panels();
doupdate();
}

void backfill()
{
erase();

foreach (immutable y; 0 .. LINES - 1)
foreach (immutable x; 0 .. COLS)
printw("%d", (y + x) % 10);
}

void wait_a_while(int msec)
{
if (msec != 1)
timeout(msec);

int c = getch();

if (c == 'q')
{
endwin();
exit(1);
}
}

void saywhat(immutable string text)
{
mvprintw(LINES - 1, 0, "%-30.30s", toStringz(text));
}

/* mkpanel - alloc a win and panel and associate them */

PANEL* mkpanel(int rows, int cols, int tly, int tlx)
{
WINDOW* win = newwin(rows, cols, tly, tlx);
PANEL* pan;

if (win)
{
pan = new_panel(win);

if (!pan)
delwin(win);
}

return pan;
}

void rmpanel(PANEL* pan)
{
WINDOW* win = pan.win;

del_panel(pan);
delwin(win);
}

void fill_panel(PANEL* pan)
{
char num = *cast(char*)(pan.user + 1);
int maxy, maxx;

box(pan.win, 0, 0);
mvwprintw(pan.win, 1, 1, "-pan%c-", num);
getmaxyx(pan.win, maxy, maxx);

foreach (immutable y; 2 .. maxy - 1)
foreach (immutable x; 1 .. maxx - 1)
mvwaddch(pan.win, y, x, num);
}

int main(string[] args)
{
PANEL* p1, p2, p3, p4, p5;
WINDOW* w4, w5;

int nap_msec = 1;

if (args.length > 1 && to!int(args[1]))
nap_msec = to!int(args[1]);

initscr();
curs_set(false);
backfill();

foreach (immutable y; 0 .. 5)
{
p1 = mkpanel(10, 10, 0, 0);
set_panel_userptr(p1, toStringz("p1"));

p2 = mkpanel(14, 14, 5, 5);
set_panel_userptr(p2, toStringz("p2"));

p3 = mkpanel(6, 8, 12, 12);
set_panel_userptr(p3, toStringz("p3"));

p4 = mkpanel(10, 10, 10, 30);
w4 = panel_window(p4);
set_panel_userptr(p4, toStringz("p4"));

p5 = mkpanel(10, 10, 13, 37);
w5 = panel_window(p5);
set_panel_userptr(p5, toStringz("p5"));

fill_panel(p1);
fill_panel(p2);
fill_panel(p3);
fill_panel(p4);
fill_panel(p5);
hide_panel(p4);
hide_panel(p5);
pflush();
wait_a_while(nap_msec);

saywhat("hide 3, show 1, show 2, show 4, show 5;");
move_panel(p1, 0, 0);
hide_panel(p3);
show_panel(p1);
show_panel(p2);
show_panel(p4);
show_panel(p5);
pflush();
wait_a_while(nap_msec);

saywhat("show 1;");
show_panel(p1);
pflush();
wait_a_while(nap_msec);

saywhat("show 2;");
show_panel(p2);
pflush();
wait_a_while(nap_msec);

saywhat("move 2;");
move_panel(p2, 10, 10);
pflush();
wait_a_while(nap_msec);

saywhat("show 3;");
show_panel(p3);
pflush();
wait_a_while(nap_msec);

saywhat("move 3;");
move_panel(p3, 5, 5);
pflush();
wait_a_while(nap_msec);

saywhat("bottom 3;");
bottom_panel(p3);
pflush();
wait_a_while(nap_msec);

saywhat("show 4;");
show_panel(p4);
pflush();
wait_a_while(nap_msec);

saywhat("show 5;");
show_panel(p5);
pflush();
wait_a_while(nap_msec);

saywhat("top 3;");
top_panel(p3);
pflush();
wait_a_while(nap_msec);

saywhat("top 1;");
top_panel(p1);
pflush();
wait_a_while(nap_msec);

saywhat("top 2;");
top_panel(p2);
pflush();
wait_a_while(nap_msec);

saywhat("top 3;");
top_panel(p3);
pflush();
wait_a_while(nap_msec);

saywhat("top 4;");
top_panel(p4);
pflush();
wait_a_while(nap_msec);

foreach (immutable itmp; 0 .. 6)
{
saywhat("move 4;");
mvwaddstr(w4, 3, 1, toStringz(mod[itmp]));
move_panel(p4, 4, itmp * 10);
mvwaddstr(w5, 4, 1, toStringz(mod[itmp]));
pflush();
wait_a_while(nap_msec);

saywhat("move 5;");
mvwaddstr(w4, 4, 1, toStringz(mod[itmp]));
move_panel(p5, 7, itmp * 10 + 6);
mvwaddstr(w5, 3, 1, toStringz(mod[itmp]));
pflush();
wait_a_while(nap_msec);
}

saywhat("move 4;");
move_panel(p4, 4, 60);
pflush();
wait_a_while(nap_msec);

saywhat("top 5;");
top_panel(p5);
pflush();
wait_a_while(nap_msec);

saywhat("top 2;");
top_panel(p2);
pflush();
wait_a_while(nap_msec);

saywhat("top 1;");
top_panel(p1);
pflush();
wait_a_while(nap_msec);

saywhat("delete 2;");
rmpanel(p2);
pflush();
wait_a_while(nap_msec);

saywhat("hide 3;");
hide_panel(p3);
pflush();
wait_a_while(nap_msec);

saywhat("delete 1;");
rmpanel(p1);
pflush();
wait_a_while(nap_msec);

saywhat("delete 4; ");
rmpanel(p4);
pflush();
wait_a_while(nap_msec);

saywhat("delete 5; ");
rmpanel(p5);
pflush();
wait_a_while(nap_msec);

if (nap_msec == 1)
break;

nap_msec = 100L;
}

endwin();

return 0;
} /* end of main */
38 changes: 38 additions & 0 deletions source/pdcurses/curses.d
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,44 @@ auto PAIR_NUMBER(T)(auto ref T n)

/* These will _only_ work as macros */

extern (D) pragma (inline, true)
void getbegyx(WINDOW *w, ref int y, ref int x)
{
y = getbegy(w);
x = getbegx(w);
}

extern (D) pragma (inline, true)
void getmaxyx(WINDOW *w, ref int y, ref int x)
{
y = getmaxy(w);
x = getmaxx(w);
}

extern (D) pragma (inline, true)
void getparyx(WINDOW *w, ref int y, ref int x)
{
y = getpary(w);
x = getparx(w);
}

extern (D) pragma (inline, true)
void getyx(WINDOW *w, ref int y, ref int x)
{
y = getcury(w);
x = getcurx(w);
}

extern (D) pragma (inline, true)
void getsyx(ref int y, ref int x)
{
if (curscr._leaveit) {
y = x = -1;
} else {
getyx(curscr, y, x);
}
}

/* return codes from PDC_getclipboard() and PDC_setclipboard() calls */

enum PDC_CLIP_SUCCESS = 0;
Expand Down
50 changes: 50 additions & 0 deletions source/pdcurses/panel.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module pdcurses.panel;

import pdcurses.curses;

/* Public Domain Curses */

/*----------------------------------------------------------------------*
* Panels for PDCurses *
*----------------------------------------------------------------------*/

extern (C):

struct panelobs
{
panelobs* above;
panel* pan;
}

alias PANELOBS = panelobs;

struct panel
{
WINDOW* win;
int wstarty;
int wendy;
int wstartx;
int wendx;
panel* below;
panel* above;
const(void)* user;
panelobs* obscure;
}

alias PANEL = panel;

int bottom_panel (PANEL* pan);
int del_panel (PANEL* pan);
int hide_panel (PANEL* pan);
int move_panel (PANEL* pan, int starty, int startx);
PANEL* new_panel (WINDOW* win);
PANEL* panel_above (const(PANEL)* pan);
PANEL* panel_below (const(PANEL)* pan);
int panel_hidden (const(PANEL)* pan);
const(void)* panel_userptr (const(PANEL)* pan);
WINDOW* panel_window (const(PANEL)* pan);
int replace_panel (PANEL* pan, WINDOW* win);
int set_panel_userptr (PANEL* pan, const(void)* uptr);
int show_panel (PANEL* pan);
int top_panel (PANEL* pan);
void update_panels ();

0 comments on commit 6c438fe

Please sign in to comment.