]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/artgtk.cpp
Use wxRound() instead of (int)(my_double + 0.5);
[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);
44};
45
46/*static*/ void wxArtProvider::InitNativeProvider()
47{
3a7af48f 48 Push(new wxGTK2ArtProvider);
7bebedd8
VS
49}
50
51// ----------------------------------------------------------------------------
52// CreateBitmap routine
53// ----------------------------------------------------------------------------
54
a158acac
VS
55namespace
56{
57
58const char *wxArtIDToStock(const wxArtID& id)
7bebedd8
VS
59{
60 #define ART(wxid, gtkid) \
61 if (id == wxid) return gtkid;
3d257b8d 62
7bebedd8
VS
63 ART(wxART_ERROR, GTK_STOCK_DIALOG_ERROR)
64 ART(wxART_INFORMATION, GTK_STOCK_DIALOG_INFO)
65 ART(wxART_WARNING, GTK_STOCK_DIALOG_WARNING)
66 ART(wxART_QUESTION, GTK_STOCK_DIALOG_QUESTION)
3d257b8d 67
7bebedd8
VS
68 //ART(wxART_HELP_SIDE_PANEL, )
69 ART(wxART_HELP_SETTINGS, GTK_STOCK_SELECT_FONT)
70 //ART(wxART_HELP_BOOK, )
71 ART(wxART_HELP_FOLDER, GTK_STOCK_DIRECTORY)
72 ART(wxART_HELP_PAGE, GTK_STOCK_FILE)
73 ART(wxART_MISSING_IMAGE, GTK_STOCK_MISSING_IMAGE)
74 ART(wxART_ADD_BOOKMARK, GTK_STOCK_ADD)
75 ART(wxART_DEL_BOOKMARK, GTK_STOCK_REMOVE)
76 ART(wxART_GO_BACK, GTK_STOCK_GO_BACK)
77 ART(wxART_GO_FORWARD, GTK_STOCK_GO_FORWARD)
78 ART(wxART_GO_UP, GTK_STOCK_GO_UP)
79 ART(wxART_GO_DOWN, GTK_STOCK_GO_DOWN)
80 ART(wxART_GO_TO_PARENT, GTK_STOCK_GO_UP)
81 ART(wxART_GO_HOME, GTK_STOCK_HOME)
82 ART(wxART_FILE_OPEN, GTK_STOCK_OPEN)
83 ART(wxART_PRINT, GTK_STOCK_PRINT)
84 ART(wxART_HELP, GTK_STOCK_HELP)
85 ART(wxART_TIP, GTK_STOCK_DIALOG_INFO)
86 //ART(wxART_REPORT_VIEW, )
87 //ART(wxART_LIST_VIEW, )
88 //ART(wxART_NEW_DIR, )
89 ART(wxART_FOLDER, GTK_STOCK_DIRECTORY)
ddf6088b 90 ART(wxART_FOLDER_OPEN, GTK_STOCK_DIRECTORY)
7bebedd8
VS
91 //ART(wxART_GO_DIR_UP, )
92 ART(wxART_EXECUTABLE_FILE, GTK_STOCK_EXECUTE)
93 ART(wxART_NORMAL_FILE, GTK_STOCK_FILE)
94 ART(wxART_TICK_MARK, GTK_STOCK_APPLY)
95 ART(wxART_CROSS_MARK, GTK_STOCK_CANCEL)
ddf6088b 96
ddf6088b
RR
97 ART(wxART_FLOPPY, GTK_STOCK_FLOPPY)
98 ART(wxART_CDROM, GTK_STOCK_CDROM)
99 ART(wxART_HARDDISK, GTK_STOCK_HARDDISK)
100 ART(wxART_REMOVABLE, GTK_STOCK_HARDDISK)
50f65637
VZ
101
102 ART(wxART_FILE_SAVE, GTK_STOCK_SAVE)
103 ART(wxART_FILE_SAVE_AS, GTK_STOCK_SAVE_AS)
104
105 ART(wxART_COPY, GTK_STOCK_COPY)
106 ART(wxART_CUT, GTK_STOCK_CUT)
107 ART(wxART_PASTE, GTK_STOCK_PASTE)
108 ART(wxART_DELETE, GTK_STOCK_DELETE)
8e1c7e63 109 ART(wxART_NEW, GTK_STOCK_NEW)
50f65637
VZ
110
111 ART(wxART_UNDO, GTK_STOCK_UNDO)
112 ART(wxART_REDO, GTK_STOCK_REDO)
113
114 ART(wxART_QUIT, GTK_STOCK_QUIT)
115
116 ART(wxART_FIND, GTK_STOCK_FIND)
117 ART(wxART_FIND_AND_REPLACE, GTK_STOCK_FIND_AND_REPLACE)
3d257b8d 118
7bebedd8 119 return NULL;
3d257b8d 120
7bebedd8
VS
121 #undef ART
122}
123
a158acac 124GtkIconSize ArtClientToIconSize(const wxArtClient& client)
7bebedd8
VS
125{
126 if (client == wxART_TOOLBAR)
127 return GTK_ICON_SIZE_LARGE_TOOLBAR;
128 else if (client == wxART_MENU)
129 return GTK_ICON_SIZE_MENU;
130 else if (client == wxART_CMN_DIALOG || client == wxART_MESSAGE_BOX)
131 return GTK_ICON_SIZE_DIALOG;
132 else if (client == wxART_BUTTON)
133 return GTK_ICON_SIZE_BUTTON;
134 else
e53a95bc 135 return GTK_ICON_SIZE_INVALID; // this is arbitrary
7bebedd8
VS
136}
137
a158acac 138GtkIconSize FindClosestIconSize(const wxSize& size)
7bebedd8
VS
139{
140 #define NUM_SIZES 6
141 static struct
142 {
143 GtkIconSize icon;
144 gint x, y;
145 } s_sizes[NUM_SIZES];
146 static bool s_sizesInitialized = false;
147
148 if (!s_sizesInitialized)
149 {
150 s_sizes[0].icon = GTK_ICON_SIZE_MENU;
151 s_sizes[1].icon = GTK_ICON_SIZE_SMALL_TOOLBAR;
152 s_sizes[2].icon = GTK_ICON_SIZE_LARGE_TOOLBAR;
153 s_sizes[3].icon = GTK_ICON_SIZE_BUTTON;
154 s_sizes[4].icon = GTK_ICON_SIZE_DND;
155 s_sizes[5].icon = GTK_ICON_SIZE_DIALOG;
156 for (size_t i = 0; i < NUM_SIZES; i++)
157 {
abc736fd 158 gtk_icon_size_lookup(s_sizes[i].icon,
34a246df 159 &s_sizes[i].x, &s_sizes[i].y);
7bebedd8
VS
160 }
161 s_sizesInitialized = true;
162 }
163
164 GtkIconSize best = GTK_ICON_SIZE_DIALOG; // presumably largest
165 unsigned distance = INT_MAX;
166 for (size_t i = 0; i < NUM_SIZES; i++)
167 {
168 // only use larger bitmaps, scaling down looks better than scaling up:
169 if (size.x > s_sizes[i].x || size.y > s_sizes[i].y)
170 continue;
3d257b8d 171
02761f6c 172 unsigned dist = (size.x - s_sizes[i].x) * (size.x - s_sizes[i].x) +
7bebedd8
VS
173 (size.y - s_sizes[i].y) * (size.y - s_sizes[i].y);
174 if (dist == 0)
175 return s_sizes[i].icon;
176 else if (dist < distance)
177 {
178 distance = dist;
179 best = s_sizes[i].icon;
3d257b8d 180 }
7bebedd8
VS
181 }
182 return best;
183}
184
a158acac 185GdkPixbuf *CreateStockIcon(const char *stockid, GtkIconSize size)
7bebedd8 186{
7bebedd8
VS
187 // FIXME: This code is not 100% correct, because stock pixmap are
188 // context-dependent and may be affected by theme engine, the
189 // correct value can only be obtained for given GtkWidget object.
3d257b8d 190 //
7bebedd8
VS
191 // Fool-proof implementation of stock bitmaps would extend wxBitmap
192 // with "stock-id" representation (in addition to pixmap and pixbuf
193 // ones) and would convert it to pixbuf when rendered.
3d257b8d 194
e261c9e2
PC
195 GtkStyle* style = wxGTKPrivate::GetButtonWidget()->style;
196 GtkIconSet* iconset = gtk_style_lookup_icon_set(style, stockid);
7bebedd8
VS
197
198 if (!iconset)
b5ab476a
VS
199 return NULL;
200
e261c9e2 201 return gtk_icon_set_render_icon(iconset, style,
b5ab476a
VS
202 gtk_widget_get_default_direction(),
203 GTK_STATE_NORMAL, size, NULL, NULL);
204}
7bebedd8 205
a158acac 206GdkPixbuf *CreateThemeIcon(const char *iconname,
b5ab476a
VS
207 GtkIconSize iconsize, const wxSize& sz)
208{
209 wxSize size(sz);
210 if (size == wxDefaultSize)
211 {
212 gtk_icon_size_lookup(iconsize, &size.x, &size.y);
213 }
3d257b8d 214
b5ab476a
VS
215 return gtk_icon_theme_load_icon(
216 gtk_icon_theme_get_default(),
217 iconname,
218 size.x,
219 (GtkIconLookupFlags)0, NULL);
220}
b5ab476a 221
a158acac
VS
222} // anonymous namespace
223
b5ab476a
VS
224wxBitmap wxGTK2ArtProvider::CreateBitmap(const wxArtID& id,
225 const wxArtClient& client,
226 const wxSize& size)
227{
228 wxCharBuffer stockid = wxArtIDToStock(id);
229 GtkIconSize stocksize = (size == wxDefaultSize) ?
a158acac 230 ArtClientToIconSize(client) :
b5ab476a
VS
231 FindClosestIconSize(size);
232
e53a95bc
RR
233 // we must have some size, this is arbitrary
234 if (stocksize == GTK_ICON_SIZE_INVALID)
235 stocksize = GTK_ICON_SIZE_BUTTON;
236
b5ab476a
VS
237 // allow passing GTK+ stock IDs to wxArtProvider:
238 if (!stockid)
239 stockid = id.ToAscii();
240
241 GdkPixbuf *pixbuf = CreateStockIcon(stockid, stocksize);
ff654490 242 if (!pixbuf)
3a7af48f 243 pixbuf = CreateThemeIcon(stockid, stocksize, size);
7bebedd8
VS
244
245 if (pixbuf && size != wxDefaultSize &&
246 (size.x != gdk_pixbuf_get_width(pixbuf) ||
247 size.y != gdk_pixbuf_get_height(pixbuf)))
248 {
249 GdkPixbuf *p2 = gdk_pixbuf_scale_simple(pixbuf, size.x, size.y,
250 GDK_INTERP_BILINEAR);
251 if (p2)
252 {
3fe39b0c 253 g_object_unref (pixbuf);
7bebedd8
VS
254 pixbuf = p2;
255 }
256 }
3d257b8d 257
7bebedd8 258 wxBitmap bmp;
3a7af48f
PC
259 if (pixbuf != NULL)
260 bmp.SetPixbuf(pixbuf);
7bebedd8
VS
261
262 return bmp;
263}
264
a158acac
VS
265// ----------------------------------------------------------------------------
266// wxArtProvider::GetNativeSizeHint()
267// ----------------------------------------------------------------------------
268
269/*static*/
270wxSize wxArtProvider::GetNativeSizeHint(const wxArtClient& client)
271{
272 // Gtk has specific sizes for each client, see artgtk.cpp
273 GtkIconSize gtk_size = ArtClientToIconSize(client);
274 // no size hints for this client
275 if (gtk_size == GTK_ICON_SIZE_INVALID)
276 return wxDefaultSize;
277 gint width, height;
278 gtk_icon_size_lookup( gtk_size, &width, &height);
279 return wxSize(width, height);
280}