NeoMutt  2021-10-29-220-g2b1eec
Teaching an old dog new tricks
DOXYGEN
sbar.c
Go to the documentation of this file.
1 
63 #include "config.h"
64 #include "mutt/lib.h"
65 #include "core/lib.h"
66 #include "lib.h"
67 #include "color/lib.h"
68 
73 {
74  char *display;
75 };
76 
80 static int sbar_recalc(struct MuttWindow *win)
81 {
82  if (!win)
83  return -1;
84 
85  win->actions |= WA_REPAINT;
86  mutt_debug(LL_DEBUG5, "recalc done, request WA_REPAINT\n");
87  return 0;
88 }
89 
93 static int sbar_repaint(struct MuttWindow *win)
94 {
95  if (!mutt_window_is_visible(win))
96  return 0;
97 
98  struct SBarPrivateData *priv = win->wdata;
99 
100  mutt_window_move(win, 0, 0);
101 
103  mutt_window_move(win, 0, 0);
104  mutt_paddstr(win, win->state.cols, priv->display);
106 
107  mutt_debug(LL_DEBUG5, "repaint done\n");
108  return 0;
109 }
110 
117 static int sbar_color_observer(struct NotifyCallback *nc)
118 {
119  if ((nc->event_type != NT_COLOR) || !nc->global_data)
120  return -1;
121 
122  struct EventColor *ev_c = nc->event_data;
123 
124  // MT_COLOR_MAX is sent on `uncolor *`
125  if ((ev_c->cid != MT_COLOR_STATUS) && (ev_c->cid != MT_COLOR_MAX))
126  return 0;
127 
128  struct MuttWindow *win_sbar = nc->global_data;
129 
130  win_sbar->actions |= WA_REPAINT;
131  mutt_debug(LL_DEBUG5, "color done, request WA_REPAINT\n");
132 
133  return 0;
134 }
135 
144 static int sbar_window_observer(struct NotifyCallback *nc)
145 {
146  if ((nc->event_type != NT_WINDOW) || !nc->global_data || !nc->event_data)
147  return -1;
148 
149  struct MuttWindow *win_sbar = nc->global_data;
150  struct EventWindow *ev_w = nc->event_data;
151  if (ev_w->win != win_sbar)
152  return 0;
153 
154  if (nc->event_subtype == NT_WINDOW_STATE)
155  {
156  win_sbar->actions |= WA_REPAINT;
157  mutt_debug(LL_DEBUG5, "window state done, request WA_REPAINT\n");
158  }
159  else if (nc->event_subtype == NT_WINDOW_DELETE)
160  {
162  notify_observer_remove(win_sbar->notify, sbar_window_observer, win_sbar);
163  mutt_debug(LL_DEBUG5, "window delete done\n");
164  }
165 
166  return 0;
167 }
168 
172 static void sbar_wdata_free(struct MuttWindow *win, void **ptr)
173 {
174  struct SBarPrivateData *priv = *ptr;
175 
176  FREE(&priv->display);
177 
178  FREE(ptr);
179 }
180 
185 static struct SBarPrivateData *sbar_data_new(void)
186 {
187  struct SBarPrivateData *sbar_data = mutt_mem_calloc(1, sizeof(struct SBarPrivateData));
188 
189  return sbar_data;
190 }
191 
196 struct MuttWindow *sbar_new(void)
197 {
198  struct MuttWindow *win_sbar =
201 
202  win_sbar->wdata = sbar_data_new();
203  win_sbar->wdata_free = sbar_wdata_free;
204  win_sbar->recalc = sbar_recalc;
205  win_sbar->repaint = sbar_repaint;
206 
209 
210  return win_sbar;
211 }
212 
220 void sbar_set_title(struct MuttWindow *win, const char *title)
221 {
222  if (!win || !win->wdata || (win->type != WT_STATUS_BAR))
223  return;
224 
225  struct SBarPrivateData *priv = win->wdata;
226  mutt_str_replace(&priv->display, title);
227 
228  win->actions |= WA_REPAINT;
229 }
Color and attribute parsing.
@ MT_COLOR_MAX
Definition: color.h:88
@ MT_COLOR_STATUS
Status bar (takes a pattern)
Definition: color.h:70
@ MT_COLOR_NORMAL
Plain text.
Definition: color.h:53
Convenience wrapper for the core headers.
void mutt_paddstr(struct MuttWindow *win, int n, const char *s)
Display a string on screen, padded if necessary.
Definition: curs_lib.c:865
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
static int sbar_color_observer(struct NotifyCallback *nc)
Notification that a Color has changed - Implements observer_t -.
Definition: sbar.c:117
static int sbar_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition: sbar.c:144
static int sbar_recalc(struct MuttWindow *win)
Recalculate the Window data - Implements MuttWindow::recalc() -.
Definition: sbar.c:80
static int sbar_repaint(struct MuttWindow *win)
Repaint the Window - Implements MuttWindow::repaint() -.
Definition: sbar.c:93
static void sbar_wdata_free(struct MuttWindow *win, void **ptr)
Free the private data attached to the MuttWindow - Implements MuttWindow::wdata_free() -.
Definition: sbar.c:172
@ LL_DEBUG5
Log at debug level 5.
Definition: logging.h:44
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define FREE(x)
Definition: memory.h:40
Convenience wrapper for the library headers.
bool notify_observer_remove(struct Notify *notify, observer_t callback, void *global_data)
Remove an observer from an object.
Definition: notify.c:228
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
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:257
void mutt_curses_set_color_by_id(enum ColorId cid)
Set the current colour for text.
Definition: mutt_curses.c:52
bool mutt_window_is_visible(struct MuttWindow *win)
Is the Window visible?
Definition: mutt_window.c:500
int mutt_window_move(struct MuttWindow *win, int col, int row)
Move the cursor in a Window.
Definition: mutt_window.c:292
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
@ WT_STATUS_BAR
Status Bar containing extra info about the Index/Pager/etc.
Definition: mutt_window.h:102
@ 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.
Definition: mutt_window.h:207
@ NT_WINDOW_DELETE
Window is about to be deleted.
Definition: mutt_window.h:206
#define WA_REPAINT
Redraw the contents of the Window.
Definition: mutt_window.h:111
#define MUTT_WIN_SIZE_UNLIMITED
Use as much space as possible.
Definition: mutt_window.h:52
@ MUTT_WIN_SIZE_FIXED
Window has a fixed size.
Definition: mutt_window.h:47
@ NT_WINDOW
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:53
@ NT_COLOR
Colour has changed, NotifyColor, EventColor.
Definition: notify_type.h:39
struct MuttWindow * sbar_new(void)
Add the Simple Bar (status)
Definition: sbar.c:196
static struct SBarPrivateData * sbar_data_new(void)
Free the private data attached to the MuttWindow.
Definition: sbar.c:185
void sbar_set_title(struct MuttWindow *win, const char *title)
Set the title for the Simple Bar.
Definition: sbar.c:220
Key value store.
An Event that happened to a Colour.
Definition: notify2.h:53
enum ColorId cid
Colour ID that has changed.
Definition: notify2.h:54
An Event that happened to a Window.
Definition: mutt_window.h:216
struct MuttWindow * win
Window that changed.
Definition: mutt_window.h:217
int(* repaint)(struct MuttWindow *win)
Definition: mutt_window.h:182
struct WindowState state
Current state of the Window.
Definition: mutt_window.h:127
void * wdata
Private data.
Definition: mutt_window.h:145
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Definition: mutt_window.h:138
int(* recalc)(struct MuttWindow *win)
Definition: mutt_window.h:171
void(* wdata_free)(struct MuttWindow *win, void **ptr)
Definition: mutt_window.h:160
WindowActionFlags actions
Actions to be performed, e.g. WA_RECALC.
Definition: mutt_window.h:132
enum WindowType type
Window type, e.g. WT_SIDEBAR.
Definition: mutt_window.h:144
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
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
Private data for the Simple Bar.
Definition: sbar.c:73
char * display
Cached display string.
Definition: sbar.c:74
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:60