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

Last change on this file since 652 was 652, checked in by Sam Hocevar, 11 years ago

imlib: merge JCMouse into EventHandler?.

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