]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/colordlg.cpp
trying to resolve GSocketManager API mess: the meaning of Install/Uninstall_Callback...
[wxWidgets.git] / src / gtk / colordlg.cpp
CommitLineData
274ad000
VS
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
274ad000
VS
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
ff654490 19#if wxUSE_COLOURDLG
274ad000
VS
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
b6dd7ed8
VZ
29#if wxUSE_LIBHILDON
30 #include <hildon-widgets/hildon-color-selector.h>
31#endif // wxUSE_LIBHILDON
32
274ad000
VS
33IMPLEMENT_DYNAMIC_CLASS(wxColourDialog, wxDialog)
34
35wxColourDialog::wxColourDialog(wxWindow *parent, wxColourData *data)
36{
37 Create(parent, data);
38}
39
40bool wxColourDialog::Create(wxWindow *parent, wxColourData *data)
41{
42 if (data)
43 m_data = *data;
44
b6dd7ed8
VZ
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
274ad000
VS
52 wxString title(_("Choose colour"));
53 m_widget = gtk_color_selection_dialog_new(wxGTK_CONV(title));
b6dd7ed8 54#endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
d0ee33f5 55
9ff9d30c
PC
56 g_object_ref(m_widget);
57
b6dd7ed8 58 if ( parentGTK )
fa349e95 59 {
b6dd7ed8 60 gtk_window_set_transient_for(GTK_WINDOW(m_widget), parentGTK);
fa349e95 61 }
d0ee33f5
WS
62
63 GtkColorSelection *sel =
274ad000
VS
64 GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(m_widget)->colorsel);
65 gtk_color_selection_set_has_palette(sel, true);
66
67 return true;
68}
d0ee33f5 69
274ad000
VS
70int wxColourDialog::ShowModal()
71{
72 ColourDataToDialog();
d0ee33f5 73
274ad000 74 gint result = gtk_dialog_run(GTK_DIALOG(m_widget));
b25630fb 75 gtk_widget_hide(m_widget);
d0ee33f5 76
274ad000
VS
77 switch (result)
78 {
79 default:
80 wxFAIL_MSG(_T("unexpected GtkColorSelectionDialog return code"));
81 // fall through
82
83 case GTK_RESPONSE_CANCEL:
84 case GTK_RESPONSE_DELETE_EVENT:
85 case GTK_RESPONSE_CLOSE:
86 return wxID_CANCEL;
d0ee33f5 87
274ad000
VS
88 case GTK_RESPONSE_OK:
89 DialogToColourData();
90 return wxID_OK;
d0ee33f5 91 }
274ad000
VS
92}
93
94void wxColourDialog::ColourDataToDialog()
95{
b6dd7ed8
VZ
96 const GdkColor * const
97 col = m_data.GetColour().Ok() ? m_data.GetColour().GetColor()
98 : NULL;
99
100#if wxUSE_LIBHILDON
101 HildonColorSelector * const sel = HILDON_COLOR_SELECTOR(m_widget);
5c33522f 102 hildon_color_selector_set_color(sel, const_cast<GdkColor *>(col));
b6dd7ed8 103#else // !wxUSE_LIBHILDON
d0ee33f5 104 GtkColorSelection *sel =
274ad000 105 GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(m_widget)->colorsel);
d0ee33f5 106
b6dd7ed8
VZ
107 if ( col )
108 gtk_color_selection_set_current_color(sel, col);
274ad000
VS
109
110 // setup the palette:
111
112 GdkColor colors[16];
113 gint n_colors = 0;
114 for (unsigned i = 0; i < 16; i++)
115 {
116 wxColour c = m_data.GetCustomColour(i);
117 if (c.Ok())
118 {
119 colors[n_colors] = *c.GetColor();
120 n_colors++;
121 }
122 }
123
b976323b 124 wxGtkString pal(gtk_color_selection_palette_to_string(colors, n_colors));
274ad000
VS
125
126 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
ed97f493 127 g_object_set(settings, "gtk-color-palette", pal.c_str(), NULL);
b6dd7ed8 128#endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
274ad000
VS
129}
130
131void wxColourDialog::DialogToColourData()
132{
b6dd7ed8
VZ
133#if wxUSE_LIBHILDON
134 HildonColorSelector * const sel = HILDON_COLOR_SELECTOR(m_widget);
135 const GdkColor * const clr = hildon_color_selector_get_color(sel);
136 if ( clr )
137 m_data.SetColour(*clr);
138#else // !wxUSE_LIBHILDON
d0ee33f5 139 GtkColorSelection *sel =
274ad000
VS
140 GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(m_widget)->colorsel);
141
142 GdkColor clr;
143 gtk_color_selection_get_current_color(sel, &clr);
b6dd7ed8 144 m_data.SetColour(clr);
d0ee33f5 145
274ad000
VS
146 // Extract custom palette:
147
148 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
149 gchar *pal;
150 g_object_get(settings, "gtk-color-palette", &pal, NULL);
151
152 GdkColor *colors;
153 gint n_colors;
154 if (gtk_color_selection_palette_from_string(pal, &colors, &n_colors))
155 {
156 for (int i = 0; i < wxMin(n_colors, 16); i++)
157 {
b91fd704 158 m_data.SetCustomColour(i, wxColour(colors[i]));
274ad000
VS
159 }
160 g_free(colors);
161 }
d0ee33f5 162
274ad000 163 g_free(pal);
b6dd7ed8 164#endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
274ad000
VS
165}
166
ff654490 167#endif // wxUSE_COLOURDLG
274ad000 168