Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-08-05 22:48:11

0001 #ifndef Fireworks_TableWidget_FWTextTreeCellRenderer_h
0002 #define Fireworks_TableWidget_FWTextTreeCellRenderer_h
0003 
0004 #include <cassert>
0005 #include <iostream>
0006 
0007 #include "Fireworks/TableWidget/interface/FWTextTableCellRenderer.h"
0008 #include "Fireworks/TableWidget/interface/GlobalContexts.h"
0009 #include "Fireworks/TableWidget/interface/FWTabularWidget.h"
0010 
0011 #include "TGTextEntry.h"
0012 #include "TGPicture.h"
0013 #include "TSystem.h"
0014 #include "TGClient.h"
0015 #include "TVirtualX.h"
0016 
0017 class FWTextTreeCellRenderer : public FWTextTableCellRenderer {
0018 protected:
0019   const static int s_iconOffset = 2;
0020 
0021 public:
0022   FWTextTreeCellRenderer(const TGGC* iContext = &(getDefaultGC()),
0023                          const TGGC* iHighlightContext = &(getDefaultHighlightGC()),
0024                          Justify iJustify = kJustifyLeft)
0025       : FWTextTableCellRenderer(iContext, iHighlightContext, iJustify),
0026         m_indentation(0),
0027         m_editor(nullptr),
0028         m_showEditor(false),
0029         m_isParent(false),
0030         m_isOpen(false),
0031         m_blackIcon(true) {}
0032 
0033   // Where to find the icons
0034   static const TString& coreIcondir() {
0035     static TString path = Form("%s/src/Fireworks/Core/icons/", gSystem->Getenv("CMSSW_BASE"));
0036     if (gSystem->AccessPathName(path.Data())) {  // cannot find directory
0037       assert(gSystem->Getenv("CMSSW_RELEASE_BASE"));
0038       path = Form("%s/src/Fireworks/Core/icons/", gSystem->Getenv("CMSSW_RELEASE_BASE"));
0039     }
0040 
0041     return path;
0042   }
0043 
0044   static const TGPicture* closedImage(bool isBlack = true) {
0045     static const TGPicture* s_picture_white = gClient->GetPicture(coreIcondir() + "arrow-white-right-blackbg.png");
0046     static const TGPicture* s_picture_black = gClient->GetPicture(coreIcondir() + "arrow-black-right.png");
0047 
0048     return isBlack ? s_picture_black : s_picture_white;
0049   }
0050 
0051   static const TGPicture* openedImage(bool isBlack = true) {
0052     static const TGPicture* s_picture_white = gClient->GetPicture(coreIcondir() + "arrow-white-down-blackbg.png");
0053     static const TGPicture* s_picture_black = gClient->GetPicture(coreIcondir() + "arrow-black-down.png");
0054 
0055     return isBlack ? s_picture_black : s_picture_white;
0056   }
0057 
0058   static int iconWidth() { return openedImage(true)->GetWidth() + s_iconOffset; }
0059 
0060   virtual void setIndentation(int indentation = 0) { m_indentation = indentation; }
0061   virtual void setCellEditor(TGTextEntry* editor) { m_editor = editor; }
0062   virtual void showEditor(bool value) { m_showEditor = value; }
0063 
0064   void setIsParent(bool value) { m_isParent = value; }
0065   void setIsOpen(bool value) { m_isOpen = value; }
0066   void setBlackIcon(bool value) { m_blackIcon = value; }
0067 
0068   UInt_t width() const override {
0069     int w = FWTextTableCellRenderer::width() + 15 + m_indentation;
0070     if (m_isParent)
0071       w += iconWidth();
0072     return w;
0073   }
0074 
0075   void draw(Drawable_t iID, int iX, int iY, unsigned int iWidth, unsigned int iHeight) override {
0076     if (m_showEditor && m_editor) {
0077       //  printf("renderer draw editor %d %d %d %d \n", iX, iY, m_editor->GetWidth(), m_editor->GetHeight());
0078 
0079       // fill to cover buffer offset
0080       static TGGC editGC(FWTextTableCellRenderer::getDefaultGC());
0081       editGC.SetForeground(m_editor->GetBackground());
0082       gVirtualX->FillRectangle(iID,
0083                                editGC(),
0084                                iX - FWTabularWidget::kTextBuffer,
0085                                iY - FWTabularWidget::kTextBuffer,
0086                                iWidth + 2 * FWTabularWidget::kTextBuffer,
0087                                iHeight + 2 * FWTabularWidget::kTextBuffer);
0088 
0089       if (iY > -2) {
0090         // redraw editor
0091         if (!m_editor->IsMapped()) {
0092           m_editor->MapWindow();
0093           m_editor->SetFocus();
0094         }
0095         m_editor->MoveResize(iX, iY, m_editor->GetWidth(), m_editor->GetHeight());
0096         m_editor->SetCursorPosition(data().size());
0097         gClient->NeedRedraw(m_editor);
0098 
0099         return;
0100       } else {
0101         // hide editor if selected entry scrolled away
0102         if (m_editor->IsMapped())
0103           m_editor->UnmapWindow();
0104       }
0105     }
0106 
0107     if (selected()) {
0108       GContext_t c = highlightContext()->GetGC();
0109       gVirtualX->FillRectangle(iID,
0110                                c,
0111                                iX - FWTabularWidget::kTextBuffer,
0112                                iY - FWTabularWidget::kTextBuffer,
0113                                iWidth + 2 * FWTabularWidget::kTextBuffer,
0114                                iHeight + 2 * FWTabularWidget::kTextBuffer);
0115     }
0116     int xOffset = 0;
0117     if (m_isParent) {
0118       const TGPicture* img = m_isOpen ? openedImage(m_blackIcon) : closedImage(m_blackIcon);
0119       img->Draw(iID, graphicsContext()->GetGC(), m_indentation + iX, iY + 2);
0120       xOffset += img->GetWidth() + s_iconOffset;
0121     }
0122 
0123     FontMetrics_t metrics;
0124     font()->GetFontMetrics(&metrics);
0125 
0126     gVirtualX->DrawString(iID,
0127                           graphicsContext()->GetGC(),
0128                           iX + m_indentation + xOffset,
0129                           iY + metrics.fAscent,
0130                           data().c_str(),
0131                           data().size());
0132   }
0133 
0134 private:
0135   int m_indentation;
0136   TGTextEntry* m_editor;
0137   bool m_showEditor;
0138   bool m_isParent;
0139   bool m_isOpen;
0140   bool m_blackIcon;
0141 };
0142 
0143 #endif