]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/colordlg.cpp
fix wxExecute() return code checks and removed not working code to open URLs in new...
[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 && defined(__WXGTK20__)
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 IMPLEMENT_DYNAMIC_CLASS(wxColourDialog, wxDialog)
30
31 wxColourDialog::wxColourDialog(wxWindow *parent, wxColourData *data)
32 {
33 Create(parent, data);
34 }
35
36 bool wxColourDialog::Create(wxWindow *parent, wxColourData *data)
37 {
38 if (data)
39 m_data = *data;
40
41 wxString title(_("Choose colour"));
42 m_widget = gtk_color_selection_dialog_new(wxGTK_CONV(title));
43
44 if (parent)
45 gtk_window_set_transient_for(GTK_WINDOW(m_widget),
46 GTK_WINDOW(parent->m_widget));
47
48 GtkColorSelection *sel =
49 GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(m_widget)->colorsel);
50 gtk_color_selection_set_has_palette(sel, true);
51
52 return true;
53 }
54
55 int wxColourDialog::ShowModal()
56 {
57 ColourDataToDialog();
58
59 gint result = gtk_dialog_run(GTK_DIALOG(m_widget));
60 gtk_widget_hide(m_widget);
61
62 switch (result)
63 {
64 default:
65 wxFAIL_MSG(_T("unexpected GtkColorSelectionDialog return code"));
66 // fall through
67
68 case GTK_RESPONSE_CANCEL:
69 case GTK_RESPONSE_DELETE_EVENT:
70 case GTK_RESPONSE_CLOSE:
71 return wxID_CANCEL;
72
73 case GTK_RESPONSE_OK:
74 DialogToColourData();
75 return wxID_OK;
76 }
77 }
78
79 void wxColourDialog::ColourDataToDialog()
80 {
81 GtkColorSelection *sel =
82 GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(m_widget)->colorsel);
83
84 if (m_data.GetColour().Ok())
85 {
86 gtk_color_selection_set_current_color(sel,
87 m_data.GetColour().GetColor());
88 }
89
90 // setup the palette:
91
92 GdkColor colors[16];
93 gint n_colors = 0;
94 for (unsigned i = 0; i < 16; i++)
95 {
96 wxColour c = m_data.GetCustomColour(i);
97 if (c.Ok())
98 {
99 colors[n_colors] = *c.GetColor();
100 n_colors++;
101 }
102 }
103
104 gchar *pal = gtk_color_selection_palette_to_string(colors, n_colors);
105
106 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
107 g_object_set(settings, "gtk-color-palette", pal, NULL);
108
109 g_free(pal);
110 }
111
112 void wxColourDialog::DialogToColourData()
113 {
114 GtkColorSelection *sel =
115 GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(m_widget)->colorsel);
116
117 GdkColor clr;
118 gtk_color_selection_get_current_color(sel, &clr);
119 m_data.SetColour(wxColour(clr.red >> 8, clr.green >> 8, clr.blue >> 8));
120
121 // Extract custom palette:
122
123 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
124 gchar *pal;
125 g_object_get(settings, "gtk-color-palette", &pal, NULL);
126
127 GdkColor *colors;
128 gint n_colors;
129 if (gtk_color_selection_palette_from_string(pal, &colors, &n_colors))
130 {
131 for (int i = 0; i < wxMin(n_colors, 16); i++)
132 {
133 m_data.SetCustomColour(i, wxColour(colors[i].red >> 8,
134 colors[i].green >> 8,
135 colors[i].blue >> 8));
136 }
137 g_free(colors);
138 }
139
140 g_free(pal);
141 }
142
143 #endif // wxUSE_COLOURDLG && defined(__WXGTK20__)
144