Use pkg-config to find ncursesw
[centerim5.git] / src / Footer.cpp
blobd16954e163cf3b664669f9ff029452d98ae76bd6
1 // Copyright (C) 2011-2015 Petr Pavlu <setup@dagobah.cz>
2 //
3 // This file is part of CenterIM.
4 //
5 // CenterIM is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // CenterIM is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with CenterIM. If not, see <http://www.gnu.org/licenses/>.
18 #include "Footer.h"
20 #include <cppconsui/KeyConfig.h>
21 #include <cstring>
22 #include <string>
24 Footer *Footer::my_instance_ = nullptr;
26 Footer *Footer::instance()
28 return my_instance_;
31 void Footer::onScreenResized()
33 moveResizeRect(CENTERIM->getScreenArea(CenterIM::FOOTER_AREA));
36 void Footer::setText(const char *fmt, ...)
38 values_.clear();
40 if (fmt == nullptr) {
41 updateText();
42 return;
45 values_.push_back(std::string(fmt));
47 va_list args;
48 va_start(args, fmt);
49 while (*fmt != '\0') {
50 if (*fmt == '%') {
51 if (*(fmt + 1) == '%')
52 ++fmt;
53 else if (*(fmt + 1) == 's') {
54 const char *v = va_arg(args, const char *);
55 values_.push_back(std::string(v));
56 ++fmt;
59 ++fmt;
61 va_end(args);
63 updateText();
66 Footer::Footer() : Window(0, 24, 80, 1, TYPE_NON_FOCUSABLE, false)
68 setColorScheme(CenterIM::SCHEME_FOOTER);
70 label_ = new CppConsUI::Label;
71 addWidget(*label_, 0, 0);
73 onScreenResized();
76 void Footer::init()
78 g_assert(my_instance_ == nullptr);
80 my_instance_ = new Footer;
81 my_instance_->show();
84 void Footer::finalize()
86 g_assert(my_instance_ != nullptr);
88 delete my_instance_;
89 my_instance_ = nullptr;
92 void Footer::updateText()
94 if (values_.empty()) {
95 label_->setText(nullptr);
96 return;
99 Values::iterator i = values_.begin();
100 const char *fmt = i->c_str();
101 ++i;
103 char out[1024];
104 char *cur_out = out;
106 while (*fmt != '\0' && cur_out < out + sizeof(out) - 1) {
107 if (*fmt == '%') {
108 if (*(fmt + 1) == '%')
109 ++fmt;
110 else if (*(fmt + 1) == 's') {
111 char *con = g_strdup(i->c_str());
112 ++i;
113 char *act = std::strstr(con, "|");
114 g_assert(act);
115 *act++ = '\0';
116 const char *key = KEYCONFIG->getKeyBind(con, act);
117 g_free(con);
119 std::strncpy(cur_out, key, out + sizeof(out) - 1 - cur_out);
120 cur_out += std::strlen(key);
122 fmt += 2;
123 continue;
126 *cur_out++ = *fmt++;
128 *cur_out = '\0';
130 label_->setText(out);
133 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: