1 /*
2 * widget.c - handles widget objects and the widget stack
3 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
4 *
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.
9 *
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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "aconfig.h"
20 #include <stdlib.h>
21 #include <term.h>
22 #include <unistd.h>
23 #include <sys/ioctl.h>
24 #include "die.h"
25 #include "widget.h"
26
27 int screen_lines;
28 int screen_cols;
29
30 static int cursor_visibility = -1;
31
update_cursor_visibility(void)32 static void update_cursor_visibility(void)
33 {
34 const struct widget *active_widget;
35
36 active_widget = get_active_widget();
37 if (active_widget &&
38 active_widget->cursor_visibility != cursor_visibility) {
39 cursor_visibility = active_widget->cursor_visibility;
40 curs_set(cursor_visibility);
41 }
42 }
43
widget_init(struct widget * widget,int lines_,int cols,int y,int x,chtype bkgd,unsigned int flags)44 void widget_init(struct widget *widget, int lines_, int cols, int y, int x,
45 chtype bkgd, unsigned int flags)
46 {
47 WINDOW *old_window;
48
49 if (y == SCREEN_CENTER)
50 y = (screen_lines - lines_) / 2;
51 if (x == SCREEN_CENTER)
52 x = (screen_cols - cols) / 2;
53
54 old_window = widget->window;
55 widget->window = newwin(lines_, cols, y, x);
56 if (!widget->window)
57 fatal_error("cannot create window");
58 keypad(widget->window, TRUE);
59 nodelay(widget->window, TRUE);
60 leaveok(widget->window, !(flags & WIDGET_CURSOR_VISIBLE));
61 wbkgdset(widget->window, bkgd);
62 werase(widget->window);
63
64 if (flags & WIDGET_BORDER)
65 box(widget->window, 0, 0);
66 if (flags & WIDGET_SUBWINDOW) {
67 if (widget->subwindow)
68 delwin(widget->subwindow);
69 widget->subwindow = derwin(widget->window,
70 lines_ - 2, cols - 2, 1, 1);
71 if (!widget->subwindow)
72 fatal_error("cannot create subwindow");
73 wbkgdset(widget->subwindow, bkgd);
74 }
75 widget->cursor_visibility = !!(flags & WIDGET_CURSOR_VISIBLE);
76
77 if (widget->panel) {
78 replace_panel(widget->panel, widget->window);
79 } else {
80 widget->panel = new_panel(widget->window);
81 if (!widget->panel)
82 fatal_error("cannot create panel");
83 set_panel_userptr(widget->panel, widget);
84 }
85
86 if (old_window)
87 delwin(old_window);
88
89 update_cursor_visibility();
90 }
91
widget_free(struct widget * widget)92 void widget_free(struct widget *widget)
93 {
94 if (widget->panel) {
95 del_panel(widget->panel);
96 widget->panel = NULL;
97 }
98 if (widget->subwindow) {
99 delwin(widget->subwindow);
100 widget->subwindow = NULL;
101 }
102 if (widget->window) {
103 delwin(widget->window);
104 widget->window = NULL;
105 }
106
107 update_cursor_visibility();
108 }
109
get_active_widget(void)110 const struct widget *get_active_widget(void)
111 {
112 PANEL *active_panel;
113
114 active_panel = panel_below(NULL);
115 if (active_panel)
116 return panel_userptr(active_panel);
117 else
118 return NULL;
119 }
120
window_size_changed(void)121 void window_size_changed(void)
122 {
123 PANEL *panel, *below;
124 const struct widget *widget;
125 struct winsize size;
126
127 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) == 0)
128 resize_term(size.ws_row, size.ws_col);
129
130 getmaxyx(stdscr, screen_lines, screen_cols);
131 if (tigetflag("xenl") != 1 && tigetflag("am") != 1)
132 --screen_lines;
133
134 for (panel = panel_below(NULL); panel; panel = below) {
135 below = panel_below(panel);
136 widget = panel_userptr(panel);
137 widget->window_size_changed();
138 }
139 }
140