1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2007-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING.  If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_gtk_manager_h)
27 #define octave_gtk_manager_h 1
28 
29 #include "octave-config.h"
30 
31 #include <map>
32 #include <set>
33 #include <string>
34 
35 #include "Cell.h"
36 #include "graphics-toolkit.h"
37 
38 namespace octave
39 {
40   class gtk_manager
41   {
42   public:
43 
gtk_manager(void)44     gtk_manager (void) { }
45 
~gtk_manager(void)46     ~gtk_manager (void)
47     {
48       unload_all_toolkits ();
49     }
50 
51     graphics_toolkit get_toolkit (void) const;
52 
53     void register_toolkit (const std::string& name);
54 
55     void unregister_toolkit (const std::string& name);
56 
load_toolkit(const graphics_toolkit & tk)57     void load_toolkit (const graphics_toolkit& tk)
58     {
59       loaded_toolkits[tk.get_name ()] = tk;
60     }
61 
unload_toolkit(const std::string & name)62     void unload_toolkit (const std::string& name)
63     {
64       loaded_toolkits.erase (name);
65     }
66 
find_toolkit(const std::string & name)67     graphics_toolkit find_toolkit (const std::string& name) const
68     {
69       auto p = loaded_toolkits.find (name);
70 
71       if (p != loaded_toolkits.end ())
72         return p->second;
73       else
74         return graphics_toolkit ();
75     }
76 
available_toolkits_list(void)77     Cell available_toolkits_list (void) const
78     {
79       Cell m (1, available_toolkits.size ());
80 
81       octave_idx_type i = 0;
82       for (const auto& tkit : available_toolkits)
83         m(i++) = tkit;
84 
85       return m;
86     }
87 
loaded_toolkits_list(void)88     Cell loaded_toolkits_list (void) const
89     {
90       Cell m (1, loaded_toolkits.size ());
91 
92       octave_idx_type i = 0;
93       for (const auto& nm_tkit_p : loaded_toolkits)
94         m(i++) = nm_tkit_p.first;
95 
96       return m;
97     }
98 
unload_all_toolkits(void)99     void unload_all_toolkits (void)
100     {
101       while (! loaded_toolkits.empty ())
102         {
103           auto p = loaded_toolkits.begin ();
104 
105           std::string name = p->first;
106 
107           p->second.close ();
108 
109           // The toolkit may have unloaded itself.  If not, we'll do it here.
110           if (loaded_toolkits.find (name) != loaded_toolkits.end ())
111             unload_toolkit (name);
112         }
113     }
114 
default_toolkit(void)115     std::string default_toolkit (void) const { return dtk; }
116 
117   private:
118 
119     // The name of the default toolkit.
120     std::string dtk;
121 
122     // The list of toolkits that we know about.
123     std::set<std::string> available_toolkits;
124 
125     // The list of toolkits we have actually loaded.
126     std::map<std::string, graphics_toolkit> loaded_toolkits;
127   };
128 }
129 
130 #endif
131