1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/artstd.cpp
3 // Purpose: stock wxArtProvider instance with native GTK+ stock icons
4 // Author: Vaclav Slavik
8 // Copyright: (c) Vaclav Slavik, 2004
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ---------------------------------------------------------------------------
14 // ---------------------------------------------------------------------------
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
19 #if defined(__BORLANDC__)
23 #include "wx/artprov.h"
24 #include "wx/gtk/private.h"
26 // compatibility with older GTK+ versions:
27 #ifndef GTK_STOCK_FILE
28 #define GTK_STOCK_FILE "gtk-file"
30 #ifndef GTK_STOCK_DIRECTORY
31 #define GTK_STOCK_DIRECTORY "gtk-directory"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 class wxGTK2ArtProvider
: public wxArtProvider
42 virtual wxBitmap
CreateBitmap(const wxArtID
& id
, const wxArtClient
& client
,
46 /*static*/ void wxArtProvider::InitNativeProvider()
48 Push(new wxGTK2ArtProvider
);
51 // ----------------------------------------------------------------------------
52 // CreateBitmap routine
53 // ----------------------------------------------------------------------------
58 const char *wxArtIDToStock(const wxArtID
& id
)
60 #define ART(wxid, gtkid) \
61 if (id == wxid) return gtkid;
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
)
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
)
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
)
102 ART(wxART_FILE_SAVE
, GTK_STOCK_SAVE
)
103 ART(wxART_FILE_SAVE_AS
, GTK_STOCK_SAVE_AS
)
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
)
111 ART(wxART_UNDO
, GTK_STOCK_UNDO
)
112 ART(wxART_REDO
, GTK_STOCK_REDO
)
114 ART(wxART_QUIT
, GTK_STOCK_QUIT
)
116 ART(wxART_FIND
, GTK_STOCK_FIND
)
117 ART(wxART_FIND_AND_REPLACE
, GTK_STOCK_FIND_AND_REPLACE
)
124 GtkIconSize
ArtClientToIconSize(const wxArtClient
& client
)
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
;
135 return GTK_ICON_SIZE_INVALID
; // this is arbitrary
138 GtkIconSize
FindClosestIconSize(const wxSize
& size
)
145 } s_sizes
[NUM_SIZES
];
146 static bool s_sizesInitialized
= false;
148 if (!s_sizesInitialized
)
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
++)
158 gtk_icon_size_lookup(s_sizes
[i
].icon
,
159 &s_sizes
[i
].x
, &s_sizes
[i
].y
);
161 s_sizesInitialized
= true;
164 GtkIconSize best
= GTK_ICON_SIZE_DIALOG
; // presumably largest
165 unsigned distance
= INT_MAX
;
166 for (size_t i
= 0; i
< NUM_SIZES
; i
++)
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
)
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
);
175 return s_sizes
[i
].icon
;
176 else if (dist
< distance
)
179 best
= s_sizes
[i
].icon
;
185 GdkPixbuf
*CreateStockIcon(const char *stockid
, GtkIconSize size
)
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.
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.
195 GtkStyle
* style
= wxGTKPrivate::GetButtonWidget()->style
;
196 GtkIconSet
* iconset
= gtk_style_lookup_icon_set(style
, stockid
);
201 return gtk_icon_set_render_icon(iconset
, style
,
202 gtk_widget_get_default_direction(),
203 GTK_STATE_NORMAL
, size
, NULL
, NULL
);
206 GdkPixbuf
*CreateThemeIcon(const char *iconname
,
207 GtkIconSize iconsize
, const wxSize
& sz
)
210 if (size
== wxDefaultSize
)
212 gtk_icon_size_lookup(iconsize
, &size
.x
, &size
.y
);
215 return gtk_icon_theme_load_icon(
216 gtk_icon_theme_get_default(),
219 (GtkIconLookupFlags
)0, NULL
);
222 } // anonymous namespace
224 wxBitmap
wxGTK2ArtProvider::CreateBitmap(const wxArtID
& id
,
225 const wxArtClient
& client
,
228 wxCharBuffer stockid
= wxArtIDToStock(id
);
229 GtkIconSize stocksize
= (size
== wxDefaultSize
) ?
230 ArtClientToIconSize(client
) :
231 FindClosestIconSize(size
);
233 // we must have some size, this is arbitrary
234 if (stocksize
== GTK_ICON_SIZE_INVALID
)
235 stocksize
= GTK_ICON_SIZE_BUTTON
;
237 // allow passing GTK+ stock IDs to wxArtProvider:
239 stockid
= id
.ToAscii();
241 GdkPixbuf
*pixbuf
= CreateStockIcon(stockid
, stocksize
);
243 pixbuf
= CreateThemeIcon(stockid
, stocksize
, size
);
245 if (pixbuf
&& size
!= wxDefaultSize
&&
246 (size
.x
!= gdk_pixbuf_get_width(pixbuf
) ||
247 size
.y
!= gdk_pixbuf_get_height(pixbuf
)))
249 GdkPixbuf
*p2
= gdk_pixbuf_scale_simple(pixbuf
, size
.x
, size
.y
,
250 GDK_INTERP_BILINEAR
);
253 g_object_unref (pixbuf
);
260 bmp
.SetPixbuf(pixbuf
);
265 // ----------------------------------------------------------------------------
266 // wxArtProvider::GetNativeSizeHint()
267 // ----------------------------------------------------------------------------
270 wxSize
wxArtProvider::GetNativeSizeHint(const wxArtClient
& client
)
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
;
278 gtk_icon_size_lookup( gtk_size
, &width
, &height
);
279 return wxSize(width
, height
);