]> git.mxchange.org Git - flightgear.git/blob - Lib/PUI/puPopupMenu.cxx
Merge FG_Lib as subdirectory
[flightgear.git] / Lib / PUI / puPopupMenu.cxx
1 #include "puLocal.h"
2
3 #define PUMENU_BUTTON_HEIGHT       25
4 #define PUMENU_BUTTON_EXTRA_WIDTH  25
5
6 puObject *puPopupMenu::add_item ( char *str, puCallback cb )
7 {
8   int w, h ;
9   getSize ( &w, &h ) ;
10   puOneShot *b = new puOneShot ( 0, h, str ) ;
11   b->setStyle        ( PUSTYLE_PLAIN ) ;
12   b->setColourScheme ( colour[PUCOL_FOREGROUND][0],
13                        colour[PUCOL_FOREGROUND][1],
14                        colour[PUCOL_FOREGROUND][2],
15                        colour[PUCOL_FOREGROUND][3] ) ;
16   b->setCallback     ( cb ) ;
17   recalc_bbox () ;
18   return b ;
19 }
20
21 void puPopupMenu::close ( void )
22 {
23   puPopup::close () ;
24
25   int widest = 0 ;
26   puObject *ob = dlist ;
27
28   /*
29    * June 17th, 1998, Shammi
30    * There seems to be some mismatch with the
31    * #define pumenusize and the actual size
32    * There seems to be some overlap resulting
33    * in more than one option being highlighted.
34    * By setting the size to the actual values,
35    * the overlap area seems to be less now.
36    */
37
38   int w, h ;
39
40   for ( ob = dlist ; ob != NULL ; ob = ob -> next )
41   {
42     ob -> getSize ( &w, &h ) ;
43
44     if ( w > widest ) widest = w ;
45   }
46
47   for ( ob = dlist ; ob != NULL ; ob = ob -> next )
48   {
49     ob -> getSize ( &w, &h ) ;
50     ob -> setSize ( widest, h ) ;
51   }
52
53   recalc_bbox () ;
54 }
55
56
57 int puPopupMenu::checkKey ( int key, int updown )
58 {
59   if ( dlist == NULL || ! isVisible () || ! isActive () )
60     return FALSE ;
61
62   if ( updown == PU_DOWN )
63   {
64     hide () ;
65
66     /* Turn everything off ready for next time. */
67
68     for ( puObject *bo = dlist ; bo != NULL ; bo = bo->next )
69       bo -> clrValue () ;
70   }
71
72   puObject *bo ;
73
74   /*
75     We have to walk the list backwards to ensure that
76     the click order is the same as the DRAW order.
77   */
78
79   for ( bo = dlist ; bo->next != NULL ; bo = bo->next )
80     /* Find the last object in our list. */ ;
81
82   for ( ; bo != NULL ; bo = bo->prev )
83     if ( bo -> checkKey ( key, updown ) )
84       return TRUE ;
85
86   return FALSE ;
87 }
88
89
90 int puPopupMenu::checkHit ( int button, int updown, int x, int y )
91 {
92   if ( dlist == NULL || ! isVisible () || ! isActive () )
93     return FALSE ;
94
95   /* Must test 'isHit' before making the menu invisible! */
96
97   int hit = isHit ( x, y ) ;
98
99   /*
100    * June 17th, 1998, Shammi :
101    * There seemed to be a miscalculation with the menus initially
102    * Therefore I moved the recalculation stuff before the clearing.
103    */
104
105   /*
106     This might be a bit redundant - but it's too hard to keep
107     track of changing abox sizes when daughter objects are
108     changing sizes.
109   */
110
111   recalc_bbox();
112   x -= abox.min[0] ;
113   y -= abox.min[1] ;
114
115   /*
116    * June 17th, 1998, Shammi :
117    * Also clear the menu when the dragging the mouse and not hit.
118    */
119
120   if (   updown == active_mouse_edge || active_mouse_edge == PU_UP_AND_DOWN ||
121        ( updown == PU_DRAG && !hit ) )
122   {
123
124     /* June 17th, 1998, Shammi :
125      * Do not hide the menu if mouse is dragged out
126      */
127
128     if ( updown != PU_DRAG )
129       hide () ;
130
131     /* Turn everything off ready for next time. */
132
133     /* June 17th, 1998, Shammi:
134      * Make sure we check for a hit, if the mouse is moved
135      * out of the menu.
136      */
137
138     for ( puObject *bo = dlist ; bo != NULL ; bo = bo->next )
139     {
140       if ( ! hit )
141         bo -> checkHit ( button, updown, x , y ) ;
142
143       bo -> clrValue () ;
144     }
145   }
146
147   if ( ! hit )
148     return FALSE ;
149
150   puObject *bo ;
151   
152   /*
153     We have to walk the list backwards to ensure that
154     the click order is the same as the DRAW order.
155   */
156
157   /* June 17th, 1998, Shammi :
158    * If the mouse is dragged and the menuItem is not hit, 
159    * clear it
160    */
161
162   for ( bo = dlist ; bo->next != NULL ; bo = bo->next )
163     if ( updown == PU_DRAG && ! bo -> checkHit ( button, updown, x, y ) )
164       bo -> clrValue () ;
165
166     /* Find the last object in our list. */ ;
167
168   for ( ; bo != NULL ; bo = bo->prev )
169     if ( bo -> checkHit ( button, updown, x, y ) )
170       return TRUE ;
171
172   return FALSE ;
173 }
174
175