summaryrefslogtreecommitdiff
path: root/src/gui
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/gui
parent32e976ae72442b8ce36098c2b9d85a47db5cfda2 (diff)
downloadweechat-519f62c11d4c22f4544588a7f8f1ee7acc6a4a7c.zip
Added "max_height" parameter for bar item rebuild callbacks
Diffstat (limited to 'src/gui')
-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
6 files changed, 122 insertions, 39 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);