00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "tdeapplication.h"
00022 #include "kxmlguibuilder.h"
00023 #include "tdemenubar.h"
00024 #include "tdepopupmenu.h"
00025 #include "tdetoolbar.h"
00026 #include "kstatusbar.h"
00027 #include "tdemainwindow.h"
00028 #include "tdeaction.h"
00029 #include "tdeglobalsettings.h"
00030 #include <tdelocale.h>
00031 #include <kiconloader.h>
00032 #include <kdebug.h>
00033 #include <tqobjectlist.h>
00034
00035 class KXMLGUIBuilderPrivate
00036 {
00037 public:
00038 KXMLGUIBuilderPrivate() {
00039 }
00040 ~KXMLGUIBuilderPrivate() {
00041 }
00042
00043 TQWidget *m_widget;
00044
00045 TQString tagMainWindow;
00046 TQString tagMenuBar;
00047 TQString tagMenu;
00048 TQString tagToolBar;
00049 TQString tagStatusBar;
00050
00051 TQString tagSeparator;
00052 TQString tagTearOffHandle;
00053 TQString tagMenuTitle;
00054
00055 TQString attrName;
00056 TQString attrLineSeparator;
00057
00058 TQString attrText1;
00059 TQString attrText2;
00060 TQString attrContext;
00061
00062 TQString attrIcon;
00063
00064 TDEInstance *m_instance;
00065 KXMLGUIClient *m_client;
00066 };
00067
00068 KXMLGUIBuilder::KXMLGUIBuilder( TQWidget *widget )
00069 {
00070 d = new KXMLGUIBuilderPrivate;
00071 d->m_widget = widget;
00072
00073 d->tagMainWindow = TQString::fromLatin1( "mainwindow" );
00074 d->tagMenuBar = TQString::fromLatin1( "menubar" );
00075 d->tagMenu = TQString::fromLatin1( "menu" );
00076 d->tagToolBar = TQString::fromLatin1( "toolbar" );
00077 d->tagStatusBar = TQString::fromLatin1( "statusbar" );
00078
00079 d->tagSeparator = TQString::fromLatin1( "separator" );
00080 d->tagTearOffHandle = TQString::fromLatin1( "tearoffhandle" );
00081 d->tagMenuTitle = TQString::fromLatin1( "title" );
00082
00083 d->attrName = TQString::fromLatin1( "name" );
00084 d->attrLineSeparator = TQString::fromLatin1( "lineseparator" );
00085
00086 d->attrText1 = TQString::fromLatin1( "text" );
00087 d->attrText2 = TQString::fromLatin1( "Text" );
00088 d->attrContext = TQString::fromLatin1( "context" );
00089
00090 d->attrIcon = TQString::fromLatin1( "icon" );
00091
00092 d->m_instance = 0;
00093 d->m_client = 0;
00094 }
00095
00096 KXMLGUIBuilder::~KXMLGUIBuilder()
00097 {
00098 delete d;
00099 }
00100
00101 TQWidget *KXMLGUIBuilder::widget()
00102 {
00103 return d->m_widget;
00104 }
00105
00106 TQStringList KXMLGUIBuilder::containerTags() const
00107 {
00108 TQStringList res;
00109 res << d->tagMenu << d->tagToolBar << d->tagMainWindow << d->tagMenuBar << d->tagStatusBar;
00110
00111 return res;
00112 }
00113
00114 TQWidget *KXMLGUIBuilder::createContainer( TQWidget *parent, int index, const TQDomElement &element, int &id )
00115 {
00116 id = -1;
00117 if ( element.tagName().lower() == d->tagMainWindow )
00118 {
00119 TDEMainWindow *mainwindow = 0;
00120 if ( ::tqqt_cast<TDEMainWindow *>( d->m_widget ) )
00121 mainwindow = static_cast<TDEMainWindow *>(d->m_widget);
00122
00123 return mainwindow;
00124 }
00125
00126 if ( element.tagName().lower() == d->tagMenuBar )
00127 {
00128 KMenuBar *bar;
00129
00130 if ( ::tqqt_cast<TDEMainWindow *>( d->m_widget ) )
00131 bar = static_cast<TDEMainWindow *>(d->m_widget)->menuBar();
00132 else
00133 bar = new KMenuBar( d->m_widget );
00134
00135 bar->show();
00136 return bar;
00137 }
00138
00139 if ( element.tagName().lower() == d->tagMenu )
00140 {
00141
00142
00143
00144
00145
00146
00147 TQWidget* p = parent;
00148 while ( p && !::tqqt_cast<TDEMainWindow *>( p ) )
00149 p = p->parentWidget();
00150
00151 TQCString name = element.attribute( d->attrName ).utf8();
00152
00153 if (!kapp->authorizeTDEAction(name))
00154 return 0;
00155
00156 TDEPopupMenu *popup = new TDEPopupMenu( p, name);
00157
00158 TQString i18nText;
00159 TQDomElement textElem = element.namedItem( d->attrText1 ).toElement();
00160 if ( textElem.isNull() )
00161 textElem = element.namedItem( d->attrText2 ).toElement();
00162 TQCString text = textElem.text().utf8();
00163 TQCString context = textElem.attribute(d->attrContext).utf8();
00164
00165 if ( text.isEmpty() )
00166 i18nText = i18n( "No text!" );
00167 else if ( context.isEmpty() )
00168 i18nText = i18n( text );
00169 else
00170 i18nText = i18n( context, text );
00171
00172 TQString icon = element.attribute( d->attrIcon );
00173 TQIconSet pix;
00174
00175 if ( !icon.isEmpty() )
00176 {
00177 TDEInstance *instance = d->m_instance;
00178 if ( !instance )
00179 instance = TDEGlobal::instance();
00180
00181 pix = SmallIconSet( icon, 16, instance );
00182 }
00183
00184 if ( parent && ::tqqt_cast<KMenuBar *>( parent ) )
00185 {
00186 if ( !icon.isEmpty() )
00187 id = static_cast<KMenuBar *>(parent)->insertItem( pix, i18nText, popup, -1, index );
00188 else
00189 id = static_cast<KMenuBar *>(parent)->insertItem( i18nText, popup, -1, index );
00190 }
00191 else if ( parent && ::tqqt_cast<TQPopupMenu *>( parent ) )
00192 {
00193 if ( !icon.isEmpty() )
00194 id = static_cast<TQPopupMenu *>(parent)->insertItem( pix, i18nText, popup, -1, index );
00195 else
00196 id = static_cast<TQPopupMenu *>(parent)->insertItem( i18nText, popup, -1, index );
00197 }
00198
00199 return popup;
00200 }
00201
00202 if ( element.tagName().lower() == d->tagToolBar )
00203 {
00204 bool honor = (element.attribute( d->attrName ) == "mainToolBar");
00205
00206 TQCString name = element.attribute( d->attrName ).utf8();
00207
00208 TDEToolBar *bar = static_cast<TDEToolBar*>(TQT_TQWIDGET(d->m_widget->child( name, "TDEToolBar" )));
00209 if( !bar )
00210 {
00211 bar = new TDEToolBar( d->m_widget, name, honor, false );
00212 }
00213
00214 if ( ::tqqt_cast<TDEMainWindow *>( d->m_widget ) )
00215 {
00216 if ( d->m_client && !d->m_client->xmlFile().isEmpty() )
00217 bar->setXMLGUIClient( d->m_client );
00218 }
00219
00220 bar->loadState( element );
00221
00222 return bar;
00223 }
00224
00225 if ( element.tagName().lower() == d->tagStatusBar )
00226 {
00227 if ( ::tqqt_cast<TDEMainWindow *>( d->m_widget ) )
00228 {
00229 TDEMainWindow *mainWin = static_cast<TDEMainWindow *>(d->m_widget);
00230 mainWin->statusBar()->show();
00231 return mainWin->statusBar();
00232 }
00233 KStatusBar *bar = new KStatusBar( d->m_widget );
00234 return bar;
00235 }
00236
00237 return 0L;
00238 }
00239
00240 void KXMLGUIBuilder::removeContainer( TQWidget *container, TQWidget *parent, TQDomElement &element, int id )
00241 {
00242
00243
00244 if ( ::tqqt_cast<TQPopupMenu *>( container ) )
00245 {
00246 if ( parent )
00247 {
00248 if ( ::tqqt_cast<KMenuBar *>( parent ) )
00249 static_cast<KMenuBar *>(parent)->removeItem( id );
00250 else if ( ::tqqt_cast<TQPopupMenu *>( parent ) )
00251 static_cast<TQPopupMenu *>(parent)->removeItem( id );
00252 }
00253
00254 delete container;
00255 }
00256 else if ( ::tqqt_cast<TDEToolBar *>( container ) )
00257 {
00258 TDEToolBar *tb = static_cast<TDEToolBar *>( container );
00259
00260 tb->saveState( element );
00261 delete tb;
00262 }
00263 else if ( ::tqqt_cast<KMenuBar *>( container ) )
00264 {
00265 KMenuBar *mb = static_cast<KMenuBar *>( container );
00266 mb->hide();
00267
00268
00269
00270
00271 }
00272 else if ( ::tqqt_cast<KStatusBar *>( container ) )
00273 {
00274 if ( ::tqqt_cast<TDEMainWindow *>( d->m_widget ) )
00275 container->hide();
00276 else
00277 delete static_cast<KStatusBar *>(container);
00278 }
00279 else
00280 kdWarning() << "Unhandled container to remove : " << container->className() << endl;
00281 }
00282
00283 TQStringList KXMLGUIBuilder::customTags() const
00284 {
00285 TQStringList res;
00286 res << d->tagSeparator << d->tagTearOffHandle << d->tagMenuTitle;
00287 return res;
00288 }
00289
00290 int KXMLGUIBuilder::createCustomElement( TQWidget *parent, int index, const TQDomElement &element )
00291 {
00292 if ( element.tagName().lower() == d->tagSeparator )
00293 {
00294 if ( ::tqqt_cast<TQPopupMenu *>( parent ) )
00295 {
00296
00297 TQPopupMenu *menu = static_cast<TQPopupMenu *>(parent);
00298 int count = menu->count();
00299 if (count)
00300 {
00301 int previousId = -1;
00302 if ((index == -1) || (index > count))
00303 previousId = menu->idAt(count-1);
00304 else if (index > 0)
00305 previousId = menu->idAt(index-1);
00306 if (previousId != -1)
00307 {
00308 if (menu->text(previousId).isEmpty() &&
00309 !menu->iconSet(previousId) &&
00310 !menu->pixmap(previousId))
00311 return 0;
00312 }
00313 }
00314
00315 if(count == 0)
00316 return 0;
00317 else
00318 return menu->insertSeparator( index );
00319 }
00320 else if ( ::tqqt_cast<TQMenuBar *>( parent ) )
00321 return static_cast<TQMenuBar *>(parent)->insertSeparator( index );
00322 else if ( ::tqqt_cast<TDEToolBar *>( parent ) )
00323 {
00324 TDEToolBar *bar = static_cast<TDEToolBar *>( parent );
00325
00326 bool isLineSep = true;
00327
00328 TQDomNamedNodeMap attributes = element.attributes();
00329 unsigned int i = 0;
00330 for (; i < attributes.length(); i++ )
00331 {
00332 TQDomAttr attr = attributes.item( i ).toAttr();
00333
00334 if ( attr.name().lower() == d->attrLineSeparator &&
00335 attr.value().lower() == TQString::fromLatin1("false") )
00336 {
00337 isLineSep = false;
00338 break;
00339 }
00340 }
00341
00342 int id = TDEAction::getToolButtonID();
00343
00344 if ( isLineSep )
00345 bar->insertLineSeparator( index, id );
00346 else
00347 bar->insertSeparator( index, id );
00348
00349 return id;
00350 }
00351 }
00352 else if ( element.tagName().lower() == d->tagTearOffHandle )
00353 {
00354 if ( ::tqqt_cast<TQPopupMenu *>( parent ) && TDEGlobalSettings::insertTearOffHandle())
00355 return static_cast<TQPopupMenu *>(parent)->insertTearOffHandle( -1, index );
00356 }
00357 else if ( element.tagName().lower() == d->tagMenuTitle )
00358 {
00359 if ( ::tqqt_cast<TDEPopupMenu *>( parent ) )
00360 {
00361 TQString i18nText;
00362 TQCString text = element.text().utf8();
00363
00364 if ( text.isEmpty() )
00365 i18nText = i18n( "No text!" );
00366 else
00367 i18nText = i18n( text );
00368
00369 TQString icon = element.attribute( d->attrIcon );
00370 TQPixmap pix;
00371
00372 if ( !icon.isEmpty() )
00373 {
00374 TDEInstance *instance = d->m_instance;
00375 if ( !instance )
00376 instance = TDEGlobal::instance();
00377
00378 pix = SmallIcon( icon, instance );
00379 }
00380
00381 if ( !icon.isEmpty() )
00382 return static_cast<TDEPopupMenu *>(parent)->insertTitle( pix, i18nText, -1, index );
00383 else
00384 return static_cast<TDEPopupMenu *>(parent)->insertTitle( i18nText, -1, index );
00385 }
00386 }
00387 return 0;
00388 }
00389
00390 void KXMLGUIBuilder::removeCustomElement( TQWidget *parent, int id )
00391 {
00392 if ( ::tqqt_cast<TQPopupMenu *>( parent ) )
00393 static_cast<TQPopupMenu *>(parent)->removeItem( id );
00394 else if ( ::tqqt_cast<TQMenuBar *>( parent ) )
00395 static_cast<TQMenuBar *>(parent)->removeItem( id );
00396 else if ( ::tqqt_cast<TDEToolBar *>( parent ) )
00397 static_cast<TDEToolBar *>(parent)->removeItemDelayed( id );
00398 }
00399
00400 KXMLGUIClient *KXMLGUIBuilder::builderClient() const
00401 {
00402 return d->m_client;
00403 }
00404
00405 void KXMLGUIBuilder::setBuilderClient( KXMLGUIClient *client )
00406 {
00407 d->m_client = client;
00408 if ( client )
00409 setBuilderInstance( client->instance() );
00410 }
00411
00412 TDEInstance *KXMLGUIBuilder::builderInstance() const
00413 {
00414 return d->m_instance;
00415 }
00416
00417 void KXMLGUIBuilder::setBuilderInstance( TDEInstance *instance )
00418 {
00419 d->m_instance = instance;
00420 }
00421
00422 void KXMLGUIBuilder::finalizeGUI( KXMLGUIClient * )
00423 {
00424 if ( !d->m_widget || !::tqqt_cast<TDEMainWindow *>( d->m_widget ) )
00425 return;
00426 #if 0
00427 TDEToolBar *toolbar = 0;
00428 QListIterator<TDEToolBar> it( ( (TDEMainWindow*)d->m_widget )->toolBarIterator() );
00429 while ( ( toolbar = it.current() ) ) {
00430 kdDebug() << "KXMLGUIBuilder::finalizeGUI toolbar=" << (void*)toolbar << endl;
00431 ++it;
00432 toolbar->positionYourself();
00433 }
00434 #else
00435 static_cast<TDEMainWindow *>(d->m_widget)->finalizeGUI( false );
00436 #endif
00437 }
00438
00439 void KXMLGUIBuilder::virtual_hook( int, void* )
00440 { }
00441