source: abuse/trunk/src/imlib/pmenu.cpp @ 131

Last change on this file since 131 was 131, checked in by Sam Hocevar, 15 years ago
  • Use strdup(foo) everywhere instead of strcpy(malloc(strlen(foo)+1),foo);
File size: 10.0 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 <string.h>
13
14#include "pmenu.hpp"
15
16void pmenu::move(int new_x, int new_y)
17{
18  wm->move_window(bar,new_x,new_y);
19}
20
21pmenu::pmenu(int X, int Y, pmenu_item *first, image *screen)
22{
23  top=first;
24  active=NULL;
25
26  short cx1,cy1,cx2,cy2;
27  screen->get_clip(cx1,cy1,cx2,cy2);
28  if (cx1<X) cx1=X;
29  int w = cx2 - cx1 - Jwindow::left_border() - Jwindow::right_border() + 1;
30  int h = Jwindow::top_border() + Jwindow::bottom_border();
31
32  bar=wm->new_window(X, Y, w, 0, NULL);
33  bar->freeze();  // can't drag this window
34  bar->screen->widget_bar(0,0,w-1,h-1,wm->bright_color(),wm->medium_color(),
35            wm->dark_color());
36
37
38
39  int total=0,tx,tw;
40  pmenu_item *p=top;
41  for (;p;p=p->next) total++;
42
43  tw=w/(total+1);
44  tx=tw/2;
45
46  for (p=top;p;p=p->next,tx+=tw)
47    p->draw_self(bar,itemx(p),1,itemw(p),1,p==active);
48/*  }
49  else
50  {
51    for (p=top;p;p=p->next,tx+=tw)
52      p->draw(bar,itemx(p),1,itemw(p),1,p==active);
53  }*/
54
55}
56
57pmenu_item::pmenu_item(int ID, char const *Name, char const *on_off_flag, int Hotkey, pmenu_item *Next)
58{
59  xp=-1;
60  id=ID;
61  hotkey=Hotkey;
62  on_off=on_off_flag;
63  if (Name)
64    n = strdup(Name);
65  else n=NULL;
66  next=Next;
67  sub=NULL;
68}
69
70pmenu_item::pmenu_item(char const *Name, psub_menu *Sub, pmenu_item *Next, int xpos)
71{
72  xp=xpos;
73  id=0; hotkey=-1;
74  next=Next;
75  on_off=NULL;
76  CONDITION(Name,"Sub menu cannot have a NULL name");
77  n = strdup(Name);
78  sub=Sub;
79}
80
81pmenu_item *pmenu_item::find_id(int search_id)
82{
83  if (id==search_id) return this;
84  else if (sub) return sub->find_id(search_id);
85  else return NULL;
86}
87
88pmenu_item *pmenu_item::find_key(int key)
89{
90  if (key==hotkey && hotkey!=-1) return this;
91  else if (sub) return sub->find_key(key);
92  else return NULL;
93}
94
95pmenu::~pmenu()
96{
97  while (top)
98  {
99    pmenu_item *p=top;
100    top=top->next;
101    delete p;
102  }
103  if (bar) wm->close_window(bar);
104}
105
106psub_menu::~psub_menu()
107{
108  if (win)
109    wm->close_window(win);
110
111  while (first)
112  {
113    pmenu_item *tmp=first;
114    first=first->next;
115    delete tmp;
116  }
117}
118
119pmenu_item *psub_menu::find_id(int search_id)
120{
121  for (pmenu_item *f=first;f;f=f->next)
122  {
123    pmenu_item *ret=f->find_id(search_id);
124    if (ret) return ret;
125  }
126  return NULL;
127}
128
129pmenu_item *psub_menu::find_key(int key)
130{
131  for (pmenu_item *f=first;f;f=f->next)
132  {
133    pmenu_item *ret=f->find_key(key);
134    if (ret) return ret;
135  }
136  return NULL;
137}
138
139
140void psub_menu::hide(Jwindow *parent, int x, int y)
141{
142  int w,h;
143  calc_size(w,h);
144  short cx1,cy1,cx2,cy2;
145  screen->get_clip(cx1,cy1,cx2,cy2);
146  if (w+x>cx2)
147    x=cx2-w;
148
149
150  if (win)
151  {
152    if (active!=-1)
153    {
154      int w,h;
155      calc_size(w,h);
156      item_num(active)->draw(win,x+3,y+3+active*(wm->font()->height()+1),w-6,0,0);
157    }
158    wm->close_window(win);
159    win=NULL;
160  }
161}
162
163void psub_menu::calc_size(int &w, int &h)
164{
165  int tw=wm->font()->width(),th=wm->font()->height();
166  w=h=0;
167  for (pmenu_item *p=first;p;p=p->next)
168  {
169    if (p->name())
170    {
171      int l=strlen(p->name())*tw+8;
172      if (p->on_off) l+=tw*4;
173      if (l>w) w=l;
174    }
175    h++;
176  }
177  h=h*(th+1)+8;
178}
179
180void psub_menu::draw(Jwindow *parent, int x, int y)
181{
182  if (win) wm->close_window(win);
183
184  int w,h,i=0;
185  calc_size(w,h);
186  short cx1,cy1,cx2,cy2;
187  screen->get_clip(cx1,cy1,cx2,cy2);
188  if (parent->x+w+x>cx2)
189    x=cx2-w-parent->x;
190  if (h+y+parent->y>cy2)
191    if (parent->y+parent->h+wm->font()->height()>cy2)
192      y=-h;
193     else y=y-h+wm->font()->height()+5;
194
195
196  win=wm->new_window(parent->x+x,parent->y+y,
197             w - Jwindow::left_border() - Jwindow::right_border(),
198             h - Jwindow::top_border() - Jwindow::bottom_border(),
199                     NULL);
200  win->freeze();
201  win->screen->widget_bar(0,0,w-1,h-1,wm->bright_color(),wm->medium_color(),wm->dark_color());
202
203  int has_flags=0;
204  pmenu_item *p=first;
205  for (;p;p=p->next) if (p->on_off) has_flags=1;
206  x=has_flags ? 3+wm->font()->width() : 3;
207  y=3;
208
209  for (p=first;p;p=p->next,i++,y+=wm->font()->height()+1)
210    p->draw(win,x,y,w-6,0,i==active);
211
212}
213
214void pmenu_item::draw_self(Jwindow *parent, int x, int y, int w, int top, int active)
215{
216  int bx=x;
217  if (on_off) bx=x-wm->font()->width();
218
219  if (!n)
220  {
221    int h=wm->font()->height();
222    parent->screen->widget_bar(x,y+h/2-1,x+w-1,y+h/2,wm->dark_color(),wm->medium_color(),wm->bright_color());
223  } else
224  {
225    if (active)
226    {
227      if (xp!=-1)
228        parent->screen->xor_bar(bx,y,x+w-1,y+wm->font()->height()+1,wm->dark_color());
229      else
230      {
231    parent->screen->bar(bx,y,x+w-1,y+wm->font()->height()+1,wm->dark_color());
232    wm->font()->put_string(parent->screen,x+1,y+1,n,wm->medium_color());   
233    if (on_off && *on_off) wm->font()->put_string(parent->screen,bx+1,y+1,"*",wm->medium_color());
234      }
235    } else
236    {
237      if (xp!=-1)
238        parent->screen->xor_bar(bx,y,x+w-1,y+wm->font()->height()+1,wm->dark_color());
239      else
240      {
241    parent->screen->bar(bx,y,x+w-1,y+wm->font()->height()+1,wm->medium_color());
242    wm->font()->put_string(parent->screen,x+1,y+1,n,wm->bright_color());
243    if (on_off && *on_off) wm->font()->put_string(parent->screen,bx+1,y+1,"*",wm->bright_color());
244      }
245    }
246  }
247}
248
249void pmenu_item::draw(Jwindow *parent, int x, int y, int w, int top,
250              int active)
251{
252  if (n)
253  {
254    if (active)
255    {
256      draw_self(parent,x,y,w,top,active);
257      if (sub)
258      {
259    if (top)
260          sub->draw(parent,x,y+wm->font()->height()+2);
261    else
262      sub->draw(parent,x+w,y);
263      }
264    }
265    else
266    {
267      if (sub)
268      {
269    if (top)
270          sub->hide(parent,x,y+wm->font()->height()+2);
271    else
272      sub->hide(parent,x+w,y);
273      }
274      draw_self(parent,x,y,w,top,active);
275
276    }
277
278  } else draw_self(parent,x,y,w,top,active);
279}
280
281int pmenu::itemx(pmenu_item *p)
282{
283  if (p->xp!=-1) return p->xp;
284  int w=bar->screen->width();
285
286
287  int total=0,tw,i=0,x=0;
288  for (pmenu_item *pp=top;pp;pp=pp->next,i++)
289  { if (pp==p) x=i;
290    total++;
291  }
292
293
294  tw=w/(total+1);
295  return tw/2+x*tw;
296}
297
298
299void pmenu::draw(image *screen, int top_only)
300{
301
302}
303
304
305int psub_menu::handle_event(Jwindow *parent, int x, int y, event &ev)
306{
307  int w,h;
308  calc_size(w,h);
309  short cx1,cy1,cx2,cy2;
310  screen->get_clip(cx1,cy1,cx2,cy2);
311
312  x=win->x;
313  y=win->y;
314
315  int has_flags=0,dx=3;
316  for (pmenu_item *p=first;p;p=p->next) if (p->on_off) has_flags=1;
317  if (has_flags) dx+=wm->font()->width();
318
319  int th=wm->font()->height();
320  if (ev.mouse_move.x>=x && ev.mouse_move.y>=y && ev.mouse_move.x<x+w && ev.mouse_move.y<y+h)
321  {
322    int new_active=(ev.mouse_move.y-y-3)/(th+1);
323    if (item_num(new_active)==NULL) new_active=-1;
324
325    if (new_active!=active)
326    {
327      if (active!=-1)
328        item_num(active)->draw(win,dx,3+active*(th+1),w-6,0,0);
329      active=new_active;
330      if (active!=-1)
331        item_num(active)->draw(win,dx,3+active*(th+1),w-6,0,1);
332    }
333    if (ev.type==EV_MOUSE_BUTTON)
334    {
335      if (active!=-1)
336        return item_num(active)->handle_event(win,dx,3+active*(th+1),w-6,0,ev);
337      else return 0;
338    } else return 1;
339  } else if (active!=-1)
340    return item_num(active)->handle_event(win,win->x+dx,win->y+3+active*(th+1),w-6,0,ev);
341  else return 0;
342
343
344}
345
346int pmenu_item::handle_event(Jwindow *parent, int x, int y, int w, int top,
347                 event &ev)
348{
349  x+=parent->x;
350  y+=parent->y;
351  if (ev.mouse_move.x>=x && ev.mouse_move.y>=y && ev.mouse_move.x<x+w &&
352      ev.mouse_move.y<y+wm->font()->height()+2)
353  {
354    if (sub) return 1;
355    else
356    {
357      if (ev.type==EV_MOUSE_BUTTON &&n)
358        wm->push_event(new event(id,(char *)this));
359      return 1;
360    }
361  } else if (sub)
362  {
363    if (top)
364      return sub->handle_event(parent,x,y+wm->font()->height()+2,ev);
365    else return sub->handle_event(parent,x+w,y,ev);
366  } else return 0;
367}
368
369pmenu_item *pmenu::inarea(int mx, int my, image *screen)
370{
371  short cx1,cy1,cx2,cy2;
372  screen->get_clip(cx1,cy1,cx2,cy2);
373  mx-=bar->x;
374  my-=bar->y;
375  if (mx<0 || my<0 || mx>=bar->screen->width() || my>=bar->screen->height()) return NULL;
376  else
377  {
378    for (pmenu_item *p=top;p;p=p->next)
379    {
380      if (!p->next) return p;
381      else if (itemx(p->next)>mx) return p;
382    }
383    return NULL;
384  }
385}
386
387int psub_menu::own_event(event &ev)
388{
389  if (win && ev.window==win) return 1; else
390    for (pmenu_item *p=first;p;p=p->next)
391      if (p->own_event(ev))
392        return 1;
393  return 0;
394}
395
396int pmenu_item::own_event(event &ev)
397{
398  if (sub)
399    return sub->own_event(ev);
400  else return 0;
401}
402
403pmenu_item::~pmenu_item()
404{ if (n) free(n); if (sub) delete sub;
405}
406
407int pmenu::handle_event(event &ev, image *screen)
408{
409  if (!active && ev.window!=bar) return 0;
410/*
411    int yes=0;
412    if (ev.window==bar) yes=1;    // event in top bar?
413    else
414    {
415      for (pmenu_item *p=top;p && !yes;p=p->next)  // event in submenu?
416      if (p->own_event(ev)) yes=1;
417    }
418    if (!yes) return 0;        // event is not for us...
419  }*/
420
421  switch (ev.type)
422  {
423    case EV_KEY :
424    {
425      for (pmenu_item *p=top;p;p=p->next)
426      {
427    pmenu_item *r=p->find_key(ev.key);
428    if (r)
429    {
430      wm->push_event(new event(r->id,(char *)r));
431      return 1;
432    }
433      }
434      return 0;
435    } break;
436    case EV_MOUSE_MOVE :
437    {
438      pmenu_item *new_selection=inarea(ev.mouse_move.x,ev.mouse_move.y,screen);
439      if (!new_selection && active &&
440      active->handle_event(bar,itemx(active),1,itemw(active),1,ev))
441    return 1;
442      else if (active!=new_selection)
443      {
444    if (active)
445      active->draw(bar,itemx(active),1,itemw(active),1,0);
446    active=new_selection;
447    if (active)
448      active->draw(bar,itemx(active),1,itemw(active),1,1);
449      }
450      if (active) return 1;
451      else return 0;
452    } break;
453    case EV_MOUSE_BUTTON :
454    {
455      if (active)
456      {
457        if (active->handle_event(bar,itemx(active),1,itemw(active),1,ev))
458    {
459      active->draw(bar,itemx(active),1,itemw(active),1,0);
460      active=NULL;
461      return 1;
462    } else return 0;
463      }
464      else return 0;
465    } break;
466  }
467  return 0;
468}
469
470
Note: See TracBrowser for help on using the repository browser.