4545#include < wx/bitmap.h>
4646#include < wx/button.h>
4747#include < wx/choice.h>
48+ #include < wx/dcbuffer.h>
4849#include < wx/dcclient.h>
4950#include < wx/dcmemory.h>
5051#include < wx/dcscreen.h>
@@ -241,7 +242,7 @@ wxDEFINE_EVENT(EVT_RECENT_SELECT, ValueEvent<agi::Color>);
241242
242243// / @class ColorPickerRecent
243244// / @brief A grid of recently used colors which can be selected by clicking on them
244- class ColorPickerRecent final : public wxStaticBitmap {
245+ class ColorPickerRecent final : public wxControl {
245246 int rows; // /< Number of rows of colors
246247 int cols; // /< Number of cols of colors
247248 int cellsize; // /< Width/Height of each cell
@@ -250,21 +251,18 @@ class ColorPickerRecent final : public wxStaticBitmap {
250251 std::vector<agi::Color> colors;
251252
252253 void OnClick (wxMouseEvent &evt) {
253- wxSize cs = GetClientSize ();
254- int cx = evt.GetX () * cols / cs.x ;
255- int cy = evt.GetY () * rows / cs.y ;
254+ int cx = evt.GetX () / cellsize;
255+ int cy = evt.GetY () / cellsize;
256256 if (cx < 0 || cx > cols || cy < 0 || cy > rows) return ;
257257 int i = cols*cy + cx;
258258
259259 if (i >= 0 && i < (int )colors.size ())
260260 AddPendingEvent (ValueEvent<agi::Color>(EVT_RECENT_SELECT, GetId (), colors[i]));
261261 }
262262
263- void UpdateBitmap () {
264- wxSize sz = GetClientSize ();
265-
266- wxBitmap background (sz.x , sz.y );
267- wxMemoryDC dc (background);
263+ void OnPaint (wxPaintEvent &) {
264+ wxAutoBufferedPaintDC dc (this );
265+ dc.Clear ();
268266
269267 dc.SetPen (*wxTRANSPARENT_PEN);
270268
@@ -277,20 +275,13 @@ class ColorPickerRecent final : public wxStaticBitmap {
277275 dc.DrawRectangle (x, y, x+cellsize, y+cellsize);
278276 }
279277 }
280-
281- {
282- wxEventBlocker blocker (this );
283- SetBitmap (background);
284- }
285-
286- Refresh (false );
287278 }
288279
289280 bool AcceptsFocusFromKeyboard () const override { return false ; }
290281
291282public:
292283 ColorPickerRecent (wxWindow *parent, int cols, int rows, int cellsize)
293- : wxStaticBitmap (parent, -1 , wxBitmap() , wxDefaultPosition, wxDefaultSize, STATIC_BORDER_FLAG)
284+ : wxControl (parent, -1 , wxDefaultPosition, wxDefaultSize, STATIC_BORDER_FLAG)
294285 , rows(rows)
295286 , cols(cols)
296287 , cellsize(cellsize)
@@ -300,16 +291,17 @@ class ColorPickerRecent final : public wxStaticBitmap {
300291 SetMinSize (GetSize ());
301292 SetMaxSize (GetSize ());
302293 SetCursor (*wxCROSS_CURSOR);
294+ SetBackgroundStyle (wxBG_STYLE_PAINT);
303295
304296 Bind (wxEVT_LEFT_DOWN, &ColorPickerRecent::OnClick, this );
305- Bind (wxEVT_SIZE, [ this ](wxSizeEvent&) { UpdateBitmap (); } );
297+ Bind (wxEVT_PAINT, &ColorPickerRecent::OnPaint, this );
306298 }
307299
308300 // / Load the colors to show
309301 void Load (std::vector<agi::Color> const & recent_colors) {
310302 colors = recent_colors;
311303 colors.resize (rows * cols);
312- UpdateBitmap ( );
304+ Refresh ( false );
313305 }
314306
315307 // / Get the list of recent colors
@@ -325,7 +317,7 @@ class ColorPickerRecent final : public wxStaticBitmap {
325317 colors.pop_back ();
326318 }
327319
328- UpdateBitmap ( );
320+ Refresh ( false );
329321 }
330322};
331323
0 commit comments