forked from smcameron/space-nerds-in-space
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snis_text_window.c
175 lines (157 loc) · 4.65 KB
/
snis_text_window.c
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
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include "snis_font.h"
#include "snis_typeface.h"
#include "snis_graph.h"
#include "snis_text_window.h"
#include "wwviaudio.h"
struct text_window {
int x, y, w, h;
int font;
int total_lines;
int first_entry, last_entry;
int top_line;
int visible_lines;
int lineheight;
int thumb_pos;
int print_slowly;
int printing_pos;
char **text;
int color;
};
static int default_timer = 0;
static volatile int *textwindow_timer = &default_timer;
static int tty_chatter_sound = -1;
int text_window_entry_count(struct text_window *tw)
{
int rc;
rc = tw->last_entry - tw->first_entry + 1;
if (rc <= 0)
rc += tw->total_lines;
return rc;
}
void text_window_add_text(struct text_window *tw, char *text)
{
strncpy(tw->text[tw->last_entry], text, 79);
tw->text[tw->last_entry][79] = '\0';
tw->last_entry = (tw->last_entry + 1) % tw->total_lines;
if (tw->last_entry == tw->first_entry)
tw->first_entry = (tw->first_entry + 1) % tw->total_lines;
if (tw->visible_lines > text_window_entry_count(tw))
tw->top_line = tw->last_entry - text_window_entry_count(tw);
else
tw->top_line = tw->last_entry - tw->visible_lines;
if (tw->top_line < 0)
tw->top_line += tw->total_lines;
#if 0
printf("top = %d, f=%d l=%d, c=%d\n", tw->top_line, tw->first_entry, tw->last_entry,
text_window_entry_count(tw));
#endif
tw->printing_pos = 0;
}
struct text_window *text_window_init(int x, int y, int w,
int total_lines, int visible_lines, int color)
{
int i;
struct text_window *tw;
tw = malloc(sizeof(*tw));
tw->x = x;
tw->y = y;
tw->w = w;
tw->total_lines = total_lines;
tw->visible_lines = visible_lines;
tw->color = color;
tw->text = malloc(sizeof(*tw) * total_lines);
for (i = 0; i < total_lines; i++) {
tw->text[i] = malloc(80);
memset(tw->text[i], 0, 80);
}
tw->lineheight = 20;
tw->first_entry = 0;
tw->last_entry = 0;
tw->top_line = 0;
tw->h = tw->lineheight * tw->visible_lines + 10;
tw->font = TINY_FONT;
tw->print_slowly = 1;
tw->printing_pos = 0;
return tw;
}
void text_window_draw(GtkWidget *w, GdkGC *gc, struct text_window *tw)
{
int i, j;
int thumb_pos, thumb_top, thumb_bottom, twec;
sng_set_foreground(tw->color);
/* draw outer rectangle */
sng_current_draw_rectangle(w->window, gc, 0, tw->x, tw->y, tw->w, tw->h);
/* draw scroll bar */
sng_current_draw_rectangle(w->window, gc, 0, tw->x + tw->w - 15, tw->y + 5, 10, tw->h - 10);
twec = text_window_entry_count(tw);
if (twec == 0) {
thumb_pos = 0;
thumb_top = tw->y + 5;
thumb_bottom = tw->y + tw->h - 10;
} else {
float f;
/* figure which line the thumb pos is on. */
thumb_pos = (tw->top_line + (tw->visible_lines / 2) - tw->first_entry) % tw->total_lines;
if (thumb_pos < 0)
thumb_pos += tw->total_lines;
/* figure where that is on the screen */
f = (float) thumb_pos / (float) text_window_entry_count(tw);
thumb_pos = (int) (f * tw->h) + tw->y;
thumb_top = (int) (((float) (tw->visible_lines / 2.0) / (float) twec) *
-tw->lineheight * tw->visible_lines + thumb_pos);
if (thumb_top < tw->y + 5)
thumb_top = tw->y + 5;
thumb_bottom = (int) (((float) (tw->visible_lines / 2.0) / (float) twec) *
tw->lineheight * tw->visible_lines + thumb_pos);
if (thumb_bottom > tw->y + tw->h - 10)
thumb_bottom = tw->y + tw->h - 10;
}
sng_current_draw_rectangle(w->window, gc, 0,
tw->x + tw->w - 13, thumb_pos - tw->lineheight / 2,
6, tw->lineheight);
sng_current_draw_rectangle(w->window, gc, 0,
tw->x + tw->w - 11, thumb_top,
2, thumb_bottom - thumb_top);
j = 0;
for (i = tw->top_line;
j < tw->visible_lines && j < text_window_entry_count(tw);
i = (i + 1) % tw->total_lines) {
int len = strlen(tw->text[i]);
if (!tw->print_slowly || i != tw->last_entry -1) {
sng_abs_xy_draw_string(w, gc, tw->text[i], tw->font, tw->x + 10,
tw->y + j * tw->lineheight + tw->lineheight);
} else {
char tmpbuf[100];
strncpy(tmpbuf, tw->text[i], 99);
tmpbuf[99] = '\0';
if (tw->printing_pos < len - 1) {
if (((*textwindow_timer >> 2) & 1) == 0) {
tmpbuf[tw->printing_pos] = '_';
tmpbuf[tw->printing_pos + 1] = '\0';
} else {
tmpbuf[tw->printing_pos] = '\0';
}
tw->printing_pos++;
if (tty_chatter_sound != -1)
wwviaudio_add_sound(tty_chatter_sound);
} else {
if (((*textwindow_timer >> 2) & 0x01) == 0)
strcat(tmpbuf, "_");
}
sng_abs_xy_draw_string(w, gc, tmpbuf, tw->font, tw->x + 10,
tw->y + j * tw->lineheight + tw->lineheight);
}
j++;
}
}
void text_window_set_timer(volatile int *timer)
{
textwindow_timer = timer;
}
void text_window_set_chatter_sound(int chatter_sound)
{
tty_chatter_sound = chatter_sound;
}