support for GTK3
[wxWidgets.git] / src / gtk / aboutdlg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/aboutdlg.cpp
3 // Purpose: native GTK+ wxAboutBox() implementation
4 // Author: Vadim Zeitlin
5 // Created: 2006-10-08
6 // RCS-ID: $Id$
7 // Copyright: (c) 2006 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #if wxUSE_ABOUTDLG && defined(__WXGTK26__)
23
24 #include "wx/aboutdlg.h"
25
26 #ifndef WX_PRECOMP
27 #include "wx/utils.h" // for wxLaunchDefaultBrowser()
28 #endif //WX_PRECOMP
29
30 #ifndef __WXGTK3__
31 #include "wx/generic/aboutdlgg.h"
32 #endif
33
34 #include <gtk/gtk.h>
35 #include "wx/gtk/private.h"
36 #include "wx/gtk/private/gtk2-compat.h"
37
38 // ----------------------------------------------------------------------------
39 // GtkArray: temporary array of GTK strings
40 // ----------------------------------------------------------------------------
41
42 namespace
43 {
44
45 class GtkArray
46 {
47 public:
48 // Create empty GtkArray
49 GtkArray() : m_strings(0), m_count(0)
50 {
51 }
52
53 // Create GtkArray from wxArrayString. Note that the created object is
54 // only valid as long as 'a' is!
55 GtkArray(const wxArrayString& a)
56 {
57 m_count = a.size();
58 m_strings = new const gchar *[m_count + 1];
59
60 for ( size_t n = 0; n < m_count; n++ )
61 {
62 #if wxUSE_UNICODE
63 // notice that there is no need to copy the string pointer here
64 // because this class is used only as a temporary and during its
65 // existence the pointer persists in wxString which uses it either
66 // for internal representation (in wxUSE_UNICODE_UTF8 case) or as
67 // cached m_convertedToChar (in wxUSE_UNICODE_WCHAR case)
68 m_strings[n] = wxGTK_CONV_SYS(a[n]);
69 #else // !wxUSE_UNICODE
70 // and in ANSI build we can simply borrow the pointer from
71 // wxCharBuffer (which owns it in this case) instead of copying it
72 // but we then become responsible for freeing it
73 m_strings[n] = wxGTK_CONV_SYS(a[n]).release();
74 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
75 }
76
77 // array must be NULL-terminated
78 m_strings[m_count] = NULL;
79 }
80
81 operator const gchar **() const { return m_strings; }
82
83 ~GtkArray()
84 {
85 #if !wxUSE_UNICODE
86 for ( size_t n = 0; n < m_count; n++ )
87 free(const_cast<gchar *>(m_strings[n]));
88 #endif
89
90 delete [] m_strings;
91 }
92
93 private:
94 const gchar **m_strings;
95 size_t m_count;
96
97 wxDECLARE_NO_COPY_CLASS(GtkArray);
98 };
99
100 } // anonymous namespace
101
102 // ============================================================================
103 // implementation
104 // ============================================================================
105
106 // GTK+ about dialog is modeless, keep track of it in this variable
107 static GtkAboutDialog *gs_aboutDialog = NULL;
108
109 extern "C" {
110 static void wxGtkAboutDialogOnClose(GtkAboutDialog *about)
111 {
112 gtk_widget_destroy(GTK_WIDGET(about));
113 if ( about == gs_aboutDialog )
114 gs_aboutDialog = NULL;
115 }
116 }
117
118 #ifdef __WXGTK3__
119 extern "C" {
120 static gboolean activate_link(GtkAboutDialog*, const char* link, void* dontIgnore)
121 {
122 if (dontIgnore)
123 {
124 wxLaunchDefaultBrowser(wxGTK_CONV_BACK_SYS(link));
125 return true;
126 }
127 return false;
128 }
129 }
130 #else
131 extern "C" {
132 static void wxGtkAboutDialogOnLink(GtkAboutDialog*, const char* link, void*)
133 {
134 wxLaunchDefaultBrowser(wxGTK_CONV_BACK_SYS(link));
135 }
136 }
137 #endif
138
139 void wxAboutBox(const wxAboutDialogInfo& info, wxWindow* WXUNUSED(parent))
140 {
141 #ifndef __WXGTK3__
142 if ( !gtk_check_version(2,6,0) )
143 #endif
144 {
145 // don't create another dialog if one is already present
146 if ( !gs_aboutDialog )
147 gs_aboutDialog = GTK_ABOUT_DIALOG(gtk_about_dialog_new());
148
149 GtkAboutDialog * const dlg = gs_aboutDialog;
150 gtk_about_dialog_set_program_name(dlg, wxGTK_CONV_SYS(info.GetName()));
151 if ( info.HasVersion() )
152 gtk_about_dialog_set_version(dlg, wxGTK_CONV_SYS(info.GetVersion()));
153 else
154 gtk_about_dialog_set_version(dlg, NULL);
155 if ( info.HasCopyright() )
156 gtk_about_dialog_set_copyright(dlg, wxGTK_CONV_SYS(info.GetCopyrightToDisplay()));
157 else
158 gtk_about_dialog_set_copyright(dlg, NULL);
159 if ( info.HasDescription() )
160 gtk_about_dialog_set_comments(dlg, wxGTK_CONV_SYS(info.GetDescription()));
161 else
162 gtk_about_dialog_set_comments(dlg, NULL);
163 if ( info.HasLicence() )
164 gtk_about_dialog_set_license(dlg, wxGTK_CONV_SYS(info.GetLicence()));
165 else
166 gtk_about_dialog_set_license(dlg, NULL);
167
168 wxIcon icon = info.GetIcon();
169 if ( icon.IsOk() )
170 gtk_about_dialog_set_logo(dlg, info.GetIcon().GetPixbuf());
171
172 if ( info.HasWebSite() )
173 {
174 #ifdef __WXGTK3__
175 g_signal_connect(dlg, "activate-link", G_CALLBACK(activate_link), dlg);
176 #else
177 // NB: must be called before gtk_about_dialog_set_website() as
178 // otherwise it has no effect (although GTK+ docs don't mention
179 // this...)
180 gtk_about_dialog_set_url_hook(wxGtkAboutDialogOnLink, NULL, NULL);
181 #endif
182
183 gtk_about_dialog_set_website(dlg, wxGTK_CONV_SYS(info.GetWebSiteURL()));
184 gtk_about_dialog_set_website_label
185 (
186 dlg,
187 wxGTK_CONV_SYS(info.GetWebSiteDescription())
188 );
189 }
190 else
191 {
192 gtk_about_dialog_set_website(dlg, NULL);
193 gtk_about_dialog_set_website_label(dlg, NULL);
194 #ifdef __WXGTK3__
195 g_signal_connect(dlg, "activate-link", G_CALLBACK(activate_link), NULL);
196 #else
197 gtk_about_dialog_set_url_hook(NULL, NULL, NULL);
198 #endif
199 }
200
201 if ( info.HasDevelopers() )
202 gtk_about_dialog_set_authors(dlg, GtkArray(info.GetDevelopers()));
203 else
204 gtk_about_dialog_set_authors(dlg, GtkArray());
205 if ( info.HasDocWriters() )
206 gtk_about_dialog_set_documenters(dlg, GtkArray(info.GetDocWriters()));
207 else
208 gtk_about_dialog_set_documenters(dlg, GtkArray());
209 if ( info.HasArtists() )
210 gtk_about_dialog_set_artists(dlg, GtkArray(info.GetArtists()));
211 else
212 gtk_about_dialog_set_artists(dlg, GtkArray());
213
214 wxString transCredits;
215 if ( info.HasTranslators() )
216 {
217 const wxArrayString& translators = info.GetTranslators();
218 const size_t count = translators.size();
219 for ( size_t n = 0; n < count; n++ )
220 {
221 transCredits << translators[n] << wxT('\n');
222 }
223 }
224 else // no translators explicitly specified
225 {
226 // maybe we have translator credits in the message catalog?
227 wxString translator = _("translator-credits");
228
229 // gtk_about_dialog_set_translator_credits() is smart enough to
230 // detect if "translator-credits" is untranslated and hide the
231 // translators tab in that case, however it will still show the
232 // "credits" button, (at least GTK 2.10.6) even if there are no
233 // credits informations at all, so we still need to do the check
234 // ourselves
235 if ( translator != wxT("translator-credits") ) // untranslated!
236 transCredits = translator;
237 }
238
239 if ( !transCredits.empty() )
240 gtk_about_dialog_set_translator_credits(dlg, wxGTK_CONV_SYS(transCredits));
241 else
242 gtk_about_dialog_set_translator_credits(dlg, NULL);
243
244 g_signal_connect(dlg, "response",
245 G_CALLBACK(wxGtkAboutDialogOnClose), NULL);
246
247 gtk_window_present(GTK_WINDOW(dlg));
248 }
249 #ifndef __WXGTK3__
250 else
251 // native about dialog not available, fall back to the generic one
252 wxGenericAboutBox(info);
253 #endif
254 }
255
256 #endif // wxUSE_ABOUTDLG && GTK+ 2.6+