]> ncurses.scripts.mit.edu Git - ncurses.git/blob - c++/cursespad.cc
ncurses 5.6 - patch 20070127
[ncurses.git] / c++ / cursespad.cc
1 // * this is for making emacs happy: -*-Mode: C++;-*-
2 /****************************************************************************
3  * Copyright (c) 1998-2003,2005 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
36 #include <stdio.h>
37
38 #include "etip.h"
39 #include "cursesw.h"
40
41 MODULE_ID("$Id: cursespad.cc,v 1.11 2005/07/23 20:51:23 tom Exp $")
42
43 NCursesPad::NCursesPad(int nlines, int ncols)
44   : NCursesWindow(),
45     viewWin(static_cast<NCursesWindow*>(0)),
46     viewSub(static_cast<NCursesWindow*>(0)),
47     h_gridsize(0), v_gridsize(0),
48     min_row(0), min_col(0)
49 {
50   w = ::newpad(nlines, ncols);
51   if (static_cast<WINDOW*>(0) == w) {
52     count--;
53     err_handler("Cannot construct window");
54   }
55   alloced = TRUE;
56 }
57
58
59 int NCursesPad::driver (int key)
60 {
61   // Default implementation
62   switch(key) {
63   case KEY_UP:
64     // =======
65     return REQ_PAD_UP;
66   case KEY_DOWN:
67     // =========
68     return REQ_PAD_DOWN;
69   case KEY_LEFT:
70     // =========
71     return REQ_PAD_LEFT;
72   case KEY_RIGHT:
73     // ==========
74     return REQ_PAD_RIGHT;
75   case KEY_EXIT:
76     // =========
77   case CTRL('X'):
78     // ==========
79     return REQ_PAD_EXIT;
80
81   default: return(key);
82   }
83 }
84
85
86 void NCursesPad::operator()(void)
87 {
88   NCursesWindow* W = Win();
89
90   if (static_cast<NCursesWindow*>(0) != W) {
91     int Width  = W->width();
92     int Height = W->height();
93
94     int req = REQ_PAD_REFRESH;
95
96     W->keypad(TRUE);
97     W->meta(TRUE);
98     refresh();
99
100     do {
101       bool changed = FALSE;
102
103       switch (req) {
104       case REQ_PAD_REFRESH:
105         // ================
106         changed = TRUE;
107         break;
108       case REQ_PAD_LEFT:
109         // =============
110         if (min_col > 0) {
111           changed = TRUE;
112           if (min_col < h_gridsize)
113             min_col = 0;
114           else
115             min_col -= h_gridsize;
116         }
117         else
118           OnNavigationError(req);
119         break;
120       case REQ_PAD_RIGHT:
121         // ==============
122         if (min_col < (width() - Width - 1)) {
123           changed = TRUE;
124           if (min_col > (width() - Width - h_gridsize - 1))
125             min_col = width() - Width - 1;
126           else
127             min_col += h_gridsize;
128         }
129         else
130           OnNavigationError(req);
131         break;
132       case REQ_PAD_UP:
133         // ===========
134         if (min_row > 0) {
135           changed = TRUE;
136           if (min_row < v_gridsize)
137             min_row = 0;
138           else
139             min_row -= v_gridsize;
140         }
141         else
142           OnNavigationError(req);
143         break;
144       case REQ_PAD_DOWN:
145         // =============
146         if (min_row < (height() - Height - 1)) {
147           changed = TRUE;
148           if (min_row > (height() - Height - v_gridsize - 1))
149             min_row = height() - Height - 1;
150           else
151             min_row += v_gridsize;
152         }
153         else
154           OnNavigationError(req);
155         break;
156
157       default:
158         OnUnknownOperation(req);
159       }
160
161       if (changed) {
162         noutrefresh();
163         W->syncup();
164         OnOperation(req);
165         viewWin->refresh();
166       }
167     } while( (req=driver(W->getch())) != REQ_PAD_EXIT );
168   }
169 }
170
171
172 int NCursesPad::refresh()
173 {
174   int res = noutrefresh();
175   if (res==OK && (static_cast<NCursesWindow*>(0) != viewWin)) {
176     res = (viewWin->refresh());
177   }
178   return(res);
179 }
180
181 int NCursesPad::noutrefresh()
182 {
183   int res = OK;
184   NCursesWindow* W = Win();
185   if (static_cast<NCursesWindow*>(0) != W) {
186     int high = W->maxy();
187     int wide = W->maxx();
188     res = copywin(*W, min_row, min_col,
189                   0, 0, high, wide,
190                   FALSE);
191     if (res==OK) {
192       W->syncup();
193       res = viewWin->noutrefresh();
194     }
195   }
196   return (res);
197 }
198
199 void NCursesPad::setWindow(NCursesWindow& view,
200                            int v_grid NCURSES_PARAM_INIT(1),
201                            int h_grid NCURSES_PARAM_INIT(1))
202 {
203   viewWin = &view;
204   min_row = min_col = 0;
205   if (h_grid <=0 || v_grid <= 0)
206     err_handler("Illegal Gridsize");
207   else {
208     h_gridsize = h_grid;
209     v_gridsize = v_grid;
210   }
211 }
212
213 void NCursesPad::setSubWindow(NCursesWindow& sub)
214 {
215   if (static_cast<NCursesWindow*>(0) == viewWin)
216     err_handler("Pad has no viewport");
217   if (!viewWin->isDescendant(sub))
218     THROW(new NCursesException("NCursesFramePad", E_SYSTEM_ERROR));
219   viewSub = &sub;
220 }
221
222 void NCursesFramedPad::OnOperation(int pad_req)
223 {
224   NCursesWindow* W = Win();
225   NCursesWindow* W2 = getWindow();
226
227   if ((static_cast<NCursesWindow*>(0) != W) && (static_cast<NCursesWindow*>(0) != W2)) {
228     int Width  = W->width();
229     int Height = W->height();
230     int i, row, col, h_len, v_len;
231
232     h_len = (Width*Width + width() - 1)/width();
233     if (h_len==0)
234       h_len = 1;
235     if (h_len > Width)
236       h_len = Width;
237
238     v_len = (Height*Height + height() - 1)/height();
239     if (v_len==0)
240       v_len = 1;
241     if (v_len > Height)
242       v_len = Height;
243
244     col  = (min_col * Width + width() - 1)  / width();
245     if (col + h_len > Width)
246       col = Width - h_len;
247
248     row  = (min_row * Height + height() - 1) / height();
249     if (row + v_len > Height)
250       row = Height - v_len;
251
252     W2->vline(1,Width+1,Height);
253     W2->attron(A_REVERSE);
254     if (v_len>=2) {
255       W2->addch(row+1,Width+1,ACS_UARROW);
256       for(i=2;i<v_len;i++)
257         W2->addch(row+i,Width+1,' ');
258       W2->addch(row+v_len,Width+1,ACS_DARROW);
259     }
260     else {
261       for(i=1;i<=v_len;i++)
262         W2->addch(row+i,Width+1,' ');
263     }
264     W2->attroff(A_REVERSE);
265
266     W2->hline(Height+1,1,Width);
267     W2->attron(A_REVERSE);
268     if (h_len >= 2) {
269       W2->addch(Height+1,col+1,ACS_LARROW);
270       for(i=2;i<h_len;i++)
271         W2->addch(Height+1,col+i,' ');
272       W2->addch(Height+1,col+h_len,ACS_RARROW);
273     }
274     else {
275       for(i=1;i<=h_len;i++)
276         W2->addch(Height+1,col+i,' ');
277     }
278     W2->attroff(A_REVERSE);
279   }
280 }