window.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /* PDCurses */
  2. #include <curspriv.h>
  3. /*man-start**************************************************************
  4. window
  5. ------
  6. ### Synopsis
  7. WINDOW *newwin(int nlines, int ncols, int begy, int begx);
  8. WINDOW *derwin(WINDOW* orig, int nlines, int ncols,
  9. int begy, int begx);
  10. WINDOW *subwin(WINDOW* orig, int nlines, int ncols,
  11. int begy, int begx);
  12. WINDOW *dupwin(WINDOW *win);
  13. int delwin(WINDOW *win);
  14. int mvwin(WINDOW *win, int y, int x);
  15. int mvderwin(WINDOW *win, int pary, int parx);
  16. int syncok(WINDOW *win, bool bf);
  17. void wsyncup(WINDOW *win);
  18. void wcursyncup(WINDOW *win);
  19. void wsyncdown(WINDOW *win);
  20. WINDOW *resize_window(WINDOW *win, int nlines, int ncols);
  21. int wresize(WINDOW *win, int nlines, int ncols);
  22. WINDOW *PDC_makelines(WINDOW *win);
  23. WINDOW *PDC_makenew(int nlines, int ncols, int begy, int begx);
  24. void PDC_sync(WINDOW *win);
  25. ### Description
  26. newwin() creates a new window with the given number of lines, nlines
  27. and columns, ncols. The upper left corner of the window is at line
  28. begy, column begx. If nlines is zero, it defaults to LINES - begy;
  29. ncols to COLS - begx. Create a new full-screen window by calling
  30. newwin(0, 0, 0, 0).
  31. delwin() deletes the named window, freeing all associated memory. In
  32. the case of overlapping windows, subwindows should be deleted before
  33. the main window.
  34. mvwin() moves the window so that the upper left-hand corner is at
  35. position (y,x). If the move would cause the window to be off the
  36. screen, it is an error and the window is not moved. Moving subwindows
  37. is allowed.
  38. subwin() creates a new subwindow within a window. The dimensions of
  39. the subwindow are nlines lines and ncols columns. The subwindow is at
  40. position (begy, begx) on the screen. This position is relative to the
  41. screen, and not to the window orig. Changes made to either window
  42. will affect both. When using this routine, you will often need to
  43. call touchwin() before calling wrefresh().
  44. derwin() is the same as subwin(), except that begy and begx are
  45. relative to the origin of the window orig rather than the screen.
  46. There is no difference between subwindows and derived windows.
  47. mvderwin() moves a derived window (or subwindow) inside its parent
  48. window. The screen-relative parameters of the window are not changed.
  49. This routine is used to display different parts of the parent window
  50. at the same physical position on the screen.
  51. dupwin() creates an exact duplicate of the window win.
  52. wsyncup() causes a touchwin() of all of the window's parents.
  53. If wsyncok() is called with a second argument of TRUE, this causes a
  54. wsyncup() to be called every time the window is changed.
  55. wcursyncup() causes the current cursor position of all of a window's
  56. ancestors to reflect the current cursor position of the current
  57. window.
  58. wsyncdown() causes a touchwin() of the current window if any of its
  59. parent's windows have been touched.
  60. resize_window() allows the user to resize an existing window. It
  61. returns the pointer to the new window, or NULL on failure.
  62. wresize() is an ncurses-compatible wrapper for resize_window(). Note
  63. that, unlike ncurses, it will NOT process any subwindows of the
  64. window. (However, you still can call it _on_ subwindows.) It returns
  65. OK or ERR.
  66. PDC_makenew() allocates all data for a new WINDOW * except the actual
  67. lines themselves. If it's unable to allocate memory for the window
  68. structure, it will free all allocated memory and return a NULL
  69. pointer.
  70. PDC_makelines() allocates the memory for the lines.
  71. PDC_sync() handles wrefresh() and wsyncup() calls when a window is
  72. changed.
  73. ### Return Value
  74. newwin(), subwin(), derwin() and dupwin() return a pointer to the new
  75. window, or NULL on failure. delwin(), mvwin(), mvderwin() and
  76. syncok() return OK or ERR. wsyncup(), wcursyncup() and wsyncdown()
  77. return nothing.
  78. ### Errors
  79. It is an error to call resize_window() before calling initscr().
  80. Also, an error will be generated if we fail to create a newly sized
  81. replacement window for curscr, or stdscr. This could happen when
  82. increasing the window size. NOTE: If this happens, the previously
  83. successfully allocated windows are left alone; i.e., the resize is
  84. NOT cancelled for those windows.
  85. ### Portability
  86. X/Open ncurses NetBSD
  87. newwin Y Y Y
  88. delwin Y Y Y
  89. mvwin Y Y Y
  90. subwin Y Y Y
  91. derwin Y Y Y
  92. mvderwin Y Y Y
  93. dupwin Y Y Y
  94. wsyncup Y Y Y
  95. syncok Y Y Y
  96. wcursyncup Y Y Y
  97. wsyncdown Y Y Y
  98. wresize - Y Y
  99. resize_window - - -
  100. PDC_makelines - - -
  101. PDC_makenew - - -
  102. PDC_sync - - -
  103. **man-end****************************************************************/
  104. #include <stdlib.h>
  105. WINDOW *PDC_makenew(int nlines, int ncols, int begy, int begx)
  106. {
  107. WINDOW *win;
  108. PDC_LOG(("PDC_makenew() - called: lines %d cols %d begy %d begx %d\n",
  109. nlines, ncols, begy, begx));
  110. /* allocate the window structure itself */
  111. win = calloc(1, sizeof(WINDOW));
  112. if (!win)
  113. return win;
  114. /* allocate the line pointer array */
  115. win->_y = malloc(nlines * sizeof(chtype *));
  116. if (!win->_y)
  117. {
  118. free(win);
  119. return (WINDOW *)NULL;
  120. }
  121. /* allocate the minchng and maxchng arrays */
  122. win->_firstch = malloc(nlines * sizeof(int));
  123. if (!win->_firstch)
  124. {
  125. free(win->_y);
  126. free(win);
  127. return (WINDOW *)NULL;
  128. }
  129. win->_lastch = malloc(nlines * sizeof(int));
  130. if (!win->_lastch)
  131. {
  132. free(win->_firstch);
  133. free(win->_y);
  134. free(win);
  135. return (WINDOW *)NULL;
  136. }
  137. /* initialize window variables */
  138. win->_maxy = nlines; /* real max screen size */
  139. win->_maxx = ncols; /* real max screen size */
  140. win->_begy = begy;
  141. win->_begx = begx;
  142. win->_bkgd = ' '; /* wrs 4/10/93 -- initialize background to blank */
  143. win->_clear = (bool) ((nlines == LINES) && (ncols == COLS));
  144. win->_bmarg = nlines - 1;
  145. win->_parx = win->_pary = -1;
  146. /* init to say window all changed */
  147. touchwin(win);
  148. return win;
  149. }
  150. WINDOW *PDC_makelines(WINDOW *win)
  151. {
  152. int i, j, nlines, ncols;
  153. PDC_LOG(("PDC_makelines() - called\n"));
  154. if (!win)
  155. return (WINDOW *)NULL;
  156. nlines = win->_maxy;
  157. ncols = win->_maxx;
  158. for (i = 0; i < nlines; i++)
  159. {
  160. win->_y[i] = malloc(ncols * sizeof(chtype));
  161. if (!win->_y[i])
  162. {
  163. /* if error, free all the data */
  164. for (j = 0; j < i; j++)
  165. free(win->_y[j]);
  166. free(win->_firstch);
  167. free(win->_lastch);
  168. free(win->_y);
  169. free(win);
  170. return (WINDOW *)NULL;
  171. }
  172. }
  173. return win;
  174. }
  175. void PDC_sync(WINDOW *win)
  176. {
  177. PDC_LOG(("PDC_sync() - called:\n"));
  178. if (win->_immed)
  179. wrefresh(win);
  180. if (win->_sync)
  181. wsyncup(win);
  182. }
  183. WINDOW *newwin(int nlines, int ncols, int begy, int begx)
  184. {
  185. WINDOW *win;
  186. PDC_LOG(("newwin() - called:lines=%d cols=%d begy=%d begx=%d\n",
  187. nlines, ncols, begy, begx));
  188. if (!nlines)
  189. nlines = LINES - begy;
  190. if (!ncols)
  191. ncols = COLS - begx;
  192. if (!SP || begy + nlines > SP->lines || begx + ncols > SP->cols)
  193. return (WINDOW *)NULL;
  194. win = PDC_makenew(nlines, ncols, begy, begx);
  195. if (win)
  196. win = PDC_makelines(win);
  197. if (win)
  198. werase(win);
  199. return win;
  200. }
  201. int delwin(WINDOW *win)
  202. {
  203. int i;
  204. PDC_LOG(("delwin() - called\n"));
  205. if (!win)
  206. return ERR;
  207. /* subwindows use parents' lines */
  208. if (!(win->_flags & (_SUBWIN|_SUBPAD)))
  209. for (i = 0; i < win->_maxy && win->_y[i]; i++)
  210. if (win->_y[i])
  211. free(win->_y[i]);
  212. free(win->_firstch);
  213. free(win->_lastch);
  214. free(win->_y);
  215. free(win);
  216. return OK;
  217. }
  218. int mvwin(WINDOW *win, int y, int x)
  219. {
  220. PDC_LOG(("mvwin() - called\n"));
  221. if (!win || (y + win->_maxy > LINES || y < 0)
  222. || (x + win->_maxx > COLS || x < 0))
  223. return ERR;
  224. win->_begy = y;
  225. win->_begx = x;
  226. touchwin(win);
  227. return OK;
  228. }
  229. WINDOW *subwin(WINDOW *orig, int nlines, int ncols, int begy, int begx)
  230. {
  231. WINDOW *win;
  232. int i, j, k;
  233. PDC_LOG(("subwin() - called: lines %d cols %d begy %d begx %d\n",
  234. nlines, ncols, begy, begx));
  235. /* make sure window fits inside the original one */
  236. if (!orig || (begy < orig->_begy) || (begx < orig->_begx) ||
  237. (begy + nlines) > (orig->_begy + orig->_maxy) ||
  238. (begx + ncols) > (orig->_begx + orig->_maxx))
  239. return (WINDOW *)NULL;
  240. j = begy - orig->_begy;
  241. k = begx - orig->_begx;
  242. if (!nlines)
  243. nlines = orig->_maxy - 1 - j;
  244. if (!ncols)
  245. ncols = orig->_maxx - 1 - k;
  246. win = PDC_makenew(nlines, ncols, begy, begx);
  247. if (!win)
  248. return (WINDOW *)NULL;
  249. /* initialize window variables */
  250. win->_attrs = orig->_attrs;
  251. win->_bkgd = orig->_bkgd;
  252. win->_leaveit = orig->_leaveit;
  253. win->_scroll = orig->_scroll;
  254. win->_nodelay = orig->_nodelay;
  255. win->_delayms = orig->_delayms;
  256. win->_use_keypad = orig->_use_keypad;
  257. win->_immed = orig->_immed;
  258. win->_sync = orig->_sync;
  259. win->_pary = j;
  260. win->_parx = k;
  261. win->_parent = orig;
  262. for (i = 0; i < nlines; i++, j++)
  263. win->_y[i] = orig->_y[j] + k;
  264. win->_flags |= _SUBWIN;
  265. return win;
  266. }
  267. WINDOW *derwin(WINDOW *orig, int nlines, int ncols, int begy, int begx)
  268. {
  269. return subwin(orig, nlines, ncols, begy + orig->_begy, begx + orig->_begx);
  270. }
  271. int mvderwin(WINDOW *win, int pary, int parx)
  272. {
  273. int i, j;
  274. WINDOW *mypar;
  275. if (!win || !(win->_parent))
  276. return ERR;
  277. mypar = win->_parent;
  278. if (pary < 0 || parx < 0 || (pary + win->_maxy) > mypar->_maxy ||
  279. (parx + win->_maxx) > mypar->_maxx)
  280. return ERR;
  281. j = pary;
  282. for (i = 0; i < win->_maxy; i++)
  283. win->_y[i] = (mypar->_y[j++]) + parx;
  284. win->_pary = pary;
  285. win->_parx = parx;
  286. return OK;
  287. }
  288. WINDOW *dupwin(WINDOW *win)
  289. {
  290. WINDOW *new;
  291. chtype *ptr, *ptr1;
  292. int nlines, ncols, begy, begx, i;
  293. if (!win)
  294. return (WINDOW *)NULL;
  295. nlines = win->_maxy;
  296. ncols = win->_maxx;
  297. begy = win->_begy;
  298. begx = win->_begx;
  299. new = PDC_makenew(nlines, ncols, begy, begx);
  300. if (new)
  301. new = PDC_makelines(new);
  302. if (!new)
  303. return (WINDOW *)NULL;
  304. /* copy the contents of win into new */
  305. for (i = 0; i < nlines; i++)
  306. {
  307. for (ptr = new->_y[i], ptr1 = win->_y[i];
  308. ptr < new->_y[i] + ncols; ptr++, ptr1++)
  309. *ptr = *ptr1;
  310. new->_firstch[i] = 0;
  311. new->_lastch[i] = ncols - 1;
  312. }
  313. new->_curx = win->_curx;
  314. new->_cury = win->_cury;
  315. new->_maxy = win->_maxy;
  316. new->_maxx = win->_maxx;
  317. new->_begy = win->_begy;
  318. new->_begx = win->_begx;
  319. new->_flags = win->_flags;
  320. new->_attrs = win->_attrs;
  321. new->_clear = win->_clear;
  322. new->_leaveit = win->_leaveit;
  323. new->_scroll = win->_scroll;
  324. new->_nodelay = win->_nodelay;
  325. new->_delayms = win->_delayms;
  326. new->_use_keypad = win->_use_keypad;
  327. new->_tmarg = win->_tmarg;
  328. new->_bmarg = win->_bmarg;
  329. new->_parx = win->_parx;
  330. new->_pary = win->_pary;
  331. new->_parent = win->_parent;
  332. new->_bkgd = win->_bkgd;
  333. new->_flags = win->_flags;
  334. return new;
  335. }
  336. WINDOW *resize_window(WINDOW *win, int nlines, int ncols)
  337. {
  338. WINDOW *new;
  339. int i, save_cury, save_curx, new_begy, new_begx;
  340. PDC_LOG(("resize_window() - called: nlines %d ncols %d\n",
  341. nlines, ncols));
  342. if (!win || !SP)
  343. return (WINDOW *)NULL;
  344. if (win->_flags & _SUBPAD)
  345. {
  346. new = subpad(win->_parent, nlines, ncols, win->_begy, win->_begx);
  347. if (!new)
  348. return (WINDOW *)NULL;
  349. }
  350. else if (win->_flags & _SUBWIN)
  351. {
  352. new = subwin(win->_parent, nlines, ncols, win->_begy, win->_begx);
  353. if (!new)
  354. return (WINDOW *)NULL;
  355. }
  356. else
  357. {
  358. if (win == SP->slk_winptr)
  359. {
  360. new_begy = SP->lines - SP->slklines;
  361. new_begx = 0;
  362. }
  363. else
  364. {
  365. new_begy = win->_begy;
  366. new_begx = win->_begx;
  367. }
  368. new = PDC_makenew(nlines, ncols, new_begy, new_begx);
  369. if (!new)
  370. return (WINDOW *)NULL;
  371. }
  372. save_curx = min(win->_curx, (new->_maxx - 1));
  373. save_cury = min(win->_cury, (new->_maxy - 1));
  374. if (!(win->_flags & (_SUBPAD|_SUBWIN)))
  375. {
  376. new = PDC_makelines(new);
  377. if (!new)
  378. return (WINDOW *)NULL;
  379. new->_bkgd = win->_bkgd;
  380. werase(new);
  381. copywin(win, new, 0, 0, 0, 0, min(win->_maxy, new->_maxy) - 1,
  382. min(win->_maxx, new->_maxx) - 1, FALSE);
  383. for (i = 0; i < win->_maxy && win->_y[i]; i++)
  384. if (win->_y[i])
  385. free(win->_y[i]);
  386. }
  387. new->_flags = win->_flags;
  388. new->_attrs = win->_attrs;
  389. new->_clear = win->_clear;
  390. new->_leaveit = win->_leaveit;
  391. new->_scroll = win->_scroll;
  392. new->_nodelay = win->_nodelay;
  393. new->_delayms = win->_delayms;
  394. new->_use_keypad = win->_use_keypad;
  395. new->_tmarg = (win->_tmarg > new->_maxy - 1) ? 0 : win->_tmarg;
  396. new->_bmarg = (win->_bmarg == win->_maxy - 1) ?
  397. new->_maxy - 1 : min(win->_bmarg, (new->_maxy - 1));
  398. new->_parent = win->_parent;
  399. new->_immed = win->_immed;
  400. new->_sync = win->_sync;
  401. new->_bkgd = win->_bkgd;
  402. new->_curx = save_curx;
  403. new->_cury = save_cury;
  404. free(win->_firstch);
  405. free(win->_lastch);
  406. free(win->_y);
  407. *win = *new;
  408. free(new);
  409. return win;
  410. }
  411. int wresize(WINDOW *win, int nlines, int ncols)
  412. {
  413. return (resize_window(win, nlines, ncols) ? OK : ERR);
  414. }
  415. void wsyncup(WINDOW *win)
  416. {
  417. WINDOW *tmp;
  418. PDC_LOG(("wsyncup() - called\n"));
  419. for (tmp = win; tmp; tmp = tmp->_parent)
  420. touchwin(tmp);
  421. }
  422. int syncok(WINDOW *win, bool bf)
  423. {
  424. PDC_LOG(("syncok() - called\n"));
  425. if (!win)
  426. return ERR;
  427. win->_sync = bf;
  428. return OK;
  429. }
  430. void wcursyncup(WINDOW *win)
  431. {
  432. WINDOW *tmp;
  433. PDC_LOG(("wcursyncup() - called\n"));
  434. for (tmp = win; tmp && tmp->_parent; tmp = tmp->_parent)
  435. wmove(tmp->_parent, tmp->_pary + tmp->_cury, tmp->_parx + tmp->_curx);
  436. }
  437. void wsyncdown(WINDOW *win)
  438. {
  439. WINDOW *tmp;
  440. PDC_LOG(("wsyncdown() - called\n"));
  441. for (tmp = win; tmp; tmp = tmp->_parent)
  442. {
  443. if (is_wintouched(tmp))
  444. {
  445. touchwin(win);
  446. break;
  447. }
  448. }
  449. }