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