aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/interface-curses/curses-utils.cpp
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2016-01-31 06:58:18 +0100
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2016-01-31 06:58:18 +0100
commitd7e4aa16ecb0595ff0248e17fcdcfc09c7616ca0 (patch)
tree6903f426e8ee97d79c30626050a67d3686ce97cb /src/interface-curses/curses-utils.cpp
parent406cf5a58b5873f89df710d56883039ff496580e (diff)
downloadsciteco-d7e4aa16ecb0595ff0248e17fcdcfc09c7616ca0.tar.gz
CursesInfoPopup: separated the Curses popup widget from the rest of the UI code
* this has been prepared a long time ago * the popup widget does not in any way depend on the InterfaceCurses class and could be used elsewhere. * common and generic Curses drawing functions required by both the Curses UI and the CursesInfoPopup widget have been factored out into curses-utils.cpp (namespace Curses) * this improved the UI-logic separation and helped in making interface-curses.cpp smaller
Diffstat (limited to 'src/interface-curses/curses-utils.cpp')
-rw-r--r--src/interface-curses/curses-utils.cpp152
1 files changed, 152 insertions, 0 deletions
diff --git a/src/interface-curses/curses-utils.cpp b/src/interface-curses/curses-utils.cpp
new file mode 100644
index 0000000..add9a69
--- /dev/null
+++ b/src/interface-curses/curses-utils.cpp
@@ -0,0 +1,152 @@
+/*
+ * Copyright (C) 2012-2016 Robin Haberkorn
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <string.h>
+
+#include <glib.h>
+
+#include <curses.h>
+
+#include "sciteco.h"
+#include "string-utils.h"
+#include "curses-utils.h"
+
+namespace SciTECO {
+
+gsize
+Curses::format_str(WINDOW *win, const gchar *str,
+ gssize len, gint max_width)
+{
+ int old_x, old_y;
+ gint chars_added = 0;
+
+ getyx(win, old_y, old_x);
+
+ if (len < 0)
+ len = strlen(str);
+ if (max_width < 0)
+ max_width = getmaxx(win) - old_x;
+
+ while (len > 0) {
+ /*
+ * NOTE: This mapping is similar to
+ * View::set_representations()
+ */
+ switch (*str) {
+ case CTL_KEY_ESC:
+ chars_added++;
+ if (chars_added > max_width)
+ goto truncate;
+ waddch(win, '$' | A_REVERSE);
+ break;
+ case '\r':
+ chars_added += 2;
+ if (chars_added > max_width)
+ goto truncate;
+ waddch(win, 'C' | A_REVERSE);
+ waddch(win, 'R' | A_REVERSE);
+ break;
+ case '\n':
+ chars_added += 2;
+ if (chars_added > max_width)
+ goto truncate;
+ waddch(win, 'L' | A_REVERSE);
+ waddch(win, 'F' | A_REVERSE);
+ break;
+ case '\t':
+ chars_added += 3;
+ if (chars_added > max_width)
+ goto truncate;
+ waddch(win, 'T' | A_REVERSE);
+ waddch(win, 'A' | A_REVERSE);
+ waddch(win, 'B' | A_REVERSE);
+ break;
+ default:
+ if (IS_CTL(*str)) {
+ chars_added += 2;
+ if (chars_added > max_width)
+ goto truncate;
+ waddch(win, '^' | A_REVERSE);
+ waddch(win, CTL_ECHO(*str) | A_REVERSE);
+ } else {
+ chars_added++;
+ if (chars_added > max_width)
+ goto truncate;
+ waddch(win, *str);
+ }
+ }
+
+ str++;
+ len--;
+ }
+
+ return getcurx(win) - old_x;
+
+truncate:
+ if (max_width >= 3) {
+ /*
+ * Truncate string
+ */
+ wattron(win, A_UNDERLINE | A_BOLD);
+ mvwaddstr(win, old_y, old_x + max_width - 3, "...");
+ wattroff(win, A_UNDERLINE | A_BOLD);
+ }
+
+ return getcurx(win) - old_x;
+}
+
+gsize
+Curses::format_filename(WINDOW *win, const gchar *filename,
+ gint max_width)
+{
+ int old_x = getcurx(win);
+
+ gchar *filename_canon = String::canonicalize_ctl(filename);
+ size_t filename_len = strlen(filename_canon);
+
+ if (max_width < 0)
+ max_width = getmaxx(win) - old_x;
+
+ if (filename_len <= (size_t)max_width) {
+ waddstr(win, filename_canon);
+ } else {
+ const gchar *keep_post = filename_canon + filename_len -
+ max_width + 3;
+
+#ifdef G_OS_WIN32
+ const gchar *keep_pre = g_path_skip_root(filename_canon);
+ if (keep_pre) {
+ waddnstr(win, filename_canon,
+ keep_pre - filename_canon);
+ keep_post += keep_pre - filename_canon;
+ }
+#endif
+ wattron(win, A_UNDERLINE | A_BOLD);
+ waddstr(win, "...");
+ wattroff(win, A_UNDERLINE | A_BOLD);
+ waddstr(win, keep_post);
+ }
+
+ g_free(filename_canon);
+ return getcurx(win) - old_x;
+}
+
+} /* namespace SciTECO */