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: 12 июн. 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_LSPSLOT_H_
23 #define UI_TK_LSPSLOT_H_
24 
25 namespace lsp
26 {
27     namespace tk
28     {
29         class LSPWidget;
30 
31         /** Event handler type
32          *
33          * @param sender the widget that initiated an event
34          * @param ptr additional pointer passed as an argument to the bind()
35          * @param data data structure to process (handle)
36          * @return status of operation
37          */
38         typedef status_t (* ui_event_handler_t)(LSPWidget *sender, void *ptr, void *data);
39 
40         class LSPSlot
41         {
42             protected:
43                 enum bind_flags_t
44                 {
45                     BIND_DFL            = 0,
46                     BIND_ENABLED        = 1 << 0,
47                     BIND_INTERCEPT      = 1 << 1
48                 };
49 
50                 typedef struct handler_item_t
51                 {
52                     ui_handler_id_t     nID;        // Identifier of handler
53                     size_t              nFlags;     // Additional flags
54                     ui_event_handler_t  pHandler;   // Handler
55                     void               *pPtr;       // Additional argument to pass
56                     handler_item_t     *pNext;      // Pointer to the next item
57                 } handler_item_t;
58 
59             protected:
60                 handler_item_t         *pRoot;      // Pointer to the first item
61                 ui_handler_id_t         nID;        // ID generator
62 
63             protected:
64                 inline handler_item_t  *find_item(ui_handler_id_t id);
65                 ui_handler_id_t bind(ui_event_handler_t handler, bool intercept, void *arg, bool enabled);
66                 size_t disable_all(bool handler, bool interceptor);
67                 size_t enable_all(bool handler, bool interceptor);
68 
69             public:
70                 explicit LSPSlot();
71                 ~LSPSlot();
72 
73             public:
74                 /** Bind slot
75                  *
76                  * @param handler event handler routine
77                  * @param arg argument
78                  * @param enabled binding is enabled
79                  * @return identifier of handler or negative status code
80                  */
81                 ui_handler_id_t bind(ui_event_handler_t handler, void *arg = NULL, bool enabled = true);
82 
83                 /** Intercept slot
84                  *
85                  * @param handler interceptor handler routine
86                  * @param arg argument
87                  * @param enabled interceptor is enabled
88                  * @return identifier of interceptor or negative status code
89                  */
90                 ui_handler_id_t intercept(ui_event_handler_t handler, void *arg = NULL, bool enabled = true);
91 
92                 /** Unbind handler or interceptor by identifier
93                  *
94                  * @param id handler identifier
95                  * @return status of operation
96                  */
97                 status_t unbind(ui_handler_id_t id);
98 
99                 /** Unbind handler or interceptor by contents. Removes first occured binding
100                  *
101                  * @param handler event handler routine
102                  * @param arg argument
103                  * @return identifier of removed handler on success, negative status code on error
104                  */
105                 ui_handler_id_t unbind(ui_event_handler_t handler, void *arg = NULL);
106 
107                 /** Unbind all handlers and interceptors for the slot
108                  *
109                  * @return number of handlers removed from bindings
110                  */
111                 size_t unbind_all();
112 
113                 /** Disable event handler or interceptor for the slot
114                  *
115                  * @param id handler identifier
116                  * @return status of operation
117                  */
118                 status_t disable(ui_handler_id_t id);
119 
120                 /** Disable all event handlers or interceptors
121                  *
122                  * @return number of non-disabled handlers that were disabled
123                  */
124                 size_t disable_all();
125 
126                 /** Disable all event handlers only
127                  *
128                  * @return number of non-disabled handlers that were disabled
129                  */
130                 size_t disable_all_bindings();
131 
132                 /** Disable all event interceptors only
133                  *
134                  * @return number of non-disabled handlers that were disabled
135                  */
136                 size_t disable_all_interceptors();
137 
138                 /** Enable event handler in the slot
139                  *
140                  * @param id handler identifier
141                  * @return status of operation
142                  */
143                 status_t enable(ui_handler_id_t id);
144 
145                 /** Enable all event handlers and interceptors
146                  *
147                  * @return number of non-enabled handlers that were disabled
148                  */
149                 size_t enable_all();
150 
151                 /** Enable all interceptors
152                  *
153                  * @return number of non-enabled interceptors that were enabled
154                  */
155                 size_t enable_all_interceptors();
156 
157                 /** Enable all bindings
158                  *
159                  * @return number of non-enabled handlers that were enabled
160                  */
161                 size_t enable_all_bindings();
162 
163                 /** Execute slot handlers
164                  *
165                  * @param sender the object that initiated event
166                  * @param data data to process
167                  * @return status of operation
168                  */
169                 status_t execute(LSPWidget *sender, void *data);
170         };
171     }
172 } /* namespace lsp */
173 
174 #endif /* UI_TK_LSPSLOT_H_ */
175