TGedPatternSelect.cxx

Go to the documentation of this file.
00001 // @(#)root/ged:$Id: TGedPatternSelect.cxx 35527 2010-09-21 12:27:01Z brun $
00002 // Author: Marek Biskup, Ilka Antcheva   22/07/03
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2002, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 
00013 //////////////////////////////////////////////////////////////////////////
00014 //                                                                      //
00015 // TGedPatternFrame, TGedPatternSelector, TGedPatternPopup              //
00016 // and TGedPatternColor                                                 //
00017 //                                                                      //
00018 // The TGedPatternFrame is a small frame with border showing            //
00019 // a specific pattern (fill style.                                      //
00020 //                                                                      //
00021 // The TGedPatternSelector is a composite frame with TGedPatternFrames  //
00022 // of all diferent styles                                               //
00023 //                                                                      //
00024 // The TGedPatternPopup is a popup containing a TGedPatternSelector.    //
00025 //                                                                      //
00026 // The TGedPatternSelect widget is a button with pattern area with      //
00027 // a little down arrow. When clicked on the arrow the                   //
00028 // TGedPatternPopup pops up.                                            //
00029 //                                                                      //
00030 // Selecting a pattern in this widget will generate the event:          //
00031 // kC_PATTERNSEL, kPAT_SELCHANGED, widget id, style.                    //
00032 //                                                                      //
00033 // and the signal:                                                      //
00034 // PatternSelected(Style_t pattern)                                     //
00035 //                                                                      //
00036 // TGedSelect is button that shows popup window when clicked.           //
00037 // TGedPopup is a popup window.                                         //
00038 //                                                                      //
00039 //////////////////////////////////////////////////////////////////////////
00040 
00041 #include "TGResourcePool.h"
00042 #include "TGedPatternSelect.h"
00043 #include "TGToolTip.h"
00044 #include "TGButton.h"
00045 #include "Riostream.h"
00046 #include "RStipples.h"
00047 
00048 ClassImp(TGedPopup)
00049 ClassImp(TGedSelect)
00050 ClassImp(TGedPatternFrame)
00051 ClassImp(TGedPatternSelector)
00052 ClassImp(TGedPatternPopup)
00053 ClassImp(TGedPatternSelect)
00054 
00055 TGGC* TGedPatternFrame::fgGC = 0;
00056 
00057 
00058 //______________________________________________________________________________
00059 TGedPatternFrame::TGedPatternFrame(const TGWindow *p, Style_t pattern,
00060                                    int width, int height)
00061    : TGFrame(p, width, height, kOwnBackground)
00062 {
00063    // Pattern select ctor.
00064 
00065    Pixel_t white;
00066    gClient->GetColorByName("white", white); // white background
00067    SetBackgroundColor(white);
00068 
00069    // special case: solid
00070    if (pattern == 1001)
00071       SetBackgroundColor(0);     // if solid then black
00072 
00073    fPattern = pattern;
00074 
00075    AddInput(kButtonPressMask | kButtonReleaseMask);
00076    fMsgWindow  = p;
00077    fActive = kFALSE;
00078    snprintf(fTipText, 5, "%d", pattern);
00079 
00080    // solid and hollow must be treated separately
00081    if (pattern != 0 && pattern != 1001)
00082       fTip = new TGToolTip(fClient->GetDefaultRoot(), this, fTipText, 1000);
00083    else if (pattern == 0)
00084       fTip = new TGToolTip(fClient->GetDefaultRoot(), this, "0 - hollow", 1000);
00085    else // pattern == 1001
00086       fTip = new TGToolTip(fClient->GetDefaultRoot(), this, "1001 - solid", 1000);
00087 
00088    AddInput(kEnterWindowMask | kLeaveWindowMask);
00089 
00090    if (!fgGC) {
00091       GCValues_t gcv;
00092       gcv.fMask = kGCLineStyle  | kGCLineWidth  | kGCFillStyle |
00093                   kGCForeground | kGCBackground;
00094       gcv.fLineStyle  = kLineSolid;
00095       gcv.fLineWidth  = 0;
00096       gcv.fFillStyle  = 0;
00097       gcv.fBackground = white;
00098       gcv.fForeground = 0;    // black foreground
00099       fgGC = gClient->GetGC(&gcv, kTRUE);
00100    }
00101 }
00102 
00103 //______________________________________________________________________________
00104 Bool_t TGedPatternFrame::HandleCrossing(Event_t *event)
00105 {
00106    // Handle mouse crossing event.
00107 
00108    if (fTip) {
00109       if (event->fType == kEnterNotify)
00110          fTip->Reset();
00111       else
00112          fTip->Hide();
00113    }
00114    return kTRUE;
00115 }
00116 
00117 //______________________________________________________________________________
00118 Bool_t TGedPatternFrame::HandleButton(Event_t *event)
00119 {
00120    // Handle mouse button event.
00121 
00122    if (event->fType == kButtonPress) {
00123       SendMessage(fMsgWindow, MK_MSG(kC_PATTERNSEL, kPAT_CLICK), event->fCode, fPattern);
00124    } else {    // kButtonRelease
00125       SendMessage(fMsgWindow, MK_MSG(kC_PATTERNSEL, kPAT_SELCHANGED), event->fCode, fPattern);
00126    }
00127 
00128    return kTRUE;
00129 }
00130 
00131 //______________________________________________________________________________
00132 void TGedPatternFrame::DrawBorder()
00133 {
00134    // Draw border.
00135 
00136    gVirtualX->DrawRectangle(fId, GetBckgndGC()(), 0, 0, fWidth, fHeight);
00137    Draw3dRectangle(kDoubleBorder | kSunkenFrame, 0, 0, fWidth, fHeight);
00138 }
00139 
00140 //______________________________________________________________________________
00141 void TGedPatternFrame::DoRedraw()
00142 {
00143    // Redraw selected pattern.
00144 
00145    TGFrame::DoRedraw();
00146 
00147    if (fPattern > 3000 && fPattern < 3026) {
00148       SetFillStyle(fgGC, fPattern);
00149       gVirtualX->FillRectangle(fId, fgGC->GetGC(), 0, 0, fWidth, fHeight);
00150    }
00151 }
00152 
00153 //______________________________________________________________________________
00154 void TGedPatternFrame::SetFillStyle(TGGC* gc, Style_t fstyle)
00155 {
00156    // Set fill area style.
00157    // fstyle   : compound fill area interior style
00158    //    fstyle = 1000*interiorstyle + styleindex
00159    // this function should be in TGGC !!!
00160 
00161    Int_t style = fstyle/1000;
00162    Int_t fasi  = fstyle%1000;
00163    Int_t stn = (fasi >= 1 && fasi <=25) ? fasi : 2;
00164 
00165    static Pixmap_t fillPattern = 0;
00166 
00167    switch (style) {
00168       case 1:         // solid
00169          gc->SetFillStyle(kFillSolid);
00170          break;
00171       case 2:         // pattern
00172          break;
00173       case 3:         // hatch
00174          gc->SetFillStyle(kFillStippled);
00175          if (fillPattern != 0) {
00176             gVirtualX->DeletePixmap(fillPattern);
00177             fillPattern = 0;
00178          }
00179          fillPattern = gVirtualX->CreateBitmap(gClient->GetDefaultRoot()->GetId(),
00180                                                (const char*)gStipples[stn], 16, 16);
00181          gc->SetStipple(fillPattern);
00182          break;
00183       default:
00184         break;
00185    }
00186 }
00187 
00188 //______________________________________________________________________________
00189 TGedPatternSelector::TGedPatternSelector(const TGWindow *p) :
00190    TGCompositeFrame(p, 124, 190)
00191 {
00192    // Create pattern popup window.
00193 
00194    SetLayoutManager(new TGTileLayout(this, 1));
00195 
00196    Int_t i;
00197    for (i = 1; i <= 25; i++)
00198       fCe[i-1] = new TGedPatternFrame(this, 3000 + i);
00199 
00200    fCe[25] = new TGedPatternFrame(this, 0);
00201    fCe[26] = new TGedPatternFrame(this, 1001);
00202 
00203    for (i = 0; i < 27; i++)
00204       AddFrame(fCe[i], new TGLayoutHints(kLHintsNoHints));
00205 
00206    fMsgWindow  = p;
00207    fActive = -1;
00208 }
00209 
00210 //______________________________________________________________________________
00211 TGedPatternSelector::~TGedPatternSelector()
00212 {
00213    // Delete pattern popup window.
00214 
00215    Cleanup();
00216 }
00217 
00218 //______________________________________________________________________________
00219 void TGedPatternSelector::SetActive(Int_t newat)
00220 {
00221    // Set selected the current style.
00222 
00223    if (fActive != newat) {
00224       if ((fActive >= 0) && (fActive < 27)) {
00225          fCe[fActive]->SetActive(kFALSE);
00226       }
00227       fActive = newat;
00228       if ((fActive >= 0) && (fActive < 27)) {
00229          fCe[fActive]->SetActive(kTRUE);
00230       }
00231    }
00232 }
00233 
00234 //______________________________________________________________________________
00235 Bool_t TGedPatternSelector::ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2)
00236 {
00237    // Process message generated by pattern popup window.
00238 
00239    switch (GET_MSG(msg)) {
00240       case kC_PATTERNSEL:
00241          switch (GET_SUBMSG(msg)) {
00242             case kPAT_SELCHANGED:
00243                switch (parm1) {
00244                   case kButton1:
00245                      SendMessage(fMsgWindow, MK_MSG(kC_PATTERNSEL,
00246                                  kPAT_SELCHANGED), parm1, parm2);
00247                      break;
00248                }
00249                break;
00250             case kPAT_CLICK:
00251                switch (parm1) {
00252                   case kButton1:
00253                      SetActive(parm2);
00254                      break;
00255                }
00256                break;
00257          }
00258    }
00259 
00260    return kTRUE;
00261 }
00262 
00263 //______________________________________________________________________________
00264 TGedPopup::TGedPopup(const TGWindow *p, const TGWindow *m, UInt_t w, UInt_t h,
00265                      UInt_t options, Pixel_t back)
00266    : TGCompositeFrame(p, w, h, options, back)
00267 {
00268    // Create a popup frame.
00269 
00270    fMsgWindow = m;
00271    SetWindowAttributes_t wattr;
00272 
00273    wattr.fMask = kWAOverrideRedirect | kWASaveUnder ;
00274    wattr.fOverrideRedirect = kTRUE;
00275    wattr.fSaveUnder = kTRUE;
00276    gVirtualX->ChangeWindowAttributes(fId, &wattr);
00277 
00278    AddInput(kStructureNotifyMask);
00279 }
00280 
00281 //______________________________________________________________________________
00282 void TGedPopup::EndPopup()
00283 {
00284    // Ungrab pointer and unmap popup window.
00285 
00286    gVirtualX->GrabPointer(0, 0, 0, 0, kFALSE);
00287    UnmapWindow();
00288 }
00289 
00290 //______________________________________________________________________________
00291 void TGedPopup::PlacePopup(Int_t x, Int_t y, UInt_t w, UInt_t h)
00292 {
00293    // Place popup window at the specified place.
00294 
00295    Int_t rx, ry;
00296    UInt_t rw, rh;
00297 
00298    // Parent is root window for the popup:
00299    gVirtualX->GetWindowSize(fParent->GetId(), rx, ry, rw, rh);
00300 
00301    if (x < 0) x = 0;
00302    if (x + fWidth > rw) x = rw - fWidth;
00303    if (y < 0) y = 0;
00304    if (y + fHeight > rh) y = rh - fHeight;
00305 
00306    MoveResize(x, y, w, h);
00307    MapSubwindows();
00308    Layout();
00309    MapRaised();
00310 
00311    gVirtualX->GrabPointer(fId,
00312                           kButtonPressMask | kButtonReleaseMask | kPointerMotionMask,
00313                           kNone, kNone, fClient->GetResourcePool()->GetGrabCursor());
00314    gClient->WaitForUnmap(this);
00315    EndPopup();
00316 }
00317 
00318 //______________________________________________________________________________
00319 Bool_t TGedPopup::HandleButton(Event_t *event)
00320 {
00321    // Handle mouse button event in popup window.
00322 
00323    if ((event->fX < 0) || (event->fX >= (Int_t) fWidth) ||
00324        (event->fY < 0) || (event->fY >= (Int_t) fHeight))  {
00325 
00326       if (event->fType == kButtonRelease) EndPopup();
00327 
00328    } else {
00329       TGFrame *f = GetFrameFromPoint(event->fX, event->fY);
00330       if (f && f != this) {
00331          TranslateCoordinates(f, event->fX, event->fY, event->fX, event->fY);
00332          f->HandleButton(event);
00333       }
00334    }
00335    return kTRUE;
00336 }
00337 
00338 //______________________________________________________________________________
00339 Bool_t TGedPopup::ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2)
00340 {
00341    // Process messages generated by popup window.
00342 
00343    switch (GET_MSG(msg)) {
00344       case kC_POPUP:
00345          switch (GET_SUBMSG(msg)) {
00346             case kPOP_HIDE:
00347                EndPopup();
00348                SendMessage(fMsgWindow, MK_MSG(kC_POPUP, kPOP_HIDE),
00349                            parm1, parm2);
00350                break;
00351             default:
00352                break;
00353          }
00354          break;
00355    }
00356    return kTRUE;
00357 }
00358 
00359 //______________________________________________________________________________
00360 TGedPatternPopup::TGedPatternPopup(const TGWindow *p, const TGWindow *m, Style_t pattern)
00361    : TGedPopup(p, m, 10, 10, kDoubleBorder | kRaisedFrame | kOwnBackground,
00362                GetDefaultFrameBackground())
00363 {
00364    // Pattern popup constructor.
00365 
00366    fCurrentPattern = pattern;
00367 
00368    TGedPatternSelector *ps = new TGedPatternSelector(this);
00369    AddFrame(ps, new TGLayoutHints(kLHintsCenterX, 1, 1, 1, 1));
00370 
00371    MapSubwindows();
00372    Resize(ps->GetDefaultWidth() + 6, ps->GetDefaultHeight());
00373 }
00374 
00375 //______________________________________________________________________________
00376 TGedPatternPopup::~TGedPatternPopup()
00377 {
00378    // Destructor of pattern popup window.
00379 
00380    Cleanup();
00381 }
00382 
00383 //______________________________________________________________________________
00384 Bool_t TGedPatternPopup::ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2)
00385 {
00386    // Process messages generated by pattern popup window.
00387 
00388    switch (GET_MSG(msg)) {
00389       case kC_PATTERNSEL:
00390          switch (GET_SUBMSG(msg)) {
00391             case kPAT_SELCHANGED:
00392                SendMessage(fMsgWindow, MK_MSG(kC_PATTERNSEL, kPAT_SELCHANGED),
00393                            parm1, parm2);
00394                UnmapWindow();
00395                break;
00396 
00397             default:
00398                break;
00399          }
00400          break;
00401    }
00402    return kTRUE;
00403 }
00404 
00405 //______________________________________________________________________________
00406 TGedSelect::TGedSelect(const TGWindow *p, Int_t id)
00407    : TGCheckButton(p, "", id)
00408 {
00409    // Create pattern select button.
00410 
00411    fPopup = 0;
00412 
00413    GCValues_t gcv;
00414    gcv.fMask = kGCLineStyle  | kGCLineWidth  | kGCFillStyle |
00415                kGCForeground | kGCBackground;
00416    gcv.fLineStyle  = kLineSolid;
00417    gcv.fLineWidth  = 0;
00418    gcv.fFillStyle  = 0;
00419    Pixel_t white;
00420    gClient->GetColorByName("white", white); // white background
00421    gcv.fBackground = white;
00422    gcv.fForeground = 0;    // black foreground
00423    fDrawGC = gClient->GetGC(&gcv, kTRUE);
00424 
00425    Enable();
00426    SetState(kButtonUp);
00427    AddInput(kButtonPressMask | kButtonReleaseMask);
00428 }
00429 
00430 //______________________________________________________________________________
00431 TGedSelect::~TGedSelect()
00432 {
00433    // Destructor of pattern select button.
00434 
00435    if (fPopup)
00436       delete fPopup;
00437    fClient->FreeGC(fDrawGC);
00438 }
00439 
00440 //______________________________________________________________________________
00441 Bool_t TGedSelect::HandleButton(Event_t *event)
00442 {
00443    // Handle mouse button events in pattern select button.
00444 
00445    TGFrame::HandleButton(event);
00446 
00447    if (!IsEnabled()) return kTRUE;
00448 
00449    if (event->fCode != kButton1) return kFALSE;
00450 
00451    if ((event->fType == kButtonPress) && HasFocus()) WantFocus();
00452 
00453    if (event->fType == kButtonPress) {
00454       if (fState != kButtonDown) {
00455          fPrevState = fState;
00456          SetState(kButtonDown);
00457       }
00458    } else {
00459       if (fState != kButtonUp) {
00460          SetState(kButtonUp);
00461          Window_t wdummy;
00462          Int_t ax, ay;
00463          if (fPopup) {
00464             gVirtualX->TranslateCoordinates(fId, gClient->GetDefaultRoot()->GetId(),
00465                                             0, fHeight, ax, ay, wdummy);
00466             fPopup->PlacePopup(ax, ay, fPopup->GetDefaultWidth(),
00467                                fPopup->GetDefaultHeight());
00468          }
00469       }
00470    }
00471    return kTRUE;
00472 }
00473 
00474 //______________________________________________________________________________
00475 void TGedSelect::Enable()
00476 {
00477    // Set widget state flag (kTRUE=enabled, kFALSE=disabled).
00478 
00479    SetFlags(kWidgetIsEnabled);
00480    fClient->NeedRedraw(this);
00481 }
00482 
00483 //______________________________________________________________________________
00484 void TGedSelect::Disable()
00485 {
00486    // Clear widget state flag.
00487 
00488    ClearFlags(kWidgetIsEnabled);
00489    fClient->NeedRedraw(this);
00490 }
00491 
00492 //________________________________________________________________________________
00493 void TGedSelect::DoRedraw()
00494 {
00495    // Draw separator and arrow.
00496 
00497    Int_t  x, y;
00498    UInt_t h;
00499 
00500    TGButton::DoRedraw();
00501 
00502    if (IsEnabled()) {
00503 
00504       // separator
00505       x = fWidth - 6 - fBorderWidth - 6;
00506       y = fBorderWidth + 1;
00507       h = fHeight - fBorderWidth - 1;  // actually y1
00508 
00509       if (fState == kButtonDown) { ++x; ++y; }
00510 
00511       gVirtualX->DrawLine(fId, GetShadowGC()(),  x, y, x, h - 2);
00512       gVirtualX->DrawLine(fId, GetHilightGC()(), x + 1, y, x + 1, h - 1);
00513       gVirtualX->DrawLine(fId, GetHilightGC()(), x, h - 1, x + 1, h - 1);
00514 
00515       // arrow
00516 
00517       x = fWidth - 6 - fBorderWidth - 2;
00518       y = (fHeight - 4) / 2 + 1;
00519 
00520       if (fState == kButtonDown) { ++x; ++y; }
00521 
00522       DrawTriangle(GetShadowGC()(), x, y);
00523 
00524    } else {
00525 
00526       // separator
00527       x = fWidth - 6 - fBorderWidth - 6;
00528       y = fBorderWidth + 1;
00529       h = fHeight - fBorderWidth - 1;  // actually y1
00530 
00531       gVirtualX->DrawLine(fId, GetShadowGC()(),  x, y, x, h - 2);
00532       gVirtualX->DrawLine(fId, GetHilightGC()(), x + 1, y, x + 1, h - 1);
00533       gVirtualX->DrawLine(fId, GetHilightGC()(), x, h - 1, x + 1, h - 1);
00534 
00535       // sunken arrow
00536 
00537       x = fWidth - 6 - fBorderWidth - 2;
00538       y = (fHeight - 4) / 2 + 1;
00539 
00540       DrawTriangle(GetHilightGC()(), x + 1, y + 1);
00541       DrawTriangle(GetShadowGC()(), x, y);
00542    }
00543 }
00544 
00545 //______________________________________________________________________________
00546 void TGedSelect::DrawTriangle(GContext_t gc, Int_t x, Int_t y)
00547 {
00548    // Draw small triangle.
00549 
00550    Point_t points[3];
00551 
00552    points[0].fX = x;
00553    points[0].fY = y;
00554    points[1].fX = x + 5;
00555    points[1].fY = y;
00556    points[2].fX = x + 2;
00557    points[2].fY = y + 3;
00558 
00559    gVirtualX->FillPolygon(fId, gc, points, 3);
00560 }
00561 
00562 
00563 //______________________________________________________________________________
00564 TGedPatternSelect::TGedPatternSelect(const TGWindow *p, Style_t pattern, Int_t id)
00565    : TGedSelect(p, id)
00566 {
00567    // Create and pop up pattern select window.
00568 
00569    fPattern = pattern;
00570    SetPopup(new TGedPatternPopup(gClient->GetDefaultRoot(), this, fPattern));
00571    SetPattern(fPattern);
00572 }
00573 
00574 //______________________________________________________________________________
00575 Bool_t TGedPatternSelect::ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2)
00576 {
00577    // Process message according to the user input.
00578 
00579    if (GET_MSG(msg) == kC_PATTERNSEL && GET_SUBMSG(msg) == kPAT_SELCHANGED)
00580    {
00581       SetPattern(parm2);
00582       parm1 = (Long_t)fWidgetId;
00583       SendMessage(fMsgWindow, MK_MSG(kC_PATTERNSEL, kPAT_SELCHANGED),
00584                   parm1, parm2);
00585    }
00586    return kTRUE;
00587 }
00588 
00589 //______________________________________________________________________________
00590 void TGedPatternSelect::DoRedraw()
00591 {
00592    // Draw selected pattern as current one.
00593 
00594    TGedSelect::DoRedraw();
00595 
00596    Int_t  x, y;
00597    UInt_t w, h;
00598 
00599    if (IsEnabled()) { // pattern rectangle
00600 
00601       x = fBorderWidth + 2;
00602       y = fBorderWidth + 2;  // 1;
00603       h = fHeight - (fBorderWidth * 2) - 4;  // -3;  // 14
00604       w = h * 2;
00605       if (fState == kButtonDown) { ++x; ++y; }
00606 
00607       gVirtualX->DrawRectangle(fId, GetShadowGC()(), x, y, w - 1, h - 1);
00608 
00609       TGedPatternFrame::SetFillStyle(fDrawGC, 1001);
00610 
00611       Pixel_t white;
00612       gClient->GetColorByName("white", white); // white background
00613       fDrawGC->SetForeground(white);
00614       gVirtualX->FillRectangle(fId, fDrawGC->GetGC(), x + 1, y + 1, w - 2, h - 2);
00615 
00616       if (fPattern != 0) {
00617          fDrawGC->SetForeground(0);
00618          TGedPatternFrame::SetFillStyle(fDrawGC, fPattern);
00619          gVirtualX->FillRectangle(fId, fDrawGC->GetGC(), x + 1, y + 1, w - 2, h - 2);
00620       }
00621    } else { // sunken rectangle
00622 
00623       x = fBorderWidth + 2;
00624       y = fBorderWidth + 2;  // 1;
00625       w = 42;
00626       h = fHeight - (fBorderWidth * 2) - 4;  // 3;
00627       Draw3dRectangle(kSunkenFrame, x, y, w, h);
00628    }
00629 }
00630 
00631 //______________________________________________________________________________
00632 void TGedPatternSelect::SetPattern(Style_t pattern, Bool_t emit)
00633 {
00634    // Set pattern.
00635 
00636    fPattern = pattern;
00637    gClient->NeedRedraw(this);
00638    if (emit)
00639       PatternSelected(fPattern);
00640 }
00641 
00642 //______________________________________________________________________________
00643 void TGedPatternSelect::SavePrimitive(ostream &out, Option_t * /*= ""*/)
00644 {
00645    // Save the pattern select widget as a C++ statement(s) on output stream out
00646 
00647    out <<"   TGedPatternSelect *";
00648    out << GetName() << " = new TGedPatternSelect(" << fParent->GetName()
00649        << "," << fPattern << "," << WidgetId() << ");" << endl;
00650 }

Generated on Tue Jul 5 14:19:47 2011 for ROOT_528-00b_version by  doxygen 1.5.1