File: mhtxtbuf.c

package info (click to toggle)
nethack 3.6.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,468 kB
  • sloc: ansic: 266,495; cpp: 13,652; yacc: 2,903; perl: 1,426; lex: 581; sh: 535; xml: 372; awk: 98; makefile: 68; fortran: 51; sed: 11
file content (269 lines) | stat: -rw-r--r-- 9,525 bytes parent folder | download | duplicates (6)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* NetHack 3.6	mhtxtbuf.c	$NHDT-Date: 1432512803 2015/05/25 00:13:23 $  $NHDT-Branch: master $:$NHDT-Revision: 1.10 $ */
/* Copyright (C) 2003 by Alex Kompel 	 */
/* NetHack may be freely redistributed.  See license for details. */

#include "mhtxtbuf.h"

/* Collect Nethack text messages and render text into edit box.
   Wrap text if necessary.
   Recognize formatted lines as having more that 4 consecutive.
   spaces inside the string.
   Strip leading and trailing spaces.
   Always break at the original line end (do not merge text that comes
   from NetHack engine)
*/

/*----------------------------------------------------------------*/
#define NHTEXT_BUFFER_INCREMENT 10
/*----------------------------------------------------------------*/
struct text_buffer_line {
    int attr;
    short beg_padding;
    short end_padding;
    BOOL formatted;
    char *text;
};
/*----------------------------------------------------------------*/
typedef struct mswin_nethack_text_buffer {
    BOOL b_wrap_text;
    int n_size;
    int n_used;
    struct text_buffer_line *text_buffer_line;
} NHTextBuffer, *PNHTextBuffer;
/*----------------------------------------------------------------*/
#define NHTextLine(pb, i) ((pb)->text_buffer_line[(i)])
static TCHAR *nh_append(TCHAR *s, int *size, const char *ap);
/*----------------------------------------------------------------*/
PNHTextBuffer
mswin_init_text_buffer(BOOL wrap_text)
{
    PNHTextBuffer pb = (PNHTextBuffer) malloc(sizeof(NHTextBuffer));
    if (!pb)
        panic("Out of memory");

    ZeroMemory(pb, sizeof(NHTextBuffer));
    pb->b_wrap_text = wrap_text;
    pb->n_size = 0;
    pb->n_used = 0;
    pb->text_buffer_line = NULL;
    return pb;
}
/*----------------------------------------------------------------*/
void
mswin_free_text_buffer(PNHTextBuffer pb)
{
    int i;

    if (!pb)
        return;

    for (i = 0; i < pb->n_used; i++) {
        free(pb->text_buffer_line[i].text);
    }
    free(pb->text_buffer_line);
    free(pb);
}
/*----------------------------------------------------------------*/
void
mswin_add_text(PNHTextBuffer pb, int attr, const char *text)
{
    char *p;
    struct text_buffer_line *new_line;

    /* grow buffer */
    if (pb->n_used >= pb->n_size) {
        pb->n_size += NHTEXT_BUFFER_INCREMENT;
        pb->text_buffer_line = (struct text_buffer_line *) realloc(
            pb->text_buffer_line,
            pb->n_size * sizeof(struct text_buffer_line));
        if (!pb->text_buffer_line)
            panic("Memory allocation error");
    }

    /* analyze the new line of text */
    new_line = &NHTextLine(pb, pb->n_used);
    new_line->attr = attr;
    new_line->beg_padding = 0;
    new_line->text = strdup(text);
    for (p = new_line->text; *p && isspace(*p); p++) {
        new_line->beg_padding++;
    }
    if (*p) {
        memmove(new_line->text, new_line->text + new_line->beg_padding,
                strlen(new_line->text) - new_line->beg_padding + 1);
        for (p = new_line->text + strlen(new_line->text);
             p >= new_line->text && isspace(*p); p--) {
            new_line->end_padding++;
            *p = 0;
        }

        /* if there are 3 (or more) consecutive spaces inside the string
           consider it formatted */
        new_line->formatted = (strstr(new_line->text, "   ") != NULL)
                              || (new_line->beg_padding > 8);
    } else {
        new_line->end_padding = 0;
        new_line->text[0] = 0;
        new_line->formatted = FALSE;
    }
    pb->n_used++;
}
/*----------------------------------------------------------------*/
void
mswin_set_text_wrap(PNHTextBuffer pb, BOOL wrap_text)
{
    pb->b_wrap_text = wrap_text;
}
/*----------------------------------------------------------------*/
BOOL
mswin_get_text_wrap(PNHTextBuffer pb)
{
    return pb->b_wrap_text;
}
/*----------------------------------------------------------------*/
static TCHAR *
nh_append(TCHAR *s, int *size, const char *ap)
{
    int tlen, tnewlen;

    if (!(ap && *ap))
        return s;

    /* append the calculated line to the text buffer */
    tlen = s ? _tcslen(s) : 0;
    tnewlen = tlen + strlen(ap);
    if (tnewlen >= *size) {
        *size = max(tnewlen, *size + BUFSZ);
        s = (TCHAR *) realloc(s, *size * sizeof(TCHAR));
        if (!s)
            panic("Out of memory");
        ZeroMemory(s + tlen, (*size - tlen) * sizeof(TCHAR));
    }
    if (strcmp(ap, "\r\n") == 0) {
        _tcscat(s, TEXT("\r\n"));
    } else {
        NH_A2W(ap, s + tlen, strlen(ap));
        s[tnewlen] = 0;
    }
    return s;
}
/*----------------------------------------------------------------*/
void
mswin_render_text(PNHTextBuffer pb, HWND edit_control)
{
    RECT rt_client;    /* boundaries of the client area of the edit control */
    SIZE size_text;    /* size of the edit control */
    RECT rt_text;      /* calculated text rectangle for the visible line */
    char buf[BUFSZ];   /* buffer for the visible line */
    TCHAR tbuf[BUFSZ]; /* temp buffer for DrawText */
    TCHAR *pText = NULL;    /* resulting text (formatted) */
    int pTextSize = 0;      /* resulting text size */
    char *p_cur = NULL;     /* current position in the
                               NHTextBuffer->text_buffer_line->text */
    char *p_buf_cur = NULL; /* current position in the visible line buffer */
    int i;
    HDC hdcEdit;           /* device context for the edit control */
    HFONT hFont, hOldFont; /* edit control font */

    GetClientRect(edit_control, &rt_client);
    size_text.cx = rt_client.right - rt_client.left;
    size_text.cy = rt_client.bottom - rt_client.top;
    size_text.cx -=
        GetSystemMetrics(SM_CXVSCROLL); /* add a slight right margin - the
                                           text looks better that way */
    hdcEdit = GetDC(edit_control);
    hFont = (HFONT) SendMessage(edit_control, WM_GETFONT, 0, 0);
    if (hFont)
        hOldFont = SelectObject(hdcEdit, hFont);

    /* loop through each line (outer loop) and wrap it around (inner loop) */
    ZeroMemory(buf, sizeof(buf));
    p_buf_cur = buf;
    for (i = 0; i < pb->n_used; i++) {
        if (pb->b_wrap_text) {
            p_cur = NHTextLine(pb, i).text;

            /* insert an line break for the empty string */
            if (!NHTextLine(pb, i).text[0]) {
                pText = nh_append(pText, &pTextSize, "\r\n");
                continue;
            }

            /* add margin to the "formatted" line of text */
            if (NHTextLine(pb, i).formatted) {
                strcpy(buf, "   ");
                p_buf_cur += 3;
            }

            /* scroll thourgh the current line of text and wrap it
               so it fits to width of the edit control */
            while (*p_cur) {
                char *p_word_pos = p_buf_cur;

                /* copy one word into the buffer */
                while (*p_cur && isspace(*p_cur))
                    if (p_buf_cur != buf)
                        *p_buf_cur++ = *p_cur++;
                    else
                        p_cur++;

                while (*p_cur && !isspace(*p_cur))
                    *p_buf_cur++ = *p_cur++;

                /* check if it fits */
                SetRect(&rt_text, 0, 0, size_text.cx, size_text.cy);
                DrawText(hdcEdit, NH_A2W(buf, tbuf, p_buf_cur - buf),
                         p_buf_cur - buf, &rt_text,
                         DT_CALCRECT | DT_LEFT | DT_SINGLELINE | DT_NOCLIP);
                if ((rt_text.right - rt_text.left) >= size_text.cx) {
                    /* Backtrack.
                       Only backtrack if the last word caused the overflow -
                       do not backtrack if the entire current line does not
                       fit the visible area.
                       Otherwise it is a infinite loop.
                    */
                    if (p_word_pos > buf) {
                        p_cur -= (p_buf_cur - p_word_pos);
                        p_buf_cur = p_word_pos;
                    }
                    *p_buf_cur = 0; /* break the line */

                    /* append the calculated line to the text buffer */
                    pText = nh_append(pText, &pTextSize, buf);
                    pText = nh_append(pText, &pTextSize, "\r\n");
                    ZeroMemory(buf, sizeof(buf));
                    p_buf_cur = buf;
                }
            }

            /* always break the line at the end of the buffer text */
            if (p_buf_cur != buf) {
                /* flush the current buffrer */
                *p_buf_cur = 0; /* break the line */
                pText = nh_append(pText, &pTextSize, buf);
                pText = nh_append(pText, &pTextSize, "\r\n");
                ZeroMemory(buf, sizeof(buf));
                p_buf_cur = buf;
            }
        } else { /* do not wrap text */
            int j;
            for (j = 0; j < NHTextLine(pb, i).beg_padding; j++)
                pText = nh_append(pText, &pTextSize, " ");
            pText = nh_append(pText, &pTextSize, NHTextLine(pb, i).text);
            pText = nh_append(pText, &pTextSize, "\r\n");
        }
    }

    /* cleanup */
    if (hFont)
        SelectObject(hdcEdit, hOldFont);
    ReleaseDC(edit_control, hdcEdit);

    /* update edit control text */
    if (pText) {
        SendMessage(edit_control, EM_FMTLINES, 1, 0);
        SetWindowText(edit_control, pText);
        free(pText);
    }
}
/*----------------------------------------------------------------*/