1 /*
2  * Simulator of microcontrollers (view.cc)
3  *
4  * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
5  *
6  * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
7  *
8  */
9 
10 /* This file is part of microcontroller simulator: ucsim.
11 
12 UCSIM is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16 
17 UCSIM is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 GNU General Public License for more details.
21 
22 You should have received a copy of the GNU General Public License
23 along with UCSIM; see the file COPYING.  If not, write to the Free
24 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 02111-1307, USA. */
26 /*@1@*/
27 
28 #include "ddconfig.h"
29 
30 #include <stdlib.h>
31 #include "i_string.h"
32 
33 #include "appcl.h"
34 
35 
36 /*
37  * Box
38  */
39 
cl_box(int ix,int iy,int iw,int ih)40 cl_box::cl_box(int ix, int iy, int iw, int ih):
41   cl_base()
42 {
43   x= ix;
44   y= iy;
45   w= iw;
46   h= ih;
47 }
48 
49 void
set(int ix,int iy,int iw,int ih)50 cl_box::set(int ix, int iy, int iw, int ih)
51 {
52   x= ix;
53   y= iy;
54   w= iw;
55   h= ih;
56 }
57 
58 void
move_rel(int dx,int dy)59 cl_box::move_rel(int dx, int dy)
60 {
61   x+= dx;
62   y+= dy;
63 }
64 
65 void
grow(int dw,int dh)66 cl_box::grow(int dw, int dh)
67 {
68   w+= dw;
69   h+= dh;
70 }
71 
72 
73 /*
74  * Astbract of a viewer
75  ******************************************************************************
76  */
77 
cl_view(class cl_box * ipos,char * iname,class cl_app * iapp)78 cl_view::cl_view(class cl_box *ipos, char *iname, class cl_app *iapp):
79   cl_base()
80 {
81   pos= new cl_box(ipos->x,ipos->y, ipos->w,ipos->h);
82   if ((window= newwin(pos->h,pos->w, pos->y,pos->x)))
83     {
84       leaveok(window, TRUE);
85       panel= new_panel(window);
86     }
87   else
88     {
89       panel= 0;
90       delwin(window);
91       window= 0;
92     }
93   parent= 0;
94   next= last= 0;
95   app= iapp;
96   state= SF_NOTHING;
97   options= OF_SELECTABLE;
98   if (!iname ||
99       !(*iname))
100     {
101       name= (char*)malloc(100);
102       sprintf(name, "view%p", this);
103     }
104   else
105     name= strdup(iname);
106 }
107 
cl_view(char * iname,class cl_app * iapp)108 cl_view::cl_view(char *iname, class cl_app *iapp):
109   cl_base()
110 {
111   window= 0;
112   panel= 0;
113   parent= 0;
114   next= last= 0;
115   app= iapp;
116   state= SF_NOTHING;
117   options= OF_SELECTABLE;
118   if (!iname ||
119       !(*iname))
120     {
121       name= (char*)malloc(100);
122       sprintf(name, "view%p", this);
123     }
124   else
125     name= strdup(iname);
126 }
127 
~cl_view(void)128 cl_view::~cl_view(void)
129 {
130   if (panel)
131     del_panel(panel);
132   if (window)
133     delwin(window);
134   if (palette)
135     free(palette);
136   if (name)
137     free(name);
138 }
139 
140 int
init(void)141 cl_view::init(void)
142 {
143   input= mk_input();
144   palette= mk_palette();
145   //draw();
146   return(0);
147 }
148 
149 class cl_gin *
mk_input(void)150 cl_view::mk_input(void)
151 {
152   return(0);
153 }
154 
155 int *
mk_palette(void)156 cl_view::mk_palette(void)
157 {
158   return(0);
159 }
160 
161 int
ok(void)162 cl_view::ok(void)
163 {
164   return(window && panel);
165 }
166 
167 
168 /*
169  * Make output into the view
170  */
171 
172 int
draw(void)173 cl_view::draw(void)
174 {
175   int color, x, y;
176 
177   color= get_color(palette?0:C_WIN_NORMAL);
178 
179   wattrset(window, color);
180   for (y= 0; y < pos->h; y++)
181     for (x= 0; x < pos->w; x++)
182       mvwaddch(window, y,x, ' ');
183   app->drawn++;
184 
185   return(0);
186 }
187 
188 int
update(void)189 cl_view::update(void)
190 {
191   draw();
192   update_panels();
193   doupdate();
194   return(0);
195 }
196 
197 int
get_color(int color)198 cl_view::get_color(int color)
199 {
200   int *p;
201   class cl_view *v;
202 
203   v= this;
204   while (v)
205     {
206       p= v->get_palette();
207       if (p)
208 	color= p[color];
209       v= v->parent;
210     }
211   return(color);
212 }
213 
214 int *
get_palette(void)215 cl_view::get_palette(void)
216 {
217   return(palette);
218 }
219 
220 
221 /*
222  * Event handling
223  */
224 
225 int
get_event(struct t_event * event)226 cl_view::get_event(struct t_event *event)
227 {
228   if (parent)
229     return(parent->get_event(event));
230   if (input)
231     return(input->get_event(event));
232   return(0);
233 }
234 
235 int
handle_event(struct t_event * event)236 cl_view::handle_event(struct t_event *event)
237 {
238   return(0);
239 }
240 
241 int
unhandled(struct t_event * event)242 cl_view::unhandled(struct t_event *event)
243 {
244   return(0);
245 }
246 
247 int
mk_event(struct t_event * event,FILE * f,int key)248 cl_view::mk_event(struct t_event *event, FILE *f, int key)
249 {
250   event->what= EV_KEY;
251   event->event.key= key;
252   return(1);
253 }
254 
255 
256 class cl_view *
prev(void)257 cl_view::prev(void)
258 {
259   class cl_view *v;
260 
261   v= next;
262   while (v != this)
263     v= v->next;
264   return(v);
265 }
266 
267 class cl_view *
prev_view(void)268 cl_view::prev_view(void)
269 {
270   if (parent &&
271       parent->first() == this)
272     return(0);
273   else
274     return(prev());
275 }
276 
277 
278 int
select(void)279 cl_view::select(void)
280 {
281   /*  class cl_view *v;
282 
283   if (!(options & OF_SELECTABLE))
284     return(0);
285   if (state & SF_SELECTED)
286     return(1);
287   if (parent &&
288       !(parent->select()))
289     return(0);
290   if (parent)
291     {
292       v= parent->current_sub_view();
293       if (v &&
294 	  v != this)
295 	v->unselect();
296       parent->set_current(this);
297     }
298   change_state(SF_FOCUSED, 1);
299   change_state(SF_SELECTED, 1);
300   draw();
301   return(1);*/
302   if (options & OF_SELECTABLE)
303     if (parent)
304       parent->set_current(this);
305   return(1);
306 }
307 
308 int
unselect(void)309 cl_view::unselect(void)
310 {
311   class cl_view *csv= current_sub_view();
312   if (csv &&
313       !(csv->unselect()))
314     return(0);
315   if (!terminal_view())
316     change_state(SF_FOCUSED, 0);
317   change_state(SF_SELECTED, 0);
318   draw();
319   return(1);
320 }
321 
322 class cl_view *
current_sub_view(void)323 cl_view::current_sub_view(void)
324 {
325   return(0);
326 }
327 
328 int
terminal_view(void)329 cl_view::terminal_view(void)
330 {
331   return(1);
332 }
333 
334 void
change_state(unsigned int what,int enable)335 cl_view::change_state(unsigned int what, int enable)
336 {
337   if (enable)
338     state|= what;
339   else
340     state&= ~what;
341   if (parent)
342     {
343       switch (what)
344 	{
345 	case SF_FOCUSED:
346 	  //reset_cursor();
347 	  /*message(parent, EV_BROADCAST,
348 	    (enable)?CM_RECEIVED_FOCUS:CM_RELEASED_FOCUS, this);*/
349 	  break;
350 	}
351     }
352   draw();
353 }
354 
355 
356 /* End of gui.src/view.cc */
357