]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/colordlg.cpp
Fix memory leak when a spacer is added, and crash when a window is added before wxSiz...
[wxWidgets.git] / src / gtk1 / 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "colordlg.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_COLOURDLG && defined(__WXGTK20__)
24
25 #include "wx/colordlg.h"
26
27 #ifndef WX_PRECOMP
28 #include "wx/intl.h"
29 #endif
30
31 #include "wx/gtk/private.h"
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 wxString title(_("Choose colour"));
46 m_widget = gtk_color_selection_dialog_new(wxGTK_CONV(title));
47
48 if (parent)
49 gtk_window_set_transient_for(GTK_WINDOW(m_widget),
50 GTK_WINDOW(parent->m_widget));
51
52 GtkColorSelection *sel =
53 GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(m_widget)->colorsel);
54 gtk_color_selection_set_has_palette(sel, true);
55
56 return true;
57 }
58
59 int wxColourDialog::ShowModal()
60 {
61 ColourDataToDialog();
62
63 gint result = gtk_dialog_run(GTK_DIALOG(m_widget));
64 gtk_widget_hide(m_widget);
65
66 switch (result)
67 {
68 default:
69 wxFAIL_MSG(_T("unexpected GtkColorSelectionDialog return code"));
70 // fall through
71
72 case GTK_RESPONSE_CANCEL:
73 case GTK_RESPONSE_DELETE_EVENT:
74 case GTK_RESPONSE_CLOSE:
75 return wxID_CANCEL;
76
77 case GTK_RESPONSE_OK:
78 DialogToColourData();
79 return wxID_OK;
80 };
81 }
82
83 void wxColourDialog::ColourDataToDialog()
84 {
85 GtkColorSelection *sel =
86 GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(m_widget)->colorsel);
87
88 if (m_data.GetColour().Ok())
89 {
90 gtk_color_selection_set_current_color(sel,
91 m_data.GetColour().GetColor());
92 }
93
94 // setup the palette:
95
96 GdkColor colors[16];
97 gint n_colors = 0;
98 for (unsigned i = 0; i < 16; i++)
99 {
100 wxColour c = m_data.GetCustomColour(i);
101 if (c.Ok())
102 {
103 colors[n_colors] = *c.GetColor();
104 n_colors++;
105 }
106 }
107
108 gchar *pal = gtk_color_selection_palette_to_string(colors, n_colors);
109
110 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
111 g_object_set(settings, "gtk-color-palette", pal, NULL);
112
113 g_free(pal);
114 }
115
116 void wxColourDialog::DialogToColourData()
117 {
118 GtkColorSelection *sel =
119 GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(m_widget)->colorsel);
120
121 GdkColor clr;
122 gtk_color_selection_get_current_color(sel, &clr);
123 m_data.SetColour(wxColour(clr.red >> 8, clr.green >> 8, clr.blue >> 8));
124
125 // Extract custom palette:
126
127 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
128 gchar *pal;
129 g_object_get(settings, "gtk-color-palette", &pal, NULL);
130
131 GdkColor *colors;
132 gint n_colors;
133 if (gtk_color_selection_palette_from_string(pal, &colors, &n_colors))
134 {
135 for (int i = 0; i < wxMin(n_colors, 16); i++)
136 {
137 m_data.SetCustomColour(i, wxColour(colors[i].red >> 8,
138 colors[i].green >> 8,
139 colors[i].blue >> 8));
140 }
141 g_free(colors);
142 }
143
144 g_free(pal);
145 }
146
147 #endif // wxUSE_COLOURDLG && defined(__WXGTK20__)
148