]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/artgtk.cpp
Fixes for file dialog. No longer spits out
[wxWidgets.git] / src / gtk1 / artgtk.cpp
CommitLineData
7bebedd8
VS
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"
5d7457f9 26#include "wx/module.h"
7bebedd8
VS
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
43class wxGTK2ArtProvider : public wxArtProvider
44{
45protected:
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
59static 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 ART(wxART_FOLDER, 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 return NULL;
98
99 #undef ART
100}
101
102static GtkIconSize wxArtClientToIconSize(const wxArtClient& client)
103{
104 if (client == wxART_TOOLBAR)
105 return GTK_ICON_SIZE_LARGE_TOOLBAR;
106 else if (client == wxART_MENU)
107 return GTK_ICON_SIZE_MENU;
108 else if (client == wxART_CMN_DIALOG || client == wxART_MESSAGE_BOX)
109 return GTK_ICON_SIZE_DIALOG;
110 else if (client == wxART_BUTTON)
111 return GTK_ICON_SIZE_BUTTON;
112 else
113 return GTK_ICON_SIZE_BUTTON; // this is arbitrary
114}
115
116static GtkIconSize FindClosestIconSize(const wxSize& size)
117{
118 #define NUM_SIZES 6
119 static struct
120 {
121 GtkIconSize icon;
122 gint x, y;
123 } s_sizes[NUM_SIZES];
124 static bool s_sizesInitialized = false;
125
126 if (!s_sizesInitialized)
127 {
128 s_sizes[0].icon = GTK_ICON_SIZE_MENU;
129 s_sizes[1].icon = GTK_ICON_SIZE_SMALL_TOOLBAR;
130 s_sizes[2].icon = GTK_ICON_SIZE_LARGE_TOOLBAR;
131 s_sizes[3].icon = GTK_ICON_SIZE_BUTTON;
132 s_sizes[4].icon = GTK_ICON_SIZE_DND;
133 s_sizes[5].icon = GTK_ICON_SIZE_DIALOG;
134 for (size_t i = 0; i < NUM_SIZES; i++)
135 {
136 gtk_icon_size_lookup(s_sizes[i].icon,
137 &s_sizes[i].x, &s_sizes[i].y);
138 }
139 s_sizesInitialized = true;
140 }
141
142 GtkIconSize best = GTK_ICON_SIZE_DIALOG; // presumably largest
143 unsigned distance = INT_MAX;
144 for (size_t i = 0; i < NUM_SIZES; i++)
145 {
146 // only use larger bitmaps, scaling down looks better than scaling up:
147 if (size.x > s_sizes[i].x || size.y > s_sizes[i].y)
148 continue;
149
150 unsigned dist = (size.x - s_sizes[i].x) * (size.x - s_sizes[i].x) +
151 (size.y - s_sizes[i].y) * (size.y - s_sizes[i].y);
152 if (dist == 0)
153 return s_sizes[i].icon;
154 else if (dist < distance)
155 {
156 distance = dist;
157 best = s_sizes[i].icon;
158 }
159 }
160 return best;
161}
162
5d7457f9
VS
163
164static GtkStyle *gs_gtkStyle = NULL;
165
b5ab476a 166static GdkPixbuf *CreateStockIcon(const char *stockid, GtkIconSize size)
7bebedd8 167{
7bebedd8
VS
168 // FIXME: This code is not 100% correct, because stock pixmap are
169 // context-dependent and may be affected by theme engine, the
170 // correct value can only be obtained for given GtkWidget object.
171 //
172 // Fool-proof implementation of stock bitmaps would extend wxBitmap
173 // with "stock-id" representation (in addition to pixmap and pixbuf
174 // ones) and would convert it to pixbuf when rendered.
175
5d7457f9
VS
176 if (gs_gtkStyle == NULL)
177 {
178 GtkWidget *widget = gtk_button_new();
179 gs_gtkStyle = gtk_rc_get_style(widget);
180 wxASSERT( gs_gtkStyle != NULL );
181 g_object_ref(G_OBJECT(gs_gtkStyle));
182 gtk_widget_destroy(widget);
183 }
184
185 GtkIconSet *iconset = gtk_style_lookup_icon_set(gs_gtkStyle, stockid);
7bebedd8
VS
186
187 if (!iconset)
b5ab476a
VS
188 return NULL;
189
5d7457f9 190 return gtk_icon_set_render_icon(iconset, gs_gtkStyle,
b5ab476a
VS
191 gtk_widget_get_default_direction(),
192 GTK_STATE_NORMAL, size, NULL, NULL);
193}
7bebedd8 194
b5ab476a
VS
195#if GTK_CHECK_VERSION(2,4,0)
196static GdkPixbuf *CreateThemeIcon(const char *iconname,
197 GtkIconSize iconsize, const wxSize& sz)
198{
199 wxSize size(sz);
200 if (size == wxDefaultSize)
201 {
202 gtk_icon_size_lookup(iconsize, &size.x, &size.y);
203 }
204
205 return gtk_icon_theme_load_icon(
206 gtk_icon_theme_get_default(),
207 iconname,
208 size.x,
209 (GtkIconLookupFlags)0, NULL);
210}
211#endif // GTK+ >= 2.4.0
212
213wxBitmap wxGTK2ArtProvider::CreateBitmap(const wxArtID& id,
214 const wxArtClient& client,
215 const wxSize& size)
216{
217 wxCharBuffer stockid = wxArtIDToStock(id);
218 GtkIconSize stocksize = (size == wxDefaultSize) ?
219 wxArtClientToIconSize(client) :
220 FindClosestIconSize(size);
221
222 // allow passing GTK+ stock IDs to wxArtProvider:
223 if (!stockid)
224 stockid = id.ToAscii();
225
226 GdkPixbuf *pixbuf = CreateStockIcon(stockid, stocksize);
227
228#if GTK_CHECK_VERSION(2,4,0)
229 if (!pixbuf)
230 pixbuf = CreateThemeIcon(stockid, stocksize, size);
231#endif
7bebedd8
VS
232
233 if (pixbuf && size != wxDefaultSize &&
234 (size.x != gdk_pixbuf_get_width(pixbuf) ||
235 size.y != gdk_pixbuf_get_height(pixbuf)))
236 {
237 GdkPixbuf *p2 = gdk_pixbuf_scale_simple(pixbuf, size.x, size.y,
238 GDK_INTERP_BILINEAR);
239 if (p2)
240 {
241 gdk_pixbuf_unref(pixbuf);
242 pixbuf = p2;
243 }
244 }
245
246 if (!pixbuf)
247 return wxNullBitmap;
248
249 wxBitmap bmp;
250 bmp.SetWidth(gdk_pixbuf_get_width(pixbuf));
251 bmp.SetHeight(gdk_pixbuf_get_height(pixbuf));
252 bmp.SetPixbuf(pixbuf);
253
254 return bmp;
255}
256
5d7457f9
VS
257// ----------------------------------------------------------------------------
258// Cleanup
259// ----------------------------------------------------------------------------
260
261class wxArtGtkModule: public wxModule
262{
263public:
264 bool OnInit() { return true; }
265 void OnExit()
266 {
267 if (gs_gtkStyle)
268 {
269 g_object_unref(G_OBJECT(gs_gtkStyle));
270 gs_gtkStyle = NULL;
271 }
272 }
273
274 DECLARE_DYNAMIC_CLASS(wxArtGtkModule)
275};
276
277IMPLEMENT_DYNAMIC_CLASS(wxArtGtkModule, wxModule)
278
7bebedd8 279#endif // defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)