Salut à vous !

J'essaie de faire un bouton à états (enabled, disabled, mouse over, clicked) qui aurait une image par état.
Sous windows je n'ai aucun problème, mais sous Linux (Debian) l'évènement EVT_PAINT ne se lance jamais, même avec un Refresh...

Je vous mets le code de mon bouton, eclairez moi si vous avez des suggestions, car là je piétine...

ImageButton.h :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
 
#ifndef ___ImageButton___
#define ___ImageButton___
 
#include <wx/button.h>
#include <vector>
 
enum JokerButtonIndexes
{
	jiENABLE,
	jiMOUSEOVER,
	jiDOWN,
	jiDISABLE,
};
 
const int jbCENTER			= 0x00000001;
const int jbSTRETCH			= 0x00000010;
const int jbTRANSPARENT		= 0x00000100;
const int jbBORDER			= 0x00001000;
const int jbROUNDBORDER	= 0x00010000;
 
typedef std::vector <wxBitmap>	wxBitmapArray;
typedef std::vector <wxColour>	wxColourArray;
 
class ImageButton : public wxButton
{
private:
	bool m_mouseDown;
	bool m_mouseOver;
	bool m_down;
	bool m_enabled;
	wxString m_caption;
	bool m_autoSize;
 
	bool m_stretch;
	bool m_center;
	bool m_transparent;
	bool m_border;
	bool m_roundBorder;
 
	wxBitmapArray m_images;
	wxColourArray m_colours;
	wxFont m_font;
	wxColourArray m_fontColours;
	wxColour m_backgroundColour;
 
public:
	ImageButton( wxWindow * p_parent, wxWindowID p_id, const wxString & p_caption,
			    const wxPoint & p_position, const wxSize & p_size, int p_style = 0);
 
	virtual bool Enable( bool p_value) { return (m_enabled = p_value); }
	virtual bool SetBackgroundColour( const wxColour & p_colour) { m_backgroundColour = p_colour;return true; }
	virtual const wxColour & GetBackgroundColour() { return m_backgroundColour; }
	virtual bool SetFont( const wxFont & p_font) { m_font = p_font;return true; }
	void SetImages( const wxBitmapArray & p_images);
	void SetColours( const wxColourArray & p_colours);
	void SetFontColours( const wxColourArray & p_colours);
 
private:
	void _onMouseEnter		( wxMouseEvent & p_event);
	void _onMouseLeave		( wxMouseEvent & p_event);
	void _onMouseLeftDown		( wxMouseEvent & p_event);
	void _onMouseLeftUp		( wxMouseEvent & p_event);
	void _onPaint			( wxPaintEvent & p_event);
 
public:
	inline void SetCaption		( const wxString & p_caption) { m_caption = p_caption;Refresh(); }
	inline void SetAutoSize		( bool p_value) { m_autoSize = p_value;Refresh(); }
	inline void SetStretch			( bool p_value) { m_stretch = p_value;Refresh(); }
	inline void SetCenter			( bool p_value) { m_center = p_value;Refresh(); }
	inline void SetTransparent		( bool p_value) { m_transparent = p_value;Refresh(); }
	inline void SetBorder			( bool p_value) { m_border = p_value;Refresh(); }
	inline void SetRoundBorder		( bool p_value) { m_roundBorder = p_value;Refresh(); }
	inline void SetImageEnable		( const wxBitmap & p_image)			{ m_images[jiENABLE] = p_image;Refresh(); }
	inline void SetImageMouseOver		( const wxBitmap & p_image)			{ m_images[jiMOUSEOVER] = p_image;Refresh(); }
	inline void SetImageDown		( const wxBitmap & p_image)			{ m_images[jiDOWN] = p_image;Refresh(); }
	inline void SetImageDisable		( const wxBitmap & p_image)			{ m_images[jiDISABLE] = p_image;Refresh(); }
	inline void SetColourEnable		( const wxColour & p_colour)	{ m_colours[jiENABLE] = p_colour;Refresh(); }
	inline void SetColourMouseOver		( const wxColour & p_colour)	{ m_colours[jiMOUSEOVER] = p_colour;Refresh(); }
	inline void SetColourDown		( const wxColour & p_colour)	{ m_colours[jiDOWN] = p_colour;Refresh(); }
	inline void SetColourDisable		( const wxColour & p_colour)	{ m_colours[jiDISABLE] = p_colour;Refresh(); }
	inline void SetFontColourEnable		( const wxColour & p_colour)	{ m_fontColours[jiENABLE] = p_colour;Refresh(); }
	inline void SetFontColourMouseOver	( const wxColour & p_colour)	{ m_fontColours[jiMOUSEOVER] = p_colour;Refresh(); }
	inline void SetFontColourDown		( const wxColour & p_colour)	{ m_fontColours[jiDOWN] = p_colour;Refresh(); }
	inline void SetFontColourDisable	( const wxColour & p_colour)	{ m_fontColours[jiDISABLE] = p_colour;Refresh(); }
 
	inline const  wxBitmap & GetImageEnable			() { return m_images[jiENABLE]; }
	inline const wxBitmap & GetImageMouseOver		() { return m_images[jiMOUSEOVER]; }
	inline const wxBitmap & GetImageDown			() { return m_images[jiDOWN]; }
	inline const  wxBitmap & GetImageDisable		() { return m_images[jiDISABLE]; }
	inline const wxColour & GetColourEnable			() { return m_colours[jiENABLE]; }
	inline const wxColour & GetColourMouseOver		() { return m_colours[jiMOUSEOVER]; }
	inline const wxColour & GetColourDown			() { return m_colours[jiDOWN]; }
	inline const wxColour & GetColourDisable		() { return m_colours[jiDISABLE]; }
	inline const wxColour & GetFontColourEnable		() { return m_fontColours[jiENABLE]; }
	inline const wxColour & GetFontColourMouseOver		() { return m_fontColours[jiMOUSEOVER]; }
	inline const wxColour & GetFontColourDown		() { return m_fontColours[jiDOWN]; }
	inline const wxColour & GetFontColourDisable		() { return m_fontColours[jiDISABLE]; }
	inline const wxFont & GetFont				() { return m_font; }
 
	DECLARE_EVENT_TABLE()
};
#endif
ImageButton.cpp :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
 
#include "ImageButton.h"
#include <wx/dcclient.h>
 
BEGIN_EVENT_TABLE( ImageButton, wxButton)
	EVT_LEFT_DOWN(		ImageButton::_onMouseLeftDown)
	EVT_LEFT_UP(		ImageButton::_onMouseLeftUp)
	EVT_LEAVE_WINDOW(	ImageButton::_onMouseLeave)
	EVT_ENTER_WINDOW(	ImageButton::_onMouseEnter)
	EVT_PAINT(			ImageButton::_onPaint)
END_EVENT_TABLE()
 
 
ImageButton :: ImageButton( wxWindow * p_parent, wxWindowID p_id, 
					   const wxString & p_caption, const wxPoint & p_position,
					   const wxSize & p_size, int p_style)
	:	wxButton( p_parent, p_id, p_caption, p_position, p_size, wxNO_BORDER),
		m_mouseDown			( false),
		m_mouseOver			( false),
		m_down				( false),
		m_enabled			( true),
		m_caption			( p_caption),
		m_autoSize			( false),
		m_stretch			( false),
		m_center			( false),
		m_transparent		( false),
		m_border			( false),
		m_roundBorder		( false),
		m_font				( wxFont( 8, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL)),
		m_backgroundColour	( wxColour( 192, 192, 192))
 
{
	if ((p_style & jbSTRETCH) != 0)
	{
		m_stretch = true;
	}
 
	if ((p_style & jbCENTER) != 0)
	{
		m_center = true;
	}
 
	if ((p_style & jbTRANSPARENT) != 0)
	{
		m_transparent = true;
	}
 
	if ((p_style & jbBORDER) != 0)
	{
		m_border = true;
	}
 
	if ((p_style & jbROUNDBORDER) != 0)
	{
		m_roundBorder = true;
	}
 
	m_images.push_back( wxBitmap());
	m_images.push_back( wxBitmap());
	m_images.push_back( wxBitmap());
	m_images.push_back( wxBitmap());
 
	m_colours.push_back( wxColour( 192, 192, 192));
	m_colours.push_back( wxColour( 192, 192, 192));
	m_colours.push_back( wxColour( 192, 192, 192));
	m_colours.push_back( wxColour( 192, 192, 192));
 
	m_fontColours.push_back( wxColour( L"black"));
	m_fontColours.push_back( wxColour( L"black"));
	m_fontColours.push_back( wxColour( L"black"));
	m_fontColours.push_back( wxColour( L"black"));
}
 
void ImageButton :: SetImages( const wxBitmapPtrArray & p_images)
{
	for (size_t i = 0 ; i < p_images.size() ; i++)
	{
		m_images[i] = p_images[i];
	}
	Refresh();
}
 
void ImageButton :: SetColours( const wxColourArray & p_colours)
{
	for (size_t i = 0 ; i < p_colours.size() ; i++)
	{
		m_colours[i] = p_colours[i];
	}
	Refresh();
}
 
void ImageButton :: SetFontColours( const wxColourArray & p_colours)
{
	for (size_t i = 0 ; i < p_colours.size() ; i++)
	{
		m_fontColours[i] = p_colours[i];
	}
	Refresh();
}
 
void ImageButton :: _onMouseEnter( wxMouseEvent & p_event)
{
//	std::cout << "ImageButton :: _onMouseEnter\n";
	m_mouseOver = true;
	Refresh();
	p_event.Skip();
}
 
void ImageButton :: _onMouseLeave( wxMouseEvent & p_event)
{
//	std::cout << "ImageButton :: _onMouseLeave\n";
	m_mouseOver = false;
	m_mouseDown = false;
	m_down = false;
	Refresh();
	p_event.Skip();
}
 
void ImageButton :: _onMouseLeftDown( wxMouseEvent & p_event)
{
//	std::cout << "ImageButton :: _onMouseLeftDown\n";
	m_mouseDown = true;
	Refresh();
	p_event.Skip();
}
 
void ImageButton :: _onMouseLeftUp( wxMouseEvent & p_event)
{
//	std::cout << "ImageButton :: _onMouseLeftUp\n";
	m_mouseDown = false;
	m_down = false;
	Refresh();
	p_event.Skip();
}
 
void ImageButton :: _onPaint( wxPaintEvent & p_event)
{
	std::cout << "ImageButton :: _onPaint\n";
	wxSize l_size = GetClientSize();
	int h = m_images[jiENABLE].GetHeight();
	int y = (l_size.y - h) / 2;
	int l = m_images[jiENABLE].GetWidth();
	int x = (l_size.x - l) / 2;
 
	if ( ! m_center)
	{
		x = 0;
		y = 0;
	}
 
	bool l_imaged = true;
	wxBitmap l_image;
	wxColour l_colour;
	wxColour l_fontColour;
	wxFont l_font;
 
	l_image = m_images[jiENABLE];
	l_colour = m_colours[jiENABLE];
	l_fontColour = m_fontColours[jiENABLE];
 
	if ( ! m_enabled)
	{
		if (m_images[jiDISABLE].IsOk())
		{
			l_image = m_images[jiDISABLE];
		}
		l_colour = m_colours[jiDISABLE];
		l_fontColour = m_fontColours[jiDISABLE];
	}
	else if (m_mouseDown || m_down)
	{
		if (m_images[jiDOWN].IsOk())
		{
			l_image = m_images[jiDOWN];
		}
		l_colour = m_colours[jiDOWN];
		l_fontColour = m_fontColours[jiDOWN];
	}
	else if (m_mouseOver)
	{
		if (m_images[jiMOUSEOVER].IsOk())
		{
			l_image = m_images[jiMOUSEOVER];
		}
		l_colour = m_colours[jiMOUSEOVER];
		l_fontColour = m_fontColours[jiMOUSEOVER];
	}
	else if ( ! m_images[jiENABLE].IsOk())
	{
		l_imaged = false;
		std::cout << "Not imaged\n";
	}
	else
	{
		std::cout << "Imaged\n";
	}
 
	wxPaintDC l_dc( this);
	l_dc.DestroyClippingRegion();
	wxRect l_rect( 0, 0, l_size.x, l_size.y);
	l_dc.SetClippingRegion( l_rect);
	wxBrush l_brush( l_dc.GetBrush());
	wxPen l_pen( l_dc.GetPen());
 
	if (m_border)
	{
		l_rect.x += 1;
		l_rect.y += 1;
		l_rect.height -= 2;
		l_rect.width -= 2;
		l_brush.SetStyle( wxSOLID);
 
		if (m_roundBorder)
		{
			l_brush.SetColour( m_backgroundColour);
			l_pen.SetColour( m_backgroundColour);
			l_dc.SetBrush( l_brush);
			l_dc.SetPen( l_pen);
			l_dc.DrawRectangle( 0, 0, l_size.x, l_size.y);
 
			l_brush.SetColour( l_colour);
			l_pen.SetColour( wxColour( L"black"));
			float l_rayon = l_size.y / 4.0;
			l_dc.SetBrush( l_brush);
			l_dc.SetPen( l_pen);
			l_dc.DrawRoundedRectangle( 0, 0, l_size.x, l_size.y, l_rayon);
		}
		else
		{
			l_pen.SetColour( wxColour( L"black"));
			l_brush.SetColour( l_colour);
			l_dc.SetBrush( l_brush);
			l_dc.SetPen( l_pen);
			l_dc.DrawRectangle( 0, 0, l_size.x, l_size.y);
		}
	}
	else
	{
		l_brush.SetColour( l_colour);
		l_pen.SetColour( l_colour);
		l_dc.SetBrush( l_brush);
		l_dc.SetPen( l_pen);
		l_dc.DrawRectangle( 0, 0, l_size.x, l_size.y);
	}
 
	if (l_imaged)
	{
		int h = l_image.GetHeight();
		int y = (l_size.y - h) / 2;
		int l = l_image.GetWidth();
		int x = (l_size.x - l) / 2;
		if (m_border)
		{                   
			h -= 2;
			y -= 1;
			l -= 2;
			x -= 1;
		}
 
		if (m_stretch)
		{
			l_image = wxBitmap( l_image.ConvertToImage().Scale( l, h, wxIMAGE_QUALITY_HIGH));
		}
		l_dc.DrawBitmap( l_image, x, y, m_transparent);
		l_brush.SetStyle( wxTRANSPARENT);
		l_dc.SetBrush( l_brush);
	}
 
	l_dc.SetFont( m_font);
 
	if (m_center)
	{
		wxSize l_textSize = l_dc.GetTextExtent( m_caption);
		l_size = GetClientSize();
		x = (l_size.x - l_textSize.x) / 2;
		y = (l_size.y - l_textSize.y) / 2;
	}
 
	l_dc.SetTextBackground( l_colour);
	l_dc.SetTextForeground( l_fontColour);
	l_dc.DrawText( m_caption, wxPoint( x, y));
}