1 /* filekeys.c by Adam Rogoyski <apoc@laker.net> Temperanc on EFNet irc
2  * Copyright (C) 1998, 1999 Adam Rogoyski
3  * --- GNU General Public License Disclamer ---
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13 
14 
15 #include "hexedit.h"
16 
17 #define NAME_POS 57   /* Magic number of where filename always starts */
18 
19 extern struct FileNames *fp;
20 extern struct FileNames **pages;  /* node that starts a new page */
21 
22 
23 void
file_key_up(int * current_page)24 file_key_up (int *current_page)
25 {
26    char trunc_file[PATH_MAX + 1];
27 
28    if (cursor_y <= MAIN_TOP_LINE)
29    {
30       if (*current_page <= 0)
31       /*
32          do_beep ();
33       */
34          ;
35       else
36       {
37 	 cursor_y = BOTTOM_LINE;
38 	 memset (trunc_file, 0x00, PATH_MAX + 1);
39 	 fp = *(pages + --*current_page);
40 	 while (fp->p != *(pages + *current_page + 1))
41 	    fp = fp->p;
42 	 werase (Globals.wstatus);
43 	 werase (Globals.wmain);
44 	 printPage (*(pages + *current_page));
45 	 statWindow (fp->filename);
46 	 wmove (Globals.wmain, cursor_y - MAIN_TOP_LINE,
47 		NAME_POS);
48 	 strncpy (trunc_file, fp->filename, COLS - NAME_POS);
49 	 wattrset (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
50                                              : A_BOLD);
51 	 wprintw (Globals.wmain, "%s", trunc_file);
52 	 wattroff (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
53                                              : A_BOLD);
54 	 wrefresh (Globals.wstatus);
55 	 wrefresh (Globals.wmain);
56 	 move (cursor_y, NAME_POS);
57       }
58    }
59    else
60    {
61       struct FileNames *tp = *(pages + *current_page);
62       while (tp->p != fp)
63 	 tp = tp->p;
64       wmove (Globals.wmain, cursor_y - MAIN_TOP_LINE,
65 	     NAME_POS);
66       strncpy (trunc_file, fp->filename, COLS - NAME_POS);
67       wattrset (Globals.wmain, color_term ? COLOR_PAIR(1) : A_NORMAL);
68       wprintw (Globals.wmain, "%s", trunc_file);
69       cursor_y--;
70       fp = tp;
71       memset (trunc_file, 0x00, PATH_MAX + 1);
72       strncpy (trunc_file, fp->filename, COLS - NAME_POS);
73       wmove (Globals.wmain, cursor_y - MAIN_TOP_LINE,
74 	     NAME_POS);
75       wattrset (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
76                                           : A_BOLD);
77       wprintw (Globals.wmain, "%s", trunc_file);
78       wattroff (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
79                                           : A_BOLD);
80       statWindow (fp->filename);
81       wrefresh (Globals.wstatus);
82       wrefresh (Globals.wmain);
83       move (cursor_y, NAME_POS);
84    }
85    refresh ();
86 }
87 
88 
89 void
file_key_down(int * current_page,int n)90 file_key_down (int *current_page, int n)
91 {
92    char trunc_file[PATH_MAX + 1];
93 
94    if (cursor_y >= BOTTOM_LINE)
95    {
96       if ((*current_page < (n - 1)) && (n > 1))
97       {
98 	 werase (Globals.wstatus);
99 	 werase (Globals.wmain);
100 	 printPage (*(pages + ++*current_page));
101 	 fp = *(pages + *current_page);
102 	 wmove (Globals.wmain, 0, NAME_POS);
103 	 wattrset (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
104                                              : A_BOLD);
105 	 strncpy (trunc_file, fp->filename, COLS - NAME_POS);
106 	 wprintw (Globals.wmain, "%s", trunc_file);
107 	 wattroff (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
108                                              : A_BOLD);
109 	 statWindow (fp->filename);
110 	 cursor_y = MAIN_TOP_LINE;
111 	 move (cursor_y, NAME_POS);
112 	 wrefresh (Globals.wstatus);
113 	 wrefresh (Globals.wmain);
114       }
115       else
116       /*
117          do_beep ();
118       */
119          ;
120    }
121    else
122    {
123       if (!fp->p)
124       /*
125          do_beep ();
126       */
127          ;
128       else
129       {
130 	 wmove (Globals.wmain, cursor_y - MAIN_TOP_LINE,
131 		NAME_POS);
132 	 strncpy (trunc_file, fp->filename, COLS - NAME_POS);
133          wattrset (Globals.wmain, color_term ? COLOR_PAIR(1) : A_NORMAL);
134 	 wprintw (Globals.wmain, "%s", trunc_file);
135 	 cursor_y++;
136 	 fp = fp->p;
137 	 memset (trunc_file, 0x00, PATH_MAX + 1);
138 	 strncpy (trunc_file, fp->filename, COLS - NAME_POS);
139 	 wmove (Globals.wmain, cursor_y - MAIN_TOP_LINE,
140 		NAME_POS);
141 	 wattrset (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
142                                              : A_BOLD);
143 	 wprintw (Globals.wmain, "%s", trunc_file);
144 	 wattroff (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
145                                              : A_BOLD);
146 	 statWindow (fp->filename);
147 	 wrefresh (Globals.wstatus);
148 	 wrefresh (Globals.wmain);
149 	 move (cursor_y, NAME_POS);
150       }
151    }
152    refresh ();
153 }
154 
155 
156 void
file_redraw(int * current_page)157 file_redraw (int *current_page)
158 {
159    char trunc_file[PATH_MAX + 1];
160 
161    werase (Globals.wstatus);
162    wrefresh (Globals.wstatus);
163    werase (Globals.wmain);
164    wrefresh (Globals.wmain);
165    werase (Globals.whelp);
166    wrefresh (Globals.whelp);
167    printPage (*(pages + *current_page));
168    wmove (Globals.wmain, cursor_y - MAIN_TOP_LINE, NAME_POS);
169    wattrset (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
170                                        : A_BOLD);
171    strncpy (trunc_file, fp->filename, COLS - NAME_POS);
172    wprintw (Globals.wmain, "%s", trunc_file);
173    wattroff (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
174                                        : A_BOLD);
175    statWindow (fp->filename);
176    helpWindow ("^C ^X  Exit   ^M  Select File");
177    wrefresh (Globals.wstatus);
178    wrefresh (Globals.wmain);
179    wrefresh (Globals.whelp);
180    wmove (Globals.wmain, 0, NAME_POS);
181    move (cursor_y, NAME_POS);
182    refresh ();
183 }
184 
185 
186 void
file_pagedown(int * current_page,int n)187 file_pagedown (int *current_page, int n)
188 {
189    char trunc_file[PATH_MAX + 1];
190 
191    if ((*current_page < (n - 1)) && (n > 1))
192    {
193       int i = MAIN_TOP_LINE;
194       fp = *(pages + ++*current_page);
195       for (i = MAIN_TOP_LINE; (i < cursor_y) && fp->p; i++)
196       {
197          fp = fp->p;
198       }
199       cursor_y = i;  /* bottom may not exist */
200       werase (Globals.wmain);
201       printPage (*(pages + *current_page));
202       wmove (Globals.wmain, cursor_y - MAIN_TOP_LINE, NAME_POS);
203       wattrset (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
204                                           : A_BOLD);
205       strncpy (trunc_file, fp->filename, COLS - NAME_POS);
206       wprintw (Globals.wmain, "%s", trunc_file);
207       wattroff (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
208                                           : A_BOLD);
209    }
210    else
211    {
212       memset (trunc_file, 0x00, PATH_MAX + 1);
213       strncpy (trunc_file, fp->filename, COLS - NAME_POS);
214       wmove (Globals.wmain, cursor_y - MAIN_TOP_LINE, NAME_POS);
215       wattrset (Globals.wmain, color_term ? COLOR_PAIR(1) : A_NORMAL);
216       wprintw (Globals.wmain, "%s", trunc_file);
217 
218       cursor_y = MAIN_TOP_LINE;
219       fp = *(pages + *current_page);
220       while (fp->p)
221       {
222          cursor_y++;
223          fp = fp->p;
224       }
225       memset (trunc_file, 0x00, PATH_MAX + 1);
226       strncpy (trunc_file, fp->filename, COLS - NAME_POS);
227       wmove (Globals.wmain, cursor_y - MAIN_TOP_LINE, NAME_POS);
228       wattrset (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
229                                           : A_BOLD);
230       wprintw (Globals.wmain, "%s", trunc_file);
231       wattroff (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
232                                           : A_BOLD);
233    }
234    statWindow (fp->filename);
235    move (cursor_y, NAME_POS);
236    wrefresh (Globals.wstatus);
237    wrefresh (Globals.wmain);
238    refresh ();
239 }
240 
241 
242 void
file_pageup(int * current_page)243 file_pageup (int *current_page)
244 {
245    char trunc_file[PATH_MAX + 1];
246 
247    if (*current_page > 0)
248    {
249       int i = MAIN_TOP_LINE;
250       fp = *(pages + --*current_page);
251       for (i = MAIN_TOP_LINE; i < cursor_y; i++)
252       {
253          fp = fp->p;
254       }
255       werase (Globals.wmain);
256       printPage (*(pages + *current_page));
257       statWindow (fp->filename);
258       strncpy (trunc_file, fp->filename, COLS - NAME_POS);
259       wmove (Globals.wmain, cursor_y - MAIN_TOP_LINE, NAME_POS);
260       wattrset (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
261                                           : A_BOLD);
262       wprintw (Globals.wmain, "%s", trunc_file);
263       wattroff (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
264                                           : A_BOLD);
265    }
266    else
267    {
268       memset (trunc_file, 0x00, PATH_MAX + 1);
269       strncpy (trunc_file, fp->filename, COLS - NAME_POS);
270       wmove (Globals.wmain, cursor_y - MAIN_TOP_LINE, NAME_POS);
271       wattrset (Globals.wmain, color_term ? COLOR_PAIR(1) : A_NORMAL);
272       wprintw (Globals.wmain, "%s", trunc_file);
273 
274       cursor_y = MAIN_TOP_LINE;
275       fp = *pages;
276       memset (trunc_file, 0x00, PATH_MAX + 1);
277       strncpy (trunc_file, fp->filename, COLS - NAME_POS);
278       wmove (Globals.wmain, cursor_y - MAIN_TOP_LINE, NAME_POS);
279       wattrset (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
280                                           : A_BOLD);
281       wprintw (Globals.wmain, "%s", trunc_file);
282       wattroff (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
283                                           : A_BOLD);
284       statWindow (fp->filename);
285    }
286    move (cursor_y, NAME_POS);
287    wrefresh (Globals.wstatus);
288    wrefresh (Globals.wmain);
289    refresh ();
290 }
291 
292 
293 void
file_home(int * current_page)294 file_home (int *current_page)
295 {
296    char trunc_file[PATH_MAX + 1];
297 
298    if (*current_page)
299    {
300       *current_page = 0;
301       cursor_y = MAIN_TOP_LINE;
302       werase (Globals.wmain);
303       werase (Globals.wstatus);
304       fp = *pages;
305       statWindow (fp->filename);
306       printPage (*pages);
307       strncpy (trunc_file, fp->filename, COLS - NAME_POS);
308       wmove (Globals.wmain, cursor_y - MAIN_TOP_LINE,
309 	     NAME_POS);
310       wattrset (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
311                                           : A_BOLD);
312       wprintw (Globals.wmain, "%s", trunc_file);
313       wattroff (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
314                                           : A_BOLD);
315       move (cursor_y, NAME_POS);
316    }
317    else
318       /* already on first page */
319    {
320       strncpy (trunc_file, fp->filename, COLS - NAME_POS);
321       wmove (Globals.wmain, cursor_y - MAIN_TOP_LINE,
322 	     NAME_POS);
323       wattrset (Globals.wmain, color_term ? COLOR_PAIR(1) : A_NORMAL);
324       wprintw (Globals.wmain, "%s", trunc_file);
325       fp = *pages;
326       strncpy (trunc_file, fp->filename, COLS - NAME_POS);
327       cursor_y = MAIN_TOP_LINE;
328       wmove (Globals.wmain, cursor_y - MAIN_TOP_LINE,
329 	     NAME_POS);
330       wattrset (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
331                                           : A_BOLD);
332       wprintw (Globals.wmain, "%s", trunc_file);
333       wattroff (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
334                                           : A_BOLD);
335       werase (Globals.wstatus);
336       statWindow (fp->filename);
337       move (cursor_y, NAME_POS);
338    }
339    wrefresh (Globals.wstatus);
340    wrefresh (Globals.wmain);
341    refresh ();
342 }
343 
344 
345 void
file_end(int * current_page,int n)346 file_end (int *current_page, int n)
347 {
348    char trunc_file[PATH_MAX + 1];
349 
350    if (*current_page < (n - 1))
351    {
352       cursor_y = MAIN_TOP_LINE;
353       werase (Globals.wmain);
354       *current_page = n - 1;
355       fp = *(pages + *current_page);
356       while (fp->p)
357       {
358          cursor_y++;
359          fp = fp->p;
360       }
361       printPage (*(pages + *current_page));
362       memset (trunc_file, 0x00, PATH_MAX + 1);
363       strncpy (trunc_file, fp->filename, COLS - NAME_POS);
364       wmove (Globals.wmain, cursor_y - MAIN_TOP_LINE,
365 	     NAME_POS);
366       wattrset (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
367                                           : A_BOLD);
368       wprintw (Globals.wmain, "%s", trunc_file);
369       wattroff (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
370                                           : A_BOLD);
371    }
372    else
373    {
374       memset (trunc_file, 0x00, PATH_MAX + 1);
375       strncpy (trunc_file, fp->filename, COLS - NAME_POS);
376       wmove (Globals.wmain, cursor_y - MAIN_TOP_LINE,
377 	     NAME_POS);
378       wattrset (Globals.wmain, color_term ? COLOR_PAIR(1) : A_NORMAL);
379       wprintw (Globals.wmain, "%s", trunc_file);
380       fp = *(pages + *current_page);
381       cursor_y = MAIN_TOP_LINE;
382       while (fp->p)
383       {
384          cursor_y++;
385          fp = fp->p;
386       }
387       wattrset (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
388                                           : A_BOLD);
389       memset (trunc_file, 0x00, PATH_MAX + 1);
390       strncpy (trunc_file, fp->filename, COLS - NAME_POS);
391       wmove (Globals.wmain, cursor_y - MAIN_TOP_LINE,
392              NAME_POS);
393       wprintw (Globals.wmain, "%s", trunc_file);
394       wattroff (Globals.wmain, color_term ? COLOR_PAIR(2) | A_BOLD
395                                           : A_BOLD);
396    }
397    statWindow (fp->filename);
398    move (cursor_y, NAME_POS);
399    wrefresh (Globals.wmain);
400    wrefresh (Globals.wstatus);
401    refresh ();
402 }
403