]> ncurses.scripts.mit.edu Git - ncurses.git/blob - c++/cursespad.cc
2d067a0e56f00790550fc8a9d35f11ed3a5b4a54
[ncurses.git] / c++ / cursespad.cc
1 // * this is for making emacs happy: -*-Mode: C++;-*-
2 /****************************************************************************
3  * Copyright (c) 1998-2002,2003 Free Software Foundation, Inc.              *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29
30 /****************************************************************************
31  *   Author: Juergen Pfeifer, 1999                                          *
32  ****************************************************************************/
33
34 #include "internal.h"
35 #include "etip.h"
36 #include "cursesw.h"
37
38 MODULE_ID("$Id: cursespad.cc,v 1.7 2003/10/25 15:04:46 tom Exp $")
39
40 NCursesPad::NCursesPad(int lines, int cols)
41   : NCursesWindow(),
42     viewWin((NCursesWindow*)0),
43     viewSub((NCursesWindow*)0),
44     h_gridsize(0), v_gridsize(0),
45     min_row(0), min_col(0)
46 {
47   w = ::newpad(lines,cols);
48   if ((WINDOW*)0==w) {
49     count--;
50     err_handler("Cannot construct window");
51   }
52   alloced = TRUE;
53 }
54
55
56 int NCursesPad::driver (int key)
57 {
58   // Default implementation
59   switch(key) {
60   case KEY_UP:
61     // =======
62     return REQ_PAD_UP;
63   case KEY_DOWN:
64     // =========
65     return REQ_PAD_DOWN;
66   case KEY_LEFT:
67     // =========
68     return REQ_PAD_LEFT;
69   case KEY_RIGHT:
70     // ==========
71     return REQ_PAD_RIGHT;
72   case KEY_EXIT:
73     // =========
74   case CTRL('X'):
75     // ==========
76     return REQ_PAD_EXIT;
77
78   default: return(key);
79   }
80 }
81
82
83 void NCursesPad::operator()(void)
84 {
85   NCursesWindow* W = Win();
86
87   if ((NCursesWindow*)0 != W) {
88     int Width  = W->width();
89     int Height = W->height();
90
91     int req = REQ_PAD_REFRESH;
92
93     W->keypad(TRUE);
94     W->meta(TRUE);
95     refresh();
96
97     do {
98       bool changed = FALSE;
99
100       switch (req) {
101       case REQ_PAD_REFRESH:
102         // ================
103         changed = TRUE;
104         break;
105       case REQ_PAD_LEFT:
106         // =============
107         if (min_col > 0) {
108           changed = TRUE;
109           if (min_col < h_gridsize)
110             min_col = 0;
111           else
112             min_col -= h_gridsize;
113         }
114         else
115           OnNavigationError(req);
116         break;
117       case REQ_PAD_RIGHT:
118         // ==============
119         if (min_col < (width() - Width - 1)) {
120           changed = TRUE;
121           if (min_col > (width() - Width - h_gridsize - 1))
122             min_col = width() - Width - 1;
123           else
124             min_col += h_gridsize;
125         }
126         else
127           OnNavigationError(req);
128         break;
129       case REQ_PAD_UP:
130         // ===========
131         if (min_row > 0) {
132           changed = TRUE;
133           if (min_row < v_gridsize)
134             min_row = 0;
135           else
136             min_row -= v_gridsize;
137         }
138         else
139           OnNavigationError(req);
140         break;
141       case REQ_PAD_DOWN:
142         // =============
143         if (min_row < (height() - Height - 1)) {
144           changed = TRUE;
145           if (min_row > (height() - Height - v_gridsize - 1))
146             min_row = height() - Height - 1;
147           else
148             min_row += v_gridsize;
149         }
150         else
151           OnNavigationError(req);
152         break;
153
154       default:
155         OnUnknownOperation(req);
156       }
157
158       if (changed) {
159         noutrefresh();
160         W->syncup();
161         OnOperation(req);
162         viewWin->refresh();
163       }
164     } while( (req=driver(W->getch())) != REQ_PAD_EXIT );
165   }
166 }
167
168
169 int NCursesPad::refresh()
170 {
171   int res = noutrefresh();
172   if (res==OK && ((NCursesWindow*)0 != viewWin)) {
173     res = (viewWin->refresh());
174   }
175   return(res);
176 }
177
178 int NCursesPad::noutrefresh()
179 {
180   int res = OK;
181   NCursesWindow* W = Win();
182   if ((NCursesWindow*)0 != W) {
183     res = copywin(*W,min_row,min_col,
184                   0,0,W->maxy(),W->maxx(),
185                   FALSE);
186     if (res==OK) {
187       W->syncup();
188       res = viewWin->noutrefresh();
189     }
190   }
191   return (res);
192 }
193
194 void NCursesPad::setWindow(NCursesWindow& view,
195                            int v_grid NCURSES_PARAM_INIT(1),
196                            int h_grid NCURSES_PARAM_INIT(1))
197 {
198   viewWin = &view;
199   min_row = min_col = 0;
200   if (h_grid <=0 || v_grid <= 0)
201     err_handler("Illegal Gridsize");
202   else {
203     h_gridsize = h_grid;
204     v_gridsize = v_grid;
205   }
206 }
207
208 void NCursesPad::setSubWindow(NCursesWindow& sub)
209 {
210   if ((NCursesWindow*)0 == viewWin)
211     err_handler("Pad has no viewport");
212   if (!viewWin->isDescendant(sub))
213     THROW(new NCursesException("NCursesFramePad", E_SYSTEM_ERROR));
214   viewSub = &sub;
215 }
216
217 void NCursesFramedPad::OnOperation(int pad_req)
218 {
219   NCursesWindow* W = Win();
220   NCursesWindow* Win = getWindow();
221
222   if (((NCursesWindow*)0 != W) && ((NCursesWindow*)0 != Win)) {
223     int Width  = W->width();
224     int Height = W->height();
225     int i, row, col, h_len, v_len;
226
227     h_len = (Width*Width + width() - 1)/width();
228     if (h_len==0)
229       h_len = 1;
230     if (h_len > Width)
231       h_len = Width;
232
233     v_len = (Height*Height + height() - 1)/height();
234     if (v_len==0)
235       v_len = 1;
236     if (v_len > Height)
237       v_len = Height;
238
239     col  = (min_col * Width + width() - 1)  / width();
240     if (col + h_len > Width)
241       col = Width - h_len;
242
243     row  = (min_row * Height + height() - 1) / height();
244     if (row + v_len > Height)
245       row = Height - v_len;
246
247     Win->vline(1,Width+1,Height);
248     Win->attron(A_REVERSE);
249     if (v_len>=2) {
250       Win->addch(row+1,Width+1,ACS_UARROW);
251       for(i=2;i<v_len;i++)
252         Win->addch(row+i,Width+1,' ');
253       Win->addch(row+v_len,Width+1,ACS_DARROW);
254     }
255     else {
256       for(i=1;i<=v_len;i++)
257         Win->addch(row+i,Width+1,' ');
258     }
259     Win->attroff(A_REVERSE);
260
261     Win->hline(Height+1,1,Width);
262     Win->attron(A_REVERSE);
263     if (h_len >= 2) {
264       Win->addch(Height+1,col+1,ACS_LARROW);
265       for(i=2;i<h_len;i++)
266         Win->addch(Height+1,col+i,' ');
267       Win->addch(Height+1,col+h_len,ACS_RARROW);
268     }
269     else {
270       for(i=1;i<=h_len;i++)
271         Win->addch(Height+1,col+i,' ');
272     }
273     Win->attroff(A_REVERSE);
274   }
275 }