1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/colordlg.cpp
3 // Purpose: Native wxColourDialog for GTK+
4 // Author: Vaclav Slavik
7 // Copyright: (c) Vaclav Slavik, 2004
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/colordlg.h"
21 #include "wx/modalhook.h"
28 #include "wx/gtk/private.h"
29 #include "wx/gtk/private/gtk2-compat.h"
30 #include "wx/gtk/private/dialogcount.h"
33 #include <hildon-widgets/hildon-color-selector.h>
34 #endif // wxUSE_LIBHILDON
38 #include <hildon/hildon.h>
40 #endif // wxUSE_LIBHILDON2
42 IMPLEMENT_DYNAMIC_CLASS(wxColourDialog
, wxDialog
)
44 wxColourDialog::wxColourDialog(wxWindow
*parent
, wxColourData
*data
)
49 bool wxColourDialog::Create(wxWindow
*parent
, wxColourData
*data
)
54 m_parent
= GetParentForModalDialog(parent
, 0);
55 GtkWindow
* const parentGTK
= m_parent
? GTK_WINDOW(m_parent
->m_widget
)
59 m_widget
= hildon_color_selector_new(parentGTK
);
60 #elif wxUSE_LIBHILDON2 // !wxUSE_LIBHILDON
61 m_widget
= hildon_color_chooser_dialog_new();
62 #else // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
63 wxString
title(_("Choose colour"));
64 m_widget
= gtk_color_selection_dialog_new(wxGTK_CONV(title
));
65 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
67 g_object_ref(m_widget
);
71 gtk_window_set_transient_for(GTK_WINDOW(m_widget
), parentGTK
);
74 #if !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
75 GtkColorSelection
* sel
= GTK_COLOR_SELECTION(
76 gtk_color_selection_dialog_get_color_selection(
77 GTK_COLOR_SELECTION_DIALOG(m_widget
)));
78 gtk_color_selection_set_has_palette(sel
, true);
79 #endif // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
84 int wxColourDialog::ShowModal()
86 WX_HOOK_MODAL_DIALOG();
90 wxOpenModalDialogLocker modalLocker
;
92 gint result
= gtk_dialog_run(GTK_DIALOG(m_widget
));
93 gtk_widget_hide(m_widget
);
98 wxFAIL_MSG(wxT("unexpected GtkColorSelectionDialog return code"));
101 case GTK_RESPONSE_CANCEL
:
102 case GTK_RESPONSE_DELETE_EVENT
:
103 case GTK_RESPONSE_CLOSE
:
106 case GTK_RESPONSE_OK
:
107 DialogToColourData();
112 void wxColourDialog::ColourDataToDialog()
114 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
115 const GdkColor
* const
116 col
= m_data
.GetColour().IsOk() ? m_data
.GetColour().GetColor()
120 HildonColorSelector
* const sel
= HILDON_COLOR_SELECTOR(m_widget
);
121 hildon_color_selector_set_color(sel
, const_cast<GdkColor
*>(col
));
122 #elif wxUSE_LIBHILDON2
133 hildon_color_chooser_dialog_set_color((HildonColorChooserDialog
*)m_widget
, &clr
);
134 #else // !wxUSE_LIBHILDON2/!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
135 GtkColorSelection
* sel
= GTK_COLOR_SELECTION(
136 gtk_color_selection_dialog_get_color_selection(
137 GTK_COLOR_SELECTION_DIALOG(m_widget
)));
139 const wxColour
& color
= m_data
.GetColour();
143 gtk_color_selection_set_current_rgba(sel
, color
);
145 gtk_color_selection_set_current_color(sel
, color
.GetColor());
149 // setup the palette:
151 GdkColor colors
[wxColourData::NUM_CUSTOM
];
153 for (unsigned i
= 0; i
< WXSIZEOF(colors
); i
++)
155 wxColour c
= m_data
.GetCustomColour(i
);
158 colors
[n_colors
] = *c
.GetColor();
163 wxGtkString
pal(gtk_color_selection_palette_to_string(colors
, n_colors
));
165 GtkSettings
*settings
= gtk_widget_get_settings(GTK_WIDGET(sel
));
166 g_object_set(settings
, "gtk-color-palette", pal
.c_str(), NULL
);
167 #endif // wxUSE_LIBHILDON / wxUSE_LIBHILDON2 /!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
170 void wxColourDialog::DialogToColourData()
173 HildonColorSelector
* const sel
= HILDON_COLOR_SELECTOR(m_widget
);
174 const GdkColor
* const clr
= hildon_color_selector_get_color(sel
);
176 m_data
.SetColour(*clr
);
177 #elif wxUSE_LIBHILDON2 // !wxUSE_LIBHILDON
178 const GdkColor
* const
179 col
= m_data
.GetColour().IsOk() ? m_data
.GetColour().GetColor() : NULL
;
190 GdkColor new_color
= clr
;
191 hildon_color_chooser_dialog_get_color((HildonColorChooserDialog
*)m_widget
, &new_color
);
193 m_data
.SetColour(new_color
);
194 #else // !wxUSE_LIBHILDON2
196 GtkColorSelection
* sel
= GTK_COLOR_SELECTION(
197 gtk_color_selection_dialog_get_color_selection(
198 GTK_COLOR_SELECTION_DIALOG(m_widget
)));
202 gtk_color_selection_get_current_rgba(sel
, &clr
);
205 gtk_color_selection_get_current_color(sel
, &clr
);
207 m_data
.SetColour(clr
);
209 // Extract custom palette:
211 GtkSettings
*settings
= gtk_widget_get_settings(GTK_WIDGET(sel
));
213 g_object_get(settings
, "gtk-color-palette", &pal
, NULL
);
217 if (gtk_color_selection_palette_from_string(pal
, &colors
, &n_colors
))
219 for (int i
= 0; i
< n_colors
&& i
< wxColourData::NUM_CUSTOM
; i
++)
221 m_data
.SetCustomColour(i
, wxColour(colors
[i
]));
227 #endif // wxUSE_LIBHILDON / wxUSE_LIBHILDON2 /!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
230 #endif // wxUSE_COLOURDLG