ListWindow: convert list_window_callback_fn_t to an abstract class
authorMax Kellermann <max@musicpd.org>
Sat, 17 Mar 2018 09:30:06 +0000 (10:30 +0100)
committerMax Kellermann <max@musicpd.org>
Sat, 17 Mar 2018 09:30:06 +0000 (10:30 +0100)
20 files changed:
src/AlbumListPage.cxx
src/AlbumListPage.hxx
src/ArtistListPage.cxx
src/ArtistListPage.hxx
src/ListText.hxx [new file with mode: 0644]
src/ListWindow.cxx
src/ListWindow.hxx
src/TextListRenderer.cxx
src/TextListRenderer.hxx
src/TextPage.cxx
src/TextPage.hxx
src/screen_browser.cxx
src/screen_browser.hxx
src/screen_find.cxx
src/screen_find.hxx
src/screen_help.cxx
src/screen_keydef.cxx
src/screen_queue.cxx
src/screen_search.cxx
src/screen_song.cxx

index 4247b29..b8b5a35 100644 (file)
@@ -48,35 +48,23 @@ CompareUTF8(const std::string &a, const std::string &b)
        return n < 0;
 }
 
-/* list_window callback */
-static const char *
-album_lw_callback(unsigned idx, void *data)
-{
-       const auto &list = *(const std::vector<std::string> *)data;
-
-       assert(idx < list.size());
-
-       const char *str_utf8 = list[idx].c_str();
-
-       static char buf[BUFSIZE];
-       g_strlcpy(buf, Utf8ToLocale(str_utf8).c_str(), sizeof(buf));
-       return buf;
-}
-
-/* list_window callback */
-static const char *
-AlbumListCallback(unsigned idx, void *data)
+const char *
+AlbumListPage::GetListItemText(unsigned idx) const
 {
-       const auto &list = *(const std::vector<std::string> *)data;
-
        if (idx == 0)
                return "..";
-       else if (idx == list.size() + 1)
+       else if (idx == album_list.size() + 1)
                return _("All tracks");
 
        --idx;
 
-       return album_lw_callback(idx, data);
+       assert(idx < album_list.size());
+
+       const char *str_utf8 = album_list[idx].c_str();
+
+       static char buf[BUFSIZE];
+       g_strlcpy(buf, Utf8ToLocale(str_utf8).c_str(), sizeof(buf));
+       return buf;
 }
 
 static void
@@ -240,15 +228,12 @@ AlbumListPage::OnCommand(struct mpdclient &c, command_t cmd)
        case CMD_LIST_RFIND:
        case CMD_LIST_FIND_NEXT:
        case CMD_LIST_RFIND_NEXT:
-               screen_find(screen, &lw, cmd,
-                           AlbumListCallback, &album_list);
+               screen_find(screen, &lw, cmd, *this);
                SetDirty();
                return true;
 
        case CMD_LIST_JUMP:
-               screen_jump(screen, &lw,
-                           AlbumListCallback, &album_list,
-                           *this);
+               screen_jump(screen, &lw, *this, *this);
                SetDirty();
                return true;
 
index 8e2f7c1..cb2c4b7 100644 (file)
 
 #include "ListPage.hxx"
 #include "ListRenderer.hxx"
+#include "ListText.hxx"
 
 #include <vector>
 #include <string>
 
 class ScreenManager;
 
-class AlbumListPage final : public ListPage, ListRenderer {
+class AlbumListPage final : public ListPage, ListRenderer, ListText {
        ScreenManager &screen;
        std::vector<std::string> album_list;
        std::string artist;
@@ -71,6 +72,9 @@ public:
        /* virtual methods from class ListRenderer */
        void PaintListItem(WINDOW *w, unsigned i, unsigned y, unsigned width,
                           bool selected) const override;
+
+       /* virtual methods from class ListText */
+       const char *GetListItemText(unsigned i) const override;
 };
 
 #endif
index b34c80d..c675929 100644 (file)
@@ -47,15 +47,12 @@ CompareUTF8(const std::string &a, const std::string &b)
        return n < 0;
 }
 
-/* list_window callback */
-static const char *
-screen_artist_lw_callback(unsigned idx, void *data)
+const char *
+ArtistListPage::GetListItemText(unsigned idx) const
 {
-       const auto &list = *(const std::vector<std::string> *)data;
-
-       assert(idx < list.size());
+       assert(idx < artist_list.size());
 
-       const char *str_utf8 = list[idx].c_str();
+       const char *str_utf8 = artist_list[idx].c_str();
 
        static char buf[BUFSIZE];
        g_strlcpy(buf, Utf8ToLocale(str_utf8).c_str(), sizeof(buf));
@@ -191,15 +188,12 @@ ArtistListPage::OnCommand(struct mpdclient &c, command_t cmd)
        case CMD_LIST_RFIND:
        case CMD_LIST_FIND_NEXT:
        case CMD_LIST_RFIND_NEXT:
-               screen_find(screen, &lw, cmd,
-                           screen_artist_lw_callback, &artist_list);
+               screen_find(screen, &lw, cmd, *this);
                SetDirty();
                return true;
 
        case CMD_LIST_JUMP:
-               screen_jump(screen, &lw,
-                           screen_artist_lw_callback, &artist_list,
-                           *this);
+               screen_jump(screen, &lw, *this, *this);
                SetDirty();
                return true;
 
index 7bad8a0..5bff25c 100644 (file)
 
 #include "ListPage.hxx"
 #include "ListRenderer.hxx"
+#include "ListText.hxx"
 
 #include <vector>
 #include <string>
 
 class ScreenManager;
 
-class ArtistListPage final : public ListPage, ListRenderer {
+class ArtistListPage final : public ListPage, ListRenderer, ListText {
        ScreenManager &screen;
        std::vector<std::string> artist_list;
 
@@ -58,6 +59,9 @@ public:
        /* virtual methods from class ListRenderer */
        void PaintListItem(WINDOW *w, unsigned i, unsigned y, unsigned width,
                           bool selected) const override;
+
+       /* virtual methods from class ListText */
+       const char *GetListItemText(unsigned i) const override;
 };
 
 #endif
diff --git a/src/ListText.hxx b/src/ListText.hxx
new file mode 100644 (file)
index 0000000..2b45dc3
--- /dev/null
@@ -0,0 +1,28 @@
+/* ncmpc (Ncurses MPD Client)
+ * (c) 2004-2018 The Music Player Daemon Project
+ * Project homepage: http://musicpd.org
+ *
+ * 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.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef LIST_TEXT_HXX
+#define LIST_TEXT_HXX
+
+class ListText {
+public:
+       virtual const char *GetListItemText(unsigned i) const = 0;
+};
+
+#endif
index 8a82313..fef92da 100644 (file)
@@ -19,6 +19,7 @@
 
 #include "ListWindow.hxx"
 #include "ListRenderer.hxx"
+#include "ListText.hxx"
 #include "config.h"
 #include "options.hxx"
 #include "charset.hxx"
@@ -325,8 +326,7 @@ ListWindow::Paint(const ListRenderer &renderer) const
 }
 
 bool
-ListWindow::Find(list_window_callback_fn_t callback,
-                void *callback_data,
+ListWindow::Find(const ListText &text,
                 const char *str,
                 bool wrap,
                 bool bell_on_wrap)
@@ -337,7 +337,7 @@ ListWindow::Find(list_window_callback_fn_t callback,
 
        do {
                while (i < length) {
-                       const char *label = callback(i, callback_data);
+                       const char *label = text.GetListItemText(i);
                        assert(label != nullptr);
 
                        if (match_line(label, str)) {
@@ -362,8 +362,7 @@ ListWindow::Find(list_window_callback_fn_t callback,
 }
 
 bool
-ListWindow::ReverseFind(list_window_callback_fn_t callback,
-                       void *callback_data,
+ListWindow::ReverseFind(const ListText &text,
                        const char *str,
                        bool wrap,
                        bool bell_on_wrap)
@@ -377,7 +376,7 @@ ListWindow::ReverseFind(list_window_callback_fn_t callback,
 
        do {
                while (i >= 0) {
-                       const char *label = callback(i, callback_data);
+                       const char *label = text.GetListItemText(i);
                        assert(label != nullptr);
 
                        if (match_line(label, str)) {
@@ -401,14 +400,12 @@ ListWindow::ReverseFind(list_window_callback_fn_t callback,
 
 #ifdef NCMPC_MINI
 bool
-ListWindow::Jump(list_window_callback_fn_t callback,
-                void *callback_data,
-                const char *str)
+ListWindow::Jump(const ListText &text, const char *str)
 {
        assert(str != nullptr);
 
        for (unsigned i = 0; i < length; i++) {
-               const char *label = callback(i, callback_data);
+               const char *label = text.GetListItemText(i);
                assert(label != nullptr);
 
                if (g_ascii_strncasecmp(label, str, strlen(str)) == 0) {
@@ -420,9 +417,7 @@ ListWindow::Jump(list_window_callback_fn_t callback,
 }
 #else
 bool
-ListWindow::Jump(list_window_callback_fn_t callback,
-                void *callback_data,
-                const char *str)
+ListWindow::Jump(const ListText &text, const char *str)
 {
        assert(str != nullptr);
 
@@ -431,7 +426,7 @@ ListWindow::Jump(list_window_callback_fn_t callback,
                return false;
 
        for (unsigned i = 0; i < length; i++) {
-               const char *label = callback(i, callback_data);
+               const char *label = text.GetListItemText(i);
                assert(label != nullptr);
 
                if (match_regex(regex, label)) {
index 06e5ea1..3829cad 100644 (file)
 #include "ncmpc_curses.h"
 #include "Size.hxx"
 
+class ListText;
 class ListRenderer;
 
-typedef const char *
-(*list_window_callback_fn_t)(unsigned i, void *data);
-
 /**
  * The bounds of a range selection, see list_window_get_range().
  */
@@ -189,7 +187,7 @@ struct ListWindow {
        /**
         * Find a string in a list window.
         */
-       bool Find(list_window_callback_fn_t callback, void *callback_data,
+       bool Find(const ListText &text,
                  const char *str,
                  bool wrap,
                  bool bell_on_wrap);
@@ -197,8 +195,7 @@ struct ListWindow {
        /**
         * Find a string in a list window (reversed).
         */
-       bool ReverseFind(list_window_callback_fn_t callback,
-                        void *callback_data,
+       bool ReverseFind(const ListText &text,
                         const char *str,
                         bool wrap,
                         bool bell_on_wrap);
@@ -207,8 +204,7 @@ struct ListWindow {
         * Find a string in a list window which begins with the given
         * characters in *str.
         */
-       bool Jump(list_window_callback_fn_t callback, void *callback_data,
-                 const char *str);
+       bool Jump(const ListText &text, const char *str);
 
 private:
        gcc_pure
index 9ca1b02..1a19bab 100644 (file)
@@ -17,7 +17,9 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+#include "config.h"
 #include "TextListRenderer.hxx"
+#include "ListText.hxx"
 #include "paint.hxx"
 
 #include <assert.h>
@@ -34,7 +36,7 @@ void
 TextListRenderer::PaintListItem(WINDOW *w, unsigned i, unsigned,
                                unsigned width, bool selected) const
 {
-       const char *label = callback(i, callback_data);
+       const char *label = text.GetListItemText(i);
        assert(label != nullptr);
 
        list_window_paint_row(w, width, selected, label);
index 59b7520..42ee76e 100644 (file)
 #ifndef NCMPC_TEXT_LIST_RENDERER_HXX
 #define NCMPC_TEXT_LIST_RENDERER_HXX
 
-#include "ListWindow.hxx"
 #include "ListRenderer.hxx"
 
 class ScreenManager;
+class ListText;
 
 class TextListRenderer final : public ListRenderer {
-       list_window_callback_fn_t callback;
-       void *callback_data;
+       const ListText &text;
 
 public:
-       TextListRenderer(list_window_callback_fn_t _callback,
-                        void *_callback_data)
-               :callback(_callback), callback_data(_callback_data) {}
+       explicit TextListRenderer(const ListText &_text)
+               :text(_text) {}
 
        /* virtual methods from class ListRenderer */
        void PaintListItem(WINDOW *w, unsigned i, unsigned y, unsigned width,
index c9f0c67..0982534 100644 (file)
@@ -72,7 +72,7 @@ TextPage::Append(const char *str)
 }
 
 const char *
-TextPage::ListCallback(unsigned idx) const
+TextPage::GetListItemText(unsigned idx) const
 {
        assert(idx < lines.size());
 
@@ -85,7 +85,7 @@ TextPage::ListCallback(unsigned idx) const
 void
 TextPage::Paint() const
 {
-       lw.Paint(TextListRenderer(ListCallback, const_cast<TextPage *>(this)));
+       lw.Paint(TextListRenderer(*this));
 }
 
 bool
@@ -95,7 +95,7 @@ TextPage::OnCommand(struct mpdclient &c, command_t cmd)
                return true;
 
        lw.SetCursor(lw.start);
-       if (screen_find(screen, &lw, cmd, ListCallback, this)) {
+       if (screen_find(screen, &lw, cmd, *this)) {
                /* center the row */
                lw.Center(lw.selected);
                SetDirty();
index 879bcc9..e5d7403 100644 (file)
@@ -21,6 +21,7 @@
 #define TEXT_PAGE_HXX
 
 #include "ListPage.hxx"
+#include "ListText.hxx"
 
 #include <vector>
 #include <string>
@@ -28,7 +29,7 @@
 struct mpdclient;
 class ScreenManager;
 
-class TextPage : public ListPage {
+class TextPage : public ListPage, ListText {
 protected:
        ScreenManager &screen;
 
@@ -63,18 +64,14 @@ protected:
                wrefresh(lw.w);
        }
 
-private:
-       const char *ListCallback(unsigned idx) const;
-
-       static const char *ListCallback(unsigned idx, void *data) {
-               const auto &p = *(const TextPage *)data;
-               return p.ListCallback(idx);
-       }
-
 public:
        /* virtual methods from class Page */
        void Paint() const override;
        bool OnCommand(struct mpdclient &c, command_t cmd) override;
+
+private:
+       /* virtual methods from class ListText */
+       const char *GetListItemText(unsigned i) const override;
 };
 
 #endif
index 2920ad0..7abc6f3 100644 (file)
@@ -73,17 +73,15 @@ screen_browser_sync_highlights(FileList *fl, const MpdQueue *playlist)
 
 #endif
 
-/* list_window callback */
-static const char *
-browser_lw_callback(unsigned idx, void *data)
+const char *
+FileListPage::GetListItemText(unsigned idx) const
 {
-       const auto *fl = (const FileList *) data;
        static char buf[BUFSIZE];
 
-       assert(fl != nullptr);
-       assert(idx < fl->size());
+       assert(filelist != nullptr);
+       assert(idx < filelist->size());
 
-       const auto &entry = (*fl)[idx];
+       const auto &entry = (*filelist)[idx];
        const auto *entity = entry.entity;
 
        if( entity == nullptr )
@@ -381,13 +379,11 @@ FileListPage::OnCommand(struct mpdclient &c, command_t cmd)
        case CMD_LIST_RFIND:
        case CMD_LIST_FIND_NEXT:
        case CMD_LIST_RFIND_NEXT:
-               screen_find(screen, &lw, cmd, browser_lw_callback, filelist);
+               screen_find(screen, &lw, cmd, *this);
                SetDirty();
                return true;
        case CMD_LIST_JUMP:
-               screen_jump(screen, &lw,
-                           browser_lw_callback, filelist,
-                           *this);
+               screen_jump(screen, &lw, *this, *this);
                SetDirty();
                return true;
 
index 1502c22..ed4e1f1 100644 (file)
@@ -25,6 +25,7 @@
 #include "ncmpc_curses.h"
 #include "ListPage.hxx"
 #include "ListRenderer.hxx"
+#include "ListText.hxx"
 
 struct mpdclient;
 struct MpdQueue;
@@ -32,7 +33,7 @@ class ScreenManager;
 class FileList;
 struct FileListEntry;
 
-class FileListPage : public ListPage, ListRenderer {
+class FileListPage : public ListPage, ListRenderer, ListText {
 protected:
        ScreenManager &screen;
 
@@ -77,6 +78,9 @@ private:
                           unsigned y, unsigned width,
                           bool selected) const final;
 
+       /* virtual methods from class ListText */
+       const char *GetListItemText(unsigned i) const override;
+
 public:
        /* virtual methods from class Page */
        void Paint() const override;
index 7c092c0..4b9176b 100644 (file)
@@ -21,6 +21,7 @@
 #include "screen_utils.hxx"
 #include "screen_status.hxx"
 #include "screen.hxx"
+#include "ListWindow.hxx"
 #include "keyboard.hxx"
 #include "i18n.h"
 #include "options.hxx"
@@ -32,8 +33,7 @@
 /* query user for a string and find it in a list window */
 bool
 screen_find(ScreenManager &screen, ListWindow *lw, command_t findcmd,
-           list_window_callback_fn_t callback_fn,
-           void *callback_data)
+           const ListText &text)
 {
        bool found;
        const char *prompt = FIND_PROMPT;
@@ -64,11 +64,11 @@ screen_find(ScreenManager &screen, ListWindow *lw, command_t findcmd,
                        return true;
 
                found = reversed
-                       ? lw->ReverseFind(callback_fn, callback_data,
+                       ? lw->ReverseFind(text,
                                          screen.findbuf.c_str(),
                                          options.find_wrap,
                                          options.bell_on_wrap)
-                       : lw->Find(callback_fn, callback_data,
+                       : lw->Find(text,
                                   screen.findbuf.c_str(),
                                   options.find_wrap,
                                   options.bell_on_wrap);
@@ -88,7 +88,7 @@ screen_find(ScreenManager &screen, ListWindow *lw, command_t findcmd,
  * which begins with this string while the users types */
 void
 screen_jump(ScreenManager &screen, ListWindow *lw,
-           list_window_callback_fn_t callback_fn, void *callback_data,
+           const ListText &text,
            const ListRenderer &renderer)
 {
        constexpr size_t WRLN_MAX_LINE_SIZE = 1024;
@@ -122,7 +122,7 @@ screen_jump(ScreenManager &screen, ListWindow *lw,
                        if (iter < buffer + WRLN_MAX_LINE_SIZE - 3)
                                ++iter;
                }
-               lw->Jump(callback_fn, callback_data, search_str);
+               lw->Jump(text, search_str);
 
                /* repaint the list_window */
                lw->Paint(renderer);
index 6fe09d5..9658722 100644 (file)
 #define NCMPC_SCREEN_FIND_H
 
 #include "command.hxx"
-#include "ListWindow.hxx"
 
 class ScreenManager;
+class ListWindow;
 class ListRenderer;
+class ListText;
 
 /**
  * query user for a string and find it in a list window
@@ -38,14 +39,12 @@ class ListRenderer;
 bool
 screen_find(ScreenManager &screen, ListWindow *lw,
            command_t findcmd,
-           list_window_callback_fn_t callback_fn,
-           void *callback_data);
+           const ListText &text);
 
 /* query user for a string and jump to the entry
  * which begins with this string while the users types */
 void
 screen_jump(ScreenManager &screen, ListWindow *lw,
-           list_window_callback_fn_t callback_fn, void *callback_data,
-           const ListRenderer &renderer);
+           const ListText &text, const ListRenderer &renderer);
 
 #endif
index 3000b00..b61630a 100644 (file)
@@ -21,6 +21,7 @@
 #include "screen_interface.hxx"
 #include "ListPage.hxx"
 #include "ListRenderer.hxx"
+#include "ListText.hxx"
 #include "screen_find.hxx"
 #include "paint.hxx"
 #include "charset.hxx"
@@ -198,7 +199,7 @@ static const struct help_text_row help_text[] = {
 #endif
 };
 
-class HelpPage final : public ListPage, ListRenderer {
+class HelpPage final : public ListPage, ListRenderer, ListText {
        ScreenManager &screen;
 
 public:
@@ -214,6 +215,9 @@ public:
                           unsigned y, unsigned width,
                           bool selected) const override;
 
+       /* virtual methods from class ListText */
+       const char *GetListItemText(unsigned i) const override;
+
        /* virtual methods from class Page */
        void Paint() const override;
        bool OnCommand(struct mpdclient &c, command_t cmd) override;
@@ -223,8 +227,8 @@ public:
        }
 };
 
-static const char *
-list_callback(unsigned i, gcc_unused void *data)
+const char *
+HelpPage::GetListItemText(unsigned i) const
 {
        const struct help_text_row *row = &help_text[i];
 
@@ -290,7 +294,7 @@ HelpPage::OnCommand(struct mpdclient &c, command_t cmd)
                return true;
 
        lw.SetCursor(lw.start);
-       if (screen_find(screen, &lw, cmd, list_callback, nullptr)) {
+       if (screen_find(screen, &lw, cmd, *this)) {
                /* center the row */
                lw.Center(lw.selected);
                SetDirty();
index b1d1de7..c2e1b72 100644 (file)
@@ -20,6 +20,7 @@
 #include "screen_keydef.hxx"
 #include "screen_interface.hxx"
 #include "ListPage.hxx"
+#include "ListText.hxx"
 #include "TextListRenderer.hxx"
 #include "ProxyPage.hxx"
 #include "screen_status.hxx"
@@ -38,7 +39,7 @@
 #include <string.h>
 #include <glib.h>
 
-class CommandKeysPage final : public ListPage {
+class CommandKeysPage final : public ListPage, ListText {
        ScreenManager &screen;
 
        command_definition_t *cmds;
@@ -116,19 +117,16 @@ private:
         */
        void AddKey(int cmd_index);
 
-       const char *ListCallback(unsigned idx) const;
-
-       static const char *ListCallback(unsigned idx, void *data) {
-               const auto &p = *(const CommandKeysPage *)data;
-               return p.ListCallback(idx);
-       }
-
 public:
        /* virtual methods from class Page */
        void OnOpen(struct mpdclient &c) override;
        void Paint() const override;
        bool OnCommand(struct mpdclient &c, command_t cmd) override;
        const char *GetTitle(char *s, size_t size) const override;
+
+private:
+       /* virtual methods from class ListText */
+       const char *GetListItemText(unsigned i) const override;
 };
 
 /* TODO: rename to check_n_keys / subcmd_count_keys? */
@@ -222,7 +220,7 @@ CommandKeysPage::AddKey(int cmd_index)
 }
 
 const char *
-CommandKeysPage::ListCallback(unsigned idx) const
+CommandKeysPage::GetListItemText(unsigned idx) const
 {
        static char buf[256];
 
@@ -260,8 +258,7 @@ CommandKeysPage::GetTitle(char *str, size_t size) const
 void
 CommandKeysPage::Paint() const
 {
-       lw.Paint(TextListRenderer(ListCallback,
-                                 const_cast<CommandKeysPage *>(this)));
+       lw.Paint(TextListRenderer(*this));
 }
 
 bool
@@ -297,7 +294,7 @@ CommandKeysPage::OnCommand(struct mpdclient &c, command_t cmd)
        case CMD_LIST_RFIND:
        case CMD_LIST_FIND_NEXT:
        case CMD_LIST_RFIND_NEXT:
-               screen_find(screen, &lw, cmd, ListCallback, this);
+               screen_find(screen, &lw, cmd, *this);
                SetDirty();
                return true;
 
@@ -310,7 +307,7 @@ CommandKeysPage::OnCommand(struct mpdclient &c, command_t cmd)
        return false;
 }
 
-class CommandListPage final : public ListPage {
+class CommandListPage final : public ListPage, ListText {
        ScreenManager &screen;
 
        command_definition_t *cmds = nullptr;
@@ -369,20 +366,16 @@ public:
        void Apply();
        void Save();
 
-private:
-       const char *ListCallback(unsigned idx) const;
-
-       static const char *ListCallback(unsigned idx, void *data) {
-               const auto &p = *(const CommandListPage *)data;
-               return p.ListCallback(idx);
-       }
-
 public:
        /* virtual methods from class Page */
        void OnOpen(struct mpdclient &c) override;
        void Paint() const override;
        bool OnCommand(struct mpdclient &c, command_t cmd) override;
        const char *GetTitle(char *s, size_t size) const override;
+
+private:
+       /* virtual methods from class ListText */
+       const char *GetListItemText(unsigned i) const override;
 };
 
 bool
@@ -440,7 +433,7 @@ CommandListPage::Save()
 }
 
 const char *
-CommandListPage::ListCallback(unsigned idx) const
+CommandListPage::GetListItemText(unsigned idx) const
 {
        static char buf[256];
 
@@ -497,8 +490,7 @@ CommandListPage::GetTitle(char *, size_t) const
 void
 CommandListPage::Paint() const
 {
-       lw.Paint(TextListRenderer(ListCallback,
-                                 const_cast<CommandListPage *>(this)));
+       lw.Paint(TextListRenderer(*this));
 }
 
 bool
@@ -527,7 +519,7 @@ CommandListPage::OnCommand(struct mpdclient &c, command_t cmd)
        case CMD_LIST_RFIND:
        case CMD_LIST_FIND_NEXT:
        case CMD_LIST_RFIND_NEXT:
-               screen_find(screen, &lw, cmd, ListCallback, this);
+               screen_find(screen, &lw, cmd, *this);
                SetDirty();
                return true;
 
index 4f4f8cb..5953880 100644 (file)
@@ -21,6 +21,7 @@
 #include "screen_interface.hxx"
 #include "ListPage.hxx"
 #include "ListRenderer.hxx"
+#include "ListText.hxx"
 #include "screen_file.hxx"
 #include "screen_status.hxx"
 #include "screen_find.hxx"
@@ -56,7 +57,7 @@
 
 #define MAX_SONG_LENGTH 512
 
-class QueuePage final : public ListPage, ListRenderer {
+class QueuePage final : public ListPage, ListRenderer, ListText {
        ScreenManager &screen;
 
 #ifndef NCMPC_MINI
@@ -110,6 +111,9 @@ private:
                           unsigned y, unsigned width,
                           bool selected) const override;
 
+       /* virtual methods from class ListText */
+       const char *GetListItemText(unsigned i) const override;
+
 public:
        /* virtual methods from class Page */
        void OnOpen(struct mpdclient &c) override;
@@ -164,15 +168,14 @@ QueuePage::RestoreSelection()
        SaveSelection();
 }
 
-static const char *
-screen_queue_lw_callback(unsigned idx, void *data)
+const char *
+QueuePage::GetListItemText(unsigned idx) const
 {
-       auto &playlist = *(MpdQueue *)data;
        static char songname[MAX_SONG_LENGTH];
 
-       assert(idx < playlist.size());
+       assert(idx < playlist->size());
 
-       const auto &song = playlist[idx];
+       const auto &song = (*playlist)[idx];
        strfsong(songname, MAX_SONG_LENGTH, options.list_format, &song);
 
        return songname;
@@ -512,14 +515,12 @@ QueuePage::OnCommand(struct mpdclient &c, command_t cmd)
        case CMD_LIST_RFIND:
        case CMD_LIST_FIND_NEXT:
        case CMD_LIST_RFIND_NEXT:
-               screen_find(screen, &lw, cmd,
-                           screen_queue_lw_callback, &c.playlist);
+               screen_find(screen, &lw, cmd, *this);
                SaveSelection();
                SetDirty();
                return true;
        case CMD_LIST_JUMP:
-               screen_jump(screen, &lw, screen_queue_lw_callback, &c.playlist,
-                           *this);
+               screen_jump(screen, &lw, *this, *this);
                SaveSelection();
                SetDirty();
                return true;
index aeca9a8..505fcc5 100644 (file)
@@ -129,13 +129,15 @@ public:
 };
 
 /* search info */
-static const char *
-lw_search_help_callback(unsigned idx, gcc_unused void *data)
-{
-       assert(idx < G_N_ELEMENTS(help_text));
+class SearchHelpText final : public ListText {
+public:
+       /* virtual methods from class ListText */
+       const char *GetListItemText(unsigned idx) const override {
+               assert(idx < G_N_ELEMENTS(help_text));
 
-       return help_text[idx];
-}
+               return help_text[idx];
+       }
+};
 
 void
 SearchPage::Clear(bool clear_pattern)
@@ -398,7 +400,7 @@ SearchPage::Paint() const
        if (filelist) {
                FileListPage::Paint();
        } else {
-               lw.Paint(TextListRenderer(lw_search_help_callback, nullptr));
+               lw.Paint(TextListRenderer(SearchHelpText()));
        }
 }
 
index 9a58e8f..8e777e1 100644 (file)
@@ -20,6 +20,7 @@
 #include "screen_song.hxx"
 #include "screen_interface.hxx"
 #include "ListPage.hxx"
+#include "ListText.hxx"
 #include "TextListRenderer.hxx"
 #include "screen_file.hxx"
 #include "screen_lyrics.hxx"
@@ -98,7 +99,7 @@ static unsigned max_stats_label_width;
 
 static struct mpd_song *next_song;
 
-class SongPage final : public ListPage {
+class SongPage final : public ListPage, ListText {
        ScreenManager &screen;
 
        mpd_song *selected_song = nullptr;
@@ -142,6 +143,10 @@ public:
        void Update(struct mpdclient &c, unsigned events) override;
        bool OnCommand(struct mpdclient &c, command_t cmd) override;
        const char *GetTitle(char *s, size_t size) const override;
+
+private:
+       /* virtual methods from class ListText */
+       const char *GetListItemText(unsigned i) const override;
 };
 
 void
@@ -159,11 +164,9 @@ SongPage::Clear()
        }
 }
 
-static const char *
-screen_song_list_callback(unsigned idx, void *data)
+const char *
+SongPage::GetListItemText(unsigned idx) const
 {
-       const auto &lines = *(const std::vector<std::string> *)data;
-
        return lines[idx].c_str();
 }
 
@@ -197,8 +200,7 @@ SongPage::GetTitle(gcc_unused char *str, gcc_unused size_t size) const
 void
 SongPage::Paint() const
 {
-       lw.Paint(TextListRenderer(screen_song_list_callback,
-                                 const_cast<void *>((const void *)&lines)));
+       lw.Paint(TextListRenderer(*this));
 }
 
 void
@@ -529,7 +531,7 @@ SongPage::OnCommand(struct mpdclient &c, command_t cmd)
                break;
        }
 
-       if (screen_find(screen, &lw, cmd, screen_song_list_callback, &lines)) {
+       if (screen_find(screen, &lw, cmd, *this)) {
                /* center the row */
                lw.Center(lw.selected);
                SetDirty();