Home | History | Annotate | Download | only in pwl
      1 // Copyright 2014 PDFium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
      6 
      7 #ifndef FPDFSDK_PWL_CPWL_WND_H_
      8 #define FPDFSDK_PWL_CPWL_WND_H_
      9 
     10 #include <memory>
     11 #include <vector>
     12 
     13 #include "core/fpdfdoc/cpdf_formcontrol.h"
     14 #include "core/fxcrt/observable.h"
     15 #include "core/fxcrt/unowned_ptr.h"
     16 #include "core/fxge/cfx_color.h"
     17 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
     18 #include "fpdfsdk/cpdfsdk_widget.h"
     19 #include "fpdfsdk/pwl/cpwl_timer.h"
     20 #include "fpdfsdk/pwl/cpwl_timer_handler.h"
     21 
     22 class CPWL_Edit;
     23 class CPWL_MsgControl;
     24 class CPWL_ScrollBar;
     25 class CFX_SystemHandler;
     26 class IPVT_FontMap;
     27 struct PWL_SCROLL_INFO;
     28 
     29 // window styles
     30 #define PWS_CHILD 0x80000000L
     31 #define PWS_BORDER 0x40000000L
     32 #define PWS_BACKGROUND 0x20000000L
     33 #define PWS_HSCROLL 0x10000000L
     34 #define PWS_VSCROLL 0x08000000L
     35 #define PWS_VISIBLE 0x04000000L
     36 #define PWS_READONLY 0x01000000L
     37 #define PWS_AUTOFONTSIZE 0x00800000L
     38 #define PWS_AUTOTRANSPARENT 0x00400000L
     39 #define PWS_NOREFRESHCLIP 0x00200000L
     40 
     41 // edit and label styles
     42 #define PES_MULTILINE 0x0001L
     43 #define PES_PASSWORD 0x0002L
     44 #define PES_LEFT 0x0004L
     45 #define PES_RIGHT 0x0008L
     46 #define PES_MIDDLE 0x0010L
     47 #define PES_TOP 0x0020L
     48 #define PES_BOTTOM 0x0040L
     49 #define PES_CENTER 0x0080L
     50 #define PES_CHARARRAY 0x0100L
     51 #define PES_AUTOSCROLL 0x0200L
     52 #define PES_AUTORETURN 0x0400L
     53 #define PES_UNDO 0x0800L
     54 #define PES_RICH 0x1000L
     55 #define PES_SPELLCHECK 0x2000L
     56 #define PES_TEXTOVERFLOW 0x4000L
     57 #define PES_NOREAD 0x8000L
     58 
     59 // listbox styles
     60 #define PLBS_MULTIPLESEL 0x0001L
     61 #define PLBS_HOVERSEL 0x0008L
     62 
     63 // combobox styles
     64 #define PCBS_ALLOWCUSTOMTEXT 0x0001L
     65 
     66 struct CPWL_Dash {
     67   CPWL_Dash() : nDash(0), nGap(0), nPhase(0) {}
     68   CPWL_Dash(int32_t dash, int32_t gap, int32_t phase)
     69       : nDash(dash), nGap(gap), nPhase(phase) {}
     70 
     71   void Reset() {
     72     nDash = 0;
     73     nGap = 0;
     74     nPhase = 0;
     75   }
     76 
     77   int32_t nDash;
     78   int32_t nGap;
     79   int32_t nPhase;
     80 };
     81 
     82 inline bool operator==(const CFX_Color& c1, const CFX_Color& c2) {
     83   return c1.nColorType == c2.nColorType && c1.fColor1 - c2.fColor1 < 0.0001 &&
     84          c1.fColor1 - c2.fColor1 > -0.0001 &&
     85          c1.fColor2 - c2.fColor2 < 0.0001 &&
     86          c1.fColor2 - c2.fColor2 > -0.0001 &&
     87          c1.fColor3 - c2.fColor3 < 0.0001 &&
     88          c1.fColor3 - c2.fColor3 > -0.0001 &&
     89          c1.fColor4 - c2.fColor4 < 0.0001 && c1.fColor4 - c2.fColor4 > -0.0001;
     90 }
     91 
     92 inline bool operator!=(const CFX_Color& c1, const CFX_Color& c2) {
     93   return !(c1 == c2);
     94 }
     95 
     96 #define PWL_SCROLLBAR_WIDTH 12.0f
     97 #define PWL_SCROLLBAR_TRANSPARENCY 150
     98 #define PWL_DEFAULT_BLACKCOLOR CFX_Color(CFX_Color::kGray, 0)
     99 #define PWL_DEFAULT_WHITECOLOR CFX_Color(CFX_Color::kGray, 1)
    100 
    101 class CPWL_Wnd : public CPWL_TimerHandler, public Observable<CPWL_Wnd> {
    102  public:
    103   class PrivateData {
    104    protected:
    105     ~PrivateData() {}
    106   };
    107 
    108   class ProviderIface : public Observable<ProviderIface> {
    109    public:
    110     virtual ~ProviderIface() {}
    111 
    112     // get a matrix which map user space to CWnd client space
    113     virtual CFX_Matrix GetWindowMatrix(PrivateData* pAttached) = 0;
    114   };
    115 
    116   class FocusHandlerIface {
    117    public:
    118     virtual ~FocusHandlerIface() {}
    119     virtual void OnSetFocus(CPWL_Edit* pEdit) = 0;
    120   };
    121 
    122   class CreateParams {
    123    public:
    124     CreateParams();
    125     CreateParams(const CreateParams& other);
    126     ~CreateParams();
    127 
    128     CFX_FloatRect rcRectWnd;                          // required
    129     CFX_SystemHandler* pSystemHandler;                // required
    130     IPVT_FontMap* pFontMap;                           // required
    131     ProviderIface::ObservedPtr pProvider;             // required
    132     UnownedPtr<FocusHandlerIface> pFocusHandler;      // optional
    133     uint32_t dwFlags;                                 // optional
    134     CFX_Color sBackgroundColor;                       // optional
    135     CPDFSDK_Widget::ObservedPtr pAttachedWidget;      // required
    136     BorderStyle nBorderStyle;                         // optional
    137     int32_t dwBorderWidth;                            // optional
    138     CFX_Color sBorderColor;                           // optional
    139     CFX_Color sTextColor;                             // optional
    140     int32_t nTransparency;                            // optional
    141     float fFontSize;                                  // optional
    142     CPWL_Dash sDash;                                  // optional
    143     UnownedPtr<PrivateData> pAttachedData;            // optional
    144     CPWL_Wnd* pParentWnd;                             // ignore
    145     CPWL_MsgControl* pMsgControl;                     // ignore
    146     int32_t eCursorType;                              // ignore
    147     CFX_Matrix mtChild;                               // ignore
    148   };
    149 
    150   CPWL_Wnd();
    151   ~CPWL_Wnd() override;
    152 
    153   virtual ByteString GetClassName() const;
    154 
    155   // Returns |true| iff this instance is still allocated.
    156   virtual bool InvalidateRect(CFX_FloatRect* pRect);
    157 
    158   virtual bool OnKeyDown(uint16_t nChar, uint32_t nFlag);
    159   virtual bool OnChar(uint16_t nChar, uint32_t nFlag);
    160   virtual bool OnLButtonDblClk(const CFX_PointF& point, uint32_t nFlag);
    161   virtual bool OnLButtonDown(const CFX_PointF& point, uint32_t nFlag);
    162   virtual bool OnLButtonUp(const CFX_PointF& point, uint32_t nFlag);
    163   virtual bool OnRButtonDown(const CFX_PointF& point, uint32_t nFlag);
    164   virtual bool OnRButtonUp(const CFX_PointF& point, uint32_t nFlag);
    165   virtual bool OnMouseMove(const CFX_PointF& point, uint32_t nFlag);
    166   virtual bool OnMouseWheel(short zDelta,
    167                             const CFX_PointF& point,
    168                             uint32_t nFlag);
    169   virtual void SetScrollInfo(const PWL_SCROLL_INFO& info);
    170   virtual void SetScrollPosition(float pos);
    171   virtual void ScrollWindowVertically(float pos);
    172   virtual void NotifyLButtonDown(CPWL_Wnd* child, const CFX_PointF& pos);
    173   virtual void NotifyLButtonUp(CPWL_Wnd* child, const CFX_PointF& pos);
    174   virtual void NotifyMouseMove(CPWL_Wnd* child, const CFX_PointF& pos);
    175   virtual void SetFocus();
    176   virtual void KillFocus();
    177   virtual void SetCursor();
    178 
    179   // Returns |true| iff this instance is still allocated.
    180   virtual bool SetVisible(bool bVisible);
    181   virtual void SetFontSize(float fFontSize);
    182   virtual float GetFontSize() const;
    183 
    184   virtual WideString GetSelectedText();
    185   virtual void ReplaceSelection(const WideString& text);
    186   virtual CFX_FloatRect GetFocusRect() const;
    187   virtual CFX_FloatRect GetClientRect() const;
    188 
    189   void InvalidateFocusHandler(FocusHandlerIface* handler);
    190   void InvalidateProvider(ProviderIface* provider);
    191   void Create(const CreateParams& cp);
    192   void Destroy();
    193   bool Move(const CFX_FloatRect& rcNew, bool bReset, bool bRefresh);
    194 
    195   void SetCapture();
    196   void ReleaseCapture();
    197 
    198   void DrawAppearance(CFX_RenderDevice* pDevice,
    199                       const CFX_Matrix& mtUser2Device);
    200 
    201   CFX_Color GetBackgroundColor() const;
    202   void SetBackgroundColor(const CFX_Color& color);
    203   CFX_Color GetBorderColor() const;
    204   CFX_Color GetTextColor() const;
    205   void SetTextColor(const CFX_Color& color);
    206   CFX_Color GetBorderLeftTopColor(BorderStyle nBorderStyle) const;
    207   CFX_Color GetBorderRightBottomColor(BorderStyle nBorderStyle) const;
    208 
    209   void SetBorderStyle(BorderStyle eBorderStyle);
    210   BorderStyle GetBorderStyle() const;
    211   const CPWL_Dash& GetBorderDash() const;
    212 
    213   int32_t GetBorderWidth() const;
    214   int32_t GetInnerBorderWidth() const;
    215   CFX_FloatRect GetWindowRect() const;
    216   CFX_PointF GetCenterPoint() const;
    217 
    218   bool IsVisible() const { return m_bVisible; }
    219   bool HasFlag(uint32_t dwFlags) const;
    220   void AddFlag(uint32_t dwFlags);
    221   void RemoveFlag(uint32_t dwFlags);
    222 
    223   void SetClipRect(const CFX_FloatRect& rect);
    224   const CFX_FloatRect& GetClipRect() const;
    225 
    226   CPWL_Wnd* GetParentWindow() const;
    227   PrivateData* GetAttachedData() const;
    228 
    229   bool WndHitTest(const CFX_PointF& point) const;
    230   bool ClientHitTest(const CFX_PointF& point) const;
    231   bool IsCaptureMouse() const;
    232 
    233   void EnableWindow(bool bEnable);
    234   bool IsEnabled() const { return m_bEnabled; }
    235   const CPWL_Wnd* GetFocused() const;
    236   bool IsFocused() const;
    237   bool IsReadOnly() const;
    238   CPWL_ScrollBar* GetVScrollBar() const;
    239 
    240   IPVT_FontMap* GetFontMap() const;
    241   ProviderIface* GetProvider() const;
    242   FocusHandlerIface* GetFocusHandler() const;
    243 
    244   int32_t GetTransparency();
    245   void SetTransparency(int32_t nTransparency);
    246 
    247   CFX_Matrix GetChildToRoot() const;
    248   CFX_Matrix GetChildMatrix() const;
    249   void SetChildMatrix(const CFX_Matrix& mt);
    250   CFX_Matrix GetWindowMatrix() const;
    251 
    252   virtual void OnSetFocus();
    253   virtual void OnKillFocus();
    254 
    255  protected:
    256   // CPWL_TimerHandler
    257   CFX_SystemHandler* GetSystemHandler() const override;
    258 
    259   virtual void CreateChildWnd(const CreateParams& cp);
    260 
    261   // Returns |true| iff this instance is still allocated.
    262   virtual bool RePosChildWnd();
    263 
    264   virtual void DrawThisAppearance(CFX_RenderDevice* pDevice,
    265                                   const CFX_Matrix& mtUser2Device);
    266 
    267   virtual void OnCreate(CreateParams* pParamsToAdjust);
    268   virtual void OnCreated();
    269   virtual void OnDestroy();
    270 
    271   void SetNotifyFlag(bool bNotifying = true) { m_bNotifying = bNotifying; }
    272   bool IsNotifying() const { return m_bNotifying; }
    273   bool IsValid() const { return m_bCreated; }
    274   const CreateParams& GetCreationParams() const { return m_CreationParams; }
    275 
    276   // Returns |true| iff this instance is still allocated.
    277   bool InvalidateRectMove(const CFX_FloatRect& rcOld,
    278                           const CFX_FloatRect& rcNew);
    279 
    280   bool IsWndCaptureMouse(const CPWL_Wnd* pWnd) const;
    281   bool IsWndCaptureKeyboard(const CPWL_Wnd* pWnd) const;
    282   const CPWL_Wnd* GetRootWnd() const;
    283 
    284   static bool IsCTRLpressed(uint32_t nFlag) {
    285     return CPDFSDK_FormFillEnvironment::IsCTRLKeyDown(nFlag);
    286   }
    287   static bool IsSHIFTpressed(uint32_t nFlag) {
    288     return CPDFSDK_FormFillEnvironment::IsSHIFTKeyDown(nFlag);
    289   }
    290   static bool IsALTpressed(uint32_t nFlag) {
    291     return CPDFSDK_FormFillEnvironment::IsALTKeyDown(nFlag);
    292   }
    293 
    294  private:
    295   CFX_PointF ParentToChild(const CFX_PointF& point) const;
    296   CFX_FloatRect ParentToChild(const CFX_FloatRect& rect) const;
    297 
    298   void DrawChildAppearance(CFX_RenderDevice* pDevice,
    299                            const CFX_Matrix& mtUser2Device);
    300 
    301   CFX_FloatRect PWLtoWnd(const CFX_FloatRect& rect) const;
    302 
    303   void AddChild(CPWL_Wnd* pWnd);
    304   void RemoveChild(CPWL_Wnd* pWnd);
    305 
    306   void CreateScrollBar(const CreateParams& cp);
    307   void CreateVScrollBar(const CreateParams& cp);
    308 
    309   void AdjustStyle();
    310   void CreateMsgControl();
    311   void DestroyMsgControl();
    312 
    313   CPWL_MsgControl* GetMsgControl() const;
    314 
    315   CreateParams m_CreationParams;
    316   std::vector<CPWL_Wnd*> m_Children;
    317   UnownedPtr<CPWL_ScrollBar> m_pVScrollBar;
    318   CFX_FloatRect m_rcWindow;
    319   CFX_FloatRect m_rcClip;
    320   bool m_bCreated;
    321   bool m_bVisible;
    322   bool m_bNotifying;
    323   bool m_bEnabled;
    324 };
    325 
    326 #endif  // FPDFSDK_PWL_CPWL_WND_H_
    327