Fix build failure.
[cboard.git] / src / window.c
blob061ff6cb4733ad12f95ebf88150927b243554458
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2002-2019 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"
30 WIN **wins;
31 wint_t pushkey;
34 * Creates a new window on the 'wins' stack. Returns the newly create window
35 * structure. The 'func' parameter is a function pointer that is called from
36 * game_loop(). 'efunc' is called just before the window is destroyed.
38 WIN *
39 window_create (const char *title, int h, int w, int y, int x,
40 window_func func, void *data, window_exit_func efunc,
41 window_resize_func rfunc)
43 int i = 0;
45 if (wins)
46 for (i = 0; wins[i]; i++);
48 wins = Realloc (wins, (i + 2) * sizeof (WIN *));
49 wins[i] = Calloc (1, sizeof (WIN));
50 wins[i]->w = newwin (h, w, y, x);
51 wins[i]->p = new_panel (wins[i]->w);
52 wins[i]->data = data;
53 wins[i]->rows = h;
54 wins[i]->cols = w;
55 wins[i]->func = func;
56 wins[i]->efunc = efunc;
57 wins[i]->rfunc = rfunc;
58 wins[i]->title = (title) ? strdup (title) : NULL;
59 wins[i + 1] = NULL;
60 return wins[i];
63 void
64 window_destroy (WIN * win)
66 int i, n;
67 WIN **new = NULL;
69 if (!wins)
70 return;
72 for (i = 0; wins[i]; i++);
74 while (i > 0 && wins[--i]->keep)
76 if (win && win == wins[i])
77 win = NULL;
79 free (wins[i]->title);
80 if (wins[i]->p)
81 del_panel (wins[i]->p);
83 delwin (wins[i]->w);
85 if (wins[i]->freedata && wins[i]->data)
86 free (wins[i]->data);
88 free (wins[i]);
89 wins[i] = NULL;
92 for (i = n = 0; wins[i]; i++)
94 if (win && !win->keep && win == wins[i])
96 if (win->p)
97 del_panel (win->p);
99 free (win->title);
100 delwin (win->w);
101 free (win);
102 win = NULL;
103 continue;
106 if (win && win->keep && win == wins[i])
108 del_panel (win->p);
109 win->p = NULL;
112 new = Realloc (new, (n + 2) * sizeof (WIN *));
113 new[n++] = wins[i];
116 free (wins);
117 wins = NULL;
119 if (new)
121 new[n] = NULL;
122 wins = new;
126 void
127 window_draw_title (WINDOW * win, const char *title, int width, chtype attr,
128 chtype battr)
130 int i;
132 if (title)
134 wchar_t *p;
136 wattron (win, attr);
138 for (i = 1; i < width - 1; i++)
139 mvwaddch (win, 1, i, ' ');
141 if (mblen (title, strlen (title)) > width)
143 p = str_etc (title, width - 2, 1);
145 else
146 p = str_to_wchar (title);
148 mvwaddwstr (win, 1, CENTERX (width, p), p);
149 wattroff (win, attr);
150 free (p);
153 wattron (win, battr);
154 box (win, ACS_VLINE, ACS_HLINE);
155 wattroff (win, battr);
158 void
159 window_draw_prompt (WINDOW * win, int y, int width, const char *str,
160 chtype attr)
162 int i;
164 wattron (win, attr);
166 for (i = 1; i < width - 1; i++)
167 mvwaddch (win, y, i, ' ');
169 wchar_t *promptw = str_to_wchar (str);
170 mvwprintw (win, y, CENTERX (width, promptw), "%ls", promptw);
171 free (promptw);
172 wattroff (win, attr);
175 void
176 window_resize_all ()
178 int i;
180 if (!wins)
181 return;
183 for (i = 0; wins[i]; i++)
185 WIN *w = wins[i];
187 if (!w->keep && w->rfunc)
188 w->rfunc (w);