lib

symbolaction.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002 Heinrich Kuettler <heinrich.kuettler@gmx.de>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include <tqlistbox.h>
21#include <tqpainter.h>
22
23#include <tdeapplication.h>
24#include <kcombobox.h>
25#include <tdeglobalsettings.h>
26#include <tdetoolbar.h>
27#include <kdebug.h>
28
29#include "symbolaction.h"
30
31/*
32 * The items for the SymbolCombos. *
33 */
34
35KFORMULA_NAMESPACE_BEGIN
36
37class SymbolComboItem : public TQListBoxItem
38{
39public:
40 SymbolComboItem( const TQString&, const TQFont& , TQChar, TQComboBox* combo );
41 virtual ~SymbolComboItem();
42
43 virtual int width( const TQListBox* ) const;
44 virtual int height( const TQListBox* ) const;
45
46protected:
47 virtual void paint( TQPainter *p );
48
49private:
50 TQComboBox *m_combo;
51 TQString m_name;
52 TQFont m_font;
53 TQChar m_symbol;
54
55 static int widest;
56};
57
58int SymbolComboItem::widest = 0;
59
60SymbolComboItem::SymbolComboItem( const TQString &name, const TQFont& font,
61 TQChar symbol, TQComboBox *combo )
62 : TQListBoxItem( combo->listBox() ),
63 m_combo( combo ),
64 m_name( name ),
65 m_font( font ),
66 m_symbol( symbol )
67{
68 setText( name );
69 int charWidth = TQFontMetrics( m_font ).width( TQChar( m_symbol ) );
70 widest = TQMAX( widest, charWidth );
71}
72
73SymbolComboItem::~SymbolComboItem()
74{
75}
76
77int SymbolComboItem::width( const TQListBox * /*lb*/ ) const
78{
79 return widest + TQFontMetrics( TDEGlobalSettings::generalFont() ).width( text() ) + 12;
80}
81
82int SymbolComboItem::height( const TQListBox * /*lb*/ ) const
83{
84 int generalHeight = TQFontMetrics( TDEGlobalSettings::generalFont() ).lineSpacing();
85 int fontHeight = TQFontMetrics( m_font ).lineSpacing();
86 return TQMAX( generalHeight, fontHeight ) + 2;
87}
88
89void SymbolComboItem::paint( TQPainter *p )
90{
91 p->setFont( m_font );
92 TQFontMetrics fm( p->fontMetrics() );
93 p->drawText( 3, fm.ascent() + fm.leading() / 2,
94 TQString( "%1" ).arg( TQChar( m_symbol ) ) );
95
96 p->setFont( TDEGlobalSettings::generalFont() );
97 fm = p->fontMetrics();
98 p->drawText( widest + 6, height( m_combo->listBox() ) / 2 + fm.strikeOutPos(), m_name );
99}
100
101/*
102 * The symbol action *
103 */
104SymbolAction::SymbolAction( TQObject* parent, const char* name )
105 : TDESelectAction( parent, name )
106{
107 setEditable( false );
108}
109
110SymbolAction::SymbolAction( const TQString& text, const TDEShortcut& cut,
111 const TQObject* receiver, const char* slot,
112 TQObject* parent, const char* name )
113 : TDESelectAction( text, cut, receiver, slot, parent, name )
114{
115 setEditable( false );
116}
117
118int SymbolAction::plug( TQWidget* w, int index )
119{
120 if (kapp && !kapp->authorizeTDEAction(name()))
121 return -1;
122 if ( w->inherits( "TDEToolBar" ) )
123 {
124 TDEToolBar* bar = static_cast<TDEToolBar*>( w );
125 int id_ = TDEAction::getToolButtonID();
126 KComboBox *cb = new KComboBox( bar );
127 connect( cb, TQ_SIGNAL( activated( const TQString & ) ),
128 TQ_SLOT( slotActivated( const TQString & ) ) );
129 cb->setEnabled( isEnabled() );
130 bar->insertWidget( id_, comboWidth(), cb, index );
131 cb->setMinimumWidth( cb->sizeHint().width() );
132
133 addContainer( bar, id_ );
134
135 connect( bar, TQ_SIGNAL( destroyed() ), this, TQ_SLOT( slotDestroyed() ) );
136
137 updateItems( containerCount() - 1 );
138
139 return containerCount() - 1;
140 }
141 else return TDESelectAction::plug( w, index );
142}
143
144void SymbolAction::setSymbols( const TQStringList &names,
145 const TQFont& font,
146 const TQMemArray<TQChar>& chars )
147{
148 m_chars = chars;
149 m_font = font;
150 setItems( names );
151
152 int len = containerCount();
153 for ( int i = 0; i < len; ++i )
154 updateItems( i );
155}
156
157void SymbolAction::updateItems( int id )
158{
159 TQWidget *w = container( id );
160 if ( w->inherits( "TDEToolBar" ) ) {
161 TQWidget *r = static_cast<TDEToolBar*>( w )->getWidget( itemId( id ) );
162 if ( r->inherits( "TQComboBox" ) ) {
163 TQComboBox *cb = static_cast<TQComboBox*>( r );
164 cb->clear();
165
166 for( uint i = 0; i < items().count(); ++i ) {
167 new SymbolComboItem( *items().at( i ), m_font, m_chars.at( i ), cb );
168 }
169 cb->setMinimumWidth( cb->sizeHint().width() );
170 }
171 }
172}
173
174KFORMULA_NAMESPACE_END
virtual bool isEnabled() const
void activated()
static int getToolButtonID()
virtual int comboWidth() const
virtual TQStringList items() const
virtual void setItems(const TQStringList &lst)
virtual int plug(TQWidget *widget, int index=-1)
int insertWidget(int id, int width, TQWidget *_widget, int index=-1)
TDEAction * cut(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)
const char * name(StdAction id)