]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/artgtk.cpp
Give wxCollapsiblePane's pane a name for easier debugging
[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 };
45
46 /*static*/ void wxArtProvider::InitNativeProvider()
47 {
48 Push(new wxGTK2ArtProvider);
49 }
50
51 // ----------------------------------------------------------------------------
52 // CreateBitmap routine
53 // ----------------------------------------------------------------------------
54
55 namespace
56 {
57
58 const char *wxArtIDToStock(const wxArtID& id)
59 {
60 #define ART(wxid, gtkid) \
61 if (id == wxid) return gtkid;
62
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)
67
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)
90 ART(wxART_FOLDER_OPEN, GTK_STOCK_DIRECTORY)
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)
96
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)
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)
109 ART(wxART_NEW, GTK_STOCK_NEW)
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)
118
119 return NULL;
120
121 #undef ART
122 }
123
124 GtkIconSize ArtClientToIconSize(const wxArtClient& client)
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
135 return GTK_ICON_SIZE_INVALID; // this is arbitrary
136 }
137
138 GtkIconSize FindClosestIconSize(const wxSize& size)
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 {
158 gtk_icon_size_lookup(s_sizes[i].icon,
159 &s_sizes[i].x, &s_sizes[i].y);
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;
171
172 unsigned dist = (size.x - s_sizes[i].x) * (size.x - s_sizes[i].x) +
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;
180 }
181 }
182 return best;
183 }
184
185 GdkPixbuf *CreateStockIcon(const char *stockid, GtkIconSize size)
186 {
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.
190 //
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.
194
195 GtkStyle* style = wxGTKPrivate::GetButtonWidget()->style;
196 GtkIconSet* iconset = gtk_style_lookup_icon_set(style, stockid);
197
198 if (!iconset)
199 return NULL;
200
201 return gtk_icon_set_render_icon(iconset, style,
202 gtk_widget_get_default_direction(),
203 GTK_STATE_NORMAL, size, NULL, NULL);
204 }
205
206 GdkPixbuf *CreateThemeIcon(const char *iconname,
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 }
214
215 return gtk_icon_theme_load_icon(
216 gtk_icon_theme_get_default(),
217 iconname,
218 size.x,
219 (GtkIconLookupFlags)0, NULL);
220 }
221
222 } // anonymous namespace
223
224 wxBitmap wxGTK2ArtProvider::CreateBitmap(const wxArtID& id,
225 const wxArtClient& client,
226 const wxSize& size)
227 {
228 wxCharBuffer stockid = wxArtIDToStock(id);
229 GtkIconSize stocksize = (size == wxDefaultSize) ?
230 ArtClientToIconSize(client) :
231 FindClosestIconSize(size);
232
233 // we must have some size, this is arbitrary
234 if (stocksize == GTK_ICON_SIZE_INVALID)
235 stocksize = GTK_ICON_SIZE_BUTTON;
236
237 // allow passing GTK+ stock IDs to wxArtProvider:
238 if (!stockid)
239 stockid = id.ToAscii();
240
241 GdkPixbuf *pixbuf = CreateStockIcon(stockid, stocksize);
242 if (!pixbuf)
243 pixbuf = CreateThemeIcon(stockid, stocksize, size);
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 {
253 g_object_unref (pixbuf);
254 pixbuf = p2;
255 }
256 }
257
258 wxBitmap bmp;
259 if (pixbuf != NULL)
260 bmp.SetPixbuf(pixbuf);
261
262 return bmp;
263 }
264
265 // ----------------------------------------------------------------------------
266 // wxArtProvider::GetNativeSizeHint()
267 // ----------------------------------------------------------------------------
268
269 /*static*/
270 wxSize 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 }