KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 <mark.roszko@gmail.com>
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 <pgm_base.h>
36#include <bitmaps.h>
37#include <wx/dcclient.h>
38
39#define FIELD_OFFSET_BGJOB_TEXT 0
40#define FIELD_OFFSET_BGJOB_GAUGE 1
41#define FIELD_OFFSET_BGJOB_CANCEL 2
42#define FIELD_OFFSET_NOTIFICATION_BUTTON 3
43
44
45KISTATUSBAR::KISTATUSBAR( int aNumberFields, wxWindow* parent, wxWindowID id ) :
46 wxStatusBar( parent, id ),
47 m_normalFieldsCount( aNumberFields )
48{
49#ifdef __WXOSX__
50 // we need +1 extra field on OSX to offset from the rounded corner on the right
51 // OSX doesn't use resize grippers like the other platforms and the statusbar field
52 // includes the rounded part
53 const int ExtraFields = 5;
54#else
55 const int ExtraFields = 4;
56#endif
57 SetFieldsCount( aNumberFields + ExtraFields );
58
59 int* widths = new int[aNumberFields + ExtraFields];
60
61 for( int i = 0; i < aNumberFields; i++ )
62 widths[i] = -1;
63
64 widths[aNumberFields + FIELD_OFFSET_BGJOB_TEXT] = -1; // background status text field
65 // (variable size)
66 widths[aNumberFields + FIELD_OFFSET_BGJOB_GAUGE] = 75; // background progress button
67 widths[aNumberFields + FIELD_OFFSET_BGJOB_CANCEL] = 20; // background stop button
68 widths[aNumberFields + FIELD_OFFSET_NOTIFICATION_BUTTON] = 20; // notifications button
69#ifdef __WXOSX__
70 // offset from the right edge
71 widths[aNumberFields + ExtraFields - 1] = 10;
72#endif
73
74 SetStatusWidths( aNumberFields + ExtraFields, widths );
75 delete[] widths;
76
77
78 int* styles = new int[aNumberFields + ExtraFields];
79
80 for( int i = 0; i < aNumberFields + ExtraFields; i++ )
81 styles[i] = wxSB_FLAT;
82
83 SetStatusStyles( aNumberFields + ExtraFields, styles );
84 delete[] styles;
85
86 m_backgroundTxt = new wxStaticText( this, wxID_ANY, wxT( "" ) );
87
88 m_backgroundProgressBar = new wxGauge( this, wxID_ANY, 100, wxDefaultPosition, wxDefaultSize,
89 wxGA_HORIZONTAL | wxGA_SMOOTH );
90
91 m_backgroundStopButton = new wxButton( this, wxID_ANY, "X", wxDefaultPosition, wxDefaultSize,
92 wxBU_EXACTFIT );
93
94 m_notificationsButton = new BITMAP_BUTTON( this, wxID_ANY, wxNullBitmap, wxDefaultPosition,
95 wxDefaultSize, wxBU_EXACTFIT );
96
98 m_notificationsButton->SetBitmap( KiBitmapBundle( BITMAPS::notifications ) );
101
103
104 Bind( wxEVT_SIZE, &KISTATUSBAR::onSize, this );
106
108 Layout();
109}
110
111
113{
114 m_notificationsButton->Unbind( wxEVT_BUTTON, &KISTATUSBAR::onNotificationsIconClick, this );
115 Unbind( wxEVT_SIZE, &KISTATUSBAR::onSize, this );
117 this );
118}
119
120
121void KISTATUSBAR::onNotificationsIconClick( wxCommandEvent& aEvent )
122{
123 wxPoint pos = m_notificationsButton->GetScreenPosition();
124
125 wxRect r;
126 GetFieldRect( m_normalFieldsCount + 3, r );
127 pos.x += r.GetWidth();
128
129 Pgm().GetNotificationsManager().ShowList( this, pos );
130}
131
132
133void KISTATUSBAR::onBackgroundProgressClick( wxMouseEvent& aEvent )
134{
135 wxPoint pos = m_backgroundProgressBar->GetScreenPosition();
136
137 wxRect r;
138 GetFieldRect( m_normalFieldsCount + 1, r );
139 pos.x += r.GetWidth();
140
141 Pgm().GetBackgroundJobMonitor().ShowList( this, pos );
142}
143
144void KISTATUSBAR::onSize( wxSizeEvent& aEvent )
145{
146 wxRect r;
147 GetFieldRect( m_normalFieldsCount + FIELD_OFFSET_BGJOB_TEXT, r );
148 int x = r.GetLeft();
149 int y = r.GetTop();
150
151 m_backgroundTxt->SetPosition( { x, y } );
152
154 x = r.GetLeft();
155 y = r.GetTop();
156 int w = r.GetWidth();
157 int h = r.GetHeight();
158 constexpr int b = 5;
159
160 auto buttonSize = m_backgroundStopButton->GetEffectiveMinSize();
161 m_backgroundStopButton->SetPosition( { x + w - buttonSize.GetWidth(), y } );
162 m_backgroundStopButton->SetSize( buttonSize.GetWidth(), h );
163
164 m_backgroundProgressBar->SetPosition( { x, y } );
165 m_backgroundProgressBar->SetSize( w - buttonSize.GetWidth() - b, h );
166
168 x = r.GetLeft();
169 y = r.GetTop();
170 h = r.GetHeight();
171 buttonSize = m_notificationsButton->GetEffectiveMinSize();
172 m_notificationsButton->SetPosition( { x, y } );
173 m_notificationsButton->SetSize( buttonSize.GetWidth() + 6, h );
174}
175
176
178{
180
181 if( aCancellable )
183 else
185}
186
187
189{
192}
193
194
196{
197 m_backgroundProgressBar->SetValue( aAmount );
198}
199
200
202{
203 m_backgroundProgressBar->SetRange( aAmount );
204}
205
206
207void KISTATUSBAR::SetBackgroundStatusText( const wxString& aTxt )
208{
209 m_backgroundTxt->SetLabel( aTxt );
210}
211
212
214{
215 wxString cnt = "";
216
217 if( aCount > 0 )
218 cnt = fmt::format( "{}", aCount );
219
221
222 // force a repaint or it wont until it gets activity
223 Refresh();
224}
225
226#include <widgets/ui_common.h>
227void KISTATUSBAR::SetEllipsedTextField( const wxString& aText, int aFieldId )
228{
229 wxRect fieldRect;
230 int width = -1;
231 wxString etext = aText;
232
233 // Only GetFieldRect() returns the current size for variable size fields
234 // Other methods return -1 for the width of these fields.
235 if( GetFieldRect( aFieldId, fieldRect ) )
236 width = fieldRect.GetWidth();
237
238 if( width > 20 )
239 {
240 wxClientDC dc( this );
241
242 // Gives a margin to the text to be sure it is not clamped at its end
243 int margin = KIUI::GetTextSize( wxT( "XX" ), this ).x;
244 etext = wxControl::Ellipsize( etext, dc, wxELLIPSIZE_MIDDLE, width - margin );
245 }
246
247 SetStatusText( etext, aFieldId );
248}
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.
Definition: bitmap_button.h:42
void SetBadgeText(const wxString &aText)
void SetBitmapCentered(bool aCentered=true)
void SetBitmap(const wxBitmapBundle &aBmp)
Set the bitmap shown when the button is enabled.
void SetShowBadge(bool aShowBadge)
void SetPadding(int aPadding)
Set the amount of padding present on each side of the bitmap.
BITMAP_BUTTON * m_notificationsButton
Definition: kistatusbar.h:100
void SetBackgroundStatusText(const wxString &aTxt)
Set the status text that displays next to the progress bar.
void onBackgroundProgressClick(wxMouseEvent &aEvent)
void onSize(wxSizeEvent &aEvent)
int m_normalFieldsCount
Definition: kistatusbar.h:101
void SetBackgroundProgress(int aAmount)
Set the current progress of the progress bar.
wxGauge * m_backgroundProgressBar
Definition: kistatusbar.h:97
wxButton * m_backgroundStopButton
Definition: kistatusbar.h:98
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.
KISTATUSBAR(int aNumberFields, wxWindow *parent, wxWindowID id)
Definition: kistatusbar.cpp:45
wxStaticText * m_backgroundTxt
Definition: kistatusbar.h:99
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:129
virtual NOTIFICATIONS_MANAGER & GetNotificationsManager() const
Definition: pgm_base.h:134
#define FIELD_OFFSET_BGJOB_TEXT
Definition: kistatusbar.cpp:39
#define FIELD_OFFSET_BGJOB_GAUGE
Definition: kistatusbar.cpp:40
#define FIELD_OFFSET_NOTIFICATION_BUTTON
Definition: kistatusbar.cpp:42
#define FIELD_OFFSET_BGJOB_CANCEL
Definition: kistatusbar.cpp:41
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:1071
see class PGM_BASE
Functions to provide common constants and other functions to assist in making a consistent UI.