Skip to content

Commit

Permalink
[cleanup] (#3529)
Browse files Browse the repository at this point in the history
- clang-format
- add missing `explicit`
- add missing `override` (and remove `virtual`)
- add extra `const`
- use `= default`
- use member initialization
- Replace `NULL` by `nullptr`
- use reference instead of pointer
- use enum class for `ZoomText::MarkerType`
- remove some unneeded `#include` and use some forward declarations
- add some sub-functions
- replace output parameter by return type
  • Loading branch information
Jarod42 authored Nov 16, 2024
1 parent 005407a commit a7dab79
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 144 deletions.
5 changes: 1 addition & 4 deletions ZoomNavigator/znSettingsDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

#include "cl_config.h"
#include "event_notifier.h"
#include "windowattrmanager.h"
#include "zn_config_item.h"

wxDEFINE_EVENT(wxEVT_ZN_SETTINGS_UPDATED, wxCommandEvent);
Expand All @@ -37,7 +36,7 @@ znSettingsDlg::znSettingsDlg(wxWindow* parent)
{
znConfigItem data;
clConfig conf("zoom-navigator.conf");
if(conf.ReadItem(&data)) {
if (conf.ReadItem(&data)) {
m_checkBoxEnableZN->SetValue(data.IsEnabled());
m_colourPickerHighlightColour->SetColour(wxColour(data.GetHighlightColour()));
m_checkBoxUseVScrollbar->SetValue(data.IsUseScrollbar());
Expand All @@ -48,8 +47,6 @@ znSettingsDlg::znSettingsDlg(wxWindow* parent)
CentreOnParent();
}

znSettingsDlg::~znSettingsDlg() {}

void znSettingsDlg::OnOK(wxCommandEvent& event)
{
znConfigItem data;
Expand Down
7 changes: 4 additions & 3 deletions ZoomNavigator/znSettingsDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ wxDECLARE_EVENT(wxEVT_ZN_SETTINGS_UPDATED, wxCommandEvent);
class znSettingsDlg : public znSettingsDlgBase
{
public:
znSettingsDlg(wxWindow* parent);
virtual ~znSettingsDlg();
explicit znSettingsDlg(wxWindow* parent);
~znSettingsDlg() override = default;

protected:
virtual void OnOK(wxCommandEvent& event);
void OnOK(wxCommandEvent& event) override;
};
#endif // ZNSETTINGSDLG_H
5 changes: 0 additions & 5 deletions ZoomNavigator/zn_config_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,9 @@
znConfigItem::znConfigItem()
: clConfigItem("ZoomNavigator")
, m_highlightColour("LIGHT GREY")
, m_enabled(false)
, m_zoomFactor(-10)
, m_useScrollbar(true)
{
}

znConfigItem::~znConfigItem() {}

void znConfigItem::FromJSON(const JSONItem& json)
{
m_highlightColour = json.namedObject("m_highlightColour").toString();
Expand Down
14 changes: 7 additions & 7 deletions ZoomNavigator/zn_config_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@
class znConfigItem : public clConfigItem
{
wxString m_highlightColour;
bool m_enabled;
int m_zoomFactor;
bool m_useScrollbar;
bool m_enabled = false;
int m_zoomFactor = -10;
bool m_useScrollbar = true;

public:
znConfigItem();
virtual ~znConfigItem();
znConfigItem();
~znConfigItem() override = default;

public:
virtual void FromJSON(const JSONItem& json);
virtual JSONItem ToJSON() const;
void FromJSON(const JSONItem& json) override;
JSONItem ToJSON() const override;

void SetEnabled(bool enabled) { this->m_enabled = enabled; }
void SetHighlightColour(const wxString& highlightColour) { this->m_highlightColour = highlightColour; }
Expand Down
82 changes: 30 additions & 52 deletions ZoomNavigator/zoomnavigator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,25 @@

#include "bookmark_manager.h"
#include "event_notifier.h"
#include "globals.h"
#include "zoomtext.h"
#include "znSettingsDlg.h"
#include "zn_config_item.h"

#include <wx/menu.h>
#include <wx/msgdlg.h>
#include <wx/stc/stc.h>
#include <wx/timer.h>
#include <wx/wupdlock.h>
#include <wx/xrc/xmlres.h>

#define CHECK_CONDITION(cond) \
if (!cond) \
return;

const wxString ZOOM_PANE_TITLE(_("Zoom"));
static const wxString ZOOM_PANE_TITLE(_("Zoom"));

// Define the plugin entry point
CL_PLUGIN_API IPlugin* CreatePlugin(IManager* manager)
{
return new ZoomNavigator(manager);
}
CL_PLUGIN_API IPlugin* CreatePlugin(IManager* manager) { return new ZoomNavigator(manager); }

CL_PLUGIN_API PluginInfo* GetPluginInfo()
{
Expand All @@ -72,46 +70,37 @@ namespace
{
/// @[param]in ctrl the control
/// @[param]in marker_mask marker type to check
/// @[param]out lines
size_t GetMarkers(wxStyledTextCtrl* ctrl, int marker_mask, std::vector<int>* lines)
std::vector<int> GetMarkers(wxStyledTextCtrl& ctrl, marker_mask_type marker_mask)
{
std::vector<int> lines;
int nFoundLine = 0;
while (true) {
nFoundLine = ctrl->MarkerNext(nFoundLine, marker_mask);
nFoundLine = ctrl.MarkerNext(nFoundLine, marker_mask);
if (nFoundLine == wxNOT_FOUND) {
break;
}
lines->push_back(nFoundLine);
lines.push_back(nFoundLine);
++nFoundLine;
}
return lines->size();
return lines;
}
} // namespace

ZoomNavigator::ZoomNavigator(IManager* manager)
: IPlugin(manager)
, mgr(manager)
, m_zoompane(NULL)
, m_topWindow(NULL)
, m_text(NULL)
, m_markerFirstLine(wxNOT_FOUND)
, m_markerLastLine(wxNOT_FOUND)
, m_enabled(false)
, m_lastLine(wxNOT_FOUND)
, m_startupCompleted(false)
, m_config(new clConfig("zoom-navigator.conf"))
{
m_config = new clConfig("zoom-navigator.conf");
m_longName = _("Zoom Navigator");
m_shortName = wxT("ZoomNavigator");
m_topWindow = m_mgr->GetTheApp();

m_topWindow->Connect(wxEVT_IDLE, wxIdleEventHandler(ZoomNavigator::OnIdle), NULL, this);
m_topWindow->Connect(wxEVT_IDLE, wxIdleEventHandler(ZoomNavigator::OnIdle), nullptr, this);
EventNotifier::Get()->Bind(wxEVT_INIT_DONE, &ZoomNavigator::OnInitDone, this);
EventNotifier::Get()->Bind(wxEVT_FILE_SAVED, &ZoomNavigator::OnFileSaved, this);
EventNotifier::Get()->Bind(wxEVT_ZN_SETTINGS_UPDATED, &ZoomNavigator::OnSettingsChanged, this);

m_topWindow->Connect(XRCID("zn_settings"), wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(ZoomNavigator::OnSettings), NULL, this);
wxCommandEventHandler(ZoomNavigator::OnSettings), nullptr, this);

m_timer = new wxTimer(this);
Bind(wxEVT_TIMER, &ZoomNavigator::OnTimer, this, m_timer->GetId());
Expand All @@ -128,9 +117,9 @@ void ZoomNavigator::UnPlug()
EventNotifier::Get()->Unbind(wxEVT_INIT_DONE, &ZoomNavigator::OnInitDone, this);
EventNotifier::Get()->Unbind(wxEVT_FILE_SAVED, &ZoomNavigator::OnFileSaved, this);

m_topWindow->Disconnect(wxEVT_IDLE, wxIdleEventHandler(ZoomNavigator::OnIdle), NULL, this);
m_topWindow->Disconnect(wxEVT_IDLE, wxIdleEventHandler(ZoomNavigator::OnIdle), nullptr, this);
m_topWindow->Disconnect(XRCID("zn_settings"), wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(ZoomNavigator::OnSettings), NULL, this);
wxCommandEventHandler(ZoomNavigator::OnSettings), nullptr, this);
// cancel the timer
Unbind(wxEVT_TIMER, &ZoomNavigator::OnTimer, this, m_timer->GetId());
m_timer->Stop();
Expand All @@ -148,17 +137,14 @@ void ZoomNavigator::CreateToolBar(clToolBarGeneric* toolbar) { wxUnusedVar(toolb
void ZoomNavigator::CreatePluginMenu(wxMenu* pluginsMenu)
{
wxMenu* menu = new wxMenu();
wxMenuItem* item(NULL);
item = new wxMenuItem(menu, XRCID("zn_settings"), _("Settings"), _("Settings"), wxITEM_NORMAL);
menu->Append(item);
menu->Append(new wxMenuItem(menu, XRCID("zn_settings"), _("Settings"), _("Settings"), wxITEM_NORMAL));
pluginsMenu->Append(wxID_ANY, _("Zoom Navigator"), menu);
}

void ZoomNavigator::OnShowHideClick(wxCommandEvent& e) {}

void ZoomNavigator::DoInitialize()
{

znConfigItem data;
if (m_config->ReadItem(&data)) {
m_enabled = data.IsEnabled();
Expand Down Expand Up @@ -196,27 +182,19 @@ void ZoomNavigator::DoUpdate()
CHECK_CONDITION(stc->IsShown());

// locate any error and warning markers in the main editor and duplicate then into the zoomed view
std::vector<int> error_lines;
std::vector<int> warning_lines;
m_text->DeleteAllMarkers();
if (GetMarkers(stc, mmt_error, &error_lines)) {
m_text->UpdateMarkers(error_lines, ZoomText::MARKER_ERROR);
}

if (GetMarkers(stc, mmt_warning, &warning_lines)) {
m_text->UpdateMarkers(warning_lines, ZoomText::MARKER_WARNING);
}
m_text->UpdateMarkers(GetMarkers(*stc, mmt_error), ZoomText::MarkerType::Error);
m_text->UpdateMarkers(GetMarkers(*stc, mmt_warning), ZoomText::MarkerType::Warning);

if (curEditor->GetFileName().GetFullPath() != m_curfile) {
SetEditorText(curEditor);
}

int first = stc->GetFirstVisibleLine();
int last = stc->LinesOnScreen() + first;
const int first = stc->GetFirstVisibleLine();
const int last = stc->LinesOnScreen() + first;

if (m_markerFirstLine != first || m_markerLastLine != last) {
PatchUpHighlights(first, last);
SetZoomTextScrollPosToMiddle(stc);
SetZoomTextScrollPosToMiddle(*stc);
}
}

Expand All @@ -230,14 +208,14 @@ void ZoomNavigator::SetEditorText(IEditor* editor)
}
}

void ZoomNavigator::SetZoomTextScrollPosToMiddle(wxStyledTextCtrl* stc)
void ZoomNavigator::SetZoomTextScrollPosToMiddle(wxStyledTextCtrl& stc)
{
// make the middle line of editor text centered in the zoomview
int first = stc->GetFirstVisibleLine() + (stc->LinesOnScreen() / 2);
int first = stc.GetFirstVisibleLine() + (stc.LinesOnScreen() / 2);

// we want to make 'first' centered
int numLinesOnScreen = m_text->LinesOnScreen();
int linesAboveIt = numLinesOnScreen / 2;
const int numLinesOnScreen = m_text->LinesOnScreen();
const int linesAboveIt = numLinesOnScreen / 2;

first = first - linesAboveIt;
if (first < 0)
Expand Down Expand Up @@ -266,18 +244,18 @@ void ZoomNavigator::OnPreviewClicked(wxMouseEvent& e)
CHECK_CONDITION(m_enabled);

// the first line is taken from the preview
int pos = m_text->PositionFromPoint(e.GetPosition());
const int pos = m_text->PositionFromPoint(e.GetPosition());
if (pos == wxSTC_INVALID_POSITION) {
return;
}
int first = m_text->LineFromPosition(pos);
int nLinesOnScreen = curEditor->GetCtrl()->LinesOnScreen();
const int nLinesOnScreen = curEditor->GetCtrl()->LinesOnScreen();
first -= (nLinesOnScreen / 2);
if (first < 0)
first = 0;

// however, the last line is set according to the actual editor
int last = nLinesOnScreen + first;
const int last = nLinesOnScreen + first;

PatchUpHighlights(first, last);
curEditor->GetCtrl()->SetFirstVisibleLine(first);
Expand All @@ -290,10 +268,10 @@ void ZoomNavigator::OnPreviewClicked(wxMouseEvent& e)

void ZoomNavigator::DoCleanup()
{
SetEditorText(NULL);
SetEditorText(nullptr);
m_markerFirstLine = wxNOT_FOUND;
m_markerLastLine = wxNOT_FOUND;
m_text->UpdateLexer(NULL);
m_text->UpdateLexer(nullptr);
}

void ZoomNavigator::OnSettings(wxCommandEvent& e)
Expand All @@ -312,7 +290,7 @@ void ZoomNavigator::OnSettingsChanged(wxCommandEvent& e)

if (!m_enabled) {
// Clear selection
m_text->UpdateText(NULL);
m_text->UpdateText(nullptr);

} else {
DoCleanup();
Expand Down
38 changes: 15 additions & 23 deletions ZoomNavigator/zoomnavigator.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,53 +35,45 @@

#include "cl_command_event.h"
#include "plugin.h"
#include "zoomtext.h"

#include <set>
#include <wx/timer.h>

extern const wxString ZOOM_PANE_TITLE;

class ZoomNavUpdateTimer;
class wxTimer;
class ZoomText;

class ZoomNavigator : public IPlugin
{
IManager* mgr;
wxPanel* m_zoompane;
wxEvtHandler* m_topWindow;
ZoomText* m_text;
wxPanel* m_zoompane = nullptr;
wxEvtHandler* m_topWindow = nullptr;
ZoomText* m_text = nullptr;
int m_markerFirstLine = wxNOT_FOUND;
int m_markerLastLine = wxNOT_FOUND;
bool m_enabled;
clConfig* m_config;
int m_lastLine;
bool m_startupCompleted;
bool m_enabled = false;
clConfig* m_config = nullptr;
bool m_startupCompleted = false;
wxString m_curfile;
wxTimer* m_timer = nullptr;

protected:
void DoInitialize();
void PatchUpHighlights(const int first, const int last);
void SetEditorText(IEditor* editor);
void SetZoomTextScrollPosToMiddle(wxStyledTextCtrl* stc);
void SetZoomTextScrollPosToMiddle(wxStyledTextCtrl& stc);
void DoUpdate();
void DoCleanup();
void OnTimer(wxTimerEvent& event);

public:
ZoomNavigator(IManager* manager);
~ZoomNavigator();
explicit ZoomNavigator(IManager* manager);
~ZoomNavigator() override;

//--------------------------------------------
// Abstract methods
//--------------------------------------------
virtual void CreateToolBar(clToolBarGeneric* toolbar);
virtual void CreatePluginMenu(wxMenu* pluginsMenu);
virtual void HookPopupMenu(wxMenu* menu, MenuType type);
virtual void UnPlug();
void CreateToolBar(clToolBarGeneric* toolbar) override;
void CreatePluginMenu(wxMenu* pluginsMenu) override;
void HookPopupMenu(wxMenu* menu, MenuType type) override;
void UnPlug() override;

void OnIdle(wxIdleEvent& e);

void OnShowHideClick(wxCommandEvent& e);
void OnPreviewClicked(wxMouseEvent& e);
void OnSettings(wxCommandEvent& e);
Expand Down
Loading

0 comments on commit a7dab79

Please sign in to comment.