wxPen *wxPenList::FindOrCreatePen (const wxColour& colour, int width, int style)
{
- for (wxNode * node = First (); node; node = node->Next ())
+ for (wxNode * node = First (); node; node = node->Next ())
+ {
+ wxPen *each_pen = (wxPen *) node->Data ();
+ if (each_pen &&
+ each_pen->GetVisible() &&
+ each_pen->GetWidth () == width &&
+ each_pen->GetStyle () == style &&
+ each_pen->GetColour ().Red () == colour.Red () &&
+ each_pen->GetColour ().Green () == colour.Green () &&
+ each_pen->GetColour ().Blue () == colour.Blue ())
+ return each_pen;
+ }
+
+ wxPen *pen = new wxPen (colour, width, style);
+ if ( !pen->Ok() )
{
- wxPen *each_pen = (wxPen *) node->Data ();
- if (each_pen &&
- each_pen->GetVisible() &&
- each_pen->GetWidth () == width &&
- each_pen->GetStyle () == style &&
- each_pen->GetColour ().Red () == colour.Red () &&
- each_pen->GetColour ().Green () == colour.Green () &&
- each_pen->GetColour ().Blue () == colour.Blue ())
- return each_pen;
+ // don't save the invalid pens in the list
+ delete pen;
+
+ return NULL;
}
- wxPen *pen = new wxPen (colour, width, style);
- // Yes, we can return a pointer to this in a later FindOrCreatePen call,
- // because we created it within FindOrCreatePen. Safeguards against
- // returning a pointer to an automatic variable and hanging on to it
- // (dangling pointer).
- pen->SetVisible(TRUE);
+ // Yes, we can return a pointer to this in a later FindOrCreatePen call,
+ // because we created it within FindOrCreatePen. Safeguards against
+ // returning a pointer to an automatic variable and hanging on to it
+ // (dangling pointer).
+ pen->SetVisible(TRUE);
- return pen;
+ return pen;
}
wxBrushList::~wxBrushList ()
wxBrush *wxBrushList::FindOrCreateBrush (const wxColour& colour, int style)
{
- for (wxNode * node = First (); node; node = node->Next ())
+ for (wxNode * node = First (); node; node = node->Next ())
{
- wxBrush *each_brush = (wxBrush *) node->Data ();
- if (each_brush &&
- each_brush->GetVisible() &&
- each_brush->GetStyle () == style &&
- each_brush->GetColour ().Red () == colour.Red () &&
- each_brush->GetColour ().Green () == colour.Green () &&
- each_brush->GetColour ().Blue () == colour.Blue ())
- return each_brush;
+ wxBrush *each_brush = (wxBrush *) node->Data ();
+ if (each_brush &&
+ each_brush->GetVisible() &&
+ each_brush->GetStyle () == style &&
+ each_brush->GetColour ().Red () == colour.Red () &&
+ each_brush->GetColour ().Green () == colour.Green () &&
+ each_brush->GetColour ().Blue () == colour.Blue ())
+ return each_brush;
}
- // Yes, we can return a pointer to this in a later FindOrCreateBrush call,
- // because we created it within FindOrCreateBrush. Safeguards against
- // returning a pointer to an automatic variable and hanging on to it
- // (dangling pointer).
- wxBrush *brush = new wxBrush (colour, style);
+ wxBrush *brush = new wxBrush (colour, style);
+
+ if ( !brush->Ok() )
+ {
+ // don't put the brushes we failed to create into the list
+ delete brush;
+
+ return NULL;
+ }
- brush->SetVisible(TRUE);
+ brush->SetVisible(TRUE);
- return brush;
+ // Yes, we can return a pointer to this in a later FindOrCreateBrush call,
+ // because we created it within FindOrCreateBrush. Safeguards against
+ // returning a pointer to an automatic variable and hanging on to it
+ // (dangling pointer).
+ return brush;
}
void wxBrushList::RemoveBrush (wxBrush * brush)