]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/artgtk.cpp
fix warnings about unreachable return statements
[wxWidgets.git] / src / gtk / artgtk.cpp
CommitLineData
7bebedd8 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/gtk/artgtk.cpp
7bebedd8
VS
3// Purpose: stock wxArtProvider instance with native GTK+ stock icons
4// Author: Vaclav Slavik
5// Modified by:
6// Created: 2004-08-22
7// RCS-ID: $Id$
8// Copyright: (c) Vaclav Slavik, 2004
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ---------------------------------------------------------------------------
13// headers
14// ---------------------------------------------------------------------------
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#if defined(__BORLANDC__)
20 #pragma hdrstop
21#endif
22
7bebedd8 23#include "wx/artprov.h"
ca5bf83b 24#include "wx/gtk/private.h"
02761f6c 25
7bebedd8
VS
26// compatibility with older GTK+ versions:
27#ifndef GTK_STOCK_FILE
28 #define GTK_STOCK_FILE "gtk-file"
29#endif
30#ifndef GTK_STOCK_DIRECTORY
31 #define GTK_STOCK_DIRECTORY "gtk-directory"
32#endif
33
34
35// ----------------------------------------------------------------------------
36// wxGTK2ArtProvider
37// ----------------------------------------------------------------------------
38
39class wxGTK2ArtProvider : public wxArtProvider
40{
41protected:
42 virtual wxBitmap CreateBitmap(const wxArtID& id, const wxArtClient& client,
43 const wxSize& size);
7b7fd3e6
VS
44 virtual wxIconBundle CreateIconBundle(const wxArtID& id,
45 const wxArtClient& client);
7bebedd8
VS
46};
47
48/*static*/ void wxArtProvider::InitNativeProvider()
49{
0d68cd13 50 PushBack(new wxGTK2ArtProvider);
7bebedd8
VS
51}
52
53// ----------------------------------------------------------------------------
54// CreateBitmap routine
55// ----------------------------------------------------------------------------
56
a158acac
VS
57namespace
58{
59
785b6373 60wxString wxArtIDToStock(const wxArtID& id)
7bebedd8
VS
61{
62 #define ART(wxid, gtkid) \
785b6373 63 if (id == wxid) return gtkid;
3d257b8d 64
7bebedd8
VS
65 ART(wxART_ERROR, GTK_STOCK_DIALOG_ERROR)
66 ART(wxART_INFORMATION, GTK_STOCK_DIALOG_INFO)
67 ART(wxART_WARNING, GTK_STOCK_DIALOG_WARNING)
68 ART(wxART_QUESTION, GTK_STOCK_DIALOG_QUESTION)
3d257b8d 69
7bebedd8
VS
70 //ART(wxART_HELP_SIDE_PANEL, )
71 ART(wxART_HELP_SETTINGS, GTK_STOCK_SELECT_FONT)
72 //ART(wxART_HELP_BOOK, )
73 ART(wxART_HELP_FOLDER, GTK_STOCK_DIRECTORY)
74 ART(wxART_HELP_PAGE, GTK_STOCK_FILE)
75 ART(wxART_MISSING_IMAGE, GTK_STOCK_MISSING_IMAGE)
76 ART(wxART_ADD_BOOKMARK, GTK_STOCK_ADD)
77 ART(wxART_DEL_BOOKMARK, GTK_STOCK_REMOVE)
78 ART(wxART_GO_BACK, GTK_STOCK_GO_BACK)
79 ART(wxART_GO_FORWARD, GTK_STOCK_GO_FORWARD)
80 ART(wxART_GO_UP, GTK_STOCK_GO_UP)
81 ART(wxART_GO_DOWN, GTK_STOCK_GO_DOWN)
82 ART(wxART_GO_TO_PARENT, GTK_STOCK_GO_UP)
83 ART(wxART_GO_HOME, GTK_STOCK_HOME)
a7cfad3a
VZ
84 ART(wxART_GOTO_FIRST, GTK_STOCK_GOTO_FIRST)
85 ART(wxART_GOTO_LAST, GTK_STOCK_GOTO_LAST)
7bebedd8
VS
86 ART(wxART_FILE_OPEN, GTK_STOCK_OPEN)
87 ART(wxART_PRINT, GTK_STOCK_PRINT)
88 ART(wxART_HELP, GTK_STOCK_HELP)
89 ART(wxART_TIP, GTK_STOCK_DIALOG_INFO)
90 //ART(wxART_REPORT_VIEW, )
91 //ART(wxART_LIST_VIEW, )
92 //ART(wxART_NEW_DIR, )
93 ART(wxART_FOLDER, GTK_STOCK_DIRECTORY)
ddf6088b 94 ART(wxART_FOLDER_OPEN, GTK_STOCK_DIRECTORY)
7bebedd8
VS
95 //ART(wxART_GO_DIR_UP, )
96 ART(wxART_EXECUTABLE_FILE, GTK_STOCK_EXECUTE)
97 ART(wxART_NORMAL_FILE, GTK_STOCK_FILE)
98 ART(wxART_TICK_MARK, GTK_STOCK_APPLY)
99 ART(wxART_CROSS_MARK, GTK_STOCK_CANCEL)
ddf6088b 100
ddf6088b
RR
101 ART(wxART_FLOPPY, GTK_STOCK_FLOPPY)
102 ART(wxART_CDROM, GTK_STOCK_CDROM)
103 ART(wxART_HARDDISK, GTK_STOCK_HARDDISK)
104 ART(wxART_REMOVABLE, GTK_STOCK_HARDDISK)
50f65637
VZ
105
106 ART(wxART_FILE_SAVE, GTK_STOCK_SAVE)
107 ART(wxART_FILE_SAVE_AS, GTK_STOCK_SAVE_AS)
108
109 ART(wxART_COPY, GTK_STOCK_COPY)
110 ART(wxART_CUT, GTK_STOCK_CUT)
111 ART(wxART_PASTE, GTK_STOCK_PASTE)
112 ART(wxART_DELETE, GTK_STOCK_DELETE)
8e1c7e63 113 ART(wxART_NEW, GTK_STOCK_NEW)
50f65637
VZ
114
115 ART(wxART_UNDO, GTK_STOCK_UNDO)
116 ART(wxART_REDO, GTK_STOCK_REDO)
117
a7cfad3a
VZ
118 ART(wxART_PLUS, GTK_STOCK_ADD)
119 ART(wxART_MINUS, GTK_STOCK_REMOVE)
120
b9727751 121 ART(wxART_CLOSE, GTK_STOCK_CLOSE)
50f65637
VZ
122 ART(wxART_QUIT, GTK_STOCK_QUIT)
123
124 ART(wxART_FIND, GTK_STOCK_FIND)
125 ART(wxART_FIND_AND_REPLACE, GTK_STOCK_FIND_AND_REPLACE)
3d257b8d 126
7bebedd8 127 #undef ART
7b7fd3e6
VS
128
129 // allow passing GTK+ stock IDs to wxArtProvider -- if a recognized wx
130 // ID wasn't found, pass it to GTK+ in the hope it is a GTK+ or theme
131 // icon name:
785b6373 132 return id;
7bebedd8
VS
133}
134
a158acac 135GtkIconSize ArtClientToIconSize(const wxArtClient& client)
7bebedd8
VS
136{
137 if (client == wxART_TOOLBAR)
138 return GTK_ICON_SIZE_LARGE_TOOLBAR;
7b7fd3e6 139 else if (client == wxART_MENU || client == wxART_FRAME_ICON)
7bebedd8
VS
140 return GTK_ICON_SIZE_MENU;
141 else if (client == wxART_CMN_DIALOG || client == wxART_MESSAGE_BOX)
142 return GTK_ICON_SIZE_DIALOG;
143 else if (client == wxART_BUTTON)
144 return GTK_ICON_SIZE_BUTTON;
145 else
e53a95bc 146 return GTK_ICON_SIZE_INVALID; // this is arbitrary
7bebedd8
VS
147}
148
a158acac 149GtkIconSize FindClosestIconSize(const wxSize& size)
7bebedd8
VS
150{
151 #define NUM_SIZES 6
152 static struct
153 {
154 GtkIconSize icon;
155 gint x, y;
156 } s_sizes[NUM_SIZES];
157 static bool s_sizesInitialized = false;
158
159 if (!s_sizesInitialized)
160 {
161 s_sizes[0].icon = GTK_ICON_SIZE_MENU;
162 s_sizes[1].icon = GTK_ICON_SIZE_SMALL_TOOLBAR;
163 s_sizes[2].icon = GTK_ICON_SIZE_LARGE_TOOLBAR;
164 s_sizes[3].icon = GTK_ICON_SIZE_BUTTON;
165 s_sizes[4].icon = GTK_ICON_SIZE_DND;
166 s_sizes[5].icon = GTK_ICON_SIZE_DIALOG;
167 for (size_t i = 0; i < NUM_SIZES; i++)
168 {
abc736fd 169 gtk_icon_size_lookup(s_sizes[i].icon,
34a246df 170 &s_sizes[i].x, &s_sizes[i].y);
7bebedd8
VS
171 }
172 s_sizesInitialized = true;
173 }
174
175 GtkIconSize best = GTK_ICON_SIZE_DIALOG; // presumably largest
176 unsigned distance = INT_MAX;
177 for (size_t i = 0; i < NUM_SIZES; i++)
178 {
179 // only use larger bitmaps, scaling down looks better than scaling up:
180 if (size.x > s_sizes[i].x || size.y > s_sizes[i].y)
181 continue;
3d257b8d 182
02761f6c 183 unsigned dist = (size.x - s_sizes[i].x) * (size.x - s_sizes[i].x) +
7bebedd8
VS
184 (size.y - s_sizes[i].y) * (size.y - s_sizes[i].y);
185 if (dist == 0)
186 return s_sizes[i].icon;
187 else if (dist < distance)
188 {
189 distance = dist;
190 best = s_sizes[i].icon;
3d257b8d 191 }
7bebedd8
VS
192 }
193 return best;
194}
195
a158acac 196GdkPixbuf *CreateStockIcon(const char *stockid, GtkIconSize size)
7bebedd8 197{
7bebedd8
VS
198 // FIXME: This code is not 100% correct, because stock pixmap are
199 // context-dependent and may be affected by theme engine, the
200 // correct value can only be obtained for given GtkWidget object.
3d257b8d 201 //
7bebedd8
VS
202 // Fool-proof implementation of stock bitmaps would extend wxBitmap
203 // with "stock-id" representation (in addition to pixmap and pixbuf
204 // ones) and would convert it to pixbuf when rendered.
3d257b8d 205
385e8575 206 GtkStyle* style = gtk_widget_get_style(wxGTKPrivate::GetButtonWidget());
e261c9e2 207 GtkIconSet* iconset = gtk_style_lookup_icon_set(style, stockid);
7bebedd8
VS
208
209 if (!iconset)
b5ab476a
VS
210 return NULL;
211
e261c9e2 212 return gtk_icon_set_render_icon(iconset, style,
b5ab476a
VS
213 gtk_widget_get_default_direction(),
214 GTK_STATE_NORMAL, size, NULL, NULL);
215}
7bebedd8 216
7b7fd3e6
VS
217GdkPixbuf *CreateThemeIcon(const char *iconname, int size)
218{
219 return gtk_icon_theme_load_icon
220 (
221 gtk_icon_theme_get_default(),
222 iconname,
223 size,
224 (GtkIconLookupFlags)0,
225 NULL
226 );
227}
228
229
230// creates either stock or theme icon
231GdkPixbuf *CreateGtkIcon(const char *icon_name,
232 GtkIconSize stock_size, const wxSize& pixel_size)
233{
234 // try stock GTK+ icon first
235 GdkPixbuf *pixbuf = CreateStockIcon(icon_name, stock_size);
236 if ( pixbuf )
237 return pixbuf;
238
239 // if that fails, try theme icon
240 wxSize size(pixel_size);
241 if ( pixel_size == wxDefaultSize )
242 gtk_icon_size_lookup(stock_size, &size.x, &size.y);
243 return CreateThemeIcon(icon_name, size.x);
244}
245
246template<typename SizeType, typename LoaderFunc>
247wxIconBundle DoCreateIconBundle(const char *stockid,
248 const SizeType *sizes_from,
249 const SizeType *sizes_to,
250 LoaderFunc get_icon)
251
b5ab476a 252{
7b7fd3e6
VS
253 wxIconBundle bundle;
254
255 for ( const SizeType *i = sizes_from; i != sizes_to; ++i )
b5ab476a 256 {
7b7fd3e6
VS
257 GdkPixbuf *pixbuf = get_icon(stockid, *i);
258 if ( !pixbuf )
259 continue;
260
261 wxIcon icon;
54195d23 262 icon.CopyFromBitmap(wxBitmap(pixbuf));
7b7fd3e6 263 bundle.AddIcon(icon);
b5ab476a 264 }
3d257b8d 265
7b7fd3e6 266 return bundle;
b5ab476a 267}
b5ab476a 268
a158acac
VS
269} // anonymous namespace
270
b5ab476a
VS
271wxBitmap wxGTK2ArtProvider::CreateBitmap(const wxArtID& id,
272 const wxArtClient& client,
273 const wxSize& size)
274{
785b6373 275 const wxString stockid = wxArtIDToStock(id);
7b7fd3e6 276
b5ab476a 277 GtkIconSize stocksize = (size == wxDefaultSize) ?
a158acac 278 ArtClientToIconSize(client) :
b5ab476a 279 FindClosestIconSize(size);
e53a95bc
RR
280 // we must have some size, this is arbitrary
281 if (stocksize == GTK_ICON_SIZE_INVALID)
282 stocksize = GTK_ICON_SIZE_BUTTON;
283
785b6373 284 GdkPixbuf *pixbuf = CreateGtkIcon(stockid.utf8_str(), stocksize, size);
7bebedd8
VS
285
286 if (pixbuf && size != wxDefaultSize &&
287 (size.x != gdk_pixbuf_get_width(pixbuf) ||
288 size.y != gdk_pixbuf_get_height(pixbuf)))
289 {
290 GdkPixbuf *p2 = gdk_pixbuf_scale_simple(pixbuf, size.x, size.y,
291 GDK_INTERP_BILINEAR);
292 if (p2)
293 {
3fe39b0c 294 g_object_unref (pixbuf);
7bebedd8
VS
295 pixbuf = p2;
296 }
297 }
3d257b8d 298
54195d23 299 return wxBitmap(pixbuf);
7bebedd8
VS
300}
301
7b7fd3e6
VS
302wxIconBundle
303wxGTK2ArtProvider::CreateIconBundle(const wxArtID& id,
304 const wxArtClient& WXUNUSED(client))
305{
785b6373 306 const wxString stockid = wxArtIDToStock(id);
7b7fd3e6
VS
307
308 // try to load the bundle as stock icon first
385e8575 309 GtkStyle* style = gtk_widget_get_style(wxGTKPrivate::GetButtonWidget());
785b6373 310 GtkIconSet* iconset = gtk_style_lookup_icon_set(style, stockid.utf8_str());
7b7fd3e6
VS
311 if ( iconset )
312 {
313 GtkIconSize *sizes;
314 gint n_sizes;
315 gtk_icon_set_get_sizes(iconset, &sizes, &n_sizes);
316 wxIconBundle bundle = DoCreateIconBundle
317 (
785b6373 318 stockid.utf8_str(),
7b7fd3e6
VS
319 sizes, sizes + n_sizes,
320 &CreateStockIcon
321 );
322 g_free(sizes);
323 return bundle;
324 }
325
326 // otherwise try icon themes
327#ifdef __WXGTK26__
328 if ( !gtk_check_version(2,6,0) )
329 {
330 gint *sizes = gtk_icon_theme_get_icon_sizes
331 (
332 gtk_icon_theme_get_default(),
785b6373 333 stockid.utf8_str()
7b7fd3e6
VS
334 );
335 if ( !sizes )
336 return wxNullIconBundle;
337
338 gint *last = sizes;
339 while ( *last )
340 last++;
341
342 wxIconBundle bundle = DoCreateIconBundle
343 (
785b6373 344 stockid.utf8_str(),
7b7fd3e6
VS
345 sizes, last,
346 &CreateThemeIcon
347 );
348 g_free(sizes);
349 return bundle;
350 }
351#endif // __WXGTK26__
352
353 return wxNullIconBundle;
354}
355
a158acac
VS
356// ----------------------------------------------------------------------------
357// wxArtProvider::GetNativeSizeHint()
358// ----------------------------------------------------------------------------
359
360/*static*/
361wxSize wxArtProvider::GetNativeSizeHint(const wxArtClient& client)
362{
363 // Gtk has specific sizes for each client, see artgtk.cpp
364 GtkIconSize gtk_size = ArtClientToIconSize(client);
365 // no size hints for this client
366 if (gtk_size == GTK_ICON_SIZE_INVALID)
367 return wxDefaultSize;
368 gint width, height;
369 gtk_icon_size_lookup( gtk_size, &width, &height);
370 return wxSize(width, height);
371}