NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
ibar.c
Go to the documentation of this file.
1 
64 #include "config.h"
65 #include <stdbool.h>
66 #include "mutt/lib.h"
67 #include "config/lib.h"
68 #include "core/lib.h"
69 #include "gui/lib.h"
70 #include "lib.h"
71 #include "private_data.h"
72 #include "shared_data.h"
73 #include "status.h"
74 
79 {
82  char *status_format;
85 };
86 
90 static int ibar_recalc(struct MuttWindow *win)
91 {
92  char buf[1024] = { 0 };
93 
94  struct IBarPrivateData *ibar_data = win->wdata;
95  struct IndexSharedData *shared = ibar_data->shared;
96  struct IndexPrivateData *priv = ibar_data->priv;
97 
98  const char *c_status_format = cs_subset_string(shared->sub, "status_format");
99  menu_status_line(buf, sizeof(buf), shared, priv->menu, win->state.cols,
100  NONULL(c_status_format));
101 
102  if (!mutt_str_equal(buf, ibar_data->status_format))
103  {
104  mutt_str_replace(&ibar_data->status_format, buf);
105  win->actions |= WA_REPAINT;
106  mutt_debug(LL_DEBUG5, "recalc done, request WA_REPAINT\n");
107  }
108 
109  const bool c_ts_enabled = cs_subset_bool(shared->sub, "ts_enabled");
110  if (c_ts_enabled && TsSupported)
111  {
112  const char *c_ts_status_format =
113  cs_subset_string(shared->sub, "ts_status_format");
114  menu_status_line(buf, sizeof(buf), shared, priv->menu, sizeof(buf),
115  NONULL(c_ts_status_format));
116  if (!mutt_str_equal(buf, ibar_data->ts_status_format))
117  {
118  mutt_str_replace(&ibar_data->ts_status_format, buf);
119  win->actions |= WA_REPAINT;
120  mutt_debug(LL_DEBUG5, "recalc done, request WA_REPAINT\n");
121  }
122 
123  const char *c_ts_icon_format =
124  cs_subset_string(shared->sub, "ts_icon_format");
125  menu_status_line(buf, sizeof(buf), shared, priv->menu, sizeof(buf),
126  NONULL(c_ts_icon_format));
127  if (!mutt_str_equal(buf, ibar_data->ts_icon_format))
128  {
129  mutt_str_replace(&ibar_data->ts_icon_format, buf);
130  win->actions |= WA_REPAINT;
131  mutt_debug(LL_DEBUG5, "recalc done, request WA_REPAINT\n");
132  }
133  }
134 
135  return 0;
136 }
137 
141 static int ibar_repaint(struct MuttWindow *win)
142 {
143  if (!mutt_window_is_visible(win))
144  return 0;
145 
146  struct IBarPrivateData *ibar_data = win->wdata;
147  struct IndexSharedData *shared = ibar_data->shared;
148 
149  mutt_window_move(win, 0, 0);
152 
153  mutt_window_move(win, 0, 0);
154  mutt_draw_statusline(win, win->state.cols, ibar_data->status_format,
155  mutt_str_len(ibar_data->status_format));
157 
158  const bool c_ts_enabled = cs_subset_bool(shared->sub, "ts_enabled");
159  if (c_ts_enabled && TsSupported)
160  {
161  mutt_ts_status(ibar_data->ts_status_format);
162  mutt_ts_icon(ibar_data->ts_icon_format);
163  }
164 
165  mutt_debug(LL_DEBUG5, "repaint done\n");
166  return 0;
167 }
168 
172 static int ibar_color_observer(struct NotifyCallback *nc)
173 {
174  if ((nc->event_type != NT_COLOR) || !nc->global_data || !nc->event_data)
175  return -1;
176 
177  struct EventColor *ev_c = nc->event_data;
178 
179  // MT_COLOR_MAX is sent on `uncolor *`
180  if ((ev_c->color != MT_COLOR_STATUS) && (ev_c->color != MT_COLOR_MAX))
181  return 0;
182 
183  struct MuttWindow *win_ibar = nc->global_data;
184  win_ibar->actions |= WA_REPAINT;
185  mutt_debug(LL_DEBUG5, "color done, request WA_REPAINT\n");
186 
187  return 0;
188 }
189 
193 static int ibar_config_observer(struct NotifyCallback *nc)
194 {
195  if ((nc->event_type != NT_CONFIG) || !nc->global_data || !nc->event_data)
196  return -1;
197 
198  struct EventConfig *ev_c = nc->event_data;
199  if ((ev_c->name[0] != 's') && (ev_c->name[0] != 't'))
200  return 0;
201 
202  if (!mutt_str_equal(ev_c->name, "status_format") &&
203  !mutt_str_equal(ev_c->name, "ts_enabled") &&
204  !mutt_str_equal(ev_c->name, "ts_icon_format") &&
205  !mutt_str_equal(ev_c->name, "ts_status_format"))
206  {
207  return 0;
208  }
209 
210  struct MuttWindow *win_ibar = nc->global_data;
211  win_ibar->actions |= WA_RECALC;
212  mutt_debug(LL_DEBUG5, "config done, request WA_RECALC\n");
213 
214  return 0;
215 }
216 
220 static int ibar_index_observer(struct NotifyCallback *nc)
221 {
222  if ((nc->event_type != NT_INDEX) || !nc->global_data)
223  return -1;
224 
225  struct MuttWindow *win_ibar = nc->global_data;
226  if (!win_ibar)
227  return 0;
228 
229  struct IndexSharedData *shared = nc->event_data;
230  if (!shared)
231  return 0;
232 
234  {
235  win_ibar->actions |= WA_RECALC;
236  mutt_debug(LL_DEBUG5, "index done, request WA_RECALC\n");
237  }
238 
239  if (nc->event_subtype & NT_INDEX_EMAIL)
240  {
241  win_ibar->actions |= WA_RECALC;
242  mutt_debug(LL_DEBUG5, "index done, request WA_RECALC\n");
243  }
244 
245  return 0;
246 }
247 
251 static int ibar_menu_observer(struct NotifyCallback *nc)
252 {
253  if ((nc->event_type != NT_MENU) || !nc->global_data)
254  return -1;
255 
256  struct MuttWindow *win_ibar = nc->global_data;
257  win_ibar->actions |= WA_RECALC;
258  mutt_debug(LL_DEBUG5, "menu done, request WA_RECALC\n");
259 
260  return 0;
261 }
262 
266 static int ibar_window_observer(struct NotifyCallback *nc)
267 {
268  if ((nc->event_type != NT_WINDOW) || !nc->global_data)
269  return -1;
270 
271  struct MuttWindow *win_ibar = nc->global_data;
272  struct EventWindow *ev_w = nc->event_data;
273  if (ev_w->win != win_ibar)
274  return 0;
275 
276  if (nc->event_subtype == NT_WINDOW_STATE)
277  {
278  win_ibar->actions |= WA_REPAINT;
279  mutt_debug(LL_DEBUG5, "window state done, request WA_REPAINT\n");
280  }
281  else if (nc->event_subtype == NT_WINDOW_DELETE)
282  {
283  struct MuttWindow *dlg = window_find_parent(win_ibar, WT_DLG_INDEX);
284  struct IndexSharedData *shared = dlg->wdata;
285 
290  notify_observer_remove(win_ibar->notify, ibar_window_observer, win_ibar);
291 
292  mutt_debug(LL_DEBUG5, "window delete done\n");
293  }
294 
295  return 0;
296 }
297 
301 static void ibar_data_free(struct MuttWindow *win, void **ptr)
302 {
303  struct IBarPrivateData *ibar_data = *ptr;
304 
305  FREE(&ibar_data->status_format);
306  FREE(&ibar_data->ts_status_format);
307  FREE(&ibar_data->ts_icon_format);
308 
309  FREE(ptr);
310 }
311 
316  struct IndexPrivateData *priv)
317 {
318  struct IBarPrivateData *ibar_data = mutt_mem_calloc(1, sizeof(struct IBarPrivateData));
319 
320  ibar_data->shared = shared;
321  ibar_data->priv = priv;
322 
323  return ibar_data;
324 }
325 
334  struct IndexPrivateData *priv)
335 {
336  struct MuttWindow *win_ibar =
339 
340  win_ibar->wdata = ibar_data_new(shared, priv);
341  win_ibar->wdata_free = ibar_data_free;
342  win_ibar->recalc = ibar_recalc;
343  win_ibar->repaint = ibar_repaint;
344 
350 
351  return win_ibar;
352 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:904
Convenience wrapper for the gui headers.
char * ts_icon_format
Cached terminal icon string.
Definition: ibar.c:84
static int ibar_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t.
Definition: ibar.c:266
#define NONULL(x)
Definition: string2.h:37
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
void(* wdata_free)(struct MuttWindow *win, void **ptr)
Definition: mutt_window.h:160
static int ibar_repaint(struct MuttWindow *win)
Repaint the Window - Implements MuttWindow::repaint() -.
Definition: ibar.c:141
#define WA_REPAINT
Redraw the contents of the Window.
Definition: mutt_window.h:111
Data passed to a notification function.
Definition: observer.h:39
struct MuttWindow * win
Window that changed.
Definition: mutt_window.h:217
struct IndexSharedData * shared
Shared Index data.
Definition: ibar.c:80
struct Menu * menu
Menu controlling the index.
Definition: private_data.h:44
An Event that happened to a Colour.
Definition: color.h:122
An Event that happened to a Window.
Definition: mutt_window.h:215
Private state data for the Index.
Definition: private_data.h:34
#define NT_INDEX_EMAIL
Email has changed.
Definition: lib.h:65
Window uses all available vertical space.
Definition: mutt_window.h:38
void mutt_curses_set_color(enum ColorId color)
Set the current colour for text.
Definition: mutt_curses.c:56
struct ConfigSubset * sub
Config set to use.
Definition: shared_data.h:38
void mutt_window_clrtoeol(struct MuttWindow *win)
Clear to the end of the line.
Definition: mutt_window.c:259
char * status_format
Cached screen status string.
Definition: ibar.c:82
A config-change event.
Definition: subset.h:69
Menu has changed, MenuRedrawFlags.
Definition: notify_type.h:49
Private state data for the Index.
Window is about to be deleted.
Definition: mutt_window.h:206
Data shared between Index, Pager and Sidebar.
Definition: shared_data.h:36
char * ts_status_format
Cached terminal status string.
Definition: ibar.c:83
static struct IBarPrivateData * ibar_data_new(struct IndexSharedData *shared, struct IndexPrivateData *priv)
Free the private data attached to the MuttWindow.
Definition: ibar.c:315
#define NT_INDEX_MAILBOX
Mailbox has changed.
Definition: lib.h:64
Container for Accounts, Notifications.
Definition: neomutt.h:36
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:53
struct Notify * notify
Notifications: NotifyIndex, IndexSharedData.
Definition: shared_data.h:44
Status Bar containing extra info about the Index/Pager/etc.
Definition: mutt_window.h:102
Convenience wrapper for the config headers.
int mutt_window_move(struct MuttWindow *win, int col, int row)
Move the cursor in a Window.
Definition: mutt_window.c:310
int event_subtype
Send: Event subtype, e.g. NT_ACCOUNT_ADD.
Definition: observer.h:43
enum NotifyType event_type
Send: Event type, e.g. NT_ACCOUNT.
Definition: observer.h:42
WindowActionFlags actions
Actions to be performed, e.g. WA_RECALC.
Definition: mutt_window.h:132
Index data has changed, NotifyIndex, IndexSharedData.
Definition: notify_type.h:47
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Definition: mutt_window.h:138
Convenience wrapper for the core headers.
static void ibar_data_free(struct MuttWindow *win, void **ptr)
Free the private data attached to the MuttWindow - Implements MuttWindow::wdata_free() -...
Definition: ibar.c:301
struct IndexPrivateData * priv
Private Index data.
Definition: ibar.c:81
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:189
void * global_data
Data from notify_observer_add()
Definition: observer.h:45
Window has a fixed size.
Definition: mutt_window.h:47
Plain text.
Definition: color.h:58
Window state has changed, e.g. WN_VISIBLE.
Definition: mutt_window.h:207
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:60
Colour has changed, NotifyColor, EventColor.
Definition: notify_type.h:39
struct WindowState state
Current state of the Window.
Definition: mutt_window.h:127
bool mutt_window_is_visible(struct MuttWindow *win)
Is the Window visible?
Definition: mutt_window.c:529
Status bar (takes a pattern)
Definition: color.h:75
#define MUTT_WIN_SIZE_UNLIMITED
Use as much space as possible.
Definition: mutt_window.h:52
void mutt_draw_statusline(struct MuttWindow *win, int cols, const char *buf, size_t buflen)
Draw a highlighted status bar.
Definition: dlg_index.c:875
struct MuttWindow * window_find_parent(struct MuttWindow *win, enum WindowType type)
Find a (grand-)parent of a Window by type.
Definition: mutt_window.c:575
enum ColorId color
Definition: color.h:124
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
static int ibar_recalc(struct MuttWindow *win)
Recalculate the Window data - Implements MuttWindow::recalc() -.
Definition: ibar.c:90
static int ibar_color_observer(struct NotifyCallback *nc)
Notification that a Color has changed - Implements observer_t.
Definition: ibar.c:172
void menu_status_line(char *buf, size_t buflen, struct IndexSharedData *shared, struct Menu *menu, int cols, const char *fmt)
Create the status line.
Definition: status.c:445
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
bool TsSupported
Terminal Setting is supported.
Definition: terminal.c:43
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
Data to draw the Index Bar.
Definition: ibar.c:78
void * event_data
Data from notify_send()
Definition: observer.h:44
GUI display a user-configurable status line.
Config has changed, NotifyConfig, EventConfig.
Definition: notify_type.h:42
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:664
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:446
struct MuttWindow * parent
Parent Window.
Definition: mutt_window.h:135
static int ibar_index_observer(struct NotifyCallback *nc)
Notification that the Index has changed - Implements observer_t.
Definition: ibar.c:220
int(* repaint)(struct MuttWindow *win)
Definition: mutt_window.h:182
#define FREE(x)
Definition: memory.h:40
void mutt_ts_icon(char *str)
Set the icon in the terminal title bar.
Definition: terminal.c:118
bool notify_observer_remove(struct Notify *notify, observer_t callback, void *global_data)
Remove an observer from an object.
Definition: notify.c:228
int(* recalc)(struct MuttWindow *win)
Definition: mutt_window.h:171
void mutt_ts_status(char *str)
Set the text of the terminal title bar.
Definition: terminal.c:104
struct MuttWindow * mutt_window_new(enum WindowType type, enum MuttWindowOrientation orient, enum MuttWindowSize size, int cols, int rows)
Create a new Window.
Definition: mutt_window.c:180
Index Dialog, index_pager_init()
Definition: mutt_window.h:86
Log at debug level 5.
Definition: logging.h:44
Convenience wrapper for the library headers.
struct MuttWindow * ibar_new(struct MuttWindow *parent, struct IndexSharedData *shared, struct IndexPrivateData *priv)
Create the Index Bar (status)
Definition: ibar.c:333
void * wdata
Private data.
Definition: mutt_window.h:145
const char * name
Name of config item that changed.
Definition: subset.h:72
static int ibar_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t.
Definition: ibar.c:193
static int ibar_menu_observer(struct NotifyCallback *nc)
Notification that a Menu has changed - Implements observer_t.
Definition: ibar.c:251
Data shared between Index, Pager and Sidebar.
#define WA_RECALC
Recalculate the contents of the Window.
Definition: mutt_window.h:110