issue 158 : dissociate object initialization from panel initialization

git-svn-id: http://tint2.googlecode.com/svn/trunk@220 121b4492-b84c-0410-8b4c-0d4edfb3f3cc
This commit is contained in:
lorthiois@bbsoft.fr 2009-10-18 17:54:09 +00:00
parent c83bdf1848
commit cd5b2b26a9
12 changed files with 192 additions and 174 deletions

View file

@ -26,9 +26,9 @@
#include "window.h"
#include "server.h"
#include "taskbar.h"
#include "panel.h"
#include "area.h"
#include "panel.h"
#include "taskbar.h"
#include "battery.h"
#include "clock.h"
@ -95,80 +95,80 @@ void init_battery()
g_free(path1);
}
FILE *fp;
Panel *panel;
Battery *battery;
int i, bat_percentage_height, bat_percentage_height_ink, bat_time_height, bat_time_height_ink;
for (i=0 ; i < nb_panel ; i++) {
panel = &panel1[i];
battery = &panel->battery;
if (battery_dir == 0) battery->area.on_screen = 0;
if (!battery->area.on_screen) continue;
battery->area.parent = panel;
battery->area.panel = panel;
battery->area._draw_foreground = draw_battery;
battery->area._resize = resize_battery;
battery->area.resize = 1;
battery->area.redraw = 1;
if((fp = fopen(path_energy_now, "r")) == NULL) {
fprintf(stderr, "ERROR: battery applet can't open energy_now\n");
panel->battery.area.on_screen = 0;
continue;
}
fclose(fp);
if((fp = fopen(path_energy_full, "r")) == NULL) {
fprintf(stderr, "ERROR: battery applet can't open energy_full\n");
panel->battery.area.on_screen = 0;
continue;
}
fclose(fp);
if((fp = fopen(path_current_now, "r")) == NULL) {
fprintf(stderr, "ERROR: battery applet can't open current_now\n");
panel->battery.area.on_screen = 0;
continue;
}
fclose(fp);
if((fp = fopen(path_status, "r")) == NULL) {
fprintf(stderr, "ERROR: battery applet can't open status");
panel->battery.area.on_screen = 0;
continue;
}
fclose(fp);
update_battery(&battery_state);
snprintf(buf_bat_percentage, sizeof(buf_bat_percentage), "%d%%", battery_state.percentage);
snprintf(buf_bat_time, sizeof(buf_bat_time), "%02d:%02d", battery_state.time.hours, battery_state.time.minutes);
get_text_size(bat1_font_desc, &bat_percentage_height_ink, &bat_percentage_height, panel->area.height, buf_bat_percentage, strlen(buf_bat_percentage));
get_text_size(bat2_font_desc, &bat_time_height_ink, &bat_time_height, panel->area.height, buf_bat_time, strlen(buf_bat_time));
if (panel_horizontal) {
// panel horizonal => fixed height and posy
battery->area.posy = panel->area.pix.border.width + panel->area.paddingy;
battery->area.height = panel->area.height - (2 * battery->area.posy);
}
else {
// panel vertical => fixed width, height, posy and posx
battery->area.posy = panel->clock.area.posy + panel->clock.area.height + panel->area.paddingx;
battery->area.height = (2 * battery->area.paddingxlr) + (bat_time_height + bat_percentage_height);
battery->area.posx = panel->area.pix.border.width + panel->area.paddingy;
battery->area.width = panel->area.width - (2 * panel->area.pix.border.width) - (2 * panel->area.paddingy);
}
battery->bat1_posy = (battery->area.height - bat_percentage_height) / 2;
battery->bat1_posy -= ((bat_time_height_ink + 2) / 2);
battery->bat2_posy = battery->bat1_posy + bat_percentage_height + 2 - (bat_percentage_height - bat_percentage_height_ink)/2 - (bat_time_height - bat_time_height_ink)/2;
}
if (battery_dir)
g_free(battery_dir);
}
void init_battery_panel(void *p)
{
Panel *panel = (Panel*)p;
Battery *battery = &panel->battery;
FILE *fp;
int bat_percentage_height, bat_percentage_height_ink, bat_time_height, bat_time_height_ink;
char *battery_dir = 0;
if (battery_dir == 0) battery->area.on_screen = 0;
if (!battery->area.on_screen) return;
battery->area.parent = p;
battery->area.panel = p;
battery->area._draw_foreground = draw_battery;
battery->area._resize = resize_battery;
battery->area.resize = 1;
battery->area.redraw = 1;
if((fp = fopen(path_energy_now, "r")) == NULL) {
fprintf(stderr, "ERROR: battery applet can't open energy_now\n");
panel->battery.area.on_screen = 0;
return;
}
fclose(fp);
if((fp = fopen(path_energy_full, "r")) == NULL) {
fprintf(stderr, "ERROR: battery applet can't open energy_full\n");
panel->battery.area.on_screen = 0;
return;
}
fclose(fp);
if((fp = fopen(path_current_now, "r")) == NULL) {
fprintf(stderr, "ERROR: battery applet can't open current_now\n");
panel->battery.area.on_screen = 0;
return;
}
fclose(fp);
if((fp = fopen(path_status, "r")) == NULL) {
fprintf(stderr, "ERROR: battery applet can't open status");
panel->battery.area.on_screen = 0;
return;
}
fclose(fp);
update_battery(&battery_state);
snprintf(buf_bat_percentage, sizeof(buf_bat_percentage), "%d%%", battery_state.percentage);
snprintf(buf_bat_time, sizeof(buf_bat_time), "%02d:%02d", battery_state.time.hours, battery_state.time.minutes);
get_text_size(bat1_font_desc, &bat_percentage_height_ink, &bat_percentage_height, panel->area.height, buf_bat_percentage, strlen(buf_bat_percentage));
get_text_size(bat2_font_desc, &bat_time_height_ink, &bat_time_height, panel->area.height, buf_bat_time, strlen(buf_bat_time));
if (panel_horizontal) {
// panel horizonal => fixed height and posy
battery->area.posy = panel->area.pix.border.width + panel->area.paddingy;
battery->area.height = panel->area.height - (2 * battery->area.posy);
}
else {
// panel vertical => fixed width, height, posy and posx
battery->area.posy = panel->clock.area.posy + panel->clock.area.height + panel->area.paddingx;
battery->area.height = (2 * battery->area.paddingxlr) + (bat_time_height + bat_percentage_height);
battery->area.posx = panel->area.pix.border.width + panel->area.paddingy;
battery->area.width = panel->area.width - (2 * panel->area.pix.border.width) - (2 * panel->area.paddingy);
}
battery->bat1_posy = (battery->area.height - bat_percentage_height) / 2;
battery->bat1_posy -= ((bat_time_height_ink + 2) / 2);
battery->bat2_posy = battery->bat1_posy + bat_percentage_height + 2 - (bat_percentage_height - bat_percentage_height_ink)/2 - (bat_time_height - bat_time_height_ink)/2;
}
void update_battery() {
FILE *fp;
char tmp[25];

View file

@ -17,6 +17,7 @@
#include "common.h"
#include "area.h"
typedef struct Battery {
// always start with area
Area area;
@ -57,6 +58,7 @@ extern char *path_energy_now, *path_energy_full, *path_current_now, *path_status
void update_battery();
void init_battery();
void init_battery_panel(void *panel);
void draw_battery(void *obj, cairo_t *c, int active);

View file

@ -26,9 +26,9 @@
#include "window.h"
#include "server.h"
#include "taskbar.h"
#include "panel.h"
#include "area.h"
#include "panel.h"
#include "taskbar.h"
#include "clock.h"
@ -56,58 +56,56 @@ void init_precision()
void init_clock()
{
Panel *panel;
Clock *clock;
int i, time_height, time_height_ink, date_height, date_height_ink;
init_precision();
// update clock to force update (-time_precision)
struct timeval stv;
gettimeofday(&stv, 0);
time_clock.tv_sec = stv.tv_sec - time_precision;
time_clock.tv_sec -= time_clock.tv_sec % time_precision;
}
for (i=0 ; i < nb_panel ; i++) {
panel = &panel1[i];
clock = &panel->clock;
if (!clock->area.on_screen) continue;
void init_clock_panel(void *p)
{
Panel *panel =(Panel*)p;
Clock *clock = &panel->clock;
int i, time_height, time_height_ink, date_height, date_height_ink;
clock->area.parent = panel;
clock->area.panel = panel;
clock->area._draw_foreground = draw_clock;
clock->area._resize = resize_clock;
clock->area.resize = 1;
clock->area.redraw = 1;
clock->area.parent = p;
clock->area.panel = p;
clock->area._draw_foreground = draw_clock;
clock->area._resize = resize_clock;
clock->area.resize = 1;
clock->area.redraw = 1;
strftime(buf_time, sizeof(buf_time), time1_format, localtime(&time_clock.tv_sec));
get_text_size(time1_font_desc, &time_height_ink, &time_height, panel->area.height, buf_time, strlen(buf_time));
if (time2_format) {
strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date));
}
strftime(buf_time, sizeof(buf_time), time1_format, localtime(&time_clock.tv_sec));
get_text_size(time1_font_desc, &time_height_ink, &time_height, panel->area.height, buf_time, strlen(buf_time));
if (time2_format) {
strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date));
}
if (panel_horizontal) {
// panel horizonal => fixed height and posy
clock->area.posy = panel->area.pix.border.width + panel->area.paddingy;
clock->area.height = panel->area.height - (2 * clock->area.posy);
}
else {
// panel vertical => fixed width, height, posy and posx
clock->area.posy = panel->area.pix.border.width + panel->area.paddingxlr;
clock->area.height = (2 * clock->area.paddingxlr) + (time_height + date_height);
clock->area.posx = panel->area.pix.border.width + panel->area.paddingy;
clock->area.width = panel->area.width - (2 * panel->area.pix.border.width) - (2 * panel->area.paddingy);
}
if (panel_horizontal) {
// panel horizonal => fixed height and posy
clock->area.posy = panel->area.pix.border.width + panel->area.paddingy;
clock->area.height = panel->area.height - (2 * clock->area.posy);
}
else {
// panel vertical => fixed width, height, posy and posx
clock->area.posy = panel->area.pix.border.width + panel->area.paddingxlr;
clock->area.height = (2 * clock->area.paddingxlr) + (time_height + date_height);
clock->area.posx = panel->area.pix.border.width + panel->area.paddingy;
clock->area.width = panel->area.width - (2 * panel->area.pix.border.width) - (2 * panel->area.paddingy);
}
clock->time1_posy = (clock->area.height - time_height) / 2;
if (time2_format) {
strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date));
clock->time1_posy = (clock->area.height - time_height) / 2;
if (time2_format) {
strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date));
clock->time1_posy -= ((date_height_ink + 2) / 2);
clock->time2_posy = clock->time1_posy + time_height + 2 - (time_height - time_height_ink)/2 - (date_height - date_height_ink)/2;
}
clock->time1_posy -= ((date_height_ink + 2) / 2);
clock->time2_posy = clock->time1_posy + time_height + 2 - (time_height - time_height_ink)/2 - (date_height - date_height_ink)/2;
}
}

View file

@ -36,6 +36,7 @@ extern char *clock_rclick_command;
// initialize clock : y position, precision, ...
void init_clock();
void init_clock_panel(void *panel);
void init_precision();
void draw_clock (void *obj, cairo_t *c, int active);

View file

@ -36,11 +36,11 @@
#include "common.h"
#include "server.h"
#include "panel.h"
#include "task.h"
#include "taskbar.h"
#include "systraybar.h"
#include "clock.h"
#include "panel.h"
#include "config.h"
#include "window.h"
#include "tooltip.h"
@ -748,19 +748,15 @@ void config_finish ()
// TODO: user can configure layout => ordered objects in panel.area.list
// clock and systray before taskbar because resize(clock) can resize others object ??
init_panel();
init_tooltip();
init_clock();
#ifdef ENABLE_BATTERY
init_battery();
#endif
init_systray();
init_taskbar();
init_tooltip();
visible_object();
init_panel();
cleanup_config();
task_refresh_tasklist();
}

View file

@ -20,3 +20,4 @@ void config_finish ();
void save_config ();
#endif

View file

@ -59,8 +59,8 @@ int max_tick_urgent;
// panel's initial config
Panel panel_config;
// panels (one panel per monitor)
Panel *panel1 = 0;
int nb_panel;
Panel *panel1 = NULL;
int nb_panel = 0;
Imlib_Image default_icon = NULL;
@ -68,20 +68,40 @@ Imlib_Image default_icon = NULL;
void init_panel()
{
int i;
Panel *p;
int i, old_nb_panel;
Panel *new_panel, *p;
cleanup_taskbar();
for (i=0 ; i < nb_panel ; i++) {
free_area(&panel1[i].area);
}
// alloc panels (one monitor or all monitors)
old_nb_panel = nb_panel;
if (panel_config.monitor >= 0)
nb_panel = 1;
else
nb_panel = server.nb_monitor;
panel1 = malloc(nb_panel * sizeof(Panel));
fprintf(stderr, "tint2 : nb monitor %d, nb desktop %d\n", nb_panel, server.nb_desktop);
/* if (nb_panel < old_nb_panel) {
// freed old panels
for (i=nb_panel ; i < old_nb_panel ; i++) {
}
}*/
if (nb_panel != old_nb_panel)
new_panel = realloc(panel1, nb_panel * sizeof(Panel));
else
new_panel = panel1;
for (i=0 ; i < nb_panel ; i++) {
p = &panel1[i];
p = &new_panel[i];
memcpy(p, &panel_config, sizeof(Panel));
if (i >= old_nb_panel) {
// new panel
memcpy(p, &panel_config, sizeof(Panel));
}
p->monitor = i;
p->area.parent = p;
p->area.panel = p;
@ -91,17 +111,24 @@ void init_panel()
p->g_taskbar.parent = p;
p->g_taskbar.panel = p;
p->g_task.area.panel = p;
init_panel_size_and_position(p);
// add childs
if (p->clock.area.on_screen)
if (p->clock.area.on_screen) {
init_clock_panel(p);
p->area.list = g_slist_append(p->area.list, &p->clock);
}
#ifdef ENABLE_BATTERY
if (p->battery.area.on_screen)
if (p->battery.area.on_screen) {
init_battery_panel(p);
p->area.list = g_slist_append(p->area.list, &p->battery);
}
#endif
// systray only on first panel
if (systray.area.on_screen && i == 0)
if (systray.area.on_screen && i == 0) {
init_systray_panel(p);
p->area.list = g_slist_append(p->area.list, &systray);
}
// full width mode
if (!p->initial_width) {
@ -109,22 +136,35 @@ void init_panel()
p->pourcentx = 1;
}
init_panel_size_and_position(p);
if (i >= old_nb_panel) {
// new panel
//printf("new window\n");
// Catch some events
long event_mask = ExposureMask|ButtonPressMask|ButtonReleaseMask;
if (g_tooltip.enabled)
event_mask |= PointerMotionMask|LeaveWindowMask;
XSetWindowAttributes att = { ParentRelative, 0L, 0, 0L, 0, 0, Always, 0L, 0L, False, event_mask, NoEventMask, False, 0, 0 };
if (p->main_win) XDestroyWindow(server.dsp, p->main_win);
p->main_win = XCreateWindow(server.dsp, server.root_win, p->posx, p->posy, p->area.width, p->area.height, 0, server.depth, InputOutput, CopyFromParent, CWEventMask, &att);
// Catch some events
long event_mask = ExposureMask|ButtonPressMask|ButtonReleaseMask;
if (g_tooltip.enabled)
event_mask |= PointerMotionMask|LeaveWindowMask;
XSetWindowAttributes att = { ParentRelative, 0L, 0, 0L, 0, 0, Always, 0L, 0L, False, event_mask, NoEventMask, False, 0, 0 };
if (p->main_win) XDestroyWindow(server.dsp, p->main_win);
p->main_win = XCreateWindow(server.dsp, server.root_win, p->posx, p->posy, p->area.width, p->area.height, 0, server.depth, InputOutput, CopyFromParent, CWEventMask, &att);
set_panel_properties(p);
set_panel_background(p);
XMapWindow (server.dsp, p->main_win);
set_panel_properties(p);
set_panel_background(p);
XMapWindow (server.dsp, p->main_win);
}
else {
// old panel
//printf("move old window\n");
XMoveResizeWindow(server.dsp, p->main_win, p->posx, p->posy, p->area.width, p->area.height);
set_panel_background(p);
}
}
panel1 = new_panel;
panel_refresh = 1;
init_taskbar();
visible_object();
task_refresh_tasklist();
}

View file

@ -105,7 +105,10 @@ extern Panel panel_config;
extern Panel *panel1;
extern int nb_panel;
// realloc panels according to number of monitor
// it's used on initial config and when new monitor added
void init_panel();
void init_panel_size_and_position(Panel *panel);
void cleanup_panel();
void resize_panel(void *obj);

View file

@ -290,7 +290,6 @@ void get_desktops()
server.nb_desktop = 1;
fprintf(stderr, "warning : WM doesn't respect NETWM specs. tint2 default to 1 desktop.\n");
}
fprintf(stderr, "tint2 : nb monitor %d, nb desktop %d\n", server.nb_monitor, server.nb_desktop);
}

View file

@ -47,24 +47,24 @@ int refresh_systray;
void init_systray()
{
Panel *panel = &panel1[0];
if (systray.area.on_screen)
systray.area.on_screen = init_net();
if (!systray.area.on_screen)
return;
systray.area.parent = panel;
systray.area.panel = panel;
systray.area._draw_foreground = draw_systray;
systray.area._resize = resize_systray;
systray.area.resize = 1;
systray.area.redraw = 1;
refresh_systray = 0;
}
void init_systray_panel(void *p)
{
Panel *panel =(Panel*)p;
// configure systray
// draw only one systray (even with multi panel)
if (panel_horizontal) {
systray.area.posy = panel->area.pix.border.width + panel->area.paddingy;
systray.area.height = panel->area.height - (2 * systray.area.posy);
@ -73,6 +73,8 @@ void init_systray()
systray.area.posx = panel->area.pix.border.width + panel->area.paddingy;
systray.area.width = panel->area.width - (2 * panel->area.pix.border.width) - (2 * panel->area.paddingy);
}
systray.area.parent = p;
systray.area.panel = p;
}

View file

@ -44,6 +44,7 @@ extern int refresh_systray;
void init_systray();
void init_systray_panel(void *p);
void cleanup_systray();
void draw_systray(void *obj, cairo_t *c, int active);
void resize_systray(void *obj);

View file

@ -636,33 +636,8 @@ void event_configure_notify (Window win)
{
// change in root window (xrandr)
if (win == server.root_win) {
int i, old_nb_panel = nb_panel;
get_monitors();
if (panel_config.monitor >= 0)
nb_panel = 1;
else
nb_panel = server.nb_monitor;
if (old_nb_panel != nb_panel) {
// changed number of panel
printf("changed number of panel\n");
//realloc(panel1, nb_panel * sizeof(Panel));
}
for (i=0 ; i < nb_panel && i < old_nb_panel ; i++) {
Panel *panel = &panel1[i];
init_panel_size_and_position(panel);
XMoveResizeWindow(server.dsp, panel->main_win, panel->posx, panel->posy, panel->area.width, panel->area.height);
set_panel_background(panel);
// force the resize of childs
GSList *l0;
panel->area.resize = 1;
for (l0 = panel->area.list; l0 ; l0 = l0->next)
((Area*)l0->data)->resize = 1;
}
panel_refresh = 1;
init_panel();
return;
}