From cfbe97839a6365f27efd8e4eb90b5fa53394ddf4 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 24 Feb 2024 13:09:48 +0100 Subject: [PATCH] start extracting out an editor widget --- CMakeLists.txt | 1 + FXEditor.cpp | 22 ++++++++++++++++++++++ FXEditor.h | 20 ++++++++++++++++++++ commands.cpp | 35 ++++++++++++++++++++++++++++------- commands.h | 6 ++++-- 5 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 FXEditor.cpp create mode 100644 FXEditor.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 361834e..492689a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,6 +73,7 @@ add_executable (${PROJECT_NAME} ${PLATFORM} datablock.cpp datafile.cpp exportdlg.cpp + FXEditor.cpp FXFileDialogEx.cpp fxhelper.cpp FXMenuSeparatorEx.cpp diff --git a/FXEditor.cpp b/FXEditor.cpp new file mode 100644 index 0000000..1a31e03 --- /dev/null +++ b/FXEditor.cpp @@ -0,0 +1,22 @@ +#include "FXEditor.h" + +FXDEFMAP(FXEditor) MessageMap[]= +{ + FXMAPFUNC(SEL_KEYPRESS, 0, FXEditor::onKeyPress) +}; + +FXIMPLEMENT(FXEditor, FXText, MessageMap, ARRAYNUMBER(MessageMap)) + +FXEditor::FXEditor(FXComposite *p, FXObject * tgt, FXSelector sel, + FXuint opts, FXint x, FXint y, FXint w, FXint h) : + FXText(p, tgt, sel, opts, x, y, w, h) +{ +}; + +long FXEditor::onKeyPress(FXObject *sender, FXSelector sel, void *ptr) +{ + if (isEnabled()) { + FXEvent* event=(FXEvent*)ptr; + } + return FXText::onKeyPress(sender, sel, ptr); +} diff --git a/FXEditor.h b/FXEditor.h new file mode 100644 index 0000000..e219f71 --- /dev/null +++ b/FXEditor.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include + +class FXEditor: public FXText { + + FXDECLARE(FXEditor) + + public: + FXEditor(FXComposite *p, FXObject * tgt=NULL, FXSelector sel=0, + FXuint opts = 0, FXint x = 0, FXint y = 0, FXint w = 0, FXint h = 0); + virtual ~FXEditor() {} + + virtual long onKeyPress(FXObject *sender, FXSelector sel, void *ptr); + protected: + FXEditor() {}; + FXEditor(const FXEditor&) {}; +}; + diff --git a/commands.cpp b/commands.cpp index b5f435f..2ddd0f3 100644 --- a/commands.cpp +++ b/commands.cpp @@ -20,10 +20,10 @@ FXDEFMAP(FXCommands) MessageMap[]= FXMAPFUNC(SEL_UPDATE, FXCommands::ID_ROWCOL, FXCommands::updRowCol), }; -FXIMPLEMENT(FXCommands,FXText,MessageMap, ARRAYNUMBER(MessageMap)) +FXIMPLEMENT(FXCommands,FXEditor,MessageMap, ARRAYNUMBER(MessageMap)) FXCommands::FXCommands(FXComposite* p, FXObject* tgt,FXSelector sel, FXuint opts, FXint x,FXint y,FXint w,FXint h) - : FXText(p, tgt,sel, opts, x,y,w,h) + : FXEditor(p, tgt,sel, opts, x,y,w,h) { // initial visible rows & tab stop setVisibleRows(6); @@ -42,7 +42,7 @@ FXCommands::FXCommands(FXComposite* p, FXObject* tgt,FXSelector sel, FXuint opts textStyles[i].selectForeColor = getApp()->getSelforeColor(); textStyles[i].selectBackColor = getApp()->getSelbackColor(); textStyles[i].hiliteForeColor = getApp()->getHiliteColor(); - textStyles[i].hiliteBackColor = FXRGB(255, 128, 128); // from FXText.cpp + textStyles[i].hiliteBackColor = FXRGB(255, 128, 128); // from FXEditor.cpp textStyles[i].activeBackColor = getApp()->getBackColor(); textStyles[i].style = 0; // no underline, italic, bold } @@ -79,7 +79,7 @@ FXCommands::FXCommands(FXComposite* p, FXObject* tgt,FXSelector sel, FXuint opts void FXCommands::create() { - FXText::create(); + FXEditor::create(); disable(); setBackColor(getApp()->getBaseColor()); } @@ -234,7 +234,28 @@ long FXCommands::onKeyPress(FXObject* sender,FXSelector sel,void* ptr) { FXEvent* event=(FXEvent*)ptr; if (!isEnabled()) - return FXText::onKeyPress(sender, sel, ptr); + return FXEditor::onKeyPress(sender, sel, ptr); + else if (event->code == KEY_Return || event->code == KEY_KP_Enter) + { + // Keeps the indentation level, when you go to the next line + int curs = getCursorPos(); + int begin = lineStart(curs); + + FXString line; + extractText(line, begin, curs-begin); + + int pos = line.find_first_not_of(" \t"); + if (pos != -1) + line.erase(pos, line.length() - pos); + + if (!line.empty()) + { + long res = FXEditor::onKeyPress(sender, sel, ptr); + if (res) + insertText(lineStart(getCursorPos()), line); + return res; + } + } else if (event->code == KEY_Tab || event->code == KEY_KP_Tab) { int curs = getCursorPos(); @@ -255,7 +276,7 @@ long FXCommands::onKeyPress(FXObject* sender,FXSelector sel,void* ptr) return 1; } } - return FXText::onKeyPress(sender, sel, ptr); + return FXEditor::onKeyPress(sender, sel, ptr); } long FXCommands::onUpdate(FXObject* sender,FXSelector sel,void* ptr) @@ -266,7 +287,7 @@ long FXCommands::onUpdate(FXObject* sender,FXSelector sel,void* ptr) saveCommands(); } mapShowRoute(); - return FXText::onUpdate(sender, sel, ptr); + return FXEditor::onUpdate(sender, sel, ptr); } long FXCommands::updConfirmed(FXObject* sender, FXSelector, void*) { diff --git a/commands.h b/commands.h index 6626c3d..6d2b3fb 100644 --- a/commands.h +++ b/commands.h @@ -6,9 +6,11 @@ #include +#include "FXEditor.h" + #include -class FXCommands : public FXText +class FXCommands : public FXEditor { FXDECLARE(FXCommands) @@ -42,7 +44,7 @@ class FXCommands : public FXText public: enum { - ID_ROWCOL = FXText::ID_LAST, + ID_ROWCOL = FXEditor::ID_LAST, ID_UNIT_CONFIRM, ID_UNIT_ADD, ID_LAST