/* placement.c - window and icon placement on screen
 * 
 *  WindowMaker window manager
 * 
 *  Copyright (c) 1997 Alfredo K. Kojima
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "wconfig.h"

#include <X11/Xlib.h>
#include <stdlib.h>
#include <stdio.h>

#include "generic/wwmlib.h"
#include "WindowMaker.h"
#include "wcore.h"
#include "icon.h"
#include "actions.h"
#include "funcs.h"
#include "application.h"
#include "appicon.h"
#include "dock.h"

extern WPreferences wPreferences;

/*
 * interactive window placement is in moveres.c
 */

extern void
InteractivePlaceWindow(WWindow *wwin, int *x_ret, int *y_ret);


void
PlaceIcon(WScreen *scr, int *x_ret, int *y_ret)
{
    int x1, y1, x2, y2;
    int icon_x, icon_y;
    int left_margin, right_margin;
    WWindow *wwin;
    WApplication *wapp;

    if (scr->dock) {
	if (scr->dock->on_right_side) {
	    left_margin = 0;
	    right_margin = scr->scr_width-ICON_WIDTH;
	} else {
	    left_margin = ICON_WIDTH;
	    right_margin = scr->scr_width;
	}
    } else {
	left_margin = 0;
	right_margin = scr->scr_width;
    }
    x1 = left_margin;
    y2 = scr->scr_height;
    y1 = y2-ICON_HEIGHT*2;    
    x2 = left_margin+ICON_WIDTH;

    while (1) {
	wwin = scr->focused_window;
	scr->icon_aux++;
	while (wwin) {
	    if (x2>=right_margin+ICON_WIDTH) {
		x1 = left_margin;
		x2 = left_margin+ICON_WIDTH*2;
		y1 -= ICON_HEIGHT;
		y2 -= ICON_HEIGHT;
		if (y2<ICON_HEIGHT) {
		    /* what's this guy doing!? */
		    *x_ret = 0;
		    *y_ret = 0;
		    return;
		}
		break;
	    }	    
	    /* arrange application icon */
	    wapp = wApplicationOf(wwin->main_window);
	    /* if there IS an icon and it's not yet arranged */
	    if (wapp && wapp->app_icon && wapp->icon_aux!=scr->icon_aux
		&& !wapp->app_icon->docked) {
		icon_x = wapp->app_icon->x_pos;
		icon_y = wapp->app_icon->y_pos;
		/* remember that we've already arranged this one */
		wapp->icon_aux = scr->icon_aux;
		/* don't go to next, as we still need to see if the window
		 * has a miniwindow */
	    } else if (wwin->flags.miniaturized) {
		icon_x = wwin->icon_x;
		icon_y = wwin->icon_y;
		/* go to next */
		wwin = wwin->prev;
	    } else {
		/* skip this one */
		wwin = wwin->prev;
		continue;
	    }
	    /* test if place is taken */
	    if (icon_y>y1 && icon_y<y2) {
		if (icon_x<x2 && icon_x>=x1) { 
		    x2 = icon_x+ICON_WIDTH*2;
		    x1 = icon_x+ICON_WIDTH;
		    /* this place can't be used */
		    wwin = scr->focused_window;
		    break;
		}
	    }
	}
	if (!wwin) {
	    /* found spot */
	    break;
	}
    }
    *x_ret = x1;
    *y_ret = y2-ICON_HEIGHT;
}



void
PlaceWindow(WWindow *wwin, int *x_ret, int *y_ret)
{
    WScreen *scr = wwin->screen_ptr;
    int h = scr->title_font.height+8;

    switch (wPreferences.window_placement) {
     case WPM_MANUAL:
	InteractivePlaceWindow(wwin, x_ret, y_ret);
	break;

     case WPM_CASCADE:
	scr->cascade_index++;
	*x_ret = h*scr->cascade_index + ICON_WIDTH;
	*y_ret = h*scr->cascade_index + ICON_WIDTH;
	if ((*x_ret > scr->scr_width/2)||(*y_ret > scr->scr_height/2))
	  scr->cascade_index = 0;
	break;
#ifdef DEBUG	
     default:
	puts("Invalid window placement!!!");
	*x_ret = 0;
	*y_ret = 0;
#endif
    }
}

