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