summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSebastien Helleu <flashcode@flashtux.org>2008-03-07 10:54:33 +0100
committerSebastien Helleu <flashcode@flashtux.org>2008-03-07 10:54:33 +0100
commit519f62c11d4c22f4544588a7f8f1ee7acc6a4a7c (patch)
tree459ebd9095079f6fc12b7dd10c6ee93ed6aa6f24 /src
parent32e976ae72442b8ce36098c2b9d85a47db5cfda2 (diff)
downloadweechat-519f62c11d4c22f4544588a7f8f1ee7acc6a4a7c.zip
Added "max_height" parameter for bar item rebuild callbacks
Diffstat (limited to 'src')
-rw-r--r--src/gui/curses/gui-curses-bar.c106
-rw-r--r--src/gui/curses/gui-curses-chat.c12
-rw-r--r--src/gui/curses/gui-curses-window.c18
-rw-r--r--src/gui/curses/gui-curses.h1
-rw-r--r--src/gui/gui-bar-item.c20
-rw-r--r--src/gui/gui-bar-item.h4
-rw-r--r--src/plugins/scripts/lua/weechat-lua-api.c14
-rw-r--r--src/plugins/scripts/perl/weechat-perl-api.c14
-rw-r--r--src/plugins/scripts/python/weechat-python-api.c14
-rw-r--r--src/plugins/scripts/ruby/weechat-ruby-api.c14
-rw-r--r--src/plugins/scripts/script-api.c3
-rw-r--r--src/plugins/scripts/script-api.h3
-rw-r--r--src/plugins/weechat-plugin.h2
13 files changed, 159 insertions, 66 deletions
diff --git a/src/gui/curses/gui-curses-bar.c b/src/gui/curses/gui-curses-bar.c
index 24b350804..ecdc83aff 100644
--- a/src/gui/curses/gui-curses-bar.c
+++ b/src/gui/curses/gui-curses-bar.c
@@ -29,6 +29,8 @@
#include "../../core/weechat.h"
#include "../../core/wee-log.h"
+#include "../../core/wee-string.h"
+#include "../../core/wee-utf8.h"
#include "../gui-bar.h"
#include "../gui-bar-item.h"
#include "../gui-chat.h"
@@ -213,44 +215,79 @@ gui_bar_window_new (struct t_gui_bar *bar, struct t_gui_window *window)
}
/*
- * gui_bar_window_print: print text on a bar window
+ * gui_bar_window_print_string: print a string text on a bar window
+ * return number of chars displayed on screen
*/
-void
-gui_bar_window_print (struct t_gui_bar_window *bar_win, char *text,
- int *max_width)
+int
+gui_bar_window_print_string (struct t_gui_bar_window *bar_win,
+ char *string, int max_chars)
{
- int weechat_color;
- char str_color[3];
-
+ int weechat_color, num_displayed, size_on_screen;
+ char str_color[3], utf_char[16], *next_char, *output;
+
+ if (!string || !string[0])
+ return 0;
+
if ((bar_win->bar->position == GUI_BAR_POSITION_LEFT)
|| (bar_win->bar->position == GUI_BAR_POSITION_RIGHT))
gui_window_set_weechat_color (bar_win->win_bar, GUI_COLOR_CHAT);
else
gui_window_set_weechat_color (bar_win->win_bar, GUI_COLOR_STATUS);
- while (text && text[0])
+ num_displayed = 0;
+
+ while (string && string[0])
{
- if (text[0] == GUI_COLOR_COLOR_CHAR)
+ if (string[0] == GUI_COLOR_COLOR_CHAR)
{
- text++;
- if (isdigit (text[0]) && isdigit (text[1]))
+ string++;
+ if (isdigit (string[0]) && isdigit (string[1]))
{
- str_color[0] = text[0];
- str_color[1] = text[1];
+ str_color[0] = string[0];
+ str_color[1] = string[1];
str_color[2] = '\0';
- text += 2;
+ string += 2;
sscanf (str_color, "%d", &weechat_color);
gui_window_set_weechat_color (bar_win->win_bar, weechat_color);
}
}
else
{
- wprintw (bar_win->win_bar, "%c", text[0]);
- (*max_width)--;
- text++;
+ next_char = utf8_next_char (string);
+ if (!next_char)
+ break;
+
+ memcpy (utf_char, string, next_char - string);
+ utf_char[next_char - string] = '\0';
+
+ if (gui_window_utf_char_valid (utf_char))
+ {
+ output = string_iconv_from_internal (NULL, utf_char);
+ wprintw (bar_win->win_bar, "%s",
+ (output) ? output : utf_char);
+ size_on_screen = utf8_char_size_screen ((output) ?
+ output : utf_char);
+ num_displayed += size_on_screen;
+ max_chars -= size_on_screen;
+ if (output)
+ free (output);
+ }
+ else
+ {
+ wprintw (bar_win->win_bar, ".");
+ num_displayed++;
+ max_chars--;
+ }
+
+ string = next_char;
+
+ if (max_chars <= 0)
+ break;
}
}
+
+ return num_displayed;
}
/*
@@ -261,8 +298,8 @@ void
gui_bar_window_draw (struct t_gui_window *window,
struct t_gui_bar_window *bar_win)
{
- int i, max_width;
- char *item_value;
+ int x, y, i, max_width, max_height, items_count, num_lines, line;
+ char *item_value, *item_value2, **items;
struct t_gui_bar_item *ptr_item;
if ((bar_win->bar->position == GUI_BAR_POSITION_LEFT)
@@ -272,7 +309,12 @@ gui_bar_window_draw (struct t_gui_window *window,
gui_window_curses_clear (bar_win->win_bar, GUI_COLOR_STATUS);
max_width = bar_win->width;
+ max_height = bar_win->height;
+
+ x = 0;
+ y = 0;
+ /* for each item of bar */
for (i = 0; i < bar_win->bar->items_count; i++)
{
ptr_item = gui_bar_item_search (bar_win->bar->items_array[i]);
@@ -280,14 +322,32 @@ gui_bar_window_draw (struct t_gui_window *window,
{
item_value = (ptr_item->build_callback) (ptr_item->build_callback_data,
ptr_item, window,
- max_width);
+ max_width, max_height);
if (item_value)
{
if (item_value[0])
{
- gui_bar_window_print (bar_win, item_value, &max_width);
- if (max_width < 0)
- max_width = 0;
+ /* replace \n by spaces when height is 1 */
+ item_value2 = (max_height == 1) ?
+ string_replace (item_value, "\n", " ") : NULL;
+ items = string_explode ((item_value2) ? item_value2 : item_value,
+ "\n", 0, 0,
+ &items_count);
+ num_lines = (items_count <= max_height) ?
+ items_count : max_height;
+ for (line = 0; line < num_lines; line++)
+ {
+ wmove (bar_win->win_bar, y, x);
+ x += gui_bar_window_print_string (bar_win, items[line],
+ max_width);
+ if (num_lines > 1)
+ {
+ x = 0;
+ y++;
+ }
+ }
+ if (item_value2)
+ free (item_value2);
}
free (item_value);
}
diff --git a/src/gui/curses/gui-curses-chat.c b/src/gui/curses/gui-curses-chat.c
index 081bb7ffa..6adbb13f6 100644
--- a/src/gui/curses/gui-curses-chat.c
+++ b/src/gui/curses/gui-curses-chat.c
@@ -414,13 +414,7 @@ gui_chat_display_word_raw (struct t_gui_window *window, char *string,
{
memcpy (utf_char, string, next_char - string);
utf_char[next_char - string] = '\0';
- if ((((unsigned char)(utf_char[0]) == 146)
- || ((unsigned char)(utf_char[0]) == 0x7F))
- && (!utf_char[1]))
- {
- wprintw (GUI_CURSES(window)->win_chat, ".");
- }
- else
+ if (gui_window_utf_char_valid (utf_char))
{
output = string_iconv_from_internal (NULL, utf_char);
wprintw (GUI_CURSES(window)->win_chat,
@@ -428,6 +422,10 @@ gui_chat_display_word_raw (struct t_gui_window *window, char *string,
if (output)
free (output);
}
+ else
+ {
+ wprintw (GUI_CURSES(window)->win_chat, ".");
+ }
}
string = next_char;
diff --git a/src/gui/curses/gui-curses-window.c b/src/gui/curses/gui-curses-window.c
index 89f6a3087..d2ab8720e 100644
--- a/src/gui/curses/gui-curses-window.c
+++ b/src/gui/curses/gui-curses-window.c
@@ -135,6 +135,24 @@ gui_window_objects_free (struct t_gui_window *window, int free_separator)
}
/*
+ * gui_window_utf_char_valid: return 1 if utf char is valid for screen
+ * otherwise return 0
+ */
+
+int
+gui_window_utf_char_valid (char *utf_char)
+{
+ /* 146 or 0x7F are not valid */
+ if ((((unsigned char)(utf_char[0]) == 146)
+ || ((unsigned char)(utf_char[0]) == 0x7F))
+ && (!utf_char[1]))
+ return 0;
+
+ /* any other char is valid */
+ return 1;
+}
+
+/*
* gui_window_wprintw: decode then display string with wprintw
*/
diff --git a/src/gui/curses/gui-curses.h b/src/gui/curses/gui-curses.h
index 5a194f88a..c2a77f76e 100644
--- a/src/gui/curses/gui-curses.h
+++ b/src/gui/curses/gui-curses.h
@@ -79,6 +79,7 @@ extern void gui_keyboard_default_bindings ();
extern int gui_keyboard_read_cb (void *data);
/* window functions */
+extern int gui_window_utf_char_valid (char *utf_char);
extern void gui_window_wprintw (WINDOW *window, char *data, ...);
extern void gui_window_curses_clear (WINDOW *window, int num_color);
extern void gui_window_set_weechat_color (WINDOW *window, int num_color);
diff --git a/src/gui/gui-bar-item.c b/src/gui/gui-bar-item.c
index 2239486ed..b70286cb5 100644
--- a/src/gui/gui-bar-item.c
+++ b/src/gui/gui-bar-item.c
@@ -109,7 +109,7 @@ gui_bar_item_new (struct t_weechat_plugin *plugin, char *name,
char *(*build_callback)(void *data,
struct t_gui_bar_item *item,
struct t_gui_window *window,
- int max_width),
+ int max_width, int max_height),
void *build_callback_data)
{
struct t_gui_bar_item *new_bar_item;
@@ -249,7 +249,7 @@ gui_bar_item_free_all_plugin (struct t_weechat_plugin *plugin)
char *
gui_bar_item_default_buffer_count (void *data, struct t_gui_bar_item *item,
struct t_gui_window *window,
- int max_width)
+ int max_width, int max_height)
{
char buf[64];
@@ -258,6 +258,7 @@ gui_bar_item_default_buffer_count (void *data, struct t_gui_bar_item *item,
(void) item;
(void) window;
(void) max_width;
+ (void) max_height;
snprintf (buf, sizeof (buf), "%s[%s%d%s] ",
GUI_COLOR(GUI_COLOR_STATUS_DELIMITERS),
@@ -275,7 +276,7 @@ gui_bar_item_default_buffer_count (void *data, struct t_gui_bar_item *item,
char *
gui_bar_item_default_buffer_plugin (void *data, struct t_gui_bar_item *item,
struct t_gui_window *window,
- int max_width)
+ int max_width, int max_height)
{
char buf[64];
@@ -283,6 +284,7 @@ gui_bar_item_default_buffer_plugin (void *data, struct t_gui_bar_item *item,
(void) data;
(void) item;
(void) max_width;
+ (void) max_height;
if (!window)
return NULL;
@@ -303,7 +305,7 @@ gui_bar_item_default_buffer_plugin (void *data, struct t_gui_bar_item *item,
char *
gui_bar_item_default_buffer_name (void *data, struct t_gui_bar_item *item,
struct t_gui_window *window,
- int max_width)
+ int max_width, int max_height)
{
char buf[256];
@@ -311,6 +313,7 @@ gui_bar_item_default_buffer_name (void *data, struct t_gui_bar_item *item,
(void) data;
(void) item;
(void) max_width;
+ (void) max_height;
if (!window)
return NULL;
@@ -336,7 +339,7 @@ gui_bar_item_default_buffer_name (void *data, struct t_gui_bar_item *item,
char *
gui_bar_item_default_nicklist_count (void *data, struct t_gui_bar_item *item,
struct t_gui_window *window,
- int max_width)
+ int max_width, int max_height)
{
char buf[64];
@@ -344,6 +347,7 @@ gui_bar_item_default_nicklist_count (void *data, struct t_gui_bar_item *item,
(void) data;
(void) item;
(void) max_width;
+ (void) max_height;
if (!window || !window->buffer->nicklist)
return NULL;
@@ -364,7 +368,7 @@ gui_bar_item_default_nicklist_count (void *data, struct t_gui_bar_item *item,
char *
gui_bar_item_default_scroll (void *data, struct t_gui_bar_item *item,
struct t_gui_window *window,
- int max_width)
+ int max_width, int max_height)
{
char buf[64];
@@ -372,6 +376,7 @@ gui_bar_item_default_scroll (void *data, struct t_gui_bar_item *item,
(void) data;
(void) item;
(void) max_width;
+ (void) max_height;
if (!window || !window->scroll)
return NULL;
@@ -390,7 +395,7 @@ gui_bar_item_default_scroll (void *data, struct t_gui_bar_item *item,
char *
gui_bar_item_default_hotlist (void *data, struct t_gui_bar_item *item,
struct t_gui_window *window,
- int max_width)
+ int max_width, int max_height)
{
char buf[1024], format[32];
struct t_gui_hotlist *ptr_hotlist;
@@ -401,6 +406,7 @@ gui_bar_item_default_hotlist (void *data, struct t_gui_bar_item *item,
(void) item;
(void) window;
(void) max_width;
+ (void) max_height;
if (!gui_hotlist)
return NULL;
diff --git a/src/gui/gui-bar-item.h b/src/gui/gui-bar-item.h
index 5f5dfec7c..78cc93681 100644
--- a/src/gui/gui-bar-item.h
+++ b/src/gui/gui-bar-item.h
@@ -41,7 +41,7 @@ struct t_gui_bar_item
char *(*build_callback)(void *data,
struct t_gui_bar_item *item,
struct t_gui_window *window,
- int max_width);
+ int max_width, int max_height);
/* callback called for building item */
void *build_callback_data; /* data for callback */
struct t_gui_bar_item *prev_item; /* link to previous bar item */
@@ -61,7 +61,7 @@ extern struct t_gui_bar_item *gui_bar_item_new (struct t_weechat_plugin *plugin,
char *(*build_callback)(void *data,
struct t_gui_bar_item *item,
struct t_gui_window *window,
- int max_width),
+ int max_width, int max_height),
void *build_callback_data);
extern void gui_bar_item_update (char *name);
extern void gui_bar_item_free (struct t_gui_bar_item *item);
diff --git a/src/plugins/scripts/lua/weechat-lua-api.c b/src/plugins/scripts/lua/weechat-lua-api.c
index 58d390092..f996c847c 100644
--- a/src/plugins/scripts/lua/weechat-lua-api.c
+++ b/src/plugins/scripts/lua/weechat-lua-api.c
@@ -3412,20 +3412,22 @@ weechat_lua_api_bar_item_search (lua_State *L)
char *
weechat_lua_api_bar_item_build_cb (void *data, struct t_gui_bar_item *item,
struct t_gui_window *window,
- int max_width)
+ int max_width, int max_height)
{
struct t_script_callback *script_callback;
- char *lua_argv[4], *ret;
- static char buf[32];
+ char *lua_argv[5], *ret;
+ static char str_width[32], str_height[32];
script_callback = (struct t_script_callback *)data;
- snprintf (buf, sizeof (buf) - 1, "%d", max_width);
+ snprintf (str_width, sizeof (str_width), "%d", max_width);
+ snprintf (str_height, sizeof (str_height), "%d", max_height);
lua_argv[0] = script_ptr2str (item);
lua_argv[1] = script_ptr2str (window);
- lua_argv[2] = buf;
- lua_argv[3] = NULL;
+ lua_argv[2] = str_width;
+ lua_argv[3] = str_height;
+ lua_argv[4] = NULL;
ret = (char *)weechat_lua_exec (script_callback->script,
WEECHAT_SCRIPT_EXEC_STRING,
diff --git a/src/plugins/scripts/perl/weechat-perl-api.c b/src/plugins/scripts/perl/weechat-perl-api.c
index 4136490c5..66157e3c8 100644
--- a/src/plugins/scripts/perl/weechat-perl-api.c
+++ b/src/plugins/scripts/perl/weechat-perl-api.c
@@ -2737,20 +2737,22 @@ static XS (XS_weechat_bar_item_search)
char *
weechat_perl_api_bar_item_build_cb (void *data, struct t_gui_bar_item *item,
struct t_gui_window *window,
- int max_width)
+ int max_width, int max_height)
{
struct t_script_callback *script_callback;
- char *perl_argv[4], *ret;
- static char buf[32];
+ char *perl_argv[5], *ret;
+ static char str_width[32], str_height[32];
script_callback = (struct t_script_callback *)data;
- snprintf (buf, sizeof (buf) - 1, "%d", max_width);
+ snprintf (str_width, sizeof (str_width), "%d", max_width);
+ snprintf (str_height, sizeof (str_height), "%d", max_height);
perl_argv[0] = script_ptr2str (item);
perl_argv[1] = script_ptr2str (window);
- perl_argv[2] = buf;
- perl_argv[3] = NULL;
+ perl_argv[2] = str_width;
+ perl_argv[3] = str_height;
+ perl_argv[4] = NULL;
ret = (char *)weechat_perl_exec (script_callback->script,
WEECHAT_SCRIPT_EXEC_STRING,
diff --git a/src/plugins/scripts/python/weechat-python-api.c b/src/plugins/scripts/python/weechat-python-api.c
index e4a1dd232..79092b528 100644
--- a/src/plugins/scripts/python/weechat-python-api.c
+++ b/src/plugins/scripts/python/weechat-python-api.c
@@ -3013,20 +3013,22 @@ weechat_python_api_bar_item_search (PyObject *self, PyObject *args)
char *
weechat_python_api_bar_item_build_cb (void *data, struct t_gui_bar_item *item,
struct t_gui_window *window,
- int max_width)
+ int max_width, int max_height)
{
struct t_script_callback *script_callback;
- char *python_argv[4], *ret;
- static char buf[32];
+ char *python_argv[5], *ret;
+ static char str_width[32], str_height[32];
script_callback = (struct t_script_callback *)data;
- snprintf (buf, sizeof (buf) - 1, "%d", max_width);
+ snprintf (str_width, sizeof (str_width), "%d", max_width);
+ snprintf (str_height, sizeof (str_height), "%d", max_height);
python_argv[0] = script_ptr2str (item);
python_argv[1] = script_ptr2str (window);
- python_argv[2] = buf;
- python_argv[3] = NULL;
+ python_argv[2] = str_width;
+ python_argv[3] = str_height;
+ python_argv[4] = NULL;
ret = (char *)weechat_python_exec (script_callback->script,
WEECHAT_SCRIPT_EXEC_STRING,
diff --git a/src/plugins/scripts/ruby/weechat-ruby-api.c b/src/plugins/scripts/ruby/weechat-ruby-api.c
index 152e3d5f6..46c1c8db0 100644
--- a/src/plugins/scripts/ruby/weechat-ruby-api.c
+++ b/src/plugins/scripts/ruby/weechat-ruby-api.c
@@ -3473,20 +3473,22 @@ weechat_ruby_api_bar_item_search (VALUE class, VALUE name)
char *
weechat_ruby_api_bar_item_build_cb (void *data, struct t_gui_bar_item *item,
struct t_gui_window *window,
- int max_width)
+ int max_width, int max_height)
{
struct t_script_callback *script_callback;
- char *ruby_argv[4], *ret;
- static char buf[32];
+ char *ruby_argv[5], *ret;
+ static char str_width[32], str_height[32];
script_callback = (struct t_script_callback *)data;
- snprintf (buf, sizeof (buf) - 1, "%d", max_width);
+ snprintf (str_width, sizeof (str_width), "%d", max_width);
+ snprintf (str_height, sizeof (str_height), "%d", max_height);
ruby_argv[0] = script_ptr2str (item);
ruby_argv[1] = script_ptr2str (window);
- ruby_argv[2] = buf;
- ruby_argv[3] = NULL;
+ ruby_argv[2] = str_width;
+ ruby_argv[3] = str_height;
+ ruby_argv[4] = NULL;
ret = (char *)weechat_ruby_exec (script_callback->script,
WEECHAT_SCRIPT_EXEC_STRING,
diff --git a/src/plugins/scripts/script-api.c b/src/plugins/scripts/script-api.c
index ebb73ba15..1230254df 100644
--- a/src/plugins/scripts/script-api.c
+++ b/src/plugins/scripts/script-api.c
@@ -825,7 +825,8 @@ script_api_bar_item_new (struct t_weechat_plugin *weechat_plugin,
char *(*build_callback)(void *data,
struct t_gui_bar_item *item,
struct t_gui_window *window,
- int remaining_space),
+ int max_width,
+ int max_height),
char *function_build)
{
struct t_script_callback *new_script_callback;
diff --git a/src/plugins/scripts/script-api.h b/src/plugins/scripts/script-api.h
index 5a7177bf6..2aa7bd085 100644
--- a/src/plugins/scripts/script-api.h
+++ b/src/plugins/scripts/script-api.h
@@ -157,7 +157,8 @@ extern struct t_gui_bar_item *script_api_bar_item_new (struct t_weechat_plugin *
char *(*build_callback)(void *data,
struct t_gui_bar_item *item,
struct t_gui_window *window,
- int remaining_space),
+ int max_width,
+ int max_height),
char *function_build);
extern void script_api_bar_item_remove (struct t_weechat_plugin *weechat_plugin,
struct t_plugin_script *script,
diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h
index e0c278623..22ca98ce9 100644
--- a/src/plugins/weechat-plugin.h
+++ b/src/plugins/weechat-plugin.h
@@ -329,7 +329,7 @@ struct t_weechat_plugin
char *(*build_callback)(void *data,
struct t_gui_bar_item *item,
struct t_gui_window *window,
- int max_width),
+ int max_width, int max_height),
void *build_callback_data);
void (*bar_item_update) (char *name);
void (*bar_item_remove) (struct t_gui_bar_item *item);