1 /***************************************************************************
2 * Copyright (C) 2003 by Sébastien Laoût *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include <kapplication.h>
22 #include <qdesktopwidget.h>
23 #include <q3popupmenu.h>
26 #include "popupmenu.h"
28 #if KDE_IS_VERSION( 3, 2, 90 ) // KDE 3.3.x
29 #define MENU_Y_OFFSET 1
31 #define MENU_Y_OFFSET 2
34 /** NOTE 1 : This implementation forget BIDI support (eg RightToLeft languages
35 * expect to try to popup the menu first at bottom-right
36 * and not at bottom-left.
37 * NOTE 2 : This implementation do not support virtual desktop with more than
38 * one screen. Pehrapse QPopupMenu solve it by itself but I can't
39 * try : I have only one screen.
40 * => Have those methods directly in Qt (or KDE) would be a great benefits !
43 void PopupMenu::execAtRectCenter(Q3PopupMenu
&menu
, const QRect
&rect
)
45 // Compute point where to popup the menu that should be centered :
46 QSize menuSize
= menu
.sizeHint();
47 QSize menuHalfSize
= menuSize
/ 2;
48 QPoint point
= rect
.center() - QPoint(menuHalfSize
.width(), menuHalfSize
.height());
50 // Very strange : menu.exec(point) do that clipping (but not when exec() by mouse !!! ) :
51 // If menu is partially out of the screen, move it :
52 /* int desktopWidth = kapp->desktop()->width();
53 int desktopHeight = kapp->desktop()->height();
54 if (point.x() + menuSize.width() > desktopWidth) point.setX(desktopWidth - menuSize.width());
55 if (point.y() + menuSize.height() - 2 > desktopHeight) point.setY(desktopHeight - menuSize.height() + 2);
56 if (point.x() < 0) point.setX(0);
57 if (point.y() < 0) point.setY(0);*/
59 // And show the menu :
60 menu
.exec( point
+ QPoint(0, MENU_Y_OFFSET
) ); // Stupid offset (Qt bug ?) : we should show the menus 2 pixels more bottom !
63 // Needed on debug to draw the passed global rectangle :
64 //#include <qpainter.h>
67 void PopupMenu::execAtRectBottom(Q3PopupMenu
&menu
, const QRect
&rect
, bool centered
)
69 QSize menuSize
= menu
.sizeHint() - QSize(1, 1); // A size is [1..n] => We want two lengths that are [0..(n-1)]
70 int desktopWidth
= kapp
->desktop()->width(); // to be compared/added/substracted with QRects/QPoints...
71 int desktopHeight
= kapp
->desktop()->height();
73 /** Paint the rect on the screen (desktop).
74 * For test purpose only (to be sure the passed rectangle is right).
75 * Comment this (and the qpainter and qpen includes) for a non-debug version.
77 /*QPainter paint(kapp->desktop(), kapp->desktop(), true);
78 paint.setPen( QPen(Qt::black, 1) );
82 // rect.bottomLeft() and rect.bottomRight() must be VISIBLE :
83 // show the menu 1 pixel more BOTTOM (add 1 in Y) :
84 QPoint point
= rect
.bottomLeft() + QPoint(0, 1);
85 if (point
.y() + menuSize
.height() < desktopHeight
) { // First try at bottom
87 point
= QPoint( rect
.center().x() - menuSize
.width() / 2, point
.y() );
88 else if (point
.x() + menuSize
.width() < desktopWidth
) // Then, try at bottom-left
89 /*point is already set*/;
90 else // Overwise, at bottom-right
91 point
= rect
.bottomRight() - QPoint(menuSize
.width(), - 1);
92 // Idem : rect.topLeft() and rect.topRight() must be VISIBLE :
93 // show the menu 1 pixel more TOP (substract 1 in Y) :
94 } else { // Overwize, try at top
96 point
= QPoint( rect
.center().x() - menuSize
.width() / 2, rect
.top() - menuSize
.height() - 1 );
97 else if (point
.x() + menuSize
.width() < desktopWidth
) // Then, try at top-left
98 point
= rect
.topLeft() - QPoint(0, menuSize
.height() + 1);
99 else // Overwise, at top-right
100 point
= rect
.topRight() - QPoint(menuSize
.width(), menuSize
.height() + 1);
103 // No need to clip : it will be done by menu.exec(...)
105 // And show the menu :
106 menu
.exec( point
+ QPoint(0, MENU_Y_OFFSET
) ); // Stupid offset (Qt bug ?) : we should show the menus 2 pixels more bottom !
109 void PopupMenu::execAtRectRight(Q3PopupMenu
&menu
, const QRect
&rect
, bool centered
)
111 QSize menuSize
= menu
.sizeHint() - QSize(1, 1); // A size is [1..n] => We want two lengths that are [0..(n-1)]
112 int desktopWidth
= kapp
->desktop()->width(); // to be compared/added/substracted with QRects/QPoints...
113 int desktopHeight
= kapp
->desktop()->height();
115 /** Paint the rect on the screen (desktop).
116 * For test purpose only (to be sure the passed rectangle is right).
117 * Comment this (and the qpainter and qpen includes) for a non-debug version.
119 /*QPainter paint(kapp->desktop(), kapp->desktop(), true);
120 paint.setPen( QPen(Qt::black, 1) );
121 paint.drawRect(rect);
124 // rect.topRight() and rect.topLeft() must be VISIBLE :
125 // show the menu 1 pixel more RIGHT (add 1 in X) :
126 QPoint point
= rect
.topRight() + QPoint(1, 0);
127 if (point
.x() + menuSize
.width() < desktopWidth
) { // First try at right
129 point
= QPoint( point
.x(), rect
.center().y() - menuSize
.height() / 2 );
130 else if (point
.y() + menuSize
.height() < desktopHeight
) // Then, try at top-right
131 /*point is already set*/;
132 else // Overwise, at top-left
133 point
= rect
.bottomRight() - QPoint(-1, menuSize
.height());
134 // Idem : rect.topLeft() and rect.bottomLeft() must be VISIBLE :
135 // show the menu 1 pixel more LEFT (substract 1 in X) :
136 } else { // Overwize, try at top
138 point
= QPoint( rect
.left() - menuSize
.width() - 1, rect
.center().y() - menuSize
.height() / 2 );
139 else if (point
.y() + menuSize
.height() < desktopHeight
) // Then, try at top-left
140 point
= rect
.topLeft() - QPoint(menuSize
.width() + 1, 0);
141 else // Overwise, at bottom-left
142 point
= rect
.bottomLeft() - QPoint(menuSize
.width() + 1, menuSize
.height());
145 // No need to clip : it will be done by menu.exec(...)
147 // And show the menu :
148 menu
.exec( point
+ QPoint(0, MENU_Y_OFFSET
) ); // Stupid offset (Qt bug ?) : we should show the menus 2 pixels more bottom !
151 // # i n c l u d e " p o p u p m e n u . m o c " // Comment this if you don't compile PopupMenuTest class