1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-30 06:46:38 +02:00

Added auto-resize feature for bars

This commit is contained in:
Sebastien Helleu
2008-04-04 15:50:05 +02:00
parent 48bbd32f12
commit 8211dd6043
3 changed files with 236 additions and 70 deletions
+171 -40
View File
@@ -380,6 +380,40 @@ gui_bar_window_new (struct t_gui_bar *bar, struct t_gui_window *window)
return 0;
}
/*
* 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)
{
ptr_item = gui_bar_item_search (bar_window->bar->items_array[i]);
if (ptr_item && ptr_item->build_callback)
content = NULL;
content_length = 1;
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,
0, 0);
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)
if (!content)
{
x = 0;
y++;
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);
}
}
if (item_value2)
free (item_value2);
if (items)
string_free_exploded (items);
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
{
x = 0;
y = 0;
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,
bar_window->width,
bar_window->height);
if (item_value)
{
if (item_value[0])
{
/* 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++)
{
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);
}
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 ();
}
+64 -30
View File
@@ -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,17 +371,37 @@ 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
*/
@@ -391,18 +409,16 @@ gui_bar_set_position (struct t_gui_bar *bar, char *position)
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)
{
+1
View File
@@ -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);