]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/artgtk.cpp
fixing setting initial value under osx_cocoa for single line text controls
[wxWidgets.git] / src / gtk / artgtk.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/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 #include "wx/artprov.h"
24 #include "wx/gtk/private.h"
25
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
39 class wxGTK2ArtProvider : public wxArtProvider
40 {
41 protected:
42 virtual wxBitmap CreateBitmap(const wxArtID& id, const wxArtClient& client,
43 const wxSize& size);
44 virtual wxIconBundle CreateIconBundle(const wxArtID& id,
45 const wxArtClient& client);
46 };
47
48 /*static*/ void wxArtProvider::InitNativeProvider()
49 {
50 Push(new wxGTK2ArtProvider);
51 }
52
53 // ----------------------------------------------------------------------------
54 // CreateBitmap routine
55 // ----------------------------------------------------------------------------
56
57 namespace
58 {
59
60 wxString wxArtIDToStock(const wxArtID& id)
61 {
62 #define ART(wxid, gtkid) \
63 if (id == wxid) return gtkid;
64
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)
69
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)
92 ART(wxART_FOLDER_OPEN, GTK_STOCK_DIRECTORY)
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)
98
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)
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)
111 ART(wxART_NEW, GTK_STOCK_NEW)
112
113 ART(wxART_UNDO, GTK_STOCK_UNDO)
114 ART(wxART_REDO, GTK_STOCK_REDO)
115
116 ART(wxART_QUIT, GTK_STOCK_QUIT)
117
118 ART(wxART_FIND, GTK_STOCK_FIND)
119 ART(wxART_FIND_AND_REPLACE, GTK_STOCK_FIND_AND_REPLACE)
120
121 #undef ART
122
123 // allow passing GTK+ stock IDs to wxArtProvider -- if a recognized wx
124 // ID wasn't found, pass it to GTK+ in the hope it is a GTK+ or theme
125 // icon name:
126 return id;
127 }
128
129 GtkIconSize ArtClientToIconSize(const wxArtClient& client)
130 {
131 if (client == wxART_TOOLBAR)
132 return GTK_ICON_SIZE_LARGE_TOOLBAR;
133 else if (client == wxART_MENU || client == wxART_FRAME_ICON)
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 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 gtk_icon_size_lookup(s_sizes[i].icon,
164 &s_sizes[i].x, &s_sizes[i].y);
165 }
166 s_sizesInitialized = true;
167 }
168
169 GtkIconSize best = GTK_ICON_SIZE_DIALOG; // presumably largest
170 unsigned distance = INT_MAX;
171 for (size_t i = 0; i < NUM_SIZES; i++)
172 {
173 // only use larger bitmaps, scaling down looks better than scaling up:
174 if (size.x > s_sizes[i].x || size.y > s_sizes[i].y)
175 continue;
176
177 unsigned dist = (size.x - s_sizes[i].x) * (size.x - s_sizes[i].x) +
178 (size.y - s_sizes[i].y) * (size.y - s_sizes[i].y);
179 if (dist == 0)
180 return s_sizes[i].icon;
181 else if (dist < distance)
182 {
183 distance = dist;
184 best = s_sizes[i].icon;
185 }
186 }
187 return best;
188 }
189
190 GdkPixbuf *CreateStockIcon(const char *stockid, GtkIconSize size)
191 {
192 // FIXME: This code is not 100% correct, because stock pixmap are
193 // context-dependent and may be affected by theme engine, the
194 // correct value can only be obtained for given GtkWidget object.
195 //
196 // Fool-proof implementation of stock bitmaps would extend wxBitmap
197 // with "stock-id" representation (in addition to pixmap and pixbuf
198 // ones) and would convert it to pixbuf when rendered.
199
200 GtkStyle* style = wxGTKPrivate::GetButtonWidget()->style;
201 GtkIconSet* iconset = gtk_style_lookup_icon_set(style, stockid);
202
203 if (!iconset)
204 return NULL;
205
206 return gtk_icon_set_render_icon(iconset, style,
207 gtk_widget_get_default_direction(),
208 GTK_STATE_NORMAL, size, NULL, NULL);
209 }
210
211 GdkPixbuf *CreateThemeIcon(const char *iconname, int size)
212 {
213 return gtk_icon_theme_load_icon
214 (
215 gtk_icon_theme_get_default(),
216 iconname,
217 size,
218 (GtkIconLookupFlags)0,
219 NULL
220 );
221 }
222
223
224 // creates either stock or theme icon
225 GdkPixbuf *CreateGtkIcon(const char *icon_name,
226 GtkIconSize stock_size, const wxSize& pixel_size)
227 {
228 // try stock GTK+ icon first
229 GdkPixbuf *pixbuf = CreateStockIcon(icon_name, stock_size);
230 if ( pixbuf )
231 return pixbuf;
232
233 // if that fails, try theme icon
234 wxSize size(pixel_size);
235 if ( pixel_size == wxDefaultSize )
236 gtk_icon_size_lookup(stock_size, &size.x, &size.y);
237 return CreateThemeIcon(icon_name, size.x);
238 }
239
240 template<typename SizeType, typename LoaderFunc>
241 wxIconBundle DoCreateIconBundle(const char *stockid,
242 const SizeType *sizes_from,
243 const SizeType *sizes_to,
244 LoaderFunc get_icon)
245
246 {
247 wxIconBundle bundle;
248
249 for ( const SizeType *i = sizes_from; i != sizes_to; ++i )
250 {
251 GdkPixbuf *pixbuf = get_icon(stockid, *i);
252 if ( !pixbuf )
253 continue;
254
255 wxIcon icon;
256 icon.SetPixbuf(pixbuf);
257 bundle.AddIcon(icon);
258 }
259
260 return bundle;
261 }
262
263 } // anonymous namespace
264
265 wxBitmap wxGTK2ArtProvider::CreateBitmap(const wxArtID& id,
266 const wxArtClient& client,
267 const wxSize& size)
268 {
269 const wxString stockid = wxArtIDToStock(id);
270
271 GtkIconSize stocksize = (size == wxDefaultSize) ?
272 ArtClientToIconSize(client) :
273 FindClosestIconSize(size);
274 // we must have some size, this is arbitrary
275 if (stocksize == GTK_ICON_SIZE_INVALID)
276 stocksize = GTK_ICON_SIZE_BUTTON;
277
278 GdkPixbuf *pixbuf = CreateGtkIcon(stockid.utf8_str(), stocksize, size);
279
280 if (pixbuf && size != wxDefaultSize &&
281 (size.x != gdk_pixbuf_get_width(pixbuf) ||
282 size.y != gdk_pixbuf_get_height(pixbuf)))
283 {
284 GdkPixbuf *p2 = gdk_pixbuf_scale_simple(pixbuf, size.x, size.y,
285 GDK_INTERP_BILINEAR);
286 if (p2)
287 {
288 g_object_unref (pixbuf);
289 pixbuf = p2;
290 }
291 }
292
293 wxBitmap bmp;
294 if (pixbuf != NULL)
295 bmp.SetPixbuf(pixbuf);
296
297 return bmp;
298 }
299
300 wxIconBundle
301 wxGTK2ArtProvider::CreateIconBundle(const wxArtID& id,
302 const wxArtClient& WXUNUSED(client))
303 {
304 const wxString stockid = wxArtIDToStock(id);
305
306 // try to load the bundle as stock icon first
307 GtkStyle* style = wxGTKPrivate::GetButtonWidget()->style;
308 GtkIconSet* iconset = gtk_style_lookup_icon_set(style, stockid.utf8_str());
309 if ( iconset )
310 {
311 GtkIconSize *sizes;
312 gint n_sizes;
313 gtk_icon_set_get_sizes(iconset, &sizes, &n_sizes);
314 wxIconBundle bundle = DoCreateIconBundle
315 (
316 stockid.utf8_str(),
317 sizes, sizes + n_sizes,
318 &CreateStockIcon
319 );
320 g_free(sizes);
321 return bundle;
322 }
323
324 // otherwise try icon themes
325 #ifdef __WXGTK26__
326 if ( !gtk_check_version(2,6,0) )
327 {
328 gint *sizes = gtk_icon_theme_get_icon_sizes
329 (
330 gtk_icon_theme_get_default(),
331 stockid.utf8_str()
332 );
333 if ( !sizes )
334 return wxNullIconBundle;
335
336 gint *last = sizes;
337 while ( *last )
338 last++;
339
340 wxIconBundle bundle = DoCreateIconBundle
341 (
342 stockid.utf8_str(),
343 sizes, last,
344 &CreateThemeIcon
345 );
346 g_free(sizes);
347 return bundle;
348 }
349 #endif // __WXGTK26__
350
351 return wxNullIconBundle;
352 }
353
354 // ----------------------------------------------------------------------------
355 // wxArtProvider::GetNativeSizeHint()
356 // ----------------------------------------------------------------------------
357
358 /*static*/
359 wxSize wxArtProvider::GetNativeSizeHint(const wxArtClient& client)
360 {
361 // Gtk has specific sizes for each client, see artgtk.cpp
362 GtkIconSize gtk_size = ArtClientToIconSize(client);
363 // no size hints for this client
364 if (gtk_size == GTK_ICON_SIZE_INVALID)
365 return wxDefaultSize;
366 gint width, height;
367 gtk_icon_size_lookup( gtk_size, &width, &height);
368 return wxSize(width, height);
369 }