summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSebastien Helleu <flashcode@flashtux.org>2008-04-04 15:50:05 +0200
committerSebastien Helleu <flashcode@flashtux.org>2008-04-04 15:50:05 +0200
commit8211dd6043557f445bf664507fe6daf22a5aa694 (patch)
treee4d52a1730c08315d9e8c5f8c4b976bca663fdaf /src
parent48bbd32f123f1212441e909e7892eeec8e97f16e (diff)
downloadweechat-8211dd6043557f445bf664507fe6daf22a5aa694.zip
Added auto-resize feature for bars
Diffstat (limited to 'src')
-rw-r--r--src/gui/curses/gui-curses-bar.c211
-rw-r--r--src/gui/gui-bar.c94
-rw-r--r--src/gui/gui-bar.h1
3 files changed, 236 insertions, 70 deletions
diff --git a/src/gui/curses/gui-curses-bar.c b/src/gui/curses/gui-curses-bar.c
index 1ea49bc5e..3f9b9c355 100644
--- a/src/gui/curses/gui-curses-bar.c
+++ b/src/gui/curses/gui-curses-bar.c
@@ -381,6 +381,40 @@ gui_bar_window_new (struct t_gui_bar *bar, struct t_gui_window *window)
}
/*
+ * gui_bar_window_recreate_bar_windows: recreate bar windows for all windows
+ */
+
+void
+gui_bar_window_recreate_bar_windows (struct t_gui_bar *bar)
+{
+ struct t_gui_window *ptr_win;
+ struct t_gui_bar_window *ptr_bar_win;
+
+ if (bar->type == GUI_BAR_TYPE_ROOT)
+ {
+ gui_bar_window_calculate_pos_size (bar->bar_window, NULL);
+ gui_bar_window_create_win (bar->bar_window);
+ gui_window_refresh_needed = 1;
+ }
+ else
+ {
+ for (ptr_win = gui_windows; ptr_win; ptr_win = ptr_win->next_window)
+ {
+ for (ptr_bar_win = GUI_CURSES(ptr_win)->bar_windows;
+ ptr_bar_win; ptr_bar_win = ptr_bar_win->next_bar_window)
+ {
+ if (ptr_bar_win->bar == bar)
+ {
+ gui_bar_window_calculate_pos_size (ptr_bar_win, ptr_win);
+ gui_bar_window_create_win (ptr_bar_win);
+ ptr_win->refresh_needed = 1;
+ }
+ }
+ }
+ }
+}
+
+/*
* gui_bar_window_free: free a bar window
*/
@@ -585,8 +619,9 @@ void
gui_bar_window_draw (struct t_gui_bar_window *bar_window,
struct t_gui_window *window)
{
- int x, y, i, max_width, max_height, items_count, num_lines, line;
- char *item_value, *item_value2, **items;
+ int x, y, i, items_count, num_lines, line;
+ char *content, *item_value, *item_value2, **items;
+ int content_length, length, max_length;
struct t_gui_bar_item *ptr_item;
if ((bar_window->bar->position == GUI_BAR_POSITION_LEFT)
@@ -595,55 +630,151 @@ gui_bar_window_draw (struct t_gui_bar_window *bar_window,
else
gui_window_curses_clear (bar_window->win_bar, GUI_COLOR_STATUS);
- max_width = bar_window->width;
- max_height = bar_window->height;
-
- x = 0;
- y = 0;
-
- /* for each item of bar */
- for (i = 0; i < bar_window->bar->items_count; i++)
+ if (bar_window->bar->size == 0)
+ {
+ content = NULL;
+ content_length = 1;
+ for (i = 0; i < bar_window->bar->items_count; i++)
+ {
+ ptr_item = gui_bar_item_search (bar_window->bar->items_array[i]);
+ if (ptr_item && ptr_item->build_callback)
+ {
+ item_value = (ptr_item->build_callback) (ptr_item->build_callback_data,
+ ptr_item, window,
+ 0, 0);
+ if (item_value)
+ {
+ if (item_value[0])
+ {
+ if (!content)
+ {
+ content_length += strlen (item_value);
+ content = strdup (item_value);
+ }
+ else
+ {
+ content_length += 1 + strlen (item_value);
+ content = realloc (content, content_length);
+ if ((bar_window->bar->position == GUI_BAR_POSITION_LEFT)
+ || (bar_window->bar->position == GUI_BAR_POSITION_RIGHT))
+ {
+ strcat (content, "\n");
+ }
+ strcat (content, item_value);
+ }
+ }
+ free (item_value);
+ }
+ }
+ }
+ if (content)
+ {
+ items = string_explode (content, "\n", 0, 0, &items_count);
+ if (items_count == 0)
+ {
+ gui_bar_set_current_size (bar_window->bar, 1);
+ }
+ else
+ {
+ switch (bar_window->bar->position)
+ {
+ case GUI_BAR_POSITION_BOTTOM:
+ case GUI_BAR_POSITION_TOP:
+ if (bar_window->bar->current_size != items_count)
+ {
+ gui_bar_set_current_size (bar_window->bar, items_count);
+ gui_bar_window_recreate_bar_windows (bar_window->bar);
+ }
+ break;
+ case GUI_BAR_POSITION_LEFT:
+ case GUI_BAR_POSITION_RIGHT:
+ /* search longer line */
+ max_length = 0;
+ for (line = 0; line < items_count; line++)
+ {
+ length = gui_chat_strlen_screen (items[line]);
+ if (length > max_length)
+ max_length = length;
+ }
+ if (max_length == 0)
+ max_length = 1;
+ if (bar_window->bar->current_size != max_length)
+ {
+ gui_bar_set_current_size (bar_window->bar,
+ max_length);
+ gui_bar_window_recreate_bar_windows (bar_window->bar);
+ }
+ break;
+ case GUI_BAR_NUM_POSITIONS:
+ /* make C compiler happy */
+ break;
+ }
+ x = 0;
+ y = 0;
+ for (line = 0; line < items_count; line++)
+ {
+ wmove (bar_window->win_bar, y, x);
+ x += gui_bar_window_print_string (bar_window,
+ items[line],
+ bar_window->width);
+ x = 0;
+ y++;
+ }
+ }
+ if (items)
+ string_free_exploded (items);
+ free (content);
+ }
+ }
+ else
{
- ptr_item = gui_bar_item_search (bar_window->bar->items_array[i]);
- if (ptr_item && ptr_item->build_callback)
+ x = 0;
+ y = 0;
+
+ for (i = 0; i < bar_window->bar->items_count; i++)
{
- item_value = (ptr_item->build_callback) (ptr_item->build_callback_data,
- ptr_item, window,
- max_width, max_height);
- if (item_value)
+ ptr_item = gui_bar_item_search (bar_window->bar->items_array[i]);
+ if (ptr_item && ptr_item->build_callback)
{
- if (item_value[0])
+ item_value = (ptr_item->build_callback) (ptr_item->build_callback_data,
+ ptr_item, window,
+ bar_window->width,
+ bar_window->height);
+ if (item_value)
{
- /* 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++)
+ if (item_value[0])
{
- wmove (bar_window->win_bar, y, x);
- x += gui_bar_window_print_string (bar_window,
- items[line],
- max_width);
- if (num_lines > 1)
+ /* replace \n by spaces when height is 1 */
+ item_value2 = (bar_window->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 <= bar_window->height) ?
+ items_count : bar_window->height;
+ for (line = 0; line < num_lines; line++)
{
- x = 0;
- y++;
+ wmove (bar_window->win_bar, y, x);
+ x += gui_bar_window_print_string (bar_window,
+ items[line],
+ bar_window->width);
+ if (num_lines > 1)
+ {
+ x = 0;
+ y++;
+ }
}
+ if (item_value2)
+ free (item_value2);
+ if (items)
+ string_free_exploded (items);
}
- if (item_value2)
- free (item_value2);
- if (items)
- string_free_exploded (items);
+ free (item_value);
}
- free (item_value);
}
}
}
-
+
wnoutrefresh (bar_window->win_bar);
if (bar_window->bar->separator)
@@ -680,7 +811,7 @@ gui_bar_window_draw (struct t_gui_bar_window *bar_window,
}
wnoutrefresh (bar_window->win_separator);
}
-
+
refresh ();
}
diff --git a/src/gui/gui-bar.c b/src/gui/gui-bar.c
index 602ef8f4d..70862944c 100644
--- a/src/gui/gui-bar.c
+++ b/src/gui/gui-bar.c
@@ -266,12 +266,12 @@ gui_bar_refresh (struct t_gui_bar *bar)
void
gui_bar_set_name (struct t_gui_bar *bar, char *name)
{
- if (name && name[0])
- {
- if (bar->name)
- free (bar->name);
- bar->name = strdup (name);
- }
+ if (!name || !name[0])
+ return;
+
+ if (bar->name)
+ free (bar->name);
+ bar->name = strdup (name);
}
/*
@@ -360,8 +360,6 @@ gui_bar_set_number (struct t_gui_bar *bar, int number)
gui_bar_window_new (ptr_bar, ptr_win);
}
}
-
- gui_window_refresh_needed = 1;
}
/*
@@ -373,36 +371,54 @@ gui_bar_set_position (struct t_gui_bar *bar, char *position)
{
int position_value;
- if (position && position[0])
+ if (!position || !position[0])
+ return;
+
+ position_value = gui_bar_get_position (position);
+ if ((position_value >= 0) && ((int)bar->position != position_value))
{
- position_value = gui_bar_get_position (position);
- if ((position_value >= 0) && ((int)bar->position != position_value))
- {
- bar->position = position_value;
- gui_bar_refresh (bar);
- }
+ bar->position = position_value;
}
}
/*
+ * gui_bar_set_current_size: set current size for a bar
+ */
+
+void
+gui_bar_set_current_size (struct t_gui_bar *bar, int current_size)
+{
+ if (current_size < 0)
+ return;
+
+ if (current_size == 0)
+ current_size = 1;
+
+ /* check if new size is ok if it's more than before */
+ if (current_size > bar->current_size
+ && !gui_bar_check_size_add (bar, current_size - bar->current_size))
+ return;
+
+ bar->current_size = current_size;
+}
+
+/*
* gui_bar_set_size: set size for a bar
*/
void
gui_bar_set_size (struct t_gui_bar *bar, int size)
{
- if (size >= 0)
- {
- /* check if new size is ok */
- if (size > bar->current_size
- && !gui_bar_check_size_add (bar, size - bar->current_size))
- return;
-
- bar->size = size;
- bar->current_size = (size == 0) ? 1 : size;
-
- gui_bar_refresh (bar);
- }
+ if (size < 0)
+ return;
+
+ /* check if new size is ok if it's more than before */
+ if (size > bar->current_size
+ && !gui_bar_check_size_add (bar, size - bar->current_size))
+ return;
+
+ bar->size = size;
+ bar->current_size = (size == 0) ? 1 : size;
}
/*
@@ -440,6 +456,7 @@ gui_bar_set (struct t_gui_bar *bar, char *property, char *value)
{
long number;
char *error;
+ int new_size;
if (!bar || !property || !value)
return;
@@ -453,18 +470,35 @@ gui_bar_set (struct t_gui_bar *bar, char *property, char *value)
error = NULL;
number = strtol (value, &error, 10);
if (error && !error[0])
+ {
gui_bar_set_number (bar, number);
+ gui_window_refresh_needed = 1;
+ }
}
else if (string_strcasecmp (property, "position") == 0)
{
gui_bar_set_position (bar, value);
+ gui_bar_refresh (bar);
}
else if (string_strcasecmp (property, "size") == 0)
{
error = NULL;
- number = strtol (value, &error, 10);
- if (error && !error[0])
- gui_bar_set_size (bar, number);
+ number = strtol (((value[0] == '+') || (value[0] == '-')) ?
+ value + 1 : value,
+ &error,
+ 10);
+ if (!error || error[0])
+ return;
+ if (value[0] == '+')
+ new_size = bar->current_size + number;
+ else if (value[0] == '-')
+ new_size = bar->current_size - number;
+ else
+ new_size = number;
+ if ((value[0] == '-') && (new_size < 1))
+ return;
+ gui_bar_set_size (bar, new_size);
+ gui_bar_refresh (bar);
}
else if (string_strcasecmp (property, "separator") == 0)
{
diff --git a/src/gui/gui-bar.h b/src/gui/gui-bar.h
index 100b5f23f..d5b781e55 100644
--- a/src/gui/gui-bar.h
+++ b/src/gui/gui-bar.h
@@ -82,6 +82,7 @@ extern struct t_gui_bar *gui_bar_search (char *name);
extern struct t_gui_bar *gui_bar_new (struct t_weechat_plugin *plugin,
char *name, char *type, char *position,
int size, int separator, char *items);
+extern void gui_bar_set_current_size (struct t_gui_bar *bar, int current_size);
extern void gui_bar_set (struct t_gui_bar *bar, char *property, char *value);
extern void gui_bar_update (char *name);
extern void gui_bar_free (struct t_gui_bar *bar);