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

kdeui

  • kdeui
kmainwindow.cpp
1  /* This file is part of the KDE libraries
2  Copyright
3  (C) 2000 Reginald Stadlbauer (reggie@kde.org)
4  (C) 1997 Stephan Kulow (coolo@kde.org)
5  (C) 1997-2000 Sven Radej (radej@kde.org)
6  (C) 1997-2000 Matthias Ettrich (ettrich@kde.org)
7  (C) 1999 Chris Schlaeger (cs@kde.org)
8  (C) 2002 Joseph Wenninger (jowenn@kde.org)
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU Library General Public
12  License version 2 as published by the Free Software Foundation.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Library General Public License for more details.
18 
19  You should have received a copy of the GNU Library General Public License
20  along with this library; see the file COPYING.LIB. If not, write to
21  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  Boston, MA 02110-1301, USA.
23  */
24 #include "config.h"
25 
26 #include "kmainwindow.h"
27 #include "kmainwindowiface.h"
28 #include "ktoolbarhandler.h"
29 #include "kwhatsthismanager_p.h"
30 #include <tqsessionmanager.h>
31 #include <tqobjectlist.h>
32 #include <tqstyle.h>
33 #include <tqlayout.h>
34 #include <tqwidgetlist.h>
35 #include <tqtimer.h>
36 
37 #include <kaccel.h>
38 #include <kaction.h>
39 #include <kapplication.h>
40 #include <kconfig.h>
41 #include <kdebug.h>
42 #include <khelpmenu.h>
43 #include <kmenubar.h>
44 #include <kstatusbar.h>
45 #include <kwin.h>
46 #include <kedittoolbar.h>
47 #include <kmainwindow.h>
48 
49 #include <klocale.h>
50 #include <kstandarddirs.h>
51 #include <kstaticdeleter.h>
52 #if defined Q_WS_X11
53 #include <netwm.h>
54 #endif
55 
56 #include <stdlib.h>
57 #include <ctype.h>
58 #include <assert.h>
59 
60 class KMainWindowPrivate {
61 public:
62  bool showHelpMenu:1;
63 
64  bool autoSaveSettings:1;
65  bool settingsDirty:1;
66  bool autoSaveWindowSize:1;
67  bool care_about_geometry:1;
68  bool shuttingDown:1;
69  TQString autoSaveGroup;
70  KAccel * kaccel;
71  KMainWindowInterface *m_interface;
72  KDEPrivate::ToolBarHandler *toolBarHandler;
73  TQTimer* settingsTimer;
74  KToggleAction *showStatusBarAction;
75  TQRect defaultWindowSize;
76  TQPtrList<TQDockWindow> hiddenDockWindows;
77 };
78 
79 TQPtrList<KMainWindow>* KMainWindow::memberList = 0L;
80 static bool no_query_exit = false;
81 static KMWSessionManaged* ksm = 0;
82 static KStaticDeleter<KMWSessionManaged> ksmd;
83 
84 class KMWSessionManaged : public KSessionManaged
85 {
86 public:
87  KMWSessionManaged()
88  {
89  }
90  ~KMWSessionManaged()
91  {
92  }
93  bool saveState( QSessionManager& )
94  {
95  KConfig* config = KApplication::kApplication()->sessionConfig();
96  if ( KMainWindow::memberList->first() ){
97  // According to Jochen Wilhelmy <digisnap@cs.tu-berlin.de>, this
98  // hook is useful for better document orientation
99  KMainWindow::memberList->first()->saveGlobalProperties(config);
100  }
101 
102  TQPtrListIterator<KMainWindow> it(*KMainWindow::memberList);
103  int n = 0;
104  for (it.toFirst(); it.current(); ++it){
105  n++;
106  it.current()->savePropertiesInternal(config, n);
107  }
108  config->setGroup(TQString::fromLatin1("Number"));
109  config->writeEntry(TQString::fromLatin1("NumberOfWindows"), n );
110  return true;
111  }
112 
113  bool commitData( QSessionManager& sm )
114  {
115  // not really a fast method but the only compatible one
116  if ( sm.allowsInteraction() ) {
117  bool canceled = false;
118  TQPtrListIterator<KMainWindow> it(*KMainWindow::memberList);
119  ::no_query_exit = true;
120  for (it.toFirst(); it.current() && !canceled;){
121  KMainWindow *window = *it;
122  ++it; // Update now, the current window might get deleted
123  if ( !window->testWState( TQt::WState_ForceHide ) ) {
124  TQCloseEvent e;
125  TQApplication::sendEvent( window, &e );
126  canceled = !e.isAccepted();
127  /* Don't even think_about deleting widgets with
128  Qt::WDestructiveClose flag set at this point. We
129  are faking a close event, but we are *not*_
130  closing the window. The purpose of the faked
131  close event is to prepare the application so it
132  can safely be quit without the user losing data
133  (possibly showing a message box "do you want to
134  save this or that?"). It is possible that the
135  session manager quits the application later
136  (emitting TQApplication::aboutToQuit() when this
137  happens), but it is also possible that the user
138  cancels the shutdown, so the application will
139  continue to run.
140  */
141  }
142  }
143  ::no_query_exit = false;
144  if (canceled)
145  return false;
146 
147  KMainWindow* last = 0;
148  for (it.toFirst(); it.current() && !canceled; ++it){
149  KMainWindow *window = *it;
150  if ( !window->testWState( TQt::WState_ForceHide ) ) {
151  last = window;
152  }
153  }
154  if ( last )
155  return last->queryExit();
156  // else
157  return true;
158  }
159 
160  // the user wants it, the user gets it
161  return true;
162  }
163 };
164 
165 static bool being_first = true;
166 
167 KMainWindow::KMainWindow( TQWidget* parent, const char *name, WFlags f )
168  : TQMainWindow( parent, name, f ), KXMLGUIBuilder( this ), helpMenu2( 0 ), factory_( 0 )
169 {
170  initKMainWindow(name, 0);
171 }
172 
173 KMainWindow::KMainWindow( int cflags, TQWidget* parent, const char *name, WFlags f )
174  : TQMainWindow( parent, name, f ), KXMLGUIBuilder( this ), helpMenu2( 0 ), factory_( 0 )
175 {
176  initKMainWindow(name, cflags);
177 }
178 
179 void KMainWindow::initKMainWindow(const char *name, int cflags)
180 {
181  KWhatsThisManager::init ();
182  setDockMenuEnabled( false );
183  mHelpMenu = 0;
184  kapp->setTopWidget( this );
185  actionCollection()->setWidget( this );
186  connect(kapp, TQT_SIGNAL(shutDown()), this, TQT_SLOT(shuttingDown()));
187  if( !memberList )
188  memberList = new TQPtrList<KMainWindow>;
189 
190  if ( !ksm )
191  ksm = ksmd.setObject(ksm, new KMWSessionManaged());
192  // set a unique object name. Required by session management.
193  TQCString objname;
194  TQCString s;
195  int unusedNumber;
196  if ( !name )
197  { // no name given
198  objname = kapp->instanceName() + "-mainwindow#";
199  s = objname + '1'; // start adding number immediately
200  unusedNumber = 1;
201  }
202  else if( name[0] != '\0' && name[ strlen( name ) - 1 ] == '#' )
203  { // trailing # - always add a number
204  objname = name;
205  s = objname + '1'; // start adding number immediately
206  unusedNumber = 1;
207  }
208  else
209  {
210  objname = name;
211  s = objname;
212  unusedNumber = 0; // add numbers only when needed
213  }
214  for(;;) {
215  TQWidgetList* list = kapp->topLevelWidgets();
216  TQWidgetListIt it( *list );
217  bool found = false;
218  for( TQWidget* w = it.current();
219  w != NULL;
220  ++it, w = it.current())
221  if( w != this && w->name() == s )
222  {
223  found = true;
224  break;
225  }
226  delete list;
227  if( !found )
228  break;
229  s.setNum( ++unusedNumber );
230  s = objname + s;
231  }
232  setName( s );
233 
234  memberList->append( this );
235 
236  d = new KMainWindowPrivate;
237  d->showHelpMenu = true;
238  d->settingsDirty = false;
239  d->autoSaveSettings = false;
240  d->autoSaveWindowSize = true; // for compatibility
241  d->kaccel = actionCollection()->kaccel();
242  d->toolBarHandler = 0;
243  d->settingsTimer = 0;
244  d->showStatusBarAction = NULL;
245  d->shuttingDown = false;
246  if ((d->care_about_geometry = being_first)) {
247  being_first = false;
248  if ( kapp->geometryArgument().isNull() ) // if there is no geometry, it doesn't mater
249  d->care_about_geometry = false;
250  else
251  parseGeometry(false);
252  }
253 
254  setCaption( kapp->caption() );
255  if ( cflags & NoDCOPObject)
256  d->m_interface = 0;
257  else
258  d->m_interface = new KMainWindowInterface(this);
259 
260  if (!kapp->authorize("movable_toolbars"))
261  setDockWindowsMovable(false);
262 }
263 
264 KAction *KMainWindow::toolBarMenuAction()
265 {
266  if ( !d->toolBarHandler )
267  return 0;
268 
269  return d->toolBarHandler->toolBarMenuAction();
270 }
271 
272 
273 void KMainWindow::setupToolbarMenuActions()
274 {
275  if ( d->toolBarHandler )
276  d->toolBarHandler->setupActions();
277 }
278 
279 void KMainWindow::parseGeometry(bool parsewidth)
280 {
281  assert ( !kapp->geometryArgument().isNull() );
282  assert ( d->care_about_geometry );
283 
284 #if defined Q_WS_X11
285  int x, y;
286  int w, h;
287  int m = XParseGeometry( kapp->geometryArgument().latin1(), &x, &y, (unsigned int*)&w, (unsigned int*)&h);
288  if (parsewidth) {
289  TQSize minSize = minimumSize();
290  TQSize maxSize = maximumSize();
291  if ( !(m & WidthValue) )
292  w = width();
293  if ( !(m & HeightValue) )
294  h = height();
295  w = QMIN(w,maxSize.width());
296  h = QMIN(h,maxSize.height());
297  w = QMAX(w,minSize.width());
298  h = QMAX(h,minSize.height());
299  resize(w, h);
300  } else {
301  if ( parsewidth && !(m & XValue) )
302  x = geometry().x();
303  if ( parsewidth && !(m & YValue) )
304  y = geometry().y();
305  if ( (m & XNegative) )
306  x = KApplication::desktop()->width() + x - w;
307  if ( (m & YNegative) )
308  y = KApplication::desktop()->height() + y - h;
309  move(x, y);
310  }
311 #endif
312 }
313 
314 KMainWindow::~KMainWindow()
315 {
316  delete d->settingsTimer;
317  TQMenuBar* mb = internalMenuBar();
318  delete mb;
319  delete d->m_interface;
320  delete d;
321  memberList->remove( this );
322 }
323 
324 KPopupMenu* KMainWindow::helpMenu( const TQString &aboutAppText, bool showWhatsThis )
325 {
326  if( !mHelpMenu ) {
327  if ( aboutAppText.isEmpty() )
328  mHelpMenu = new KHelpMenu( this, instance()->aboutData(), showWhatsThis);
329  else
330  mHelpMenu = new KHelpMenu( this, aboutAppText, showWhatsThis );
331 
332  if ( !mHelpMenu )
333  return 0;
334  connect( mHelpMenu, TQT_SIGNAL( showAboutApplication() ),
335  this, TQT_SLOT( showAboutApplication() ) );
336  }
337 
338  return mHelpMenu->menu();
339 }
340 
341 KPopupMenu* KMainWindow::customHelpMenu( bool showWhatsThis )
342 {
343  if( !mHelpMenu ) {
344  mHelpMenu = new KHelpMenu( this, TQString::null, showWhatsThis );
345  connect( mHelpMenu, TQT_SIGNAL( showAboutApplication() ),
346  this, TQT_SLOT( showAboutApplication() ) );
347  }
348 
349  return mHelpMenu->menu();
350 }
351 
352 bool KMainWindow::canBeRestored( int number )
353 {
354  if ( !kapp->isRestored() )
355  return false;
356  KConfig *config = kapp->sessionConfig();
357  if ( !config )
358  return false;
359  config->setGroup( TQString::fromLatin1("Number") );
360  int n = config->readNumEntry( TQString::fromLatin1("NumberOfWindows") , 1 );
361  return number >= 1 && number <= n;
362 }
363 
364 const TQString KMainWindow::classNameOfToplevel( int number )
365 {
366  if ( !kapp->isRestored() )
367  return TQString::null;
368  KConfig *config = kapp->sessionConfig();
369  if ( !config )
370  return TQString::null;
371  TQString s;
372  s.setNum( number );
373  s.prepend( TQString::fromLatin1("WindowProperties") );
374  config->setGroup( s );
375  if ( !config->hasKey( TQString::fromLatin1("ClassName") ) )
376  return TQString::null;
377  else
378  return config->readEntry( TQString::fromLatin1("ClassName") );
379 }
380 
381 void KMainWindow::show()
382 {
383  TQMainWindow::show();
384 
385  for ( TQPtrListIterator<TQDockWindow> it( d->hiddenDockWindows ); it.current(); ++it )
386  it.current()->show();
387 
388  d->hiddenDockWindows.clear();
389 }
390 
391 void KMainWindow::hide()
392 {
393  if ( isVisible() ) {
394 
395  d->hiddenDockWindows.clear();
396 
397  TQObjectList *list = queryList( TQDOCKWINDOW_OBJECT_NAME_STRING );
398  for( TQObjectListIt it( *list ); it.current(); ++it ) {
399  TQDockWindow *dw = (TQDockWindow*)it.current();
400  if ( dw->isTopLevel() && dw->isVisible() ) {
401  d->hiddenDockWindows.append( dw );
402  dw->hide();
403  }
404  }
405  delete list;
406  }
407 
408  TQWidget::hide();
409 }
410 
411 bool KMainWindow::restore( int number, bool show )
412 {
413  if ( !canBeRestored( number ) )
414  return false;
415  KConfig *config = kapp->sessionConfig();
416  if ( readPropertiesInternal( config, number ) ){
417  if ( show )
418  KMainWindow::show();
419  return false;
420  }
421  return false;
422 }
423 
424 KXMLGUIFactory *KMainWindow::guiFactory()
425 {
426  if ( !factory_ )
427  factory_ = new KXMLGUIFactory( this, TQT_TQOBJECT(this), "guifactory" );
428  return factory_;
429 }
430 
431 int KMainWindow::configureToolbars()
432 {
433  saveMainWindowSettings(KGlobal::config());
434  KEditToolbar dlg(actionCollection(), xmlFile(), true, this);
435  connect(&dlg, TQT_SIGNAL(newToolbarConfig()), TQT_SLOT(saveNewToolbarConfig()));
436  return dlg.exec();
437 }
438 
439 void KMainWindow::saveNewToolbarConfig()
440 {
441  createGUI(xmlFile());
442  applyMainWindowSettings( KGlobal::config() );
443 }
444 
445 void KMainWindow::setupGUI( int options, const TQString & xmlfile ) {
446  setupGUI(TQSize(), options, xmlfile);
447 }
448 
449 void KMainWindow::setupGUI( TQSize defaultSize, int options, const TQString & xmlfile ) {
450  if( options & Keys ){
451  KStdAction::keyBindings(guiFactory(),
452  TQT_SLOT(configureShortcuts()), actionCollection());
453  }
454 
455  if( (options & StatusBar) && internalStatusBar() ){
456  createStandardStatusBarAction();
457  }
458 
459  if( options & ToolBar ){
460  setStandardToolBarMenuEnabled( true );
461  KStdAction::configureToolbars(TQT_TQOBJECT(this),
462  TQT_SLOT(configureToolbars() ), actionCollection());
463  }
464 
465  if( options & Create ){
466  createGUI(xmlfile,false);
467  }
468 
469  if( options & Save ){
470  // setupGUI() is typically called in the constructor before show(),
471  // so the default window size will be incorrect unless the application
472  // hard coded the size which they should try not to do (i.e. use
473  // size hints).
474  if(initialGeometrySet())
475  {
476  // Do nothing...
477  }
478  else if(defaultSize.isValid())
479  {
480  resize(defaultSize);
481  }
482  else if(!isShown())
483  {
484  adjustSize();
485  }
486  setAutoSaveSettings();
487  }
488 
489 }
490 
491 void KMainWindow::createGUI( const TQString &xmlfile, bool _conserveMemory )
492 {
493  // disabling the updates prevents unnecessary redraws
494  setUpdatesEnabled( false );
495 
496  // just in case we are rebuilding, let's remove our old client
497  guiFactory()->removeClient( this );
498 
499  // make sure to have an empty GUI
500  TQMenuBar* mb = internalMenuBar();
501  if ( mb )
502  mb->clear();
503 
504  (void)toolBarIterator(); // make sure toolbarList is most-up-to-date
505  toolbarList.setAutoDelete( true );
506  toolbarList.clear();
507  toolbarList.setAutoDelete( false );
508 
509  // don't build a help menu unless the user ask for it
510  if (d->showHelpMenu) {
511  // we always want a help menu
512  if (!helpMenu2)
513  helpMenu2 = new KHelpMenu(this, instance()->aboutData(), true,
514  actionCollection());
515  }
516 
517  // we always want to load in our global standards file
518  setXMLFile( locate( "config", "ui/ui_standards.rc", instance() ) );
519 
520  // now, merge in our local xml file. if this is null, then that
521  // means that we will be only using the global file
522  if ( !xmlfile.isNull() ) {
523  setXMLFile( xmlfile, true );
524  } else {
525  TQString auto_file(instance()->instanceName() + "ui.rc");
526  setXMLFile( auto_file, true );
527  }
528 
529  // make sure we don't have any state saved already
530  setXMLGUIBuildDocument( TQDomDocument() );
531 
532  // do the actual GUI building
533  guiFactory()->addClient( this );
534 
535  // try and get back *some* of our memory
536  if ( _conserveMemory )
537  {
538  // before freeing the memory allocated by the DOM document we also
539  // free all memory allocated internally in the KXMLGUIFactory for
540  // the menubar and the toolbars . This however implies that we
541  // have to take care of deleting those widgets ourselves. For
542  // destruction this is no problem, but when rebuilding we have
543  // to take care of that (and we want to rebuild the GUI when
544  // using stuff like the toolbar editor ).
545  // In addition we have to take care of not removing containers
546  // like popupmenus, defined in the XML document.
547  // this code should probably go into a separate method in KMainWindow.
548  // there's just one problem: I'm bad in finding names ;-) , so
549  // I skipped this ;-)
550 
551  TQDomDocument doc = domDocument();
552 
553  for( TQDomNode n = doc.documentElement().firstChild();
554  !n.isNull(); n = n.nextSibling())
555  {
556  TQDomElement e = n.toElement();
557 
558  if ( e.tagName().lower() == "toolbar" )
559  factory_->resetContainer( e.attribute( "name" ) );
560  else if ( e.tagName().lower() == "menubar" )
561  factory_->resetContainer( e.tagName(), true );
562  }
563 
564  conserveMemory();
565  }
566 
567  setUpdatesEnabled( true );
568  updateGeometry();
569 }
570 
571 void KMainWindow::setHelpMenuEnabled(bool showHelpMenu)
572 {
573  d->showHelpMenu = showHelpMenu;
574 }
575 
576 bool KMainWindow::isHelpMenuEnabled()
577 {
578  return d->showHelpMenu;
579 }
580 
581 void KMainWindow::setCaption( const TQString &caption )
582 {
583  setPlainCaption( kapp->makeStdCaption(caption) );
584 }
585 
586 void KMainWindow::setCaption( const TQString &caption, bool modified )
587 {
588  setPlainCaption( kapp->makeStdCaption(caption, true, modified) );
589 }
590 
591 void KMainWindow::setPlainCaption( const TQString &caption )
592 {
593  TQMainWindow::setCaption( caption );
594 #if defined Q_WS_X11
595  NETWinInfo info( qt_xdisplay(), winId(), qt_xrootwin(), 0 );
596  info.setName( caption.utf8().data() );
597 #endif
598 }
599 
600 void KMainWindow::appHelpActivated( void )
601 {
602  if( !mHelpMenu ) {
603  mHelpMenu = new KHelpMenu( this );
604  if ( !mHelpMenu )
605  return;
606  }
607  mHelpMenu->appHelpActivated();
608 }
609 
610 void KMainWindow::slotStateChanged(const TQString &newstate)
611 {
612  stateChanged(newstate, KXMLGUIClient::StateNoReverse);
613 }
614 
615 /*
616  * Get rid of this for KDE 4.0
617  */
618 void KMainWindow::slotStateChanged(const TQString &newstate,
619  KXMLGUIClient::ReverseStateChange reverse)
620 {
621  stateChanged(newstate, reverse);
622 }
623 
624 /*
625  * Enable this for KDE 4.0
626  */
627 // void KMainWindow::slotStateChanged(const TQString &newstate,
628 // bool reverse)
629 // {
630 // stateChanged(newstate,
631 // reverse ? KXMLGUIClient::StateReverse : KXMLGUIClient::StateNoReverse);
632 // }
633 
634 void KMainWindow::closeEvent ( TQCloseEvent *e )
635 {
636  // Save settings if auto-save is enabled, and settings have changed
637  if (d->settingsDirty && d->autoSaveSettings)
638  saveAutoSaveSettings();
639 
640  if (queryClose()) {
641  e->accept();
642 
643  int not_withdrawn = 0;
644  TQPtrListIterator<KMainWindow> it(*KMainWindow::memberList);
645  for (it.toFirst(); it.current(); ++it){
646  if ( !it.current()->isHidden() && it.current()->isTopLevel() && it.current() != this )
647  not_withdrawn++;
648  }
649 
650  if ( !no_query_exit && not_withdrawn <= 0 ) { // last window close accepted?
651  if ( queryExit() && !kapp->sessionSaving() && !d->shuttingDown ) { // Yes, Quit app?
652  // don't call queryExit() twice
653  disconnect(kapp, TQT_SIGNAL(shutDown()), this, TQT_SLOT(shuttingDown()));
654  d->shuttingDown = true;
655  kapp->deref(); // ...and quit application.
656  } else {
657  // cancel closing, it's stupid to end up with no windows at all....
658  e->ignore();
659  }
660  }
661  }
662 }
663 
664 bool KMainWindow::queryExit()
665 {
666  return true;
667 }
668 
669 bool KMainWindow::queryClose()
670 {
671  return true;
672 }
673 
674 void KMainWindow::saveGlobalProperties( KConfig* )
675 {
676 }
677 
678 void KMainWindow::readGlobalProperties( KConfig* )
679 {
680 }
681 
682 #if defined(KDE_COMPAT)
683 void KMainWindow::updateRects()
684 {
685 }
686 #endif
687 
688 void KMainWindow::showAboutApplication()
689 {
690 }
691 
692 void KMainWindow::savePropertiesInternal( KConfig *config, int number )
693 {
694  bool oldASWS = d->autoSaveWindowSize;
695  d->autoSaveWindowSize = true; // make saveMainWindowSettings save the window size
696 
697  TQString s;
698  s.setNum(number);
699  s.prepend(TQString::fromLatin1("WindowProperties"));
700  config->setGroup(s);
701 
702  // store objectName, className, Width and Height for later restoring
703  // (Only useful for session management)
704  config->writeEntry(TQString::fromLatin1("ObjectName"), name());
705  config->writeEntry(TQString::fromLatin1("ClassName"), className());
706 
707  saveMainWindowSettings(config); // Menubar, statusbar and Toolbar settings.
708 
709  s.setNum(number);
710  config->setGroup(s);
711  saveProperties(config);
712 
713  d->autoSaveWindowSize = oldASWS;
714 }
715 
716 void KMainWindow::saveMainWindowSettings(KConfig *config, const TQString &configGroup)
717 {
718  kdDebug(200) << "KMainWindow::saveMainWindowSettings " << configGroup << endl;
719  TQString oldGroup;
720 
721  if (!configGroup.isEmpty())
722  {
723  oldGroup = config->group();
724  config->setGroup(configGroup);
725  }
726 
727  // Called by session management - or if we want to save the window size anyway
728  if ( d->autoSaveWindowSize )
729  saveWindowSize( config );
730 
731  TQStatusBar* sb = internalStatusBar();
732  if (sb) {
733  if(!config->hasDefault("StatusBar") && !sb->isHidden() )
734  config->revertToDefault("StatusBar");
735  else
736  config->writeEntry("StatusBar", sb->isHidden() ? "Disabled" : "Enabled");
737  }
738 
739  TQMenuBar* mb = internalMenuBar();
740  if (mb) {
741  TQString MenuBar = TQString::fromLatin1("MenuBar");
742  if(!config->hasDefault("MenuBar") && !mb->isHidden() )
743  config->revertToDefault("MenuBar");
744  else
745  config->writeEntry("MenuBar", mb->isHidden() ? "Disabled" : "Enabled");
746  }
747 
748  int n = 1; // Toolbar counter. toolbars are counted from 1,
749  KToolBar *toolbar = 0;
750  TQPtrListIterator<KToolBar> it( toolBarIterator() );
751  while ( ( toolbar = it.current() ) ) {
752  ++it;
753  TQString group;
754  if (!configGroup.isEmpty())
755  {
756  // Give a number to the toolbar, but prefer a name if there is one,
757  // because there's no real guarantee on the ordering of toolbars
758  group = (!::qstrcmp(toolbar->name(), "unnamed") ? TQString::number(n) : TQString(" ")+toolbar->name());
759  group.prepend(" Toolbar");
760  group.prepend(configGroup);
761  }
762  toolbar->saveSettings(config, group);
763  n++;
764  }
765  if (!configGroup.isEmpty())
766  config->setGroup(oldGroup);
767 }
768 
769 void KMainWindow::setStandardToolBarMenuEnabled( bool enable )
770 {
771  if ( enable ) {
772  if ( d->toolBarHandler )
773  return;
774 
775  d->toolBarHandler = new KDEPrivate::ToolBarHandler( this );
776 
777  if ( factory() )
778  factory()->addClient( d->toolBarHandler );
779  } else {
780  if ( !d->toolBarHandler )
781  return;
782 
783  if ( factory() )
784  factory()->removeClient( d->toolBarHandler );
785 
786  delete d->toolBarHandler;
787  d->toolBarHandler = 0;
788  }
789 }
790 
791 bool KMainWindow::isStandardToolBarMenuEnabled() const
792 {
793  return ( d->toolBarHandler );
794 }
795 
796 void KMainWindow::createStandardStatusBarAction(){
797  if(!d->showStatusBarAction){
798  d->showStatusBarAction = KStdAction::showStatusbar(TQT_TQOBJECT(this), TQT_SLOT(setSettingsDirty()), actionCollection());
799  KStatusBar *sb = statusBar(); // Creates statusbar if it doesn't exist already.
800  connect(d->showStatusBarAction, TQT_SIGNAL(toggled(bool)), sb, TQT_SLOT(setShown(bool)));
801  d->showStatusBarAction->setChecked(sb->isHidden());
802  }
803 }
804 
805 bool KMainWindow::readPropertiesInternal( KConfig *config, int number )
806 {
807  if ( number == 1 )
808  readGlobalProperties( config );
809 
810  // in order they are in toolbar list
811  TQString s;
812  s.setNum(number);
813  s.prepend(TQString::fromLatin1("WindowProperties"));
814 
815  config->setGroup(s);
816 
817  // restore the object name (window role)
818  if ( config->hasKey(TQString::fromLatin1("ObjectName" )) )
819  setName( config->readEntry(TQString::fromLatin1("ObjectName")).latin1()); // latin1 is right here
820 
821  applyMainWindowSettings(config); // Menubar, statusbar and toolbar settings.
822 
823  s.setNum(number);
824  config->setGroup(s);
825  readProperties(config);
826  return true;
827 }
828 
829 void KMainWindow::applyMainWindowSettings(KConfig *config, const TQString &configGroup)
830 {
831  return applyMainWindowSettings(config,configGroup,false);
832 }
833 
834 void KMainWindow::applyMainWindowSettings(KConfig *config, const TQString &configGroup,bool force)
835 {
836  kdDebug(200) << "KMainWindow::applyMainWindowSettings" << endl;
837 
838  KConfigGroupSaver saver( config, configGroup.isEmpty() ? config->group() : configGroup );
839 
840  restoreWindowSize(config);
841 
842  TQStatusBar* sb = internalStatusBar();
843  if (sb) {
844  TQString entry = config->readEntry("StatusBar", "Enabled");
845  if ( entry == "Disabled" )
846  sb->hide();
847  else
848  sb->show();
849  if(d->showStatusBarAction)
850  d->showStatusBarAction->setChecked(!sb->isHidden());
851  }
852 
853  TQMenuBar* mb = internalMenuBar();
854  if (mb) {
855  TQString entry = config->readEntry ("MenuBar", "Enabled");
856  if ( entry == "Disabled" )
857  mb->hide();
858  else
859  mb->show();
860  }
861 
862  int n = 1; // Toolbar counter. toolbars are counted from 1,
863  KToolBar *toolbar;
864  TQPtrListIterator<KToolBar> it( toolBarIterator() ); // must use own iterator
865 
866  for ( ; it.current(); ++it) {
867  toolbar= it.current();
868  TQString group;
869  if (!configGroup.isEmpty())
870  {
871  // Give a number to the toolbar, but prefer a name if there is one,
872  // because there's no real guarantee on the ordering of toolbars
873  group = (!::qstrcmp(toolbar->name(), "unnamed") ? TQString::number(n) : TQString(" ")+toolbar->name());
874  group.prepend(" Toolbar");
875  group.prepend(configGroup);
876  }
877  toolbar->applySettings(config, group, force);
878  n++;
879  }
880 
881  finalizeGUI( true );
882 }
883 
884 void KMainWindow::finalizeGUI( bool force )
885 {
886  //kdDebug(200) << "KMainWindow::finalizeGUI force=" << force << endl;
887  // The whole reason for this is that moveToolBar relies on the indexes
888  // of the other toolbars, so in theory it should be called only once per
889  // toolbar, but in increasing order of indexes.
890  // Since we can't do that immediately, we move them, and _then_
891  // we call positionYourself again for each of them, but this time
892  // the toolbariterator should give them in the proper order.
893  // Both the XMLGUI and applySettings call this, hence "force" for the latter.
894  TQPtrListIterator<KToolBar> it( toolBarIterator() );
895  for ( ; it.current() ; ++it ) {
896  it.current()->positionYourself( force );
897  }
898 
899  d->settingsDirty = false;
900 }
901 
902 void KMainWindow::saveWindowSize( KConfig * config ) const
903 {
904  int scnum = TQApplication::desktop()->screenNumber(parentWidget());
905  TQRect desk = TQApplication::desktop()->screenGeometry(scnum);
906  int w, h;
907 #if defined Q_WS_X11
908  // save maximalization as desktop size + 1 in that direction
909  KWin::WindowInfo info = KWin::windowInfo( winId(), NET::WMState );
910  w = info.state() & NET::MaxHoriz ? desk.width() + 1 : width();
911  h = info.state() & NET::MaxVert ? desk.height() + 1 : height();
912 #else
913  if (isMaximized()) {
914  w = desk.width() + 1;
915  h = desk.height() + 1;
916  }
917  //TODO: add "Maximized" property instead "+1" hack
918 #endif
919  TQRect size( desk.width(), w, desk.height(), h );
920  bool defaultSize = (size == d->defaultWindowSize);
921  TQString widthString = TQString::fromLatin1("Width %1").arg(desk.width());
922  TQString heightString = TQString::fromLatin1("Height %1").arg(desk.height());
923  if (!config->hasDefault(widthString) && defaultSize)
924  config->revertToDefault(widthString);
925  else
926  config->writeEntry(widthString, w );
927 
928  if (!config->hasDefault(heightString) && defaultSize)
929  config->revertToDefault(heightString);
930  else
931  config->writeEntry(heightString, h );
932 }
933 
934 void KMainWindow::restoreWindowSize( KConfig * config )
935 {
936  if (d->care_about_geometry) {
937  parseGeometry(true);
938  } else {
939  // restore the size
940  int scnum = TQApplication::desktop()->screenNumber(parentWidget());
941  TQRect desk = TQApplication::desktop()->screenGeometry(scnum);
942  if ( d->defaultWindowSize.isNull() ) // only once
943  d->defaultWindowSize = TQRect(desk.width(), width(), desk.height(), height()); // store default values
944  TQSize size( config->readNumEntry( TQString::fromLatin1("Width %1").arg(desk.width()), 0 ),
945  config->readNumEntry( TQString::fromLatin1("Height %1").arg(desk.height()), 0 ) );
946  if (size.isEmpty()) {
947  // try the KDE 2.0 way
948  size = TQSize( config->readNumEntry( TQString::fromLatin1("Width"), 0 ),
949  config->readNumEntry( TQString::fromLatin1("Height"), 0 ) );
950  if (!size.isEmpty()) {
951  // make sure the other resolutions don't get old settings
952  config->writeEntry( TQString::fromLatin1("Width"), 0 );
953  config->writeEntry( TQString::fromLatin1("Height"), 0 );
954  }
955  }
956  if ( !size.isEmpty() ) {
957 #ifdef Q_WS_X11
958  int state = ( size.width() > desk.width() ? NET::MaxHoriz : 0 )
959  | ( size.height() > desk.height() ? NET::MaxVert : 0 );
960  if(( state & NET::Max ) == NET::Max )
961  ; // no resize
962  else if(( state & NET::MaxHoriz ) == NET::MaxHoriz )
963  resize( width(), size.height());
964  else if(( state & NET::MaxVert ) == NET::MaxVert )
965  resize( size.width(), height());
966  else
967  resize( size );
968  // TQWidget::showMaximized() is both insufficient and broken
969  KWin::setState( winId(), state );
970 #else
971  if (size.width() > desk.width() || size.height() > desk.height())
972  setWindowState( WindowMaximized );
973  else
974  resize( size );
975 #endif
976  }
977  }
978 }
979 
980 bool KMainWindow::initialGeometrySet() const
981 {
982  return d->care_about_geometry;
983 }
984 
985 void KMainWindow::ignoreInitialGeometry()
986 {
987  d->care_about_geometry = false;
988 }
989 
990 void KMainWindow::setSettingsDirty()
991 {
992  //kdDebug(200) << "KMainWindow::setSettingsDirty" << endl;
993  d->settingsDirty = true;
994  if ( d->autoSaveSettings )
995  {
996  // Use a timer to save "immediately" user-wise, but not too immediately
997  // (to compress calls and save only once, in case of multiple changes)
998  if ( !d->settingsTimer )
999  {
1000  d->settingsTimer = new TQTimer( this );
1001  connect( d->settingsTimer, TQT_SIGNAL( timeout() ), TQT_SLOT( saveAutoSaveSettings() ) );
1002  }
1003  d->settingsTimer->start( 500, true );
1004  }
1005 }
1006 
1007 bool KMainWindow::settingsDirty() const
1008 {
1009  return d->settingsDirty;
1010 }
1011 
1012 TQString KMainWindow::settingsGroup() const
1013 {
1014  return d->autoSaveGroup;
1015 }
1016 
1017 void KMainWindow::setAutoSaveSettings( const TQString & groupName, bool saveWindowSize )
1018 {
1019  d->autoSaveSettings = true;
1020  d->autoSaveGroup = groupName;
1021  d->autoSaveWindowSize = saveWindowSize;
1022  // Get notified when the user moves a toolbar around
1023  disconnect( this, TQT_SIGNAL( dockWindowPositionChanged( TQDockWindow * ) ),
1024  this, TQT_SLOT( setSettingsDirty() ) );
1025  connect( this, TQT_SIGNAL( dockWindowPositionChanged( TQDockWindow * ) ),
1026  this, TQT_SLOT( setSettingsDirty() ) );
1027 
1028  // Now read the previously saved settings
1029  applyMainWindowSettings( KGlobal::config(), groupName );
1030 }
1031 
1032 void KMainWindow::resetAutoSaveSettings()
1033 {
1034  d->autoSaveSettings = false;
1035  if ( d->settingsTimer )
1036  d->settingsTimer->stop();
1037 }
1038 
1039 bool KMainWindow::autoSaveSettings() const
1040 {
1041  return d->autoSaveSettings;
1042 }
1043 
1044 TQString KMainWindow::autoSaveGroup() const
1045 {
1046  return d->autoSaveGroup;
1047 }
1048 
1049 void KMainWindow::saveAutoSaveSettings()
1050 {
1051  Q_ASSERT( d->autoSaveSettings );
1052  //kdDebug(200) << "KMainWindow::saveAutoSaveSettings -> saving settings" << endl;
1053  saveMainWindowSettings( KGlobal::config(), d->autoSaveGroup );
1054  KGlobal::config()->sync();
1055  d->settingsDirty = false;
1056  if ( d->settingsTimer )
1057  d->settingsTimer->stop();
1058 }
1059 
1060 void KMainWindow::resizeEvent( TQResizeEvent * )
1061 {
1062  if ( d->autoSaveWindowSize )
1063  setSettingsDirty();
1064 }
1065 
1066 bool KMainWindow::hasMenuBar()
1067 {
1068  return (internalMenuBar());
1069 }
1070 
1071 KMenuBar *KMainWindow::menuBar()
1072 {
1073  KMenuBar * mb = internalMenuBar();
1074  if ( !mb ) {
1075  mb = new KMenuBar( this );
1076  // trigger a re-layout and trigger a call to the private
1077  // setMenuBar method.
1078  TQMainWindow::menuBar();
1079  }
1080  return mb;
1081 }
1082 
1083 KStatusBar *KMainWindow::statusBar()
1084 {
1085  KStatusBar * sb = internalStatusBar();
1086  if ( !sb ) {
1087  sb = new KStatusBar( this );
1088  // trigger a re-layout and trigger a call to the private
1089  // setStatusBar method.
1090  TQMainWindow::statusBar();
1091  }
1092  return sb;
1093 }
1094 
1095 void KMainWindow::shuttingDown()
1096 {
1097  // Needed for Qt <= 3.0.3 at least to prevent reentrancy
1098  // when queryExit() shows a dialog. Check before removing!
1099  static bool reentrancy_protection = false;
1100  if (!reentrancy_protection)
1101  {
1102  reentrancy_protection = true;
1103  // call the virtual queryExit
1104  queryExit();
1105  reentrancy_protection = false;
1106  }
1107 
1108 }
1109 
1110 KMenuBar *KMainWindow::internalMenuBar()
1111 {
1112  TQObjectList *l = queryList( "KMenuBar", 0, false, false );
1113  if ( !l || !l->first() ) {
1114  delete l;
1115  return 0;
1116  }
1117 
1118  KMenuBar *m = (KMenuBar*)l->first();
1119  delete l;
1120  return m;
1121 }
1122 
1123 KStatusBar *KMainWindow::internalStatusBar()
1124 {
1125  TQObjectList *l = queryList( "KStatusBar", 0, false, false );
1126  if ( !l || !l->first() ) {
1127  delete l;
1128  return 0;
1129  }
1130 
1131  KStatusBar *s = (KStatusBar*)l->first();
1132  delete l;
1133  return s;
1134 }
1135 
1136 void KMainWindow::childEvent( TQChildEvent* e)
1137 {
1138  TQMainWindow::childEvent( e );
1139 }
1140 
1141 KToolBar *KMainWindow::toolBar( const char * name )
1142 {
1143  if (!name)
1144  name = "mainToolBar";
1145  KToolBar *tb = (KToolBar*)child( name, "KToolBar" );
1146  if ( tb )
1147  return tb;
1148  bool honor_mode = (!strcmp(name, "mainToolBar"));
1149 
1150  if ( builderClient() )
1151  return new KToolBar(this, name, honor_mode); // XMLGUI constructor
1152  else
1153  return new KToolBar(this, DockTop, false, name, honor_mode ); // non-XMLGUI
1154 }
1155 
1156 TQPtrListIterator<KToolBar> KMainWindow::toolBarIterator()
1157 {
1158  toolbarList.clear();
1159  TQPtrList<TQToolBar> lst;
1160  for ( int i = (int)TQMainWindow::DockUnmanaged; i <= (int)DockMinimized; ++i ) {
1161  lst = toolBars( (ToolBarDock)i );
1162  for ( TQToolBar *tb = lst.first(); tb; tb = lst.next() ) {
1163  if ( !tb->inherits( "KToolBar" ) )
1164  continue;
1165  toolbarList.append( (KToolBar*)tb );
1166  }
1167  }
1168  return TQPtrListIterator<KToolBar>( toolbarList );
1169 }
1170 
1171 KAccel * KMainWindow::accel()
1172 {
1173  if ( !d->kaccel )
1174  d->kaccel = new KAccel( this, "kmw-kaccel" );
1175  return d->kaccel;
1176 }
1177 
1178 void KMainWindow::paintEvent( TQPaintEvent * pe )
1179 {
1180  TQMainWindow::paintEvent(pe); //Upcall to handle SH_MainWindow_SpaceBelowMenuBar rendering
1181 }
1182 
1183 TQSize KMainWindow::sizeForCentralWidgetSize(TQSize size)
1184 {
1185  KToolBar *tb = (KToolBar*)child( "mainToolBar", "KToolBar" );
1186  if (tb && !tb->isHidden()) {
1187  switch( tb->barPos() )
1188  {
1189  case KToolBar::Top:
1190  case KToolBar::Bottom:
1191  size += TQSize(0, tb->sizeHint().height());
1192  break;
1193 
1194  case KToolBar::Left:
1195  case KToolBar::Right:
1196  size += TQSize(toolBar()->sizeHint().width(), 0);
1197  break;
1198 
1199  case KToolBar::Flat:
1200  size += TQSize(0, 3+kapp->style().pixelMetric( TQStyle::PM_DockWindowHandleExtent ));
1201  break;
1202 
1203  default:
1204  break;
1205  }
1206  }
1207  KMenuBar *mb = internalMenuBar();
1208  if (mb && !mb->isHidden()) {
1209  size += TQSize(0,mb->heightForWidth(size.width()));
1210  if (style().styleHint(TQStyle::SH_MainWindow_SpaceBelowMenuBar, this))
1211  size += TQSize( 0, dockWindowsMovable() ? 1 : 2);
1212  }
1213  TQStatusBar *sb = internalStatusBar();
1214  if( sb && !sb->isHidden() )
1215  size += TQSize(0, sb->sizeHint().height());
1216 
1217  return size;
1218 }
1219 
1220 #if KDE_IS_VERSION( 3, 9, 0 )
1221 #ifdef __GNUC__
1222 #warning Remove, should be in Qt
1223 #endif
1224 #endif
1225 void KMainWindow::setIcon( const TQPixmap& p )
1226 {
1227  TQMainWindow::setIcon( p );
1228 #ifdef Q_WS_X11
1229  // Qt3 doesn't support _NET_WM_ICON, but KApplication::setTopWidget(), which
1230  // is used by KMainWindow, sets it
1231  KWin::setIcons( winId(), p, TQPixmap());
1232 #endif
1233 }
1234 
1235 TQPtrList<KMainWindow>* KMainWindow::getMemberList() { return memberList; }
1236 
1237 // why do we support old gcc versions? using KXMLGUIBuilder::finalizeGUI;
1238 // DF: because they compile KDE much faster :)
1239 void KMainWindow::finalizeGUI( KXMLGUIClient *client )
1240 { KXMLGUIBuilder::finalizeGUI( client ); }
1241 
1242 void KMainWindow::virtual_hook( int id, void* data )
1243 { KXMLGUIBuilder::virtual_hook( id, data );
1244  KXMLGUIClient::virtual_hook( id, data ); }
1245 
1246 
1247 
1248 #include "kmainwindow.moc"
1249 

kdeui

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

kdeui

Skip menu "kdeui"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kdeui by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |