forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsbar.c
183 lines (152 loc) · 4.65 KB
/
sbar.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* @file
* Simple Bar (status)
*
* @authors
* Copyright (C) 2021 Richard Russon <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page gui_sbar Simple Bar (status)
*
* Simple Bar (status)
*/
#include "config.h"
#include <stddef.h>
#include "mutt/lib.h"
#include "core/lib.h"
#include "lib.h"
/**
* struct SBarPrivateData - Private data for the Simple Bar
*/
struct SBarPrivateData
{
char *display; ///< Cached display string
};
/**
* sbar_recalc - Recalculate the Window data - Implements MuttWindow::recalc()
*/
static int sbar_recalc(struct MuttWindow *win)
{
if (!win)
return -1;
win->actions |= WA_REPAINT;
mutt_debug(LL_DEBUG5, "recalc done, request WA_REPAINT\n");
return 0;
}
/**
* sbar_repaint - Repaint the Window - Implements MuttWindow::repaint()
*/
static int sbar_repaint(struct MuttWindow *win)
{
if (!mutt_window_is_visible(win))
return 0;
struct SBarPrivateData *priv = win->wdata;
mutt_window_move(win, 0, 0);
mutt_curses_set_color(MT_COLOR_STATUS);
mutt_window_move(win, 0, 0);
mutt_paddstr(win, win->state.cols, priv->display);
mutt_curses_set_color(MT_COLOR_NORMAL);
mutt_debug(LL_DEBUG5, "repaint done\n");
return 0;
}
/**
* sbar_color_observer - Notification that a Color has changed - Implements ::observer_t
*/
static int sbar_color_observer(struct NotifyCallback *nc)
{
if ((nc->event_type != NT_COLOR) || !nc->global_data)
return -1;
struct EventColor *ev_c = nc->event_data;
// MT_COLOR_MAX is sent on `uncolor *`
if ((ev_c->color != MT_COLOR_STATUS) && (ev_c->color != MT_COLOR_MAX))
return 0;
struct MuttWindow *win_sbar = nc->global_data;
win_sbar->actions |= WA_REPAINT;
mutt_debug(LL_DEBUG5, "color done, request WA_REPAINT\n");
return 0;
}
/**
* sbar_window_observer - Notification that a Window has changed - Implements ::observer_t
*/
static int sbar_window_observer(struct NotifyCallback *nc)
{
if ((nc->event_type != NT_WINDOW) || !nc->global_data || !nc->event_data)
return -1;
struct MuttWindow *win_sbar = nc->global_data;
if (nc->event_subtype == NT_WINDOW_STATE)
{
win_sbar->actions |= WA_REPAINT;
mutt_debug(LL_DEBUG5, "window state done, request WA_REPAINT\n");
}
else if (nc->event_subtype == NT_WINDOW_DELETE)
{
notify_observer_remove(NeoMutt->notify, sbar_color_observer, win_sbar);
notify_observer_remove(win_sbar->notify, sbar_window_observer, win_sbar);
mutt_debug(LL_DEBUG5, "window delete done\n");
}
return 0;
}
/**
* sbar_wdata_free - Free the private data attached to the MuttWindow - Implements MuttWindow::wdata_free()
*/
static void sbar_wdata_free(struct MuttWindow *win, void **ptr)
{
struct SBarPrivateData *priv = *ptr;
FREE(&priv->display);
FREE(ptr);
}
/**
* sbar_data_new - Free the private data attached to the MuttWindow
*/
static struct SBarPrivateData *sbar_data_new(void)
{
struct SBarPrivateData *sbar_data = mutt_mem_calloc(1, sizeof(struct SBarPrivateData));
return sbar_data;
}
/**
* sbar_new - Add the Simple Bar (status)
* @param parent Parent Window
* @retval ptr New Simple Bar
*/
struct MuttWindow *sbar_new(struct MuttWindow *parent)
{
struct MuttWindow *win_sbar =
mutt_window_new(WT_STATUS_BAR, MUTT_WIN_ORIENT_VERTICAL,
MUTT_WIN_SIZE_FIXED, MUTT_WIN_SIZE_UNLIMITED, 1);
win_sbar->wdata = sbar_data_new();
win_sbar->wdata_free = sbar_wdata_free;
win_sbar->recalc = sbar_recalc;
win_sbar->repaint = sbar_repaint;
notify_observer_add(NeoMutt->notify, NT_COLOR, sbar_color_observer, win_sbar);
notify_observer_add(win_sbar->notify, NT_WINDOW, sbar_window_observer, win_sbar);
return win_sbar;
}
/**
* sbar_set_title - Set the title for the Simple Bar
* @param win Window of the Simple Bar
* @param title String to set
*
* @note The title string will be copied
*/
void sbar_set_title(struct MuttWindow *win, const char *title)
{
if (!win || !win->wdata || (win->type != WT_STATUS_BAR))
return;
struct SBarPrivateData *priv = win->wdata;
mutt_str_replace(&priv->display, title);
win->actions |= WA_REPAINT;
}