File: clTabRenderer.cpp

package info (click to toggle)
codelite 17.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 136,204 kB
  • sloc: cpp: 491,547; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 804; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (525 lines) | stat: -rw-r--r-- 15,647 bytes parent folder | download | duplicates (2)
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#include "clTabRenderer.h"

#include "ColoursAndFontsManager.h"
#include "Notebook.h"
#include "clGenericNotebook.hpp"
#include "clSystemSettings.h"
#include "clTabRendererMinimal.hpp"
#include "cl_config.h"
#include "cl_defs.h"
#include "editor_config.h"
#include "globals.h"
#include "macros.h"
#include "wxStringHash.h"

#include <wx/dcclient.h>
#include <wx/dcmemory.h>
#include <wx/renderer.h>
#include <wx/settings.h>
#include <wx/xrc/xmlres.h>

#if CL_BUILD
#include "drawingutils.h"
#endif

namespace
{
void GetTabColours(const clTabColours& colours, size_t style, wxColour* activeTabBgColour, wxColour* bgColour)
{
    *bgColour = colours.tabAreaColour;
    *activeTabBgColour = colours.activeTabBgColour;
    wxUnusedVar(style);
}
int X_BUTTON_SIZE = 20;
void SetBestXButtonSize(wxWindow* win) { wxUnusedVar(win); }

} // namespace

clTabColours::clTabColours() { UpdateColours(0); }

void clTabColours::UpdateColours(size_t notebookStyle)
{
    wxUnusedVar(notebookStyle);
#ifdef __WXMAC__
    wxColour base_colour = clSystemSettings::GetDefaultPanelColour();
    bool is_dark = DrawingUtils::IsDark(base_colour);

    tabAreaColour = base_colour.ChangeLightness(80);
    activeTabBgColour = base_colour; //.ChangeLightness(120);
    activeTabTextColour = clSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT);
    activeTabPenColour = activeTabBgColour.ChangeLightness(is_dark ? 30 : 70);
    activeTabInnerPenColour = activeTabPenColour;

    // inactive tab colours
    inactiveTabTextColour = activeTabTextColour.ChangeLightness(is_dark ? 85 : 115);
    inactiveTabBgColour = tabAreaColour;
    inactiveTabPenColour = tabAreaColour.ChangeLightness(90);
    inactiveTabInnerPenColour = inactiveTabBgColour;
    markerColour = clConfig::Get().Read("ActiveTabMarkerColour", wxColour("#dc7633"));
#else
    wxColour base_colour = clSystemSettings::GetDefaultPanelColour();
    bool is_dark = DrawingUtils::IsDark(base_colour);

    tabAreaColour = base_colour.ChangeLightness(is_dark ? 60 : 90);
    activeTabBgColour = base_colour.ChangeLightness(is_dark ? 107 : 100);
    activeTabTextColour = clSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT);
    activeTabPenColour = base_colour;
    activeTabInnerPenColour = activeTabPenColour;

    // inactive tab colours
    inactiveTabTextColour = activeTabTextColour.ChangeLightness(is_dark ? 85 : 115);
    inactiveTabBgColour = tabAreaColour;
    inactiveTabPenColour = tabAreaColour.ChangeLightness(90);
    inactiveTabInnerPenColour = inactiveTabBgColour;
    markerColour = clConfig::Get().Read("ActiveTabMarkerColour", wxColour("#dc7633"));
#endif
}

bool clTabColours::IsDarkColours() const { return DrawingUtils::IsDark(activeTabBgColour); }

clTabInfo::clTabInfo(clTabCtrl* tabCtrl, size_t style, wxWindow* page, const wxString& text, int bmp)
    : m_bitmap(bmp)
    , m_tabCtrl(tabCtrl)
    , m_label(text)
    , m_window(page)
    , m_active(false)
    , m_textWidth(0)
    , m_xButtonState(eButtonState::kDisabled)
{
    SetBestXButtonSize(tabCtrl);
    CalculateOffsets(style);
    CreateDisabledBitmap();
}

void clTabInfo::CreateDisabledBitmap()
{
    // the disabled image as the same index as the normal one
    m_disabledBitmp = m_bitmap;
}

clTabInfo::clTabInfo(clTabCtrl* tabCtrl)
    : m_tabCtrl(tabCtrl)
    , m_window(NULL)
    , m_active(false)
    , m_textX(wxNOT_FOUND)
    , m_textY(wxNOT_FOUND)
    , m_bmpX(wxNOT_FOUND)
    , m_bmpY(wxNOT_FOUND)
    , m_bmpCloseX(wxNOT_FOUND)
    , m_bmpCloseY(wxNOT_FOUND)
    , m_textWidth(0)
    , m_xButtonState(eButtonState::kDisabled)
{
    SetBestXButtonSize(tabCtrl);
    CalculateOffsets(0);
}

void clTabInfo::CalculateOffsets(size_t style, wxDC& dc)
{
    m_bmpCloseX = wxNOT_FOUND;
    m_bmpCloseY = wxNOT_FOUND;

    int Y_spacer = m_tabCtrl ? m_tabCtrl->GetArt()->ySpacer : 10;
    int X_spacer = m_tabCtrl ? m_tabCtrl->GetArt()->xSpacer : 10;
    bool using_bold_font = m_tabCtrl ? m_tabCtrl->GetArt()->IsUseBoldFont() : false;

    wxDCFontChanger font_changer(dc);
    wxFont font = clTabRenderer::GetTabFont(using_bold_font);
    dc.SetFont(font);

    // On vertical tabs, use the short label
    wxSize sz = dc.GetTextExtent(GetBestLabel(style));
    wxSize fixedHeight = dc.GetTextExtent("Tp");
    m_height = fixedHeight.GetHeight() + (4 * Y_spacer);

    // Make that the tab can contain at least the minimum bitmap height
    int bmpHeight = clTabRenderer::GetDefaultBitmapHeight(Y_spacer);
    m_height = wxMax(m_height, bmpHeight);

    m_width = 0;
    m_width += X_spacer;

    // bitmap
    m_bmpX = wxNOT_FOUND;
    m_bmpY = wxNOT_FOUND;

    // x button
    wxRect xrect;
    if((style & kNotebook_CloseButtonOnActiveTab)) {
        xrect = wxRect(m_width, 0, X_BUTTON_SIZE, X_BUTTON_SIZE);
        m_bmpCloseX = xrect.GetX();
        m_bmpCloseY = 0; // we will fix this later
        m_width += xrect.GetWidth() + m_tabCtrl->FromDIP(5);
    }

    // Text
    m_textX = m_width;
    m_textY = ((m_height - sz.y) / 2);
    m_width += sz.x;
    m_textWidth = sz.x;
    m_width += X_spacer;

    if(HasBitmap()) {
        const wxBitmap& bmp = m_tabCtrl->GetBitmaps()->Get(m_bitmap, false);
        m_bmpX = m_width;
        m_width += bmp.GetScaledWidth();
        m_bmpY = ((m_height - bmp.GetScaledHeight()) / 2);
        m_width += X_spacer;
    }

    // Update the rect width
    m_rect.SetWidth(m_width);
    m_rect.SetHeight(m_height);

    // If we got a X button, adjust its Y axis
    if(!xrect.IsEmpty()) {
        xrect = xrect.CenterIn(m_rect, wxVERTICAL);
        m_bmpCloseY = xrect.GetY();
    }
}

void clTabInfo::CalculateOffsets(size_t style)
{
    if(m_tabCtrl) {
        wxClientDC dc(m_tabCtrl);
        dc.SetFont(DrawingUtils::GetDefaultGuiFont());
        CalculateOffsets(style, dc);
    }
}

void clTabInfo::SetBitmap(int bitmap, size_t style)
{
    this->m_bitmap = bitmap;
    CreateDisabledBitmap();
    CalculateOffsets(style);
}

void clTabInfo::SetLabel(const wxString& label, size_t style)
{
    this->m_label = label;
    CalculateOffsets(style);
}

void clTabInfo::SetActive(bool active, size_t style)
{
    this->m_active = active;
    this->m_xButtonState = active ? eButtonState::kNormal : eButtonState::kDisabled;
    CalculateOffsets(style);
}

wxRect clTabInfo::GetCloseButtonRect() const
{
    wxRect xRect(GetBmpCloseX() + GetRect().x, 0, clTabRenderer::GetXButtonSize(), clTabRenderer::GetXButtonSize());
    xRect.Inflate(2);
    return xRect.CenterIn(GetRect(), wxVERTICAL);
}

const wxString& clTabInfo::GetBestLabel(size_t style) const { return m_label; }

const wxBitmap& clTabInfo::GetBitmap(int index, bool disabled) const
{
    if(!m_tabCtrl) {
        return wxNullBitmap;
    }
    return m_tabCtrl->GetBitmaps()->Get(index, disabled);
}

bool clTabInfo::HasDisableBitmap() const
{
    return m_tabCtrl && m_tabCtrl->GetBitmaps()->Get(m_disabledBitmp, true).IsOk();
}
bool clTabInfo::HasBitmap() const { return m_tabCtrl && m_tabCtrl->GetBitmaps()->Get(m_bitmap, false).IsOk(); }

std::unordered_map<wxString, clTabRenderer*> clTabRenderer::ms_Renderes;

clTabRenderer::clTabRenderer(const wxString& name, const wxWindow* parent)
    : bottomAreaHeight(0)
    , majorCurveWidth(0)
    , smallCurveWidth(0)
    , overlapWidth(0)
    , verticalOverlapWidth(0)
    , ySpacer(5)
    , m_name(name)
{
    xSpacer = 10;
    ySpacer = EditorConfigST::Get()->GetOptions()->GetNotebookTabHeight() + 2;
}

wxFont clTabRenderer::GetTabFont(bool bold)
{
    wxFont f = DrawingUtils::GetDefaultGuiFont();
    if(bold) {
        f.SetWeight(wxFONTWEIGHT_BOLD);
    }
    return f;
}

#define DRAW_LINE(__p1, __p2) \
    dc.DrawLine(__p1, __p2);  \
    dc.DrawLine(__p1, __p2);  \
    dc.DrawLine(__p1, __p2);  \
    dc.DrawLine(__p1, __p2);

void clTabRenderer::ClearActiveTabExtraLine(clTabInfo::Ptr_t activeTab, wxDC& dc, const clTabColours& colours,
                                            size_t style)
{
    wxPoint pt1, pt2;
    dc.SetPen(colours.activeTabPenColour);
    if(style & kNotebook_BottomTabs) {
        // bottom tabs
        dc.SetPen(colours.activeTabBgColour);
        pt1 = activeTab->GetRect().GetTopLeft();
        pt2 = activeTab->GetRect().GetTopRight();
        pt1.x += 1;
        pt2.x -= 1;
        DRAW_LINE(pt1, pt2);

    } else {
        // Top tabs
        dc.SetPen(colours.activeTabBgColour);
        pt1 = activeTab->GetRect().GetBottomLeft();
        pt2 = activeTab->GetRect().GetBottomRight();
#ifndef __WXOSX__
        pt1.x += 1;
        pt2.x -= 1;
#else
        pt1.x += 2; // Skip the marker on the left side drawn in pen 3 pixel width
                    //        pt2.x -= 1;
#endif
        DRAW_LINE(pt1, pt2);
        pt1.y += 1;
        pt2.y += 1;
        DRAW_LINE(pt1, pt2);
    }
}

void clTabRenderer::DrawButton(wxWindow* win, wxDC& dc, const clTabInfo& tabInfo, const clTabColours& colours,
                               eButtonState state)
{
    // Draw the X button
    wxRect buttonRect = wxRect(tabInfo.m_bmpCloseX + tabInfo.GetRect().GetX(),
                               tabInfo.m_bmpCloseY + tabInfo.GetRect().GetY(), X_BUTTON_SIZE, X_BUTTON_SIZE);
    buttonRect = buttonRect.CenterIn(tabInfo.GetRect(), wxVERTICAL);

    wxColour text_colour = colours.activeTabTextColour;
    wxString unicode_symbol = wxT("\u2715");
    if(tabInfo.IsModified()) {
        // instead of drawing the standard X we draw
        // a round circele marked in RED indicating that this
        // tab is modified
        text_colour = colours.markerColour;
        unicode_symbol = wxT("\u25CF");
    }

    DrawingUtils::DrawButtonX(dc, win, buttonRect, text_colour,
                              tabInfo.IsActive() ? colours.activeTabBgColour : colours.inactiveTabBgColour, state,
                              unicode_symbol);
}

void clTabRenderer::DrawChevron(wxWindow* win, wxDC& dc, const wxRect& rect, const clTabColours& colours)
{
    wxColour buttonColour;
    if(DrawingUtils::IsDark(colours.tabAreaColour)) {
        buttonColour = colours.tabAreaColour.ChangeLightness(150);
    } else {
        buttonColour = colours.tabAreaColour.ChangeLightness(50);
    }
    DrawingUtils::DrawDropDownArrow(win, dc, rect, buttonColour);
}

int clTabRenderer::GetDefaultBitmapHeight(int Y_spacer)
{
    int bmpHeight = 0;
    wxBitmap dummyBmp = clGetManager()->GetStdIcons()->LoadBitmap("cog");
    if(dummyBmp.IsOk()) {
        bmpHeight = dummyBmp.GetScaledHeight() + (2 * Y_spacer);
    }
    return bmpHeight;
}

clTabRenderer::Ptr_t clTabRenderer::CreateRenderer(const wxWindow* win, size_t tabStyle)
{
    if(ms_Renderes.empty()) {
        RegisterRenderer(new clTabRendererMinimal(win));
    }
    clTabRenderer::Ptr_t renderer;
    renderer = Create(win, "MINIMAL");
    return renderer;
}

wxArrayString clTabRenderer::GetRenderers()
{
    if(ms_Renderes.empty()) {
        RegisterRenderer(new clTabRendererMinimal(nullptr));
    }

    wxArrayString renderers;
    renderers.reserve(ms_Renderes.size());

    for(auto vt : ms_Renderes) {
        renderers.Add(vt.first);
    }
    renderers.Sort();
    return renderers;
}

int clTabRenderer::GetMarkerWidth()
{
#ifdef __WXOSX__
    return 2;
#else
    return 3;
#endif
}

wxColour clTabRenderer::DrawBackground(wxWindow* parent, wxDC& dc, const wxRect& clientRect,
                                       const clTabColours& colours, size_t style)
{
    wxUnusedVar(parent);
    wxUnusedVar(colours);
    wxUnusedVar(style);
#ifdef __WXOSX__
    dc.SetPen(colours.tabAreaColour);
#else
    dc.SetPen(colours.inactiveTabPenColour);
#endif
    dc.SetBrush(colours.tabAreaColour);
    dc.DrawRectangle(clientRect);

#ifndef __WXOSX__
    bool isBottom = style & kNotebook_BottomTabs;

    wxPoint p1, p2;
    if(isBottom) {
        p1 = clientRect.GetTopLeft();
        p2 = clientRect.GetTopRight();
    } else {
        p1 = clientRect.GetBottomLeft();
        p2 = clientRect.GetBottomRight();
    }
    dc.SetPen(colours.inactiveTabPenColour);
    dc.DrawLine(p1, p2);
#endif
    return colours.tabAreaColour;
}

void clTabRenderer::FinaliseBackground(wxWindow* parent, wxDC& dc, const wxRect& clientRect,
                                       const wxRect& activeTabRect, const clTabColours& colours, size_t style)
{
    wxUnusedVar(parent);
    wxUnusedVar(activeTabRect);

    // top tabs
    wxColour bg_colour;
    wxColour active_tab_colour;
    GetTabColours(colours, style, &active_tab_colour, &bg_colour);
    bool is_dark = DrawingUtils::IsDark(bg_colour);
    dc.SetPen(bg_colour.ChangeLightness(is_dark ? 60 : 80));
    //dc.DrawLine(clientRect.GetTopRight(), clientRect.GetTopLeft());
    //dc.DrawLine(clientRect.GetTopLeft(), clientRect.GetBottomLeft());
    //dc.DrawLine(clientRect.GetTopRight(), clientRect.GetBottomRight());
}

void clTabRenderer::AdjustColours(clTabColours& colours, size_t style)
{
    wxUnusedVar(style);
    wxUnusedVar(colours);
}

void clTabRenderer::DrawMarker(wxDC& dc, const clTabInfo& tabInfo, const clTabColours& colours, size_t style)
{
    if((style & kNotebook_UnderlineActiveTab) == 0) {
        return;
    }

    bool is_dark = DrawingUtils::IsDark(colours.tabAreaColour);
#ifdef __WXMAC__
    wxColour marker_colour = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
#else
    wxColour marker_colour = colours.tabAreaColour.ChangeLightness(is_dark ? 130 : 70);
#endif
    wxPen markerPen(marker_colour);

    // Draw marker line if needed
    // wxRect confinedRect = parent->GetClientRect();
    bool isGTK3 = true;
    wxDirection direction;
    wxPoint p1, p2;
    if(style & kNotebook_BottomTabs) {
        // Bottom tabs
        if(isGTK3) {
            direction = wxDOWN;
            p1 = tabInfo.GetRect().GetTopLeft();
            p2 = tabInfo.GetRect().GetTopRight();
        } else {
            direction = wxRIGHT;
            p1 = tabInfo.GetRect().GetTopLeft();
            p2 = tabInfo.GetRect().GetBottomLeft();
            p1.x--;
            p2.x--;
        }
        dc.SetPen(markerPen);
        DrawMarkerLine(dc, p1, p2, direction);
    } else {
        // Top tabs
        if(isGTK3) {
            direction = wxUP;
            p1 = tabInfo.GetRect().GetBottomLeft();
            p2 = tabInfo.GetRect().GetBottomRight();

        } else {
            direction = wxRIGHT;
            p1 = tabInfo.GetRect().GetTopLeft();
            p2 = tabInfo.GetRect().GetBottomLeft();
            p1.x--;
            p2.x--;
        }
        dc.SetPen(markerPen);
        DrawMarkerLine(dc, p1, p2, direction);
    }
}

void clTabRenderer::DrawMarkerLine(wxDC& dc, const wxPoint& p1, const wxPoint& p2, wxDirection direction)
{
    const int width = GetMarkerWidth();
    wxPoint point1 = p1;
    wxPoint point2 = p2;
    for(int i = 0; i < width; ++i) {
        dc.DrawLine(point1, point2);
        if(direction == wxDOWN) {
            point1.y++;
            point2.y++;
        } else if(direction == wxUP) {
            point1.y--;
            point2.y--;
        } else if(direction == wxLEFT) {
            point1.x--;
            point2.x--;
        } else {
            // wxRIGHT
            point1.x++;
            point2.x++;
        }
    }
}

int clTabRenderer::GetXButtonSize() { return X_BUTTON_SIZE; }

void clTabRenderer::RegisterRenderer(clTabRenderer* renderer)
{
    if(!renderer) {
        return;
    }
    if(ms_Renderes.count(renderer->GetName())) {
        return;
    }
    ms_Renderes.insert({ renderer->GetName(), renderer });
}

clTabRenderer* clTabRenderer::Create(const wxWindow* parent, const wxString& name)
{
    if(ms_Renderes.count(name) == 0) {
        return nullptr;
    }
    return ms_Renderes.find(name)->second->New(parent);
}