Update copyright year to 2015.
[cboard.git] / src / window.c
blob4cedca020ed8833cf2bacb62d4b8f4a86f5d760a
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2002-2015 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
23 #include <stdlib.h>
24 #include <string.h>
26 #include "common.h"
27 #include "misc.h"
28 #include "window.h"
31 * Creates a new window on the 'wins' stack. Returns the newly create window
32 * structure. The 'func' parameter is a function pointer that is called from
33 * game_loop(). 'efunc' is called just before the window is destroyed.
35 WIN *window_create(const char *title, int h, int w, int y, int x,
36 window_func func, void *data, window_exit_func efunc)
38 int i = 0;
40 if (wins)
41 for (i = 0; wins[i]; i++);
43 wins = Realloc(wins, (i + 2) * sizeof(WIN *));
44 wins[i] = Calloc(1, sizeof(WIN));
45 wins[i]->w = newwin(h, w, y, x);
46 wins[i]->p = new_panel(wins[i]->w);
47 wins[i]->data = data;
48 wins[i]->rows = h;
49 wins[i]->cols = w;
50 wins[i]->func = func;
51 wins[i]->efunc = efunc;
52 wins[i]->title = (title) ? strdup(title) : NULL;
53 wins[i+1] = NULL;
54 return wins[i];
57 void window_destroy(WIN *win)
59 int i, n;
60 WIN **new = NULL;
62 if (!wins)
63 return;
65 for (i = 0; wins[i]; i++);
67 while (i > 0 && wins[--i]->keep) {
68 if (win && memcmp(win, wins[i], sizeof(WIN)) == 0)
69 win = NULL;
71 free(wins[i]->title);
72 if (wins[i]->p)
73 del_panel(wins[i]->p);
75 delwin(wins[i]->w);
77 if (wins[i]->freedata && wins[i]->data)
78 free(wins[i]->data);
80 free(wins[i]);
81 wins[i] = NULL;
84 for (i = n = 0; wins[i]; i++) {
85 if (win && !win->keep && memcmp(win, wins[i], sizeof(WIN)) == 0) {
86 if (win->p)
87 del_panel(win->p);
89 free(win->title);
90 delwin(win->w);
91 free(win);
92 win = NULL;
93 continue;
96 if (win && win->keep && memcmp(win, wins[i], sizeof(WIN)) == 0) {
97 del_panel(win->p);
98 win->p = NULL;
101 new = Realloc(new, (n + 2) * sizeof(WIN *));
102 new[n++] = wins[i];
105 free(wins);
106 wins = NULL;
108 if (new) {
109 new[n] = NULL;
110 wins = new;
114 void window_draw_title(WINDOW *win, const char *title, int width, chtype attr,
115 chtype battr)
117 int i;
119 if (title) {
120 wchar_t *p;
122 wattron(win, attr);
124 for (i = 1; i < width - 1; i++)
125 mvwaddch(win, 1, i, ' ');
127 if (mblen (title, strlen(title)) > width) {
128 p = str_etc(title, width - 2, 1);
130 else
131 p = str_to_wchar (title);
133 mvwaddwstr(win, 1, CENTERX(width, p), p);
134 wattroff(win, attr);
135 free (p);
138 wattron(win, battr);
139 box(win, ACS_VLINE, ACS_HLINE);
140 wattroff(win, battr);
143 void window_draw_prompt(WINDOW *win, int y, int width, const char *str,
144 chtype attr)
146 int i;
148 wattron(win, attr);
150 for (i = 1; i < width - 1; i++)
151 mvwaddch(win, y, i, ' ');
153 wchar_t *promptw = str_to_wchar (str);
154 mvwprintw(win, y, CENTERX(width, promptw), "%ls", promptw);
155 free (promptw);
156 wattroff(win, attr);