22
22
#include "dialog.h"
23
23
24
24
static void back_lines (int n );
25
- static void print_page (WINDOW * win , int height , int width );
26
- static void print_line (WINDOW * win , int row , int width );
25
+ static void print_page (WINDOW * win , int height , int width , update_text_fn
26
+ update_text , void * data );
27
+ static void print_line (WINDOW * win , int row , int width );
27
28
static char * get_line (void );
28
29
static void print_position (WINDOW * win );
29
30
30
31
static int hscroll ;
31
32
static int begin_reached , end_reached , page_length ;
32
- static const char * buf ;
33
- static const char * page ;
33
+ static char * buf ;
34
+ static char * page ;
34
35
35
36
/*
36
37
* refresh window content
37
38
*/
38
39
static void refresh_text_box (WINDOW * dialog , WINDOW * box , int boxh , int boxw ,
39
- int cur_y , int cur_x )
40
+ int cur_y , int cur_x , update_text_fn update_text ,
41
+ void * data )
40
42
{
41
- print_page (box , boxh , boxw );
43
+ print_page (box , boxh , boxw , update_text , data );
42
44
print_position (dialog );
43
45
wmove (dialog , cur_y , cur_x ); /* Restore cursor position */
44
46
wrefresh (dialog );
@@ -49,9 +51,11 @@ static void refresh_text_box(WINDOW *dialog, WINDOW *box, int boxh, int boxw,
49
51
* Display text from a file in a dialog box.
50
52
*
51
53
* keys is a null-terminated array
54
+ * update_text() may not add or remove any '\n' or '\0' in tbuf
52
55
*/
53
- int dialog_textbox (const char * title , const char * tbuf , int initial_height ,
54
- int initial_width , int * keys , int * _vscroll , int * _hscroll )
56
+ int dialog_textbox (const char * title , char * tbuf , int initial_height ,
57
+ int initial_width , int * keys , int * _vscroll , int * _hscroll ,
58
+ update_text_fn update_text , void * data )
55
59
{
56
60
int i , x , y , cur_x , cur_y , key = 0 ;
57
61
int height , width , boxh , boxw ;
@@ -131,7 +135,8 @@ int dialog_textbox(const char *title, const char *tbuf, int initial_height,
131
135
132
136
/* Print first page of text */
133
137
attr_clear (box , boxh , boxw , dlg .dialog .atr );
134
- refresh_text_box (dialog , box , boxh , boxw , cur_y , cur_x );
138
+ refresh_text_box (dialog , box , boxh , boxw , cur_y , cur_x , update_text ,
139
+ data );
135
140
136
141
while (!done ) {
137
142
key = wgetch (dialog );
@@ -150,7 +155,8 @@ int dialog_textbox(const char *title, const char *tbuf, int initial_height,
150
155
begin_reached = 1 ;
151
156
page = buf ;
152
157
refresh_text_box (dialog , box , boxh , boxw ,
153
- cur_y , cur_x );
158
+ cur_y , cur_x , update_text ,
159
+ data );
154
160
}
155
161
break ;
156
162
case 'G' : /* Last page */
@@ -160,8 +166,8 @@ int dialog_textbox(const char *title, const char *tbuf, int initial_height,
160
166
/* point to last char in buf */
161
167
page = buf + strlen (buf );
162
168
back_lines (boxh );
163
- refresh_text_box (dialog , box , boxh , boxw ,
164
- cur_y , cur_x );
169
+ refresh_text_box (dialog , box , boxh , boxw , cur_y ,
170
+ cur_x , update_text , data );
165
171
break ;
166
172
case 'K' : /* Previous line */
167
173
case 'k' :
@@ -171,7 +177,7 @@ int dialog_textbox(const char *title, const char *tbuf, int initial_height,
171
177
172
178
back_lines (page_length + 1 );
173
179
refresh_text_box (dialog , box , boxh , boxw , cur_y ,
174
- cur_x );
180
+ cur_x , update_text , data );
175
181
break ;
176
182
case 'B' : /* Previous page */
177
183
case 'b' :
@@ -180,8 +186,8 @@ int dialog_textbox(const char *title, const char *tbuf, int initial_height,
180
186
if (begin_reached )
181
187
break ;
182
188
back_lines (page_length + boxh );
183
- refresh_text_box (dialog , box , boxh , boxw ,
184
- cur_y , cur_x );
189
+ refresh_text_box (dialog , box , boxh , boxw , cur_y ,
190
+ cur_x , update_text , data );
185
191
break ;
186
192
case 'J' : /* Next line */
187
193
case 'j' :
@@ -191,7 +197,7 @@ int dialog_textbox(const char *title, const char *tbuf, int initial_height,
191
197
192
198
back_lines (page_length - 1 );
193
199
refresh_text_box (dialog , box , boxh , boxw , cur_y ,
194
- cur_x );
200
+ cur_x , update_text , data );
195
201
break ;
196
202
case KEY_NPAGE : /* Next page */
197
203
case ' ' :
@@ -200,8 +206,8 @@ int dialog_textbox(const char *title, const char *tbuf, int initial_height,
200
206
break ;
201
207
202
208
begin_reached = 0 ;
203
- refresh_text_box (dialog , box , boxh , boxw ,
204
- cur_y , cur_x );
209
+ refresh_text_box (dialog , box , boxh , boxw , cur_y ,
210
+ cur_x , update_text , data );
205
211
break ;
206
212
case '0' : /* Beginning of line */
207
213
case 'H' : /* Scroll left */
@@ -216,8 +222,8 @@ int dialog_textbox(const char *title, const char *tbuf, int initial_height,
216
222
hscroll -- ;
217
223
/* Reprint current page to scroll horizontally */
218
224
back_lines (page_length );
219
- refresh_text_box (dialog , box , boxh , boxw ,
220
- cur_y , cur_x );
225
+ refresh_text_box (dialog , box , boxh , boxw , cur_y ,
226
+ cur_x , update_text , data );
221
227
break ;
222
228
case 'L' : /* Scroll right */
223
229
case 'l' :
@@ -227,8 +233,8 @@ int dialog_textbox(const char *title, const char *tbuf, int initial_height,
227
233
hscroll ++ ;
228
234
/* Reprint current page to scroll horizontally */
229
235
back_lines (page_length );
230
- refresh_text_box (dialog , box , boxh , boxw ,
231
- cur_y , cur_x );
236
+ refresh_text_box (dialog , box , boxh , boxw , cur_y ,
237
+ cur_x , update_text , data );
232
238
break ;
233
239
case KEY_ESC :
234
240
if (on_key_esc (dialog ) == KEY_ESC )
@@ -301,12 +307,23 @@ static void back_lines(int n)
301
307
}
302
308
303
309
/*
304
- * Print a new page of text. Called by dialog_textbox().
310
+ * Print a new page of text.
305
311
*/
306
- static void print_page (WINDOW * win , int height , int width )
312
+ static void print_page (WINDOW * win , int height , int width , update_text_fn
313
+ update_text , void * data )
307
314
{
308
315
int i , passed_end = 0 ;
309
316
317
+ if (update_text ) {
318
+ char * end ;
319
+
320
+ for (i = 0 ; i < height ; i ++ )
321
+ get_line ();
322
+ end = page ;
323
+ back_lines (height );
324
+ update_text (buf , page - buf , end - buf , data );
325
+ }
326
+
310
327
page_length = 0 ;
311
328
for (i = 0 ; i < height ; i ++ ) {
312
329
print_line (win , i , width );
@@ -319,7 +336,7 @@ static void print_page(WINDOW * win, int height, int width)
319
336
}
320
337
321
338
/*
322
- * Print a new line of text. Called by dialog_textbox() and print_page().
339
+ * Print a new line of text.
323
340
*/
324
341
static void print_line (WINDOW * win , int row , int width )
325
342
{
0 commit comments