← Back to team overview

kicad-developers team mailing list archive

[PATCH] eeschema: neatly wrap placeholder text in symbol chooser

 

Hi,

The placeholder text that was added to the symbol preview in the chooser
("double-click here...") has a manually placed line break, making it
flow poorly anywhere the line length doesn't work well --- on my system
with my DPI setting and fonts the line is cut off at almost any
non-ridiculous window sizing. I wrote a small function to wrap text
drawn to a wxDC to fix that.

I just added this as a static function near where it's used, but if
there's a better place to stick "miscellaneous wxDC helpers" please
advise and I'll move it; I couldn't find a decent spot.

-- 
Chris
>From 6b303f61f34416d653a47a6b6865bf4aa9e16627 Mon Sep 17 00:00:00 2001
From: Chris Pavlina <pavlina.chris@xxxxxxxxx>
Date: Wed, 3 Jan 2018 18:51:27 -0700
Subject: [PATCH] eeschema: neatly wrap placeholder text in symbol chooser

---
 eeschema/dialogs/dialog_choose_component.cpp | 57 +++++++++++++++++++++++++---
 1 file changed, 52 insertions(+), 5 deletions(-)

diff --git a/eeschema/dialogs/dialog_choose_component.cpp b/eeschema/dialogs/dialog_choose_component.cpp
index a04e686c0..7ece76cda 100644
--- a/eeschema/dialogs/dialog_choose_component.cpp
+++ b/eeschema/dialogs/dialog_choose_component.cpp
@@ -35,6 +35,7 @@
 #include <wx/splitter.h>
 #include <wx/timer.h>
 #include <wx/utils.h>
+#include <wx/tokenzr.h>
 
 #include <class_library.h>
 #include <sch_base_frame.h>
@@ -443,6 +444,55 @@ void DIALOG_CHOOSE_COMPONENT::OnComponentSelected( wxCommandEvent& aEvent )
 }
 
 
+static void draw_wrapped_text( wxDC& aDC, wxString const& aText )
+{
+    wxStringTokenizer tokenizer( aText, " " );
+    wxSize const dc_size = aDC.GetSize();
+    std::vector<wxString> lines;
+    wxString line_accumulator;
+    int total_height = 0;
+
+    // Wrap tooltip
+    while( tokenizer.HasMoreTokens() )
+    {
+        wxString word = tokenizer.GetNextToken();
+        wxSize linesize = aDC.GetTextExtent( line_accumulator + " " + word );
+
+        if( linesize.x >= dc_size.x - 10 && !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;
+    }
+}
+
+
 void DIALOG_CHOOSE_COMPONENT::RenderPreview( LIB_PART* aComponent, int aUnit )
 {
     wxPaintDC dc( m_sch_view_ctrl );
@@ -455,11 +505,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" );
+        draw_wrapped_text( dc, tooltip );
         return;
     }
 
-- 
2.15.1


Follow ups