// Author: Roel Aaij   14/08/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 "TGTableContainer.h"
#include "TGTableCell.h"
#include "TGLayout.h"
#include "TGWindow.h"
#include "TGScrollBar.h"
#include "TGTable.h"

ClassImp(TGTableFrame)
ClassImp(TGTableHeaderFrame)

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TGTableFrame and TGTableHeaderFrame                                  //
//                                                                      //
// TGTableFrame contains a composite frame that uses a TGMatrixLayout   //
// to Layout the frames it contains.                                    //
//                                                                      //
// TGTableHeaderFrame implements a frame used to display TGTableHeaders //
// in a TGTable.                                                        //
//                                                                      //
// Both classes are for internal use in TGTable only.                   //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

//______________________________________________________________________________
TGTableFrame::TGTableFrame(const TGWindow *p, UInt_t nrows, UInt_t ncolumns)
   : TQObject(), fFrame(0), fCanvas(0)
{
   // Create the container used to view TGTableCells. p.

   fFrame = new TGCompositeFrame(p, 10, 10, kHorizontalFrame,
                                 TGFrame::GetWhitePixel());
   fFrame->Connect("ProcessedEvent(Event_t*)", "TGTableFrame", this,
                   "HandleMouseWheel(Event_t*)");
   fCanvas = 0;
   fFrame->SetLayoutManager(new TGMatrixLayout(fFrame, nrows, ncolumns));

   gVirtualX->GrabButton(fFrame->GetId(), kAnyButton, kAnyModifier,
                         kButtonPressMask | kButtonReleaseMask |
                         kPointerMotionMask, kNone, kNone);
}

//______________________________________________________________________________
void TGTableFrame::HandleMouseWheel(Event_t *event)
{
   // Handle mouse wheel to scroll.

   if (event->fType != kButtonPress && event->fType != kButtonRelease)
      return;

   Int_t page = 0;
   if (event->fCode == kButton4 || event->fCode == kButton5) {
      if (!fCanvas) return;
      if (fCanvas->GetContainer()->GetHeight())
         page = Int_t(Float_t(fCanvas->GetViewPort()->GetHeight() *
                              fCanvas->GetViewPort()->GetHeight()) /
                              fCanvas->GetContainer()->GetHeight());
   }

   if (event->fCode == kButton4) {
      //scroll up
      Int_t newpos = fCanvas->GetVsbPosition() - page;
      if (newpos < 0) newpos = 0;
      fCanvas->SetVsbPosition(newpos);
   }
   if (event->fCode == kButton5) {
      // scroll down
      Int_t newpos = fCanvas->GetVsbPosition() + page;
      fCanvas->SetVsbPosition(newpos);
   }
}

//______________________________________________________________________________
void TGTableFrame::DrawRegion(Int_t x, Int_t y, UInt_t w, UInt_t h)
{
   // Draw a region of container in viewport.

   TGFrameElement *el;
   //   Handle_t id = fId;

   Int_t xx = fCanvas->GetX() + fCanvas->GetHsbPosition() + x; // translate coordinates to current page position
   Int_t yy = fCanvas->GetY() + fCanvas->GetVsbPosition() + y;

   TIter next(fFrame->GetList());

   while ((el = (TGFrameElement *) next())) {
      if ((Int_t(el->fFrame->GetY()) >= yy - (Int_t)el->fFrame->GetHeight()) &&
          (Int_t(el->fFrame->GetX()) >= xx - (Int_t)el->fFrame->GetWidth()) &&
          (Int_t(el->fFrame->GetY()) <= yy + Int_t(h + el->fFrame->GetHeight())) &&
          (Int_t(el->fFrame->GetX()) <= xx + Int_t(w + el->fFrame->GetWidth()))) {

         // draw either in container window or in double-buffer
         //          if (!fMapSubwindows) {
         //             el->fFrame->DrawCopy(id, el->fFrame->GetX() - pos.fX, el->fFrame->GetY() - pos.fY);
         //          } else {
         gClient->NeedRedraw(el->fFrame);
         //          }
      }
   }
}

//_____________________________________________________________________________
TGTableHeaderFrame::TGTableHeaderFrame(const TGWindow *p, TGTable *table,
                                       UInt_t w, UInt_t h, EHeaderType type,
                                       UInt_t options) :
   TGCompositeFrame(p, w, h, options), fX0(0), fY0(0), fTable(table)
{
   // TGTableHeaderFrame constuctor.

   if (type == kRowHeader) {
      ChangeOptions(GetOptions() | kVerticalFrame);
      fY0 = fTable->GetTableHeader()->GetHeight();
   } else if (type == kColumnHeader) {
      ChangeOptions(GetOptions() | kHorizontalFrame);
      fX0 = fTable->GetTableHeader()->GetWidth();
   } else {
      Error("TGTableHeaderFrame::TGTableHeaderFrame",
            "specify correct header type");
   }

}

//______________________________________________________________________________
void TGTableHeaderFrame::DrawRegion(Int_t x, Int_t y, UInt_t w, UInt_t h)
{
   // Draw a region of container in viewport.

   TGFrameElement *el;
   //   Handle_t id = fId;

   Int_t xx = fX0 + x; // translate coordinates to current page position
   Int_t yy = fY0 + y;

   TIter next(fList);

   while ((el = (TGFrameElement *) next())) {
      if ((Int_t(el->fFrame->GetY()) >= yy - (Int_t)el->fFrame->GetHeight()) &&
          (Int_t(el->fFrame->GetX()) >= xx - (Int_t)el->fFrame->GetWidth()) &&
          (Int_t(el->fFrame->GetY()) <= yy + Int_t(h + el->fFrame->GetHeight())) &&
          (Int_t(el->fFrame->GetX()) <= xx + Int_t(w + el->fFrame->GetWidth()))) {

         fClient->NeedRedraw(el->fFrame);
      }
   }
}
 TGTableContainer.cxx:1
 TGTableContainer.cxx:2
 TGTableContainer.cxx:3
 TGTableContainer.cxx:4
 TGTableContainer.cxx:5
 TGTableContainer.cxx:6
 TGTableContainer.cxx:7
 TGTableContainer.cxx:8
 TGTableContainer.cxx:9
 TGTableContainer.cxx:10
 TGTableContainer.cxx:11
 TGTableContainer.cxx:12
 TGTableContainer.cxx:13
 TGTableContainer.cxx:14
 TGTableContainer.cxx:15
 TGTableContainer.cxx:16
 TGTableContainer.cxx:17
 TGTableContainer.cxx:18
 TGTableContainer.cxx:19
 TGTableContainer.cxx:20
 TGTableContainer.cxx:21
 TGTableContainer.cxx:22
 TGTableContainer.cxx:23
 TGTableContainer.cxx:24
 TGTableContainer.cxx:25
 TGTableContainer.cxx:26
 TGTableContainer.cxx:27
 TGTableContainer.cxx:28
 TGTableContainer.cxx:29
 TGTableContainer.cxx:30
 TGTableContainer.cxx:31
 TGTableContainer.cxx:32
 TGTableContainer.cxx:33
 TGTableContainer.cxx:34
 TGTableContainer.cxx:35
 TGTableContainer.cxx:36
 TGTableContainer.cxx:37
 TGTableContainer.cxx:38
 TGTableContainer.cxx:39
 TGTableContainer.cxx:40
 TGTableContainer.cxx:41
 TGTableContainer.cxx:42
 TGTableContainer.cxx:43
 TGTableContainer.cxx:44
 TGTableContainer.cxx:45
 TGTableContainer.cxx:46
 TGTableContainer.cxx:47
 TGTableContainer.cxx:48
 TGTableContainer.cxx:49
 TGTableContainer.cxx:50
 TGTableContainer.cxx:51
 TGTableContainer.cxx:52
 TGTableContainer.cxx:53
 TGTableContainer.cxx:54
 TGTableContainer.cxx:55
 TGTableContainer.cxx:56
 TGTableContainer.cxx:57
 TGTableContainer.cxx:58
 TGTableContainer.cxx:59
 TGTableContainer.cxx:60
 TGTableContainer.cxx:61
 TGTableContainer.cxx:62
 TGTableContainer.cxx:63
 TGTableContainer.cxx:64
 TGTableContainer.cxx:65
 TGTableContainer.cxx:66
 TGTableContainer.cxx:67
 TGTableContainer.cxx:68
 TGTableContainer.cxx:69
 TGTableContainer.cxx:70
 TGTableContainer.cxx:71
 TGTableContainer.cxx:72
 TGTableContainer.cxx:73
 TGTableContainer.cxx:74
 TGTableContainer.cxx:75
 TGTableContainer.cxx:76
 TGTableContainer.cxx:77
 TGTableContainer.cxx:78
 TGTableContainer.cxx:79
 TGTableContainer.cxx:80
 TGTableContainer.cxx:81
 TGTableContainer.cxx:82
 TGTableContainer.cxx:83
 TGTableContainer.cxx:84
 TGTableContainer.cxx:85
 TGTableContainer.cxx:86
 TGTableContainer.cxx:87
 TGTableContainer.cxx:88
 TGTableContainer.cxx:89
 TGTableContainer.cxx:90
 TGTableContainer.cxx:91
 TGTableContainer.cxx:92
 TGTableContainer.cxx:93
 TGTableContainer.cxx:94
 TGTableContainer.cxx:95
 TGTableContainer.cxx:96
 TGTableContainer.cxx:97
 TGTableContainer.cxx:98
 TGTableContainer.cxx:99
 TGTableContainer.cxx:100
 TGTableContainer.cxx:101
 TGTableContainer.cxx:102
 TGTableContainer.cxx:103
 TGTableContainer.cxx:104
 TGTableContainer.cxx:105
 TGTableContainer.cxx:106
 TGTableContainer.cxx:107
 TGTableContainer.cxx:108
 TGTableContainer.cxx:109
 TGTableContainer.cxx:110
 TGTableContainer.cxx:111
 TGTableContainer.cxx:112
 TGTableContainer.cxx:113
 TGTableContainer.cxx:114
 TGTableContainer.cxx:115
 TGTableContainer.cxx:116
 TGTableContainer.cxx:117
 TGTableContainer.cxx:118
 TGTableContainer.cxx:119
 TGTableContainer.cxx:120
 TGTableContainer.cxx:121
 TGTableContainer.cxx:122
 TGTableContainer.cxx:123
 TGTableContainer.cxx:124
 TGTableContainer.cxx:125
 TGTableContainer.cxx:126
 TGTableContainer.cxx:127
 TGTableContainer.cxx:128
 TGTableContainer.cxx:129
 TGTableContainer.cxx:130
 TGTableContainer.cxx:131
 TGTableContainer.cxx:132
 TGTableContainer.cxx:133
 TGTableContainer.cxx:134
 TGTableContainer.cxx:135
 TGTableContainer.cxx:136
 TGTableContainer.cxx:137
 TGTableContainer.cxx:138
 TGTableContainer.cxx:139
 TGTableContainer.cxx:140
 TGTableContainer.cxx:141
 TGTableContainer.cxx:142
 TGTableContainer.cxx:143
 TGTableContainer.cxx:144
 TGTableContainer.cxx:145
 TGTableContainer.cxx:146
 TGTableContainer.cxx:147
 TGTableContainer.cxx:148
 TGTableContainer.cxx:149
 TGTableContainer.cxx:150
 TGTableContainer.cxx:151
 TGTableContainer.cxx:152
 TGTableContainer.cxx:153
 TGTableContainer.cxx:154
 TGTableContainer.cxx:155