KiCad PCB EDA Suite
Loading...
Searching...
No Matches
kistatusbar.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) 2023 Mark Roszko <[email protected]>
5 * Copyright The 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/button.h>
26#include <wx/statusbr.h>
27#include <wx/gauge.h>
28#include <wx/stattext.h>
29#include <fmt/format.h>
30#include <array>
31#include <widgets/kistatusbar.h>
33#include <widgets/ui_common.h>
34#include <pgm_base.h>
37#include <bitmaps.h>
38#include <wx/dcclient.h>
39
40
41KISTATUSBAR::KISTATUSBAR( int aNumberFields, wxWindow* parent, wxWindowID id, STYLE_FLAGS aFlags ) :
42 wxStatusBar( parent, id ),
43 m_backgroundStopButton( nullptr ),
44 m_notificationsButton( nullptr ),
45 m_normalFieldsCount( aNumberFields ),
46 m_styleFlags( aFlags )
47{
48#ifdef __WXOSX__
49 // we need +1 extra field on OSX to offset from the rounded corner on the right
50 // OSX doesn't use resize grippers like the other platforms and the statusbar field
51 // includes the rounded part
52 int extraFields = 3;
53#else
54 int extraFields = 2;
55#endif
56
57 bool showNotification = ( m_styleFlags & NOTIFICATION_ICON );
58 bool showCancel = ( m_styleFlags & CANCEL_BUTTON );
59
60 if( showCancel )
61 extraFields++;
62
63 if( showNotification )
64 extraFields++;
65
66 SetFieldsCount( aNumberFields + extraFields );
67
68 int* widths = new int[aNumberFields + extraFields];
69
70 for( int i = 0; i < aNumberFields; i++ )
71 widths[i] = -1;
72
73 if( std::optional<int> idx = fieldIndex( FIELD::BGJOB_LABEL ) )
74 widths[aNumberFields + *idx] = -1; // background status text field (variable size)
75
76 if( std::optional<int> idx = fieldIndex( FIELD::BGJOB_GAUGE ) )
77 widths[aNumberFields + *idx] = 75; // background progress button
78
79 if( std::optional<int> idx = fieldIndex( FIELD::BGJOB_CANCEL ) )
80 widths[aNumberFields + *idx] = 20; // background stop button
81
82 if( std::optional<int> idx = fieldIndex( FIELD::NOTIFICATION ) )
83 widths[aNumberFields + *idx] = 20; // notifications button
84
85#ifdef __WXOSX__
86 // offset from the right edge
87 widths[aNumberFields + extraFields - 1] = 10;
88#endif
89
90 SetStatusWidths( aNumberFields + extraFields, widths );
91 delete[] widths;
92
93 int* styles = new int[aNumberFields + extraFields];
94
95 for( int i = 0; i < aNumberFields + extraFields; i++ )
96 styles[i] = wxSB_FLAT;
97
98 SetStatusStyles( aNumberFields + extraFields, styles );
99 delete[] styles;
100
101 m_backgroundTxt = new wxStaticText( this, wxID_ANY, wxT( "" ) );
102
103 m_backgroundProgressBar = new wxGauge( this, wxID_ANY, 100, wxDefaultPosition, wxDefaultSize,
104 wxGA_HORIZONTAL | wxGA_SMOOTH );
105
106 if( showCancel )
107 {
108 m_backgroundStopButton = new wxButton( this, wxID_ANY, "X", wxDefaultPosition,
109 wxDefaultSize, wxBU_EXACTFIT );
110 }
111
112 if( showNotification )
113 {
114 m_notificationsButton = new BITMAP_BUTTON( this, wxID_ANY, wxNullBitmap, wxDefaultPosition,
115 wxDefaultSize, wxBU_EXACTFIT );
116
117 m_notificationsButton->SetPadding( 0 );
119 m_notificationsButton->SetShowBadge( true );
120 m_notificationsButton->SetBitmapCentered( true );
121
123 }
124
125 Bind( wxEVT_SIZE, &KISTATUSBAR::onSize, this );
127
129 Layout();
130}
131
132
134{
136 m_notificationsButton->Unbind( wxEVT_BUTTON, &KISTATUSBAR::onNotificationsIconClick, this );
137
138 Unbind( wxEVT_SIZE, &KISTATUSBAR::onSize, this );
140 this );
141}
142
143
144void KISTATUSBAR::onNotificationsIconClick( wxCommandEvent& aEvent )
145{
146 wxCHECK( m_notificationsButton, /* void */ );
147 wxPoint pos = m_notificationsButton->GetScreenPosition();
148
149 wxRect r;
150 GetFieldRect( m_normalFieldsCount + 3, r );
151 pos.x += r.GetWidth();
152
153 Pgm().GetNotificationsManager().ShowList( this, pos );
154}
155
156
157void KISTATUSBAR::onBackgroundProgressClick( wxMouseEvent& aEvent )
158{
159 wxPoint pos = m_backgroundProgressBar->GetScreenPosition();
160
161 wxRect r;
162 GetFieldRect( m_normalFieldsCount + 1, r );
163 pos.x += r.GetWidth();
164
165 Pgm().GetBackgroundJobMonitor().ShowList( this, pos );
166}
167
168
169void KISTATUSBAR::onSize( wxSizeEvent& aEvent )
170{
171 constexpr int padding = 5;
172
173 wxRect r;
174 GetFieldRect( m_normalFieldsCount + *fieldIndex( FIELD::BGJOB_LABEL ), r );
175 int x = r.GetLeft();
176 int y = r.GetTop();
177 int textHeight = KIUI::GetTextSize( wxT( "bp" ), this ).y;
178
179 if( r.GetHeight() > textHeight )
180 y += ( r.GetHeight() - textHeight ) / 2;
181
182 m_backgroundTxt->SetPosition( { x, y } );
183
184 GetFieldRect( m_normalFieldsCount + *fieldIndex( FIELD::BGJOB_GAUGE ), r );
185 x = r.GetLeft();
186 y = r.GetTop();
187 int w = r.GetWidth();
188 int h = r.GetHeight();
189 wxSize buttonSize( 0, 0 );
190
192 {
193 buttonSize = m_backgroundStopButton->GetEffectiveMinSize();
194 m_backgroundStopButton->SetPosition( { x + w - buttonSize.GetWidth(), y } );
195 m_backgroundStopButton->SetSize( buttonSize.GetWidth(), h );
196 buttonSize.x += padding;
197 }
198
199 m_backgroundProgressBar->SetPosition( { x + padding, y } );
200 m_backgroundProgressBar->SetSize( w - buttonSize.GetWidth() - padding, h );
201
203 {
204 GetFieldRect( m_normalFieldsCount + *fieldIndex( FIELD::NOTIFICATION ), r );
205 x = r.GetLeft();
206 y = r.GetTop();
207 h = r.GetHeight();
208 buttonSize = m_notificationsButton->GetEffectiveMinSize();
209 m_notificationsButton->SetPosition( { x, y } );
210 m_notificationsButton->SetSize( buttonSize.GetWidth() + 6, h );
211 }
212}
213
214
216{
218
220 m_backgroundStopButton->Show( aCancellable );
221}
222
223
231
232
234{
235 m_backgroundProgressBar->SetValue( aAmount );
236}
237
238
240{
241 m_backgroundProgressBar->SetRange( aAmount );
242}
243
244
245void KISTATUSBAR::SetBackgroundStatusText( const wxString& aTxt )
246{
247 m_backgroundTxt->SetLabel( aTxt );
248}
249
250
252{
253 wxCHECK( m_notificationsButton, /* void */ );
254 wxString cnt = "";
255
256 if( aCount > 0 )
257 cnt = fmt::format( "{}", aCount );
258
259 m_notificationsButton->SetBadgeText( cnt );
260
261 // force a repaint or it wont until it gets activity
262 Refresh();
263}
264
265#include <widgets/ui_common.h>
266void KISTATUSBAR::SetEllipsedTextField( const wxString& aText, int aFieldId )
267{
268 wxRect fieldRect;
269 int width = -1;
270 wxString etext = aText;
271
272 // Only GetFieldRect() returns the current size for variable size fields
273 // Other methods return -1 for the width of these fields.
274 if( GetFieldRect( aFieldId, fieldRect ) )
275 width = fieldRect.GetWidth();
276
277 if( width > 20 )
278 {
279 wxClientDC dc( this );
280
281 // Gives a margin to the text to be sure it is not clamped at its end
282 int margin = KIUI::GetTextSize( wxT( "XX" ), this ).x;
283 etext = wxControl::Ellipsize( etext, dc, wxELLIPSIZE_MIDDLE, width - margin );
284 }
285
286 SetStatusText( etext, aFieldId );
287}
288
289
290std::optional<int> KISTATUSBAR::fieldIndex( FIELD aField ) const
291{
292 switch( aField )
293 {
294 case FIELD::BGJOB_LABEL: return 0;
295 case FIELD::BGJOB_GAUGE: return 1;
297 {
299 return 2;
300
301 break;
302 }
304 {
306 return 3;
308 return 2;
309 }
310 }
311
312 return std::nullopt;
313}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:110
void ShowList(wxWindow *aParent, wxPoint aPos)
Shows the background job list.
A bitmap button widget that behaves like an AUI toolbar item's button when it is drawn.
STYLE_FLAGS m_styleFlags
BITMAP_BUTTON * m_notificationsButton
std::optional< int > fieldIndex(FIELD aField) const
void SetBackgroundStatusText(const wxString &aTxt)
Set the status text that displays next to the progress bar.
void onBackgroundProgressClick(wxMouseEvent &aEvent)
KISTATUSBAR(int aNumberFields, wxWindow *parent, wxWindowID id, STYLE_FLAGS aFlags=DEFAULT_STYLE)
void onSize(wxSizeEvent &aEvent)
int m_normalFieldsCount
void SetBackgroundProgress(int aAmount)
Set the current progress of the progress bar.
wxGauge * m_backgroundProgressBar
wxButton * m_backgroundStopButton
void SetEllipsedTextField(const wxString &aText, int aFieldId)
Set the text in a field using wxELLIPSIZE_MIDDLE option to adjust the text size to the field size.
void HideBackgroundProgressBar()
Hide the background progress bar.
void onNotificationsIconClick(wxCommandEvent &aEvent)
void SetBackgroundProgressMax(int aAmount)
Set the max progress of the progress bar.
wxStaticText * m_backgroundTxt
void ShowBackgroundProgressBar(bool aCancellable=false)
Show the background progress bar.
void SetNotificationCount(int aCount)
Set the notification count on the notifications button.
void ShowList(wxWindow *aParent, wxPoint aPos)
Show the notification list.
virtual BACKGROUND_JOBS_MONITOR & GetBackgroundJobMonitor() const
Definition pgm_base.h:134
virtual NOTIFICATIONS_MANAGER & GetNotificationsManager() const
Definition pgm_base.h:139
KICOMMON_API wxSize GetTextSize(const wxString &aSingleLine, wxWindow *aWindow)
Return the size of aSingleLine of text when it is rendered in aWindow using whatever font is currentl...
Definition ui_common.cpp:78
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
PGM_BASE & Pgm()
The global program "get" accessor.
Definition pgm_base.cpp:946
see class PGM_BASE
Functions to provide common constants and other functions to assist in making a consistent UI.