aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2014-11-16 23:46:08 +0100
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2014-11-17 03:56:42 +0100
commit2542eb02648294256a01ae4ecb6ac81bc8ab5094 (patch)
tree81166dbe78031f7764ca2d6d4925d96283ac29c5
parente762a43754ba375789fa749ca2e00a3548500d6a (diff)
downloadsciteco-2542eb02648294256a01ae4ecb6ac81bc8ab5094.tar.gz
Make sure QRegister::view is properly initialized and cleaned up
* it must be initialized after the UI (Interface::main), so I added a View::initialize() function * the old initialize() method was renamed to setup() * use a global instance of QRegister::view so it is guaranteed to be destroyed only after any QRegisters that could still need it * Document API adapted to work with ViewCurrent references
-rw-r--r--src/document.cpp34
-rw-r--r--src/document.h16
-rw-r--r--src/interface-gtk.cpp15
-rw-r--r--src/interface-gtk.h17
-rw-r--r--src/interface-ncurses.cpp11
-rw-r--r--src/interface-ncurses.h14
-rw-r--r--src/interface.cpp2
-rw-r--r--src/interface.h12
-rw-r--r--src/main.cpp6
-rw-r--r--src/qregisters.cpp43
-rw-r--r--src/qregisters.h6
-rw-r--r--src/ring.h2
12 files changed, 99 insertions, 79 deletions
diff --git a/src/document.cpp b/src/document.cpp
index 8ed5007..015c3d6 100644
--- a/src/document.cpp
+++ b/src/document.cpp
@@ -31,42 +31,42 @@
namespace SciTECO {
void
-Document::edit(ViewCurrent *view)
+Document::edit(ViewCurrent &view)
{
maybe_create_document();
- view->ssm(SCI_SETDOCPOINTER, 0, (sptr_t)doc);
- view->ssm(SCI_SETFIRSTVISIBLELINE, first_line);
- view->ssm(SCI_SETXOFFSET, xoffset);
- view->ssm(SCI_SETSEL, anchor, (sptr_t)dot);
+ view.ssm(SCI_SETDOCPOINTER, 0, (sptr_t)doc);
+ view.ssm(SCI_SETFIRSTVISIBLELINE, first_line);
+ view.ssm(SCI_SETXOFFSET, xoffset);
+ view.ssm(SCI_SETSEL, anchor, (sptr_t)dot);
/*
* Default TECO-style character representations.
* They are reset on EVERY SETDOCPOINTER call by Scintilla.
*/
- view->set_representations();
+ view.set_representations();
}
void
-Document::undo_edit(ViewCurrent *view)
+Document::undo_edit(ViewCurrent &view)
{
maybe_create_document();
- view->undo_set_representations();
+ view.undo_set_representations();
- view->undo_ssm(SCI_SETSEL, anchor, (sptr_t)dot);
- view->undo_ssm(SCI_SETXOFFSET, xoffset);
- view->undo_ssm(SCI_SETFIRSTVISIBLELINE, first_line);
- view->undo_ssm(SCI_SETDOCPOINTER, 0, (sptr_t)doc);
+ view.undo_ssm(SCI_SETSEL, anchor, (sptr_t)dot);
+ view.undo_ssm(SCI_SETXOFFSET, xoffset);
+ view.undo_ssm(SCI_SETFIRSTVISIBLELINE, first_line);
+ view.undo_ssm(SCI_SETDOCPOINTER, 0, (sptr_t)doc);
}
void
-Document::update(ViewCurrent *view)
+Document::update(ViewCurrent &view)
{
- anchor = view->ssm(SCI_GETANCHOR);
- dot = view->ssm(SCI_GETCURRENTPOS);
- first_line = view->ssm(SCI_GETFIRSTVISIBLELINE);
- xoffset = view->ssm(SCI_GETXOFFSET);
+ anchor = view.ssm(SCI_GETANCHOR);
+ dot = view.ssm(SCI_GETCURRENTPOS);
+ first_line = view.ssm(SCI_GETFIRSTVISIBLELINE);
+ xoffset = view.ssm(SCI_GETXOFFSET);
}
/*
diff --git a/src/document.h b/src/document.h
index 763db7a..686d2f4 100644
--- a/src/document.h
+++ b/src/document.h
@@ -67,10 +67,10 @@ public:
return doc != NULL;
}
- void edit(ViewCurrent *view);
- void undo_edit(ViewCurrent *view);
+ void edit(ViewCurrent &view);
+ void undo_edit(ViewCurrent &view);
- void update(ViewCurrent *view);
+ void update(ViewCurrent &view);
inline void
update(const Document &from)
{
@@ -108,8 +108,8 @@ protected:
release_document(void)
{
if (is_initialized()) {
- ViewCurrent *view = get_create_document_view();
- view->ssm(SCI_RELEASEDOCUMENT, 0, (sptr_t)doc);
+ ViewCurrent &view = get_create_document_view();
+ view.ssm(SCI_RELEASEDOCUMENT, 0, (sptr_t)doc);
doc = NULL;
}
}
@@ -123,14 +123,14 @@ private:
* per document, it must instead be returned by
* this method.
*/
- virtual ViewCurrent *get_create_document_view(void) = 0;
+ virtual ViewCurrent &get_create_document_view(void) = 0;
inline void
maybe_create_document(void)
{
if (!is_initialized()) {
- ViewCurrent *view = get_create_document_view();
- doc = (SciDoc)view->ssm(SCI_CREATEDOCUMENT);
+ ViewCurrent &view = get_create_document_view();
+ doc = (SciDoc)view.ssm(SCI_CREATEDOCUMENT);
}
}
};
diff --git a/src/interface-gtk.cpp b/src/interface-gtk.cpp
index ed5a8c9..3421d6f 100644
--- a/src/interface-gtk.cpp
+++ b/src/interface-gtk.cpp
@@ -53,7 +53,8 @@ static gboolean exit_app(GtkWidget *w, GdkEventAny *e, gpointer p);
#define UNNAMED_FILE "(Unnamed)"
-ViewGtk::ViewGtk()
+void
+ViewGtk::initialize_impl(void)
{
sci = SCINTILLA(scintilla_new());
/*
@@ -70,17 +71,7 @@ ViewGtk::ViewGtk()
g_signal_connect(G_OBJECT(sci), SCINTILLA_NOTIFY,
G_CALLBACK(scintilla_notify), NULL);
- initialize();
-}
-
-ViewGtk::~ViewGtk()
-{
- /*
- * This does NOT destroy the Scintilla object
- * and GTK widget, if it is the current view
- * (and therefore added to the vbox).
- */
- g_object_unref(G_OBJECT(sci));
+ setup();
}
void
diff --git a/src/interface-gtk.h b/src/interface-gtk.h
index 5a083ab..6c8e055 100644
--- a/src/interface-gtk.h
+++ b/src/interface-gtk.h
@@ -34,8 +34,21 @@ typedef class ViewGtk : public View<ViewGtk> {
ScintillaObject *sci;
public:
- ViewGtk();
- ~ViewGtk();
+ ViewGtk() : sci(NULL) {}
+
+ /* implementation of View::initialize() */
+ void initialize_impl(void);
+
+ inline ~ViewGtk()
+ {
+ /*
+ * This does NOT destroy the Scintilla object
+ * and GTK widget, if it is the current view
+ * (and therefore added to the vbox).
+ */
+ if (sci)
+ g_object_unref(G_OBJECT(sci));
+ }
inline GtkWidget *
get_widget(void)
diff --git a/src/interface-ncurses.cpp b/src/interface-ncurses.cpp
index 0be2145..32c310c 100644
--- a/src/interface-ncurses.cpp
+++ b/src/interface-ncurses.cpp
@@ -56,7 +56,8 @@ static void scintilla_notify(Scintilla *sci, int idFrom,
#define SCI_COLOR_ATTR(f, b) \
((chtype)COLOR_PAIR(SCI_COLOR_PAIR(f, b)))
-ViewNCurses::ViewNCurses()
+void
+ViewNCurses::initialize_impl(void)
{
WINDOW *window;
@@ -75,13 +76,7 @@ ViewNCurses::ViewNCurses()
/* Set up window position: never changes */
mvwin(window, 1, 0);
- initialize();
-}
-
-ViewNCurses::~ViewNCurses()
-{
- delwin(get_window());
- scintilla_delete(sci);
+ setup();
}
void
diff --git a/src/interface-ncurses.h b/src/interface-ncurses.h
index d245e86..53b6b3e 100644
--- a/src/interface-ncurses.h
+++ b/src/interface-ncurses.h
@@ -35,8 +35,18 @@ typedef class ViewNCurses : public View<ViewNCurses> {
Scintilla *sci;
public:
- ViewNCurses();
- ~ViewNCurses();
+ ViewNCurses() : sci(NULL) {}
+
+ /* implementation of View::initialize() */
+ void initialize_impl(void);
+
+ inline ~ViewNCurses()
+ {
+ if (sci) {
+ delwin(get_window());
+ scintilla_delete(sci);
+ }
+ }
inline void
refresh(void)
diff --git a/src/interface.cpp b/src/interface.cpp
index fcb1738..a726e01 100644
--- a/src/interface.cpp
+++ b/src/interface.cpp
@@ -53,7 +53,7 @@ View<ViewImpl>::set_representations(void)
template <class ViewImpl>
void
-View<ViewImpl>::initialize(void)
+View<ViewImpl>::setup(void)
{
ssm(SCI_SETFOCUS, TRUE);
diff --git a/src/interface.h b/src/interface.h
index 01684fd..8fa0112 100644
--- a/src/interface.h
+++ b/src/interface.h
@@ -100,6 +100,16 @@ class View {
};
public:
+ /*
+ * called after Interface initialization.
+ * Should call setup()
+ */
+ inline void
+ initialize(void)
+ {
+ impl().initialize_impl();
+ }
+
inline sptr_t
ssm(unsigned int iMessage,
uptr_t wParam = 0, sptr_t lParam = 0)
@@ -122,7 +132,7 @@ public:
}
protected:
- void initialize(void);
+ void setup(void);
};
/**
diff --git a/src/main.cpp b/src/main.cpp
index a713964..5697063 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -53,6 +53,7 @@ namespace SciTECO {
* GCC init_priority() attribute
*/
InterfaceCurrent interface;
+ViewCurrent QRegisters::view;
/*
* Scintilla will be initialized after these
@@ -288,10 +289,9 @@ main(int argc, char **argv)
/*
* QRegister view must be initialized only now
- * (after Curses initialization)
+ * (e.g. after Curses/GTK initialization).
*/
- QRegisters::view = new ViewCurrent();
- // FIXME: view should be deallocated */
+ QRegisters::view.initialize();
/* search string and status register */
QRegisters::globals.insert("_");
diff --git a/src/qregisters.cpp b/src/qregisters.cpp
index 002d4b7..98edb0b 100644
--- a/src/qregisters.cpp
+++ b/src/qregisters.cpp
@@ -59,7 +59,6 @@ namespace States {
namespace QRegisters {
QRegisterTable *locals = NULL;
QRegister *current = NULL;
- ViewCurrent *view = NULL;
static QRegisterStack stack;
}
@@ -75,10 +74,10 @@ QRegisterData::set_string(const gchar *str)
string.reset();
string.edit(QRegisters::view);
- QRegisters::view->ssm(SCI_BEGINUNDOACTION);
- QRegisters::view->ssm(SCI_SETTEXT, 0,
- (sptr_t)(str ? : ""));
- QRegisters::view->ssm(SCI_ENDUNDOACTION);
+ QRegisters::view.ssm(SCI_BEGINUNDOACTION);
+ QRegisters::view.ssm(SCI_SETTEXT, 0,
+ (sptr_t)(str ? : ""));
+ QRegisters::view.ssm(SCI_ENDUNDOACTION);
if (QRegisters::current)
QRegisters::current->string.edit(QRegisters::view);
@@ -100,7 +99,7 @@ QRegisterData::undo_set_string(void)
QRegisters::current->string.undo_edit(QRegisters::view);
string.undo_reset();
- QRegisters::view->undo_ssm(SCI_UNDO);
+ QRegisters::view.undo_ssm(SCI_UNDO);
string.undo_edit(QRegisters::view);
}
@@ -122,10 +121,10 @@ QRegisterData::append_string(const gchar *str)
string.edit(QRegisters::view);
- QRegisters::view->ssm(SCI_BEGINUNDOACTION);
- QRegisters::view->ssm(SCI_APPENDTEXT,
+ QRegisters::view.ssm(SCI_BEGINUNDOACTION);
+ QRegisters::view.ssm(SCI_APPENDTEXT,
strlen(str), (sptr_t)str);
- QRegisters::view->ssm(SCI_ENDUNDOACTION);
+ QRegisters::view.ssm(SCI_ENDUNDOACTION);
if (QRegisters::current)
QRegisters::current->string.edit(QRegisters::view);
@@ -145,9 +144,9 @@ QRegisterData::get_string(void)
string.edit(QRegisters::view);
- size = QRegisters::view->ssm(SCI_GETLENGTH) + 1;
+ size = QRegisters::view.ssm(SCI_GETLENGTH) + 1;
str = (gchar *)g_malloc(size);
- QRegisters::view->ssm(SCI_GETTEXT, size, (sptr_t)str);
+ QRegisters::view.ssm(SCI_GETTEXT, size, (sptr_t)str);
if (QRegisters::current)
QRegisters::current->string.edit(QRegisters::view);
@@ -162,7 +161,7 @@ QRegister::edit(void)
QRegisters::current->string.update(QRegisters::view);
string.edit(QRegisters::view);
- interface.show_view(QRegisters::view);
+ interface.show_view(&QRegisters::view);
interface.info_update(this);
}
@@ -175,7 +174,7 @@ QRegister::undo_edit(void)
interface.undo_info_update(this);
string.update(QRegisters::view);
string.undo_edit(QRegisters::view);
- interface.undo_show_view(QRegisters::view);
+ interface.undo_show_view(&QRegisters::view);
}
void
@@ -213,10 +212,10 @@ QRegister::load(const gchar *filename)
string.edit(QRegisters::view);
string.reset();
- QRegisters::view->ssm(SCI_BEGINUNDOACTION);
- QRegisters::view->ssm(SCI_CLEARALL);
- QRegisters::view->ssm(SCI_APPENDTEXT, size, (sptr_t)contents);
- QRegisters::view->ssm(SCI_ENDUNDOACTION);
+ QRegisters::view.ssm(SCI_BEGINUNDOACTION);
+ QRegisters::view.ssm(SCI_CLEARALL);
+ QRegisters::view.ssm(SCI_APPENDTEXT, size, (sptr_t)contents);
+ QRegisters::view.ssm(SCI_ENDUNDOACTION);
g_free(contents);
@@ -251,12 +250,12 @@ QRegisterBufferInfo::edit(void)
{
QRegister::edit();
- QRegisters::view->ssm(SCI_BEGINUNDOACTION);
- QRegisters::view->ssm(SCI_SETTEXT, 0,
- (sptr_t)(ring.current ? ring.current->filename : ""));
- QRegisters::view->ssm(SCI_ENDUNDOACTION);
+ QRegisters::view.ssm(SCI_BEGINUNDOACTION);
+ QRegisters::view.ssm(SCI_SETTEXT, 0,
+ (sptr_t)(ring.current ? ring.current->filename : ""));
+ QRegisters::view.ssm(SCI_ENDUNDOACTION);
- QRegisters::view->undo_ssm(SCI_UNDO);
+ QRegisters::view.undo_ssm(SCI_UNDO);
}
QRegisterTable::QRegisterTable(bool _undo) : RBTree(), must_undo(_undo)
diff --git a/src/qregisters.h b/src/qregisters.h
index 3d89c4a..efd802d 100644
--- a/src/qregisters.h
+++ b/src/qregisters.h
@@ -35,8 +35,8 @@
namespace SciTECO {
namespace QRegisters {
- /* constructed after Interface.main() in main() */
- extern ViewCurrent *view;
+ /* initialized after Interface.main() in main() */
+ extern ViewCurrent view;
}
/*
@@ -55,7 +55,7 @@ protected:
}
private:
- ViewCurrent *
+ ViewCurrent &
get_create_document_view(void)
{
return QRegisters::view;
diff --git a/src/ring.h b/src/ring.h
index 35424dc..3b60833 100644
--- a/src/ring.h
+++ b/src/ring.h
@@ -76,6 +76,8 @@ public:
Buffer() : ViewCurrent(),
filename(NULL), savepoint_id(0), dirty(false)
{
+ initialize();
+ /* only have to do this once: */
set_representations();
}