| 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 | #if wxUSE_LIBHILDON2 |
| 34 | extern "C" { |
| 35 | #include <hildon/hildon.h> |
| 36 | } |
| 37 | #endif // wxUSE_LIBHILDON2 |
| 38 | |
| 39 | IMPLEMENT_DYNAMIC_CLASS(wxColourDialog, wxDialog) |
| 40 | |
| 41 | wxColourDialog::wxColourDialog(wxWindow *parent, wxColourData *data) |
| 42 | { |
| 43 | Create(parent, data); |
| 44 | } |
| 45 | |
| 46 | bool wxColourDialog::Create(wxWindow *parent, wxColourData *data) |
| 47 | { |
| 48 | if (data) |
| 49 | m_data = *data; |
| 50 | |
| 51 | m_parent = GetParentForModalDialog(parent, 0); |
| 52 | GtkWindow * const parentGTK = m_parent ? GTK_WINDOW(m_parent->m_widget) |
| 53 | : NULL; |
| 54 | |
| 55 | #if wxUSE_LIBHILDON |
| 56 | m_widget = hildon_color_selector_new(parentGTK); |
| 57 | #elif wxUSE_LIBHILDON2 // !wxUSE_LIBHILDON |
| 58 | m_widget = hildon_color_chooser_dialog_new(); |
| 59 | #else // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2 |
| 60 | wxString title(_("Choose colour")); |
| 61 | m_widget = gtk_color_selection_dialog_new(wxGTK_CONV(title)); |
| 62 | #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON |
| 63 | |
| 64 | g_object_ref(m_widget); |
| 65 | |
| 66 | if ( parentGTK ) |
| 67 | { |
| 68 | gtk_window_set_transient_for(GTK_WINDOW(m_widget), parentGTK); |
| 69 | } |
| 70 | |
| 71 | #if !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2 |
| 72 | GtkColorSelection *sel = |
| 73 | GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(m_widget)->colorsel); |
| 74 | gtk_color_selection_set_has_palette(sel, true); |
| 75 | #endif // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2 |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | int wxColourDialog::ShowModal() |
| 81 | { |
| 82 | ColourDataToDialog(); |
| 83 | |
| 84 | gint result = gtk_dialog_run(GTK_DIALOG(m_widget)); |
| 85 | gtk_widget_hide(m_widget); |
| 86 | |
| 87 | switch (result) |
| 88 | { |
| 89 | default: |
| 90 | wxFAIL_MSG(wxT("unexpected GtkColorSelectionDialog return code")); |
| 91 | // fall through |
| 92 | |
| 93 | case GTK_RESPONSE_CANCEL: |
| 94 | case GTK_RESPONSE_DELETE_EVENT: |
| 95 | case GTK_RESPONSE_CLOSE: |
| 96 | return wxID_CANCEL; |
| 97 | |
| 98 | case GTK_RESPONSE_OK: |
| 99 | DialogToColourData(); |
| 100 | return wxID_OK; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void wxColourDialog::ColourDataToDialog() |
| 105 | { |
| 106 | const GdkColor * const |
| 107 | col = m_data.GetColour().Ok() ? m_data.GetColour().GetColor() |
| 108 | : NULL; |
| 109 | |
| 110 | #if wxUSE_LIBHILDON |
| 111 | HildonColorSelector * const sel = HILDON_COLOR_SELECTOR(m_widget); |
| 112 | hildon_color_selector_set_color(sel, const_cast<GdkColor *>(col)); |
| 113 | #elif wxUSE_LIBHILDON2 |
| 114 | GdkColor clr; |
| 115 | if (col) |
| 116 | clr = *col; |
| 117 | else { |
| 118 | clr.pixel = 0; |
| 119 | clr.red = 32768; |
| 120 | clr.green = 32768; |
| 121 | clr.blue = 32768; |
| 122 | } |
| 123 | |
| 124 | hildon_color_chooser_dialog_set_color((HildonColorChooserDialog *)m_widget, &clr); |
| 125 | #else // !wxUSE_LIBHILDON2/!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2 |
| 126 | GtkColorSelection *sel = |
| 127 | GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(m_widget)->colorsel); |
| 128 | |
| 129 | if ( col ) |
| 130 | gtk_color_selection_set_current_color(sel, col); |
| 131 | |
| 132 | // setup the palette: |
| 133 | |
| 134 | GdkColor colors[16]; |
| 135 | gint n_colors = 0; |
| 136 | for (unsigned i = 0; i < 16; i++) |
| 137 | { |
| 138 | wxColour c = m_data.GetCustomColour(i); |
| 139 | if (c.Ok()) |
| 140 | { |
| 141 | colors[n_colors] = *c.GetColor(); |
| 142 | n_colors++; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | wxGtkString pal(gtk_color_selection_palette_to_string(colors, n_colors)); |
| 147 | |
| 148 | GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel)); |
| 149 | g_object_set(settings, "gtk-color-palette", pal.c_str(), NULL); |
| 150 | #endif // wxUSE_LIBHILDON / wxUSE_LIBHILDON2 /!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2 |
| 151 | } |
| 152 | |
| 153 | void wxColourDialog::DialogToColourData() |
| 154 | { |
| 155 | #if wxUSE_LIBHILDON |
| 156 | HildonColorSelector * const sel = HILDON_COLOR_SELECTOR(m_widget); |
| 157 | const GdkColor * const clr = hildon_color_selector_get_color(sel); |
| 158 | if ( clr ) |
| 159 | m_data.SetColour(*clr); |
| 160 | #elif wxUSE_LIBHILDON2 // !wxUSE_LIBHILDON |
| 161 | const GdkColor * const |
| 162 | col = m_data.GetColour().Ok() ? m_data.GetColour().GetColor() : NULL; |
| 163 | |
| 164 | GdkColor clr; |
| 165 | if (col) |
| 166 | clr = *col; |
| 167 | else { |
| 168 | clr.pixel = 0; |
| 169 | clr.red = 32768; |
| 170 | clr.green = 32768; |
| 171 | clr.blue = 32768; |
| 172 | } |
| 173 | GdkColor new_color = clr; |
| 174 | hildon_color_chooser_dialog_get_color((HildonColorChooserDialog *)m_widget, &new_color); |
| 175 | |
| 176 | m_data.SetColour(new_color); |
| 177 | #else // !wxUSE_LIBHILDON2 |
| 178 | |
| 179 | GtkColorSelection *sel = |
| 180 | GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(m_widget)->colorsel); |
| 181 | |
| 182 | GdkColor clr; |
| 183 | gtk_color_selection_get_current_color(sel, &clr); |
| 184 | m_data.SetColour(clr); |
| 185 | |
| 186 | // Extract custom palette: |
| 187 | |
| 188 | GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel)); |
| 189 | gchar *pal; |
| 190 | g_object_get(settings, "gtk-color-palette", &pal, NULL); |
| 191 | |
| 192 | GdkColor *colors; |
| 193 | gint n_colors; |
| 194 | if (gtk_color_selection_palette_from_string(pal, &colors, &n_colors)) |
| 195 | { |
| 196 | for (int i = 0; i < wxMin(n_colors, 16); i++) |
| 197 | { |
| 198 | m_data.SetCustomColour(i, wxColour(colors[i])); |
| 199 | } |
| 200 | g_free(colors); |
| 201 | } |
| 202 | |
| 203 | g_free(pal); |
| 204 | #endif // wxUSE_LIBHILDON / wxUSE_LIBHILDON2 /!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2 |
| 205 | } |
| 206 | |
| 207 | #endif // wxUSE_COLOURDLG |
| 208 | |