File: slw.cpp

package info (click to toggle)
freej 0.10git20100110-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 32,080 kB
  • ctags: 22,705
  • sloc: cpp: 156,254; ansic: 25,531; sh: 13,538; perl: 4,624; makefile: 3,278; python: 2,889; objc: 1,284; asm: 1,125; ruby: 126
file content (263 lines) | stat: -rw-r--r-- 5,148 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*  S-Lang console widgets
 *
 *  (C) Copyright 2004-2006 Denis Rojo <jaromil@dyne.org>
 *
 * This source code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Public License as published 
 * by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 *
 * This source code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * Please refer to the GNU Public License for more details.
 *
 * You should have received a copy of the GNU Public License along with
 * this source code; if not, write to:
 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <stdlib.h>

#include <slw.h>
#include <slw_console.h>

SLangWidget::SLangWidget()
  : Entry() {
  
  int c;
  
  // real name will be set by inheriting widget
  set_name("unknown");
  
  // reset geometry
  w = 0;
  h = 0;
  cur_x = 0;
  cur_y = 0;
  orig_x = 0;
  orig_y = 0;
  
  border = false;
  visible = false;
  cursor = false;
  initialized = false;
  can_focus = false;
  
  console = 0x0;
  
  // maximum lenght for a row
  max_row = 512;

  color = 1; // white on black
  
  
  // generate a blank row for line blanking
  blankrow = (char*)malloc(max_row*sizeof(char));
  for(c=0; c<max_row; c++) {
    blankrow[c] = CHAR_BLANK;
  }
    
}

SLangWidget::~SLangWidget() {   
  func("de-allocating widget");
  free(blankrow);
  
  rem();
}

int SLangWidget::feed_string(char *str) {
  register int c;
  char *p;
  
  for( p = &str[0], c = 0 ;
       *p != '\0'
	 && *p != '\n'
	 && *p != EOT;
       p++,         c++)
    this->feed( (int)(*p) );
  
  if( *p == '\n' ) feed( KEY_ENTER );
  
  return c;
}

// wrapper for broken polimorphism in old compilers
void SLangWidget::feed_key(int k) {
  this->feed( k );
}

bool SLangWidget::gotoxy(int x, int y) {
  
  // check validity of coordinates
  if( ! check(x, y) ) {
    fprintf(stderr, "gotoxy called with invalid coords");
    return false;
  }
  SLsmg_gotorc(y + orig_y, x + orig_x);
  
  return true;
}

bool SLangWidget::putch(CHAR ch, int x, int y) {

  // check validity of coordinates
  if( ! check(x, y) ) {
    fprintf(stderr, "putch called with invalid coords");
    return false;
  }

  SLsmg_set_color(color);
   
  SLsmg_gotorc( y + orig_y, x + orig_x );
  SLsmg_write_char(ch);
  
  return true;
}

int SLangWidget::putnch(CHAR *str, int x, int y, int nchars) {

  int len;
  int nwrap = 0;
  
  if( !nchars )
    nchars = strlen(str);
  
  // check validity of coordinates
  if( ! check(x, y) ) {
    fprintf(stderr, "putnch called with invalid coords");
    return false;
  }
  
  /* further check to cut off on the right bound
   * in case we are not wrapping, otherwise go down
   * to the next row.
   
   nchars  w  cut
   x-----------|-----              
  */
  if( x+nchars > w) {
 /*   
    if(wrap) {
      
      // here we do the wrapping
      // this is not yet working as it should
      int rlen, xwrap;
      
      for(rlen = nchars, xwrap = x;
	  rlen > 0;
	  rlen -= w-xwrap, nwrap++, xwrap = 0) {
	
	SLsmg_gotorc( nwrap+y+orig_y, xwrap+orig_x );
	SLsmg_write_nchars( str, w-xwrap );
	// if lower bound is reached, stop printing
	if(nwrap+y > h) return nwrap;
	
      }
      
      return nwrap;
      
    } else {   // no wrap: just cut off the rightmost part
   */   
      len = w - x;
      
  //  }
    
  } else // line fits in width
    len = nchars;
  
  SLsmg_set_color(color);

  SLsmg_gotorc( y+orig_y, x+orig_x );

  SLsmg_write_nchars( str, len );

  return nwrap;
}

void SLangWidget::blank_row(int y) {
  
  if( ! check(0,y) ) {
    fprintf(stderr, "blank_row failed: y=%u",y);
    return;
  }

  SLsmg_set_color(color);
  
  SLsmg_gotorc( y+orig_y, orig_x );
  
  SLsmg_write_nchars( blankrow, w);
  
}

void SLangWidget::blank() {
  register int c;
  SLsmg_set_color(color);
  for(c=h;c>=0;c--) {
    SLsmg_gotorc( c+orig_y,orig_x);
    SLsmg_write_nchars( blankrow, w);
  }
}

	
bool SLangWidget::check(int x, int y) {

  if(!console) { // check if its placed
    fprintf(stderr, "can't draw on unplaced widget '%s'", name);
    return false;
  }
  
  if(!initialized) { // check if its initialized
    fprintf(stderr, "can't operate on non initialized widget '%s'",name);
    return false;
  }
  
  if( x>w || y>h ) { // check bounds
    fprintf(stderr, "position out of bounds (%u,%u) in widget '%s'",
	    x, y, name);
    return false;
  }
  return true;
}

void SLangWidget::set_color(int col) {
  // check if color is valid
  switch(col) {
  case 1:
  case 2:
  case 3:
  case 4:
  case 5:
  case 6:
  case 7:
  case 11:
  case 12:
  case 13:
  case 14:
  case 15:
  case 16:
  case 17:
  case 21:
  case 22:
  case 23:
  case 24:
  case 25:
  case 26:
  case 27:
  case 31:
  case 32:
  case 33:
  case 34:
  case 35:
  case 36:
  case 37:
    func("color of widget %s set to %u",name, col);
    color = col;
    break;
  default:
    fprintf(stderr, "invalid color %u selected for widget %s", col, name);
    break;
  }
}