KiCad PCB EDA Suite
wx_angle_text.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KICAD, a free EDA CAD application.
3  *
4  * Copyright (C) 2020 Kicad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include <wx/wx.h>
25 #include <widgets/wx_angle_text.h>
26 #include <eda_rect.h>
27 
28 WX_ANGLE_TEXT::WX_ANGLE_TEXT( wxWindow* aParent, wxWindowID aId, const wxString& aLabel,
29  const wxPoint& aPos, double aAngle ) :
30  wxPanel( aParent, aId, aPos, wxDefaultSize ),
31  m_label( aLabel ),
32  m_angle( aAngle )
33 {
34  wxFont font = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
35  font.SetPointSize( font.GetPointSize() + 2 ); // rotated text looks visually smaller
36  SetFont( font );
37 
38  // Measure the size of the text
39  wxClientDC dc( this );
40  dc.SetFont( font );
41 
42  wxSize size = dc.GetTextExtent( m_label );
43  wxPoint pos = GetPosition();
44  EDA_RECT rect( wxPoint( 0, 0 ), size );
45  EDA_RECT bbox = rect.GetBoundingBoxRotated( rect.GetPosition(), m_angle );
46 
47  pos.y += bbox.GetTop() + ( rect.GetBottom() - bbox.GetBottom() );
48  size.y = bbox.GetHeight();
49  size.x = bbox.GetWidth();
50 
51  Move( pos );
52  SetSize( size );
53 
54  Bind( wxEVT_ERASE_BACKGROUND, &WX_ANGLE_TEXT::OnEraseBackground, this );
55  Bind( wxEVT_PAINT, &WX_ANGLE_TEXT::OnPaint, this );
56 }
57 
58 
59 void WX_ANGLE_TEXT::OnEraseBackground( wxEraseEvent& WXUNUSED( aEvent ) )
60 {
61  // NOP so that we don't erase other labels which intersect
62 }
63 
64 
65 void WX_ANGLE_TEXT::OnPaint( wxPaintEvent& WXUNUSED( aEvent ) )
66 {
67  wxPaintDC dc( this );
68 
69  dc.SetFont( GetFont() );
70  dc.SetTextForeground( wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ) );
71  dc.SetTextBackground( wxTransparentColor );
72  dc.SetBackgroundMode( wxTRANSPARENT );
73  dc.SetBackground( *wxTRANSPARENT_BRUSH );
74 
75  wxSize size = dc.GetTextExtent( m_label );
76  EDA_RECT rect( wxPoint( 0, 0 ), size );
77  EDA_RECT bbox = rect.GetBoundingBoxRotated( rect.GetPosition(), m_angle );
78  wxPoint pos( 0, -bbox.GetTop() );
79 
80  dc.DrawRotatedText( m_label, pos, m_angle / 10 );
81 }
82 
83 
84 EDA_RECT WX_ANGLE_TEXT::GetBoundingBox( wxWindow* aWindow, const wxString& aLabel, double aAngle )
85 {
86  wxSize size = WX_ANGLE_TEXT::GetTextSize( aWindow, aLabel );
87  EDA_RECT rect( wxPoint( 0, 0 ), size );
88 
89  return rect.GetBoundingBoxRotated( rect.GetPosition(), aAngle );
90 }
91 
92 
93 wxSize WX_ANGLE_TEXT::GetTextSize( wxWindow* aWindow, const wxString& aLabel )
94 {
95  // Create the font used for the text
96  wxFont font = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
97  font.SetPointSize( font.GetPointSize() + 2 ); // rotated text looks visually smaller
98 
99  // Measure the size of the text
100  wxClientDC dc( aWindow );
101  dc.SetFont( font );
102 
103  return dc.GetTextExtent( aLabel );
104 }
int GetTop() const
Definition: eda_rect.h:123
int GetWidth() const
Definition: eda_rect.h:119
int GetBottom() const
Definition: eda_rect.h:124
WX_ANGLE_TEXT(wxWindow *aParent, wxWindowID aId, const wxString &aLabel, const wxPoint &aPos, double aAngle)
const wxPoint GetPosition() const
Definition: eda_rect.h:115
const EDA_RECT GetBoundingBoxRotated(wxPoint aRotCenter, double aAngle)
Function GetBoundingBoxRotated.
static wxSize GetTextSize(wxWindow *aWindow, const wxString &aLabel)
Get the height and width of the unrotated text string that is created for this control.
int GetHeight() const
Definition: eda_rect.h:120
void OnEraseBackground(wxEraseEvent &WXUNUSED(aEvent))
wxString m_label
Definition: wx_angle_text.h:60
void OnPaint(wxPaintEvent &WXUNUSED(aEvent))
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
static EDA_RECT GetBoundingBox(wxWindow *aWindow, const wxString &aLabel, double aAngle)
Get the bounding box that this angled text will take up on a certain window.