]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/artgtk.cpp
remove superfluous assert
[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
23#if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
24
25#include "wx/artprov.h"
02761f6c
WS
26
27#ifndef WX_PRECOMP
28 #include "wx/module.h"
29#endif
30
abc736fd 31#include "wx/gtk/private.h"
7bebedd8
VS
32
33#include <gtk/gtk.h>
34
35// compatibility with older GTK+ versions:
36#ifndef GTK_STOCK_FILE
37 #define GTK_STOCK_FILE "gtk-file"
38#endif
39#ifndef GTK_STOCK_DIRECTORY
40 #define GTK_STOCK_DIRECTORY "gtk-directory"
41#endif
42
43
44// ----------------------------------------------------------------------------
45// wxGTK2ArtProvider
46// ----------------------------------------------------------------------------
47
48class wxGTK2ArtProvider : public wxArtProvider
49{
50protected:
51 virtual wxBitmap CreateBitmap(const wxArtID& id, const wxArtClient& client,
52 const wxSize& size);
53};
54
55/*static*/ void wxArtProvider::InitNativeProvider()
56{
571d2e0f 57 wxArtProvider::Push(new wxGTK2ArtProvider);
7bebedd8
VS
58}
59
60// ----------------------------------------------------------------------------
61// CreateBitmap routine
62// ----------------------------------------------------------------------------
63
64static const char *wxArtIDToStock(const wxArtID& id)
65{
66 #define ART(wxid, gtkid) \
67 if (id == wxid) return gtkid;
3d257b8d 68
7bebedd8
VS
69 ART(wxART_ERROR, GTK_STOCK_DIALOG_ERROR)
70 ART(wxART_INFORMATION, GTK_STOCK_DIALOG_INFO)
71 ART(wxART_WARNING, GTK_STOCK_DIALOG_WARNING)
72 ART(wxART_QUESTION, GTK_STOCK_DIALOG_QUESTION)
3d257b8d 73
7bebedd8
VS
74 //ART(wxART_HELP_SIDE_PANEL, )
75 ART(wxART_HELP_SETTINGS, GTK_STOCK_SELECT_FONT)
76 //ART(wxART_HELP_BOOK, )
77 ART(wxART_HELP_FOLDER, GTK_STOCK_DIRECTORY)
78 ART(wxART_HELP_PAGE, GTK_STOCK_FILE)
79 ART(wxART_MISSING_IMAGE, GTK_STOCK_MISSING_IMAGE)
80 ART(wxART_ADD_BOOKMARK, GTK_STOCK_ADD)
81 ART(wxART_DEL_BOOKMARK, GTK_STOCK_REMOVE)
82 ART(wxART_GO_BACK, GTK_STOCK_GO_BACK)
83 ART(wxART_GO_FORWARD, GTK_STOCK_GO_FORWARD)
84 ART(wxART_GO_UP, GTK_STOCK_GO_UP)
85 ART(wxART_GO_DOWN, GTK_STOCK_GO_DOWN)
86 ART(wxART_GO_TO_PARENT, GTK_STOCK_GO_UP)
87 ART(wxART_GO_HOME, GTK_STOCK_HOME)
88 ART(wxART_FILE_OPEN, GTK_STOCK_OPEN)
89 ART(wxART_PRINT, GTK_STOCK_PRINT)
90 ART(wxART_HELP, GTK_STOCK_HELP)
91 ART(wxART_TIP, GTK_STOCK_DIALOG_INFO)
92 //ART(wxART_REPORT_VIEW, )
93 //ART(wxART_LIST_VIEW, )
94 //ART(wxART_NEW_DIR, )
ddf6088b 95#ifdef __WXGTK24__
7bebedd8 96 ART(wxART_FOLDER, GTK_STOCK_DIRECTORY)
ddf6088b
RR
97 ART(wxART_FOLDER_OPEN, GTK_STOCK_DIRECTORY)
98#endif
7bebedd8
VS
99 //ART(wxART_GO_DIR_UP, )
100 ART(wxART_EXECUTABLE_FILE, GTK_STOCK_EXECUTE)
101 ART(wxART_NORMAL_FILE, GTK_STOCK_FILE)
102 ART(wxART_TICK_MARK, GTK_STOCK_APPLY)
103 ART(wxART_CROSS_MARK, GTK_STOCK_CANCEL)
ddf6088b
RR
104
105#ifdef __WXGTK24__
106 ART(wxART_FLOPPY, GTK_STOCK_FLOPPY)
107 ART(wxART_CDROM, GTK_STOCK_CDROM)
108 ART(wxART_HARDDISK, GTK_STOCK_HARDDISK)
109 ART(wxART_REMOVABLE, GTK_STOCK_HARDDISK)
50f65637
VZ
110
111 ART(wxART_FILE_SAVE, GTK_STOCK_SAVE)
112 ART(wxART_FILE_SAVE_AS, GTK_STOCK_SAVE_AS)
113
114 ART(wxART_COPY, GTK_STOCK_COPY)
115 ART(wxART_CUT, GTK_STOCK_CUT)
116 ART(wxART_PASTE, GTK_STOCK_PASTE)
117 ART(wxART_DELETE, GTK_STOCK_DELETE)
8e1c7e63 118 ART(wxART_NEW, GTK_STOCK_NEW)
50f65637
VZ
119
120 ART(wxART_UNDO, GTK_STOCK_UNDO)
121 ART(wxART_REDO, GTK_STOCK_REDO)
122
123 ART(wxART_QUIT, GTK_STOCK_QUIT)
124
125 ART(wxART_FIND, GTK_STOCK_FIND)
126 ART(wxART_FIND_AND_REPLACE, GTK_STOCK_FIND_AND_REPLACE)
ddf6088b 127#endif
3d257b8d 128
7bebedd8 129 return NULL;
3d257b8d 130
7bebedd8
VS
131 #undef ART
132}
133
b737ad10 134GtkIconSize wxArtClientToIconSize(const wxArtClient& client)
7bebedd8
VS
135{
136 if (client == wxART_TOOLBAR)
137 return GTK_ICON_SIZE_LARGE_TOOLBAR;
138 else if (client == wxART_MENU)
139 return GTK_ICON_SIZE_MENU;
140 else if (client == wxART_CMN_DIALOG || client == wxART_MESSAGE_BOX)
141 return GTK_ICON_SIZE_DIALOG;
142 else if (client == wxART_BUTTON)
143 return GTK_ICON_SIZE_BUTTON;
144 else
e53a95bc 145 return GTK_ICON_SIZE_INVALID; // this is arbitrary
7bebedd8
VS
146}
147
148static GtkIconSize FindClosestIconSize(const wxSize& size)
149{
150 #define NUM_SIZES 6
151 static struct
152 {
153 GtkIconSize icon;
154 gint x, y;
155 } s_sizes[NUM_SIZES];
156 static bool s_sizesInitialized = false;
157
158 if (!s_sizesInitialized)
159 {
160 s_sizes[0].icon = GTK_ICON_SIZE_MENU;
161 s_sizes[1].icon = GTK_ICON_SIZE_SMALL_TOOLBAR;
162 s_sizes[2].icon = GTK_ICON_SIZE_LARGE_TOOLBAR;
163 s_sizes[3].icon = GTK_ICON_SIZE_BUTTON;
164 s_sizes[4].icon = GTK_ICON_SIZE_DND;
165 s_sizes[5].icon = GTK_ICON_SIZE_DIALOG;
166 for (size_t i = 0; i < NUM_SIZES; i++)
167 {
abc736fd 168 gtk_icon_size_lookup(s_sizes[i].icon,
34a246df 169 &s_sizes[i].x, &s_sizes[i].y);
7bebedd8
VS
170 }
171 s_sizesInitialized = true;
172 }
173
174 GtkIconSize best = GTK_ICON_SIZE_DIALOG; // presumably largest
175 unsigned distance = INT_MAX;
176 for (size_t i = 0; i < NUM_SIZES; i++)
177 {
178 // only use larger bitmaps, scaling down looks better than scaling up:
179 if (size.x > s_sizes[i].x || size.y > s_sizes[i].y)
180 continue;
3d257b8d 181
02761f6c 182 unsigned dist = (size.x - s_sizes[i].x) * (size.x - s_sizes[i].x) +
7bebedd8
VS
183 (size.y - s_sizes[i].y) * (size.y - s_sizes[i].y);
184 if (dist == 0)
185 return s_sizes[i].icon;
186 else if (dist < distance)
187 {
188 distance = dist;
189 best = s_sizes[i].icon;
3d257b8d 190 }
7bebedd8
VS
191 }
192 return best;
193}
194
5d7457f9
VS
195
196static GtkStyle *gs_gtkStyle = NULL;
197
b5ab476a 198static GdkPixbuf *CreateStockIcon(const char *stockid, GtkIconSize size)
7bebedd8 199{
7bebedd8
VS
200 // FIXME: This code is not 100% correct, because stock pixmap are
201 // context-dependent and may be affected by theme engine, the
202 // correct value can only be obtained for given GtkWidget object.
3d257b8d 203 //
7bebedd8
VS
204 // Fool-proof implementation of stock bitmaps would extend wxBitmap
205 // with "stock-id" representation (in addition to pixmap and pixbuf
206 // ones) and would convert it to pixbuf when rendered.
3d257b8d 207
5d7457f9
VS
208 if (gs_gtkStyle == NULL)
209 {
210 GtkWidget *widget = gtk_button_new();
211 gs_gtkStyle = gtk_rc_get_style(widget);
212 wxASSERT( gs_gtkStyle != NULL );
3fe39b0c 213 g_object_ref(gs_gtkStyle);
5d7457f9
VS
214 gtk_widget_destroy(widget);
215 }
216
217 GtkIconSet *iconset = gtk_style_lookup_icon_set(gs_gtkStyle, stockid);
7bebedd8
VS
218
219 if (!iconset)
b5ab476a
VS
220 return NULL;
221
5d7457f9 222 return gtk_icon_set_render_icon(iconset, gs_gtkStyle,
b5ab476a
VS
223 gtk_widget_get_default_direction(),
224 GTK_STATE_NORMAL, size, NULL, NULL);
225}
7bebedd8 226
77f70672 227#ifdef __WXGTK24__
b5ab476a
VS
228static GdkPixbuf *CreateThemeIcon(const char *iconname,
229 GtkIconSize iconsize, const wxSize& sz)
230{
231 wxSize size(sz);
232 if (size == wxDefaultSize)
233 {
234 gtk_icon_size_lookup(iconsize, &size.x, &size.y);
235 }
3d257b8d 236
b5ab476a
VS
237 return gtk_icon_theme_load_icon(
238 gtk_icon_theme_get_default(),
239 iconname,
240 size.x,
241 (GtkIconLookupFlags)0, NULL);
242}
77f70672 243#endif
b5ab476a
VS
244
245wxBitmap wxGTK2ArtProvider::CreateBitmap(const wxArtID& id,
246 const wxArtClient& client,
247 const wxSize& size)
248{
249 wxCharBuffer stockid = wxArtIDToStock(id);
250 GtkIconSize stocksize = (size == wxDefaultSize) ?
3d257b8d 251 wxArtClientToIconSize(client) :
b5ab476a
VS
252 FindClosestIconSize(size);
253
e53a95bc
RR
254 // we must have some size, this is arbitrary
255 if (stocksize == GTK_ICON_SIZE_INVALID)
256 stocksize = GTK_ICON_SIZE_BUTTON;
257
b5ab476a
VS
258 // allow passing GTK+ stock IDs to wxArtProvider:
259 if (!stockid)
260 stockid = id.ToAscii();
261
262 GdkPixbuf *pixbuf = CreateStockIcon(stockid, stocksize);
263
77f70672
RR
264#ifdef __WXGTK24__
265 if (!gtk_check_version(2,4,0))
266 {
267 if (!pixbuf)
268 pixbuf = CreateThemeIcon(stockid, stocksize, size);
269 }
b5ab476a 270#endif
7bebedd8
VS
271
272 if (pixbuf && size != wxDefaultSize &&
273 (size.x != gdk_pixbuf_get_width(pixbuf) ||
274 size.y != gdk_pixbuf_get_height(pixbuf)))
275 {
276 GdkPixbuf *p2 = gdk_pixbuf_scale_simple(pixbuf, size.x, size.y,
277 GDK_INTERP_BILINEAR);
278 if (p2)
279 {
3fe39b0c 280 g_object_unref (pixbuf);
7bebedd8
VS
281 pixbuf = p2;
282 }
283 }
3d257b8d 284
7bebedd8
VS
285 if (!pixbuf)
286 return wxNullBitmap;
287
288 wxBitmap bmp;
289 bmp.SetWidth(gdk_pixbuf_get_width(pixbuf));
290 bmp.SetHeight(gdk_pixbuf_get_height(pixbuf));
291 bmp.SetPixbuf(pixbuf);
292
293 return bmp;
294}
295
5d7457f9
VS
296// ----------------------------------------------------------------------------
297// Cleanup
298// ----------------------------------------------------------------------------
299
300class wxArtGtkModule: public wxModule
301{
302public:
303 bool OnInit() { return true; }
304 void OnExit()
305 {
306 if (gs_gtkStyle)
307 {
3fe39b0c 308 g_object_unref(gs_gtkStyle);
5d7457f9
VS
309 gs_gtkStyle = NULL;
310 }
311 }
312
313 DECLARE_DYNAMIC_CLASS(wxArtGtkModule)
314};
315
316IMPLEMENT_DYNAMIC_CLASS(wxArtGtkModule, wxModule)
317
7bebedd8 318#endif // defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)