summaryrefslogtreecommitdiff
path: root/src/gui/gui-infobar.c
blob: 3e40246199542b5b63a253f35d10314e5b8305cd (plain)
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
/*
 * Copyright (c) 2003-2008 by FlashCode <flashcode@flashtux.org>
 * See README for License detail, AUTHORS for developers list.
 *
 * 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 3 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/>.
 */

/* gui-infobar.c: infobar functions, used by all GUI */


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#include "../core/weechat.h"
#include "../core/wee-hook.h"
#include "../core/wee-log.h"
#include "gui-infobar.h"
#include "gui-color.h"
#include "gui-window.h"


struct t_gui_infobar *gui_infobar = NULL;          /* infobar content       */
struct t_hook *gui_infobar_refresh_timer = NULL;   /* refresh timer         */
struct t_hook *gui_infobar_highlight_timer = NULL; /* highlight timer       */


/* 
 * gui_infobar_printf: display message in infobar
 */

void
gui_infobar_printf (int delay, int color, char *message, ...)
{
    static char buf[1024];
    va_list argptr;
    struct t_gui_infobar *ptr_infobar;
    char *buf2, *ptr_buf, *pos;
    
    if (!message)
        return;
    
    va_start (argptr, message);
    vsnprintf (buf, sizeof (buf) - 1, message, argptr);
    va_end (argptr);
    
    ptr_infobar = (struct t_gui_infobar *)malloc (sizeof (struct t_gui_infobar));
    if (ptr_infobar)
    {
        buf2 = (char *)gui_color_decode ((unsigned char *)buf);
        ptr_buf = (buf2) ? buf2 : buf;
        
        ptr_infobar->color = color;
        ptr_infobar->text = strdup (ptr_buf);
        pos = strchr (ptr_infobar->text, '\n');
        if (pos)
            pos[0] = '\0';
        ptr_infobar->remaining_time = (delay <= 0) ? -1 : delay;
        ptr_infobar->next_infobar = gui_infobar;
        gui_infobar = ptr_infobar;
        gui_infobar_draw (gui_current_window->buffer, 1);
        if (buf2)
            free (buf2);
        
        if (!gui_infobar_highlight_timer)
            gui_infobar_highlight_timer = hook_timer (NULL, 1 * 1000, 0, 0,
                                                      &gui_infobar_highlight_timer_cb,
                                                      NULL);
    }
    else
        log_printf (_("Error: not enough memory for infobar message"));
}

/*
 * gui_infobar_remove: remove last displayed message in infobar
 */

void
gui_infobar_remove ()
{
    struct t_gui_infobar *new_infobar;
    
    if (gui_infobar)
    {
        new_infobar = gui_infobar->next_infobar;
        if (gui_infobar->text)
            free (gui_infobar->text);
        free (gui_infobar);
        gui_infobar = new_infobar;
    }
}

/*
 * gui_infobar_remove_all: remove last displayed message in infobar
 */

void
gui_infobar_remove_all ()
{
    while (gui_infobar)
    {
        gui_infobar_remove ();
    }
}