source: abuse/trunk/src/imlib/jwindow.cpp @ 119

Last change on this file since 119 was 119, checked in by Sam Hocevar, 12 years ago
  • The small jwindow constructor can now take a name argument.
File size: 17.6 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *
5 *  This software was released into the Public Domain. As with most public
6 *  domain software, no warranty is made or implied by Crack dot Com or
7 *  Jonathan Clark.
8 */
9
10#include "config.h"
11
12#include "video.hpp"
13#include "image.hpp"
14#include "input.hpp"
15#include "event.hpp"
16#include "filter.hpp"
17#include "event.hpp"
18#include "jwindow.hpp"
19
20static int jw_left = 3, jw_right = 3, jw_top = 2, jw_bottom = 3;
21
22int frame_top() { return jw_top; }
23int frame_bottom() { return jw_bottom; }
24int frame_left() { return jw_left; }
25int frame_right() { return jw_right; }
26
27//
28//  Sets the size of the border around each window
29//
30void set_frame_size(int x)
31
32    if(x < 1)
33        x = 1;
34    jw_left = x;
35    jw_right = x;
36    jw_top = 2;
37    jw_bottom = x;
38}
39
40 // true if a window lies in this area
41int window_manager::window_in_area(int x1, int y1, int x2, int y2)
42{
43  for (jwindow *f=first;f;f=f->next)
44    if (f->x<=x2 && f->y<=y2 && f->x+f->l-1>=x1 && f->y+f->h-1>=y1)
45      return 1;
46  return 0;
47}
48
49void window_manager::grab_focus(jwindow *j)
50{ grab=j; }
51
52void window_manager::release_focus()
53{ grab=NULL; }
54
55
56void window_manager::close_window(jwindow *j)
57{
58    delete j;
59}
60
61void window_manager::hide_windows()
62{
63  jwindow *p;
64  for (p=first;p;p=p->next)
65  {
66    if (!p->is_hidden())
67    {
68      p->hide();
69      screen->add_dirty(p->x,p->y,p->x+p->l-1,p->y+p->h-1);
70    }
71  }
72}
73
74void window_manager::show_windows()
75{
76  jwindow *p;
77  for (p=first;p;p=p->next)
78    if (p->is_hidden())
79      show_window(p);     
80}
81
82void window_manager::hide_window(jwindow *j)
83{
84  jwindow *k;
85  if (j==first)
86    first=first->next;
87  else
88  {
89    for (k=first;k->next!=j;k=k->next)
90      k->screen->add_dirty(j->x-k->x,j->y-k->y,
91                   j->x+j->l-1-k->x,j->y+j->h-1-k->y);
92    k->screen->add_dirty(j->x-k->x,j->y-k->y,
93                   j->x+j->l-1-k->x,j->y+j->h-1-k->y);
94    k->next=j->next;
95  }
96  screen->add_dirty(j->x,j->y,j->x+j->l-1,j->y+j->h-1);
97  j->hide();
98}
99
100void window_manager::show_window(jwindow *j)
101{
102  if (j->is_hidden())
103  {
104    j->show();
105    j->screen->add_dirty(0,0,j->l-1,j->h-1);
106  }
107}
108
109void window_manager::get_event(event &ev)
110{
111  jwindow *j;
112  eh->get_event(ev);
113  if (ev.type==EV_KEY)
114    key_state[ev.key]=1;
115  else if (ev.type==EV_KEYRELEASE)
116    key_state[ev.key]=0;
117
118  if (state==inputing)
119  {
120    for (ev.window=NULL,j=first;j;j=j->next)
121      if (!j->is_hidden() && ev.mouse_move.x>=j->x && ev.mouse_move.y>=j->y &&
122          ev.mouse_move.x<j->x+j->l && ev.mouse_move.y<j->y+j->h)
123        ev.window=j;
124
125    if (!ev.window && grab) ev.window=grab;
126
127    if (ev.window)
128    {
129      int closew=0,movew=0;
130
131      if ((ev.type==EV_MOUSE_BUTTON && ev.mouse_button==1 && ev.window &&
132           ev.mouse_move.x>=ev.window->x && ev.mouse_move.y>=ev.window->y &&
133           ev.mouse_move.x<ev.window->x+ev.window->l && ev.mouse_move.y<ev.window->y+ev.window->y1()))
134      {
135        if (ev.mouse_move.x-ev.window->x<11) closew=1;
136        else if (ev.window->is_moveable()) movew=1;
137      } else if (grab)
138        ev.window=grab;
139
140      if (ev.type==EV_KEY && ev.key==JK_ESC)
141        closew=1;
142
143     
144   
145      if (closew)
146        ev.type=EV_CLOSE_WINDOW;
147      else if (movew)
148      {
149        int red=0;
150        if (ev.window==first)       // see if we need to raise the window
151        {
152          first=first->next;
153          if (first)
154            red=1;
155        }
156        else
157        {
158          jwindow *last=first;
159          for (;last->next!=ev.window;last=last->next);
160          if (ev.window->next)
161            red=1;
162          last->next=ev.window->next;
163        }
164        if (!first)
165          first=ev.window;
166        else
167        {
168          jwindow *last=first;
169          for (;last->next;last=last->next);
170          last->next=ev.window;
171        }
172        ev.window->next=NULL;
173        if (red)
174        {
175          jwindow *j=ev.window;
176/*        screen->add_dirty(j->x,j->y,j->x+j->l-1,j->y+j->h-1);
177          for (p=first;p!=j;p=p->next)
178            p->screen->add_dirty(j->x-p->x,j->y-p->y,j->x+j->l-1-p->x,j->y+j->h-1-p->y);*/
179          j->screen->add_dirty(0,0,j->l-1,j->h-1);
180          flush_screen();
181        }
182
183        state=dragging;
184        drag_window=ev.window;
185        drag_mousex=ev.window->x-ev.mouse_move.x;
186        drag_mousey=ev.window->y-ev.mouse_move.y;
187        ev.type=EV_SPURIOUS;
188      } else if (ev.window)
189        ev.window->inm->handle_event(ev,ev.window);
190    }
191  } else if (state==dragging)
192  {
193    ev.window=drag_window;
194    if (ev.type==EV_MOUSE_BUTTON && ev.mouse_button==0)  // user released the mouse
195    {
196      state=inputing;
197      ev.type=EV_SPURIOUS;
198    } else if (ev.type==EV_MOUSE_MOVE)
199    {
200       move_window(drag_window,ev.mouse_move.x+drag_mousex,ev.mouse_move.y+drag_mousey);
201       flush_screen();
202       ev.type=EV_DRAG_WINDOW;
203       ev.window_position.x=ev.mouse_move.x+drag_mousex;
204       ev.window_position.y=ev.mouse_move.y+drag_mousey;
205    }
206  }
207  if (ev.type==EV_REDRAW)
208  {
209    for (j=first;j;j=j->next)
210       j->screen->add_dirty(ev.redraw.x1-j->x,ev.redraw.y1-j->y,
211                     ev.redraw.x2-j->x,ev.redraw.y2-j->y);
212    screen->add_dirty(ev.redraw.x1,ev.redraw.y1,ev.redraw.x2,ev.redraw.y2);
213    flush_screen();
214    ev.type=EV_SPURIOUS;   // we took care of this one by ourselves.
215  }
216}
217
218void jwindow::resize(int L, int H)
219{
220  screen->change_size(L,H);
221  l=L; h=H;
222}
223
224void window_manager::resize_window(jwindow *j, int l, int h)
225{
226  jwindow *p;
227  screen->add_dirty(j->x,j->y,j->x+j->l-1,j->y+j->h-1);
228  for (p=first;p!=j;p=p->next)
229    p->screen->add_dirty(j->x-p->x,j->y-p->y,j->x+j->l-1-p->x,j->y+j->h-1-p->y);
230  j->resize(l,h);
231  if (!frame_suppress)
232  j->redraw();
233}
234
235void window_manager::move_window(jwindow *j, int x, int y)
236{
237    screen->add_dirty(j->x, j->y, j->x + j->l - 1, j->y + j->h - 1);
238    for(jwindow *p = first; p != j; p = p->next)
239        p->screen->add_dirty(j->x - p->x, j->y - p->y, j->x + j->l - 1 - p->x,
240                             j->y + j->h - 1 - p->y);
241    j->x = x;
242    j->y = y;
243    j->screen->add_dirty(0, 0, j->l - 1, j->h - 1);
244}
245
246window_manager::window_manager(image *Screen, palette *Pal, int Hi,
247                               int Med, int Low, JCFont *Font)
248{
249    wm = this;
250    screen = Screen;
251    hi = Hi; low = Low; med = Med; first = NULL; pal = Pal; grab = NULL;
252    bk = pal->find_closest(0, 0, 0);
253    state = inputing; fnt = Font;  wframe_fnt = Font;
254    memset(key_state, 0, sizeof(key_state));
255    eh = new event_handler(screen, pal);
256    frame_suppress = 0;
257}
258
259window_manager::~window_manager()
260{
261    delete eh;
262    while(first)
263        close_window(first);
264    wm = NULL;
265}
266
267void window_manager::add_window(jwindow *win)
268{
269    if(!first)
270        first = win;
271    else
272    {
273        jwindow *tmp = first;
274        while(tmp->next)
275            tmp = tmp->next;
276        tmp->next = win;
277        win->next = NULL;
278    }
279}
280
281void window_manager::remove_window(jwindow *win)
282{
283    if(grab == win)
284        grab = NULL;
285
286    // close the window we were dragging
287    if(state == dragging && win == drag_window)
288        state = inputing;
289
290    if(first == win)
291        first = first->next;
292    else
293    {
294        jwindow * search;
295        for(search = first; search->next != win; search = search->next)
296            search->screen->add_dirty(win->x - search->x,
297                                      win->y - search->y,
298                                      win->x + win->l - 1 - search->x,
299                                      win->y + win->h - 1 - search->y);
300        search->screen->add_dirty(win->x - search->x, win->y - search->y,
301                                  win->x + win->l - 1 - search->x,
302                                  win->y + win->h - 1 - search->y);
303        search->next = win->next;
304    }
305
306    screen->add_dirty(win->x, win->y, win->x + win->l - 1,
307                      win->y + win->h - 1);
308}
309
310jwindow * window_manager::new_window(int x, int y, int l, int h,
311                                     ifield * fields, char const *name)
312{
313    if(x > screen->width () - 4)
314        x = screen->width () - 25;
315    if(y > screen->height () - 4)
316        y = screen->height () - 10;
317 
318    jwindow * j = new jwindow (x, y, l, h, fields, name);
319    j->show();
320
321    return j;
322}
323
324void window_manager::flush_screen()
325{
326  jwindow *p,*q;
327
328  int mx=0,my=0;
329  image *mouse_pic=NULL,*mouse_save=NULL;
330 
331  if (has_mouse())
332  {   
333    mouse_pic=eh->mouse_sprite()->visual;
334    mouse_save=eh->mouse_sprite()->save;
335    mx=eh->mouse->drawx();
336    my=eh->mouse->drawy();
337
338    screen->put_part(mouse_save,0,0,mx,my,mx+mouse_pic->width()-1,my+mouse_pic->height()-1);
339    mouse_pic->put_image(screen,mx,my,1);
340  }
341 
342  for (p=first;p;p=p->next)
343    if (!p->is_hidden())
344       screen->delete_dirty(p->x,p->y,p->x+p->l-1,p->y+p->h-1);
345  update_dirty(screen);
346
347  if (has_mouse())
348    mouse_save->put_image(screen,mx,my);
349
350
351  for (p=first;p;p=p->next)
352  {
353    if (!p->is_hidden())
354    {
355      if (has_mouse())
356      {     
357        p->screen->put_part(mouse_save,0,0,mx-p->x,my-p->y,
358                            mx-p->x+mouse_pic->width()-1,
359                            my-p->y+mouse_pic->height()-1);
360        if (has_mouse())
361        mouse_pic->put_image(p->screen,mx-p->x,my-p->y,1);
362      }
363     
364
365//      screen->delete_dirty(p->x,p->y,p->x+p->l-1,p->y+p->h-1);
366      for (q=p->next;q;q=q->next)
367        if (!q->is_hidden())
368          p->screen->delete_dirty(q->x-p->x,
369                              q->y-p->y,
370                              q->x+q->l-1-p->x,
371                              q->y+q->h-1-p->y);
372      update_dirty(p->screen,p->x,p->y);
373      if (has_mouse())
374         mouse_save->put_image(p->screen,mx-p->x,my-p->y,0);
375    }
376  }
377}
378
379jwindow::jwindow(char const *name)
380{
381    _x1 = left_border();
382    _y1 = jw_top + 5;
383    _x2 = _y2 = 0;
384
385    _hidden = true;
386    _moveable = true;
387    // property.flags = JWINDOW_NOAUTOHIDE_FLAG;
388
389    inm = new input_manager(this, NULL);
390    reconfigure();
391
392    screen = NULL;
393    next = NULL;
394
395    _name = NULL;
396    if(name)
397        _name = strcpy((char *)jmalloc(strlen(name) + 1,
398                                       "jwindow::window name"), name);
399    wm->add_window(this);
400}
401
402jwindow::jwindow(int X, int Y, int L, int H, ifield *f, char const *name)
403{
404    l = 0;
405    h = 0;
406    _hidden = false;
407    _moveable = true;
408
409    _x1 = left_border();
410    _y1 = name ? top_border() : jw_top + 5;
411
412    screen = NULL;
413    inm = new input_manager(screen, f);
414    reconfigure(); /* FIXME: TODO */
415
416    l = L >= 0 ? L + left_border() : l - L;
417    h = H >= 0 ? H + top_border() : h - H;
418    y = Y >= 0 ? Y : yres - h + Y - top_border() - bottom_border() - 1;
419    x = X >= 0 ? X : xres - l + X - left_border() - right_border() - 1;
420
421    backg = wm->medium_color();
422
423    _x2 = l - 1;
424    _y2 = h - 1;
425    l += right_border();
426    h += bottom_border();
427
428    if(L == -1)
429        if(l < 15 + left_border() + right_border())
430            l = 15 + left_border() + right_border();
431    if(H == -1)
432        if(h < top_border() + bottom_border())
433            h = top_border() + bottom_border();
434    screen = new image(l, h, NULL, 2);
435    screen->clear(backg);
436    // Keep this from getting destroyed when image list is cleared
437    image_list.unlink(screen);
438    inm->screen = screen;
439
440    next = NULL;
441
442    _name = NULL;
443    if(name)
444        _name = strcpy((char *)jmalloc(strlen(name) + 1,
445                                       "jwindow::window name"), name);
446
447    wm->add_window(this);
448    if(!wm->frame_suppress)
449        redraw();
450}
451
452jwindow::~jwindow()
453{
454    wm->remove_window(this);
455    local_close();
456    if(screen)
457        delete screen;
458    delete inm;
459    if(_name)
460        jfree(_name);
461}
462
463void jwindow::reconfigure()
464{
465    int x1, y1, x2, y2;
466    ifield *i;
467    l = 2;
468    h = 2;
469    for(i = inm->first; i; i = i->next)
470    {
471        i->set_owner(this);
472        i->area(x1, y1, x2, y2);
473        if ((int)y2 > (int)h)
474            h = y2;
475        if ((int)x2 > (int)l)
476            l = x2;
477    }
478}
479
480void jwindow::local_close()
481{
482    ;
483}
484
485void jwindow::redraw()
486{
487    int hi = wm->bright_color ();
488    int med = wm->medium_color ();
489    int low = wm->dark_color ();
490    JCFont * fnt = wm->frame_font ();
491
492    if(_name)
493    {
494        if (right_border() >= 1)
495        {
496            screen->widget_bar (0, 0, l - 1, h - 1, hi, med, low);
497            if (right_border() >= 3)
498                screen->widget_bar (right_border() - 1, top_border() - 1,
499                                l - left_border(), h - bottom_border(), low,
500                                med, hi);
501         
502          else
503            screen->line (left_border() - 1, top_border() - 1,
504                           right_border() - 1, top_border() - 1, low);
505        }
506      screen->rectangle (2, 2, top_border() - 2, top_border() - 3,
507                           wm->black ());
508      screen->widget_bar (3, 3, top_border() - 3, top_border() - 4, hi, med, low);     // draws the 'close' button
509    }
510 
511  else
512    {
513      if (right_border() >= 1)
514        {
515          screen->widget_bar (0, 0, l - 1, h - 1, hi, med, low);
516          if (right_border() >= 3)
517            screen->widget_bar (right_border() - 1, jw_top + 4,
518                                l - left_border(), h - bottom_border(), low,
519                                med, hi);
520         
521          else
522            screen->line (left_border() - 1, jw_top + 4, right_border() - 1,
523                           jw_top + 4, low);
524        }
525      screen->rectangle (1, 1, 4, 4, wm->black ());
526      screen->widget_bar (2, 2, 3, 3, hi, med, low);   // draws the 'close' button
527    }
528  if (_name && _name[0])
529    {
530      screen->bar (top_border(), 1,
531                    top_border() + fnt->width () * strlen (_name) + 1,
532                    top_border() - 2, med);
533      fnt->put_string (screen, top_border() + 1, 1, _name, low);
534    }
535  screen->bar (x1 (), y1 (), x2 (), y2 (), backg);  // clear 'client' region
536  inm->redraw ();
537}
538
539int jwindow::left_border()
540{
541    return frame_left();
542}
543
544int jwindow::right_border()
545{
546    return frame_right();
547}
548
549int jwindow::top_border()
550{
551    return wm->font()->height() + frame_top();
552}
553
554int jwindow::bottom_border()
555{
556    return frame_bottom();
557}
558
559
560ifield *input_manager::unlink(int id)     // unlinks ID from fields list and return the pointer to it
561{
562  for (ifield *i=first,*last=NULL;i;i=i->next)
563  {
564    if (i->id==id)
565    {
566      if (i==first)
567        first=first->next;
568      else
569        last->next=i->next;
570      if (active==i)
571        active=first;
572      return i;
573    }
574    ifield *x=i->unlink(id);
575    if (x) return x;
576    last=i;
577  }
578  return NULL;   // no such id
579}
580
581input_manager::~input_manager()
582{ ifield *i;
583  while (first)
584  { i=first;
585    first=first->next;
586    delete i;
587  }
588}
589
590void input_manager::clear_current()
591{
592    if(owner)
593        screen = owner->screen;
594    if(active)
595        active->draw(0, screen);
596    active = NULL;
597}
598
599void input_manager::handle_event(event &ev, jwindow *j)
600{
601  ifield *i,*in_area=NULL;
602  int x1,y1,x2,y2;
603
604  if(owner)
605      screen = owner->screen;
606
607  if (j)
608  {
609    ev.mouse_move.x-=j->x;
610    ev.mouse_move.y-=j->y;
611    cur=j;
612  }
613
614  if (!grab)
615  {
616    if ((ev.type==EV_MOUSE_BUTTON && ev.mouse_button==1) || ev.type==EV_MOUSE_MOVE)
617    {
618      for (i=first;i;i=i->next)
619      {
620        i->area(x1,y1,x2,y2);
621        if (ev.mouse_move.x>=x1 && ev.mouse_move.y>=y1 &&
622            ev.mouse_move.x<=x2 && ev.mouse_move.y<=y2)
623        in_area=i;
624      }
625      if (in_area!=active && (no_selections_allowed || (in_area && in_area->selectable())))
626      {
627        if (active)
628          active->draw(0,screen);
629
630        active=in_area;
631
632        if (active)
633          active->draw(1,screen);
634      }
635    }
636    if (ev.type==EV_KEY && ev.key==JK_TAB && active)
637    {
638      active->draw(0,screen);
639      do
640      {
641        active=active->next;
642        if (!active) active=first;
643      } while (active && !active->selectable());
644      active->draw(1,screen);
645    }
646  } else active=grab;
647
648  if (active)
649  {
650    if (ev.type!=EV_MOUSE_MOVE && ev.type!=EV_MOUSE_BUTTON)
651      active->handle_event(ev,screen,this);
652    else
653    {
654      active->area(x1,y1,x2,y2);
655      if (grab || (ev.mouse_move.x>=x1 && ev.mouse_move.y>=y1 &&
656          ev.mouse_move.x<=x2 && ev.mouse_move.y<=y2))
657      {
658        if (j)
659          active->handle_event(ev,screen,j->inm);
660        else active->handle_event(ev,screen,this);
661      }
662    }
663  }
664
665  if (j)
666  {
667    ev.mouse_move.x+=j->x;
668    ev.mouse_move.y+=j->y;
669  }
670}
671
672void input_manager::allow_no_selections()
673{
674  no_selections_allowed=1;
675}
676
677void input_manager::redraw()
678{
679    ifield *i;
680    if(owner)
681        screen = owner->screen;
682    for(i = first; i; i = i->next)
683        i->draw_first(screen);
684    if(active)
685        active->draw(1, screen);
686}
687
688input_manager::input_manager(image *Screen, ifield *First)
689{
690    no_selections_allowed = 0;
691    cur = NULL;
692    grab = NULL;
693    owner = NULL;
694    screen = Screen;
695    active = first = First;
696    while(active && !active->selectable())
697        active = active->next;
698    if(screen)
699        redraw();
700}
701
702input_manager::input_manager(jwindow *Owner, ifield *First)
703{
704    no_selections_allowed = 0;
705    cur = NULL;
706    grab = NULL;
707    owner = Owner;
708    screen = NULL;
709    active = first = First;
710    while(active && !active->selectable())
711        active = active->next;
712}
713
714void input_manager::grab_focus(ifield *i)
715{ grab=i;
716  if (cur)
717    wm->grab_focus(cur);
718}
719
720void input_manager::release_focus()
721{ grab=NULL;
722  if (cur)
723    wm->release_focus();
724}
725
726void input_manager::remap(filter *f)
727{
728  for (ifield *i=first;i;i=i->next)
729   i->remap(f);
730  redraw();
731}
732
733void input_manager::add(ifield *i)
734{ ifield *f=first;
735  if (i->selectable())
736  {
737    if (!f)
738      first=i;
739    else
740    {
741      while (f->next) f=f->next;
742      f->next=i;
743    }
744  }
745}
746
747ifield *input_manager::get(int id)
748{
749  ifield *f;
750  for (f=first;f;f=f->next)
751  {
752    ifield *ret=f->find(id);
753    if (ret) return ret;
754  }
755  return NULL;
756}
757
758ifield::ifield()
759{
760    owner = NULL;
761    x = 0;
762    y = 0;
763    next = NULL;
764    id = 0;
765}
766
767ifield::~ifield()
768{
769    ;
770}
771
772/* re-position the control with respect to the "client" area of the window */
773void ifield::set_owner(jwindow * newowner)
774{
775    if(owner)
776        move(x - owner->x1(), y - owner->y1());
777    owner = newowner;
778    if(owner)
779        move(x + owner->x1(), y + owner->y1());
780}
781
Note: See TracBrowser for help on using the repository browser.