]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/colordlg.cpp
show help for the page under mouse when the [?] button is used to request help
[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
274ad000 44 if (parent)
fa349e95
KH
45 {
46 GtkWindow* gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
274ad000 47 gtk_window_set_transient_for(GTK_WINDOW(m_widget),
fa349e95
KH
48 gtk_parent);
49 }
d0ee33f5
WS
50
51 GtkColorSelection *sel =
274ad000
VS
52 GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(m_widget)->colorsel);
53 gtk_color_selection_set_has_palette(sel, true);
54
55 return true;
56}
d0ee33f5 57
274ad000
VS
58int wxColourDialog::ShowModal()
59{
60 ColourDataToDialog();
d0ee33f5 61
274ad000 62 gint result = gtk_dialog_run(GTK_DIALOG(m_widget));
b25630fb 63 gtk_widget_hide(m_widget);
d0ee33f5 64
274ad000
VS
65 switch (result)
66 {
67 default:
68 wxFAIL_MSG(_T("unexpected GtkColorSelectionDialog return code"));
69 // fall through
70
71 case GTK_RESPONSE_CANCEL:
72 case GTK_RESPONSE_DELETE_EVENT:
73 case GTK_RESPONSE_CLOSE:
74 return wxID_CANCEL;
d0ee33f5 75
274ad000
VS
76 case GTK_RESPONSE_OK:
77 DialogToColourData();
78 return wxID_OK;
d0ee33f5 79 }
274ad000
VS
80}
81
82void wxColourDialog::ColourDataToDialog()
83{
d0ee33f5 84 GtkColorSelection *sel =
274ad000 85 GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(m_widget)->colorsel);
d0ee33f5 86
274ad000
VS
87 if (m_data.GetColour().Ok())
88 {
89 gtk_color_selection_set_current_color(sel,
90 m_data.GetColour().GetColor());
91 }
92
93 // setup the palette:
94
95 GdkColor colors[16];
96 gint n_colors = 0;
97 for (unsigned i = 0; i < 16; i++)
98 {
99 wxColour c = m_data.GetCustomColour(i);
100 if (c.Ok())
101 {
102 colors[n_colors] = *c.GetColor();
103 n_colors++;
104 }
105 }
106
b976323b 107 wxGtkString pal(gtk_color_selection_palette_to_string(colors, n_colors));
274ad000
VS
108
109 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
ed97f493 110 g_object_set(settings, "gtk-color-palette", pal.c_str(), NULL);
274ad000
VS
111}
112
113void wxColourDialog::DialogToColourData()
114{
d0ee33f5 115 GtkColorSelection *sel =
274ad000
VS
116 GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(m_widget)->colorsel);
117
118 GdkColor clr;
119 gtk_color_selection_get_current_color(sel, &clr);
120 m_data.SetColour(wxColour(clr.red >> 8, clr.green >> 8, clr.blue >> 8));
d0ee33f5 121
274ad000
VS
122 // Extract custom palette:
123
124 GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(sel));
125 gchar *pal;
126 g_object_get(settings, "gtk-color-palette", &pal, NULL);
127
128 GdkColor *colors;
129 gint n_colors;
130 if (gtk_color_selection_palette_from_string(pal, &colors, &n_colors))
131 {
132 for (int i = 0; i < wxMin(n_colors, 16); i++)
133 {
134 m_data.SetCustomColour(i, wxColour(colors[i].red >> 8,
135 colors[i].green >> 8,
136 colors[i].blue >> 8));
137 }
138 g_free(colors);
139 }
d0ee33f5 140
274ad000
VS
141 g_free(pal);
142}
143
144#endif // wxUSE_COLOURDLG && defined(__WXGTK20__)
145