File: wx_collapsible_pane.cpp

package info (click to toggle)
kicad 9.0.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 770,320 kB
  • sloc: cpp: 961,692; ansic: 121,001; xml: 66,428; python: 18,387; sh: 1,010; awk: 301; asm: 292; makefile: 227; javascript: 167; perl: 10
file content (437 lines) | stat: -rw-r--r-- 11,405 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <bitmaps.h>
#include <widgets/wx_collapsible_pane.h>

#include <wx/collpane.h>
#include <wx/dc.h>
#include <wx/dcclient.h>
#include <wx/panel.h>
#include <wx/renderer.h>
#include <wx/settings.h>
#include <wx/sizer.h>
#include <wx/toplevel.h>
#include <wx/window.h>

#ifdef _WIN32
#include <windows.h>
#endif

#include <algorithm>


wxDEFINE_EVENT( WX_COLLAPSIBLE_PANE_HEADER_CHANGED, wxCommandEvent );
wxDEFINE_EVENT( WX_COLLAPSIBLE_PANE_CHANGED, wxCommandEvent );


bool WX_COLLAPSIBLE_PANE:: Create( wxWindow* aParent, wxWindowID aId, const wxString& aLabel,
                                   const wxPoint& aPos, const wxSize& aSize, long aStyle,
                                   const wxValidator& aValidator, const wxString& aName )
{
    if( !wxControl::Create( aParent, aId, aPos, aSize, aStyle, aValidator, aName ) )
        return false;

    m_sizer = new wxBoxSizer( wxVERTICAL );

    m_header = new WX_COLLAPSIBLE_PANE_HEADER( this, wxID_ANY, aLabel, wxPoint( 0, 0 ),
                                               wxDefaultSize );

    m_sizer->Add( m_header, wxSizerFlags().Border( wxBOTTOM, getBorder() ) );

    m_pane = new wxPanel( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
                          wxTAB_TRAVERSAL | wxNO_BORDER, wxT( "COLLAPSIBLE_PANE_PANE" ) );

    m_pane->Hide();

    Bind( wxEVT_SIZE, &WX_COLLAPSIBLE_PANE::onSize, this );
    Bind( WX_COLLAPSIBLE_PANE_HEADER_CHANGED, &WX_COLLAPSIBLE_PANE::onHeaderClicked, this );

    return true;
}


void WX_COLLAPSIBLE_PANE::init()
{
    m_pane   = nullptr;
    m_sizer  = nullptr;
    m_header = nullptr;
}


WX_COLLAPSIBLE_PANE::~WX_COLLAPSIBLE_PANE()
{
    if( m_sizer )
        m_sizer->SetContainingWindow( nullptr );

    // Not owned by wxWindow
    delete m_sizer;
}


void WX_COLLAPSIBLE_PANE::Collapse( bool aCollapse )
{
    if( IsCollapsed() == aCollapse )
        return;

    InvalidateBestSize();

    m_pane->Show( !aCollapse );
    m_header->SetCollapsed( aCollapse );

    SetSize( GetBestSize() );
}


bool WX_COLLAPSIBLE_PANE::IsCollapsed() const
{
    return !m_pane || !m_pane->IsShown();
}


void WX_COLLAPSIBLE_PANE::SetLabel( const wxString& aLabel )
{
    m_header->SetLabel( aLabel );
    m_header->SetInitialSize();

    Layout();
}


bool WX_COLLAPSIBLE_PANE::SetBackgroundColour( const wxColour& aColor )
{
    m_header->SetBackgroundColour( aColor );
    return wxWindow::SetBackgroundColour( aColor );
}


bool WX_COLLAPSIBLE_PANE::InformFirstDirection( int aDirection, int aSize, int aAvailableOtherDir )
{
    wxWindow* const pane = GetPane();

    if( !pane )
        return false;

    if( !pane->InformFirstDirection( aDirection, aSize, aAvailableOtherDir ) )
        return false;

    InvalidateBestSize();

    return true;
}


wxSize WX_COLLAPSIBLE_PANE::DoGetBestClientSize() const
{
    wxSize size = m_sizer->GetMinSize();

    if( IsExpanded() )
    {
        wxSize paneSize = m_pane->GetBestSize();

        size.SetWidth( std::max( size.GetWidth(), paneSize.x ) );
        size.SetHeight( size.y +  getBorder() + paneSize.y );
    }

    return size;
}


bool WX_COLLAPSIBLE_PANE::Layout()
{
    if( !m_sizer || !m_pane || !m_header )
        return false;

    wxSize size( GetSize() );

    m_sizer->SetDimension( 0, 0, size.x, m_sizer->GetMinSize().y );
    m_sizer->Layout();

    if( IsExpanded() )
    {
        int yoffset = m_sizer->GetSize().y + getBorder();
        m_pane->SetSize( 0, yoffset, size.x, size.y - yoffset );
        m_pane->Layout();
    }

    return true;
}


int WX_COLLAPSIBLE_PANE::getBorder() const
{
#if defined( __WXMSW__ )
    wxASSERT( m_header );
    return m_header->ConvertDialogToPixels( wxSize( 2, 0 ) ).x;
#else
    return 3;
#endif
}


void WX_COLLAPSIBLE_PANE::onSize( wxSizeEvent& aEvent )
{
    Layout();
    aEvent.Skip();
}


void WX_COLLAPSIBLE_PANE::onHeaderClicked( wxCommandEvent& aEvent )
{
    if( aEvent.GetEventObject() != m_header )
    {
        aEvent.Skip();
        return;
    }

    Collapse( !IsCollapsed() );

    wxCommandEvent evt( WX_COLLAPSIBLE_PANE_CHANGED, GetId() );
    evt.SetEventObject( this );
    ProcessEvent( evt );
}


// WX_COLLAPSIBLE_PANE_HEADER implementation


void WX_COLLAPSIBLE_PANE_HEADER::init()
{
    m_collapsed = true;
    m_inWindow  = false;
}


bool WX_COLLAPSIBLE_PANE_HEADER::Create( wxWindow* aParent, wxWindowID aId, const wxString& aLabel,
                                         const wxPoint& aPos, const wxSize& aSize, long aStyle,
                                         const wxValidator& aValidator, const wxString& aName )
{
    if( !wxControl::Create( aParent, aId, aPos, aSize, aStyle, aValidator, aName ) )
        return false;

    SetLabel( aLabel );

    Bind( wxEVT_PAINT, &WX_COLLAPSIBLE_PANE_HEADER::onPaint, this );
    Bind( wxEVT_SET_FOCUS, &WX_COLLAPSIBLE_PANE_HEADER::onFocus, this );
    Bind( wxEVT_KILL_FOCUS, &WX_COLLAPSIBLE_PANE_HEADER::onFocus, this );
    Bind( wxEVT_ENTER_WINDOW, &WX_COLLAPSIBLE_PANE_HEADER::onEnterWindow, this);
    Bind( wxEVT_LEAVE_WINDOW, &WX_COLLAPSIBLE_PANE_HEADER::onLeaveWindow, this);
    Bind( wxEVT_LEFT_UP, &WX_COLLAPSIBLE_PANE_HEADER::onLeftUp, this );
    Bind( wxEVT_CHAR, &WX_COLLAPSIBLE_PANE_HEADER::onChar, this );

    return true;
}


void WX_COLLAPSIBLE_PANE_HEADER::SetCollapsed( bool aCollapsed )
{
    m_collapsed = aCollapsed;
    Refresh();
}


void WX_COLLAPSIBLE_PANE_HEADER::doSetCollapsed( bool aCollapsed )
{
    SetCollapsed( aCollapsed );

    wxCommandEvent evt( WX_COLLAPSIBLE_PANE_HEADER_CHANGED, GetId() );
    evt.SetEventObject( this );
    ProcessEvent( evt );
}


wxSize WX_COLLAPSIBLE_PANE_HEADER::DoGetBestClientSize() const
{
    WX_COLLAPSIBLE_PANE_HEADER* self = const_cast<WX_COLLAPSIBLE_PANE_HEADER*>( this );

    // The code here parallels that of OnPaint() -- except without drawing.
    wxClientDC dc( self );
    wxString   text;

    wxControl::FindAccelIndex( GetLabel(), &text );

    wxSize size = dc.GetTextExtent( text );

    // Reserve space for arrow (which is a square the height of the text)
    size.x += size.GetHeight();

#ifdef __WXMSW__
    size.IncBy( GetSystemMetrics( SM_CXFOCUSBORDER ),
                GetSystemMetrics( SM_CYFOCUSBORDER ) );
#endif // __WXMSW__

    return size;
}


void WX_COLLAPSIBLE_PANE_HEADER::onPaint( wxPaintEvent& aEvent )
{
    wxPaintDC dc( this );
    wxRect    rect( wxPoint( 0, 0 ), GetClientSize() );

#ifdef __WXMSW__
    wxBrush brush = dc.GetBrush();
    brush.SetColour( GetParent()->GetBackgroundColour() );
    dc.SetBrush( brush );
    dc.SetPen( *wxTRANSPARENT_PEN );
    dc.DrawRectangle( rect );
#endif

    // Make the background look like a button when the pointer is over it
    if( m_inWindow )
    {
        dc.SetBrush( wxBrush( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNHIGHLIGHT ) ) );
        dc.SetPen( *wxTRANSPARENT_PEN );
        dc.DrawRectangle( rect );
    }

    wxString text;
    int indexAccel = wxControl::FindAccelIndex( GetLabel(), &text );

    wxSize textSize = dc.GetTextExtent( text );

    // Compute all the sizes
    wxRect arrowRect( 0, 0, textSize.GetHeight(), textSize.GetHeight() );
    wxRect textRect( arrowRect.GetTopRight(), textSize );
    textRect = textRect.CenterIn( rect, wxVERTICAL );

    // Find out if the window we are in is active or not
    bool              isActive = true;
    wxTopLevelWindow* tlw      = dynamic_cast<wxTopLevelWindow*>( wxGetTopLevelParent( this ) );

    if( tlw && !tlw->IsActive() )
        isActive = false;

    // Draw the arrow
    drawArrow( dc, arrowRect, isActive );

    // We are responsible for showing the text as disabled when the window isn't active
    wxColour clr;

    if( isActive )
        clr = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
    else
        clr = wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT );

    dc.SetTextForeground( clr );
    dc.DrawLabel( text, textRect, wxALIGN_CENTER_VERTICAL, indexAccel );

#ifdef __WXMSW__
    int flags = 0;

    if( m_inWindow )
        flags |= wxCONTROL_CURRENT;

    int focusSize = GetSystemMetrics( SM_CXFOCUSBORDER );

    if( HasFocus() )
        wxRendererNative::Get().DrawFocusRect( this, dc, textRect.Inflate( focusSize ), flags );
#endif
}


void WX_COLLAPSIBLE_PANE_HEADER::onFocus( wxFocusEvent& aEvent )
{
    Refresh();
    aEvent.Skip();
}


void WX_COLLAPSIBLE_PANE_HEADER::onEnterWindow( wxMouseEvent& aEvent )
{
    m_inWindow = true;
    Refresh();
    aEvent.Skip();
}


void WX_COLLAPSIBLE_PANE_HEADER::onLeaveWindow( wxMouseEvent& aEvent )
{
    m_inWindow = false;
    Refresh();
    aEvent.Skip();
}


void WX_COLLAPSIBLE_PANE_HEADER::onLeftUp( wxMouseEvent& aEvent )
{
    doSetCollapsed( !m_collapsed );
    aEvent.Skip();
}


void WX_COLLAPSIBLE_PANE_HEADER::onChar( wxKeyEvent& aEvent )
{
    switch( aEvent.GetKeyCode() )
    {
    case WXK_SPACE:
    case WXK_RETURN:
    case WXK_NUMPAD_ENTER:
        doSetCollapsed( !m_collapsed );
        break;

    default:
        aEvent.Skip();
        break;
    }
}


void WX_COLLAPSIBLE_PANE_HEADER::drawArrow( wxDC& aDC, wxRect aRect, bool aIsActive )
{
    // The bottom corner of the triangle is located halfway across the area and 3/4 down from
    // the top
    wxPoint btmCorner( aRect.GetWidth() / 2, 3 * aRect.GetHeight() / 4 );

    // The right corner of the triangle is located halfway down from the top and 3/4 across the area
    wxPoint rtCorner( 3 * aRect.GetWidth() / 4, aRect.GetHeight() / 2 );

    // Choose the other corner depending on if the panel is expanded or collapsed
    wxPoint otherCorner( 0, 0 );

    if( m_collapsed )
        otherCorner = wxPoint( aRect.GetWidth() / 2, aRect.GetHeight() / 4 );
    else
        otherCorner = wxPoint( aRect.GetWidth() / 4,  aRect.GetHeight() / 2 );

    // Choose the color to draw the triangle
    wxColour clr;

    // Highlight the arrow when the pointer is inside the header, otherwise use text color
    if( m_inWindow )
        clr = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT );
    else
        clr = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );

    // If the window isn't active, then use the disabled text color
    if( !aIsActive )
        clr = wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT );

    // Must set both the pen (for the outline) and the brush (for the polygon fill)
    aDC.SetPen( wxPen( clr ) );
    aDC.SetBrush( wxBrush( clr ) );

    // Draw the triangle
    wxPointList points;
    points.Append( &btmCorner );
    points.Append( &rtCorner );
    points.Append( &otherCorner );

    aDC.DrawPolygon( &points );
}