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: 07 мая 2019 г.
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_LSPLOADFILE_H_
23 #define UI_TK_WIDGETS_LSPLOADFILE_H_
24 
25 namespace lsp
26 {
27     namespace tk
28     {
29         enum load_file_state_t
30         {
31             LFS_SELECT,
32             LFS_LOADING,
33             LFS_LOADED,
34             LFS_ERROR
35         };
36 
37         class LSPLoadFile: public LSPWidget
38         {
39             public:
40                 static const w_class_t    metadata;
41 
42                 enum _load_file_state_t
43                 {
44                     LFS_TOTAL       = LFS_ERROR + 1,
45 
46                     S_PRESSED       = 1 << 0
47                 };
48 
49             protected:
50                 typedef struct state_t
51                 {
52                     LSPColor       *pColor;
53                     LSPString       sText;
54                 } state_t;
55 
56             protected:
57                 class LoadFileSink: public LSPUrlSink
58                 {
59                     protected:
60                         LSPLoadFile         *pWidget;
61 
62                     public:
63                         explicit LoadFileSink(LSPLoadFile *w);
64                         virtual ~LoadFileSink();
65 
66                         void unbind();
67                         virtual status_t    commit_url(const LSPString *url);
68                 };
69 
70             protected:
71                 load_file_state_t   nState;
72                 state_t             vStates[LFS_TOTAL];
73                 float               fProgress;
74                 size_t              nButtons;
75                 size_t              nBtnState;
76                 ssize_t             nSize;
77                 LoadFileSink       *pSink;
78                 LSPFont             sFont;
79                 LSPFileDialog       sDialog;
80                 ISurface           *pDisk;
81                 LSPString           sPath;
82 
83             protected:
84                 ISurface           *render_disk(ISurface *s, ssize_t w, const Color &c, const Color &bg);
85                 static status_t     slot_on_activate(LSPWidget *sender, void *ptr, void *data);
86                 static status_t     slot_on_submit(LSPWidget *sender, void *ptr, void *data);
87                 static status_t     slot_on_close(LSPWidget *sender, void *ptr, void *data);
88                 static status_t     slot_on_file_submit(LSPWidget *sender, void *ptr, void *data);
89                 static status_t     slot_on_dialog_close(LSPWidget *sender, void *ptr, void *data);
90 
91             public:
92                 explicit LSPLoadFile(LSPDisplay *dpy);
93                 virtual ~LSPLoadFile();
94 
95                 virtual status_t init();
96                 virtual void destroy();
97 
98             public:
state()99                 inline load_file_state_t    state() const { return nState; }
100                 LSPColor                   *state_color(size_t i);
101                 const char                 *state_text(size_t i) const;
102                 status_t                    get_state_text(size_t i, LSPString *dst);
progress()103                 inline float                progress() const { return fProgress; };
104                 const char                 *file_name() const;
105                 status_t                    get_file_name(LSPString *dst);
font()106                 inline LSPFont             *font() { return &sFont; }
filter()107                 inline LSPFileFilter       *filter() { return sDialog.filter(); }
get_path(LSPString * dst)108                 inline status_t             get_path(LSPString *dst) const { return (dst->set(&sPath)) ? STATUS_OK : STATUS_NO_MEM; }
get_path()109                 inline const char          *get_path() const { return sPath.get_native(); }
size()110                 inline ssize_t              size() const { return nSize; }
111 
112             public:
113                 status_t    set_state(load_file_state_t state);
114                 status_t    set_state_text(size_t i, const char *s);
115                 status_t    set_state_text(size_t i, const LSPString *s);
116                 status_t    set_progress(float value);
117                 status_t    set_path(const LSPString *path);
118                 status_t    set_path(const char *path);
119                 void        set_size(ssize_t size);
120 
121             public:
122                 virtual void draw(ISurface *s);
123                 virtual void size_request(size_request_t *r);
124                 virtual status_t on_mouse_down(const ws_event_t *e);
125                 virtual status_t on_mouse_up(const ws_event_t *e);
126                 virtual status_t on_mouse_move(const ws_event_t *e);
127                 virtual status_t on_activate();
128                 virtual status_t on_submit();
129                 virtual status_t on_close();
130                 virtual status_t on_drag_request(const ws_event_t *e, const char * const *ctype);
131         };
132 
133     } /* namespace tk */
134 } /* namespace lsp */
135 
136 #endif /* UI_TK_WIDGETS_LSPLOADFILE_H_ */
137