Initial import from FreeBSD RELENG_4:
[games.git] / usr.sbin / pcvt / fed / edit.c
1 /*
2  * Copyright (c) 1992, 1993, 1994 by Hellmuth Michaelis
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *    This product includes software developed by Hellmuth Michaelis.
17  * 4. The name of the developer may not be used to endorse or promote
18  *    products derived from this software without specific prior written
19  *    permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  *      edit.c, 3.00, last edit-date: [Sun Jan  2 20:08:27 1994]
33  */
34
35 /*---------------------------------------------------------------------------
36  *
37  *      edit.c          font editor edit character
38  *      ------------------------------------------
39  *
40  *      written by Hellmuth Michaelis, hm@hcshh.hcs.de
41  *
42  *      -hm     first public release
43  *      -hm     some debugging & cleanup
44  *
45  *---------------------------------------------------------------------------*/
46
47 #include "fed.h"
48
49 #define UP      0
50 #define DOWN    1
51
52 static int pen;
53
54 /*---------------------------------------------------------------------------*
55  *      fill edit mode command window
56  *---------------------------------------------------------------------------*/
57 void edit_mode(void)
58 {
59         mvwprintw(cmd_win,1,1,"(W)hite     ");
60         mvwprintw(cmd_win,2,1,"(Black      ");
61         mvwprintw(cmd_win,3,1,"(I)nvert    ");
62         mvwprintw(cmd_win,4,1,"(R)ow BLACK ");
63         mvwprintw(cmd_win,5,1,"(r)ow WHITE ");
64         mvwprintw(cmd_win,6,1,"(C)ol BLACK ");
65         mvwprintw(cmd_win,7,1,"(c)ol WHITE ");
66         mvwprintw(cmd_win,8,1,"(Q)uit/Save ");
67
68         mvwprintw(cmd_win,9 ,1,"e(X)it/undo ");
69         mvwprintw(cmd_win,10,1,"Pen (U)p    ");
70         mvwprintw(cmd_win,11,1,"Pen (D)own  ");
71         mvwprintw(cmd_win,12,1,"            ");
72         mvwprintw(cmd_win,13,1,"(^P)rev Line");
73         mvwprintw(cmd_win,14,1,"(^N)ext Line");
74         mvwprintw(cmd_win,15,1,"(^F)orwd Col");
75         mvwprintw(cmd_win,16,1,"(^B)ack  Col");
76         wrefresh(cmd_win);
77 }
78
79 /*---------------------------------------------------------------------------*
80  *      edit mode command loop
81  *---------------------------------------------------------------------------*/
82 int edit(void)
83 {
84         int c, r;
85         char l;
86         unsigned int k_ch;
87
88         c = r = 0;
89
90         pen = UP;
91
92         for(;;)
93         {
94                 if(pen == DOWN)
95                         dis_cmd("   Edit Mode, the Pen is DOWN");
96                 else
97                         dis_cmd("   Edit Mode, the Pen is UP");
98
99                 l = ((mvwinch(ch_win,(r+1),(c+1))) & A_CHARTEXT);
100                 wattron(ch_win,A_REVERSE);
101                 mvwprintw(ch_win,(r+1),(c+1),"%c",l);
102                 wattroff(ch_win,A_REVERSE);
103                 wmove(ch_win,(r+1),(c+1));
104                 wrefresh(ch_win);
105
106                 k_ch = wgetch(ch_win);
107
108                 switch(k_ch)
109                 {
110                         case K_LEFT:
111                         case KEY_LEFT:
112                                 if(c > 0)
113                                 {
114                                         normal_ch(r,c);
115                                         c--;
116                                 }
117                                 break;
118
119                         case K_DOWN:
120                         case KEY_DOWN:
121                                 if(r < (ch_height-1))
122                                 {
123                                         normal_ch(r,c);
124                                         r++;
125                                 }
126                                 break;
127
128                         case K_UP:
129                         case KEY_UP:
130                                 if(r > 0)
131                                 {
132                                         normal_ch(r,c);
133                                         r--;
134                                 }
135                                 break;
136
137                         case K_RIGHT:
138                         case KEY_RIGHT:
139                                 if(c < (ch_width-1))
140                                 {
141                                         normal_ch(r,c);
142                                         c++;
143                                 }
144                                 break;
145
146                         case KEY_HOME:
147                                 normal_ch(r,c);
148                                 c = r = 0;
149                                 break;
150
151                         case KEY_LL:
152                                 normal_ch(r,c);
153                                 c = ch_width-1;
154                                 r = ch_height-1;
155                                 break;
156
157                         case 0x0c:
158                                 wrefresh(curscr);
159                                 break;
160
161                         case '\n':
162                         case '\r':
163                         case ' ' :
164                                 chg_pt(r,c);
165                                 break;
166
167                         case 'q':
168                                 pen = UP;
169                                 normal_ch(r,c);
170                                 wrefresh(ch_win);
171                                 return(1);
172                                 break;
173
174                         case 'x':
175                                 pen = UP;
176                                 normal_ch(r,c);
177                                 wrefresh(ch_win);
178                                 return(0);
179                                 break;
180
181                         case 'w':
182                         case 'W':
183                                 setchr(WHITE);
184                                 break;
185
186                         case 'b':
187                         case 'B':
188                                 setchr(BLACK);
189                                 break;
190
191                         case 'i':
192                         case 'I':
193                                 invert();
194                                 break;
195
196                         case 'r':
197                                 setrow(WHITE);
198                                 break;
199
200                         case 'R':
201                                 setrow(BLACK);
202                                 break;
203
204                         case 'c':
205                                 setcol(WHITE);
206                                 break;
207
208                         case 'C':
209                                 setcol(BLACK);
210                                 break;
211
212                         case 'u':
213                         case 'U':
214                                 pen = UP;
215                                 break;
216
217                         case 'd':
218                         case 'D':
219                                 pen = DOWN;
220                                 break;
221
222                         default:
223                                 beep();
224                                 break;
225
226                 }
227         }
228 }
229
230 /*---------------------------------------------------------------------------*
231  *
232  *---------------------------------------------------------------------------*/
233 void normal_ch(int r, int c)
234 {
235         char l = ((mvwinch(ch_win,(r+1),(c+1))) & A_CHARTEXT);
236         wattroff(ch_win,A_REVERSE);
237         if(pen == DOWN)
238                 mvwprintw(ch_win,(r+1),(c+1),"*");
239         else
240                 mvwprintw(ch_win,(r+1),(c+1),"%c",l);
241         wmove(ch_win,(r+1),(c+1));
242 }
243
244 /*---------------------------------------------------------------------------*
245  *
246  *---------------------------------------------------------------------------*/
247 void chg_pt(int r, int c)
248 {
249         char l;
250         l = ((mvwinch(ch_win,(r+1),(c+1))) & A_CHARTEXT);
251         if(l == WHITE)
252                 l = BLACK;
253         else
254                 l = WHITE;
255         mvwprintw(ch_win,(r+1),(c+1),"%c",l);
256         wmove(ch_win,(r+1),(c+1));
257 }
258
259 /*---------------------------------------------------------------------------*
260  *      invert current character
261  *---------------------------------------------------------------------------*/
262 void invert(void)
263 {
264         int r,c;
265
266         r = 1;
267
268         while(r <= ch_height)
269         {
270                 c = 1;
271                 while(c <= ch_width)
272                 {
273                         if(WHITE == mvwinch(ch_win, r, c))
274                                 mvwaddch(ch_win, r, c, BLACK);
275                         else
276                                 mvwaddch(ch_win, r, c, WHITE);
277                         c++;
278                 }
279                 r++;
280         }
281 }
282
283 /*---------------------------------------------------------------------------*
284  *      fill current character black/white
285  *---------------------------------------------------------------------------*/
286 void setchr(char type)
287 {
288         int r,c;
289
290         r = 1;
291
292         while(r <= ch_height)
293         {
294                 c = 1;
295                 while(c <= ch_width)
296                 {
297                         mvwaddch(ch_win, r, c, type);
298                         c++;
299                 }
300                 r++;
301         }
302 }
303
304 /*---------------------------------------------------------------------------*
305  *      set current row to black/white
306  *---------------------------------------------------------------------------*/
307 void setrow(char type)
308 {
309         int r,c;
310
311         getyx(ch_win,r,c);
312
313         c = 1;
314
315         while(c <= ch_width)
316         {
317                 mvwaddch(ch_win, r, c, type);
318                 c++;
319         }
320 }
321
322 /*---------------------------------------------------------------------------*
323  *      set current column to black/white
324  *---------------------------------------------------------------------------*/
325 void setcol(char type)
326 {
327         int r,c;
328
329         getyx(ch_win,r,c);
330
331         r = 1;
332
333         while(r <= ch_height)
334         {
335                 mvwaddch(ch_win, r, c, type);
336                 r++;
337         }
338 }
339
340 /*---------------------------------- E O F ----------------------------------*/