Hildonized wxColourDialog
[wxWidgets.git] / src / gtk / colordlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/colordlg.cpp
3 // Purpose: Native wxColourDialog for GTK+
4 // Author: Vaclav Slavik
5 // Modified by:
6 // Created: 2004/06/04
7 // RCS-ID: $Id$
8 // Copyright: (c) Vaclav Slavik, 2004
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_COLOURDLG
20
21 #include "wx/colordlg.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/intl.h"
25 #endif
26
27 #include "wx/gtk/private.h"
28
29 #if wxUSE_LIBHILDON
30 #include <hildon-widgets/hildon-color-selector.h>
31 #endif // wxUSE_LIBHILDON
32
33 IMPLEMENT_DYNAMIC_CLASS(wxColourDialog, wxDialog)
34
35 wxColourDialog::wxColourDialog(wxWindow *parent, wxColourData *data)
36 {
37 Create(parent, data);
38 }
39
40 bool wxColourDialog::Create(wxWindow *parent, wxColourData *data)
41 {
42 if (data)
43 m_data = *data;
44
45 m_parent = GetParentForModalDialog(parent);
46 GtkWindow * const parentGTK = m_parent ? GTK_WINDOW(m_parent->m_widget)
47 : NULL;
48
49 #if wxUSE_LIBHILDON
50 m_widget = hildon_color_selector_new(parentGTK);
51 #else // !wxUSE_LIBHILDON
52 wxString title(_("Choose colour"));
53 m_widget = gtk_color_selection_dialog_new(wxGTK_CONV(title));
54 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
55
56 if ( parentGTK )
57 {
58 gtk_window_set_transient_for(GTK_WINDOW(m_widget), parentGTK);
59 }
60
61 GtkColorSelection *sel =
62 GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(m_widget)->colorsel);
63 gtk_color_selection_set_has_palette(sel, true);
64
65 return true;
66 }
67
68 int wxColourDialog::ShowModal()
69 {
70 ColourDataToDialog();
71
72 gint result = gtk_dialog_run(GTK_DIALOG(m_widget));
73 gtk_widget_hide(m_widget);
74
75 switch (result)
76 {
77 default:
78 wxFAIL_MSG(_T("unexpected GtkColorSelectionDialog return code"));
79 // fall through
80
81 case GTK_RESPONSE_CANCEL:
82 case GTK_RESPONSE_DELETE_EVENT:
83 case GTK_RESPONSE_CLOSE:
84 return wxID_CANCEL;
85
86 case GTK_RESPONSE_OK:
87 DialogToColourData();
88 return wxID_OK;
89 }
90 }
91
92 void wxColourDialog::ColourDataToDialog()
93 {
94 const GdkColor * const
95 col = m_data.GetColour().Ok() ? m_data.GetColour().GetColor()
96 : NULL;
97
98 #if wxUSE_LIBHILDON
99 HildonColorSelector * const sel = HILDON_COLOR_SELECTOR(m_widget);
100 hildon_color_selector_set_color(sel, wx_const_cast(GdkColor *, col));
101 #else // !wxUSE_LIBHILDON
102 GtkColorSelection *sel =
103 GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(m_widget)->colorsel);
104
105 if ( col )
106 gtk_color_selection_set_current_color(sel, col);
107
108 // setup the palette:
109
110 GdkColor colors[16];
111 gint n_colors = 0;
112 for (unsigned i = 0; i < 16; i++)
113 {
114 wxColour c = m_data.GetCustomColour(i);
115 if (c.Ok())
116 {
117 colors[n_colors] = *c.GetColor();
118 n_colors++;
119 }
120 }
121
122 wxGtkString pal(gtk_color_selection_palette_to_string(colors, n_colors));
123
124 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
125 g_object_set(settings, "gtk-color-palette", pal.c_str(), NULL);
126 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
127 }
128
129 void wxColourDialog::DialogToColourData()
130 {
131 #if wxUSE_LIBHILDON
132 HildonColorSelector * const sel = HILDON_COLOR_SELECTOR(m_widget);
133 const GdkColor * const clr = hildon_color_selector_get_color(sel);
134 if ( clr )
135 m_data.SetColour(*clr);
136 #else // !wxUSE_LIBHILDON
137 GtkColorSelection *sel =
138 GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(m_widget)->colorsel);
139
140 GdkColor clr;
141 gtk_color_selection_get_current_color(sel, &clr);
142 m_data.SetColour(clr);
143
144 // Extract custom palette:
145
146 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
147 gchar *pal;
148 g_object_get(settings, "gtk-color-palette", &pal, NULL);
149
150 GdkColor *colors;
151 gint n_colors;
152 if (gtk_color_selection_palette_from_string(pal, &colors, &n_colors))
153 {
154 for (int i = 0; i < wxMin(n_colors, 16); i++)
155 {
156 m_data.SetCustomColour(i, wxColour(colors[i]));
157 }
158 g_free(colors);
159 }
160
161 g_free(pal);
162 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
163 }
164
165 #endif // wxUSE_COLOURDLG
166