File: tweaks.cpp

package info (click to toggle)
codelite 12.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 95,112 kB
  • sloc: cpp: 424,040; ansic: 18,284; php: 9,569; lex: 4,186; yacc: 2,820; python: 2,294; sh: 312; makefile: 51; xml: 13
file content (276 lines) | stat: -rw-r--r-- 8,818 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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2014 Eran Ifrah
// file name            : tweaks.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 "tweaks.h"
#include <wx/xrc/xmlres.h>
#include <wx/menu.h>
#include "event_notifier.h"
#include "TweaksSettingsDlg.h"
#include "editor_config.h"
#include "workspace.h"

static Tweaks* thePlugin = NULL;

static int ID_TWEAKS_SETTINGS = ::wxNewId();

#define TWEAKS_ENABLED_EVENT_HANDLER()                                       \
    if(!m_settings.IsEnableTweaks() || !clCxxWorkspaceST::Get()->IsOpen()) { \
        e.Skip();                                                            \
        return;                                                              \
    }

// Define the plugin entry point
CL_PLUGIN_API IPlugin* CreatePlugin(IManager* manager)
{
    if(thePlugin == 0) {
        thePlugin = new Tweaks(manager);
    }
    return thePlugin;
}

CL_PLUGIN_API PluginInfo* GetPluginInfo()
{
    static PluginInfo info;
    info.SetAuthor(wxT("eran"));
    info.SetName(wxT("Tweaks"));
    info.SetDescription(_("Tweak codelite"));
    info.SetVersion(wxT("v1.0"));
    return &info;
}

CL_PLUGIN_API int GetPluginInterfaceVersion() { return PLUGIN_INTERFACE_VERSION; }

Tweaks::Tweaks(IManager* manager)
    : IPlugin(manager)
{
    wxPGInitResourceModule(); // Or we crash...

    m_longName = _("Tweak codelite");
    m_shortName = wxT("Tweaks");

    m_mgr->GetTheApp()->Connect(
        ID_TWEAKS_SETTINGS, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Tweaks::OnSettings), NULL, this);
    EventNotifier::Get()->Connect(wxEVT_COLOUR_TAB, clColourEventHandler(Tweaks::OnColourTab), NULL, this);
    EventNotifier::Get()->Connect(wxEVT_WORKSPACE_LOADED, wxCommandEventHandler(Tweaks::OnWorkspaceLoaded), NULL, this);
    EventNotifier::Get()->Connect(wxEVT_WORKSPACE_CLOSED, wxCommandEventHandler(Tweaks::OnWorkspaceClosed), NULL, this);
    EventNotifier::Get()->Connect(
        wxEVT_WORKSPACE_VIEW_BUILD_STARTING, clCommandEventHandler(Tweaks::OnFileViewBuildTree), NULL, this);
    EventNotifier::Get()->Connect(
        wxEVT_WORKSPACE_VIEW_CUSTOMIZE_PROJECT, clColourEventHandler(Tweaks::OnCustomizeProject), NULL, this);
    EventNotifier::Get()->Connect(
        wxEVT_GET_TAB_BORDER_COLOUR, clColourEventHandler(Tweaks::OnTabBorderColour), NULL, this);
}

void Tweaks::UnPlug()
{
    m_mgr->GetTheApp()->Disconnect(
        ID_TWEAKS_SETTINGS, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Tweaks::OnSettings), NULL, this);
    EventNotifier::Get()->Disconnect(wxEVT_COLOUR_TAB, clColourEventHandler(Tweaks::OnColourTab), NULL, this);
    EventNotifier::Get()->Disconnect(
        wxEVT_WORKSPACE_LOADED, wxCommandEventHandler(Tweaks::OnWorkspaceLoaded), NULL, this);
    EventNotifier::Get()->Disconnect(
        wxEVT_WORKSPACE_CLOSED, wxCommandEventHandler(Tweaks::OnWorkspaceClosed), NULL, this);
    EventNotifier::Get()->Disconnect(
        wxEVT_WORKSPACE_VIEW_BUILD_STARTING, clCommandEventHandler(Tweaks::OnFileViewBuildTree), NULL, this);
    EventNotifier::Get()->Disconnect(
        wxEVT_WORKSPACE_VIEW_CUSTOMIZE_PROJECT, clColourEventHandler(Tweaks::OnCustomizeProject), NULL, this);
    EventNotifier::Get()->Disconnect(
        wxEVT_GET_TAB_BORDER_COLOUR, clColourEventHandler(Tweaks::OnTabBorderColour), NULL, this);
}

Tweaks::~Tweaks() {}

clToolBar* Tweaks::CreateToolBar(wxWindow* parent)
{
    // Create the toolbar to be used by the plugin
    clToolBar* tb(NULL);
    return tb;
}

void Tweaks::CreatePluginMenu(wxMenu* pluginsMenu)
{
    wxUnusedVar(pluginsMenu);
    wxMenu* menu = new wxMenu;
    menu->Append(ID_TWEAKS_SETTINGS, _("Settings..."));
    pluginsMenu->AppendSubMenu(menu, _("Tweaks Plugin"));
}

void Tweaks::HookPopupMenu(wxMenu* menu, MenuType type)
{
    wxUnusedVar(menu);
    wxUnusedVar(type);
}

void Tweaks::UnHookPopupMenu(wxMenu* menu, MenuType type)
{
    wxUnusedVar(menu);
    wxUnusedVar(type);
}

void Tweaks::OnSettings(wxCommandEvent& e)
{
    wxUnusedVar(e);
    TweaksSettingsDlg dlg(m_mgr->GetTheApp()->GetTopWindow());
    if(dlg.ShowModal() == wxID_OK) {
        dlg.GetSettings().Save();
    }

    m_settings.Load(); // Refresh our cached settings
    // Refresh the drawings
    m_mgr->GetTheApp()->GetTopWindow()->Refresh();
}

void Tweaks::OnColourTab(clColourEvent& e)
{
    TWEAKS_ENABLED_EVENT_HANDLER();

    IEditor* editor = FindEditorByPage(e.GetPage());
    if(!editor) {
        if(m_settings.GetGlobalFgColour().IsOk() && m_settings.GetGlobalBgColour().IsOk()) {
            // Non editor tab
            e.SetBgColour(m_settings.GetGlobalBgColour());
            e.SetFgColour(m_settings.GetGlobalFgColour());

        } else {
            e.Skip();
        }

    } else {

        const ProjectTweaks& tw = m_settings.GetProjectTweaks(editor->GetProjectName());
        if(tw.IsOk()) {
            e.SetBgColour(tw.GetTabBgColour());
            e.SetFgColour(tw.GetTabFgColour());

        } else if(m_settings.GetGlobalBgColour().IsOk() && m_settings.GetGlobalFgColour().IsOk()) {
            e.SetBgColour(m_settings.GetGlobalBgColour());
            e.SetFgColour(m_settings.GetGlobalFgColour());

        } else {
            e.Skip();
        }
    }
}

IEditor* Tweaks::FindEditorByPage(wxWindow* page)
{
    for(size_t i = 0; i < m_mgr->GetPageCount(); ++i) {
        if(m_mgr->GetPage(i) == page) {
            return dynamic_cast<IEditor*>(m_mgr->GetPage(i));
        }
    }
    return NULL;
}

void Tweaks::OnWorkspaceLoaded(wxCommandEvent& e)
{
    e.Skip();
    // Refresh the list with the current workspace setup
    m_settings.Load();
    m_mgr->GetWorkspacePaneNotebook()->Refresh();
    m_mgr->GetOutputPaneNotebook()->Refresh();
}

void Tweaks::OnWorkspaceClosed(wxCommandEvent& e)
{
    e.Skip();
    m_settings.Clear();
    m_project2Icon.clear();
}

void Tweaks::OnFileViewBuildTree(clCommandEvent& e)
{
    TWEAKS_ENABLED_EVENT_HANDLER();
    m_project2Icon.clear();
    if(m_settings.GetProjects().empty()) {
        e.Skip();
        return;
    }

    // See if we got a new image for a project
    wxImageList* images = new wxImageList(16, 16);
    wxImageList* old_images = m_mgr->GetTree(TreeFileView)->GetImageList();

    // Copy the old images to the new one
    for(int i = 0; i < old_images->GetImageCount(); ++i) {
        images->Add(old_images->GetIcon(i));
    }

    ProjectTweaks::Map_t::const_iterator iter = m_settings.GetProjects().begin();
    for(; iter != m_settings.GetProjects().end(); ++iter) {
        wxString bmpfile = iter->second.GetBitmapFilename();
        bmpfile.Trim().Trim(false);
        if(bmpfile.IsEmpty()) {
            continue;
        }
        wxBitmap bmp(bmpfile, wxBITMAP_TYPE_ANY);
        if(bmp.IsOk()) {
            wxIcon icn;
            icn.CopyFromBitmap(bmp);
            int index = images->Add(icn);
            m_project2Icon.insert(std::make_pair(iter->first, index));
        }
    }

    if(m_project2Icon.empty()) {
        e.Skip();
        wxDELETE(images);

    } else {
        // send back the new image list
        e.SetClientData(images);
    }
}

void Tweaks::OnCustomizeProject(clColourEvent& e)
{
    TWEAKS_ENABLED_EVENT_HANDLER();
    if(m_project2Icon.count(e.GetString())) {
        // We got a new icon for this project!
        e.SetInt(m_project2Icon.find(e.GetString())->second);

    } else {
        e.Skip();
    }
}

void Tweaks::OnTabBorderColour(clColourEvent& e)
{
#ifdef __WXGTK__
#if CL_USE_NATIVEBOOK
    // Not supported with native notebooks
    e.Skip();
    return;
#endif
#endif
    TWEAKS_ENABLED_EVENT_HANDLER();

    if(m_settings.GetGlobalBgColour().IsOk()) {
        // return the tab colour
        e.SetBorderColour(DrawingUtils::DarkColour(m_settings.GetGlobalBgColour(), 1.5));
    } else {
        e.Skip();
    }
}