NeoMutt  2022-04-29-145-g9b6a0e
Teaching an old dog new tricks
DOXYGEN
msgwin.c
Go to the documentation of this file.
1 
80 #include "config.h"
81 #include <stddef.h>
82 #include "mutt/lib.h"
83 #include "color/lib.h"
84 #include "mutt_curses.h"
85 #include "mutt_window.h"
86 
88 static struct MuttWindow *MessageWindow = NULL;
89 
94 {
95  enum ColorId cid;
96  char *text;
97 };
98 
102 static int msgwin_recalc(struct MuttWindow *win)
103 {
104  if (window_is_focused(win)) // Someone else is using it
105  return 0;
106 
107  win->actions |= WA_REPAINT;
108  mutt_debug(LL_DEBUG5, "recalc done, request WA_REPAINT\n");
109  return 0;
110 }
111 
115 static int msgwin_repaint(struct MuttWindow *win)
116 {
117  if (window_is_focused(win)) // someone else is using it
118  return 0;
119 
120  struct MsgWinPrivateData *priv = win->wdata;
121 
122  mutt_window_move(win, 0, 0);
123 
125  mutt_window_move(win, 0, 0);
126  mutt_window_addstr(win, priv->text);
129 
130  mutt_debug(LL_DEBUG5, "repaint done\n");
131  return 0;
132 }
133 
143 {
144  if ((nc->event_type != NT_WINDOW) || !nc->global_data || !nc->event_data)
145  return -1;
146 
147  struct MuttWindow *win_msg = nc->global_data;
148  struct EventWindow *ev_w = nc->event_data;
149  if (ev_w->win != win_msg)
150  return 0;
151 
152  if (nc->event_subtype == NT_WINDOW_STATE)
153  {
154  win_msg->actions |= WA_RECALC;
155  mutt_debug(LL_NOTIFY, "window state done, request WA_RECALC\n");
156  }
157  else if (nc->event_subtype == NT_WINDOW_DELETE)
158  {
160  MessageWindow = NULL;
161  mutt_debug(LL_DEBUG5, "window delete done\n");
162  }
163  return 0;
164 }
165 
169 static void msgwin_wdata_free(struct MuttWindow *win, void **ptr)
170 {
171  struct MsgWinPrivateData *priv = *ptr;
172 
173  FREE(&priv->text);
174 
175  FREE(ptr);
176 }
177 
183 {
184  struct MsgWinPrivateData *msgwin_data = mutt_mem_calloc(1, sizeof(struct MsgWinPrivateData));
185 
186  msgwin_data->cid = MT_COLOR_NORMAL;
187 
188  return msgwin_data;
189 }
190 
195 struct MuttWindow *msgwin_new(void)
196 {
203 
205 
206  return MessageWindow;
207 }
208 
214 const char *msgwin_get_text(void)
215 {
216  if (!MessageWindow)
217  return NULL;
218 
219  struct MsgWinPrivateData *priv = MessageWindow->wdata;
220 
221  return priv->text;
222 }
223 
231 void msgwin_set_text(enum ColorId cid, const char *text)
232 {
233  if (!MessageWindow)
234  return;
235 
236  struct MsgWinPrivateData *priv = MessageWindow->wdata;
237 
238  priv->cid = cid;
239  mutt_str_replace(&priv->text, text);
240 
242 }
243 
248 {
250 }
251 
259 {
260  return MessageWindow;
261 }
262 
267 size_t msgwin_get_width(void)
268 {
269  if (!MessageWindow)
270  return 0;
271 
272  return MessageWindow->state.cols;
273 }
274 
281 void msgwin_set_height(short height)
282 {
283  if (!MessageWindow)
284  return;
285 
286  if (height < 1)
287  height = 1;
288  else if (height > 3)
289  height = 3;
290 
291  struct MuttWindow *win_cont = MessageWindow->parent;
292 
293  win_cont->req_rows = height;
294  mutt_window_reflow(win_cont->parent);
295 }
Color and attribute parsing.
ColorId
List of all colored objects.
Definition: color.h:35
@ MT_COLOR_NORMAL
Plain text.
Definition: color.h:53
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
static int msgwin_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition: msgwin.c:142
static int msgwin_recalc(struct MuttWindow *win)
Recalculate the display of the Message Window - Implements MuttWindow::recalc() -.
Definition: msgwin.c:102
static int msgwin_repaint(struct MuttWindow *win)
Redraw the Message Window - Implements MuttWindow::repaint() -.
Definition: msgwin.c:115
static void msgwin_wdata_free(struct MuttWindow *win, void **ptr)
Free the private data attached to the Message Window - Implements MuttWindow::wdata_free() -.
Definition: msgwin.c:169
@ LL_DEBUG5
Log at debug level 5.
Definition: logging.h:44
@ LL_NOTIFY
Log of notifications.
Definition: logging.h:45
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:43
void msgwin_set_text(enum ColorId cid, const char *text)
Set the text for the Message Window.
Definition: msgwin.c:231
const char * msgwin_get_text(void)
Get the text from the Message Window.
Definition: msgwin.c:214
void msgwin_set_height(short height)
Resize the Message Window.
Definition: msgwin.c:281
static struct MsgWinPrivateData * msgwin_wdata_new(void)
Create new private data for the Message Window.
Definition: msgwin.c:182
static struct MuttWindow * MessageWindow
Message Window for messages, warnings, errors etc.
Definition: msgwin.c:88
struct MuttWindow * msgwin_new(void)
Create the Message Window.
Definition: msgwin.c:195
size_t msgwin_get_width(void)
Get the width of the Message Window.
Definition: msgwin.c:267
struct MuttWindow * msgwin_get_window(void)
Get the Message Window pointer.
Definition: msgwin.c:258
void msgwin_clear_text(void)
Clear the text in the Message Window.
Definition: msgwin.c:247
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:326
struct AttrColor * mutt_curses_set_normal_backed_color_by_id(enum ColorId cid)
Set the colour and attributes by the colour id.
Definition: mutt_curses.c:65
struct AttrColor * mutt_curses_set_color_by_id(enum ColorId cid)
Set the colour and attributes by the colour id.
Definition: mutt_curses.c:81
Define wrapper functions around Curses.
void mutt_window_reflow(struct MuttWindow *win)
Resize a Window and its children.
Definition: mutt_window.c:340
int mutt_window_move(struct MuttWindow *win, int col, int row)
Move the cursor in a Window.
Definition: mutt_window.c:293
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:181
bool window_is_focused(struct MuttWindow *win)
Does the given Window have the focus?
Definition: mutt_window.c:629
int mutt_window_addstr(struct MuttWindow *win, const char *str)
Write a string to a Window.
Definition: mutt_window.c:409
void mutt_window_clrtoeol(struct MuttWindow *win)
Clear to the end of the line.
Definition: mutt_window.c:242
Window management.
#define WA_RECALC
Recalculate the contents of the Window.
Definition: mutt_window.h:110
@ WT_MESSAGE
Window for messages/errors and command entry.
Definition: mutt_window.h:99
@ 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:55
An Event that happened to a Window.
Definition: mutt_window.h:216
struct MuttWindow * win
Window that changed.
Definition: mutt_window.h:217
Private data for the Message Window.
Definition: msgwin.c:94
char * text
Cached display string.
Definition: msgwin.c:96
enum ColorId cid
Colour for the text, e.g. MT_COLOR_MESSAGE.
Definition: msgwin.c:95
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
short req_rows
Number of rows required.
Definition: mutt_window.h:125
int(* recalc)(struct MuttWindow *win)
Definition: mutt_window.h:171
void(* wdata_free)(struct MuttWindow *win, void **ptr)
Definition: mutt_window.h:160
struct MuttWindow * parent
Parent Window.
Definition: mutt_window.h:135
WindowActionFlags actions
Actions to be performed, e.g. WA_RECALC.
Definition: mutt_window.h:132
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
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:60