1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/colordlg.cpp
3 // Purpose: Native wxColourDialog for GTK+
4 // Author: Vaclav Slavik
8 // Copyright: (c) Vaclav Slavik, 2004
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/colordlg.h"
22 #include "wx/modalhook.h"
29 #include "wx/gtk/private.h"
30 #include "wx/gtk/private/gtk2-compat.h"
31 #include "wx/gtk/private/dialogcount.h"
34 #include <hildon-widgets/hildon-color-selector.h>
35 #endif // wxUSE_LIBHILDON
39 #include <hildon/hildon.h>
41 #endif // wxUSE_LIBHILDON2
43 IMPLEMENT_DYNAMIC_CLASS(wxColourDialog
, wxDialog
)
45 wxColourDialog::wxColourDialog(wxWindow
*parent
, wxColourData
*data
)
50 bool wxColourDialog::Create(wxWindow
*parent
, wxColourData
*data
)
55 m_parent
= GetParentForModalDialog(parent
, 0);
56 GtkWindow
* const parentGTK
= m_parent
? GTK_WINDOW(m_parent
->m_widget
)
60 m_widget
= hildon_color_selector_new(parentGTK
);
61 #elif wxUSE_LIBHILDON2 // !wxUSE_LIBHILDON
62 m_widget
= hildon_color_chooser_dialog_new();
63 #else // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
64 wxString
title(_("Choose colour"));
65 m_widget
= gtk_color_selection_dialog_new(wxGTK_CONV(title
));
66 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
68 g_object_ref(m_widget
);
72 gtk_window_set_transient_for(GTK_WINDOW(m_widget
), parentGTK
);
75 #if !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
76 GtkColorSelection
* sel
= GTK_COLOR_SELECTION(
77 gtk_color_selection_dialog_get_color_selection(
78 GTK_COLOR_SELECTION_DIALOG(m_widget
)));
79 gtk_color_selection_set_has_palette(sel
, true);
80 #endif // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
85 int wxColourDialog::ShowModal()
87 WX_HOOK_MODAL_DIALOG();
91 wxOpenModalDialogLocker modalLocker
;
93 gint result
= gtk_dialog_run(GTK_DIALOG(m_widget
));
94 gtk_widget_hide(m_widget
);
99 wxFAIL_MSG(wxT("unexpected GtkColorSelectionDialog return code"));
102 case GTK_RESPONSE_CANCEL
:
103 case GTK_RESPONSE_DELETE_EVENT
:
104 case GTK_RESPONSE_CLOSE
:
107 case GTK_RESPONSE_OK
:
108 DialogToColourData();
113 void wxColourDialog::ColourDataToDialog()
115 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
116 const GdkColor
* const
117 col
= m_data
.GetColour().IsOk() ? m_data
.GetColour().GetColor()
121 HildonColorSelector
* const sel
= HILDON_COLOR_SELECTOR(m_widget
);
122 hildon_color_selector_set_color(sel
, const_cast<GdkColor
*>(col
));
123 #elif wxUSE_LIBHILDON2
134 hildon_color_chooser_dialog_set_color((HildonColorChooserDialog
*)m_widget
, &clr
);
135 #else // !wxUSE_LIBHILDON2/!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
136 GtkColorSelection
* sel
= GTK_COLOR_SELECTION(
137 gtk_color_selection_dialog_get_color_selection(
138 GTK_COLOR_SELECTION_DIALOG(m_widget
)));
140 const wxColour
& color
= m_data
.GetColour();
144 gtk_color_selection_set_current_rgba(sel
, color
);
146 gtk_color_selection_set_current_color(sel
, color
.GetColor());
150 // setup the palette:
152 GdkColor colors
[wxColourData::NUM_CUSTOM
];
154 for (unsigned i
= 0; i
< WXSIZEOF(colors
); i
++)
156 wxColour c
= m_data
.GetCustomColour(i
);
159 colors
[n_colors
] = *c
.GetColor();
164 wxGtkString
pal(gtk_color_selection_palette_to_string(colors
, n_colors
));
166 GtkSettings
*settings
= gtk_widget_get_settings(GTK_WIDGET(sel
));
167 g_object_set(settings
, "gtk-color-palette", pal
.c_str(), NULL
);
168 #endif // wxUSE_LIBHILDON / wxUSE_LIBHILDON2 /!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
171 void wxColourDialog::DialogToColourData()
174 HildonColorSelector
* const sel
= HILDON_COLOR_SELECTOR(m_widget
);
175 const GdkColor
* const clr
= hildon_color_selector_get_color(sel
);
177 m_data
.SetColour(*clr
);
178 #elif wxUSE_LIBHILDON2 // !wxUSE_LIBHILDON
179 const GdkColor
* const
180 col
= m_data
.GetColour().IsOk() ? m_data
.GetColour().GetColor() : NULL
;
191 GdkColor new_color
= clr
;
192 hildon_color_chooser_dialog_get_color((HildonColorChooserDialog
*)m_widget
, &new_color
);
194 m_data
.SetColour(new_color
);
195 #else // !wxUSE_LIBHILDON2
197 GtkColorSelection
* sel
= GTK_COLOR_SELECTION(
198 gtk_color_selection_dialog_get_color_selection(
199 GTK_COLOR_SELECTION_DIALOG(m_widget
)));
203 gtk_color_selection_get_current_rgba(sel
, &clr
);
206 gtk_color_selection_get_current_color(sel
, &clr
);
208 m_data
.SetColour(clr
);
210 // Extract custom palette:
212 GtkSettings
*settings
= gtk_widget_get_settings(GTK_WIDGET(sel
));
214 g_object_get(settings
, "gtk-color-palette", &pal
, NULL
);
218 if (gtk_color_selection_palette_from_string(pal
, &colors
, &n_colors
))
220 for (int i
= 0; i
< n_colors
&& i
< wxColourData::NUM_CUSTOM
; i
++)
222 m_data
.SetCustomColour(i
, wxColour(colors
[i
]));
228 #endif // wxUSE_LIBHILDON / wxUSE_LIBHILDON2 /!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
231 #endif // wxUSE_COLOURDLG