File: theme_handler_helper.cpp

package info (click to toggle)
codelite 14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 112,816 kB
  • sloc: cpp: 483,662; ansic: 150,144; php: 9,569; lex: 4,186; python: 3,417; yacc: 2,820; sh: 1,147; makefile: 52; xml: 13
file content (230 lines) | stat: -rw-r--r-- 8,725 bytes parent folder | download
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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2014 Eran Ifrah
// file name            : theme_handler_helper.cpp
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    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 2 of the License, or
//    (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

#include "ColoursAndFontsManager.h"
#include "Notebook.h"
#include "clTabRendererClassic.h"
#include "clTabRendererCurved.h"
#include "clTabRendererSquare.h"
#include "editor_config.h"
#include "event_notifier.h"
#include "plugin.h"
#include "theme_handler_helper.h"
#include <algorithm>
#include <wx/aui/auibar.h>
#include <wx/bitmap.h>
#include <wx/button.h>
#include <wx/dataview.h>
#include <wx/html/htmlwin.h>
#include <wx/listbox.h>
#include <wx/listctrl.h>
#include <wx/stc/stc.h>
#include <wx/textctrl.h>
#include <wx/treectrl.h>
#include "clSystemSettings.h"
#if USE_AUI_NOTEBOOK
#include "clAuiMainNotebookTabArt.h"
#include <wx/aui/tabart.h>
#endif

#define IS_TYPEOF(Type, Win) (dynamic_cast<Type*>(Win))

ThemeHandlerHelper::ThemeHandlerHelper(wxWindow* win)
    : m_window(win)
{
    EventNotifier::Get()->Bind(wxEVT_CL_THEME_CHANGED, &ThemeHandlerHelper::OnThemeChanged, this);
    EventNotifier::Get()->Bind(wxEVT_CMD_COLOURS_FONTS_UPDATED, &ThemeHandlerHelper::OnColoursUpdated, this);
    EventNotifier::Get()->Bind(wxEVT_EDITOR_SETTINGS_CHANGED, &ThemeHandlerHelper::OnPreferencesUpdated, this);
}

ThemeHandlerHelper::~ThemeHandlerHelper()
{
    EventNotifier::Get()->Unbind(wxEVT_CMD_COLOURS_FONTS_UPDATED, &ThemeHandlerHelper::OnColoursUpdated, this);
    EventNotifier::Get()->Unbind(wxEVT_CL_THEME_CHANGED, &ThemeHandlerHelper::OnThemeChanged, this);
    EventNotifier::Get()->Unbind(wxEVT_EDITOR_SETTINGS_CHANGED, &ThemeHandlerHelper::OnPreferencesUpdated, this);
}

void ThemeHandlerHelper::OnThemeChanged(wxCommandEvent& e)
{
    e.Skip();
    UpdateColours(m_window);
}

void ThemeHandlerHelper::UpdateColours(wxWindow* topWindow)
{
    // Collect all toolbars
    std::queue<wxWindow*> q;
    std::vector<wxAuiToolBar*> toolbars;
    std::vector<wxWindow*> candidateWindows;
    q.push(topWindow);

    wxColour bgColour = clSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
    wxColour fgColour = clSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT);

    LexerConf::Ptr_t textLexer = EditorConfigST::Get()->GetLexer("text");
    while(!q.empty()) {
        wxWindow* w = q.front();
        q.pop();
        if(dynamic_cast<wxAuiToolBar*>(w)) {
            toolbars.push_back(dynamic_cast<wxAuiToolBar*>(w));
        } else {
            if(IS_TYPEOF(wxListBox, w) || IS_TYPEOF(wxDataViewCtrl, w) || IS_TYPEOF(wxListCtrl, w)) {
                w->SetBackgroundColour(bgColour);
                w->SetForegroundColour(fgColour);
                w->Refresh();
            } else if(IS_TYPEOF(wxStyledTextCtrl, w)) {
                // wxSTC requires different method
                wxStyledTextCtrl* stc = dynamic_cast<wxStyledTextCtrl*>(w);
                if(stc->GetLexer() == wxSTC_LEX_NULL) {
                    if(!textLexer) {
                        // Only modify text lexers
                        for(int i = 0; i < wxSTC_STYLE_MAX; i++) {
                            stc->StyleSetBackground(i, bgColour);
                            stc->StyleSetForeground(i, fgColour);
                        }

                    } else {
                        textLexer->Apply(stc);
                    }
                }
                w->Refresh();
            }
            wxWindowList::compatibility_iterator iter = w->GetChildren().GetFirst();
            while(iter) {
                q.push(iter->GetData());
                iter = iter->GetNext();
            }
        }
    }

    //    std::for_each(toolbars.begin(), toolbars.end(), [&](wxAuiToolBar* tb) {
    //        // Update the art if needed
    //        CLMainAuiTBArt* art = dynamic_cast<CLMainAuiTBArt*>(tb->GetArtProvider());
    //        if(!art) { tb->SetArtProvider(new CLMainAuiTBArt()); }
    //
    //#ifndef __WXOSX__
    //        for(size_t i = 0; i < tb->GetToolCount(); ++i) {
    //            wxAuiToolBarItem* tbItem = tb->FindToolByIndex(i);
    //            if(tbItem->GetBitmap().IsOk() &&
    //               (tbItem->GetKind() == wxITEM_NORMAL || tbItem->GetKind() == wxITEM_CHECK ||
    //                tbItem->GetKind() == wxITEM_DROPDOWN || tbItem->GetKind() == wxITEM_RADIO)) {
    //                tbItem->SetDisabledBitmap(DrawingUtils::CreateDisabledBitmap(tbItem->GetBitmap()));
    //            }
    //        }
    //#endif
    //        tb->Refresh();
    //    });

    DoUpdateNotebookStyle(m_window);
}

#if USE_AUI_NOTEBOOK
class MySimpleTabArt : public wxAuiSimpleTabArt
{
    wxColour m_activeTabBgColour;

public:
    MySimpleTabArt() { m_activeTabBgColour = *wxWHITE; }
    virtual ~MySimpleTabArt() {}
    wxAuiTabArt* Clone() { return new MySimpleTabArt(*this); }

    void DrawTab(wxDC& dc, wxWindow* wnd, const wxAuiNotebookPage& pane, const wxRect& inRect, int closeButtonState,
                 wxRect* outTabRect, wxRect* outButtonRect, int* xExtent)
    {
        if(pane.active) {
            if(DrawingUtils::IsDark(m_activeTabBgColour)) { dc.SetTextForeground(*wxWHITE); }
        } else {
            // set the default text colour
            dc.SetTextForeground(DrawingUtils::GetPanelTextColour());
        }
        wxAuiSimpleTabArt::DrawTab(dc, wnd, pane, inRect, closeButtonState, outTabRect, outButtonRect, xExtent);
    }

    void SetActiveColour(const wxColour& colour)
    {
        m_activeTabBgColour = colour;
        wxAuiSimpleTabArt::SetActiveColour(colour);
    }
};
class MyDefaultTabArt : public wxAuiGenericTabArt
{
    wxColour m_activeTabBgColour;

public:
    MyDefaultTabArt() { m_activeTabBgColour = *wxWHITE; }
    virtual ~MyDefaultTabArt() {}
    wxAuiTabArt* Clone() { return new MyDefaultTabArt(*this); }

    void DrawTab(wxDC& dc, wxWindow* wnd, const wxAuiNotebookPage& pane, const wxRect& inRect, int closeButtonState,
                 wxRect* outTabRect, wxRect* outButtonRect, int* xExtent)
    {
        if(pane.active) {
            if(DrawingUtils::IsDark(m_activeTabBgColour)) { dc.SetTextForeground(*wxWHITE); }
        } else {
            // set the default text colour
            dc.SetTextForeground(DrawingUtils::GetPanelTextColour());
        }
        wxAuiGenericTabArt::DrawTab(dc, wnd, pane, inRect, closeButtonState, outTabRect, outButtonRect, xExtent);
    }

    void SetActiveColour(const wxColour& colour)
    {
        m_activeTabBgColour = colour;
        wxAuiGenericTabArt::SetActiveColour(colour);
    }
};
#endif
void ThemeHandlerHelper::DoUpdateNotebookStyle(wxWindow* win)
{
    if(!win) { return; }
    if(dynamic_cast<Notebook*>(win)) {
        Notebook* book = dynamic_cast<Notebook*>(win);
        book->SetArt(clTabRenderer::CreateRenderer(book, book->GetStyle()));
        LexerConf::Ptr_t lexer = ColoursAndFontsManager::Get().GetLexer("text");
        wxColour activeTabBgColuor;
        if(lexer) { activeTabBgColuor = lexer->GetProperty(0).GetBgColour(); }

        // Enable tab switching using the mouse scrollbar
        book->EnableStyle(kNotebook_MouseScrollSwitchTabs,
                          EditorConfigST::Get()->GetOptions()->IsMouseScrollSwitchTabs());
    }
    
    wxWindowList::compatibility_iterator pclNode = win->GetChildren().GetFirst();
    while(pclNode) {
        wxWindow* pclChild = pclNode->GetData();
        this->DoUpdateNotebookStyle(pclChild);
        pclNode = pclNode->GetNext();
    }
}

void ThemeHandlerHelper::OnPreferencesUpdated(wxCommandEvent& e)
{
    e.Skip();
    DoUpdateNotebookStyle(m_window);
}

void ThemeHandlerHelper::OnColoursUpdated(clCommandEvent& e) { e.Skip(); }

void ThemeHandlerHelper::UpdateNotebookColours(wxWindow* topWindow) { DoUpdateNotebookStyle(topWindow); }