// @(#)root/eve:$Id$
// Author: Matevz Tadel 2007

/*************************************************************************
 * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "TEveWindowManager.h"
#include "TEveWindow.h"

//______________________________________________________________________________
//
// Manager for EVE windows.
//
// Provides the concept of the current window and takes care for proper
// destruction of the windows.
//
// It is also the EVE-parent of windows that are not attaced into the
// hierarchy of EVE-windows.
//
// Window-manager is created by the EVE-manager and can be retrieved via:
//   gEve->GetWindowManager.

ClassImp(TEveWindowManager);

//______________________________________________________________________________
TEveWindowManager::TEveWindowManager(const char* n, const char* t) :
   TEveElementList(n, t),
   TQObject       (),
   fCurrentWindow    (0),
   fDefaultContainer (0)
{
   // Constructor.
}

//______________________________________________________________________________
TEveWindowManager::~TEveWindowManager()
{
   // Destructor.
}

//==============================================================================

//______________________________________________________________________________
void TEveWindowManager::SelectWindow(TEveWindow* window)
{
   // Entry-point for communicating the fact that a window was acted
   // upon in such a way that it should become the current window.
   // If the passed window is already the current one, it is deselcted.
   //
   // For example, this is called from title-bar, when creating a new
   // window slot, etc.
   //
   // If the change is accepted (the manager can refuse to make a
   // window current), the state of window is changed accordingly and
   // WindowSelected() signal is emitted.

   if (window == fCurrentWindow)
      window = 0;

   if (fCurrentWindow)
      fCurrentWindow->SetCurrent(kFALSE);

   fCurrentWindow = window;

   if (fCurrentWindow)
      fCurrentWindow->SetCurrent(kTRUE);

   WindowSelected(fCurrentWindow);
}

//______________________________________________________________________________
void TEveWindowManager::DeleteWindow(TEveWindow* window)
{
   // Called by a window before it gets deleted.

   if (window == fCurrentWindow)
   {
      fCurrentWindow = 0;
      WindowSelected(fCurrentWindow);
   }
   WindowDeleted(window);
}

//______________________________________________________________________________
void TEveWindowManager::WindowDocked(TEveWindow* window)
{
   // Emit the "WindowDocked(TEveWindow*)" signal.

   Emit("WindowDocked(TEveWindow*)", (Long_t)window);
}

//______________________________________________________________________________
void TEveWindowManager::WindowUndocked(TEveWindow* window)
{
   // Emit the "WindowUndocked(TEveWindow*)" signal.

   Emit("WindowUndocked(TEveWindow*)", (Long_t)window);
}

//______________________________________________________________________________
void TEveWindowManager::WindowSelected(TEveWindow* window)
{
   // Emit the "WindowSelected(TEveWindow*)" signal.

   Emit("WindowSelected(TEveWindow*)", (Long_t)window);
}

//______________________________________________________________________________
void TEveWindowManager::WindowDeleted(TEveWindow* window)
{
   // Emit the "WindowDeleted(TEveWindow*)" signal.

   Emit("WindowDeleted(TEveWindow*)", (Long_t)window);
}

//==============================================================================

//______________________________________________________________________________
TEveWindowSlot* TEveWindowManager::GetCurrentWindowAsSlot() const
{
   // Return current window dynamic-casted to TEveWindowSlot.

   return dynamic_cast<TEveWindowSlot*>(fCurrentWindow);
}

void TEveWindowManager::SetDefaultContainer(TEveWindow* w)
{
   // Set default container window.
   // It has to be able to create new slots.
   // When main-frames are closed they will place the windows here.

   static const TEveException kEH("TEveWindowManager::SetDefaultContainer ");

   if ( ! w->CanMakeNewSlots())
      throw kEH + "Given window can not make new slots.";

   fDefaultContainer = w;
}

//______________________________________________________________________________
void TEveWindowManager::DestroyWindowRecursively(TEveWindow* window)
{
   // Destroy window's children and then the window itself.
   // Protected method used during shutdown.

   while (window->HasChildren())
   {
      TEveWindow* w = dynamic_cast<TEveWindow*>(window->FirstChild());
      if (w)
         DestroyWindowRecursively(w);
      else
         window->RemoveElement(window->FirstChild());
   }
   window->DestroyWindowAndSlot();
}

//______________________________________________________________________________
void TEveWindowManager::DestroyWindows()
{
   // Wait for all windows to shut-down.

   while (HasChildren())
   {
      TEveWindow* w = dynamic_cast<TEveWindow*>(FirstChild());
      if (w)
         DestroyWindowRecursively(w);
      else
         RemoveElement(FirstChild());
   }

}

//==============================================================================

//______________________________________________________________________________
void TEveWindowManager::HideAllEveDecorations()
{
   // Hide all eve decorations (title-bar and mini-bar) on all frames.

   TEveCompositeFrame *ecf = 0;
   TIter wins(TEveCompositeFrame::fgFrameList);
   while ((ecf = (TEveCompositeFrame*) wins()))
   {
      ecf->HideAllDecorations();
      ecf->Layout();
   }
}

//______________________________________________________________________________
void TEveWindowManager::ShowNormalEveDecorations()
{
   // Show eve decorations (title-bar or mini-bar) as specified for
   // the contained window on all frames.

   TEveCompositeFrame *ecf = 0;
   TIter wins(TEveCompositeFrame::fgFrameList);
   while ((ecf = (TEveCompositeFrame*) wins()))
   {
      ecf->ShowNormalDecorations();
      ecf->Layout();
   }
}

//______________________________________________________________________________
void TEveWindowManager::SetShowTitleBars(Bool_t state)
{
   // Set show title-bar state on all frames.
   // This does not modify the per-window settings - call
   // ShowNormalEveDecorations() to restore them.

   TEveCompositeFrame *ecf = 0;
   TIter wins(TEveCompositeFrame::fgFrameList);
   while ((ecf = (TEveCompositeFrame*) wins()))
   {
      ecf->SetShowTitleBar(state);
      ecf->Layout();
   }
}
 TEveWindowManager.cxx:1
 TEveWindowManager.cxx:2
 TEveWindowManager.cxx:3
 TEveWindowManager.cxx:4
 TEveWindowManager.cxx:5
 TEveWindowManager.cxx:6
 TEveWindowManager.cxx:7
 TEveWindowManager.cxx:8
 TEveWindowManager.cxx:9
 TEveWindowManager.cxx:10
 TEveWindowManager.cxx:11
 TEveWindowManager.cxx:12
 TEveWindowManager.cxx:13
 TEveWindowManager.cxx:14
 TEveWindowManager.cxx:15
 TEveWindowManager.cxx:16
 TEveWindowManager.cxx:17
 TEveWindowManager.cxx:18
 TEveWindowManager.cxx:19
 TEveWindowManager.cxx:20
 TEveWindowManager.cxx:21
 TEveWindowManager.cxx:22
 TEveWindowManager.cxx:23
 TEveWindowManager.cxx:24
 TEveWindowManager.cxx:25
 TEveWindowManager.cxx:26
 TEveWindowManager.cxx:27
 TEveWindowManager.cxx:28
 TEveWindowManager.cxx:29
 TEveWindowManager.cxx:30
 TEveWindowManager.cxx:31
 TEveWindowManager.cxx:32
 TEveWindowManager.cxx:33
 TEveWindowManager.cxx:34
 TEveWindowManager.cxx:35
 TEveWindowManager.cxx:36
 TEveWindowManager.cxx:37
 TEveWindowManager.cxx:38
 TEveWindowManager.cxx:39
 TEveWindowManager.cxx:40
 TEveWindowManager.cxx:41
 TEveWindowManager.cxx:42
 TEveWindowManager.cxx:43
 TEveWindowManager.cxx:44
 TEveWindowManager.cxx:45
 TEveWindowManager.cxx:46
 TEveWindowManager.cxx:47
 TEveWindowManager.cxx:48
 TEveWindowManager.cxx:49
 TEveWindowManager.cxx:50
 TEveWindowManager.cxx:51
 TEveWindowManager.cxx:52
 TEveWindowManager.cxx:53
 TEveWindowManager.cxx:54
 TEveWindowManager.cxx:55
 TEveWindowManager.cxx:56
 TEveWindowManager.cxx:57
 TEveWindowManager.cxx:58
 TEveWindowManager.cxx:59
 TEveWindowManager.cxx:60
 TEveWindowManager.cxx:61
 TEveWindowManager.cxx:62
 TEveWindowManager.cxx:63
 TEveWindowManager.cxx:64
 TEveWindowManager.cxx:65
 TEveWindowManager.cxx:66
 TEveWindowManager.cxx:67
 TEveWindowManager.cxx:68
 TEveWindowManager.cxx:69
 TEveWindowManager.cxx:70
 TEveWindowManager.cxx:71
 TEveWindowManager.cxx:72
 TEveWindowManager.cxx:73
 TEveWindowManager.cxx:74
 TEveWindowManager.cxx:75
 TEveWindowManager.cxx:76
 TEveWindowManager.cxx:77
 TEveWindowManager.cxx:78
 TEveWindowManager.cxx:79
 TEveWindowManager.cxx:80
 TEveWindowManager.cxx:81
 TEveWindowManager.cxx:82
 TEveWindowManager.cxx:83
 TEveWindowManager.cxx:84
 TEveWindowManager.cxx:85
 TEveWindowManager.cxx:86
 TEveWindowManager.cxx:87
 TEveWindowManager.cxx:88
 TEveWindowManager.cxx:89
 TEveWindowManager.cxx:90
 TEveWindowManager.cxx:91
 TEveWindowManager.cxx:92
 TEveWindowManager.cxx:93
 TEveWindowManager.cxx:94
 TEveWindowManager.cxx:95
 TEveWindowManager.cxx:96
 TEveWindowManager.cxx:97
 TEveWindowManager.cxx:98
 TEveWindowManager.cxx:99
 TEveWindowManager.cxx:100
 TEveWindowManager.cxx:101
 TEveWindowManager.cxx:102
 TEveWindowManager.cxx:103
 TEveWindowManager.cxx:104
 TEveWindowManager.cxx:105
 TEveWindowManager.cxx:106
 TEveWindowManager.cxx:107
 TEveWindowManager.cxx:108
 TEveWindowManager.cxx:109
 TEveWindowManager.cxx:110
 TEveWindowManager.cxx:111
 TEveWindowManager.cxx:112
 TEveWindowManager.cxx:113
 TEveWindowManager.cxx:114
 TEveWindowManager.cxx:115
 TEveWindowManager.cxx:116
 TEveWindowManager.cxx:117
 TEveWindowManager.cxx:118
 TEveWindowManager.cxx:119
 TEveWindowManager.cxx:120
 TEveWindowManager.cxx:121
 TEveWindowManager.cxx:122
 TEveWindowManager.cxx:123
 TEveWindowManager.cxx:124
 TEveWindowManager.cxx:125
 TEveWindowManager.cxx:126
 TEveWindowManager.cxx:127
 TEveWindowManager.cxx:128
 TEveWindowManager.cxx:129
 TEveWindowManager.cxx:130
 TEveWindowManager.cxx:131
 TEveWindowManager.cxx:132
 TEveWindowManager.cxx:133
 TEveWindowManager.cxx:134
 TEveWindowManager.cxx:135
 TEveWindowManager.cxx:136
 TEveWindowManager.cxx:137
 TEveWindowManager.cxx:138
 TEveWindowManager.cxx:139
 TEveWindowManager.cxx:140
 TEveWindowManager.cxx:141
 TEveWindowManager.cxx:142
 TEveWindowManager.cxx:143
 TEveWindowManager.cxx:144
 TEveWindowManager.cxx:145
 TEveWindowManager.cxx:146
 TEveWindowManager.cxx:147
 TEveWindowManager.cxx:148
 TEveWindowManager.cxx:149
 TEveWindowManager.cxx:150
 TEveWindowManager.cxx:151
 TEveWindowManager.cxx:152
 TEveWindowManager.cxx:153
 TEveWindowManager.cxx:154
 TEveWindowManager.cxx:155
 TEveWindowManager.cxx:156
 TEveWindowManager.cxx:157
 TEveWindowManager.cxx:158
 TEveWindowManager.cxx:159
 TEveWindowManager.cxx:160
 TEveWindowManager.cxx:161
 TEveWindowManager.cxx:162
 TEveWindowManager.cxx:163
 TEveWindowManager.cxx:164
 TEveWindowManager.cxx:165
 TEveWindowManager.cxx:166
 TEveWindowManager.cxx:167
 TEveWindowManager.cxx:168
 TEveWindowManager.cxx:169
 TEveWindowManager.cxx:170
 TEveWindowManager.cxx:171
 TEveWindowManager.cxx:172
 TEveWindowManager.cxx:173
 TEveWindowManager.cxx:174
 TEveWindowManager.cxx:175
 TEveWindowManager.cxx:176
 TEveWindowManager.cxx:177
 TEveWindowManager.cxx:178
 TEveWindowManager.cxx:179
 TEveWindowManager.cxx:180
 TEveWindowManager.cxx:181
 TEveWindowManager.cxx:182
 TEveWindowManager.cxx:183
 TEveWindowManager.cxx:184
 TEveWindowManager.cxx:185
 TEveWindowManager.cxx:186
 TEveWindowManager.cxx:187
 TEveWindowManager.cxx:188
 TEveWindowManager.cxx:189
 TEveWindowManager.cxx:190
 TEveWindowManager.cxx:191
 TEveWindowManager.cxx:192
 TEveWindowManager.cxx:193
 TEveWindowManager.cxx:194
 TEveWindowManager.cxx:195
 TEveWindowManager.cxx:196
 TEveWindowManager.cxx:197
 TEveWindowManager.cxx:198
 TEveWindowManager.cxx:199
 TEveWindowManager.cxx:200
 TEveWindowManager.cxx:201
 TEveWindowManager.cxx:202
 TEveWindowManager.cxx:203
 TEveWindowManager.cxx:204
 TEveWindowManager.cxx:205
 TEveWindowManager.cxx:206
 TEveWindowManager.cxx:207
 TEveWindowManager.cxx:208
 TEveWindowManager.cxx:209
 TEveWindowManager.cxx:210
 TEveWindowManager.cxx:211
 TEveWindowManager.cxx:212
 TEveWindowManager.cxx:213
 TEveWindowManager.cxx:214
 TEveWindowManager.cxx:215
 TEveWindowManager.cxx:216
 TEveWindowManager.cxx:217
 TEveWindowManager.cxx:218
 TEveWindowManager.cxx:219
 TEveWindowManager.cxx:220
 TEveWindowManager.cxx:221
 TEveWindowManager.cxx:222
 TEveWindowManager.cxx:223