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