KiCad PCB EDA Suite
html_messagebox.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) 2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2014-2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <wx/clipbrd.h>
26 #include <wx/log.h>
27 #include <kicad_string.h>
29 
30 
31 HTML_MESSAGE_BOX::HTML_MESSAGE_BOX( wxWindow* aParent, const wxString& aTitle,
32  const wxPoint& aPosition, const wxSize& aSize ) :
33  DIALOG_DISPLAY_HTML_TEXT_BASE( aParent, wxID_ANY, aTitle, aPosition, aSize )
34 {
35  m_htmlWindow->SetLayoutDirection( wxLayout_LeftToRight );
36  ListClear();
37 
38  // Gives a default logical size (the actual size depends on the display definition)
39  if( aSize != wxDefaultSize )
40  setSizeInDU( aSize.x, aSize.y );
41 
42  Center();
43 
44  m_sdbSizer1OK->SetDefault();
45 }
46 
47 
49 {
50  // Prevent wxWidgets bug which fails to release when closing the window on an <esc>.
51  if( m_htmlWindow->HasCapture() )
52  m_htmlWindow->ReleaseMouse();
53 }
54 
55 
56 void HTML_MESSAGE_BOX::OnOKButtonClick( wxCommandEvent& event )
57 {
58  // the dialog can be shown quasi-model, modal, or not modeless.
59  // therefore, use the right way to close it.
60  if( IsQuasiModal() )
61  EndQuasiModal( wxID_OK );
62  else if( IsModal() )
63  EndModal( wxID_OK );
64  else
65  Destroy();
66 }
67 
68 
70 {
71  m_htmlWindow->SetPage( wxEmptyString );
72 }
73 
74 
75 void HTML_MESSAGE_BOX::ListSet( const wxString& aList )
76 {
77  wxArrayString strings_list;
78  wxStringSplit( aList, strings_list, wxChar( '\n' ) );
79 
80  wxString msg = wxT( "<ul>" );
81 
82  for ( unsigned ii = 0; ii < strings_list.GetCount(); ii++ )
83  {
84  msg += wxT( "<li>" );
85  msg += strings_list.Item( ii ) + wxT( "</li>" );
86  }
87 
88  msg += wxT( "</ul>" );
89 
90  m_htmlWindow->AppendToPage( msg );
91 }
92 
93 
94 void HTML_MESSAGE_BOX::ListSet( const wxArrayString& aList )
95 {
96  wxString msg = wxT( "<ul>" );
97 
98  for( unsigned ii = 0; ii < aList.GetCount(); ii++ )
99  {
100  msg += wxT( "<li>" );
101  msg += aList.Item( ii ) + wxT( "</li>" );
102  }
103 
104  msg += wxT( "</ul>" );
105 
106  m_htmlWindow->AppendToPage( msg );
107 }
108 
109 
110 void HTML_MESSAGE_BOX::MessageSet( const wxString& message )
111 {
112  wxString message_value = wxString::Format(
113  wxT( "<b>%s</b><br>" ), message );
114 
115  m_htmlWindow->AppendToPage( message_value );
116 }
117 
118 
119 void HTML_MESSAGE_BOX::AddHTML_Text( const wxString& message )
120 {
121  m_htmlWindow->AppendToPage( message );
122 }
123 
124 
126 {
127  m_sdbSizer1->Show( false );
128  Layout();
129 
130  Show( true );
131 }
132 
133 
134 void HTML_MESSAGE_BOX::OnCharHook( wxKeyEvent& aEvent )
135 {
136  // shift-return (Mac default) or Ctrl-Return (GTK) for OK
137  if( aEvent.GetKeyCode() == WXK_ESCAPE )
138  {
139  wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
140  return;
141  }
142  else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'A' )
143  {
144  m_htmlWindow->SelectAll();
145  return;
146  }
147  else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'C' )
148  {
149  wxLogNull doNotLog; // disable logging of failed clipboard actions
150 
151  if( wxTheClipboard->Open() )
152  {
153  wxTheClipboard->SetData( new wxTextDataObject( m_htmlWindow->SelectionToText() ) );
154  wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
155  wxTheClipboard->Close();
156  }
157 
158  return;
159  }
160 
161  aEvent.Skip();
162 }
void setSizeInDU(int x, int y)
Set the dialog to the given dimensions in "dialog units".
bool IsQuasiModal() const
Definition: dialog_shim.h:106
virtual bool IsModal() const
Return true if the frame is shown in our modal mode and false if the frame is shown as an usual frame...
Class DIALOG_DISPLAY_HTML_TEXT_BASE.
void ListSet(const wxString &aList)
Add a list of items.
~HTML_MESSAGE_BOX() override
void MessageSet(const wxString &message)
Add a message (in bold) to message list.
bool Show(bool show) override
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
Definition: string.cpp:895
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
void ShowModeless()
Show a modeless version of the dialog (without an OK button).
void OnOKButtonClick(wxCommandEvent &event) override
void EndQuasiModal(int retCode)
void AddHTML_Text(const wxString &message)
Add HTML text (without any change) to message list.
virtual void OnCharHook(wxKeyEvent &aEvt) override
HTML_MESSAGE_BOX(wxWindow *aParent, const wxString &aTitle=wxEmptyString, const wxPoint &aPosition=wxDefaultPosition, const wxSize &aSize=wxDefaultSize)