1 /*
2  * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3  *           (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com>
4  *
5  * This file is part of lsp-plugins
6  * Created on: 18 сент. 2017 г.
7  *
8  * lsp-plugins is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * any later version.
12  *
13  * lsp-plugins is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with lsp-plugins. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef UI_TK_WIDGETS_LSPMENU_H_
23 #define UI_TK_WIDGETS_LSPMENU_H_
24 
25 namespace lsp
26 {
27     namespace tk
28     {
29         class LSPMenuItem;
30 
31         class LSPMenu: public LSPWidgetContainer
32         {
33             public:
34                 static const w_class_t    metadata;
35 
36             protected:
37                 class MenuWindow: public LSPWindow
38                 {
39                     protected:
40                         LSPMenu *pMenu;
41 
42                     protected:
43                         virtual LSPWidget    *find_widget(ssize_t x, ssize_t y);
44 
45                         LSPMenu             *get_handler(ws_event_t *e);
46 
47                     public:
48                         MenuWindow(LSPDisplay *dpy, LSPMenu *menu, size_t screen);
49                         virtual ~MenuWindow();
50 
51                     public:
52                         virtual void render(ISurface *s, bool force);
53 
54                         virtual void size_request(size_request_t *r);
55 
56                         virtual status_t on_mouse_down(const ws_event_t *e);
57 
58                         virtual status_t on_mouse_up(const ws_event_t *e);
59 
60                         virtual status_t on_mouse_scroll(const ws_event_t *e);
61 
62                         virtual status_t on_mouse_move(const ws_event_t *e);
63                 };
64 
65                 enum selection_t
66                 {
67                     SEL_NONE            = -3,
68                     SEL_TOP_SCROLL      = -2,
69                     SEL_BOTTOM_SCROLL   = -1
70                 };
71 
72             protected:
73                 cvector<LSPMenuItem>    vItems;
74                 LSPFont                 sFont;
75                 MenuWindow             *pWindow;
76                 LSPMenu                *pParentMenu;
77                 LSPMenu                *pActiveMenu;
78                 LSPTimer                sScroll;
79                 ssize_t                 nPopupLeft;
80                 ssize_t                 nPopupTop;
81                 ssize_t                 nSelected;
82                 ssize_t                 nScroll;
83                 ssize_t                 nScrollMax;
84                 size_t                  nMBState;
85                 LSPColor                sSelColor;
86                 LSPColor                sBorderColor;
87                 size_t                  nBorder;
88                 size_t                  nSpacing;
89 
90             protected:
91                 ssize_t                 find_item(ssize_t x, ssize_t y, ssize_t *ry);
92                 static status_t         timer_handler(timestamp_t time, void *arg);
93                 void                    update_scroll();
94                 void                    selection_changed(ssize_t sel, ssize_t ry);
95                 LSPMenu                *check_inside_submenu(ws_event_t *e);
96 
97                 void                    do_destroy();
98 
99             public:
100                 explicit LSPMenu(LSPDisplay *dpy);
101                 virtual ~LSPMenu();
102 
103                 virtual status_t init();
104                 virtual void destroy();
105 
106             public:
popup_left()107                 inline ssize_t  popup_left() const  { return nPopupLeft;    }
popup_top()108                 inline ssize_t  popup_top() const   { return nPopupTop;     }
border()109                 inline size_t   border() const      { return nBorder; };
spacing()110                 inline size_t   spacing() const     { return nSpacing; };
scroll()111                 inline ssize_t  scroll() const      { return nScroll; };
sel_color()112                 LSPColor       *sel_color()         { return &sSelColor; }
border_color()113                 LSPColor       *border_color()      { return &sBorderColor; }
114 
115             public:
set_popup_left(ssize_t value)116                 inline void         set_popup_left(ssize_t value) { nPopupLeft = value; }
set_popup_top(ssize_t value)117                 inline void         set_popup_top(ssize_t value) { nPopupTop = value; }
118                 void                set_border(size_t value);
119                 void                set_spacing(size_t value);
120                 void                set_scroll(ssize_t scroll);
121 
122                 virtual status_t    add(LSPWidget *child);
123 
124                 virtual status_t    remove(LSPWidget *child);
125 
126                 virtual void        draw(ISurface *s);
127 
128                 virtual void        query_resize();
129 
130                 virtual void        size_request(size_request_t *r);
131 
132                 virtual void        realize(const realize_t *r);
133 
134                 virtual bool        hide();
135 
136                 virtual bool        show();
137 
138                 virtual bool        show(size_t screen);
139 
140                 virtual bool        show(size_t screen, ssize_t left, ssize_t top);
141 
142                 virtual bool        show(LSPWidget *w, size_t screen, ssize_t left, ssize_t top);
143 
144                 virtual bool        show(LSPWidget *w);
145 
146                 virtual bool        show(LSPWidget *w, ssize_t left, ssize_t top);
147 
148                 virtual bool        show(LSPWidget *w, const ws_event_t *ev);
149 
150                 virtual status_t    on_mouse_down(const ws_event_t *e);
151 
152                 virtual status_t    on_mouse_up(const ws_event_t *e);
153 
154                 virtual status_t    on_mouse_move(const ws_event_t *e);
155 
156                 virtual status_t    on_mouse_scroll(const ws_event_t *e);
157         };
158 
159     } /* namespace tk */
160 } /* namespace lsp */
161 
162 #endif /* UI_TK_WIDGETS_LSPMENU_H_ */
163