NeoMutt  2025-12-11-800-ga0ee0f
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
utilwin.c
Go to the documentation of this file.
1
22
57
58#include "config.h"
59#include <stdbool.h>
60#include <stddef.h>
61#include "mutt/lib.h"
62#include "core/lib.h"
63#include "utilwin.h"
64#include "color/lib.h"
65#include "key/lib.h"
66#include "module_data.h"
67#include "mutt_curses.h"
68#include "mutt_window.h"
69
71#define UTILWIN_COLS 10
72
77{
78 char *text;
79};
80
84static int utilwin_recalc(struct MuttWindow *win)
85{
86 win->actions |= WA_REPAINT;
87 mutt_debug(LL_DEBUG5, "recalc done, request WA_REPAINT\n");
88 return 0;
89}
90
97static int utilwin_repaint(struct MuttWindow *win)
98{
99 struct UtilWinData *priv = win->wdata;
100
102
103 // Blank any extra rows above the bottom line
104 for (int r = 0; r < (win->state.rows - 1); r++)
105 {
106 mutt_window_move(win, r, 0);
108 }
109
110 int row = win->state.rows - 1;
111 mutt_window_move(win, row, 0);
112
113 if (priv->text)
114 mutt_window_addstr(win, priv->text);
115
117
118 mutt_debug(LL_DEBUG5, "utilwin repaint done\n");
119 return 0;
120}
121
125static void utilwin_wdata_free(struct MuttWindow *win, void **ptr)
126{
127 if (!ptr || !*ptr)
128 return;
129
130 struct UtilWinData *priv = *ptr;
131 FREE(&priv->text);
132 FREE(ptr);
133}
134
143{
144 if (nc->event_type != NT_KEY)
145 return 0;
147 return 0;
148 if (!nc->global_data || !nc->event_data)
149 return -1;
150
151 struct MuttWindow *win = nc->global_data;
152 struct EventKeyProgress *ev_k = nc->event_data;
153
154 // Nothing to display — clear/hide the window
155 if ((ev_k->count == 0) && (ev_k->key_len == 0))
156 {
157 utilwin_set_text(win, NULL);
158 window_redraw(NULL);
159 return 0;
160 }
161
162 struct Buffer *buf = buf_pool_get();
163
164 if (ev_k->count > 0)
165 buf_add_printf(buf, "%d", ev_k->count);
166
167 for (int i = 0; i < ev_k->key_len; i++)
168 {
169 keymap_get_name(ev_k->keys[i], buf);
170 }
171
172 utilwin_set_text(win, buf_string(buf));
173 buf_pool_release(&buf);
174 window_redraw(NULL);
175
176 return 0;
177}
178
183{
184 if (nc->event_type != NT_WINDOW)
185 return 0;
186 if (!nc->global_data || !nc->event_data)
187 return -1;
188
189 struct MuttWindow *win = nc->global_data;
190 struct EventWindow *ev_w = nc->event_data;
191 if (ev_w->win != win)
192 return 0;
193
195 {
197 mutt_debug(LL_DEBUG5, "window state done, request WA_REPAINT\n");
198 }
199 else if (nc->event_subtype == NT_WINDOW_DELETE)
200 {
203 mutt_debug(LL_DEBUG5, "window delete done\n");
204 }
205
206 return 0;
207}
208
216{
219
220 struct UtilWinData *priv = MUTT_MEM_CALLOC(1, struct UtilWinData);
221
222 win->wdata = priv;
224 win->recalc = utilwin_recalc;
226
227 window_set_visible(win, false);
228
230 if (mod_data)
231 mod_data->utility_window = win;
232
235
236 return win;
237}
238
247const char *utilwin_get_text(struct MuttWindow *win)
248{
249 if (!win)
250 {
252 if (mod_data)
253 win = mod_data->utility_window;
254 }
255 if (!win)
256 return NULL;
257
258 struct UtilWinData *priv = win->wdata;
259 return priv->text;
260}
261
271void utilwin_set_text(struct MuttWindow *win, const char *text)
272{
273 if (!win)
274 {
276 if (mod_data)
277 win = mod_data->utility_window;
278 }
279 if (!win)
280 return;
281
282 struct UtilWinData *priv = win->wdata;
283
284 if (!text || (*text == '\0'))
285 {
286 // Clear the old on-screen contents before hiding the window.
287 // Hidden windows are skipped by repaint, so they can't erase themselves.
289 FREE(&priv->text);
290 window_set_visible(win, false);
291 mutt_window_reflow(NULL);
292 return;
293 }
294
295 mutt_str_replace(&priv->text, text);
296
297 window_set_visible(win, true);
298 win->actions |= WA_RECALC;
299 mutt_window_reflow(NULL);
300}
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition buffer.c:204
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
Color and attribute parsing.
@ MT_COLOR_NORMAL
Plain text.
Definition color.h:53
Convenience wrapper for the core headers.
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
static int utilwin_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition utilwin.c:182
static int utilwin_key_observer(struct NotifyCallback *nc)
Notification that key progress has changed - Implements observer_t -.
Definition utilwin.c:142
static int utilwin_recalc(struct MuttWindow *win)
Recalculate the Utility Window - Implements MuttWindow::recalc() -.
Definition utilwin.c:84
static int utilwin_repaint(struct MuttWindow *win)
Repaint the Utility Window - Implements MuttWindow::repaint() -.
Definition utilwin.c:97
static void utilwin_wdata_free(struct MuttWindow *win, void **ptr)
Free the private data - Implements MuttWindow::wdata_free() -.
Definition utilwin.c:125
Gui private Module data.
void keymap_get_name(int c, struct Buffer *buf)
Get the human name for a key.
Definition keymap.c:204
Manage keymappings.
@ NT_KEY_PROGRESS
Key state has changed.
Definition notify.h:72
@ LL_DEBUG5
Log at debug level 5.
Definition logging2.h:49
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
@ MODULE_ID_GUI
ModuleGui, Graphical code
Definition module_api.h:45
Convenience wrapper for the library headers.
bool notify_observer_remove(struct Notify *notify, const observer_t callback, const void *global_data)
Remove an observer from an object.
Definition notify.c:230
bool notify_observer_add(struct Notify *notify, enum NotifyType type, observer_t callback, void *global_data)
Add an observer to an object.
Definition notify.c:191
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition string.c:284
const struct AttrColor * mutt_curses_set_color_by_id(enum ColorId cid)
Set the colour and attributes by the Colour ID.
Definition mutt_curses.c:79
Define wrapper functions around Curses.
void mutt_window_clear(struct MuttWindow *win)
Clear a Window.
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
void mutt_window_reflow(struct MuttWindow *win)
Resize a Window and its children.
struct MuttWindow * mutt_window_new(enum WindowType type, enum MuttWindowOrientation orient, enum MuttWindowSize size, int cols, int rows)
Create a new Window.
int mutt_window_move(struct MuttWindow *win, int row, int col)
Move the cursor in a Window.
void window_set_visible(struct MuttWindow *win, bool visible)
Set a Window visible or hidden.
int mutt_window_addstr(struct MuttWindow *win, const char *str)
Write a string to a Window.
void mutt_window_clrtoeol(struct MuttWindow *win)
Clear to the end of the line.
Window management.
#define WA_RECALC
Recalculate the contents of the Window.
@ WT_CUSTOM
Window with a custom drawing function.
Definition mutt_window.h:94
@ MUTT_WIN_ORIENT_VERTICAL
Window uses all available vertical space.
Definition mutt_window.h:38
@ NT_WINDOW_STATE
Window state has changed, e.g. WN_VISIBLE.
@ NT_WINDOW_DELETE
Window is about to be deleted.
#define WA_REPAINT
Redraw the contents of the Window.
@ MUTT_WIN_SIZE_FIXED
Window has a fixed size.
Definition mutt_window.h:47
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:663
@ NT_WINDOW
MuttWindow has changed, NotifyWindow, EventWindow.
Definition notify_type.h:58
@ NT_KEY
Runtime key handling changed, NotifyKey, EventKeyProgress.
Definition notify_type.h:49
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
String manipulation buffer.
Definition buffer.h:36
Runtime key progress event.
Definition notify.h:58
short key_len
Number of keys entered.
Definition notify.h:61
keycode_t keys[KEY_SEQ_MAX_LEN]
Entered keys.
Definition notify.h:62
int count
Parsed count prefix, if any.
Definition notify.h:60
An Event that happened to a Window.
struct MuttWindow * win
Window that changed.
Gui private Module data.
Definition module_data.h:32
struct MuttWindow * utility_window
Utility Window.
Definition module_data.h:42
int(* repaint)(struct MuttWindow *win)
struct WindowState state
Current state of the Window.
void * wdata
Private data.
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
int(* recalc)(struct MuttWindow *win)
void(* wdata_free)(struct MuttWindow *win, void **ptr)
WindowActionFlags actions
Actions to be performed, e.g. WA_RECALC.
Container for Accounts, Notifications.
Definition neomutt.h:41
struct Notify * notify
Notifications handler.
Definition neomutt.h:45
Data passed to a notification function.
Definition observer.h:34
void * event_data
Data from notify_send()
Definition observer.h:38
enum NotifyType event_type
Send: Event type, e.g. NT_ACCOUNT.
Definition observer.h:36
int event_subtype
Send: Event subtype, e.g. NT_ACCOUNT_ADD.
Definition observer.h:37
void * global_data
Data from notify_observer_add()
Definition observer.h:39
Utility Window private data.
Definition utilwin.c:77
char * text
Cached display string.
Definition utilwin.c:78
short rows
Number of rows, can be MUTT_WIN_SIZE_UNLIMITED.
Definition mutt_window.h:61
const char * utilwin_get_text(struct MuttWindow *win)
Get the text from the Utility Window.
Definition utilwin.c:247
void utilwin_set_text(struct MuttWindow *win, const char *text)
Set the text for the Utility Window.
Definition utilwin.c:271
struct MuttWindow * utilwin_new(void)
Create the Utility Window.
Definition utilwin.c:215
#define UTILWIN_COLS
Width of the Utility Window in columns.
Definition utilwin.c:71
Utility Window.