Debian Codegraph @

gtk/src/colorselection.ccg

raw
1 /* Copyright (C) 1998-2002 The gtkmm Development Team
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #include <glibmm/vectorutils.h>
18
19 #include <gtk/gtk.h>
20
21
22 namespace
23 {
24
25 static Gtk::ColorSelection::SlotChangePaletteHook* global_change_palette_hook = nullptr;
26
27 static void global_change_palette_hook_callback(GdkScreen* screen, const GdkColor* colors, int n_colors)
28 {
29 g_return_if_fail(global_change_palette_hook != nullptr);
30
31 try
32 {
33 (*global_change_palette_hook)(
34 Glib::wrap(screen, true),
35 Glib::ArrayHandler<Gdk::Color, Gdk::ColorTraits>::array_to_vector(colors, n_colors, Glib::OWNERSHIP_NONE));
36 }
37 catch(...)
38 {
39 Glib::exception_handlers_invoke();
40 }
41 }
42
43 static void old_change_palette_hook_callback(const Glib::RefPtr<Gdk::Screen>& screen,
44 const std::vector<Gdk::Color>& colors,
45 GtkColorSelectionChangePaletteWithScreenFunc func)
46 {
47 g_return_if_fail(func != nullptr);
48
49 (*func)(Glib::unwrap(screen),
50 Glib::ArrayHandler<Gdk::Color, Gdk::ColorTraits>::vector_to_array(colors).data(),
51 colors.size());
52 }
53
54 } // anonymous namespace
55
56
57 namespace Gtk
58 {
59
60 Gdk::Color ColorSelection::get_current_color() const
61 {
62 Gdk::Color color; //GdkColor Just a simple struct.
63 gtk_color_selection_get_current_color(const_cast<GtkColorSelection*>(gobj()), color.gobj());
64 return color;
65 }
66
67 Gdk::Color ColorSelection::get_previous_color() const
68 {
69 Gdk::Color color; //GdkColor Just a simple struct.
70 gtk_color_selection_get_previous_color(const_cast<GtkColorSelection*>(gobj()), color.gobj());
71 return color;
72 }
73
74 Gdk::RGBA ColorSelection::get_current_rgba() const
75 {
76 Gdk::RGBA color; //GdkRGBA is just a simple struct.
77 gtk_color_selection_get_current_rgba(const_cast<GtkColorSelection*>(gobj()), color.gobj());
78 return color;
79 }
80
81 Gdk::RGBA ColorSelection::get_previous_rgba() const
82 {
83 Gdk::RGBA color; //GdkRGBA is just a simple struct.
84 gtk_color_selection_get_previous_rgba(const_cast<GtkColorSelection*>(gobj()), color.gobj());
85 return color;
86 }
87
88 // static
89 std::vector<Gdk::Color> ColorSelection::palette_from_string(const Glib::ustring& str)
90 {
91 GdkColor* colors = nullptr;
92 int n_colors = 0;
93
94 gtk_color_selection_palette_from_string(str.c_str(), &colors, &n_colors);
95
96 return Glib::ArrayHandler<Gdk::Color, Gdk::ColorTraits>::array_to_vector(colors, n_colors, Glib::OWNERSHIP_SHALLOW);
97 }
98
99 // static
100 Glib::ustring ColorSelection::palette_to_string(const std::vector<Gdk::Color>& colors)
101 {
102 if(char *const str = gtk_color_selection_palette_to_string(Glib::ArrayHandler<Gdk::Color, Gdk::ColorTraits>::vector_to_array(colors).data (), colors.size()))
103 return Glib::ustring(Glib::make_unique_ptr_gfree(str).get());
104 else
105 return Glib::ustring();
106 }
107
108 // static
109 ColorSelection::SlotChangePaletteHook
110 ColorSelection::set_change_palette_hook(const ColorSelection::SlotChangePaletteHook& slot)
111 {
112 GtkColorSelectionChangePaletteWithScreenFunc new_func = nullptr;
113 SlotChangePaletteHook* new_slot = nullptr;
114 SlotChangePaletteHook old_slot;
115
116 if(slot)
117 {
118 new_func = &global_change_palette_hook_callback;
119 new_slot = new SlotChangePaletteHook(slot);
120 }
121
122 const GtkColorSelectionChangePaletteWithScreenFunc old_func =
123 gtk_color_selection_set_change_palette_with_screen_hook(new_func);
124
125 if(old_func)
126 {
127 try
128 {
129 if(old_func != &global_change_palette_hook_callback)
130 old_slot = sigc::bind<-1>(sigc::ptr_fun(&old_change_palette_hook_callback), old_func);
131 else if(global_change_palette_hook)
132 old_slot = *global_change_palette_hook;
133 }
134 catch(...)
135 {
136 gtk_color_selection_set_change_palette_with_screen_hook(old_func);
137 delete new_slot;
138 throw;
139 }
140 }
141
142 delete global_change_palette_hook;
143 global_change_palette_hook = new_slot;
144
145 return old_slot;
146 }
147
148 } // namespace Gtk