diff --git a/common/gr_basic.cpp b/common/gr_basic.cpp index d2cbae4e36..a5397017b5 100644 --- a/common/gr_basic.cpp +++ b/common/gr_basic.cpp @@ -1,3 +1,24 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr + * Copyright (C) 2011 Wayne Stambaugh + * Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + /********************************/ /* Low level graphics routines */ /********************************/ @@ -13,6 +34,7 @@ #include #include #include +#include #if defined(__WXMAC__) && defined(USE_WX_GRAPHICS_CONTEXT) #include #endif @@ -1275,3 +1297,52 @@ void GRDrawAnchor( EDA_RECT *aClipBox, wxDC *aDC, int x, int y, x, y - anchor_size, x, y + anchor_size, 0, aColor ); } + + +void GRDrawWrappedText( wxDC& aDC, wxString const& aText ) +{ + wxStringTokenizer tokenizer( aText, " " ); + wxSize const dc_size = aDC.GetSize(); + wxSize const margin = aDC.GetTextExtent( " " ); + std::vector lines; + wxString line_accumulator; + int total_height = 0; + + while( tokenizer.HasMoreTokens() ) + { + wxString word = tokenizer.GetNextToken(); + wxSize linesize = aDC.GetTextExtent( line_accumulator + " " + word ); + + if( linesize.x >= dc_size.x - margin.x && !line_accumulator.IsEmpty() ) + { + lines.push_back( line_accumulator ); + line_accumulator = word; + } + else + { + line_accumulator += " "; + line_accumulator += word; + } + } + + if( !line_accumulator.IsEmpty() ) + { + lines.push_back( line_accumulator ); + } + + for( auto const& line: lines ) + { + wxSize linesize = aDC.GetTextExtent( line ); + total_height += linesize.y; + } + + int top = ( dc_size.y - total_height ) / 2; + int pos = top; + + for( auto const& line: lines ) + { + wxSize linesize = aDC.GetTextExtent( line ); + aDC.DrawText( line, ( dc_size.x - linesize.x ) / 2, pos ); + pos += linesize.y; + } +} diff --git a/eeschema/dialogs/dialog_choose_component.cpp b/eeschema/dialogs/dialog_choose_component.cpp index 623866dd18..ad9dc2deb9 100644 --- a/eeschema/dialogs/dialog_choose_component.cpp +++ b/eeschema/dialogs/dialog_choose_component.cpp @@ -464,11 +464,8 @@ void DIALOG_CHOOSE_COMPONENT::RenderPreview( LIB_PART* aComponent, int aUnit ) if( !aComponent ) // display a tooltip { - wxString tooltip = _( "Double click here to select a symbol\nfrom the library browser" ); - wxSize tsize = dc.GetTextExtent( tooltip.BeforeLast( '\n' ) ); - dc.DrawText( tooltip.BeforeLast( '\n' ), ( dc_size.x - tsize.x )/2, ( dc_size.y - tsize.y )/2 ); - tsize = dc.GetTextExtent( tooltip.AfterLast( '\n' ) ); - dc.DrawText( tooltip.AfterLast( '\n' ), ( dc_size.x - tsize.x )/2, ( dc_size.y + tsize.y )/2 ); + wxString tooltip = _( "Double-click here to select a symbol from the library browser" ); + GRDrawWrappedText( dc, tooltip ); return; } diff --git a/include/gr_basic.h b/include/gr_basic.h index 008d1cf0ca..71f1d67923 100644 --- a/include/gr_basic.h +++ b/include/gr_basic.h @@ -3,24 +3,20 @@ * * Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr * Copyright (C) 2011 Wayne Stambaugh - * Copyright (C) 1992-2015 KiCad Developers, see AUTHORS.txt for contributors. + * Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors. * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program; if not, you may find one here: - * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html - * or you may search the http://www.gnu.org website for the version 2 license, - * or you may write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . */ /** @@ -259,4 +255,11 @@ void GRLineArray( EDA_RECT* aClipBox, wxDC* aDC,std::vector& aLines, void GRDrawAnchor( EDA_RECT* aClipBox, wxDC *aDC, int x, int y, int aSize, COLOR4D aColor ); +/** + * Draw text centered on a wxDC with wrapping. + * @param aDC wxDC instance onto which the text will be drawn + * @param aText the text to draw + */ +void GRDrawWrappedText( wxDC& aDC, wxString const& aText ); + #endif /* define GR_BASIC */