1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-09-22 19:45:54 -04:00
profanity/src/ui/statusbar.c

409 lines
11 KiB
C
Raw Normal View History

/*
2013-02-02 14:57:46 -05:00
* statusbar.c
2012-02-20 15:07:38 -05:00
*
2018-01-21 10:00:02 -05:00
* Copyright (C) 2012 - 2018 James Booth <boothj5@gmail.com>
*
2012-02-20 15:07:38 -05:00
* This file is part of Profanity.
*
* Profanity 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.
*
* Profanity 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
2016-07-23 20:14:49 -04:00
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
2012-02-20 15:07:38 -05:00
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
*
2012-02-20 15:07:38 -05:00
*/
2016-03-31 16:05:02 -04:00
#include "config.h"
2012-09-08 11:51:09 -04:00
2013-08-23 17:39:03 -04:00
#include <assert.h>
#include <string.h>
2012-04-21 20:25:19 -04:00
#include <stdlib.h>
2016-03-31 16:05:02 -04:00
#ifdef HAVE_NCURSESW_NCURSES_H
2013-01-02 15:27:37 -05:00
#include <ncursesw/ncurses.h>
2016-03-31 16:05:02 -04:00
#elif HAVE_NCURSES_H
#include <ncurses.h>
2012-09-08 11:51:09 -04:00
#endif
2013-02-02 16:59:29 -05:00
#include "config/theme.h"
2016-07-24 10:43:51 -04:00
#include "config/preferences.h"
2013-02-02 15:55:58 -05:00
#include "ui/ui.h"
2014-04-07 16:12:30 -04:00
#include "ui/statusbar.h"
2014-04-07 16:50:28 -04:00
#include "ui/inputwin.h"
#include "ui/screen.h"
2012-02-12 17:20:21 -05:00
2018-03-08 14:55:17 -05:00
typedef struct _status_bar_tab_t {
char *display_name;
gboolean highlight;
} StatusBarTab;
2018-03-07 15:58:07 -05:00
typedef struct _status_bar_t {
2018-03-07 16:29:41 -05:00
gchar *time;
2018-03-07 15:58:07 -05:00
char *message;
2018-03-08 14:55:17 -05:00
GHashTable *tabs;
int current_tab;
2018-03-07 15:58:07 -05:00
} StatusBar;
2018-03-08 14:55:17 -05:00
#define MAX_TABS 5
#define SHOW_EMPTY_TABS FALSE
#define SHOW_NAME TRUE
2018-03-07 16:29:41 -05:00
static GTimeZone *tz;
2018-03-07 15:58:07 -05:00
static StatusBar *statusbar;
static WINDOW *statusbar_win;
2018-03-08 14:55:17 -05:00
2014-04-05 20:33:00 -04:00
static void _status_bar_draw(void);
2012-02-12 17:34:31 -05:00
2018-03-08 14:55:17 -05:00
void
_destroy_tab(StatusBarTab *tab)
{
if (tab) {
if (tab->display_name) {
free(tab->display_name);
}
free(tab);
}
}
2014-04-07 16:12:30 -04:00
void
2018-03-07 16:29:41 -05:00
status_bar_init(void)
2012-02-12 17:20:21 -05:00
{
2018-03-07 16:29:41 -05:00
tz = g_time_zone_new_local();
2018-03-07 15:58:07 -05:00
statusbar = malloc(sizeof(StatusBar));
2018-03-07 16:29:41 -05:00
statusbar->time = NULL;
2018-03-07 17:12:15 -05:00
statusbar->message = NULL;
2018-03-08 14:55:17 -05:00
statusbar->tabs = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)_destroy_tab);
StatusBarTab *console = malloc(sizeof(StatusBarTab));
console->display_name = strdup("console");
g_hash_table_insert(statusbar->tabs, GINT_TO_POINTER(1), console);
statusbar->current_tab = 1;
2018-03-07 16:29:41 -05:00
int row = screen_statusbar_row();
2018-03-07 15:58:07 -05:00
int cols = getmaxx(stdscr);
statusbar_win = newwin(1, cols, row, 0);
2018-03-07 16:29:41 -05:00
2014-04-05 20:33:00 -04:00
_status_bar_draw();
2012-02-12 17:20:21 -05:00
}
2018-03-07 16:29:41 -05:00
void
status_bar_close(void)
{
if (tz) {
g_time_zone_unref(tz);
}
if (statusbar) {
if (statusbar->time) {
g_free(statusbar->time);
}
2018-03-07 17:12:15 -05:00
if (statusbar->message) {
free(statusbar->message);
}
free(statusbar);
2018-03-07 16:29:41 -05:00
}
}
2014-04-07 16:12:30 -04:00
void
status_bar_update_virtual(void)
2012-02-12 17:34:31 -05:00
{
_status_bar_draw();
2012-02-12 17:34:31 -05:00
}
2014-04-07 16:12:30 -04:00
void
status_bar_resize(void)
2012-04-21 20:25:19 -04:00
{
int cols = getmaxx(stdscr);
2018-03-07 15:58:07 -05:00
werase(statusbar_win);
int row = screen_statusbar_row();
2018-03-07 15:58:07 -05:00
mvwin(statusbar_win, row, 0);
wresize(statusbar_win, 1, cols);
2018-03-07 16:37:29 -05:00
2014-04-05 20:33:00 -04:00
_status_bar_draw();
2012-04-21 20:25:19 -04:00
}
2012-02-12 17:34:31 -05:00
2014-04-07 16:12:30 -04:00
void
status_bar_set_all_inactive(void)
{
2018-03-08 14:55:17 -05:00
g_hash_table_remove_all(statusbar->tabs);
}
2014-04-07 16:12:30 -04:00
void
status_bar_current(int i)
2013-10-02 18:33:48 -04:00
{
2018-03-08 14:55:17 -05:00
if (i == 0) {
statusbar->current_tab = 10;
} else {
statusbar->current_tab = i;
}
_status_bar_draw();
2013-10-02 18:33:48 -04:00
}
2014-04-07 16:12:30 -04:00
void
status_bar_inactive(const int win)
2012-02-12 17:20:21 -05:00
{
2018-03-08 14:55:17 -05:00
int true_win = win;
if (true_win == 0) {
true_win = 10;
}
g_hash_table_remove(statusbar->tabs, GINT_TO_POINTER(true_win));
_status_bar_draw();
2012-02-12 17:20:21 -05:00
}
2014-04-07 16:12:30 -04:00
void
2018-03-08 14:55:17 -05:00
status_bar_active(const int win, char *name)
2012-02-12 17:20:21 -05:00
{
2018-03-08 14:55:17 -05:00
int true_win = win;
if (true_win == 0) {
true_win = 10;
}
StatusBarTab *tab = malloc(sizeof(StatusBarTab));
tab->display_name = strdup(name);
tab->highlight = FALSE;
g_hash_table_replace(statusbar->tabs, GINT_TO_POINTER(true_win), tab);
_status_bar_draw();
2012-02-12 17:20:21 -05:00
}
2014-04-07 16:12:30 -04:00
void
2018-03-08 14:55:17 -05:00
status_bar_new(const int win, char* name)
{
2018-03-08 14:55:17 -05:00
int true_win = win;
if (true_win == 0) {
true_win = 10;
}
StatusBarTab *tab = malloc(sizeof(StatusBarTab));
tab->display_name = strdup(name);
tab->highlight = TRUE;
g_hash_table_replace(statusbar->tabs, GINT_TO_POINTER(true_win), tab);
_status_bar_draw();
}
2014-04-07 16:12:30 -04:00
void
status_bar_get_password(void)
2012-02-12 17:20:21 -05:00
{
2018-03-07 17:12:15 -05:00
status_bar_print_message("Enter password:");
2012-02-12 17:20:21 -05:00
}
2014-04-07 16:12:30 -04:00
void
2015-10-25 19:31:11 -04:00
status_bar_print_message(const char *const msg)
2012-02-12 17:20:21 -05:00
{
2018-03-07 17:12:15 -05:00
if (statusbar->message) {
free(statusbar->message);
statusbar->message = NULL;
}
statusbar->message = strdup(msg);
_status_bar_draw();
2012-02-12 17:20:21 -05:00
}
2014-04-07 16:12:30 -04:00
void
status_bar_clear(void)
2012-02-12 17:34:31 -05:00
{
2018-03-08 14:55:17 -05:00
if (statusbar->message) {
free(statusbar->message);
statusbar->message = NULL;
}
werase(statusbar_win);
_status_bar_draw();
2012-02-12 17:34:31 -05:00
}
2014-04-07 16:12:30 -04:00
void
status_bar_clear_message(void)
2012-11-19 18:15:42 -05:00
{
2018-03-07 17:12:15 -05:00
if (statusbar->message) {
free(statusbar->message);
statusbar->message = NULL;
}
_status_bar_draw();
2013-08-28 17:11:44 -04:00
}
2013-08-29 19:10:05 -04:00
2018-03-08 14:55:17 -05:00
static int
_tabs_width(void)
{
if (SHOW_NAME) {
if (SHOW_EMPTY_TABS) {
int width = 4;
int i = 0;
for (i = 1; i <= MAX_TABS; i++) {
StatusBarTab *tab = g_hash_table_lookup(statusbar->tabs, GINT_TO_POINTER(i));
if (tab) {
width += strlen(tab->display_name);
width += 4;
} else {
width += 3;
}
}
return width;
} else {
int width = 4;
int i = 0;
for (i = 1; i <= MAX_TABS; i++) {
StatusBarTab *tab = g_hash_table_lookup(statusbar->tabs, GINT_TO_POINTER(i));
if (tab) {
width += strlen(tab->display_name);
width += 4;
}
}
return width;
}
} else {
if (SHOW_EMPTY_TABS) {
return MAX_TABS * 3 + 4;
} else {
return g_hash_table_size(statusbar->tabs) * 3 + 4;
}
}
}
2014-04-05 20:33:00 -04:00
static void
_status_bar_draw(void)
{
2018-03-07 16:37:29 -05:00
wbkgd(statusbar_win, theme_attrs(THEME_STATUS_TEXT));
2018-03-07 17:12:15 -05:00
int pos = 1;
2018-03-07 16:29:41 -05:00
char *time_pref = prefs_get_string(PREF_TIME_STATUSBAR);
if (g_strcmp0(time_pref, "off") != 0) {
2018-03-07 17:12:15 -05:00
// time
2018-03-07 16:29:41 -05:00
if (statusbar->time) {
g_free(statusbar->time);
statusbar->time = NULL;
}
GDateTime *datetime = g_date_time_new_now(tz);
statusbar->time = g_date_time_format(datetime, time_pref);
assert(statusbar->time != NULL);
g_date_time_unref(datetime);
int bracket_attrs = theme_attrs(THEME_STATUS_BRACKET);
int time_attrs = theme_attrs(THEME_STATUS_TIME);
size_t len = strlen(statusbar->time);
wattron(statusbar_win, bracket_attrs);
2018-03-07 17:12:15 -05:00
mvwaddch(statusbar_win, 0, pos, '[');
pos++;
2018-03-07 16:29:41 -05:00
wattroff(statusbar_win, bracket_attrs);
wattron(statusbar_win, time_attrs);
2018-03-07 17:12:15 -05:00
mvwprintw(statusbar_win, 0, pos, statusbar->time);
pos += len;
2018-03-07 16:29:41 -05:00
wattroff(statusbar_win, time_attrs);
wattron(statusbar_win, bracket_attrs);
2018-03-07 17:12:15 -05:00
mvwaddch(statusbar_win, 0, pos, ']');
2018-03-07 16:29:41 -05:00
wattroff(statusbar_win, bracket_attrs);
2018-03-07 17:12:15 -05:00
pos += 2;
// message
if (statusbar->message) {
mvwprintw(statusbar_win, 0, pos, statusbar->message);
}
} else {
// message
if (statusbar->message) {
mvwprintw(statusbar_win, 0, pos, statusbar->message);
}
2018-03-07 16:29:41 -05:00
}
prefs_free_string(time_pref);
2018-03-08 14:55:17 -05:00
// tabs
int cols = getmaxx(stdscr);
pos = cols - _tabs_width();
int bracket_attrs = theme_attrs(THEME_STATUS_BRACKET);
int i = 1;
for (i = 1; i <= MAX_TABS; i++) {
int display_num = i == 10 ? 0 : i;
StatusBarTab *tab = g_hash_table_lookup(statusbar->tabs, GINT_TO_POINTER(i));
if (tab || (tab == NULL && SHOW_EMPTY_TABS)) {
wattron(statusbar_win, bracket_attrs);
if (i == statusbar->current_tab) {
mvwprintw(statusbar_win, 0, pos, "-");
} else {
mvwprintw(statusbar_win, 0, pos, "[");
}
wattroff(statusbar_win, bracket_attrs);
pos++;
if (tab) {
if (tab->highlight) {
int status_attrs = theme_attrs(THEME_STATUS_NEW);
wattron(statusbar_win, status_attrs);
mvwprintw(statusbar_win, 0, pos, "%d", display_num);
if (SHOW_NAME) {
pos++;
mvwprintw(statusbar_win, 0, pos, ":");
pos++;
mvwprintw(statusbar_win, 0, pos, tab->display_name);
pos += strlen(tab->display_name) -1 ;
}
wattroff(statusbar_win, status_attrs);
} else {
int status_attrs = theme_attrs(THEME_STATUS_ACTIVE);
wattron(statusbar_win, status_attrs);
mvwprintw(statusbar_win, 0, pos, "%d", display_num);
if (SHOW_NAME) {
pos++;
mvwprintw(statusbar_win, 0, pos, ":");
pos++;
mvwprintw(statusbar_win, 0, pos, tab->display_name);
pos += strlen(tab->display_name) - 1;
}
wattroff(statusbar_win, status_attrs);
}
} else {
mvwprintw(statusbar_win, 0, pos, " ");
}
pos++;
wattron(statusbar_win, bracket_attrs);
if (i == statusbar->current_tab) {
mvwprintw(statusbar_win, 0, pos, "-");
} else {
mvwprintw(statusbar_win, 0, pos, "]");
}
pos++;
wattroff(statusbar_win, bracket_attrs);
}
}
wattron(statusbar_win, bracket_attrs);
mvwprintw(statusbar_win, 0, pos, "[");
wattroff(statusbar_win, bracket_attrs);
pos++;
mvwprintw(statusbar_win, 0, pos, " ");
pos++;
wattron(statusbar_win, bracket_attrs);
mvwprintw(statusbar_win, 0, pos, "]");
wattroff(statusbar_win, bracket_attrs);
pos++;
2018-03-07 15:58:07 -05:00
wnoutrefresh(statusbar_win);
2014-04-05 20:33:00 -04:00
inp_put_back();
2014-07-18 18:17:42 -04:00
}