Skip to content

Commit b471d8a

Browse files
committed
Examples: panel test (from PDCurses)
The original example (source: https://github.com/wmcbrine/PDCurses/blob/master/demos/ptest.c) is in the public domain. As dub doesn't like different licenses for sub-packages, I place the converted example under the ncurses license, too.
1 parent 6dbcf3e commit b471d8a

File tree

4 files changed

+296
-1
lines changed

4 files changed

+296
-1
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ To run the examples, change to the specific directory and build and execute the
3434

3535
### Panels
3636
- panel_simple - A simple panel example
37+
- ptest - Panel test from PDCurses, shows moving, hiding and layering of panels (N)
3738

3839

3940
## TODO

examples/ptest/dub.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "ptest",
3+
"description": "Example application for the D ncurses library",
4+
"homepage": "https://github.com/D-Programming-Deimos/ncurses",
5+
"authors": [
6+
"Mark Hessling",
7+
"William McBrine",
8+
"Joachim de Groot"
9+
],
10+
"license": "ncurses",
11+
"dependencies": {
12+
"ncurses": "*"
13+
},
14+
"subConfigurations": {
15+
"ncurses": "panels"
16+
},
17+
"targetPath": "bin",
18+
"targetType": "executable",
19+
"targetName": "ptest"
20+
}

examples/ptest/source/ptest.d

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
import core.stdc.stdlib : exit;
2+
import std.conv : to;
3+
import std.string : toStringz;
4+
5+
import deimos.ncurses;
6+
import deimos.ncurses.panel;
7+
8+
string[] mod = ["test ", "TEST ", "(**) ", "*()* ", "<--> ", "LAST "];
9+
10+
void pflush()
11+
{
12+
update_panels();
13+
doupdate();
14+
}
15+
16+
void backfill()
17+
{
18+
erase();
19+
20+
foreach (immutable y; 0 .. LINES - 1)
21+
foreach (immutable x; 0 .. COLS)
22+
printw("%d", (y + x) % 10);
23+
}
24+
25+
void wait_a_while(int msec)
26+
{
27+
if (msec != 1)
28+
timeout(msec);
29+
30+
int c = getch();
31+
32+
if (c == 'q')
33+
{
34+
endwin();
35+
exit(1);
36+
}
37+
}
38+
39+
void saywhat(immutable string text)
40+
{
41+
mvprintw(LINES - 1, 0, "%-30.30s", toStringz(text));
42+
}
43+
44+
/* mkpanel - alloc a win and panel and associate them */
45+
46+
PANEL* mkpanel(int rows, int cols, int tly, int tlx)
47+
{
48+
WINDOW* win = newwin(rows, cols, tly, tlx);
49+
PANEL* pan;
50+
51+
if (win)
52+
{
53+
pan = new_panel(win);
54+
55+
if (!pan)
56+
delwin(win);
57+
}
58+
59+
return pan;
60+
}
61+
62+
void rmpanel(PANEL* pan)
63+
{
64+
WINDOW* win = pan.win;
65+
66+
del_panel(pan);
67+
delwin(win);
68+
}
69+
70+
void fill_panel(PANEL* pan)
71+
{
72+
char num = *cast(char*)(pan.user + 1);
73+
int maxy, maxx;
74+
75+
box(pan.win, 0, 0);
76+
mvwprintw(pan.win, 1, 1, "-pan%c-", num);
77+
getmaxyx(pan.win, maxy, maxx);
78+
79+
foreach (immutable y; 2 .. maxy)
80+
foreach (immutable x; 1 .. maxx)
81+
mvwaddch(pan.win, y, x, num);
82+
}
83+
84+
int main(string[] args)
85+
{
86+
PANEL* p1, p2, p3, p4, p5;
87+
WINDOW* w4, w5;
88+
89+
int nap_msec = 1;
90+
91+
if (args.length > 1 && to!int(args[1]))
92+
nap_msec = to!int(args[1]);
93+
94+
initscr();
95+
curs_set(false);
96+
backfill();
97+
98+
foreach (immutable y; 0 .. 5)
99+
{
100+
p1 = mkpanel(10, 10, 0, 0);
101+
set_panel_userptr(p1, toStringz("p1"));
102+
103+
p2 = mkpanel(14, 14, 5, 5);
104+
set_panel_userptr(p2, toStringz("p2"));
105+
106+
p3 = mkpanel(6, 8, 12, 12);
107+
set_panel_userptr(p3, toStringz("p3"));
108+
109+
p4 = mkpanel(10, 10, 10, 30);
110+
w4 = panel_window(p4);
111+
set_panel_userptr(p4, toStringz("p4"));
112+
113+
p5 = mkpanel(10, 10, 13, 37);
114+
w5 = panel_window(p5);
115+
set_panel_userptr(p5, toStringz("p5"));
116+
117+
fill_panel(p1);
118+
fill_panel(p2);
119+
fill_panel(p3);
120+
fill_panel(p4);
121+
fill_panel(p5);
122+
hide_panel(p4);
123+
hide_panel(p5);
124+
pflush();
125+
wait_a_while(nap_msec);
126+
127+
saywhat("hide 3, show 1, show 2, show 4, show 5;");
128+
move_panel(p1, 0, 0);
129+
hide_panel(p3);
130+
show_panel(p1);
131+
show_panel(p2);
132+
show_panel(p4);
133+
show_panel(p5);
134+
pflush();
135+
wait_a_while(nap_msec);
136+
137+
saywhat("show 1;");
138+
show_panel(p1);
139+
pflush();
140+
wait_a_while(nap_msec);
141+
142+
saywhat("show 2;");
143+
show_panel(p2);
144+
pflush();
145+
wait_a_while(nap_msec);
146+
147+
saywhat("move 2;");
148+
move_panel(p2, 10, 10);
149+
pflush();
150+
wait_a_while(nap_msec);
151+
152+
saywhat("show 3;");
153+
show_panel(p3);
154+
pflush();
155+
wait_a_while(nap_msec);
156+
157+
saywhat("move 3;");
158+
move_panel(p3, 5, 5);
159+
pflush();
160+
wait_a_while(nap_msec);
161+
162+
saywhat("bottom 3;");
163+
bottom_panel(p3);
164+
pflush();
165+
wait_a_while(nap_msec);
166+
167+
saywhat("show 4;");
168+
show_panel(p4);
169+
pflush();
170+
wait_a_while(nap_msec);
171+
172+
saywhat("show 5;");
173+
show_panel(p5);
174+
pflush();
175+
wait_a_while(nap_msec);
176+
177+
saywhat("top 3;");
178+
top_panel(p3);
179+
pflush();
180+
wait_a_while(nap_msec);
181+
182+
saywhat("top 1;");
183+
top_panel(p1);
184+
pflush();
185+
wait_a_while(nap_msec);
186+
187+
saywhat("top 2;");
188+
top_panel(p2);
189+
pflush();
190+
wait_a_while(nap_msec);
191+
192+
saywhat("top 3;");
193+
top_panel(p3);
194+
pflush();
195+
wait_a_while(nap_msec);
196+
197+
saywhat("top 4;");
198+
top_panel(p4);
199+
pflush();
200+
wait_a_while(nap_msec);
201+
202+
foreach (immutable itmp; 0 .. 6)
203+
{
204+
saywhat("move 4;");
205+
mvwaddstr(w4, 3, 1, toStringz(mod[itmp]));
206+
move_panel(p4, 4, itmp * 10);
207+
mvwaddstr(w5, 4, 1, toStringz(mod[itmp]));
208+
pflush();
209+
wait_a_while(nap_msec);
210+
211+
saywhat("move 5;");
212+
mvwaddstr(w4, 4, 1, toStringz(mod[itmp]));
213+
move_panel(p5, 7, itmp * 10 + 6);
214+
mvwaddstr(w5, 3, 1, toStringz(mod[itmp]));
215+
pflush();
216+
wait_a_while(nap_msec);
217+
}
218+
219+
saywhat("move 4;");
220+
move_panel(p4, 4, 60);
221+
pflush();
222+
wait_a_while(nap_msec);
223+
224+
saywhat("top 5;");
225+
top_panel(p5);
226+
pflush();
227+
wait_a_while(nap_msec);
228+
229+
saywhat("top 2;");
230+
top_panel(p2);
231+
pflush();
232+
wait_a_while(nap_msec);
233+
234+
saywhat("top 1;");
235+
top_panel(p1);
236+
pflush();
237+
wait_a_while(nap_msec);
238+
239+
saywhat("delete 2;");
240+
rmpanel(p2);
241+
pflush();
242+
wait_a_while(nap_msec);
243+
244+
saywhat("hide 3;");
245+
hide_panel(p3);
246+
pflush();
247+
wait_a_while(nap_msec);
248+
249+
saywhat("delete 1;");
250+
rmpanel(p1);
251+
pflush();
252+
wait_a_while(nap_msec);
253+
254+
saywhat("delete 4; ");
255+
rmpanel(p4);
256+
pflush();
257+
wait_a_while(nap_msec);
258+
259+
saywhat("delete 5; ");
260+
rmpanel(p5);
261+
pflush();
262+
wait_a_while(nap_msec);
263+
264+
if (nap_msec == 1)
265+
break;
266+
267+
nap_msec = 100L;
268+
}
269+
270+
endwin();
271+
272+
return 0;
273+
} /* end of main */

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"examples/menu_attrib",
5454
"examples/menu_scroll",
5555
"examples/menu_simple",
56-
"examples/panel_simple"
56+
"examples/panel_simple",
57+
"examples/ptest"
5758
]
5859
}

0 commit comments

Comments
 (0)