File: gtmessag.c

package info (click to toggle)
glktermw 1.0.4%2Bgit20200122-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 844 kB
  • sloc: ansic: 22,241; makefile: 27
file content (91 lines) | stat: -rw-r--r-- 1,996 bytes parent folder | download | duplicates (2)
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
/* gtmessag.c: The message line at the bottom of the screen
        for GlkTerm, curses.h implementation of the Glk API.
    Designed by Andrew Plotkin <erkyrath@eblong.com>
    http://www.eblong.com/zarf/glk/index.html
*/

#include "gtoption.h"
#include <wchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curses.h>
#include "glk.h"
#include "glkterm.h"

/* Nothing fancy here. We store a string, and print it on the bottom line.
    If pref_messageline is FALSE, none of these functions do anything. */

static wchar_t *msgbuf = NULL;
static int msgbuflen = 0;
static int msgbuf_size = 0;

void gli_msgline_warning(wchar_t *msg)
{
    wchar_t buf[256];
    int l;
    
    if (!pref_messageline)
        return;
    
    buf[0] = L'\0';
    wcsncat(buf, L"Glk library error: ", 256);
    l = wcslen(buf);
    wcsncat(buf + l, msg, 256 - l);
    gli_msgline(buf);
}

void gli_msgline(wchar_t *msg)
{
    int len;
    
    if (!pref_messageline)
        return;
        
    if (!msg) 
        msg = L"";
    
    len = wcslen(msg);
    if (!msgbuf) {
        msgbuf_size = len+80;
        msgbuf = (wchar_t *)malloc(msgbuf_size * sizeof(wchar_t));
    }
    else if (len+1 > msgbuf_size) {
        while (len+1 > msgbuf_size)
            msgbuf_size *= 2;
        msgbuf = (wchar_t *)realloc(msgbuf, msgbuf_size * sizeof(wchar_t));
    }

    if (!msgbuf)
        return;
    
    wcscpy(msgbuf, msg);
    msgbuflen = len;
    
    gli_msgline_redraw();
}

void gli_msgline_redraw()
{
    if (!pref_messageline)
        return;
        
    if (msgbuflen == 0) {
        move(content_box.bottom, 0);
        clrtoeol();
    }
    else {
        int len;
        
        move(content_box.bottom, 0);
        local_addwstr(L"  ");
        attron(A_REVERSE);
        if (msgbuflen > content_box.right-3)
            len = content_box.right-3;
        else
            len = msgbuflen;
        local_addnwstr(msgbuf, len);
        attrset(0);
        clrtoeol();
    }
}