don't show more than one about dialog (slightly modified patch 1829097)
[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 #ifndef WX_PRECOMP
25 #include "wx/utils.h" // for wxLaunchDefaultBrowser()
26 #endif //WX_PRECOMP
27
28 #include "wx/aboutdlg.h"
29 #include "wx/generic/aboutdlgg.h"
30
31 #include "wx/gtk/private.h"
32
33 // ----------------------------------------------------------------------------
34 // GtkArray: temporary array of GTK strings
35 // ----------------------------------------------------------------------------
36
37 namespace
38 {
39
40 class GtkArray
41 {
42 public:
43 // Create GtkArray from wxArrayString. Note that the created object is
44 // only valid as long as 'a' is!
45 GtkArray(const wxArrayString& a)
46 {
47 m_count = a.size();
48 m_strings = new const gchar *[m_count + 1];
49
50 for ( size_t n = 0; n < m_count; n++ )
51 {
52 #if wxUSE_UNICODE_UTF8
53 m_strings[n] = a[n].utf8_str();
54 #else
55 m_strings[n] = wxGTK_CONV_SYS(a[n]).release();
56 #endif
57 }
58
59 // array must be NULL-terminated
60 m_strings[m_count] = NULL;
61 }
62
63 operator const gchar **() const { return m_strings; }
64
65 ~GtkArray()
66 {
67 #if !wxUSE_UNICODE_UTF8
68 for ( size_t n = 0; n < m_count; n++ )
69 free(wx_const_cast(gchar *, m_strings[n]));
70 #endif
71
72 delete [] m_strings;
73 }
74
75 private:
76 const gchar **m_strings;
77 size_t m_count;
78
79 DECLARE_NO_COPY_CLASS(GtkArray)
80 };
81
82 } // anonymous namespace
83
84 // ============================================================================
85 // implementation
86 // ============================================================================
87
88 // GTK+ about dialog is modeless, keep track of it in this variable
89 static GtkAboutDialog *gs_aboutDialog = NULL;
90
91 extern "C" void
92 wxGtkAboutDialogOnClose(GtkAboutDialog *about)
93 {
94 gtk_widget_destroy(GTK_WIDGET(about));
95 if ( about == gs_aboutDialog )
96 gs_aboutDialog = NULL;
97 }
98
99 extern "C" void
100 wxGtkAboutDialogOnLink(GtkAboutDialog * WXUNUSED(about),
101 const gchar *link,
102 gpointer WXUNUSED(data))
103 {
104 wxLaunchDefaultBrowser(wxGTK_CONV_BACK_SYS(link));
105 }
106
107 void wxAboutBox(const wxAboutDialogInfo& info)
108 {
109 if ( !gtk_check_version(2,6,0) )
110 {
111 // don't create another dialog if one is already present
112 if ( !gs_aboutDialog )
113 gs_aboutDialog = GTK_ABOUT_DIALOG(gtk_about_dialog_new());
114
115 GtkAboutDialog * const dlg = gs_aboutDialog;
116 gtk_about_dialog_set_name(dlg, wxGTK_CONV_SYS(info.GetName()));
117 if ( info.HasVersion() )
118 gtk_about_dialog_set_version(dlg, wxGTK_CONV_SYS(info.GetVersion()));
119 if ( info.HasCopyright() )
120 gtk_about_dialog_set_copyright(dlg, wxGTK_CONV_SYS(info.GetCopyright()));
121 if ( info.HasDescription() )
122 gtk_about_dialog_set_comments(dlg, wxGTK_CONV_SYS(info.GetDescription()));
123 if ( info.HasLicence() )
124 gtk_about_dialog_set_license(dlg, wxGTK_CONV_SYS(info.GetLicence()));
125
126 wxIcon icon = info.GetIcon();
127 if ( icon.Ok() )
128 gtk_about_dialog_set_logo(dlg, info.GetIcon().GetPixbuf());
129
130 if ( info.HasWebSite() )
131 {
132 // NB: must be called before gtk_about_dialog_set_website() as
133 // otherwise it has no effect (although GTK+ docs don't mention
134 // this...)
135 gtk_about_dialog_set_url_hook(wxGtkAboutDialogOnLink, NULL, NULL);
136
137 gtk_about_dialog_set_website(dlg, wxGTK_CONV_SYS(info.GetWebSiteURL()));
138 gtk_about_dialog_set_website_label
139 (
140 dlg,
141 wxGTK_CONV_SYS(info.GetWebSiteDescription())
142 );
143 }
144
145 if ( info.HasDevelopers() )
146 gtk_about_dialog_set_authors(dlg, GtkArray(info.GetDevelopers()));
147 if ( info.HasDocWriters() )
148 gtk_about_dialog_set_documenters(dlg, GtkArray(info.GetDocWriters()));
149 if ( info.HasArtists() )
150 gtk_about_dialog_set_artists(dlg, GtkArray(info.GetArtists()));
151
152 wxString transCredits;
153 if ( info.HasTranslators() )
154 {
155 const wxArrayString& translators = info.GetTranslators();
156 const size_t count = translators.size();
157 for ( size_t n = 0; n < count; n++ )
158 {
159 transCredits << translators[n] << _T('\n');
160 }
161 }
162 else // no translators explicitely specified
163 {
164 // maybe we have translator credits in the message catalog?
165 wxString translator = _("translator-credits");
166
167 // gtk_about_dialog_set_translator_credits() is smart enough to
168 // detect if "translator-credits" is untranslated and hide the
169 // translators tab in that case, however it will still show the
170 // "credits" button, (at least GTK 2.10.6) even if there are no
171 // credits informations at all, so we still need to do the check
172 // ourselves
173 if ( translator != wxT("translator-credits") ) // untranslated!
174 transCredits = translator;
175 }
176
177 if ( !transCredits.empty() )
178 gtk_about_dialog_set_translator_credits(dlg, wxGTK_CONV_SYS(transCredits));
179
180 g_signal_connect(dlg, "response",
181 G_CALLBACK(wxGtkAboutDialogOnClose), NULL);
182
183 gtk_window_present(GTK_WINDOW(dlg));
184 return;
185 }
186
187 // native about dialog not available, fall back to the generic one
188 wxGenericAboutBox(info);
189 }
190
191 #endif // wxUSE_ABOUTDLG && GTK+ 2.6+