13 #include "placement.h"
19 #include "workspace.h"
25 namespace KWinInternal
30 Placement::Placement(Workspace* w)
40 void Placement::place(Client* c, TQRect& area )
42 Policy policy = c->rules()->checkPlacement( Default );
43 if( policy != Default )
45 place( c, area, policy );
50 placeUtility(c, area, options->placement );
51 else if( c->isDialog())
52 placeDialog(c, area, options->placement );
53 else if( c->isSplash())
54 placeOnMainWindow( c, area );
56 place(c, area, options->placement);
59 void Placement::place(Client* c, TQRect& area, Policy policy, Policy nextPlacement )
61 if( policy == Unknown )
63 if( policy == Default )
64 policy = options->placement;
65 if( policy == NoPlacement )
67 else if (policy == Random)
68 placeAtRandom(c, area, nextPlacement);
69 else if (policy == Cascade)
70 placeCascaded(c, area, nextPlacement);
71 else if (policy == Centered)
72 placeCentered(c, area, nextPlacement);
73 else if (policy == ZeroCornered)
74 placeZeroCornered(c, area, nextPlacement);
75 else if (policy == UnderMouse)
76 placeUnderMouse(c, area, nextPlacement);
77 else if (policy == OnMainWindow)
78 placeOnMainWindow(c, area, nextPlacement);
79 else if( policy == Maximizing )
80 placeMaximizing(c, area, nextPlacement);
82 placeSmart(c, area, nextPlacement);
88 void Placement::placeAtRandom(Client* c,
const TQRect& area, Policy )
92 static int py = 2 * step;
95 const TQRect maxRect = checkArea( c, area );
105 if (px > maxRect.width()/2)
106 px = maxRect.x() + step;
107 if (py > maxRect.height()/2)
108 py = maxRect.y() + step;
111 if (tx + c->width() > maxRect.right())
113 tx = maxRect.right() - c->width();
118 if (ty + c->height() > maxRect.bottom())
120 ty = maxRect.bottom() - c->height();
131 void Placement::placeSmart(Client* c,
const TQRect& area, Policy )
142 const int none = 0, h_wrong = -1, w_wrong = -2;
143 long int overlap, min_overlap = 0;
144 int x_optimal, y_optimal;
146 int desktop = c->desktop() == 0 || c->isOnAllDesktops() ? m_WorkspacePtr->currentDesktop() : c->desktop();
148 int cxl, cxr, cyt, cyb;
153 const TQRect maxRect = checkArea( c, area );
154 int x = maxRect.left(), y = maxRect.top();
155 x_optimal = x; y_optimal = y;
158 int ch = c->height() - 1;
159 int cw = c->width() - 1;
161 bool first_pass =
true;
167 if (y + ch > maxRect.bottom() && ch < maxRect.height())
169 else if(x + cw > maxRect.right())
175 cxl = x; cxr = x + cw;
176 cyt = y; cyb = y + ch;
177 ClientList::ConstIterator l;
178 for(l = m_WorkspacePtr->stackingOrder().begin(); l != m_WorkspacePtr->stackingOrder().end() ; ++l)
180 if((*l)->isOnDesktop(desktop) &&
181 (*l)->isShown(
false ) && (*l) != c)
184 xl = (*l)->x(); yt = (*l)->y();
185 xr = xl + (*l)->width(); yb = yt + (*l)->height();
188 if((cxl < xr) && (cxr > xl) &&
189 (cyt < yb) && (cyb > yt))
191 xl = QMAX(cxl, xl); xr = QMIN(cxr, xr);
192 yt = QMAX(cyt, yt); yb = QMIN(cyb, yb);
193 if((*l)->keepAbove())
194 overlap += 16 * (xr - xl) * (yb - yt);
195 else if((*l)->keepBelow() && !(*l)->isDock())
198 overlap += (xr - xl) * (yb - yt);
215 min_overlap = overlap;
218 else if (overlap >= none && overlap < min_overlap)
220 min_overlap = overlap;
229 possible = maxRect.right();
230 if (possible - cw > x) possible -= cw;
233 ClientList::ConstIterator l;
234 for(l = m_WorkspacePtr->stackingOrder().begin(); l != m_WorkspacePtr->stackingOrder().end() ; ++l)
237 if ((*l)->isOnDesktop(desktop) &&
238 (*l)->isShown(
false ) && (*l) != c)
241 xl = (*l)->x(); yt = (*l)->y();
242 xr = xl + (*l)->width(); yb = yt + (*l)->height();
246 if((y < yb) && (yt < ch + y))
249 if((xr > x) && (possible > xr)) possible = xr;
252 if((basket > x) && (possible > basket)) possible = basket;
260 else if (overlap == w_wrong)
263 possible = maxRect.bottom();
265 if (possible - ch > y) possible -= ch;
268 ClientList::ConstIterator l;
269 for(l = m_WorkspacePtr->stackingOrder().begin(); l != m_WorkspacePtr->stackingOrder().end() ; ++l)
271 if((*l)->isOnDesktop(desktop) &&
272 (*l) != c && c->isShown(
false ))
275 xl = (*l)->x(); yt = (*l)->y();
276 xr = xl + (*l)->width(); yb = yt + (*l)->height();
280 if((yb > y) && (possible > yb)) possible = yb;
283 if((basket > y) && (possible > basket)) possible = basket;
289 while((overlap != none) && (overlap != h_wrong) && (y < maxRect.bottom()));
291 if(ch>= maxRect.height())
292 y_optimal=maxRect.top();
295 c->move(x_optimal, y_optimal);
299 void Placement::reinitCascading(
int desktop )
304 for(
int i = 0; i < m_WorkspacePtr->numberOfDesktops(); i++)
306 DesktopCascadingInfo inf;
307 inf.pos = TQPoint(-1,-1);
315 cci[desktop - 1].pos = TQPoint(-1, -1);
316 cci[desktop - 1].col = cci[desktop - 1].row = 0;
323 void Placement::placeCascaded (Client* c, TQRect& area, Policy nextPlacement)
331 const int delta_x = 24;
332 const int delta_y = 24;
334 const int dn = c->desktop() == 0 || c->isOnAllDesktops() ? (m_WorkspacePtr->currentDesktop() - 1) : (c->desktop() - 1);
337 TQRect maxRect = checkArea( c, area );
340 const int ch = c->height();
341 const int cw = c->width();
342 const int X = maxRect.left();
343 const int Y = maxRect.top();
344 const int H = maxRect.height();
345 const int W = maxRect.width();
347 if( nextPlacement == Unknown )
348 nextPlacement = Smart;
351 if (cci[dn].pos.x() < 0 || cci[dn].pos.x() < X || cci[dn].pos.y() < Y )
353 cci[dn].pos = TQPoint(X, Y);
354 cci[dn].col = cci[dn].row = 0;
358 xp = cci[dn].pos.x();
359 yp = cci[dn].pos.y();
362 if ((yp + ch) > H) yp = Y;
367 place(c,area,nextPlacement);
373 if (cci[dn].pos.x() != X && cci[dn].pos.y() != Y)
382 if (xp != X && yp == Y)
385 xp = delta_x * cci[dn].col;
387 if (yp != Y && xp == X)
390 yp = delta_y * cci[dn].row;
394 if (((xp + cw) > W - X) || ((yp + ch) > H - Y))
396 place(c,area,nextPlacement);
402 c->move(TQPoint(xp, yp));
405 cci[dn].pos = TQPoint(xp + delta_x, yp + delta_y);
411 void Placement::placeCentered (Client* c,
const TQRect& area, Policy )
415 const TQRect maxRect = checkArea( c, area );
417 const int xp = maxRect.left() + (maxRect.width() - c->width()) / 2;
418 const int yp = maxRect.top() + (maxRect.height() - c->height()) / 2;
421 c->move(TQPoint(xp, yp));
427 void Placement::placeZeroCornered(Client* c,
const TQRect& area, Policy )
430 const TQRect maxRect = checkArea( c, area );
433 c->move(TQPoint(maxRect.left(), maxRect.top()));
436 void Placement::placeUtility(Client* c, TQRect& area, Policy )
443 place( c, area, Default );
447 void Placement::placeDialog(Client* c, TQRect& area, Policy nextPlacement )
449 placeOnMainWindow( c, area, nextPlacement );
452 void Placement::placeUnderMouse(Client* c, TQRect& area, Policy )
454 area = checkArea( c, area );
455 TQRect geom = c->geometry();
456 geom.moveCenter( TQCursor::pos());
457 c->move( geom.topLeft());
458 c->keepInArea( area );
461 void Placement::placeOnMainWindow(Client* c, TQRect& area, Policy nextPlacement )
463 if( nextPlacement == Unknown )
464 nextPlacement = Centered;
465 if( nextPlacement == Maximizing )
466 placeMaximizing( c, area, NoPlacement );
467 area = checkArea( c, area );
468 ClientList mainwindows = c->mainClients();
469 Client* place_on = NULL;
470 Client* place_on2 = NULL;
472 for( ClientList::ConstIterator it = mainwindows.begin();
473 it != mainwindows.end();
476 if( mainwindows.count() > 1 && (*it)->isSpecialWindow())
480 if( (*it)->isOnCurrentDesktop())
482 if( place_on == NULL )
491 place( c, area, Centered );
496 if( place_on == NULL )
498 if( mains_count != 1 )
500 place( c, area, Centered );
503 place_on = place_on2;
505 if( place_on->isDesktop())
507 place( c, area, Centered );
510 TQRect geom = c->geometry();
511 geom.moveCenter( place_on->geometry().center());
512 c->move( geom.topLeft());
514 area = checkArea( c, TQRect());
515 c->keepInArea( area );
518 void Placement::placeMaximizing(Client* c, TQRect& area, Policy nextPlacement )
520 if( nextPlacement == Unknown )
521 nextPlacement = Smart;
522 if( c->isMaximizable() && c->maxSize().width() >= area.width() && c->maxSize().height() >= area.height())
524 if( m_WorkspacePtr->clientArea( MaximizeArea, c ) == area )
525 c->maximize( Client::MaximizeFull );
528 c->setGeometry( area );
533 c->resizeWithChecks( c->maxSize().boundedTo( area.size()));
534 place( c, area, nextPlacement );
538 TQRect Placement::checkArea(
const Client* c,
const TQRect& area )
541 return m_WorkspacePtr->clientArea( PlacementArea, c->geometry().center(), c->desktop());
548 Placement::Policy Placement::policyFromString(
const TQString& policy,
bool no_special )
550 if( policy ==
"NoPlacement" )
552 else if( policy ==
"Default" && !no_special )
554 else if( policy ==
"Random" )
556 else if( policy ==
"Cascade" )
558 else if( policy ==
"Centered" )
560 else if( policy ==
"ZeroCornered" )
562 else if( policy ==
"UnderMouse" && !no_special)
564 else if( policy ==
"OnMainWindow" && !no_special)
566 else if( policy ==
"Maximizing" )
572 const char* Placement::policyToString( Policy policy )
574 const char*
const policies[] =
575 {
"NoPlacement",
"Default",
"XXX should never see",
"Random",
"Smart",
"Cascade",
"Centered",
576 "ZeroCornered",
"UnderMouse",
"OnMainWindow",
"Maximizing" };
577 assert( policy <
int(
sizeof( policies ) /
sizeof( policies[ 0 ] )));
578 return policies[ policy ];
591 void Workspace::slotWindowPackLeft()
593 if( active_client && active_client->isMovable())
594 active_client->move( packPositionLeft( active_client, active_client->geometry().left(), true ),
598 void Workspace::slotWindowPackRight()
600 if( active_client && active_client->isMovable())
602 packPositionRight( active_client, active_client->geometry().right(), true )
603 - active_client->width() + 1, active_client->y());
606 void Workspace::slotWindowPackUp()
608 if( active_client && active_client->isMovable())
609 active_client->move( active_client->x(),
610 packPositionUp( active_client, active_client->geometry().top(), true ));
613 void Workspace::slotWindowPackDown()
615 if( active_client && active_client->isMovable())
616 active_client->move( active_client->x(),
617 packPositionDown( active_client, active_client->geometry().bottom(), true ) - active_client->height() + 1 );
620 void Workspace::slotWindowGrowHorizontal()
623 active_client->growHorizontal();
626 void Client::growHorizontal()
630 TQRect geom = geometry();
631 geom.setRight( workspace()->packPositionRight(
this, geom.right(), true ));
632 TQSize adjsize =
adjustedSize( geom.size(), SizemodeFixedW );
633 if( geometry().size() == adjsize && geom.size() != adjsize && xSizeHint.width_inc > 1 )
635 int newright = workspace()->packPositionRight(
this, geom.right() + xSizeHint.width_inc - 1, true );
638 if( workspace()->clientArea( MovementArea,
639 TQPoint(( x() + newright ) / 2, geometry().center().y()),
desktop()).right() >= newright )
640 geom.setRight( newright );
642 geom.setSize(
adjustedSize( geom.size(), SizemodeFixedW ));
646 void Workspace::slotWindowShrinkHorizontal()
649 active_client->shrinkHorizontal();
652 void Client::shrinkHorizontal()
656 TQRect geom = geometry();
657 geom.setRight( workspace()->packPositionLeft(
this, geom.right(), false ));
658 if( geom.width() <= 1 )
660 geom.setSize(
adjustedSize( geom.size(), SizemodeFixedW ));
661 if( geom.width() > 20 )
665 void Workspace::slotWindowGrowVertical()
668 active_client->growVertical();
671 void Client::growVertical()
675 TQRect geom = geometry();
676 geom.setBottom( workspace()->packPositionDown(
this, geom.bottom(), true ));
677 TQSize adjsize =
adjustedSize( geom.size(), SizemodeFixedH );
678 if( geometry().size() == adjsize && geom.size() != adjsize && xSizeHint.height_inc > 1 )
680 int newbottom = workspace()->packPositionDown(
this, geom.bottom() + xSizeHint.height_inc - 1, true );
682 if( workspace()->clientArea( MovementArea,
683 TQPoint( geometry().center().x(), ( y() + newbottom ) / 2 ),
desktop()).bottom() >= newbottom )
684 geom.setBottom( newbottom );
686 geom.setSize(
adjustedSize( geom.size(), SizemodeFixedH ));
691 void Workspace::slotWindowShrinkVertical()
694 active_client->shrinkVertical();
697 void Client::shrinkVertical()
701 TQRect geom = geometry();
702 geom.setBottom( workspace()->packPositionUp(
this, geom.bottom(), false ));
703 if( geom.height() <= 1 )
705 geom.setSize(
adjustedSize( geom.size(), SizemodeFixedH ));
706 if( geom.height() > 20 )
710 int Workspace::packPositionLeft(
const Client* cl,
int oldx,
bool left_edge )
const
712 int newx = clientArea( MovementArea, cl ).left();
714 newx = clientArea( MovementArea,
715 TQPoint( cl->geometry().left() - 1, cl->geometry().center().y()), cl->desktop()).left();
718 for( ClientList::ConstIterator it = clients.begin();
722 if( !(*it)->isShown(
false ) || !(*it)->isOnDesktop( active_client->desktop()))
724 int x = left_edge ? (*it)->geometry().right() + 1 : (*it)->geometry().left() - 1;
725 if( x > newx && x < oldx
726 && !( cl->geometry().top() > (*it)->geometry().bottom()
727 || cl->geometry().bottom() < (*it)->geometry().top()))
733 int Workspace::packPositionRight(
const Client* cl,
int oldx,
bool right_edge )
const
735 int newx = clientArea( MovementArea, cl ).right();
737 newx = clientArea( MovementArea,
738 TQPoint( cl->geometry().right() + 1, cl->geometry().center().y()), cl->desktop()).right();
741 for( ClientList::ConstIterator it = clients.begin();
745 if( !(*it)->isShown(
false ) || !(*it)->isOnDesktop( cl->desktop()))
747 int x = right_edge ? (*it)->geometry().left() - 1 : (*it)->geometry().right() + 1;
748 if( x < newx && x > oldx
749 && !( cl->geometry().top() > (*it)->geometry().bottom()
750 || cl->geometry().bottom() < (*it)->geometry().top()))
756 int Workspace::packPositionUp(
const Client* cl,
int oldy,
bool top_edge )
const
758 int newy = clientArea( MovementArea, cl ).top();
760 newy = clientArea( MovementArea,
761 TQPoint( cl->geometry().center().x(), cl->geometry().top() - 1 ), cl->desktop()).top();
764 for( ClientList::ConstIterator it = clients.begin();
768 if( !(*it)->isShown(
false ) || !(*it)->isOnDesktop( cl->desktop()))
770 int y = top_edge ? (*it)->geometry().bottom() + 1 : (*it)->geometry().top() - 1;
771 if( y > newy && y < oldy
772 && !( cl->geometry().left() > (*it)->geometry().right()
773 || cl->geometry().right() < (*it)->geometry().left()))
779 int Workspace::packPositionDown(
const Client* cl,
int oldy,
bool bottom_edge )
const
781 int newy = clientArea( MovementArea, cl ).bottom();
783 newy = clientArea( MovementArea,
784 TQPoint( cl->geometry().center().x(), cl->geometry().bottom() + 1 ), cl->desktop()).bottom();
787 for( ClientList::ConstIterator it = clients.begin();
791 if( !(*it)->isShown(
false ) || !(*it)->isOnDesktop( cl->desktop()))
793 int y = bottom_edge ? (*it)->geometry().top() - 1 : (*it)->geometry().bottom() + 1;
794 if( y < newy && y > oldy
795 && !( cl->geometry().left() > (*it)->geometry().right()
796 || cl->geometry().right() < (*it)->geometry().left()))
805 void Workspace::place(Client* c, TQRect& area)
807 initPositioning->place( c, area );
810 void Workspace::placeSmart(Client* c,
const TQRect& area)
812 initPositioning->placeSmart( c, area );