]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/artgtk.cpp
better fix for #11803, don't set iconized state for hidden window
[wxWidgets.git] / src / gtk / artgtk.cpp
CommitLineData
7bebedd8 1/////////////////////////////////////////////////////////////////////////////
02761f6c 2// Name: src/gtk/artstd.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{
3a7af48f 50 Push(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)
84 ART(wxART_FILE_OPEN, GTK_STOCK_OPEN)
85 ART(wxART_PRINT, GTK_STOCK_PRINT)
86 ART(wxART_HELP, GTK_STOCK_HELP)
87 ART(wxART_TIP, GTK_STOCK_DIALOG_INFO)
88 //ART(wxART_REPORT_VIEW, )
89 //ART(wxART_LIST_VIEW, )
90 //ART(wxART_NEW_DIR, )
91 ART(wxART_FOLDER, GTK_STOCK_DIRECTORY)
ddf6088b 92 ART(wxART_FOLDER_OPEN, GTK_STOCK_DIRECTORY)
7bebedd8
VS
93 //ART(wxART_GO_DIR_UP, )
94 ART(wxART_EXECUTABLE_FILE, GTK_STOCK_EXECUTE)
95 ART(wxART_NORMAL_FILE, GTK_STOCK_FILE)
96 ART(wxART_TICK_MARK, GTK_STOCK_APPLY)
97 ART(wxART_CROSS_MARK, GTK_STOCK_CANCEL)
ddf6088b 98
ddf6088b
RR
99 ART(wxART_FLOPPY, GTK_STOCK_FLOPPY)
100 ART(wxART_CDROM, GTK_STOCK_CDROM)
101 ART(wxART_HARDDISK, GTK_STOCK_HARDDISK)
102 ART(wxART_REMOVABLE, GTK_STOCK_HARDDISK)
50f65637
VZ
103
104 ART(wxART_FILE_SAVE, GTK_STOCK_SAVE)
105 ART(wxART_FILE_SAVE_AS, GTK_STOCK_SAVE_AS)
106
107 ART(wxART_COPY, GTK_STOCK_COPY)
108 ART(wxART_CUT, GTK_STOCK_CUT)
109 ART(wxART_PASTE, GTK_STOCK_PASTE)
110 ART(wxART_DELETE, GTK_STOCK_DELETE)
8e1c7e63 111 ART(wxART_NEW, GTK_STOCK_NEW)
50f65637
VZ
112
113 ART(wxART_UNDO, GTK_STOCK_UNDO)
114 ART(wxART_REDO, GTK_STOCK_REDO)
115
b9727751 116 ART(wxART_CLOSE, GTK_STOCK_CLOSE)
50f65637
VZ
117 ART(wxART_QUIT, GTK_STOCK_QUIT)
118
119 ART(wxART_FIND, GTK_STOCK_FIND)
120 ART(wxART_FIND_AND_REPLACE, GTK_STOCK_FIND_AND_REPLACE)
3d257b8d 121
7bebedd8 122 #undef ART
7b7fd3e6
VS
123
124 // allow passing GTK+ stock IDs to wxArtProvider -- if a recognized wx
125 // ID wasn't found, pass it to GTK+ in the hope it is a GTK+ or theme
126 // icon name:
785b6373 127 return id;
7bebedd8
VS
128}
129
a158acac 130GtkIconSize ArtClientToIconSize(const wxArtClient& client)
7bebedd8
VS
131{
132 if (client == wxART_TOOLBAR)
133 return GTK_ICON_SIZE_LARGE_TOOLBAR;
7b7fd3e6 134 else if (client == wxART_MENU || client == wxART_FRAME_ICON)
7bebedd8
VS
135 return GTK_ICON_SIZE_MENU;
136 else if (client == wxART_CMN_DIALOG || client == wxART_MESSAGE_BOX)
137 return GTK_ICON_SIZE_DIALOG;
138 else if (client == wxART_BUTTON)
139 return GTK_ICON_SIZE_BUTTON;
140 else
e53a95bc 141 return GTK_ICON_SIZE_INVALID; // this is arbitrary
7bebedd8
VS
142}
143
a158acac 144GtkIconSize FindClosestIconSize(const wxSize& size)
7bebedd8
VS
145{
146 #define NUM_SIZES 6
147 static struct
148 {
149 GtkIconSize icon;
150 gint x, y;
151 } s_sizes[NUM_SIZES];
152 static bool s_sizesInitialized = false;
153
154 if (!s_sizesInitialized)
155 {
156 s_sizes[0].icon = GTK_ICON_SIZE_MENU;
157 s_sizes[1].icon = GTK_ICON_SIZE_SMALL_TOOLBAR;
158 s_sizes[2].icon = GTK_ICON_SIZE_LARGE_TOOLBAR;
159 s_sizes[3].icon = GTK_ICON_SIZE_BUTTON;
160 s_sizes[4].icon = GTK_ICON_SIZE_DND;
161 s_sizes[5].icon = GTK_ICON_SIZE_DIALOG;
162 for (size_t i = 0; i < NUM_SIZES; i++)
163 {
abc736fd 164 gtk_icon_size_lookup(s_sizes[i].icon,
34a246df 165 &s_sizes[i].x, &s_sizes[i].y);
7bebedd8
VS
166 }
167 s_sizesInitialized = true;
168 }
169
170 GtkIconSize best = GTK_ICON_SIZE_DIALOG; // presumably largest
171 unsigned distance = INT_MAX;
172 for (size_t i = 0; i < NUM_SIZES; i++)
173 {
174 // only use larger bitmaps, scaling down looks better than scaling up:
175 if (size.x > s_sizes[i].x || size.y > s_sizes[i].y)
176 continue;
3d257b8d 177
02761f6c 178 unsigned dist = (size.x - s_sizes[i].x) * (size.x - s_sizes[i].x) +
7bebedd8
VS
179 (size.y - s_sizes[i].y) * (size.y - s_sizes[i].y);
180 if (dist == 0)
181 return s_sizes[i].icon;
182 else if (dist < distance)
183 {
184 distance = dist;
185 best = s_sizes[i].icon;
3d257b8d 186 }
7bebedd8
VS
187 }
188 return best;
189}
190
a158acac 191GdkPixbuf *CreateStockIcon(const char *stockid, GtkIconSize size)
7bebedd8 192{
7bebedd8
VS
193 // FIXME: This code is not 100% correct, because stock pixmap are
194 // context-dependent and may be affected by theme engine, the
195 // correct value can only be obtained for given GtkWidget object.
3d257b8d 196 //
7bebedd8
VS
197 // Fool-proof implementation of stock bitmaps would extend wxBitmap
198 // with "stock-id" representation (in addition to pixmap and pixbuf
199 // ones) and would convert it to pixbuf when rendered.
3d257b8d 200
e261c9e2
PC
201 GtkStyle* style = wxGTKPrivate::GetButtonWidget()->style;
202 GtkIconSet* iconset = gtk_style_lookup_icon_set(style, stockid);
7bebedd8
VS
203
204 if (!iconset)
b5ab476a
VS
205 return NULL;
206
e261c9e2 207 return gtk_icon_set_render_icon(iconset, style,
b5ab476a
VS
208 gtk_widget_get_default_direction(),
209 GTK_STATE_NORMAL, size, NULL, NULL);
210}
7bebedd8 211
7b7fd3e6
VS
212GdkPixbuf *CreateThemeIcon(const char *iconname, int size)
213{
214 return gtk_icon_theme_load_icon
215 (
216 gtk_icon_theme_get_default(),
217 iconname,
218 size,
219 (GtkIconLookupFlags)0,
220 NULL
221 );
222}
223
224
225// creates either stock or theme icon
226GdkPixbuf *CreateGtkIcon(const char *icon_name,
227 GtkIconSize stock_size, const wxSize& pixel_size)
228{
229 // try stock GTK+ icon first
230 GdkPixbuf *pixbuf = CreateStockIcon(icon_name, stock_size);
231 if ( pixbuf )
232 return pixbuf;
233
234 // if that fails, try theme icon
235 wxSize size(pixel_size);
236 if ( pixel_size == wxDefaultSize )
237 gtk_icon_size_lookup(stock_size, &size.x, &size.y);
238 return CreateThemeIcon(icon_name, size.x);
239}
240
241template<typename SizeType, typename LoaderFunc>
242wxIconBundle DoCreateIconBundle(const char *stockid,
243 const SizeType *sizes_from,
244 const SizeType *sizes_to,
245 LoaderFunc get_icon)
246
b5ab476a 247{
7b7fd3e6
VS
248 wxIconBundle bundle;
249
250 for ( const SizeType *i = sizes_from; i != sizes_to; ++i )
b5ab476a 251 {
7b7fd3e6
VS
252 GdkPixbuf *pixbuf = get_icon(stockid, *i);
253 if ( !pixbuf )
254 continue;
255
256 wxIcon icon;
257 icon.SetPixbuf(pixbuf);
258 bundle.AddIcon(icon);
b5ab476a 259 }
3d257b8d 260
7b7fd3e6 261 return bundle;
b5ab476a 262}
b5ab476a 263
a158acac
VS
264} // anonymous namespace
265
b5ab476a
VS
266wxBitmap wxGTK2ArtProvider::CreateBitmap(const wxArtID& id,
267 const wxArtClient& client,
268 const wxSize& size)
269{
785b6373 270 const wxString stockid = wxArtIDToStock(id);
7b7fd3e6 271
b5ab476a 272 GtkIconSize stocksize = (size == wxDefaultSize) ?
a158acac 273 ArtClientToIconSize(client) :
b5ab476a 274 FindClosestIconSize(size);
e53a95bc
RR
275 // we must have some size, this is arbitrary
276 if (stocksize == GTK_ICON_SIZE_INVALID)
277 stocksize = GTK_ICON_SIZE_BUTTON;
278
785b6373 279 GdkPixbuf *pixbuf = CreateGtkIcon(stockid.utf8_str(), stocksize, size);
7bebedd8
VS
280
281 if (pixbuf && size != wxDefaultSize &&
282 (size.x != gdk_pixbuf_get_width(pixbuf) ||
283 size.y != gdk_pixbuf_get_height(pixbuf)))
284 {
285 GdkPixbuf *p2 = gdk_pixbuf_scale_simple(pixbuf, size.x, size.y,
286 GDK_INTERP_BILINEAR);
287 if (p2)
288 {
3fe39b0c 289 g_object_unref (pixbuf);
7bebedd8
VS
290 pixbuf = p2;
291 }
292 }
3d257b8d 293
7bebedd8 294 wxBitmap bmp;
3a7af48f
PC
295 if (pixbuf != NULL)
296 bmp.SetPixbuf(pixbuf);
7bebedd8
VS
297
298 return bmp;
299}
300
7b7fd3e6
VS
301wxIconBundle
302wxGTK2ArtProvider::CreateIconBundle(const wxArtID& id,
303 const wxArtClient& WXUNUSED(client))
304{
785b6373 305 const wxString stockid = wxArtIDToStock(id);
7b7fd3e6
VS
306
307 // try to load the bundle as stock icon first
308 GtkStyle* style = wxGTKPrivate::GetButtonWidget()->style;
785b6373 309 GtkIconSet* iconset = gtk_style_lookup_icon_set(style, stockid.utf8_str());
7b7fd3e6
VS
310 if ( iconset )
311 {
312 GtkIconSize *sizes;
313 gint n_sizes;
314 gtk_icon_set_get_sizes(iconset, &sizes, &n_sizes);
315 wxIconBundle bundle = DoCreateIconBundle
316 (
785b6373 317 stockid.utf8_str(),
7b7fd3e6
VS
318 sizes, sizes + n_sizes,
319 &CreateStockIcon
320 );
321 g_free(sizes);
322 return bundle;
323 }
324
325 // otherwise try icon themes
326#ifdef __WXGTK26__
327 if ( !gtk_check_version(2,6,0) )
328 {
329 gint *sizes = gtk_icon_theme_get_icon_sizes
330 (
331 gtk_icon_theme_get_default(),
785b6373 332 stockid.utf8_str()
7b7fd3e6
VS
333 );
334 if ( !sizes )
335 return wxNullIconBundle;
336
337 gint *last = sizes;
338 while ( *last )
339 last++;
340
341 wxIconBundle bundle = DoCreateIconBundle
342 (
785b6373 343 stockid.utf8_str(),
7b7fd3e6
VS
344 sizes, last,
345 &CreateThemeIcon
346 );
347 g_free(sizes);
348 return bundle;
349 }
350#endif // __WXGTK26__
351
352 return wxNullIconBundle;
353}
354
a158acac
VS
355// ----------------------------------------------------------------------------
356// wxArtProvider::GetNativeSizeHint()
357// ----------------------------------------------------------------------------
358
359/*static*/
360wxSize wxArtProvider::GetNativeSizeHint(const wxArtClient& client)
361{
362 // Gtk has specific sizes for each client, see artgtk.cpp
363 GtkIconSize gtk_size = ArtClientToIconSize(client);
364 // no size hints for this client
365 if (gtk_size == GTK_ICON_SIZE_INVALID)
366 return wxDefaultSize;
367 gint width, height;
368 gtk_icon_size_lookup( gtk_size, &width, &height);
369 return wxSize(width, height);
370}