kcoloractions.cpp
1/* This file is part of the KDE libraries
2 Copyright (C) 2000 Reginald Stadlbauer <reggie@kde.org>
3 Copyright (C) 2002 Werner Trobin <trobin@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
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 <kcoloractions.h>
21
22#include <tqpopupmenu.h>
23#include <tqwhatsthis.h>
24#include <tqtooltip.h>
25
26#include <tdeapplication.h>
27#include <tdetoolbar.h>
28#include <tdetoolbarbutton.h>
29#include <kdebug.h>
30
31KColorAction::KColorAction( const TQString& text, int accel,
32 TQObject* parent, const char* name )
33 : TDEAction( text, accel, parent, name )
34{
35 typ = TextColor;
36 init();
37}
38
39KColorAction::KColorAction( const TQString& text, int accel,
40 TQObject* receiver, const char* slot, TQObject* parent,
41 const char* name )
42 : TDEAction( text, accel, receiver, slot, parent, name )
43{
44 typ = TextColor;
45 init();
46}
47
48KColorAction::KColorAction( const TQString& text, Type type, int accel,
49 TQObject* parent, const char* name )
50 : TDEAction( text, accel, parent, name )
51{
52 typ = type;
53 init();
54}
55
56KColorAction::KColorAction( const TQString& text, Type type, int accel,
57 TQObject* receiver, const char* slot, TQObject* parent,
58 const char* name )
59 : TDEAction( text, accel, receiver, slot, parent, name )
60{
61 typ = type;
62 init();
63}
64
65KColorAction::KColorAction( TQObject* parent, const char* name )
66 : TDEAction( parent, name )
67{
68 typ = TextColor;
69 init();
70}
71
72void KColorAction::setColor( const TQColor &c )
73{
74 if ( c == col )
75 return;
76
77 col = c;
78 createPixmap();
79}
80
81TQColor KColorAction::color() const
82{
83 return col;
84}
85
86void KColorAction::setType( Type t )
87{
88 if ( t == typ )
89 return;
90
91 typ = t;
92 createPixmap();
93}
94
95KColorAction::Type KColorAction::type() const
96{
97 return typ;
98}
99
100void KColorAction::init()
101{
102 col = TQt::black;
103 createPixmap();
104}
105
106void KColorAction::createPixmap()
107{
108 int r, g, b;
109 TQCString pix;
110 TQCString line;
111
112 col.rgb( &r, &g, &b );
113
114 pix = "/* XPM */\n";
115
116 pix += "static char * text_xpm[] = {\n";
117
118 switch ( typ ) {
119 case TextColor: {
120 pix += "\"20 20 11 1\",\n";
121 pix += "\"h c #c0c000\",\n";
122 pix += "\"g c #808000\",\n";
123 pix += "\"f c #c0c0ff\",\n";
124 pix += "\"a c #000000\",\n";
125 pix += "\"d c #ff8000\",\n";
126 pix += "\". c none\",\n";
127 pix += "\"e c #0000c0\",\n";
128 pix += "\"i c #ffff00\",\n";
129 line.sprintf( "\"# c #%02X%02X%02X \",\n", r, g, b );
130 pix += line.copy();
131 pix += "\"b c #c00000\",\n";
132 pix += "\"c c #ff0000\",\n";
133 pix += "\"....................\",\n";
134 pix += "\"....................\",\n";
135 pix += "\"....................\",\n";
136 pix += "\"........#...........\",\n";
137 pix += "\"........#a..........\",\n";
138 pix += "\".......###..........\",\n";
139 pix += "\".......###a.........\",\n";
140 pix += "\"......##aa#.........\",\n";
141 pix += "\"......##a.#a........\",\n";
142 pix += "\".....##a...#........\",\n";
143 pix += "\".....#######a.......\",\n";
144 pix += "\"....##aaaaaa#.......\",\n";
145 pix += "\"....##a.....aaaaaaaa\",\n";
146 pix += "\"...####....#abbccdda\",\n";
147 pix += "\"....aaaa....abbccdda\",\n";
148 pix += "\"............aee##ffa\",\n";
149 pix += "\"............aee##ffa\",\n";
150 pix += "\"............agghhiia\",\n";
151 pix += "\"............agghhiia\",\n";
152 pix += "\"............aaaaaaaa\"};\n";
153 } break;
154 case FrameColor: {
155 pix += "\" 20 20 3 1 \",\n";
156
157 pix += "\" c none \",\n";
158 pix += "\"+ c white \",\n";
159 line.sprintf( "\". c #%02X%02X%02X \",\n", r, g, b );
160 pix += line.copy();
161
162 pix += "\" \",\n";
163 pix += "\" \",\n";
164 pix += "\" ................ \",\n";
165 pix += "\" ................ \",\n";
166 pix += "\" ................ \",\n";
167 pix += "\" ...++++++++++... \",\n";
168 pix += "\" ...++++++++++... \",\n";
169 pix += "\" ...++++++++++... \",\n";
170 pix += "\" ...++++++++++... \",\n";
171 pix += "\" ...++++++++++... \",\n";
172 pix += "\" ...++++++++++... \",\n";
173 pix += "\" ...++++++++++... \",\n";
174 pix += "\" ...++++++++++... \",\n";
175 pix += "\" ...++++++++++... \",\n";
176 pix += "\" ...++++++++++... \",\n";
177 pix += "\" ................ \",\n";
178 pix += "\" ................ \",\n";
179 pix += "\" ................ \",\n";
180 pix += "\" \",\n";
181 pix += "\" \";\n";
182 } break;
183 case BackgroundColor: {
184 pix += "\" 20 20 3 1 \",\n";
185
186 pix += "\" c none \",\n";
187 pix += "\". c red \",\n";
188 line.sprintf( "\"+ c #%02X%02X%02X \",\n", r, g, b );
189 pix += line.copy();
190
191 pix += "\" \",\n";
192 pix += "\" \",\n";
193 pix += "\" ................ \",\n";
194 pix += "\" ................ \",\n";
195 pix += "\" ..++++++++++++.. \",\n";
196 pix += "\" ..++++++++++++.. \",\n";
197 pix += "\" ..++++++++++++.. \",\n";
198 pix += "\" ..++++++++++++.. \",\n";
199 pix += "\" ..++++++++++++.. \",\n";
200 pix += "\" ..++++++++++++.. \",\n";
201 pix += "\" ..++++++++++++.. \",\n";
202 pix += "\" ..++++++++++++.. \",\n";
203 pix += "\" ..++++++++++++.. \",\n";
204 pix += "\" ..++++++++++++.. \",\n";
205 pix += "\" ..++++++++++++.. \",\n";
206 pix += "\" ..++++++++++++.. \",\n";
207 pix += "\" ................ \",\n";
208 pix += "\" ................ \",\n";
209 pix += "\" \",\n";
210 pix += "\" \";\n";
211 } break;
212 }
213
214 TQPixmap pixmap( pix );
215 setIconSet( TQIconSet( pixmap ) );
216}
217
218
219TDESelectColorAction::TDESelectColorAction( const TQString& text, Type type,
220 const TQObject* receiver, const char* slot,
221 TDEActionCollection* parent, const char* name ) :
222 TDEAction( text, TDEShortcut(), receiver, slot, parent, name ), m_type( type ),
223 m_color( TQt::black )
224{
225}
226
227TDESelectColorAction::~TDESelectColorAction()
228{
229}
230
231int TDESelectColorAction::plug( TQWidget* w, int index )
232{
233 if (w == 0) {
234 kdWarning() << "TDESelectColorAction::plug called with 0 argument\n";
235 return -1;
236 }
237 if (kapp && !kapp->authorizeTDEAction(name()))
238 return -1;
239
240 if ( w->inherits("TQPopupMenu") )
241 {
242 TQPopupMenu* menu = static_cast<TQPopupMenu*>( w );
243 int id;
244
245 if ( hasIcon() )
246 {
247 /* ###### CHECK: We're not allowed to specify the instance in iconSet()
248 TDEInstance *instance;
249 if ( parentCollection() )
250 instance = parentCollection()->instance();
251 else
252 instance = TDEGlobal::instance();
253 */
254 id = menu->insertItem( iconSet( TDEIcon::Small, 0 ), text(), this,//dsweet
255 TQ_SLOT( slotActivated() ), 0, -1, index );
256 }
257 else
258 id = menu->insertItem( text(), this, TQ_SLOT( slotActivated() ), //dsweet
259 0, -1, index );
260
261 updateShortcut( menu, id );
262
263 // call setItemEnabled only if the item really should be disabled,
264 // because that method is slow and the item is per default enabled
265 if ( !isEnabled() )
266 menu->setItemEnabled( id, false );
267
268 if ( !whatsThis().isEmpty() )
269 menu->setWhatsThis( id, whatsThisWithIcon() );
270
271 addContainer( menu, id );
272 connect( menu, TQ_SIGNAL( destroyed() ), this, TQ_SLOT( slotDestroyed() ) );
273
274 if ( parentCollection() )
275 parentCollection()->connectHighlight( menu, this );
276
277 return containerCount() - 1;
278 }
279 else if ( w->inherits( "TDEToolBar" ) )
280 {
281 TDEToolBar *bar = static_cast<TDEToolBar *>( w );
282
283 int id_ = getToolButtonID();
284 TDEInstance *instance;
285 if ( parentCollection() )
286 instance = parentCollection()->instance();
287 else
288 instance = TDEGlobal::instance();
289
290 if ( icon().isEmpty() ) // old code using TQIconSet directly
291 {
292 bar->insertButton( iconSet( TDEIcon::Small ).pixmap(), id_, TQ_SIGNAL( clicked() ), this,
293 TQ_SLOT( slotActivated() ),
294 isEnabled(), plainText(), index );
295 }
296 else
297 bar->insertButton( icon(), id_, TQ_SIGNAL( clicked() ), this,
298 TQ_SLOT( slotActivated() ),
299 isEnabled(), plainText(), index, instance );
300
301 bar->getButton( id_ )->setName( TQCString("toolbutton_")+name() );
302
303 if ( !whatsThis().isEmpty() )
304 TQWhatsThis::add( bar->getButton(id_), whatsThisWithIcon() );
305
306 if ( !toolTip().isEmpty() )
307 TQToolTip::add( bar->getButton(id_), toolTip() );
308
309 addContainer( bar, id_ );
310
311 connect( bar, TQ_SIGNAL( destroyed() ), this, TQ_SLOT( slotDestroyed() ) );
312
313 if ( parentCollection() )
314 parentCollection()->connectHighlight( bar, this );
315
316 return containerCount() - 1;
317 }
318
319 return -1;
320}
321
322TQColor TDESelectColorAction::color() const
323{
324 return m_color;
325}
326
327TDESelectColorAction::Type TDESelectColorAction::type() const
328{
329 return m_type;
330}
331
332void TDESelectColorAction::setColor( const TQColor &/*c*/ )
333{
334}
335
336void TDESelectColorAction::setType( Type /*t*/ )
337{
338}
339
340TQString TDESelectColorAction::whatsThisWithIcon() const
341{
342 TQString text = whatsThis();
343 if (!icon().isEmpty())
344 return TQString::fromLatin1("<img source=\"small|%1\"> %2").arg(icon()).arg(text);
345 return text;
346}
347
348#include <kcoloractions.moc>
virtual void setIconSet(const TQIconSet &iconSet)
int insertButton(const TQString &icon, int id, bool enabled=true, const TQString &text=TQString::null, int index=-1, TDEInstance *_instance=TDEGlobal::instance())
TDEToolBarButton * getButton(int id)
TDEAction * whatsThis(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)
const char * name(StdAction id)