aboutsummaryrefslogtreecommitdiff
path: root/scripts/kconfig/libcurses/window.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--scripts/kconfig/libcurses/window.c639
1 files changed, 639 insertions, 0 deletions
diff --git a/scripts/kconfig/libcurses/window.c b/scripts/kconfig/libcurses/window.c
new file mode 100644
index 000000000..891d0c05b
--- /dev/null
+++ b/scripts/kconfig/libcurses/window.c
@@ -0,0 +1,639 @@
1/* PDCurses */
2
3#include "curspriv.h"
4
5/*man-start**************************************************************
6
7window
8------
9
10### Synopsis
11
12 WINDOW *newwin(int nlines, int ncols, int begy, int begx);
13 WINDOW *derwin(WINDOW* orig, int nlines, int ncols,
14 int begy, int begx);
15 WINDOW *subwin(WINDOW* orig, int nlines, int ncols,
16 int begy, int begx);
17 WINDOW *dupwin(WINDOW *win);
18 WINDOW *wgetparent(const WINDOW *win);
19 int delwin(WINDOW *win);
20 int mvwin(WINDOW *win, int y, int x);
21 int mvderwin(WINDOW *win, int pary, int parx);
22 int syncok(WINDOW *win, bool bf);
23 bool is_subwin(const WINDOW *win);
24 bool is_syncok(const WINDOW *win);
25 void wsyncup(WINDOW *win);
26 void wcursyncup(WINDOW *win);
27 void wsyncdown(WINDOW *win);
28
29 WINDOW *resize_window(WINDOW *win, int nlines, int ncols);
30 int wresize(WINDOW *win, int nlines, int ncols);
31 WINDOW *PDC_makelines(WINDOW *win);
32 WINDOW *PDC_makenew(int nlines, int ncols, int begy, int begx);
33 void PDC_sync(WINDOW *win);
34
35### Description
36
37 newwin() creates a new window with the given number of lines, nlines
38 and columns, ncols. The upper left corner of the window is at line
39 begy, column begx. If nlines is zero, it defaults to LINES - begy;
40 ncols to COLS - begx. Create a new full-screen window by calling
41 newwin(0, 0, 0, 0).
42
43 delwin() deletes the named window, freeing all associated memory. In
44 the case of overlapping windows, subwindows should be deleted before
45 the main window.
46
47 mvwin() moves the window so that the upper left-hand corner is at
48 position (y,x). If the move would cause the window to be off the
49 screen, it is an error and the window is not moved. Moving subwindows
50 is allowed.
51
52 subwin() creates a new subwindow within a window. The dimensions of
53 the subwindow are nlines lines and ncols columns. The subwindow is at
54 position (begy, begx) on the screen. This position is relative to the
55 screen, and not to the window orig. Changes made to either window
56 will affect both. When using this routine, you will often need to
57 call touchwin() before calling wrefresh().
58
59 derwin() is the same as subwin(), except that begy and begx are
60 relative to the origin of the window orig rather than the screen.
61 There is no difference between subwindows and derived windows.
62
63 mvderwin() moves a derived window (or subwindow) inside its parent
64 window. The screen-relative parameters of the window are not changed.
65 This routine is used to display different parts of the parent window
66 at the same physical position on the screen.
67
68 dupwin() creates an exact duplicate of the window win.
69
70 wgetparent() returns the parent WINDOW pointer for subwindows, or NULL
71 for windows having no parent.
72
73 wsyncup() causes a touchwin() of all of the window's parents.
74
75 If syncok() is called with a second argument of TRUE, this causes a
76 wsyncup() to be called every time the window is changed.
77
78 is_subwin() reports whether the specified window is a subwindow,
79 created by subwin() or derwin().
80
81 is_syncok() reports whether the specified window is in syncok mode.
82
83 wcursyncup() causes the current cursor position of all of a window's
84 ancestors to reflect the current cursor position of the current
85 window.
86
87 wsyncdown() causes a touchwin() of the current window if any of its
88 parent's windows have been touched.
89
90 resize_window() allows the user to resize an existing window. It
91 returns the pointer to the new window, or NULL on failure.
92
93 wresize() is an ncurses-compatible wrapper for resize_window(). Note
94 that, unlike ncurses, it will NOT process any subwindows of the
95 window. (However, you still can call it _on_ subwindows.) It returns
96 OK or ERR.
97
98 PDC_makenew() allocates all data for a new WINDOW * except the actual
99 lines themselves. If it's unable to allocate memory for the window
100 structure, it will free all allocated memory and return a NULL
101 pointer.
102
103 PDC_makelines() allocates the memory for the lines.
104
105 PDC_sync() handles wrefresh() and wsyncup() calls when a window is
106 changed.
107
108### Return Value
109
110 newwin(), subwin(), derwin() and dupwin() return a pointer to the new
111 window, or NULL on failure. delwin(), mvwin(), mvderwin() and
112 syncok() return OK or ERR. wsyncup(), wcursyncup() and wsyncdown()
113 return nothing.
114
115 is_subwin() and is_syncok() returns TRUE or FALSE.
116
117### Errors
118
119 It is an error to call resize_window() before calling initscr().
120 Also, an error will be generated if we fail to create a newly sized
121 replacement window for curscr, or stdscr. This could happen when
122 increasing the window size. NOTE: If this happens, the previously
123 successfully allocated windows are left alone; i.e., the resize is
124 NOT cancelled for those windows.
125
126### Portability
127
128 Function | X/Open | ncurses | NetBSD
129 :---------------------|:------:|:-------:|:------:
130 newwin | Y | Y | Y
131 delwin | Y | Y | Y
132 mvwin | Y | Y | Y
133 subwin | Y | Y | Y
134 derwin | Y | Y | Y
135 mvderwin | Y | Y | Y
136 dupwin | Y | Y | Y
137 wgetparent | - | Y | -
138 wsyncup | Y | Y | Y
139 syncok | Y | Y | Y
140 is_subwin | - | Y | -
141 is_syncok | - | Y | -
142 wcursyncup | Y | Y | Y
143 wsyncdown | Y | Y | Y
144 wresize | - | Y | Y
145 resize_window | - | - | -
146 PDC_makelines | - | - | -
147 PDC_makenew | - | - | -
148 PDC_sync | - | - | -
149
150**man-end****************************************************************/
151
152#include <stdlib.h>
153
154WINDOW *PDC_makenew(int nlines, int ncols, int begy, int begx)
155{
156 WINDOW *win;
157
158 PDC_LOG(("PDC_makenew() - called: lines %d cols %d begy %d begx %d\n",
159 nlines, ncols, begy, begx));
160
161 /* allocate the window structure itself */
162
163 win = calloc(1, sizeof(WINDOW));
164 if (!win)
165 return win;
166
167 /* allocate the line pointer array */
168
169 win->_y = malloc(nlines * sizeof(chtype *));
170 if (!win->_y)
171 {
172 free(win);
173 return (WINDOW *)NULL;
174 }
175
176 /* allocate the minchng and maxchng arrays */
177
178 win->_firstch = malloc(nlines * sizeof(int));
179 if (!win->_firstch)
180 {
181 free(win->_y);
182 free(win);
183 return (WINDOW *)NULL;
184 }
185
186 win->_lastch = malloc(nlines * sizeof(int));
187 if (!win->_lastch)
188 {
189 free(win->_firstch);
190 free(win->_y);
191 free(win);
192 return (WINDOW *)NULL;
193 }
194
195 /* initialize window variables */
196
197 win->_maxy = nlines; /* real max screen size */
198 win->_maxx = ncols; /* real max screen size */
199 win->_begy = begy;
200 win->_begx = begx;
201 win->_bkgd = ' '; /* wrs 4/10/93 -- initialize background to blank */
202 win->_clear = (bool) ((nlines == LINES) && (ncols == COLS));
203 win->_bmarg = nlines - 1;
204 win->_parx = win->_pary = -1;
205
206 /* initialize pad variables*/
207
208 win->_pad._pad_y = -1;
209 win->_pad._pad_x = -1;
210 win->_pad._pad_top = -1;
211 win->_pad._pad_left = -1;
212 win->_pad._pad_bottom = -1;
213 win->_pad._pad_right = -1;
214
215 /* init to say window all changed */
216
217 touchwin(win);
218
219 return win;
220}
221
222WINDOW *PDC_makelines(WINDOW *win)
223{
224 int i, j, nlines, ncols;
225
226 PDC_LOG(("PDC_makelines() - called\n"));
227
228 if (!win)
229 return (WINDOW *)NULL;
230
231 nlines = win->_maxy;
232 ncols = win->_maxx;
233
234 for (i = 0; i < nlines; i++)
235 {
236 win->_y[i] = malloc(ncols * sizeof(chtype));
237 if (!win->_y[i])
238 {
239 /* if error, free all the data */
240
241 for (j = 0; j < i; j++)
242 free(win->_y[j]);
243
244 free(win->_firstch);
245 free(win->_lastch);
246 free(win->_y);
247 free(win);
248
249 return (WINDOW *)NULL;
250 }
251 }
252
253 return win;
254}
255
256void PDC_sync(WINDOW *win)
257{
258 PDC_LOG(("PDC_sync() - called:\n"));
259
260 if (win->_immed)
261 wrefresh(win);
262 if (win->_sync)
263 wsyncup(win);
264}
265
266WINDOW *newwin(int nlines, int ncols, int begy, int begx)
267{
268 WINDOW *win;
269
270 PDC_LOG(("newwin() - called:lines=%d cols=%d begy=%d begx=%d\n",
271 nlines, ncols, begy, begx));
272
273 if (!nlines)
274 nlines = LINES - begy;
275 if (!ncols)
276 ncols = COLS - begx;
277
278 if (!SP || begy + nlines > SP->lines || begx + ncols > SP->cols)
279 return (WINDOW *)NULL;
280
281 win = PDC_makenew(nlines, ncols, begy, begx);
282 if (win)
283 win = PDC_makelines(win);
284
285 if (win)
286 werase(win);
287
288 return win;
289}
290
291int delwin(WINDOW *win)
292{
293 int i;
294
295 PDC_LOG(("delwin() - called\n"));
296
297 if (!win)
298 return ERR;
299
300 /* subwindows use parents' lines */
301
302 if (!(win->_flags & (_SUBWIN|_SUBPAD)))
303 for (i = 0; i < win->_maxy && win->_y[i]; i++)
304 if (win->_y[i])
305 free(win->_y[i]);
306
307 free(win->_firstch);
308 free(win->_lastch);
309 free(win->_y);
310 free(win);
311
312 return OK;
313}
314
315int mvwin(WINDOW *win, int y, int x)
316{
317 PDC_LOG(("mvwin() - called\n"));
318
319 if (!win || (y + win->_maxy > LINES || y < 0)
320 || (x + win->_maxx > COLS || x < 0))
321 return ERR;
322
323 win->_begy = y;
324 win->_begx = x;
325 touchwin(win);
326
327 return OK;
328}
329
330WINDOW *subwin(WINDOW *orig, int nlines, int ncols, int begy, int begx)
331{
332 WINDOW *win;
333 int i, j, k;
334
335 PDC_LOG(("subwin() - called: lines %d cols %d begy %d begx %d\n",
336 nlines, ncols, begy, begx));
337
338 /* make sure window fits inside the original one */
339
340 if (!orig || (begy < orig->_begy) || (begx < orig->_begx) ||
341 (begy + nlines) > (orig->_begy + orig->_maxy) ||
342 (begx + ncols) > (orig->_begx + orig->_maxx))
343 return (WINDOW *)NULL;
344
345 j = begy - orig->_begy;
346 k = begx - orig->_begx;
347
348 if (!nlines)
349 nlines = orig->_maxy - j;
350 if (!ncols)
351 ncols = orig->_maxx - k;
352
353 win = PDC_makenew(nlines, ncols, begy, begx);
354 if (!win)
355 return (WINDOW *)NULL;
356
357 /* initialize window variables */
358
359 win->_attrs = orig->_attrs;
360 win->_bkgd = orig->_bkgd;
361 win->_leaveit = orig->_leaveit;
362 win->_scroll = orig->_scroll;
363 win->_nodelay = orig->_nodelay;
364 win->_delayms = orig->_delayms;
365 win->_use_keypad = orig->_use_keypad;
366 win->_immed = orig->_immed;
367 win->_sync = orig->_sync;
368 win->_pary = j;
369 win->_parx = k;
370 win->_parent = orig;
371
372 for (i = 0; i < nlines; i++, j++)
373 win->_y[i] = orig->_y[j] + k;
374
375 win->_flags |= _SUBWIN;
376
377 return win;
378}
379
380WINDOW *derwin(WINDOW *orig, int nlines, int ncols, int begy, int begx)
381{
382 return subwin(orig, nlines, ncols, begy + orig->_begy, begx + orig->_begx);
383}
384
385int mvderwin(WINDOW *win, int pary, int parx)
386{
387 int i, j;
388 WINDOW *mypar;
389
390 if (!win || !(win->_parent))
391 return ERR;
392
393 mypar = win->_parent;
394
395 if (pary < 0 || parx < 0 || (pary + win->_maxy) > mypar->_maxy ||
396 (parx + win->_maxx) > mypar->_maxx)
397 return ERR;
398
399 j = pary;
400
401 for (i = 0; i < win->_maxy; i++)
402 win->_y[i] = (mypar->_y[j++]) + parx;
403
404 win->_pary = pary;
405 win->_parx = parx;
406
407 return OK;
408}
409
410WINDOW *dupwin(WINDOW *win)
411{
412 WINDOW *new;
413 chtype *ptr, *ptr1;
414 int nlines, ncols, begy, begx, i;
415
416 if (!win)
417 return (WINDOW *)NULL;
418
419 nlines = win->_maxy;
420 ncols = win->_maxx;
421 begy = win->_begy;
422 begx = win->_begx;
423
424 new = PDC_makenew(nlines, ncols, begy, begx);
425 if (new)
426 new = PDC_makelines(new);
427
428 if (!new)
429 return (WINDOW *)NULL;
430
431 /* copy the contents of win into new */
432
433 for (i = 0; i < nlines; i++)
434 {
435 for (ptr = new->_y[i], ptr1 = win->_y[i];
436 ptr < new->_y[i] + ncols; ptr++, ptr1++)
437 *ptr = *ptr1;
438
439 new->_firstch[i] = 0;
440 new->_lastch[i] = ncols - 1;
441 }
442
443 new->_curx = win->_curx;
444 new->_cury = win->_cury;
445 new->_maxy = win->_maxy;
446 new->_maxx = win->_maxx;
447 new->_begy = win->_begy;
448 new->_begx = win->_begx;
449 new->_flags = win->_flags;
450 new->_attrs = win->_attrs;
451 new->_clear = win->_clear;
452 new->_leaveit = win->_leaveit;
453 new->_scroll = win->_scroll;
454 new->_nodelay = win->_nodelay;
455 new->_delayms = win->_delayms;
456 new->_use_keypad = win->_use_keypad;
457 new->_tmarg = win->_tmarg;
458 new->_bmarg = win->_bmarg;
459 new->_parx = win->_parx;
460 new->_pary = win->_pary;
461 new->_parent = win->_parent;
462 new->_bkgd = win->_bkgd;
463 new->_flags = win->_flags;
464
465 return new;
466}
467
468WINDOW *wgetparent(const WINDOW *win)
469{
470 PDC_LOG(("wgetparent() - called\n"));
471
472 if (!win || !win->_parent)
473 return NULL;
474
475 return win->_parent;
476}
477
478WINDOW *resize_window(WINDOW *win, int nlines, int ncols)
479{
480 WINDOW *new;
481 int i, save_cury, save_curx, new_begy, new_begx;
482
483 PDC_LOG(("resize_window() - called: nlines %d ncols %d\n",
484 nlines, ncols));
485
486 if (!win || !SP)
487 return (WINDOW *)NULL;
488
489 if (win->_flags & _SUBPAD)
490 {
491 new = subpad(win->_parent, nlines, ncols, win->_begy, win->_begx);
492 if (!new)
493 return (WINDOW *)NULL;
494 }
495 else if (win->_flags & _SUBWIN)
496 {
497 new = subwin(win->_parent, nlines, ncols, win->_begy, win->_begx);
498 if (!new)
499 return (WINDOW *)NULL;
500 }
501 else
502 {
503 if (win == SP->slk_winptr)
504 {
505 new_begy = SP->lines - SP->slklines;
506 new_begx = 0;
507 }
508 else
509 {
510 new_begy = win->_begy;
511 new_begx = win->_begx;
512 }
513
514 new = PDC_makenew(nlines, ncols, new_begy, new_begx);
515 if (!new)
516 return (WINDOW *)NULL;
517 }
518
519 save_curx = min(win->_curx, (new->_maxx - 1));
520 save_cury = min(win->_cury, (new->_maxy - 1));
521
522 if (!(win->_flags & (_SUBPAD|_SUBWIN)))
523 {
524 new = PDC_makelines(new);
525 if (!new)
526 return (WINDOW *)NULL;
527
528 new->_bkgd = win->_bkgd;
529 werase(new);
530
531 copywin(win, new, 0, 0, 0, 0, min(win->_maxy, new->_maxy) - 1,
532 min(win->_maxx, new->_maxx) - 1, FALSE);
533
534 for (i = 0; i < win->_maxy && win->_y[i]; i++)
535 if (win->_y[i])
536 free(win->_y[i]);
537 }
538
539 new->_flags = win->_flags;
540 new->_attrs = win->_attrs;
541 new->_clear = win->_clear;
542 new->_leaveit = win->_leaveit;
543 new->_scroll = win->_scroll;
544 new->_nodelay = win->_nodelay;
545 new->_delayms = win->_delayms;
546 new->_use_keypad = win->_use_keypad;
547 new->_tmarg = (win->_tmarg > new->_maxy - 1) ? 0 : win->_tmarg;
548 new->_bmarg = (win->_bmarg == win->_maxy - 1) ?
549 new->_maxy - 1 : min(win->_bmarg, (new->_maxy - 1));
550 new->_parent = win->_parent;
551 new->_immed = win->_immed;
552 new->_sync = win->_sync;
553 new->_bkgd = win->_bkgd;
554
555 new->_curx = save_curx;
556 new->_cury = save_cury;
557
558 free(win->_firstch);
559 free(win->_lastch);
560 free(win->_y);
561
562 *win = *new;
563 free(new);
564
565 return win;
566}
567
568int wresize(WINDOW *win, int nlines, int ncols)
569{
570 return (resize_window(win, nlines, ncols) ? OK : ERR);
571}
572
573void wsyncup(WINDOW *win)
574{
575 WINDOW *tmp;
576
577 PDC_LOG(("wsyncup() - called\n"));
578
579 for (tmp = win; tmp; tmp = tmp->_parent)
580 touchwin(tmp);
581}
582
583int syncok(WINDOW *win, bool bf)
584{
585 PDC_LOG(("syncok() - called\n"));
586
587 if (!win)
588 return ERR;
589
590 win->_sync = bf;
591
592 return OK;
593}
594
595bool is_subwin(const WINDOW *win)
596{
597 PDC_LOG(("is_subwin() - called\n"));
598
599 if (!win)
600 return FALSE;
601
602 return ((win->_flags & _SUBWIN) ? TRUE : FALSE);
603}
604
605bool is_syncok(const WINDOW *win)
606{
607 PDC_LOG(("is_syncok() - called\n"));
608
609 if (!win)
610 return FALSE;
611
612 return win->_sync;
613}
614
615void wcursyncup(WINDOW *win)
616{
617 WINDOW *tmp;
618
619 PDC_LOG(("wcursyncup() - called\n"));
620
621 for (tmp = win; tmp && tmp->_parent; tmp = tmp->_parent)
622 wmove(tmp->_parent, tmp->_pary + tmp->_cury, tmp->_parx + tmp->_curx);
623}
624
625void wsyncdown(WINDOW *win)
626{
627 WINDOW *tmp;
628
629 PDC_LOG(("wsyncdown() - called\n"));
630
631 for (tmp = win; tmp; tmp = tmp->_parent)
632 {
633 if (is_wintouched(tmp))
634 {
635 touchwin(win);
636 break;
637 }
638 }
639}