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>
34 #include <tqwidgetlist.h>
39 #include <kapplication.h>
42 #include <khelpmenu.h>
44 #include <kstatusbar.h>
46 #include <kedittoolbar.h>
47 #include <kmainwindow.h>
50 #include <kstandarddirs.h>
51 #include <kstaticdeleter.h>
60 class KMainWindowPrivate {
64 bool autoSaveSettings:1;
66 bool autoSaveWindowSize:1;
67 bool care_about_geometry:1;
69 TQString autoSaveGroup;
73 TQTimer* settingsTimer;
75 TQRect defaultWindowSize;
76 TQPtrList<TQDockWindow> hiddenDockWindows;
80 static bool no_query_exit =
false;
81 static KMWSessionManaged* ksm = 0;
104 for (it.toFirst(); it.current(); ++it){
106 it.current()->savePropertiesInternal(config, n);
108 config->
setGroup(TQString::fromLatin1(
"Number"));
109 config->
writeEntry(TQString::fromLatin1(
"NumberOfWindows"), n );
116 if ( sm.allowsInteraction() ) {
117 bool canceled =
false;
119 ::no_query_exit =
true;
120 for (it.toFirst(); it.current() && !canceled;){
123 if ( !window->testWState( TQt::WState_ForceHide ) ) {
125 TQApplication::sendEvent( window, &e );
126 canceled = !e.isAccepted();
143 ::no_query_exit =
false;
148 for (it.toFirst(); it.current() && !canceled; ++it){
150 if ( !window->testWState( TQt::WState_ForceHide ) ) {
165 static bool being_first =
true;
168 : TQMainWindow( parent, name, f ),
KXMLGUIBuilder( this ), helpMenu2( 0 ), factory_( 0 )
170 initKMainWindow(name, 0);
174 : TQMainWindow( parent, name, f ),
KXMLGUIBuilder( this ), helpMenu2( 0 ), factory_( 0 )
176 initKMainWindow(name, cflags);
179 void KMainWindow::initKMainWindow(
const char *name,
int cflags)
181 KWhatsThisManager::init ();
182 setDockMenuEnabled(
false );
184 kapp->setTopWidget(
this );
186 connect(kapp, TQT_SIGNAL(shutDown()),
this, TQT_SLOT(shuttingDown()));
191 ksm = ksmd.
setObject(ksm,
new KMWSessionManaged());
198 objname = kapp->instanceName() +
"-mainwindow#";
202 else if( name[0] !=
'\0' && name[ strlen( name ) - 1 ] ==
'#' )
215 TQWidgetList* list = kapp->topLevelWidgets();
216 TQWidgetListIt it( *list );
218 for( TQWidget* w = it.current();
220 ++it, w = it.current())
221 if( w !=
this && w->name() == s )
229 s.setNum( ++unusedNumber );
236 d =
new KMainWindowPrivate;
237 d->showHelpMenu =
true;
238 d->settingsDirty =
false;
239 d->autoSaveSettings =
false;
240 d->autoSaveWindowSize =
true;
242 d->toolBarHandler = 0;
243 d->settingsTimer = 0;
244 d->showStatusBarAction = NULL;
245 d->shuttingDown =
false;
246 if ((d->care_about_geometry = being_first)) {
248 if ( kapp->geometryArgument().isNull() )
249 d->care_about_geometry =
false;
255 if ( cflags & NoDCOPObject)
260 if (!kapp->authorize(
"movable_toolbars"))
261 setDockWindowsMovable(
false);
266 if ( !d->toolBarHandler )
269 return d->toolBarHandler->toolBarMenuAction();
275 if ( d->toolBarHandler )
276 d->toolBarHandler->setupActions();
281 assert ( !kapp->geometryArgument().isNull() );
282 assert ( d->care_about_geometry );
287 int m = XParseGeometry( kapp->geometryArgument().latin1(), &x, &y, (
unsigned int*)&w, (
unsigned int*)&h);
289 TQSize minSize = minimumSize();
290 TQSize maxSize = maximumSize();
291 if ( !(m & WidthValue) )
293 if ( !(m & HeightValue) )
295 w = QMIN(w,maxSize.width());
296 h = QMIN(h,maxSize.height());
297 w = QMAX(w,minSize.width());
298 h = QMAX(h,minSize.height());
301 if ( parsewidth && !(m & XValue) )
303 if ( parsewidth && !(m & YValue) )
305 if ( (m & XNegative) )
306 x = KApplication::desktop()->width() + x - w;
307 if ( (m & YNegative) )
308 y = KApplication::desktop()->height() + y - h;
316 delete d->settingsTimer;
317 TQMenuBar* mb = internalMenuBar();
319 delete d->m_interface;
327 if ( aboutAppText.isEmpty() )
330 mHelpMenu =
new KHelpMenu(
this, aboutAppText, showWhatsThis );
338 return mHelpMenu->
menu();
344 mHelpMenu =
new KHelpMenu(
this, TQString::null, showWhatsThis );
349 return mHelpMenu->
menu();
354 if ( !kapp->isRestored() )
356 KConfig *config = kapp->sessionConfig();
359 config->
setGroup( TQString::fromLatin1(
"Number") );
360 int n = config->
readNumEntry( TQString::fromLatin1(
"NumberOfWindows") , 1 );
361 return number >= 1 && number <= n;
366 if ( !kapp->isRestored() )
367 return TQString::null;
368 KConfig *config = kapp->sessionConfig();
370 return TQString::null;
373 s.prepend( TQString::fromLatin1(
"WindowProperties") );
375 if ( !config->
hasKey( TQString::fromLatin1(
"ClassName") ) )
376 return TQString::null;
378 return config->
readEntry( TQString::fromLatin1(
"ClassName") );
385 for ( TQPtrListIterator<TQDockWindow> it( d->hiddenDockWindows ); it.current(); ++it )
386 it.current()->show();
388 d->hiddenDockWindows.clear();
395 d->hiddenDockWindows.clear();
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 );
415 KConfig *config = kapp->sessionConfig();
416 if ( readPropertiesInternal( config, number ) ){
427 factory_ =
new KXMLGUIFactory(
this, TQT_TQOBJECT(
this),
"guifactory" );
446 setupGUI(TQSize(), options, xmlfile);
450 if( options &
Keys ){
455 if( (options &
StatusBar) && internalStatusBar() ){
469 if( options &
Save ){
478 else if(defaultSize.isValid())
494 setUpdatesEnabled(
false );
500 TQMenuBar* mb = internalMenuBar();
505 toolbarList.setAutoDelete(
true );
507 toolbarList.setAutoDelete(
false );
510 if (d->showHelpMenu) {
522 if ( !xmlfile.isNull() ) {
525 TQString auto_file(
instance()->instanceName() +
"ui.rc");
536 if ( _conserveMemory )
553 for( TQDomNode n = doc.documentElement().firstChild();
554 !n.isNull(); n = n.nextSibling())
556 TQDomElement e = n.toElement();
558 if ( e.tagName().lower() ==
"toolbar" )
560 else if ( e.tagName().lower() ==
"menubar" )
567 setUpdatesEnabled(
true );
573 d->showHelpMenu = showHelpMenu;
578 return d->showHelpMenu;
595 NETWinInfo info( qt_xdisplay(), winId(), qt_xrootwin(), 0 );
596 info.setName( caption.utf8().data() );
619 KXMLGUIClient::ReverseStateChange reverse)
637 if (d->settingsDirty && d->autoSaveSettings)
643 int not_withdrawn = 0;
645 for (it.toFirst(); it.current(); ++it){
646 if ( !it.current()->isHidden() && it.current()->isTopLevel() && it.current() != this )
650 if ( !no_query_exit && not_withdrawn <= 0 ) {
651 if (
queryExit() && !kapp->sessionSaving() && !d->shuttingDown ) {
653 disconnect(kapp, TQT_SIGNAL(shutDown()),
this, TQT_SLOT(shuttingDown()));
654 d->shuttingDown =
true;
682 #if defined(KDE_COMPAT)
683 void KMainWindow::updateRects()
692 void KMainWindow::savePropertiesInternal(
KConfig *config,
int number )
694 bool oldASWS = d->autoSaveWindowSize;
695 d->autoSaveWindowSize =
true;
699 s.prepend(TQString::fromLatin1(
"WindowProperties"));
704 config->
writeEntry(TQString::fromLatin1(
"ObjectName"), name());
705 config->
writeEntry(TQString::fromLatin1(
"ClassName"), className());
713 d->autoSaveWindowSize = oldASWS;
718 kdDebug(200) <<
"KMainWindow::saveMainWindowSettings " << configGroup <<
endl;
721 if (!configGroup.isEmpty())
723 oldGroup = config->
group();
728 if ( d->autoSaveWindowSize )
731 TQStatusBar* sb = internalStatusBar();
733 if(!config->
hasDefault(
"StatusBar") && !sb->isHidden() )
736 config->
writeEntry(
"StatusBar", sb->isHidden() ?
"Disabled" :
"Enabled");
739 TQMenuBar* mb = internalMenuBar();
741 TQString MenuBar = TQString::fromLatin1(
"MenuBar");
742 if(!config->
hasDefault(
"MenuBar") && !mb->isHidden() )
745 config->
writeEntry(
"MenuBar", mb->isHidden() ?
"Disabled" :
"Enabled");
751 while ( ( toolbar = it.current() ) ) {
754 if (!configGroup.isEmpty())
758 group = (!::qstrcmp(toolbar->name(),
"unnamed") ? TQString::number(n) : TQString(
" ")+toolbar->name());
759 group.prepend(
" Toolbar");
760 group.prepend(configGroup);
765 if (!configGroup.isEmpty())
772 if ( d->toolBarHandler )
780 if ( !d->toolBarHandler )
786 delete d->toolBarHandler;
787 d->toolBarHandler = 0;
793 return ( d->toolBarHandler );
797 if(!d->showStatusBarAction){
800 connect(d->showStatusBarAction, TQT_SIGNAL(toggled(
bool)), sb, TQT_SLOT(setShown(
bool)));
801 d->showStatusBarAction->setChecked(sb->isHidden());
805 bool KMainWindow::readPropertiesInternal(
KConfig *config,
int number )
813 s.prepend(TQString::fromLatin1(
"WindowProperties"));
818 if ( config->
hasKey(TQString::fromLatin1(
"ObjectName" )) )
819 setName( config->
readEntry(TQString::fromLatin1(
"ObjectName")).latin1());
836 kdDebug(200) <<
"KMainWindow::applyMainWindowSettings" <<
endl;
842 TQStatusBar* sb = internalStatusBar();
844 TQString entry = config->
readEntry(
"StatusBar",
"Enabled");
845 if ( entry ==
"Disabled" )
849 if(d->showStatusBarAction)
850 d->showStatusBarAction->setChecked(!sb->isHidden());
853 TQMenuBar* mb = internalMenuBar();
855 TQString entry = config->
readEntry (
"MenuBar",
"Enabled");
856 if ( entry ==
"Disabled" )
866 for ( ; it.current(); ++it) {
867 toolbar= it.current();
869 if (!configGroup.isEmpty())
873 group = (!::qstrcmp(toolbar->name(),
"unnamed") ? TQString::number(n) : TQString(
" ")+toolbar->name());
874 group.prepend(
" Toolbar");
875 group.prepend(configGroup);
895 for ( ; it.current() ; ++it ) {
896 it.current()->positionYourself( force );
899 d->settingsDirty =
false;
904 int scnum = TQApplication::desktop()->screenNumber(parentWidget());
905 TQRect desk = TQApplication::desktop()->screenGeometry(scnum);
910 w = info.
state() & NET::MaxHoriz ? desk.width() + 1 : width();
911 h = info.
state() & NET::MaxVert ? desk.height() + 1 : height();
914 w = desk.width() + 1;
915 h = desk.height() + 1;
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)
928 if (!config->
hasDefault(heightString) && defaultSize)
936 if (d->care_about_geometry) {
940 int scnum = TQApplication::desktop()->screenNumber(parentWidget());
941 TQRect desk = TQApplication::desktop()->screenGeometry(scnum);
942 if ( d->defaultWindowSize.isNull() )
943 d->defaultWindowSize = TQRect(desk.width(), width(), desk.height(), height());
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()) {
948 size = TQSize( config->
readNumEntry( TQString::fromLatin1(
"Width"), 0 ),
949 config->
readNumEntry( TQString::fromLatin1(
"Height"), 0 ) );
950 if (!size.isEmpty()) {
952 config->
writeEntry( TQString::fromLatin1(
"Width"), 0 );
953 config->
writeEntry( TQString::fromLatin1(
"Height"), 0 );
956 if ( !size.isEmpty() ) {
959 if (size.width() > desk.width()) {
960 state = state | NET::MaxHoriz;
962 if (size.height() > desk.height()) {
963 state = state | NET::MaxVert;
966 if (( state & NET::Max ) == NET::Max ) {
967 resize( desk.width(), desk.height());
969 else if(( state & NET::MaxHoriz ) == NET::MaxHoriz ) {
970 resize( width(), size.height());
972 else if(( state & NET::MaxVert ) == NET::MaxVert ) {
973 resize( size.width(), height());
981 if (size.width() > desk.width() || size.height() > desk.height())
982 setWindowState( WindowMaximized );
992 return d->care_about_geometry;
997 d->care_about_geometry =
false;
1003 d->settingsDirty =
true;
1004 if ( d->autoSaveSettings )
1008 if ( !d->settingsTimer )
1010 d->settingsTimer =
new TQTimer(
this );
1013 d->settingsTimer->start( 500,
true );
1019 return d->settingsDirty;
1024 return d->autoSaveGroup;
1029 d->autoSaveSettings =
true;
1030 d->autoSaveGroup = groupName;
1033 disconnect(
this, TQT_SIGNAL( dockWindowPositionChanged( TQDockWindow * ) ),
1035 connect(
this, TQT_SIGNAL( dockWindowPositionChanged( TQDockWindow * ) ),
1044 d->autoSaveSettings =
false;
1045 if ( d->settingsTimer )
1046 d->settingsTimer->stop();
1051 return d->autoSaveSettings;
1056 return d->autoSaveGroup;
1061 Q_ASSERT( d->autoSaveSettings );
1065 d->settingsDirty =
false;
1066 if ( d->settingsTimer )
1067 d->settingsTimer->stop();
1070 void KMainWindow::resizeEvent( TQResizeEvent * )
1072 if ( d->autoSaveWindowSize )
1078 return (internalMenuBar());
1105 void KMainWindow::shuttingDown()
1109 static bool reentrancy_protection =
false;
1110 if (!reentrancy_protection)
1112 reentrancy_protection =
true;
1115 reentrancy_protection =
false;
1120 KMenuBar *KMainWindow::internalMenuBar()
1122 TQObjectList *l = queryList(
"KMenuBar", 0,
false,
false );
1123 if ( !l || !l->first() ) {
1135 TQObjectList *l = queryList(
"KStatusBar", 0,
false,
false );
1136 if ( !l || !l->first() ) {
1146 void KMainWindow::childEvent( TQChildEvent* e)
1148 TQMainWindow::childEvent( e );
1154 name =
"mainToolBar";
1158 bool honor_mode = (!strcmp(name,
"mainToolBar"));
1160 if ( builderClient() )
1161 return new KToolBar(
this, name, honor_mode);
1163 return new KToolBar(
this, DockTop,
false, name, honor_mode );
1168 toolbarList.clear();
1169 TQPtrList<TQToolBar> lst;
1170 for (
int i = (
int)TQMainWindow::DockUnmanaged; i <= (int)DockMinimized; ++i ) {
1171 lst = toolBars( (ToolBarDock)i );
1172 for ( TQToolBar *tb = lst.first(); tb; tb = lst.next() ) {
1173 if ( !tb->inherits(
"KToolBar" ) )
1175 toolbarList.append( (
KToolBar*)tb );
1178 return TQPtrListIterator<KToolBar>( toolbarList );
1184 d->kaccel =
new KAccel(
this,
"kmw-kaccel" );
1188 void KMainWindow::paintEvent( TQPaintEvent * pe )
1190 TQMainWindow::paintEvent(pe);
1196 if (tb && !tb->isHidden()) {
1197 switch( tb->barPos() )
1200 case KToolBar::Bottom:
1201 size += TQSize(0, tb->sizeHint().height());
1204 case KToolBar::Left:
1205 case KToolBar::Right:
1206 size += TQSize(
toolBar()->sizeHint().width(), 0);
1209 case KToolBar::Flat:
1210 size += TQSize(0, 3+kapp->style().pixelMetric( TQStyle::PM_DockWindowHandleExtent ));
1218 if (mb && !mb->isHidden()) {
1219 size += TQSize(0,mb->heightForWidth(size.width()));
1220 if (style().styleHint(TQStyle::SH_MainWindow_SpaceBelowMenuBar,
this))
1221 size += TQSize( 0, dockWindowsMovable() ? 1 : 2);
1223 TQStatusBar *sb = internalStatusBar();
1224 if( sb && !sb->isHidden() )
1225 size += TQSize(0, sb->sizeHint().height());
1230 #if KDE_IS_VERSION( 3, 9, 0 )
1232 #warning Remove, should be in Qt
1252 void KMainWindow::virtual_hook(
int id,
void* data )
1253 { KXMLGUIBuilder::virtual_hook(
id, data );
1254 KXMLGUIClient::virtual_hook(
id, data ); }
1258 #include "kmainwindow.moc"