• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeui
 

tdeui

  • tdeui
kxmlguibuilder.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2000 Simon Hausmann <hausmann@kde.org>
3 David Faure <faure@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 as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library 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 GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include "tdeapplication.h"
22#include "kxmlguibuilder.h"
23#include "tdemenubar.h"
24#include "tdepopupmenu.h"
25#include "tdetoolbar.h"
26#include "kstatusbar.h"
27#include "tdemainwindow.h"
28#include "tdeaction.h"
29#include "tdeglobalsettings.h"
30#include <tdelocale.h>
31#include <kiconloader.h>
32#include <kdebug.h>
33#include <tqobjectlist.h>
34
35class KXMLGUIBuilderPrivate
36{
37public:
38 KXMLGUIBuilderPrivate() {
39 }
40 ~KXMLGUIBuilderPrivate() {
41 }
42
43 TQWidget *m_widget;
44
45 TQString tagMainWindow;
46 TQString tagMenuBar;
47 TQString tagMenu;
48 TQString tagToolBar;
49 TQString tagStatusBar;
50
51 TQString tagSeparator;
52 TQString tagTearOffHandle;
53 TQString tagMenuTitle;
54
55 TQString attrName;
56 TQString attrLineSeparator;
57
58 TQString attrText1;
59 TQString attrText2;
60 TQString attrContext;
61
62 TQString attrIcon;
63
64 TDEInstance *m_instance;
65 KXMLGUIClient *m_client;
66};
67
68KXMLGUIBuilder::KXMLGUIBuilder( TQWidget *widget )
69{
70 d = new KXMLGUIBuilderPrivate;
71 d->m_widget = widget;
72
73 d->tagMainWindow = TQString::fromLatin1( "mainwindow" );
74 d->tagMenuBar = TQString::fromLatin1( "menubar" );
75 d->tagMenu = TQString::fromLatin1( "menu" );
76 d->tagToolBar = TQString::fromLatin1( "toolbar" );
77 d->tagStatusBar = TQString::fromLatin1( "statusbar" );
78
79 d->tagSeparator = TQString::fromLatin1( "separator" );
80 d->tagTearOffHandle = TQString::fromLatin1( "tearoffhandle" );
81 d->tagMenuTitle = TQString::fromLatin1( "title" );
82
83 d->attrName = TQString::fromLatin1( "name" );
84 d->attrLineSeparator = TQString::fromLatin1( "lineseparator" );
85
86 d->attrText1 = TQString::fromLatin1( "text" );
87 d->attrText2 = TQString::fromLatin1( "Text" );
88 d->attrContext = TQString::fromLatin1( "context" );
89
90 d->attrIcon = TQString::fromLatin1( "icon" );
91
92 d->m_instance = 0;
93 d->m_client = 0;
94}
95
96KXMLGUIBuilder::~KXMLGUIBuilder()
97{
98 delete d;
99}
100
101TQWidget *KXMLGUIBuilder::widget()
102{
103 return d->m_widget;
104}
105
106TQStringList KXMLGUIBuilder::containerTags() const
107{
108 TQStringList res;
109 res << d->tagMenu << d->tagToolBar << d->tagMainWindow << d->tagMenuBar << d->tagStatusBar;
110
111 return res;
112}
113
114TQWidget *KXMLGUIBuilder::createContainer( TQWidget *parent, int index, const TQDomElement &element, int &id )
115{
116 id = -1;
117 if ( element.tagName().lower() == d->tagMainWindow )
118 {
119 TDEMainWindow *mainwindow = 0;
120 if ( ::tqt_cast<TDEMainWindow *>( d->m_widget ) )
121 mainwindow = static_cast<TDEMainWindow *>(d->m_widget);
122
123 return mainwindow;
124 }
125
126 if ( element.tagName().lower() == d->tagMenuBar )
127 {
128 KMenuBar *bar;
129
130 if ( ::tqt_cast<TDEMainWindow *>( d->m_widget ) )
131 bar = static_cast<TDEMainWindow *>(d->m_widget)->menuBar();
132 else
133 bar = new KMenuBar( d->m_widget );
134
135 bar->show();
136 return bar;
137 }
138
139 if ( element.tagName().lower() == d->tagMenu )
140 {
141 // Look up to see if we are inside a mainwindow. If yes, then
142 // use it as parent widget (to get tdeaction to plug itself into the
143 // mainwindow). Don't use a popupmenu as parent widget, otherwise
144 // the popup won't be hidden if it is used as a standalone menu as well.
145 // And we don't want to set the parent for a standalone popupmenu,
146 // otherwise its shortcuts appear.
147 TQWidget* p = parent;
148 while ( p && !::tqt_cast<TDEMainWindow *>( p ) )
149 p = p->parentWidget();
150
151 TQCString name = element.attribute( d->attrName ).utf8();
152
153 if (!tdeApp->authorizeTDEAction(name))
154 return 0;
155
156 TDEPopupMenu *popup = new TDEPopupMenu( p, name);
157
158 TQString i18nText;
159 TQDomElement textElem = element.namedItem( d->attrText1 ).toElement();
160 if ( textElem.isNull() ) // try with capital T
161 textElem = element.namedItem( d->attrText2 ).toElement();
162 TQCString text = textElem.text().utf8();
163 TQCString context = textElem.attribute(d->attrContext).utf8();
164
165 if ( text.isEmpty() ) // still no luck
166 i18nText = i18n( "No text!" );
167 else if ( context.isEmpty() )
168 i18nText = i18n( text );
169 else
170 i18nText = i18n( context, text );
171
172 TQString icon = element.attribute( d->attrIcon );
173 TQIconSet pix;
174
175 if ( !icon.isEmpty() )
176 {
177 TDEInstance *instance = d->m_instance;
178 if ( !instance )
179 instance = TDEGlobal::instance();
180
181 pix = SmallIconSet( icon, 16, instance );
182 }
183
184 if ( parent && ::tqt_cast<KMenuBar *>( parent ) )
185 {
186 if ( !icon.isEmpty() )
187 id = static_cast<KMenuBar *>(parent)->insertItem( pix, i18nText, popup, -1, index );
188 else
189 id = static_cast<KMenuBar *>(parent)->insertItem( i18nText, popup, -1, index );
190 }
191 else if ( parent && ::tqt_cast<TQPopupMenu *>( parent ) )
192 {
193 if ( !icon.isEmpty() )
194 id = static_cast<TQPopupMenu *>(parent)->insertItem( pix, i18nText, popup, -1, index );
195 else
196 id = static_cast<TQPopupMenu *>(parent)->insertItem( i18nText, popup, -1, index );
197 }
198
199 return popup;
200 }
201
202 if ( element.tagName().lower() == d->tagToolBar )
203 {
204 bool honor = (element.attribute( d->attrName ) == "mainToolBar");
205
206 TQCString name = element.attribute( d->attrName ).utf8();
207
208 TDEToolBar *bar = static_cast<TDEToolBar*>(d->m_widget->child( name, "TDEToolBar" ));
209 if( !bar )
210 {
211 bar = new TDEToolBar( d->m_widget, name, honor, false );
212 }
213
214 if ( ::tqt_cast<TDEMainWindow *>( d->m_widget ) )
215 {
216 if ( d->m_client && !d->m_client->xmlFile().isEmpty() )
217 bar->setXMLGUIClient( d->m_client );
218 }
219
220 bar->loadState( element );
221
222 return bar;
223 }
224
225 if ( element.tagName().lower() == d->tagStatusBar )
226 {
227 if ( ::tqt_cast<TDEMainWindow *>( d->m_widget ) )
228 {
229 TDEMainWindow *mainWin = static_cast<TDEMainWindow *>(d->m_widget);
230 mainWin->statusBar()->show();
231 return mainWin->statusBar();
232 }
233 KStatusBar *bar = new KStatusBar( d->m_widget );
234 return bar;
235 }
236
237 return 0L;
238}
239
240void KXMLGUIBuilder::removeContainer( TQWidget *container, TQWidget *parent, TQDomElement &element, int id )
241{
242 // Warning parent can be 0L
243
244 if ( ::tqt_cast<TQPopupMenu *>( container ) )
245 {
246 if ( parent )
247 {
248 if ( ::tqt_cast<KMenuBar *>( parent ) )
249 static_cast<KMenuBar *>(parent)->removeItem( id );
250 else if ( ::tqt_cast<TQPopupMenu *>( parent ) )
251 static_cast<TQPopupMenu *>(parent)->removeItem( id );
252 }
253
254 delete container;
255 }
256 else if ( ::tqt_cast<TDEToolBar *>( container ) )
257 {
258 TDEToolBar *tb = static_cast<TDEToolBar *>( container );
259
260 tb->saveState( element );
261 delete tb;
262 }
263 else if ( ::tqt_cast<KMenuBar *>( container ) )
264 {
265 KMenuBar *mb = static_cast<KMenuBar *>( container );
266 mb->hide();
267 // Don't delete menubar - it can be reused by createContainer.
268 // If you decide that you do need to delete the menubar, make
269 // sure that TQMainWindow::d->mb does not point to a deleted
270 // menubar object.
271 }
272 else if ( ::tqt_cast<KStatusBar *>( container ) )
273 {
274 if ( ::tqt_cast<TDEMainWindow *>( d->m_widget ) )
275 container->hide();
276 else
277 delete static_cast<KStatusBar *>(container);
278 }
279 else
280 kdWarning() << "Unhandled container to remove : " << container->className() << endl;
281}
282
283TQStringList KXMLGUIBuilder::customTags() const
284{
285 TQStringList res;
286 res << d->tagSeparator << d->tagTearOffHandle << d->tagMenuTitle;
287 return res;
288}
289
290int KXMLGUIBuilder::createCustomElement( TQWidget *parent, int index, const TQDomElement &element )
291{
292 if ( element.tagName().lower() == d->tagSeparator )
293 {
294 if ( ::tqt_cast<TQPopupMenu *>( parent ) )
295 {
296 // Don't insert multiple separators in a row
297 TQPopupMenu *menu = static_cast<TQPopupMenu *>(parent);
298 int count = menu->count();
299 if (count)
300 {
301 int previousId = -1;
302 if ((index == -1) || (index > count))
303 previousId = menu->idAt(count-1);
304 else if (index > 0)
305 previousId = menu->idAt(index-1);
306 if (previousId != -1)
307 {
308 if (menu->text(previousId).isEmpty() &&
309 !menu->iconSet(previousId) &&
310 !menu->pixmap(previousId))
311 return 0;
312 }
313 }
314 // Don't insert a separator at the top of the menu
315 if(count == 0)
316 return 0;
317 else
318 return menu->insertSeparator( index );
319 }
320 else if ( ::tqt_cast<TQMenuBar *>( parent ) )
321 return static_cast<TQMenuBar *>(parent)->insertSeparator( index );
322 else if ( ::tqt_cast<TDEToolBar *>( parent ) )
323 {
324 TDEToolBar *bar = static_cast<TDEToolBar *>( parent );
325
326 bool isLineSep = true;
327
328 TQDomNamedNodeMap attributes = element.attributes();
329 unsigned int i = 0;
330 for (; i < attributes.length(); i++ )
331 {
332 TQDomAttr attr = attributes.item( i ).toAttr();
333
334 if ( attr.name().lower() == d->attrLineSeparator &&
335 attr.value().lower() == TQString::fromLatin1("false") )
336 {
337 isLineSep = false;
338 break;
339 }
340 }
341
342 int id = TDEAction::getToolButtonID();
343
344 if ( isLineSep )
345 bar->insertLineSeparator( index, id );
346 else
347 bar->insertSeparator( index, id );
348
349 return id;
350 }
351 }
352 else if ( element.tagName().lower() == d->tagTearOffHandle )
353 {
354 if ( ::tqt_cast<TQPopupMenu *>( parent ) && TDEGlobalSettings::insertTearOffHandle())
355 return static_cast<TQPopupMenu *>(parent)->insertTearOffHandle( -1, index );
356 }
357 else if ( element.tagName().lower() == d->tagMenuTitle )
358 {
359 if ( ::tqt_cast<TDEPopupMenu *>( parent ) )
360 {
361 TQString i18nText;
362 TQCString text = element.text().utf8();
363
364 if ( text.isEmpty() )
365 i18nText = i18n( "No text!" );
366 else
367 i18nText = i18n( text );
368
369 TQString icon = element.attribute( d->attrIcon );
370 TQPixmap pix;
371
372 if ( !icon.isEmpty() )
373 {
374 TDEInstance *instance = d->m_instance;
375 if ( !instance )
376 instance = TDEGlobal::instance();
377
378 pix = SmallIcon( icon, instance );
379 }
380
381 if ( !icon.isEmpty() )
382 return static_cast<TDEPopupMenu *>(parent)->insertTitle( pix, i18nText, -1, index );
383 else
384 return static_cast<TDEPopupMenu *>(parent)->insertTitle( i18nText, -1, index );
385 }
386 }
387 return 0;
388}
389
390void KXMLGUIBuilder::removeCustomElement( TQWidget *parent, int id )
391{
392 if ( ::tqt_cast<TQPopupMenu *>( parent ) )
393 static_cast<TQPopupMenu *>(parent)->removeItem( id );
394 else if ( ::tqt_cast<TQMenuBar *>( parent ) )
395 static_cast<TQMenuBar *>(parent)->removeItem( id );
396 else if ( ::tqt_cast<TDEToolBar *>( parent ) )
397 static_cast<TDEToolBar *>(parent)->removeItemDelayed( id );
398}
399
400KXMLGUIClient *KXMLGUIBuilder::builderClient() const
401{
402 return d->m_client;
403}
404
405void KXMLGUIBuilder::setBuilderClient( KXMLGUIClient *client )
406{
407 d->m_client = client;
408 if ( client )
409 setBuilderInstance( client->instance() );
410}
411
412TDEInstance *KXMLGUIBuilder::builderInstance() const
413{
414 return d->m_instance;
415}
416
417void KXMLGUIBuilder::setBuilderInstance( TDEInstance *instance )
418{
419 d->m_instance = instance;
420}
421
422void KXMLGUIBuilder::finalizeGUI( KXMLGUIClient * )
423{
424 if ( !d->m_widget || !::tqt_cast<TDEMainWindow *>( d->m_widget ) )
425 return;
426#if 0
427 TDEToolBar *toolbar = 0;
428 TQPtrListIterator<TDEToolBar> it( ( (TDEMainWindow*)d->m_widget )->toolBarIterator() );
429 while ( ( toolbar = it.current() ) ) {
430 kdDebug() << "KXMLGUIBuilder::finalizeGUI toolbar=" << (void*)toolbar << endl;
431 ++it;
432 toolbar->positionYourself();
433 }
434#else
435 static_cast<TDEMainWindow *>(d->m_widget)->finalizeGUI( false );
436#endif
437}
438
439void KXMLGUIBuilder::virtual_hook( int, void* )
440{ /*BASE::virtual_hook( id, data );*/ }
441
KMenuBar
KDE Style-able menubar.
Definition: tdemenubar.h:43
KStatusBar
KDE statusbar widget
Definition: kstatusbar.h:88
KXMLGUIBuilder::removeContainer
virtual void removeContainer(TQWidget *container, TQWidget *parent, TQDomElement &element, int id)
Removes the given (and previously via createContainer ) created container.
Definition: kxmlguibuilder.cpp:240
KXMLGUIBuilder::createContainer
virtual TQWidget * createContainer(TQWidget *parent, int index, const TQDomElement &element, int &id)
Creates a container (menubar/menu/toolbar/statusbar/separator/...) from an element in the XML file.
Definition: kxmlguibuilder.cpp:114
KXMLGUIClient
A KXMLGUIClient can be used with KXMLGUIFactory to create a GUI from actions and an XML document,...
Definition: kxmlguiclient.h:44
KXMLGUIClient::instance
virtual TDEInstance * instance() const
Definition: kxmlguiclient.cpp:123
TDEAction::getToolButtonID
static int getToolButtonID()
How it works.
Definition: tdeaction.cpp:76
TDEGlobalSettings::insertTearOffHandle
static TearOffHandle insertTearOffHandle()
TDEGlobal::instance
static TDEInstance * instance()
TDEInstance
TDEMainWindow
KDE top level main window
Definition: tdemainwindow.h:99
TDEMainWindow::menuBar
KMenuBar * menuBar()
Returns a pointer to the menu bar.
Definition: tdemainwindow.cpp:1090
TDEMainWindow::statusBar
KStatusBar * statusBar()
Returns a pointer to the status bar.
Definition: tdemainwindow.cpp:1102
TDEPopupMenu
A menu with title items.
Definition: tdepopupmenu.h:123
TDEToolBar
Floatable toolbar with auto resize.
Definition: tdetoolbar.h:105
TDEToolBar::insertLineSeparator
int insertLineSeparator(int index=-1, int id=-1)
Inserts a line separator into the toolbar with the given id.
Definition: tdetoolbar.cpp:403
TDEToolBar::positionYourself
void positionYourself(bool force=false)
Definition: tdetoolbar.cpp:2011
TDEToolBar::insertSeparator
int insertSeparator(int index=-1, int id=-1)
Inserts a separator into the toolbar with the given id.
Definition: tdetoolbar.cpp:396
TDEToolBar::loadState
void loadState(const TQDomElement &e)
Load state from an XML element, called by KXMLGUIBuilder.
Definition: tdetoolbar.cpp:1779
TDEToolBar::removeItemDelayed
void removeItemDelayed(int id)
Remove item id.
Definition: tdetoolbar.cpp:696
TDEToolBar::saveState
void saveState()
Instruct the toolbar to save it's current state to either the app config file or to the XML-GUI resou...
Definition: tdetoolbar.cpp:962
kdWarning
kdbgstream kdWarning(int area=0)
endl
kndbgstream & endl(kndbgstream &s)
kdDebug
kdbgstream kdDebug(int area=0)
KNotifyClient::instance
TDEInstance * instance()
tdelocale.h

tdeui

Skip menu "tdeui"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeui

Skip menu "tdeui"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeui by doxygen 1.9.4
This website is maintained by Timothy Pearson.