]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/colordlg.cpp
Rearrange code to fix some problems
[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
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
29IMPLEMENT_DYNAMIC_CLASS(wxColourDialog, wxDialog)
30
31wxColourDialog::wxColourDialog(wxWindow *parent, wxColourData *data)
32{
33 Create(parent, data);
34}
35
36bool 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));
d0ee33f5 43
2229243b
VZ
44 m_parent = GetParentForModalDialog(parent);
45 if ( m_parent )
fa349e95 46 {
2229243b 47 GtkWindow* gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(m_parent->m_widget) );
274ad000 48 gtk_window_set_transient_for(GTK_WINDOW(m_widget),
fa349e95
KH
49 gtk_parent);
50 }
d0ee33f5
WS
51
52 GtkColorSelection *sel =
274ad000
VS
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}
d0ee33f5 58
274ad000
VS
59int wxColourDialog::ShowModal()
60{
61 ColourDataToDialog();
d0ee33f5 62
274ad000 63 gint result = gtk_dialog_run(GTK_DIALOG(m_widget));
b25630fb 64 gtk_widget_hide(m_widget);
d0ee33f5 65
274ad000
VS
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;
d0ee33f5 76
274ad000
VS
77 case GTK_RESPONSE_OK:
78 DialogToColourData();
79 return wxID_OK;
d0ee33f5 80 }
274ad000
VS
81}
82
83void wxColourDialog::ColourDataToDialog()
84{
d0ee33f5 85 GtkColorSelection *sel =
274ad000 86 GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(m_widget)->colorsel);
d0ee33f5 87
274ad000
VS
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
b976323b 108 wxGtkString pal(gtk_color_selection_palette_to_string(colors, n_colors));
274ad000
VS
109
110 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
ed97f493 111 g_object_set(settings, "gtk-color-palette", pal.c_str(), NULL);
274ad000
VS
112}
113
114void wxColourDialog::DialogToColourData()
115{
d0ee33f5 116 GtkColorSelection *sel =
274ad000
VS
117 GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(m_widget)->colorsel);
118
119 GdkColor clr;
120 gtk_color_selection_get_current_color(sel, &clr);
b91fd704 121 m_data.SetColour(wxColour(clr));
d0ee33f5 122
274ad000
VS
123 // Extract custom palette:
124
125 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
126 gchar *pal;
127 g_object_get(settings, "gtk-color-palette", &pal, NULL);
128
129 GdkColor *colors;
130 gint n_colors;
131 if (gtk_color_selection_palette_from_string(pal, &colors, &n_colors))
132 {
133 for (int i = 0; i < wxMin(n_colors, 16); i++)
134 {
b91fd704 135 m_data.SetCustomColour(i, wxColour(colors[i]));
274ad000
VS
136 }
137 g_free(colors);
138 }
d0ee33f5 139
274ad000
VS
140 g_free(pal);
141}
142
143#endif // wxUSE_COLOURDLG && defined(__WXGTK20__)
144