File: pmenu.c

package info (click to toggle)
abuse 2.00-12
  • links: PTS
  • area: main
  • in suites: slink
  • size: 12,708 kB
  • ctags: 15,389
  • sloc: ansic: 115,852; cpp: 6,792; lisp: 2,066; sh: 1,734; makefile: 1,601; asm: 264
file content (461 lines) | stat: -rw-r--r-- 10,297 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include "pmenu.hpp"

void pmenu::move(int new_x, int new_y)
{
  wm->move_window(bar,new_x,new_y);
}

pmenu::pmenu(int X, int Y, pmenu_item *first, image *screen, window_manager *wmanager)
{ 
  top=first; 
  active=NULL; 
  wm=wmanager;

  short cx1,cy1,cx2,cy2;
  screen->get_clip(cx1,cy1,cx2,cy2);
  if (cx1<X) cx1=X;
  int w=cx2-cx1+1;
  int h=wm->font()->height()+4;


  bar=wm->new_window(X,Y,w-WINDOW_FRAME_LEFT-WINDOW_FRAME_RIGHT,
		     h-WINDOW_FRAME_TOP-WINDOW_FRAME_BOTTOM,NULL);
  bar->set_moveability(0);  // can't drag this window
  bar->screen->wiget_bar(0,0,w-1,h-1,wm->bright_color(),wm->medium_color(),
		    wm->dark_color());



  int total=0,tx,tw;
  pmenu_item *p=top;
  for (;p;p=p->next) total++;

  tw=w/(total+1);
  tx=tw/2;

  for (p=top;p;p=p->next,tx+=tw) 
    p->draw_self(bar,itemx(p,wm),1,itemw(p,wm),1,wm,p==active);      
/*  }
  else
  {
    for (p=top;p;p=p->next,tx+=tw) 
      p->draw(bar,itemx(p,wm),1,itemw(p,wm),1,wm,p==active);
  }*/
  
}

pmenu_item::pmenu_item(int ID, char *Name, char *on_off_flag, int Hotkey, pmenu_item *Next)
{ 
  xp=-1;
  id=ID; 
  hotkey=Hotkey;
  on_off=on_off_flag;
  if (Name)
    n=strcpy((char *)jmalloc(strlen(Name)+1,"pmenu_item::name"),Name); 
  else n=NULL;
  next=Next;
  sub=NULL;
}

pmenu_item::pmenu_item(char *Name, psub_menu *Sub, pmenu_item *Next, int xpos)
{
  xp=xpos;
  id=0; hotkey=-1;
  next=Next;
  on_off=NULL;
  CONDITION(Name,"Sub menu cannot have a NULL name");
  n=strcpy((char *)jmalloc(strlen(Name)+1,"pmenu_item::name"),Name); 
  sub=Sub;
}

pmenu_item *pmenu_item::find_id(int search_id) 
{ 
  if (id==search_id) return this;
  else if (sub) return sub->find_id(search_id);
  else return NULL;
}

pmenu_item *pmenu_item::find_key(int key)
{
  if (key==hotkey && hotkey!=-1) return this;
  else if (sub) return sub->find_key(key);
  else return NULL;
}

pmenu::~pmenu()
{
  while (top)
  {
    pmenu_item *p=top;
    top=top->next;
    delete p;
  }
  if (bar) wm->close_window(bar);
}

psub_menu::~psub_menu() 
{ 
  if (win)   
    wm->close_window(win);
    
  while (first)
  {
    pmenu_item *tmp=first;
    first=first->next;
    delete tmp;
  }
}

pmenu_item *psub_menu::find_id(int search_id)
{
  for (pmenu_item *f=first;f;f=f->next)
  {
    pmenu_item *ret=f->find_id(search_id);
    if (ret) return ret;
  }
  return NULL;
}

pmenu_item *psub_menu::find_key(int key)
{
  for (pmenu_item *f=first;f;f=f->next)
  {
    pmenu_item *ret=f->find_key(key);
    if (ret) return ret;
  }
  return NULL;
}


void psub_menu::hide(jwindow *parent, int x, int y, window_manager* wm)
{
  int w,h;
  calc_size(w,h,wm);
  short cx1,cy1,cx2,cy2;
  screen->get_clip(cx1,cy1,cx2,cy2);
  if (w+x>cx2)
    x=cx2-w;


  if (win)
  {
    if (active!=-1)
    {
      int w,h;
      calc_size(w,h,wm);
      item_num(active)->draw(win,x+3,y+3+active*(wm->font()->height()+1),w-6,0,wm,0);
    }
    wm->close_window(win);
    win=NULL;
  }
}

void psub_menu::calc_size(int &w, int &h, window_manager *wm)
{
  int tw=wm->font()->width(),th=wm->font()->height();
  w=h=0;
  for (pmenu_item *p=first;p;p=p->next)
  {
    if (p->name())
    {
      int l=strlen(p->name())*tw+8;
      if (p->on_off) l+=tw*4;
      if (l>w) w=l;   
    }
    h++;
  }
  h=h*(th+1)+8;
}

void psub_menu::draw(jwindow *parent, int x, int y, window_manager *wmanager)
{
  if (win) wm->close_window(win);
  wm=wmanager;


  int w,h,i=0;
  calc_size(w,h,wm);
  short cx1,cy1,cx2,cy2;
  screen->get_clip(cx1,cy1,cx2,cy2);
  if (parent->x+w+x>cx2)
    x=cx2-w-parent->x;
  if (h+y+parent->y>cy2)
    if (parent->y+parent->h+wm->font()->height()>cy2)
      y=-h;
     else y=y-h+wm->font()->height()+5;
     

  win=wm->new_window(parent->x+x,parent->y+y,
		     w-WINDOW_FRAME_LEFT-WINDOW_FRAME_RIGHT,
		     h-WINDOW_FRAME_TOP-WINDOW_FRAME_BOTTOM,NULL);
  win->set_moveability(0);
  win->screen->wiget_bar(0,0,w-1,h-1,wm->bright_color(),wm->medium_color(),wm->dark_color());

  int has_flags=0;
  pmenu_item *p=first;
  for (;p;p=p->next) if (p->on_off) has_flags=1;
  x=has_flags ? 3+wm->font()->width() : 3;
  y=3;
 
  for (p=first;p;p=p->next,i++,y+=wm->font()->height()+1)
    p->draw(win,x,y,w-6,0,wm,i==active);

}

void pmenu_item::draw_self(jwindow *parent, int x, int y, int w, int top, window_manager *wm, int active)
{
  int bx=x;
  if (on_off) bx=x-wm->font()->width();

  if (!n)
  {
    int h=wm->font()->height();
    parent->screen->wiget_bar(x,y+h/2-1,x+w-1,y+h/2,wm->dark_color(),wm->medium_color(),wm->bright_color());
  } else
  {
    if (active)
    {
      if (xp!=-1)
        parent->screen->xor_bar(bx,y,x+w-1,y+wm->font()->height()+1,wm->dark_color());
      else
      {
	parent->screen->bar(bx,y,x+w-1,y+wm->font()->height()+1,wm->dark_color());
	wm->font()->put_string(parent->screen,x+1,y+1,n,wm->medium_color());	
	if (on_off && *on_off) wm->font()->put_string(parent->screen,bx+1,y+1,"*",wm->medium_color());
      }
    } else
    {
      if (xp!=-1)
        parent->screen->xor_bar(bx,y,x+w-1,y+wm->font()->height()+1,wm->dark_color());
      else
      {
	parent->screen->bar(bx,y,x+w-1,y+wm->font()->height()+1,wm->medium_color());
	wm->font()->put_string(parent->screen,x+1,y+1,n,wm->bright_color());
	if (on_off && *on_off) wm->font()->put_string(parent->screen,bx+1,y+1,"*",wm->bright_color());
      }
    }
  }
}

void pmenu_item::draw(jwindow *parent, int x, int y, int w, int top,
		      window_manager *wm, int active)
{  
  if (n)
  {
    if (active)
    {      
      draw_self(parent,x,y,w,top,wm,active);
      if (sub)
      {
	if (top)
          sub->draw(parent,x,y+wm->font()->height()+2,wm);
	else
	  sub->draw(parent,x+w,y,wm);
      }
    }
    else
    {
      if (sub)
      {
	if (top)
          sub->hide(parent,x,y+wm->font()->height()+2,wm);
	else
	  sub->hide(parent,x+w,y,wm);
      }
      draw_self(parent,x,y,w,top,wm,active);

    }

  } else draw_self(parent,x,y,w,top,wm,active);
}

int pmenu::itemx(pmenu_item *p, window_manager *wm)
{
  if (p->xp!=-1) return p->xp;
  int w=bar->screen->width();
  

  int total=0,tx,tw,i=0,x;
  for (pmenu_item *pp=top;pp;pp=pp->next,i++) 
  { if (pp==p) x=i;
    total++;
  }

  
  tw=w/(total+1); 
  return tw/2+x*tw;
}


void pmenu::draw(image *screen, window_manager *wm, int top_only)
{

}


int psub_menu::handle_event(jwindow *parent, int x, int y, window_manager *wm, event &ev)
{
  int w,h;
  calc_size(w,h,wm);
  short cx1,cy1,cx2,cy2;
  screen->get_clip(cx1,cy1,cx2,cy2);

  x=win->x;
  y=win->y;

  int has_flags=0,dx=3;
  for (pmenu_item *p=first;p;p=p->next) if (p->on_off) has_flags=1;
  if (has_flags) dx+=wm->font()->width();

  int th=wm->font()->height();
  if (ev.mouse_move.x>=x && ev.mouse_move.y>=y && ev.mouse_move.x<x+w && ev.mouse_move.y<y+h)
  {
    int new_active=(ev.mouse_move.y-y-3)/(th+1);
    if (item_num(new_active)==NULL) new_active=-1;

    if (new_active!=active)
    {
      if (active!=-1)
        item_num(active)->draw(win,dx,3+active*(th+1),w-6,0,wm,0);
      active=new_active;
      if (active!=-1)
        item_num(active)->draw(win,dx,3+active*(th+1),w-6,0,wm,1);
    }
    if (ev.type==EV_MOUSE_BUTTON)
    {
      if (active!=-1)
        return item_num(active)->handle_event(win,dx,3+active*(th+1),w-6,0,wm,ev);
      else return 0;
    } else return 1;
  } else if (active!=-1)
    return item_num(active)->handle_event(win,win->x+dx,win->y+3+active*(th+1),w-6,0,wm,ev);
  else return 0;


}

int pmenu_item::handle_event(jwindow *parent, int x, int y, int w, int top, 
			     window_manager *wm, event &ev)
{
  x+=parent->x;
  y+=parent->y;
  if (ev.mouse_move.x>=x && ev.mouse_move.y>=y && ev.mouse_move.x<x+w &&
      ev.mouse_move.y<y+wm->font()->height()+2)
  {
    if (sub) return 1;
    else 
    {
      if (ev.type==EV_MOUSE_BUTTON &&n)
        wm->push_event(new event(id,(char *)this));
      return 1;
    }    
  } else if (sub)
  { 
    if (top)
      return sub->handle_event(parent,x,y+wm->font()->height()+2,wm,ev);
    else return sub->handle_event(parent,x+w,y,wm,ev);
  } else return 0;
}

pmenu_item *pmenu::inarea(int mx, int my, image *screen, window_manager *wm) 
{ 
  short cx1,cy1,cx2,cy2;
  screen->get_clip(cx1,cy1,cx2,cy2);  
  mx-=bar->x;
  my-=bar->y;
  if (mx<0 || my<0 || mx>=bar->screen->width() || my>=bar->screen->height()) return NULL;
  else 
  {
    for (pmenu_item *p=top;p;p=p->next)
    {
      if (!p->next) return p;
      else if (itemx(p->next,wm)>mx) return p;
    }
    return NULL;
  }
}

int psub_menu::own_event(event &ev) 
{ 
  if (win && ev.window==win) return 1; else
    for (pmenu_item *p=first;p;p=p->next)
      if (p->own_event(ev)) 
        return 1; 
  return 0; 
}

int pmenu_item::own_event(event &ev) 
{ 
  if (sub) 
    return sub->own_event(ev); 
  else return 0; 
}

pmenu_item::~pmenu_item() 
{ if (n) jfree(n); if (sub) delete sub; 
}

int pmenu::handle_event(event &ev, image *screen, window_manager *wm)
{
  if (!active && ev.window!=bar) return 0;
/* 
    int yes=0;
    if (ev.window==bar) yes=1;    // event in top bar?
    else
    {
      for (pmenu_item *p=top;p && !yes;p=p->next)  // event in submenu?
      if (p->own_event(ev)) yes=1;
    }
    if (!yes) return 0;        // event is not for us...
  }*/

  switch (ev.type)
  {
    case EV_KEY :
    {
      for (pmenu_item *p=top;p;p=p->next)
      { 
	pmenu_item *r=p->find_key(ev.key);
	if (r)
	{ 
	  wm->push_event(new event(r->id,(char *)r));
	  return 1;
	}
      }
      return 0;
    } break;
    case EV_MOUSE_MOVE :
    {
      pmenu_item *new_selection=inarea(ev.mouse_move.x,ev.mouse_move.y,screen,wm);
      if (!new_selection && active && 
	  active->handle_event(bar,itemx(active,wm),1,itemw(active,wm),1,wm,ev))
	return 1;
      else if (active!=new_selection)
      {
	if (active)
	  active->draw(bar,itemx(active,wm),1,itemw(active,wm),1,wm,0);
	active=new_selection;
	if (active)
	  active->draw(bar,itemx(active,wm),1,itemw(active,wm),1,wm,1);
      }
      if (active) return 1;
      else return 0;
    } break;
    case EV_MOUSE_BUTTON :
    {
      if (active) 
      {
        if (active->handle_event(bar,itemx(active,wm),1,itemw(active,wm),1,wm,ev))
	{
	  active->draw(bar,itemx(active,wm),1,itemw(active,wm),1,wm,0);
	  active=NULL;
	  return 1;
	} else return 0;
      }
      else return 0;
    } break;
  }
  return 0;
}