20 #include <tqpainter.h>
22 #include <tqfontmetrics.h>
26 #include "tdepopupmenu.h"
29 #include <tdeapplication.h>
32 : TQWidget(parent, name)
34 setMinimumSize(16, fontMetrics().height()+8);
38 const TQColor &,
const TQColor &,
39 TQWidget *parent,
const char *name)
40 : TQWidget(parent, name)
46 const TQColor &, TQWidget *parent,
48 : TQWidget(parent, name)
59 miniicon.resize(0, 0);
76 void TDEPopupTitle::calcSize()
80 int w = miniicon.width()+TQFontMetrics(f).width(titleStr);
81 int h = TQMAX( fontMetrics().height(), miniicon.height() );
82 setMinimumSize( w+16, h+8 );
85 void TDEPopupTitle::paintEvent(TQPaintEvent *)
89 kapp->style().drawPrimitive(TQStyle::PE_HeaderSectionMenu, &p, r, palette().active());
91 if (!miniicon.isNull())
92 p.drawPixmap(4, (r.height()-miniicon.height())/2, miniicon);
94 if (!titleStr.isNull())
96 p.setPen(palette().active().text());
100 if(!miniicon.isNull())
102 p.drawText(miniicon.width()+8, 0, width()-(miniicon.width()+8),
103 height(), AlignLeft | AlignVCenter | SingleLine,
108 p.drawText(0, 0, width(), height(),
109 AlignCenter | SingleLine, titleStr);
114 TQSize TDEPopupTitle::sizeHint()
const
116 return minimumSize();
119 class TDEPopupMenu::TDEPopupMenuPrivate
122 TDEPopupMenuPrivate ()
127 ,
state(TQt::NoButton)
131 ~TDEPopupMenuPrivate ()
136 TQString m_lastTitle;
146 TQString originalText;
149 TQt::ButtonState
state;
152 TQPopupMenu* m_ctxMenu;
153 static bool s_continueCtxMenuShow;
154 static int s_highlightedItem;
158 int TDEPopupMenu::TDEPopupMenuPrivate::s_highlightedItem(-1);
159 TDEPopupMenu* TDEPopupMenu::TDEPopupMenuPrivate::s_contextedMenu(0);
160 bool TDEPopupMenu::TDEPopupMenuPrivate::s_continueCtxMenuShow(
true);
163 : TQPopupMenu(parent, name)
165 d =
new TDEPopupMenuPrivate;
172 if (TDEPopupMenuPrivate::s_contextedMenu ==
this)
174 TDEPopupMenuPrivate::s_contextedMenu = 0;
175 TDEPopupMenuPrivate::s_highlightedItem = -1;
185 int ret = insertItem(titleItem,
id, index);
186 setItemEnabled(ret,
false);
195 int ret = insertItem(titleItem,
id, index);
196 setItemEnabled(ret,
false);
202 TQMenuItem *item = findItem(
id);
208 kdWarning() <<
"TDEPopupMenu: changeTitle() called with non-title id "<<
id <<
endl;
213 kdWarning() <<
"TDEPopupMenu: changeTitle() called with invalid id " <<
id <<
endl;
219 TQMenuItem *item = findItem(
id);
225 kdWarning() <<
"TDEPopupMenu: changeTitle() called with non-title id "<<
id <<
endl;
230 kdWarning() <<
"TDEPopupMenu: changeTitle() called with invalid id " <<
id <<
endl;
237 return d->m_lastTitle;
238 TQMenuItem *item = findItem(
id);
243 tqWarning(
"TDEPopupMenu: title() called with non-title id %d.",
id);
246 tqWarning(
"TDEPopupMenu: title() called with invalid id %d.",
id);
247 return TQString::null;
252 TQMenuItem *item = findItem(
id);
257 tqWarning(
"TDEPopupMenu: titlePixmap() called with non-title id %d.",
id);
260 tqWarning(
"TDEPopupMenu: titlePixmap() called with invalid id %d.",
id);
272 TQPopupMenu::closeEvent(e);
277 d->state = TQt::NoButton;
278 TQPopupMenu::activateItemAt(index);
286 void TDEPopupMenu::keyPressEvent(TQKeyEvent* e)
288 d->state = TQt::NoButton;
292 d->state = e->state();
293 TQPopupMenu::keyPressEvent(e);
298 bool firstpass =
true;
299 TQString keyString = e->text();
303 if (key == Key_Escape || key == Key_Return || key == Key_Enter
304 || key == Key_Up || key == Key_Down || key == Key_Left
305 || key == Key_Right || key == Key_F1) {
310 d->state = e->state();
311 TQPopupMenu::keyPressEvent(e);
313 }
else if ( key == Key_Shift || key == Key_Control || key == Key_Alt || key == Key_Meta )
314 return TQPopupMenu::keyPressEvent(e);
318 if (!d->keySeq.isNull()) {
320 if (key == Key_Backspace) {
322 if (d->keySeq.length() == 1) {
328 keyString = d->keySeq.left(d->keySeq.length() - 1);
333 }
else if (key == Key_Delete) {
340 }
else if (d->noMatches) {
352 }
else if (key == Key_Backspace && parentMenu) {
359 d->keySeq += keyString;
360 int seqLen = d->keySeq.length();
362 for (; i < (int)count(); i++) {
367 if (!isItemEnabled(j))
374 if (i == d->lastHitIndex)
375 thisText = d->originalText;
380 if ((
int)accel(j) != 0)
381 thisText = thisText.replace(
"&", TQString());
384 thisText = thisText.left(seqLen);
387 if (!thisText.find(d->keySeq, 0,
false)) {
394 if (d->lastHitIndex != i)
396 changeItem(idAt(d->lastHitIndex), d->originalText);
399 if (d->lastHitIndex != i || d->lastHitIndex == -1)
400 d->originalText = text(j);
403 changeItem(j,
underlineText(d->originalText, d->keySeq.length()));
409 d->clearTimer.start(5000,
true);
428 }
else if (findItem(idAt(d->lastHitIndex)) &&
429 findItem(idAt(d->lastHitIndex))->popup()) {
441 TQPopupMenu::keyPressEvent(e);
444 bool TDEPopupMenu::focusNextPrevChild(
bool next )
447 return TQPopupMenu::focusNextPrevChild( next );
453 for (uint i = 0; i < length; i++) {
455 ret.insert(2*i,
"&");
463 if (d->lastHitIndex != -1) {
464 changeItem(idAt(d->lastHitIndex), d->originalText);
465 d->lastHitIndex = -1;
469 d->keySeq = TQString::null;
472 d->noMatches = noMatches;
477 d->shortcuts = enable;
482 d->autoExec = enable;
494 if (d->m_ctxMenu && d->m_ctxMenu->isVisible())
497 d->m_ctxMenu->hide();
500 TQPopupMenu::mousePressEvent(e);
506 d->state = TQt::ButtonState(e->button() | (e->state() & KeyButtonMask));
508 if ( !d->m_ctxMenu || !d->m_ctxMenu->isVisible() )
509 TQPopupMenu::mouseReleaseEvent(e);
516 d->m_ctxMenu =
new TQPopupMenu(
this);
517 connect(d->m_ctxMenu, TQ_SIGNAL(aboutToHide()),
this, TQ_SLOT(ctxMenuHiding()));
530 TDEPopupMenuPrivate::s_continueCtxMenuShow =
false;
535 return TDEPopupMenuPrivate::s_highlightedItem;
540 return TDEPopupMenuPrivate::s_contextedMenu;
543 void TDEPopupMenu::itemHighlighted(
int )
545 if (!d->m_ctxMenu || !d->m_ctxMenu->isVisible())
550 d->m_ctxMenu->hide();
551 showCtxMenu(mapFromGlobal(TQCursor::pos()));
554 void TDEPopupMenu::showCtxMenu(TQPoint pos)
556 TQMenuItem* item = findItem(TDEPopupMenuPrivate::s_highlightedItem);
559 TQPopupMenu* subMenu = item->popup();
562 disconnect(subMenu, TQ_SIGNAL(aboutToShow()),
this, TQ_SLOT(ctxMenuHideShowingMenu()));
566 TDEPopupMenuPrivate::s_highlightedItem = idAt(pos);
568 if (TDEPopupMenuPrivate::s_highlightedItem == -1)
570 TDEPopupMenuPrivate::s_contextedMenu = 0;
576 TQPopupMenu* subMenu = findItem(TDEPopupMenuPrivate::s_highlightedItem)->popup();
579 connect(subMenu, TQ_SIGNAL(aboutToShow()), TQ_SLOT(ctxMenuHideShowingMenu()));
580 TQTimer::singleShot(100, subMenu, TQ_SLOT(hide()));
583 if (!TDEPopupMenuPrivate::s_continueCtxMenuShow)
585 TDEPopupMenuPrivate::s_continueCtxMenuShow =
true;
589 TDEPopupMenuPrivate::s_contextedMenu =
this;
590 d->m_ctxMenu->popup(this->mapToGlobal(pos));
591 connect(
this, TQ_SIGNAL(highlighted(
int)),
this, TQ_SLOT(itemHighlighted(
int)));
598 void TDEPopupMenu::ctxMenuHideShowingMenu()
600 TQMenuItem* item = findItem(TDEPopupMenuPrivate::s_highlightedItem);
603 TQPopupMenu* subMenu = item->popup();
606 TQTimer::singleShot(0, subMenu, TQ_SLOT(hide()));
611 void TDEPopupMenu::ctxMenuHiding()
613 if (TDEPopupMenuPrivate::s_highlightedItem)
615 TQPopupMenu* subMenu = findItem(TDEPopupMenuPrivate::s_highlightedItem)->popup();
618 disconnect(subMenu, TQ_SIGNAL(aboutToShow()),
this, TQ_SLOT(ctxMenuHideShowingMenu()));
622 disconnect(
this, TQ_SIGNAL(highlighted(
int)),
this, TQ_SLOT(itemHighlighted(
int)));
623 TDEPopupMenuPrivate::s_continueCtxMenuShow =
true;
626 void TDEPopupMenu::contextMenuEvent(TQContextMenuEvent* e)
630 if (e->reason() == TQContextMenuEvent::Mouse)
632 showCtxMenu(e->pos());
634 else if (actItem != -1)
636 showCtxMenu(itemGeometry(actItem).center());
643 TQPopupMenu::contextMenuEvent(e);
646 void TDEPopupMenu::hideEvent(TQHideEvent*)
648 if (d->m_ctxMenu && d->m_ctxMenu->isVisible())
659 d->m_ctxMenu->hide();
669 : TQPopupMenu(parent, name)
671 d =
new TDEPopupMenuPrivate;
680 insertItem(titleItem);
681 d->m_lastTitle =
title;
684 void TDEPopupTitle::virtual_hook(
int,
void* )
687 void TDEPopupMenu::virtual_hook(
int,
void* )
690 #include "tdepopupmenu.moc"
kndbgstream & endl(kndbgstream &s)
kdbgstream kdWarning(int area=0)