Initial multibyte string output.
[cboard.git] / src / window.c
blobbcdde609024290fa021346d0af8482c92a4d63d8
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2002-2013 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 #ifdef HAVE_NCURSES_H
27 #include <ncurses.h>
28 #elif defined(HAVE_CURSES_H)
29 #include <curses.h>
30 #endif
32 #ifdef HAVE_PANEL_H
33 #include <panel.h>
34 #endif
36 #include "misc.h"
37 #include "window.h"
40 * Creates a new window on the 'wins' stack. Returns the newly create window
41 * structure. The 'func' parameter is a function pointer that is called from
42 * game_loop(). 'efunc' is called just before the window is destroyed.
44 WIN *window_create(const char *title, int h, int w, int y, int x,
45 window_func func, void *data, window_exit_func efunc)
47 int i = 0;
49 if (wins)
50 for (i = 0; wins[i]; i++);
52 wins = Realloc(wins, (i + 2) * sizeof(WIN *));
53 wins[i] = Calloc(1, sizeof(WIN));
54 wins[i]->w = newwin(h, w, y, x);
55 wins[i]->p = new_panel(wins[i]->w);
56 wins[i]->data = data;
57 wins[i]->rows = h;
58 wins[i]->cols = w;
59 wins[i]->func = func;
60 wins[i]->efunc = efunc;
61 wins[i]->title = (title) ? strdup(title) : NULL;
62 wins[i+1] = NULL;
63 return wins[i];
66 void window_destroy(WIN *win)
68 int i, n;
69 WIN **new = NULL;
71 if (!wins)
72 return;
74 for (i = 0; wins[i]; i++);
76 while (i > 0 && wins[--i]->keep) {
77 if (win && memcmp(win, wins[i], sizeof(WIN)) == 0)
78 win = NULL;
80 free(wins[i]->title);
81 if (wins[i]->p)
82 del_panel(wins[i]->p);
84 delwin(wins[i]->w);
86 if (wins[i]->freedata && wins[i]->data)
87 free(wins[i]->data);
89 free(wins[i]);
90 wins[i] = NULL;
93 for (i = n = 0; wins[i]; i++) {
94 if (win && !win->keep && memcmp(win, wins[i], sizeof(WIN)) == 0) {
95 if (win->p)
96 del_panel(win->p);
98 free(win->title);
99 delwin(win->w);
100 free(win);
101 continue;
104 if (win && win->keep && memcmp(win, wins[i], sizeof(WIN)) == 0) {
105 del_panel(win->p);
106 win->p = NULL;
109 new = Realloc(new, (n + 2) * sizeof(WIN *));
110 new[n] = wins[i];
111 n++;
114 free(wins);
115 wins = NULL;
117 if (new) {
118 if (n) {
119 new[n] = NULL;
120 wins = new;
122 else
123 free(new);
127 void window_draw_title(WINDOW *win, const char *title, int width, chtype attr,
128 chtype battr)
130 int i;
132 if (title) {
133 wchar_t *p;
135 wattron(win, attr);
137 for (i = 1; i < width - 1; i++)
138 mvwaddch(win, 1, i, ' ');
140 if (mblen (title, strlen(title)) > width) {
141 p = str_etc(title, width - 2, 1);
143 else
144 p = str_to_wchar (title);
146 mvwaddwstr(win, 1, CENTERX(width, p), p);
147 wattroff(win, attr);
148 free (p);
151 wattron(win, battr);
152 box(win, ACS_VLINE, ACS_HLINE);
153 wattroff(win, battr);
156 void 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);