Update URL's to GitLab.
[cboard.git] / src / window.c
blobb09b1548b8ffe4dedb41c8b10e023c123d32f1c2
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2002-2018 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 *
36 window_create (const char *title, int h, int w, int y, int x,
37 window_func func, void *data, window_exit_func efunc,
38 window_resize_func rfunc)
40 int i = 0;
42 if (wins)
43 for (i = 0; wins[i]; i++);
45 wins = Realloc (wins, (i + 2) * sizeof (WIN *));
46 wins[i] = Calloc (1, sizeof (WIN));
47 wins[i]->w = newwin (h, w, y, x);
48 wins[i]->p = new_panel (wins[i]->w);
49 wins[i]->data = data;
50 wins[i]->rows = h;
51 wins[i]->cols = w;
52 wins[i]->func = func;
53 wins[i]->efunc = efunc;
54 wins[i]->rfunc = rfunc;
55 wins[i]->title = (title) ? strdup (title) : NULL;
56 wins[i + 1] = NULL;
57 return wins[i];
60 void
61 window_destroy (WIN * win)
63 int i, n;
64 WIN **new = NULL;
66 if (!wins)
67 return;
69 for (i = 0; wins[i]; i++);
71 while (i > 0 && wins[--i]->keep)
73 if (win && win == wins[i])
74 win = NULL;
76 free (wins[i]->title);
77 if (wins[i]->p)
78 del_panel (wins[i]->p);
80 delwin (wins[i]->w);
82 if (wins[i]->freedata && wins[i]->data)
83 free (wins[i]->data);
85 free (wins[i]);
86 wins[i] = NULL;
89 for (i = n = 0; wins[i]; i++)
91 if (win && !win->keep && win == wins[i])
93 if (win->p)
94 del_panel (win->p);
96 free (win->title);
97 delwin (win->w);
98 free (win);
99 win = NULL;
100 continue;
103 if (win && win->keep && win == wins[i])
105 del_panel (win->p);
106 win->p = NULL;
109 new = Realloc (new, (n + 2) * sizeof (WIN *));
110 new[n++] = wins[i];
113 free (wins);
114 wins = NULL;
116 if (new)
118 new[n] = NULL;
119 wins = new;
123 void
124 window_draw_title (WINDOW * win, const char *title, int width, chtype attr,
125 chtype battr)
127 int i;
129 if (title)
131 wchar_t *p;
133 wattron (win, attr);
135 for (i = 1; i < width - 1; i++)
136 mvwaddch (win, 1, i, ' ');
138 if (mblen (title, strlen (title)) > width)
140 p = str_etc (title, width - 2, 1);
142 else
143 p = str_to_wchar (title);
145 mvwaddwstr (win, 1, CENTERX (width, p), p);
146 wattroff (win, attr);
147 free (p);
150 wattron (win, battr);
151 box (win, ACS_VLINE, ACS_HLINE);
152 wattroff (win, battr);
155 void
156 window_draw_prompt (WINDOW * win, int y, int width, const char *str,
157 chtype attr)
159 int i;
161 wattron (win, attr);
163 for (i = 1; i < width - 1; i++)
164 mvwaddch (win, y, i, ' ');
166 wchar_t *promptw = str_to_wchar (str);
167 mvwprintw (win, y, CENTERX (width, promptw), "%ls", promptw);
168 free (promptw);
169 wattroff (win, attr);
172 void
173 window_resize_all ()
175 int i;
177 if (!wins)
178 return;
180 for (i = 0; wins[i]; i++)
182 WIN *w = wins[i];
184 if (!w->keep && w->rfunc)
185 w->rfunc (w);