You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1130 lines
26 KiB
1130 lines
26 KiB
15 years ago
|
/**********************************************************************
|
||
|
** Copyright (C) 2003 Trolltech AS. All rights reserved.
|
||
|
**
|
||
14 years ago
|
** This file is part of TQt Designer.
|
||
15 years ago
|
**
|
||
|
** This file may be distributed and/or modified under the terms of the
|
||
|
** GNU General Public License version 2 as published by the Free Software
|
||
|
** Foundation and appearing in the file LICENSE.GPL included in the
|
||
|
** packaging of this file.
|
||
|
**
|
||
14 years ago
|
** Licensees holding valid TQt Enterprise Edition or TQt Professional Edition
|
||
|
** licenses may use this file in accordance with the TQt Commercial License
|
||
15 years ago
|
** Agreement provided with the Software.
|
||
|
**
|
||
|
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
||
|
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||
|
**
|
||
|
** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
||
|
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
||
14 years ago
|
** information about TQt Commercial License Agreements.
|
||
15 years ago
|
**
|
||
|
** Contact info@trolltech.com if any conditions of this licensing are
|
||
|
** not clear to you.
|
||
|
**
|
||
|
**********************************************************************/
|
||
|
|
||
15 years ago
|
#include <tqaction.h>
|
||
|
#include <tqapplication.h>
|
||
|
#include <tqbitmap.h>
|
||
|
#include <tqdragobject.h>
|
||
|
#include <tqlineedit.h>
|
||
|
#include <tqmainwindow.h>
|
||
|
#include <tqpainter.h>
|
||
|
#include <tqstyle.h>
|
||
15 years ago
|
#include "command.h"
|
||
|
#include "formwindow.h"
|
||
|
#include "menubareditor.h"
|
||
|
#include "popupmenueditor.h"
|
||
|
|
||
12 years ago
|
#include <tdelocale.h>
|
||
15 years ago
|
|
||
15 years ago
|
extern void find_accel( const TQString &txt, TQMap<TQChar, TQWidgetList > &accels, TQWidget *w );
|
||
15 years ago
|
|
||
|
// Drag Object Declaration -------------------------------------------
|
||
|
|
||
14 years ago
|
class MenuBarEditorItemPtrDrag : public TQStoredDrag
|
||
15 years ago
|
{
|
||
|
public:
|
||
|
MenuBarEditorItemPtrDrag( MenuBarEditorItem * item,
|
||
14 years ago
|
TQWidget * parent = 0,
|
||
15 years ago
|
const char * name = 0 );
|
||
|
~MenuBarEditorItemPtrDrag() {};
|
||
15 years ago
|
static bool canDecode( TQDragMoveEvent * e );
|
||
|
static bool decode( TQDropEvent * e, MenuBarEditorItem ** i );
|
||
15 years ago
|
};
|
||
|
|
||
|
// Drag Object Implementation ---------------------------------------
|
||
|
|
||
|
MenuBarEditorItemPtrDrag::MenuBarEditorItemPtrDrag( MenuBarEditorItem * item,
|
||
14 years ago
|
TQWidget * parent,
|
||
15 years ago
|
const char * name )
|
||
14 years ago
|
: TQStoredDrag( "qt/menubareditoritemptr", parent, name )
|
||
15 years ago
|
{
|
||
14 years ago
|
TQByteArray data( sizeof( TQ_LONG ) );
|
||
15 years ago
|
TQDataStream stream( data, IO_WriteOnly );
|
||
14 years ago
|
stream << ( TQ_LONG ) item;
|
||
15 years ago
|
setEncodedData( data );
|
||
|
}
|
||
|
|
||
15 years ago
|
bool MenuBarEditorItemPtrDrag::canDecode( TQDragMoveEvent * e )
|
||
15 years ago
|
{
|
||
|
return e->provides( "qt/menubareditoritemptr" );
|
||
|
}
|
||
|
|
||
15 years ago
|
bool MenuBarEditorItemPtrDrag::decode( TQDropEvent * e, MenuBarEditorItem ** i )
|
||
15 years ago
|
{
|
||
13 years ago
|
TQByteArray data = e->encodedData( "qt/menubareditoritemptr" );
|
||
15 years ago
|
TQDataStream stream( data, IO_ReadOnly );
|
||
15 years ago
|
|
||
|
if ( !data.size() )
|
||
|
return FALSE;
|
||
|
|
||
14 years ago
|
TQ_LONG p = 0;
|
||
15 years ago
|
stream >> p;
|
||
|
*i = ( MenuBarEditorItem *) p;
|
||
|
|
||
|
return TRUE;
|
||
|
}
|
||
|
|
||
|
// MenuBarEditorItem ---------------------------------------------------
|
||
|
|
||
14 years ago
|
MenuBarEditorItem::MenuBarEditorItem( MenuBarEditor * bar, TQObject * parent, const char * name )
|
||
|
: TQObject( parent, name ),
|
||
15 years ago
|
menuBar( bar ),
|
||
|
popupMenu( 0 ),
|
||
|
visible( TRUE ),
|
||
|
separator( FALSE ),
|
||
|
removable( FALSE )
|
||
|
{ }
|
||
|
|
||
|
MenuBarEditorItem::MenuBarEditorItem( PopupMenuEditor * menu, MenuBarEditor * bar,
|
||
14 years ago
|
TQObject * parent, const char * name )
|
||
|
: TQObject( parent, name ),
|
||
15 years ago
|
menuBar( bar ),
|
||
|
popupMenu( menu ),
|
||
|
visible( TRUE ),
|
||
|
separator( FALSE ),
|
||
|
removable( TRUE )
|
||
|
{
|
||
|
text = menu->name();
|
||
|
}
|
||
|
|
||
15 years ago
|
MenuBarEditorItem::MenuBarEditorItem( TQActionGroup * actionGroup, MenuBarEditor * bar,
|
||
14 years ago
|
TQObject * parent, const char * name )
|
||
|
: TQObject( parent, name ),
|
||
15 years ago
|
menuBar( bar ),
|
||
|
popupMenu( 0 ),
|
||
|
visible( TRUE ),
|
||
|
separator( FALSE ),
|
||
|
removable( TRUE )
|
||
|
{
|
||
|
text = actionGroup->menuText();
|
||
|
popupMenu = new PopupMenuEditor( menuBar->formWindow(), menuBar );
|
||
|
popupMenu->insert( actionGroup );
|
||
|
}
|
||
|
|
||
14 years ago
|
MenuBarEditorItem::MenuBarEditorItem( MenuBarEditorItem * item, TQObject * parent, const char * name )
|
||
|
: TQObject( parent, name ),
|
||
15 years ago
|
menuBar( item->menuBar ),
|
||
|
popupMenu( 0 ),
|
||
|
text( item->text ),
|
||
|
visible( item->visible ),
|
||
|
separator( item->separator ),
|
||
|
removable( item->removable )
|
||
|
{
|
||
|
popupMenu = new PopupMenuEditor( menuBar->formWindow(), item->popupMenu, menuBar );
|
||
|
}
|
||
|
|
||
|
// MenuBarEditor --------------------------------------------------------
|
||
|
|
||
|
int MenuBarEditor::clipboardOperation = 0;
|
||
|
MenuBarEditorItem * MenuBarEditor::clipboardItem = 0;
|
||
|
|
||
14 years ago
|
MenuBarEditor::MenuBarEditor( FormWindow * fw, TQWidget * parent, const char * name )
|
||
|
: TQMenuBar( parent, name ),
|
||
15 years ago
|
formWnd( fw ),
|
||
|
draggedItem( 0 ),
|
||
|
currentIndex( 0 ),
|
||
|
itemHeight( 0 ),
|
||
|
separatorWidth( 32 ),
|
||
|
hideWhenEmpty( TRUE ),
|
||
|
hasSeparator( FALSE )
|
||
|
{
|
||
|
setAcceptDrops( TRUE );
|
||
14 years ago
|
setFocusPolicy( TQ_StrongFocus );
|
||
15 years ago
|
|
||
|
addItem.setMenuText( i18n("new menu") );
|
||
|
addSeparator.setMenuText( i18n("new separator") );
|
||
|
|
||
15 years ago
|
lineEdit = new TQLineEdit( this, "menubar lineedit" );
|
||
15 years ago
|
lineEdit->hide();
|
||
15 years ago
|
lineEdit->setFrameStyle(TQFrame::Plain | TQFrame::NoFrame);
|
||
15 years ago
|
lineEdit->polish();
|
||
|
lineEdit->setBackgroundMode(PaletteButton);
|
||
|
lineEdit->setBackgroundOrigin(ParentOrigin);
|
||
|
lineEdit->installEventFilter( this );
|
||
|
|
||
14 years ago
|
dropLine = new TQWidget( this, "menubar dropline", TQt::WStyle_NoBorder | WStyle_StaysOnTop );
|
||
|
dropLine->setBackgroundColor( TQt::red );
|
||
15 years ago
|
dropLine->hide();
|
||
|
|
||
|
setMinimumHeight( fontMetrics().height() + 2 * borderSize() );
|
||
|
}
|
||
|
|
||
|
MenuBarEditor::~MenuBarEditor()
|
||
|
{
|
||
|
itemList.setAutoDelete( TRUE );
|
||
|
}
|
||
|
|
||
|
FormWindow * MenuBarEditor::formWindow()
|
||
|
{
|
||
|
return formWnd;
|
||
|
}
|
||
|
|
||
|
MenuBarEditorItem * MenuBarEditor::createItem( int index, bool addToCmdStack )
|
||
|
{
|
||
|
MenuBarEditorItem * i =
|
||
14 years ago
|
new MenuBarEditorItem( new PopupMenuEditor( formWnd, ( TQWidget * ) parent() ), this );
|
||
15 years ago
|
if ( addToCmdStack ) {
|
||
|
AddMenuCommand * cmd = new AddMenuCommand( i18n( "Add Menu" ), formWnd, this, i, index );
|
||
|
formWnd->commandHistory()->addCommand( cmd );
|
||
|
cmd->execute();
|
||
|
} else {
|
||
|
AddMenuCommand cmd( i18n( "Add Menu" ), formWnd, this, i, index );
|
||
|
cmd.execute();
|
||
|
}
|
||
|
return i;
|
||
|
}
|
||
|
|
||
|
void MenuBarEditor::insertItem( MenuBarEditorItem * item, int index )
|
||
|
{
|
||
14 years ago
|
item->menu()->parentMenu = this;
|
||
15 years ago
|
|
||
|
if ( index != -1 )
|
||
|
itemList.insert( index, item );
|
||
|
else
|
||
|
itemList.append( item );
|
||
|
|
||
|
if ( hideWhenEmpty && itemList.count() == 1 )
|
||
|
show(); // calls resizeInternals();
|
||
|
else
|
||
|
resizeInternals();
|
||
|
|
||
|
if ( isVisible() )
|
||
|
update();
|
||
|
}
|
||
|
|
||
15 years ago
|
void MenuBarEditor::insertItem( TQString text, PopupMenuEditor * menu, int index )
|
||
15 years ago
|
{
|
||
|
MenuBarEditorItem * item = new MenuBarEditorItem( menu, this );
|
||
|
if ( !text.isNull() )
|
||
|
item->setMenuText( text );
|
||
|
insertItem( item, index );
|
||
|
}
|
||
|
|
||
15 years ago
|
void MenuBarEditor::insertItem( TQString text, TQActionGroup * group, int index )
|
||
15 years ago
|
{
|
||
|
MenuBarEditorItem * item = new MenuBarEditorItem( group, this );
|
||
|
if ( !text.isNull() )
|
||
|
item->setMenuText( text );
|
||
|
insertItem( item, index );
|
||
|
}
|
||
|
|
||
|
|
||
|
void MenuBarEditor::insertSeparator( int index )
|
||
|
{
|
||
|
if ( hasSeparator )
|
||
|
return;
|
||
|
|
||
|
MenuBarEditorItem * i = createItem( index );
|
||
|
i->setSeparator( TRUE );
|
||
|
i->setMenuText( i18n( "separator" ) );
|
||
|
hasSeparator = TRUE;
|
||
|
}
|
||
|
|
||
|
void MenuBarEditor::removeItemAt( int index )
|
||
|
{
|
||
|
removeItem( item( index ) );
|
||
|
}
|
||
|
|
||
|
void MenuBarEditor::removeItem( MenuBarEditorItem * item )
|
||
|
{
|
||
|
if ( item &&
|
||
|
item->isRemovable() &&
|
||
|
itemList.removeRef( item ) ) {
|
||
|
|
||
|
if ( item->isSeparator() )
|
||
|
hasSeparator = FALSE;
|
||
|
|
||
|
if ( hideWhenEmpty && itemList.count() == 0 )
|
||
|
hide();
|
||
|
else
|
||
|
resizeInternals();
|
||
|
|
||
|
int n = count() + 1;
|
||
|
if ( currentIndex >= n )
|
||
|
currentIndex = n;
|
||
|
|
||
|
if ( isVisible() )
|
||
|
update();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int MenuBarEditor::findItem( MenuBarEditorItem * item )
|
||
|
{
|
||
14 years ago
|
return itemList.findRef( item );
|
||
15 years ago
|
}
|
||
|
|
||
|
int MenuBarEditor::findItem( PopupMenuEditor * menu )
|
||
|
{
|
||
|
MenuBarEditorItem * i = itemList.first();
|
||
|
|
||
|
while ( i ) {
|
||
|
if ( i->menu() == menu )
|
||
|
return itemList.at();
|
||
|
i = itemList.next();
|
||
|
}
|
||
|
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
15 years ago
|
int MenuBarEditor::findItem( TQPoint & pos )
|
||
15 years ago
|
{
|
||
|
int x = borderSize();
|
||
|
int dx = 0;
|
||
|
int y = 0;
|
||
|
int w = width();
|
||
15 years ago
|
TQSize s;
|
||
|
TQRect r;
|
||
15 years ago
|
|
||
|
MenuBarEditorItem * i = itemList.first();
|
||
|
|
||
|
while ( i ) {
|
||
|
|
||
|
if ( i->isVisible() ) {
|
||
|
|
||
|
s = itemSize( i );
|
||
|
dx = s.width();
|
||
|
|
||
|
if ( x + dx > w && x > borderSize() ) {
|
||
|
y += itemHeight;
|
||
|
x = borderSize();
|
||
|
}
|
||
|
|
||
15 years ago
|
r = TQRect( x, y, s.width(), s.height() );
|
||
15 years ago
|
|
||
14 years ago
|
if ( r.contains( pos ) )
|
||
15 years ago
|
return itemList.at();
|
||
|
|
||
|
addItemSizeToCoords( i, x, y, w );
|
||
|
}
|
||
|
|
||
|
i = itemList.next();
|
||
|
}
|
||
|
|
||
|
// check add item
|
||
|
s = itemSize( &addItem );
|
||
|
dx = s.width();
|
||
|
|
||
|
if ( x + dx > w && x > borderSize() ) {
|
||
|
y += itemHeight;
|
||
|
x = borderSize();
|
||
|
}
|
||
|
|
||
15 years ago
|
r = TQRect( x, y, s.width(), s.height() );
|
||
15 years ago
|
|
||
14 years ago
|
if ( r.contains( pos ) )
|
||
15 years ago
|
return itemList.count();
|
||
|
|
||
|
return itemList.count() + 1;
|
||
|
}
|
||
|
|
||
|
MenuBarEditorItem * MenuBarEditor::item( int index )
|
||
|
{
|
||
|
if ( index == -1 )
|
||
|
return itemList.at( currentIndex );
|
||
|
|
||
|
int c = itemList.count();
|
||
|
if ( index == c )
|
||
|
return &addItem;
|
||
|
else if ( index > c )
|
||
|
return &addSeparator;
|
||
|
|
||
|
return itemList.at( index );
|
||
|
}
|
||
|
|
||
|
int MenuBarEditor::count()
|
||
|
{
|
||
|
return itemList.count();
|
||
|
}
|
||
|
|
||
|
int MenuBarEditor::current()
|
||
|
{
|
||
|
return currentIndex;
|
||
|
}
|
||
|
|
||
|
void MenuBarEditor::cut( int index )
|
||
|
{
|
||
|
if ( clipboardItem && clipboardOperation == Cut )
|
||
|
delete clipboardItem;
|
||
|
|
||
|
clipboardOperation = Cut;
|
||
|
clipboardItem = itemList.at( index );
|
||
|
|
||
|
if ( clipboardItem == &addItem || clipboardItem == &addSeparator ) {
|
||
|
clipboardOperation = None;
|
||
|
clipboardItem = 0;
|
||
|
return; // do nothing
|
||
|
}
|
||
|
|
||
|
RemoveMenuCommand * cmd = new RemoveMenuCommand( i18n( "Cut Menu" ), formWnd, this, index );
|
||
|
formWnd->commandHistory()->addCommand( cmd );
|
||
|
cmd->execute();
|
||
|
}
|
||
|
|
||
|
void MenuBarEditor::copy( int index )
|
||
|
{
|
||
|
if ( clipboardItem && clipboardOperation == Cut )
|
||
|
delete clipboardItem;
|
||
|
|
||
|
clipboardOperation = Copy;
|
||
|
clipboardItem = itemList.at( index );
|
||
|
|
||
|
if ( clipboardItem == &addItem || clipboardItem == &addSeparator ) {
|
||
|
clipboardOperation = None;
|
||
|
clipboardItem = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void MenuBarEditor::paste( int index )
|
||
|
{
|
||
|
if ( clipboardItem && clipboardOperation ) {
|
||
|
MenuBarEditorItem * i = new MenuBarEditorItem( clipboardItem );
|
||
|
AddMenuCommand * cmd = new AddMenuCommand( i18n( "Paste Menu" ), formWnd, this, i, index );
|
||
|
formWnd->commandHistory()->addCommand( cmd );
|
||
|
cmd->execute();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void MenuBarEditor::exchange( int a, int b )
|
||
|
{
|
||
|
MenuBarEditorItem * ia = itemList.at( a );
|
||
|
MenuBarEditorItem * ib = itemList.at( b );
|
||
|
if ( !ia || !ib ||
|
||
|
ia == &addItem || ia == &addSeparator ||
|
||
|
ib == &addItem || ib == &addSeparator )
|
||
|
return; // do nothing
|
||
14 years ago
|
itemList.replace( b, ia );
|
||
|
itemList.replace( a, ib );
|
||
15 years ago
|
}
|
||
|
|
||
|
void MenuBarEditor::showLineEdit( int index )
|
||
|
{
|
||
|
if ( index == -1 )
|
||
|
index = currentIndex;
|
||
|
|
||
|
MenuBarEditorItem * i = 0;
|
||
|
|
||
|
if ( (uint) index >= itemList.count() )
|
||
|
i = &addItem;
|
||
|
else
|
||
|
i = itemList.at( index );
|
||
|
|
||
|
if ( i && i->isSeparator() )
|
||
|
return;
|
||
|
|
||
|
// open edit field for item name
|
||
|
lineEdit->setText( i->menuText() );
|
||
|
lineEdit->selectAll();
|
||
15 years ago
|
TQPoint pos = itemPos( index );
|
||
15 years ago
|
lineEdit->move( pos.x() + borderSize(), pos.y() - ( borderSize() / 2 ) );
|
||
|
lineEdit->resize( itemSize( i ) );
|
||
|
lineEdit->show();
|
||
|
lineEdit->setFocus();
|
||
|
}
|
||
|
|
||
|
void MenuBarEditor::showItem( int index )
|
||
|
{
|
||
|
if ( index == -1 )
|
||
|
index = currentIndex;
|
||
|
|
||
|
if ( (uint)index < itemList.count() ) {
|
||
|
MenuBarEditorItem * i = itemList.at( index );
|
||
|
if ( i->isSeparator() || draggedItem )
|
||
|
return;
|
||
|
PopupMenuEditor * m = i->menu();
|
||
15 years ago
|
TQPoint pos = itemPos( index );
|
||
15 years ago
|
m->move( pos.x(), pos.y() + itemHeight - 1 );
|
||
|
m->raise();
|
||
|
m->show();
|
||
|
setFocus();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void MenuBarEditor::hideItem( int index )
|
||
|
{
|
||
|
if ( index == -1 )
|
||
|
index = currentIndex;
|
||
|
|
||
|
if ( (uint)index < itemList.count() ) {
|
||
|
PopupMenuEditor * m = itemList.at( index )->menu();
|
||
|
m->hideSubMenu();
|
||
|
m->hide();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void MenuBarEditor::focusItem( int index )
|
||
|
{
|
||
|
if ( index == -1 )
|
||
|
index = currentIndex;
|
||
|
|
||
|
if ( (uint)index < itemList.count() ) {
|
||
|
PopupMenuEditor * m = itemList.at( index )->menu();
|
||
|
m->setFocus();
|
||
|
m->update();
|
||
|
update();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void MenuBarEditor::deleteItem( int index )
|
||
|
{
|
||
|
if ( index == -1 )
|
||
|
index = currentIndex;
|
||
|
|
||
|
if ( (uint)index < itemList.count() ) {
|
||
|
RemoveMenuCommand * cmd = new RemoveMenuCommand( i18n( "Delete Menu" ),
|
||
|
formWnd,
|
||
|
this,
|
||
|
currentIndex );
|
||
|
formWnd->commandHistory()->addCommand( cmd );
|
||
|
cmd->execute();
|
||
|
}
|
||
|
}
|
||
|
|
||
13 years ago
|
TQSize MenuBarEditor::sizeHint() const
|
||
15 years ago
|
{
|
||
14 years ago
|
return TQSize( parentWidget()->width(), heightForWidth( parentWidget()->width() ) );
|
||
15 years ago
|
}
|
||
|
|
||
|
int MenuBarEditor::heightForWidth( int max_width ) const
|
||
|
{
|
||
|
MenuBarEditor * that = ( MenuBarEditor * ) this;
|
||
|
int x = borderSize();
|
||
|
int y = 0;
|
||
|
|
||
15 years ago
|
TQPainter p( this );
|
||
15 years ago
|
that->itemHeight = that->itemSize( &(that->addItem) ).height();
|
||
|
|
||
|
MenuBarEditorItem * i = that->itemList.first();
|
||
|
while ( i ) {
|
||
|
if ( i->isVisible() )
|
||
|
that->addItemSizeToCoords( i, x, y, max_width );
|
||
|
i = that->itemList.next();
|
||
|
}
|
||
|
|
||
|
that->addItemSizeToCoords( &(that->addItem), x, y, max_width );
|
||
|
that->addItemSizeToCoords( &(that->addSeparator), x, y, max_width );
|
||
|
|
||
|
return y + itemHeight;
|
||
|
}
|
||
|
|
||
|
void MenuBarEditor::show()
|
||
|
{
|
||
15 years ago
|
TQWidget::show();
|
||
15 years ago
|
resizeInternals();
|
||
|
|
||
14 years ago
|
TQResizeEvent e( parentWidget()->size(), parentWidget()->size() );
|
||
|
TQApplication::sendEvent( parentWidget(), &e );
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
void MenuBarEditor::checkAccels( TQMap<TQChar, TQWidgetList > &accels )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString t;
|
||
15 years ago
|
MenuBarEditorItem * i = itemList.first();
|
||
|
while ( i ) {
|
||
|
t = i->menuText();
|
||
|
find_accel( t, accels, this );
|
||
|
// do not check the accelerators in the popup menus
|
||
|
i = itemList.next();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// public slots
|
||
|
|
||
|
void MenuBarEditor::cut()
|
||
|
{
|
||
|
cut( currentIndex );
|
||
|
}
|
||
|
|
||
|
void MenuBarEditor::copy()
|
||
|
{
|
||
|
copy( currentIndex );
|
||
|
}
|
||
|
|
||
|
void MenuBarEditor::paste()
|
||
|
{
|
||
|
paste( currentIndex );
|
||
|
}
|
||
|
|
||
|
// protected
|
||
|
|
||
15 years ago
|
bool MenuBarEditor::eventFilter( TQObject * o, TQEvent * e )
|
||
15 years ago
|
{
|
||
14 years ago
|
if ( TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(lineEdit) && e->type() == TQEvent::FocusOut ) {
|
||
15 years ago
|
leaveEditMode();
|
||
|
lineEdit->hide();
|
||
|
update();
|
||
15 years ago
|
} else if ( e->type() == TQEvent::LayoutHint ) {
|
||
13 years ago
|
resize( sizeHint() );
|
||
15 years ago
|
}
|
||
15 years ago
|
return TQMenuBar::eventFilter( o, e );
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
void MenuBarEditor::paintEvent( TQPaintEvent * )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQPainter p( this );
|
||
|
TQRect r = rect();
|
||
13 years ago
|
style().tqdrawPrimitive( TQStyle::PE_PanelMenuBar, &p,
|
||
13 years ago
|
r, colorGroup() );
|
||
15 years ago
|
drawItems( p );
|
||
|
}
|
||
|
|
||
15 years ago
|
void MenuBarEditor::mousePressEvent( TQMouseEvent * e )
|
||
15 years ago
|
{
|
||
|
mousePressPos = e->pos();
|
||
|
hideItem();
|
||
|
lineEdit->hide();
|
||
|
currentIndex = findItem( mousePressPos );
|
||
|
showItem();
|
||
|
update();
|
||
|
e->accept();
|
||
|
}
|
||
|
|
||
15 years ago
|
void MenuBarEditor::mouseDoubleClickEvent( TQMouseEvent * e )
|
||
15 years ago
|
{
|
||
|
mousePressPos = e->pos();
|
||
|
currentIndex = findItem( mousePressPos );
|
||
|
lineEdit->hide();
|
||
|
if ( currentIndex > (int)itemList.count() ) {
|
||
|
insertSeparator();
|
||
|
update();
|
||
|
} else {
|
||
|
showLineEdit();
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void MenuBarEditor::mouseMoveEvent( TQMouseEvent * e )
|
||
15 years ago
|
{
|
||
|
if ( e->state() & Qt::LeftButton ) {
|
||
|
if ( ( e->pos() - mousePressPos ).manhattanLength() > 3 ) {
|
||
|
bool itemCreated = FALSE;
|
||
|
bool isSeparator = FALSE;
|
||
|
draggedItem = item( findItem( mousePressPos ) );
|
||
|
if ( draggedItem == &addItem ) {
|
||
|
draggedItem = createItem();
|
||
|
itemCreated = TRUE;
|
||
|
} else if ( draggedItem == &addSeparator ) {
|
||
|
if (hasSeparator) // we can only have one separator
|
||
|
return;
|
||
|
draggedItem = createItem();
|
||
|
draggedItem->setSeparator( TRUE );
|
||
|
draggedItem->setMenuText( "separator" );
|
||
|
isSeparator = TRUE;
|
||
|
itemCreated = TRUE;
|
||
|
} else {
|
||
|
isSeparator = draggedItem->isSeparator();
|
||
|
}
|
||
|
|
||
|
MenuBarEditorItemPtrDrag * d =
|
||
|
new MenuBarEditorItemPtrDrag( draggedItem, this );
|
||
|
d->setPixmap( createTextPixmap( draggedItem->menuText() ) );
|
||
|
hideItem();
|
||
|
draggedItem->setVisible( FALSE );
|
||
|
update();
|
||
|
|
||
|
// If the item is dropped in the same list,
|
||
|
// we will have two instances of the same pointer
|
||
|
// in the list.
|
||
14 years ago
|
itemList.find( draggedItem );
|
||
15 years ago
|
TQLNode * node = itemList.currentNode();
|
||
15 years ago
|
dropConfirmed = FALSE;
|
||
|
d->dragCopy(); // dragevents and stuff happens
|
||
|
if ( draggedItem ) { // item was not dropped
|
||
|
if ( itemCreated ) {
|
||
|
removeItem( draggedItem );
|
||
|
} else {
|
||
|
hideItem();
|
||
|
draggedItem->setVisible( TRUE );
|
||
|
draggedItem = 0;
|
||
|
showItem();
|
||
|
}
|
||
|
} else if ( dropConfirmed ) { // item was dropped
|
||
|
dropConfirmed = FALSE;
|
||
|
hideItem();
|
||
|
itemList.takeNode( node )->setVisible( TRUE );
|
||
|
hasSeparator = isSeparator || hasSeparator;
|
||
|
showItem();
|
||
|
} else {
|
||
|
hasSeparator = isSeparator || hasSeparator;
|
||
|
}
|
||
|
update();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void MenuBarEditor::dragEnterEvent( TQDragEnterEvent * e )
|
||
15 years ago
|
{
|
||
|
if ( MenuBarEditorItemPtrDrag::canDecode( e ) ) {
|
||
|
e->accept();
|
||
|
dropLine->show();
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void MenuBarEditor::dragLeaveEvent( TQDragLeaveEvent * )
|
||
15 years ago
|
{
|
||
|
dropLine->hide();
|
||
|
}
|
||
|
|
||
15 years ago
|
void MenuBarEditor::dragMoveEvent( TQDragMoveEvent * e )
|
||
15 years ago
|
{
|
||
|
|
||
15 years ago
|
TQPoint pos = e->pos();
|
||
15 years ago
|
dropLine->move( snapToItem( pos ) );
|
||
|
|
||
|
int idx = findItem( pos );
|
||
|
if ( currentIndex != idx ) {
|
||
|
hideItem();
|
||
|
currentIndex = idx;
|
||
|
showItem();
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void MenuBarEditor::dropEvent( TQDropEvent * e )
|
||
15 years ago
|
{
|
||
|
MenuBarEditorItem * i = 0;
|
||
|
|
||
|
if ( MenuBarEditorItemPtrDrag::decode( e, &i ) ) {
|
||
|
draggedItem = 0;
|
||
|
hideItem();
|
||
|
dropInPlace( i, e->pos() );
|
||
|
e->accept();
|
||
|
}
|
||
|
|
||
|
dropLine->hide();
|
||
|
}
|
||
|
|
||
15 years ago
|
void MenuBarEditor::keyPressEvent( TQKeyEvent * e )
|
||
15 years ago
|
{
|
||
|
if ( lineEdit->isHidden() ) { // In navigation mode
|
||
|
switch ( e->key() ) {
|
||
|
|
||
14 years ago
|
case TQt::Key_Delete:
|
||
15 years ago
|
hideItem();
|
||
|
deleteItem();
|
||
|
showItem();
|
||
|
break;
|
||
|
|
||
14 years ago
|
case TQt::Key_Left:
|
||
15 years ago
|
e->accept();
|
||
14 years ago
|
navigateLeft( e->state() & TQt::ControlButton );
|
||
15 years ago
|
return;
|
||
|
|
||
14 years ago
|
case TQt::Key_Right:
|
||
15 years ago
|
e->accept();
|
||
14 years ago
|
navigateRight( e->state() & TQt::ControlButton );
|
||
15 years ago
|
return; // no update
|
||
|
|
||
14 years ago
|
case TQt::Key_Down:
|
||
15 years ago
|
e->accept();
|
||
|
focusItem();
|
||
|
return; // no update
|
||
|
|
||
14 years ago
|
case TQt::Key_PageUp:
|
||
15 years ago
|
currentIndex = 0;
|
||
|
break;
|
||
|
|
||
14 years ago
|
case TQt::Key_PageDown:
|
||
15 years ago
|
currentIndex = itemList.count();
|
||
|
break;
|
||
|
|
||
14 years ago
|
case TQt::Key_Enter:
|
||
|
case TQt::Key_Return:
|
||
|
case TQt::Key_F2:
|
||
15 years ago
|
e->accept();
|
||
|
enterEditMode();
|
||
|
return; // no update
|
||
|
|
||
14 years ago
|
case TQt::Key_Up:
|
||
|
case TQt::Key_Alt:
|
||
|
case TQt::Key_Shift:
|
||
|
case TQt::Key_Control:
|
||
|
case TQt::Key_Escape:
|
||
15 years ago
|
e->ignore();
|
||
|
setFocus(); // FIXME: this is because some other widget get the focus when CTRL is pressed
|
||
|
return; // no update
|
||
|
|
||
14 years ago
|
case TQt::Key_C:
|
||
|
if ( e->state() & TQt::ControlButton && currentIndex < (int)itemList.count() ) {
|
||
15 years ago
|
copy( currentIndex );
|
||
|
break;
|
||
|
}
|
||
|
|
||
14 years ago
|
case TQt::Key_X:
|
||
|
if ( e->state() & TQt::ControlButton && currentIndex < (int)itemList.count() ) {
|
||
15 years ago
|
hideItem();
|
||
|
cut( currentIndex );
|
||
|
showItem();
|
||
|
break;
|
||
|
}
|
||
|
|
||
14 years ago
|
case TQt::Key_V:
|
||
|
if ( e->state() & TQt::ControlButton ) {
|
||
15 years ago
|
hideItem();
|
||
|
paste( currentIndex < (int)itemList.count() ? currentIndex + 1: itemList.count() );
|
||
|
showItem();
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
default:
|
||
|
if ( e->ascii() >= 32 || e->ascii() == 0 ) {
|
||
|
showLineEdit();
|
||
15 years ago
|
TQApplication::sendEvent( lineEdit, e );
|
||
15 years ago
|
e->accept();
|
||
|
} else {
|
||
|
e->ignore();
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
} else { // In edit mode
|
||
|
|
||
|
switch ( e->key() ) {
|
||
14 years ago
|
case TQt::Key_Control:
|
||
15 years ago
|
e->ignore();
|
||
|
return;
|
||
14 years ago
|
case TQt::Key_Enter:
|
||
|
case TQt::Key_Return:
|
||
15 years ago
|
leaveEditMode();
|
||
14 years ago
|
case TQt::Key_Escape:
|
||
15 years ago
|
lineEdit->hide();
|
||
|
setFocus();
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
e->accept();
|
||
|
update();
|
||
|
}
|
||
|
|
||
15 years ago
|
void MenuBarEditor::focusOutEvent( TQFocusEvent * e )
|
||
15 years ago
|
{
|
||
13 years ago
|
TQWidget * fw = tqApp->focusWidget();
|
||
14 years ago
|
if ( e->lostFocus() && !::tqqt_cast<PopupMenuEditor*>(fw) )
|
||
15 years ago
|
hideItem();
|
||
|
update();
|
||
|
}
|
||
|
|
||
|
void MenuBarEditor::resizeInternals()
|
||
|
{
|
||
|
dropLine->resize( 2, itemHeight );
|
||
|
updateGeometry();
|
||
|
}
|
||
|
|
||
15 years ago
|
void MenuBarEditor::drawItems( TQPainter & p )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQPoint pos( borderSize(), 0 );
|
||
15 years ago
|
uint c = 0;
|
||
|
|
||
13 years ago
|
p.setPen( colorGroup().buttonText() );
|
||
15 years ago
|
|
||
|
MenuBarEditorItem * i = itemList.first();
|
||
|
while ( i ) {
|
||
|
if ( i->isVisible() )
|
||
|
drawItem( p, i, c++, pos ); // updates x y
|
||
|
i = itemList.next();
|
||
|
}
|
||
|
|
||
|
p.setPen( darkBlue );
|
||
|
drawItem( p, &addItem, c++, pos );
|
||
|
if ( !hasSeparator )
|
||
|
drawItem( p, &addSeparator, c, pos );
|
||
|
}
|
||
|
|
||
15 years ago
|
void MenuBarEditor::drawItem( TQPainter & p,
|
||
15 years ago
|
MenuBarEditorItem * i,
|
||
|
int idx,
|
||
15 years ago
|
TQPoint & pos )
|
||
15 years ago
|
{
|
||
|
int w = itemSize( i ).width();
|
||
|
|
||
|
// If the item passes the right border, and it is not the first item on the line
|
||
|
if ( pos.x() + w > width() && pos.x() > borderSize() ) { // wrap
|
||
|
pos.ry() += itemHeight;
|
||
|
pos.setX( borderSize() );
|
||
|
}
|
||
|
|
||
|
if ( i->isSeparator() ) {
|
||
|
drawSeparator( p, pos );
|
||
|
} else {
|
||
15 years ago
|
int flags = TQPainter::AlignLeft | TQPainter::AlignVCenter |
|
||
14 years ago
|
TQt::ShowPrefix | TQt::SingleLine;
|
||
15 years ago
|
p.drawText( pos.x() + borderSize(), pos.y(), w - borderSize(), itemHeight,
|
||
|
flags, i->menuText() );
|
||
|
}
|
||
|
|
||
|
if ( hasFocus() && idx == currentIndex && !draggedItem )
|
||
|
p.drawWinFocusRect( pos.x(), pos.y() + 1, w, itemHeight - 2 );
|
||
|
|
||
|
pos.rx() += w;
|
||
|
}
|
||
|
|
||
15 years ago
|
void MenuBarEditor::drawSeparator( TQPainter & p, TQPoint & pos )
|
||
15 years ago
|
{
|
||
|
p.save();
|
||
|
p.setPen( darkBlue );
|
||
|
|
||
|
int left = pos.x();
|
||
|
int top = pos.y() + 2;
|
||
|
int right = left + separatorWidth - 1;
|
||
|
int bottom = pos.y() + itemHeight - 4;
|
||
|
|
||
|
p.drawLine( left, top, left, bottom );
|
||
|
p.drawLine( right, top, right, bottom );
|
||
|
|
||
|
p.fillRect( left, pos.y() + borderSize() * 2,
|
||
|
separatorWidth - 1, itemHeight - borderSize() * 4,
|
||
14 years ago
|
TQBrush( darkBlue, TQt::Dense5Pattern ) );
|
||
15 years ago
|
|
||
|
p.restore();
|
||
|
}
|
||
|
|
||
15 years ago
|
TQSize MenuBarEditor::itemSize( MenuBarEditorItem * i )
|
||
15 years ago
|
{
|
||
|
if ( i->isSeparator() )
|
||
15 years ago
|
return TQSize( separatorWidth, itemHeight );
|
||
|
TQRect r = fontMetrics().boundingRect( i->menuText().remove( "&") );
|
||
|
return TQSize( r.width() + borderSize() * 2, r.height() + borderSize() * 4 );
|
||
15 years ago
|
}
|
||
|
|
||
|
void MenuBarEditor::addItemSizeToCoords( MenuBarEditorItem * i, int & x, int & y, int w )
|
||
|
{
|
||
|
int dx = itemSize( i ).width();
|
||
|
if ( x + dx > w && x > borderSize() ) {
|
||
|
y += itemHeight;
|
||
|
x = borderSize();
|
||
|
}
|
||
|
x += dx;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQPoint MenuBarEditor::itemPos( int index )
|
||
15 years ago
|
{
|
||
|
int x = borderSize();
|
||
|
int y = 0;
|
||
|
int w = width();
|
||
|
int dx = 0;
|
||
|
int c = 0;
|
||
|
|
||
|
MenuBarEditorItem * i = itemList.first();
|
||
|
|
||
|
while ( i ) {
|
||
|
if ( i->isVisible() ) {
|
||
|
dx = itemSize( i ).width();
|
||
|
if ( x + dx > w && x > borderSize() ) {
|
||
|
y += itemHeight;
|
||
|
x = borderSize();
|
||
|
}
|
||
|
if ( c == index )
|
||
15 years ago
|
return TQPoint( x, y );
|
||
15 years ago
|
x += dx;
|
||
|
c++;
|
||
|
}
|
||
|
i = itemList.next();
|
||
|
}
|
||
|
dx = itemSize( &addItem ).width();
|
||
|
if ( x + dx > width() && x > borderSize() ) {
|
||
|
y += itemHeight;
|
||
|
x = borderSize();
|
||
|
}
|
||
|
|
||
15 years ago
|
return TQPoint( x, y );
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
TQPoint MenuBarEditor::snapToItem( const TQPoint & pos )
|
||
15 years ago
|
{
|
||
|
int x = borderSize();
|
||
|
int y = 0;
|
||
|
int dx = 0;
|
||
|
|
||
|
MenuBarEditorItem * n = itemList.first();
|
||
|
|
||
|
while ( n ) {
|
||
|
if ( n->isVisible() ) {
|
||
|
dx = itemSize( n ).width();
|
||
|
if ( x + dx > width() && x > borderSize() ) {
|
||
|
y += itemHeight;
|
||
|
x = borderSize();
|
||
|
}
|
||
|
if ( pos.y() > y &&
|
||
|
pos.y() < y + itemHeight &&
|
||
|
pos.x() < x + dx / 2 ) {
|
||
15 years ago
|
return TQPoint( x, y );
|
||
15 years ago
|
}
|
||
|
x += dx;
|
||
|
}
|
||
|
n = itemList.next();
|
||
|
}
|
||
|
|
||
15 years ago
|
return TQPoint( x, y );
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
void MenuBarEditor::dropInPlace( MenuBarEditorItem * i, const TQPoint & pos )
|
||
15 years ago
|
{
|
||
|
int x = borderSize();
|
||
|
int y = 0;
|
||
|
int dx = 0;
|
||
|
int idx = 0;
|
||
|
|
||
|
MenuBarEditorItem * n = itemList.first();
|
||
|
|
||
|
while ( n ) {
|
||
|
if ( n->isVisible() ) {
|
||
|
dx = itemSize( n ).width();
|
||
|
if ( x + dx > width() && x > borderSize() ) {
|
||
|
y += itemHeight;
|
||
|
x = borderSize();
|
||
|
}
|
||
|
if ( pos.y() > y &&
|
||
|
pos.y() < y + itemHeight &&
|
||
|
pos.x() < x + dx / 2 )
|
||
|
break;
|
||
|
x += dx;
|
||
|
}
|
||
|
n = itemList.next();
|
||
|
idx++;
|
||
|
}
|
||
|
|
||
|
hideItem();
|
||
|
Command * cmd = 0;
|
||
14 years ago
|
int iidx = itemList.findRef( i );
|
||
15 years ago
|
if ( iidx != -1 ) { // internal dnd
|
||
|
cmd = new MoveMenuCommand( i18n( "Item Dragged" ), formWnd, this, iidx, idx );
|
||
|
item( iidx )->setVisible( TRUE );
|
||
|
} else {
|
||
|
cmd = new AddMenuCommand( i18n( "Add Menu" ), formWnd, this, i, idx );
|
||
|
dropConfirmed = TRUE; // let mouseMoveEvent set the item visible
|
||
|
}
|
||
|
formWnd->commandHistory()->addCommand( cmd );
|
||
|
cmd->execute();
|
||
|
currentIndex = ( iidx >= 0 && iidx < idx ) ? idx - 1 : idx;
|
||
|
showItem();
|
||
|
}
|
||
|
|
||
|
|
||
|
void MenuBarEditor::safeDec()
|
||
|
{
|
||
|
do {
|
||
|
currentIndex--;
|
||
|
} while ( currentIndex > 0 && !( item( currentIndex )->isVisible() ) );
|
||
|
}
|
||
|
|
||
|
void MenuBarEditor::safeInc()
|
||
|
{
|
||
|
int max = (int)itemList.count();
|
||
|
if ( !hasSeparator )
|
||
|
max += 1;
|
||
|
if ( currentIndex < max ) {
|
||
|
do {
|
||
|
currentIndex++;
|
||
|
// skip invisible items
|
||
|
} while ( currentIndex < max && !( item( currentIndex )->isVisible() ) );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void MenuBarEditor::navigateLeft( bool ctrl )
|
||
|
{
|
||
|
// FIXME: handle invisible items
|
||
|
if ( currentIndex > 0 ) {
|
||
|
hideItem();
|
||
|
if ( ctrl ) {
|
||
|
ExchangeMenuCommand * cmd = new ExchangeMenuCommand( i18n( "Move Menu Left" ),
|
||
|
formWnd,
|
||
|
this,
|
||
|
currentIndex,
|
||
|
currentIndex - 1 );
|
||
|
formWnd->commandHistory()->addCommand( cmd );
|
||
|
cmd->execute();
|
||
|
safeDec();
|
||
|
} else {
|
||
|
safeDec();
|
||
|
}
|
||
|
showItem();
|
||
|
}
|
||
|
update();
|
||
|
}
|
||
|
|
||
|
void MenuBarEditor::navigateRight( bool ctrl )
|
||
|
{
|
||
|
// FIXME: handle invisible items
|
||
|
hideItem();
|
||
|
if ( ctrl ) {
|
||
|
if ( currentIndex < ( (int)itemList.count() - 1 ) ) {
|
||
|
ExchangeMenuCommand * cmd = new ExchangeMenuCommand( i18n( "Move Menu Right" ),
|
||
|
formWnd,
|
||
|
this,
|
||
|
currentIndex,
|
||
|
currentIndex + 1 );
|
||
|
formWnd->commandHistory()->addCommand( cmd );
|
||
|
cmd->execute();
|
||
|
safeInc();
|
||
|
}
|
||
|
} else {
|
||
|
safeInc();
|
||
|
}
|
||
|
showItem();
|
||
|
update();
|
||
|
}
|
||
|
|
||
|
void MenuBarEditor::enterEditMode()
|
||
|
{
|
||
|
if ( currentIndex > (int)itemList.count() ) {
|
||
|
insertSeparator();
|
||
|
} else {
|
||
|
showLineEdit();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void MenuBarEditor::leaveEditMode()
|
||
|
{
|
||
|
MenuBarEditorItem * i = 0;
|
||
|
if ( currentIndex >= (int)itemList.count() ) {
|
||
|
i = createItem();
|
||
|
// do not put rename on cmd stack
|
||
|
RenameMenuCommand rename( i18n( "Rename Menu" ), formWnd, this, lineEdit->text(), i );
|
||
|
rename.execute();
|
||
|
} else {
|
||
|
i = itemList.at( currentIndex );
|
||
|
RenameMenuCommand * cmd =
|
||
|
new RenameMenuCommand( i18n( "Rename Menu" ), formWnd, this, lineEdit->text(), i );
|
||
|
formWnd->commandHistory()->addCommand( cmd );
|
||
|
cmd->execute();
|
||
|
}
|
||
|
showItem();
|
||
|
}
|
||
|
|
||
15 years ago
|
TQPixmap MenuBarEditor::createTextPixmap( const TQString &text )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQSize sz( fontMetrics().boundingRect( text ).size() );
|
||
|
TQPixmap pix( sz.width() + 20, sz.height() * 2 );
|
||
15 years ago
|
pix.fill( white );
|
||
15 years ago
|
TQPainter p( &pix, this );
|
||
15 years ago
|
p.drawText( 2, 0, pix.width(), pix.height(), 0, text );
|
||
|
p.end();
|
||
15 years ago
|
TQBitmap bm( pix.size() );
|
||
15 years ago
|
bm.fill( color0 );
|
||
|
p.begin( &bm );
|
||
|
p.setPen( color1 );
|
||
|
p.drawText( 2, 0, pix.width(), pix.height(), 0, text );
|
||
|
p.end();
|
||
|
pix.setMask( bm );
|
||
|
return pix;
|
||
|
}
|