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: 31 июл. 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_LSPCOMBOBOX_H_
23 #define UI_TK_LSPCOMBOBOX_H_
24 
25 namespace lsp
26 {
27     namespace tk
28     {
29         class LSPComboBox: public LSPWidget
30         {
31             public:
32                 static const w_class_t    metadata;
33 
34             protected:
35                 class LSPComboList: public LSPListBox
36                 {
37                     protected:
38                         LSPComboBox *pWidget;
39 
40                     protected:
41                         virtual void            on_selection_change();
42 
43                         virtual void            on_item_change(ssize_t index, LSPItem *item);
44 
45                         virtual void            on_item_add(size_t index);
46 
47                         virtual void            on_item_remove(size_t index);
48 
49                         virtual void            on_item_swap(size_t idx1, size_t idx2);
50 
51                         virtual void            on_item_clear();
52 
53                     public:
54                         explicit LSPComboList(LSPDisplay *dpy, LSPComboBox *widget);
55                         virtual ~LSPComboList();
56                 };
57 
58                 class LSPComboPopup: public LSPWindow
59                 {
60                     protected:
61                         LSPComboBox *pWidget;
62 
63                     protected:
64                         virtual status_t    handle_event(const ws_event_t *e);
65 
66                     public:
67                         explicit LSPComboPopup(LSPDisplay *dpy, LSPComboBox *widget, ssize_t screen = -1);
68                         virtual ~LSPComboPopup();
69 
70 //                    public:
71 //                        virtual void size_request(size_request_t *r);
72                 };
73 
74                 enum flags_t
75                 {
76                     F_OPENED        = 1 << 0,
77                     F_CIRCULAR      = 1 << 1
78                 };
79 
80             protected:
81                 size_t              nCBFlags;
82                 ssize_t             nMinWidth;
83                 ssize_t             nMinHeight;
84                 size_t              nMFlags;
85 
86                 LSPComboList        sListBox;
87                 LSPWindow          *pPopup;
88                 LSPFont             sFont;
89 
90             protected:
91                 void        do_destroy();
92                 status_t    on_list_change();
93                 status_t    on_list_submit();
94 
95                 static status_t slot_on_change(LSPWidget *sender, void *ptr, void *data);
96                 static status_t slot_on_submit(LSPWidget *sender, void *ptr, void *data);
97                 static status_t slot_on_list_change(LSPWidget *sender, void *ptr, void *data);
98                 static status_t slot_on_list_submit(LSPWidget *sender, void *ptr, void *data);
99                 static status_t slot_on_list_focus_out(LSPWidget *sender, void *ptr, void *data);
100 
101                 static status_t slot_on_list_show(LSPWidget *sender, void *ptr, void *data);
102                 static status_t slot_on_list_mouse_down(LSPWidget *sender, void *ptr, void *data);
103                 static status_t slot_on_list_key_down(LSPWidget *sender, void *ptr, void *data);
104 
105                 ssize_t     estimate_max_size(ISurface *s);
106 
107             protected:
108                 virtual void            on_selection_change();
109                 virtual void            on_item_change(size_t index, LSPItem *item);
110                 virtual void            on_item_add(size_t index);
111                 virtual void            on_item_remove(size_t index);
112                 virtual void            on_item_swap(size_t idx1, size_t idx2);
113                 virtual void            on_item_clear();
114                 virtual status_t        on_list_focus_out();
115                 virtual status_t        on_list_show();
116 
117 
118                 virtual status_t        on_grab_mouse_down(const ws_event_t *e);
119                 virtual status_t        on_grab_key_down(const ws_event_t *e);
120 
121             public:
122                 explicit LSPComboBox(LSPDisplay *dpy);
123                 virtual ~LSPComboBox();
124 
125                 virtual status_t init();
126 
127                 virtual void destroy();
128 
129             public:
130                 ssize_t             selected() const;
items()131                 inline LSPItemList *items()             { return sListBox.items();  }
min_width()132                 inline ssize_t      min_width() const   { return nMinWidth;  }
min_height()133                 inline ssize_t      min_height() const  { return nMinHeight; }
opened()134                 inline bool         opened() const      { return nCBFlags & F_OPENED;     }
circular()135                 inline bool         circular() const    { return nCBFlags & F_CIRCULAR;   }
136 
color()137                 inline LSPColor    *color()             { return sListBox.color(); }
bg_color()138                 inline LSPColor    *bg_color()          { return sListBox.bg_color(); }
font()139                 inline LSPFont     *font()              { return &sFont; }
140 
141             public:
142                 status_t set_selected(ssize_t value);
143                 void set_min_width(ssize_t value);
144                 void set_min_height(ssize_t value);
145 
146                 status_t set_opened(bool open = true);
147                 inline status_t set_closed(bool closed = true) { return set_opened(!closed); };
148 
149                 void set_circular(bool circular = true);
150 
open()151                 inline status_t open()  { return set_opened(true); }
toggle()152                 inline status_t toggle(){ return set_opened(!(nCBFlags & F_OPENED)); }
close()153                 inline status_t close() { return set_opened(false); }
154 
155             public:
156                 virtual status_t on_mouse_down(const ws_event_t *e);
157 
158                 virtual status_t on_mouse_up(const ws_event_t *e);
159 
160                 virtual status_t on_mouse_scroll(const ws_event_t *e);
161 
162                 virtual status_t on_change();
163 
164                 virtual status_t on_submit();
165 
166                 virtual void size_request(size_request_t *r);
167 
168                 virtual void draw(ISurface *s);
169         };
170 
171     } /* namespace tk */
172 } /* namespace lsp */
173 
174 #endif /* UI_TK_LSPCOMBOBOX_H_ */
175