]> git.mxchange.org Git - flightgear.git/blob - PUI/puPopupMenu.cxx
52ffbd9ac3d5815c0d5d6dabc4da32486492b009
[flightgear.git] / PUI / puPopupMenu.cxx
1
2 #include "puLocal.h"
3
4 #define PUMENU_BUTTON_HEIGHT       25
5 #define PUMENU_BUTTON_EXTRA_WIDTH  25
6
7 puObject *puPopupMenu::add_item ( char *str, puCallback cb )
8 {
9   int w, h ;
10   getSize ( &w, &h ) ;
11   puOneShot *b = new puOneShot ( 0, h, str ) ;
12   b->setStyle        ( PUSTYLE_PLAIN ) ;
13   b->setColourScheme ( colour[PUCOL_FOREGROUND][0],
14                        colour[PUCOL_FOREGROUND][1],
15                        colour[PUCOL_FOREGROUND][2],
16                        colour[PUCOL_FOREGROUND][3] ) ;
17   b->setCallback     ( cb ) ;
18   recalc_bbox () ;
19   return b ;
20 }
21
22 void puPopupMenu::close ( void )
23 {
24   puPopup::close () ;
25
26   int widest = 0 ;
27   puObject *ob = dlist ;
28
29   for ( ob = dlist ; ob != NULL ; ob = ob -> next )
30   {
31     int w, h ;
32
33     ob -> getSize ( &w, &h ) ;
34
35     if ( w > widest ) widest = w ;
36   }
37
38   for ( ob = dlist ; ob != NULL ; ob = ob -> next )
39     ob -> setSize ( widest, PUMENU_BUTTON_HEIGHT ) ;
40
41   recalc_bbox () ;
42 }
43
44
45 int puPopupMenu::checkKey ( int key, int updown )
46 {
47   if ( dlist == NULL || ! isVisible () || ! isActive () )
48     return FALSE ;
49
50   if ( updown == PU_DOWN )
51   {
52     hide () ;
53
54     /* Turn everything off ready for next time. */
55
56     for ( puObject *bo = dlist ; bo != NULL ; bo = bo->next )
57       bo -> clrValue () ;
58   }
59
60   puObject *bo ;
61
62   /*
63     We have to walk the list backwards to ensure that
64     the click order is the same as the DRAW order.
65   */
66
67   for ( bo = dlist ; bo->next != NULL ; bo = bo->next )
68     /* Find the last object in our list. */ ;
69
70   for ( ; bo != NULL ; bo = bo->prev )
71     if ( bo -> checkKey ( key, updown ) )
72       return TRUE ;
73
74   return FALSE ;
75 }
76
77
78 int puPopupMenu::checkHit ( int button, int updown, int x, int y )
79 {
80   if ( dlist == NULL || ! isVisible () || ! isActive () )
81     return FALSE ;
82
83   /* Must test 'isHit' before making the menu invisible! */
84
85   int hit = isHit ( x, y ) ;
86
87   if ( updown == active_mouse_edge || active_mouse_edge == PU_UP_AND_DOWN )
88   {
89     hide () ;
90
91     /* Turn everything off ready for next time. */
92
93     for ( puObject *bo = dlist ; bo != NULL ; bo = bo->next )
94       bo -> clrValue () ;
95   }
96
97   if ( ! hit )
98     return FALSE ;
99
100   /*
101     This might be a bit redundant - but it's too hard to keep
102     track of changing abox sizes when daughter objects are
103     changing sizes.
104   */
105
106   recalc_bbox () ;
107
108   puObject *bo ;
109
110   x -= abox.min[0] ;
111   y -= abox.min[1] ;
112
113   /*
114     We have to walk the list backwards to ensure that
115     the click order is the same as the DRAW order.
116   */
117
118   for ( bo = dlist ; bo->next != NULL ; bo = bo->next )
119     /* Find the last object in our list. */ ;
120
121   for ( ; bo != NULL ; bo = bo->prev )
122     if ( bo -> checkHit ( button, updown, x, y ) )
123       return TRUE ;
124
125   return FALSE ;
126 }
127