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

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

imlib: some refactoring in JWindow.

File size: 18.2 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    Jwindow *p, *q;
331
332    int mx = 0, my = 0;
333    image *mouse_pic = NULL, *mouse_save = NULL;
334
335    if(has_mouse())
336    {
337        mouse_pic = mouse_sprite()->visual;
338        mouse_save = mouse_sprite()->save;
339        mx = mouse->drawx();
340        my = mouse->drawy();
341
342        mouse_save->PutPart(m_surf, 0, 0, mx, my,
343                            mx + mouse_pic->Size().x - 1,
344                            my + mouse_pic->Size().y - 1);
345        m_surf->PutImage(mouse_pic, mx, my, 1);
346    }
347
348    for(p = m_first; p; p = p->next)
349        if(!p->is_hidden())
350            m_surf->delete_dirty(p->x, p->y, p->x + p->l, p->y + p->h);
351    update_dirty(m_surf);
352
353    if(has_mouse())
354        m_surf->PutImage(mouse_save, mx, my);
355
356    for(p = m_first; p; p = p->next)
357    {
358        if(p->is_hidden())
359            continue;
360
361        if(has_mouse())
362        {
363            mouse_save->PutPart(p->m_surf, 0, 0, mx - p->x, my - p->y,
364                                mx - p->x + mouse_pic->Size().x - 1,
365                                my - p->y + mouse_pic->Size().y - 1);
366            p->m_surf->PutImage(mouse_pic, mx - p->x, my - p->y, 1);
367        }
368
369//          m_surf->delete_dirty(p->x, p->y, p->x+p->l, p->y+p->h);
370        for(q = p->next; q; q = q->next)
371            if(!q->is_hidden())
372                p->m_surf->delete_dirty(q->x - p->x, q->y - p->y,
373                                          q->x + q->l - p->x,
374                                          q->y + q->h - p->y);
375        update_dirty(p->m_surf, p->x, p->y);
376        if(has_mouse())
377            p->m_surf->PutImage(mouse_save, mx - p->x, my - p->y, 0);
378    }
379}
380
381Jwindow::Jwindow(char const *name)
382{
383    _x1 = left_border();
384    _y1 = jw_top + 5;
385    _x2 = _y2 = 0;
386
387    _hidden = true;
388    _moveable = true;
389    // property.flags = JWINDOW_NOAUTOHIDE_FLAG;
390
391    inm = new InputManager(this, NULL);
392    reconfigure();
393
394    m_surf = NULL;
395    next = NULL;
396
397    _name = NULL;
398    if(name)
399        _name = strdup(name);
400    wm->add_window(this);
401}
402
403Jwindow::Jwindow(int X, int Y, int L, int H, ifield *f, char const *name)
404{
405    l = 0;
406    h = 0;
407    _hidden = false;
408    _moveable = true;
409
410    _x1 = left_border();
411    _y1 = name ? top_border() : jw_top + 5;
412
413    m_surf = NULL;
414    inm = new InputManager(m_surf, f);
415    reconfigure(); /* FIXME: TODO */
416
417    l = L >= 0 ? L + left_border() : l - L;
418    h = H >= 0 ? H + top_border() : h - H;
419    y = Y >= 0 ? Y : yres - h + Y - top_border() - bottom_border() - 1;
420    x = X >= 0 ? X : xres - l + X - left_border() - right_border() - 1;
421
422    backg = wm->medium_color();
423
424    _x2 = l - 1;
425    _y2 = h - 1;
426    l += right_border();
427    h += bottom_border();
428
429    if(L == -1)
430        if(l < 15 + left_border() + right_border())
431            l = 15 + left_border() + right_border();
432    if(H == -1)
433        if(h < top_border() + bottom_border())
434            h = top_border() + bottom_border();
435    m_surf = new image(vec2i(l, h), NULL, 2);
436    m_surf->clear(backg);
437    // Keep this from getting destroyed when image list is cleared
438    image_list.unlink(m_surf);
439    inm->m_surf = m_surf;
440
441    next = NULL;
442
443    _name = NULL;
444    if(name)
445        _name = strdup(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    delete m_surf;
457    delete inm;
458    if(_name)
459        free(_name);
460}
461
462void Jwindow::reconfigure()
463{
464    int x1, y1, x2, y2;
465    ifield *i;
466    l = 2;
467    h = 2;
468    for(i = inm->m_first; i; i = i->next)
469    {
470        i->set_owner(this);
471        i->area(x1, y1, x2, y2);
472        if ((int)y2 > (int)h)
473            h = y2;
474        if ((int)x2 > (int)l)
475            l = x2;
476    }
477}
478
479void Jwindow::local_close()
480{
481    ;
482}
483
484void Jwindow::redraw()
485{
486    int hi = wm->bright_color ();
487    int med = wm->medium_color ();
488    int low = wm->dark_color ();
489    JCFont * fnt = wm->frame_font ();
490
491    if(_name)
492    {
493        if (right_border() >= 1)
494        {
495            m_surf->widget_bar (0, 0, l - 1, h - 1, hi, med, low);
496            if (right_border() >= 3)
497                m_surf->widget_bar(right_border() - 1, top_border() - 1,
498                                     l - left_border(), h - bottom_border(),
499                                     low, med, hi);
500
501          else
502            m_surf->line (left_border() - 1, top_border() - 1,
503                           right_border() - 1, top_border() - 1, low);
504        }
505      m_surf->rectangle (2, 2, top_border() - 2, top_border() - 3,
506                           wm->black ());
507      m_surf->widget_bar (3, 3, top_border() - 3, top_border() - 4, hi, med, low);     // draws the 'close' button
508    }
509
510  else
511    {
512      if (right_border() >= 1)
513        {
514          m_surf->widget_bar (0, 0, l - 1, h - 1, hi, med, low);
515          if (right_border() >= 3)
516            m_surf->widget_bar (right_border() - 1, jw_top + 4,
517                                l - left_border(), h - bottom_border(), low,
518                                med, hi);
519
520          else
521            m_surf->line (left_border() - 1, jw_top + 4, right_border() - 1,
522                           jw_top + 4, low);
523        }
524      m_surf->rectangle (1, 1, 4, 4, wm->black ());
525      m_surf->widget_bar (2, 2, 3, 3, hi, med, low);   // draws the 'close' button
526    }
527  if (_name && _name[0])
528    {
529      m_surf->bar (top_border(), 1,
530                    top_border() + fnt->width() * strlen (_name) + 1,
531                    top_border() - 2, med);
532      fnt->put_string (m_surf, top_border() + 1, 1, _name, low);
533    }
534  m_surf->bar (x1 (), y1 (), x2 (), y2 (), backg);  // clear 'client' region
535  inm->redraw ();
536}
537
538int Jwindow::left_border()
539{
540    return frame_left();
541}
542
543int Jwindow::right_border()
544{
545    return frame_right();
546}
547
548int Jwindow::top_border()
549{
550    return wm->font()->height() + frame_top();
551}
552
553int Jwindow::bottom_border()
554{
555    return frame_bottom();
556}
557
558
559ifield *InputManager::unlink(int id)     // unlinks ID from fields list and return the pointer to it
560{
561  for (ifield *i=m_first,*last=NULL; i; i=i->next)
562  {
563    if (i->id==id)
564    {
565      if (i==m_first)
566    m_first=m_first->next;
567      else
568        last->next=i->next;
569      if (m_active==i)
570        m_active=m_first;
571      return i;
572    }
573    ifield *x=i->unlink(id);
574    if (x) return x;
575    last=i;
576  }
577  return NULL;   // no such id
578}
579
580InputManager::~InputManager()
581{ ifield *i;
582  while (m_first)
583  { i=m_first;
584    m_first=m_first->next;
585    delete i;
586  }
587}
588
589void InputManager::clear_current()
590{
591    if(m_owner)
592        m_surf = m_owner->m_surf;
593    if(m_active)
594        m_active->draw(0, m_surf);
595    m_active = NULL;
596}
597
598void InputManager::handle_event(Event &ev, Jwindow *j)
599{
600  ifield *i,*in_area=NULL;
601  int x1,y1,x2,y2;
602
603  if(m_owner)
604      m_surf = m_owner->m_surf;
605
606  if (j)
607  {
608    ev.mouse_move.x-=j->x;
609    ev.mouse_move.y-=j->y;
610    m_cur=j;
611  }
612
613  if (!m_grab)
614  {
615    if ((ev.type==EV_MOUSE_BUTTON && ev.mouse_button==1) || ev.type==EV_MOUSE_MOVE)
616    {
617      for (i=m_first; i; i=i->next)
618      {
619    i->area(x1,y1,x2,y2);
620    if (ev.mouse_move.x>=x1 && ev.mouse_move.y>=y1 &&
621        ev.mouse_move.x<=x2 && ev.mouse_move.y<=y2)
622        in_area=i;
623      }
624      if (in_area!=m_active && (no_selections_allowed || (in_area && in_area->selectable())))
625      {
626    if (m_active)
627          m_active->draw(0,m_surf);
628
629    m_active=in_area;
630
631    if (m_active)
632      m_active->draw(1,m_surf);
633      }
634    }
635    if (ev.type==EV_KEY && ev.key==JK_TAB && m_active)
636    {
637      m_active->draw(0,m_surf);
638      do
639      {
640    m_active=m_active->next;
641    if (!m_active) m_active=m_first;
642      } while (m_active && !m_active->selectable());
643      m_active->draw(1,m_surf);
644    }
645  } else m_active=m_grab;
646
647  if (m_active)
648  {
649    if (ev.type!=EV_MOUSE_MOVE && ev.type!=EV_MOUSE_BUTTON)
650      m_active->handle_event(ev,m_surf,this);
651    else
652    {
653      m_active->area(x1,y1,x2,y2);
654      if (m_grab || (ev.mouse_move.x>=x1 && ev.mouse_move.y>=y1 &&
655          ev.mouse_move.x<=x2 && ev.mouse_move.y<=y2))
656      {
657    if (j)
658      m_active->handle_event(ev,m_surf,j->inm);
659    else m_active->handle_event(ev,m_surf,this);
660      }
661    }
662  }
663
664  if (j)
665  {
666    ev.mouse_move.x+=j->x;
667    ev.mouse_move.y+=j->y;
668  }
669}
670
671void InputManager::allow_no_selections()
672{
673    no_selections_allowed=1;
674}
675
676void InputManager::redraw()
677{
678    ifield *i;
679    if(m_owner)
680        m_surf = m_owner->m_surf;
681    for(i = m_first; i; i = i->next)
682        i->draw_first(m_surf);
683    if(m_active)
684        m_active->draw(1, m_surf);
685}
686
687InputManager::InputManager(image *screen, ifield *first)
688{
689    no_selections_allowed = 0;
690    m_cur = NULL;
691    m_grab = NULL;
692    m_owner = NULL;
693    m_surf = screen;
694    m_active = m_first = first;
695    while(m_active && !m_active->selectable())
696        m_active = m_active->next;
697    if(m_surf)
698        redraw();
699}
700
701InputManager::InputManager(Jwindow *owner, ifield *first)
702{
703    no_selections_allowed = 0;
704    m_cur = NULL;
705    m_grab = NULL;
706    m_owner = owner;
707    m_surf = NULL;
708    m_active = m_first = first;
709    while(m_active && !m_active->selectable())
710        m_active = m_active->next;
711}
712
713void InputManager::grab_focus(ifield *i)
714{
715    m_grab = i;
716    if (m_cur)
717        wm->grab_focus(m_cur);
718}
719
720void InputManager::release_focus()
721{
722    m_grab = NULL;
723    if (m_cur)
724        wm->release_focus();
725}
726
727void InputManager::remap(Filter *f)
728{
729    for (ifield *i = m_first; i; i = i->next)
730        i->remap(f);
731    redraw();
732}
733
734void InputManager::add(ifield *i)
735{ ifield *f=m_first;
736  if (i->selectable())
737  {
738    if (!f)
739      m_first=i;
740    else
741    {
742      while (f->next) f=f->next;
743      f->next=i;
744    }
745  }
746}
747
748ifield *InputManager::get(int id)
749{
750  ifield *f;
751  for (f=m_first; f; f=f->next)
752  {
753    ifield *ret=f->find(id);
754    if (ret) return ret;
755  }
756  return NULL;
757}
758
759ifield::ifield()
760{
761    owner = NULL;
762    x = 0;
763    y = 0;
764    next = NULL;
765    id = 0;
766}
767
768ifield::~ifield()
769{
770    ;
771}
772
773/* re-position the control with respect to the "client" area of the window */
774void ifield::set_owner(Jwindow * newowner)
775{
776    if(owner)
777        move(x - owner->x1(), y - owner->y1());
778    owner = newowner;
779    if(owner)
780        move(x + owner->x1(), y + owner->y1());
781}
782
Note: See TracBrowser for help on using the repository browser.