Home | History | Annotate | Download | only in pdfwindow
      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_PDFWINDOW_PWL_WND_H_
      8 #define FPDFSDK_PDFWINDOW_PWL_WND_H_
      9 
     10 #include <memory>
     11 #include <vector>
     12 
     13 #include "core/fpdfdoc/cpdf_formcontrol.h"
     14 #include "core/fxcrt/cfx_observable.h"
     15 #include "core/fxcrt/fx_basic.h"
     16 #include "fpdfsdk/cfx_systemhandler.h"
     17 #include "fpdfsdk/cpdfsdk_widget.h"
     18 #include "fpdfsdk/pdfwindow/cpwl_color.h"
     19 
     20 class CPWL_MsgControl;
     21 class CPWL_ScrollBar;
     22 class CPWL_Timer;
     23 class CPWL_TimerHandler;
     24 class CPWL_Wnd;
     25 class CFX_SystemHandler;
     26 class IPVT_FontMap;
     27 class IPWL_Provider;
     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_DISABLE 0x02000000L
     37 #define PWS_READONLY 0x01000000L
     38 #define PWS_AUTOFONTSIZE 0x00800000L
     39 #define PWS_AUTOTRANSPARENT 0x00400000L
     40 #define PWS_NOREFRESHCLIP 0x00200000L
     41 
     42 // edit and label styles
     43 #define PES_MULTILINE 0x0001L
     44 #define PES_PASSWORD 0x0002L
     45 #define PES_LEFT 0x0004L
     46 #define PES_RIGHT 0x0008L
     47 #define PES_MIDDLE 0x0010L
     48 #define PES_TOP 0x0020L
     49 #define PES_BOTTOM 0x0040L
     50 #define PES_CENTER 0x0080L
     51 #define PES_CHARARRAY 0x0100L
     52 #define PES_AUTOSCROLL 0x0200L
     53 #define PES_AUTORETURN 0x0400L
     54 #define PES_UNDO 0x0800L
     55 #define PES_RICH 0x1000L
     56 #define PES_SPELLCHECK 0x2000L
     57 #define PES_TEXTOVERFLOW 0x4000L
     58 #define PES_NOREAD 0x8000L
     59 
     60 // listbox styles
     61 #define PLBS_MULTIPLESEL 0x0001L
     62 #define PLBS_HOVERSEL 0x0008L
     63 
     64 // combobox styles
     65 #define PCBS_ALLOWCUSTOMTEXT 0x0001L
     66 
     67 // richedit styles
     68 #define PRES_MULTILINE 0x0001L
     69 #define PRES_AUTORETURN 0x0002L
     70 #define PRES_AUTOSCROLL 0x0004L
     71 #define PRES_UNDO 0x0100L
     72 #define PRES_MULTIPAGES 0x0200L
     73 #define PRES_TEXTOVERFLOW 0x0400L
     74 
     75 // notification messages
     76 #define PNM_ADDCHILD 0x00000000L
     77 #define PNM_REMOVECHILD 0x00000001L
     78 #define PNM_SETSCROLLINFO 0x00000002L
     79 #define PNM_SETSCROLLPOS 0x00000003L
     80 #define PNM_SCROLLWINDOW 0x00000004L
     81 #define PNM_LBUTTONDOWN 0x00000005L
     82 #define PNM_LBUTTONUP 0x00000006L
     83 #define PNM_MOUSEMOVE 0x00000007L
     84 #define PNM_NOTERESET 0x00000008L
     85 #define PNM_SETCARETINFO 0x00000009L
     86 #define PNM_SELCHANGED 0x0000000AL
     87 #define PNM_NOTEEDITCHANGED 0x0000000BL
     88 
     89 #define PWL_CLASSNAME_EDIT "CPWL_Edit"
     90 
     91 struct CPWL_Dash {
     92   CPWL_Dash() : nDash(0), nGap(0), nPhase(0) {}
     93   CPWL_Dash(int32_t dash, int32_t gap, int32_t phase)
     94       : nDash(dash), nGap(gap), nPhase(phase) {}
     95 
     96   void Reset() {
     97     nDash = 0;
     98     nGap = 0;
     99     nPhase = 0;
    100   }
    101 
    102   int32_t nDash;
    103   int32_t nGap;
    104   int32_t nPhase;
    105 };
    106 
    107 inline bool operator==(const CPWL_Color& c1, const CPWL_Color& c2) {
    108   return c1.nColorType == c2.nColorType && c1.fColor1 - c2.fColor1 < 0.0001 &&
    109          c1.fColor1 - c2.fColor1 > -0.0001 &&
    110          c1.fColor2 - c2.fColor2 < 0.0001 &&
    111          c1.fColor2 - c2.fColor2 > -0.0001 &&
    112          c1.fColor3 - c2.fColor3 < 0.0001 &&
    113          c1.fColor3 - c2.fColor3 > -0.0001 &&
    114          c1.fColor4 - c2.fColor4 < 0.0001 && c1.fColor4 - c2.fColor4 > -0.0001;
    115 }
    116 
    117 inline bool operator!=(const CPWL_Color& c1, const CPWL_Color& c2) {
    118   return !(c1 == c2);
    119 }
    120 
    121 #define PWL_SCROLLBAR_WIDTH 12.0f
    122 #define PWL_SCROLLBAR_BUTTON_WIDTH 9.0f
    123 #define PWL_SCROLLBAR_POSBUTTON_MINWIDTH 2.0f
    124 #define PWL_SCROLLBAR_TRANSPARENCY 150
    125 #define PWL_SCROLLBAR_BKCOLOR \
    126   CPWL_Color(COLORTYPE_RGB, 220.0f / 255.0f, 220.0f / 255.0f, 220.0f / 255.0f)
    127 #define PWL_DEFAULT_SELTEXTCOLOR CPWL_Color(COLORTYPE_RGB, 1, 1, 1)
    128 #define PWL_DEFAULT_SELBACKCOLOR \
    129   CPWL_Color(COLORTYPE_RGB, 0, 51.0f / 255.0f, 113.0f / 255.0f)
    130 #define PWL_DEFAULT_BACKCOLOR PWL_DEFAULT_SELTEXTCOLOR
    131 #define PWL_DEFAULT_TEXTCOLOR CPWL_Color(COLORTYPE_RGB, 0, 0, 0)
    132 #define PWL_DEFAULT_FONTSIZE 9.0f
    133 #define PWL_DEFAULT_BLACKCOLOR CPWL_Color(COLORTYPE_GRAY, 0)
    134 #define PWL_DEFAULT_WHITECOLOR CPWL_Color(COLORTYPE_GRAY, 1)
    135 #define PWL_DEFAULT_HEAVYGRAYCOLOR CPWL_Color(COLORTYPE_GRAY, 0.50)
    136 #define PWL_DEFAULT_LIGHTGRAYCOLOR CPWL_Color(COLORTYPE_GRAY, 0.75)
    137 #define PWL_TRIANGLE_HALFLEN 2.0f
    138 #define PWL_CBBUTTON_TRIANGLE_HALFLEN 3.0f
    139 #define PWL_INVALIDATE_INFLATE 2
    140 
    141 class IPWL_Provider : public CFX_Observable<IPWL_Provider> {
    142  public:
    143   virtual ~IPWL_Provider() {}
    144 
    145   // get a matrix which map user space to CWnd client space
    146   virtual CFX_Matrix GetWindowMatrix(void* pAttachedData) = 0;
    147 
    148   /*
    149   0 L"&Undo\tCtrl+Z"
    150   1 L"&Redo\tCtrl+Shift+Z"
    151   2 L"Cu&t\tCtrl+X"
    152   3 L"&Copy\tCtrl+C"
    153   4 L"&Paste\tCtrl+V"
    154   5 L"&Delete"
    155   6  L"&Select All\tCtrl+A"
    156   */
    157   virtual CFX_WideString LoadPopupMenuString(int32_t nIndex) = 0;
    158 };
    159 
    160 class IPWL_FocusHandler {
    161  public:
    162   virtual ~IPWL_FocusHandler() {}
    163   virtual void OnSetFocus(CPWL_Wnd* pWnd) = 0;
    164 };
    165 
    166 struct PWL_CREATEPARAM {
    167  public:
    168   PWL_CREATEPARAM();
    169   PWL_CREATEPARAM(const PWL_CREATEPARAM& other);
    170 
    171   void Reset() {
    172     rcRectWnd.Reset();
    173     pSystemHandler = nullptr;
    174     pFontMap = nullptr;
    175     pProvider.Reset();
    176     pFocusHandler = nullptr;
    177     dwFlags = 0;
    178     sBackgroundColor.Reset();
    179     pAttachedWidget.Reset();
    180     nBorderStyle = BorderStyle::SOLID;
    181     dwBorderWidth = 0;
    182     sBorderColor.Reset();
    183     sTextColor.Reset();
    184     nTransparency = 0;
    185     fFontSize = 0.0f;
    186     sDash.Reset();
    187     pAttachedData = nullptr;
    188     pParentWnd = nullptr;
    189     pMsgControl = nullptr;
    190     eCursorType = 0;
    191     mtChild.SetIdentity();
    192   }
    193 
    194   CFX_FloatRect rcRectWnd;            // required
    195   CFX_SystemHandler* pSystemHandler;  // required
    196   IPVT_FontMap* pFontMap;             // required
    197   IPWL_Provider::ObservedPtr pProvider;  // required
    198   IPWL_FocusHandler* pFocusHandler;   // optional
    199   uint32_t dwFlags;                   // optional
    200   CPWL_Color sBackgroundColor;        // optional
    201   CPDFSDK_Widget::ObservedPtr pAttachedWidget;  // required
    202   BorderStyle nBorderStyle;           // optional
    203   int32_t dwBorderWidth;              // optional
    204   CPWL_Color sBorderColor;            // optional
    205   CPWL_Color sTextColor;              // optional
    206   int32_t nTransparency;              // optional
    207   FX_FLOAT fFontSize;                 // optional
    208   CPWL_Dash sDash;                    // optional
    209   void* pAttachedData;                // optional
    210   CPWL_Wnd* pParentWnd;               // ignore
    211   CPWL_MsgControl* pMsgControl;       // ignore
    212   int32_t eCursorType;                // ignore
    213   CFX_Matrix mtChild;                 // ignore
    214 };
    215 
    216 class CPWL_Timer {
    217  public:
    218   CPWL_Timer(CPWL_TimerHandler* pAttached, CFX_SystemHandler* pSystemHandler);
    219   virtual ~CPWL_Timer();
    220 
    221   int32_t SetPWLTimer(int32_t nElapse);
    222   void KillPWLTimer();
    223   static void TimerProc(int32_t idEvent);
    224 
    225  private:
    226   int32_t m_nTimerID;
    227   CPWL_TimerHandler* m_pAttached;
    228   CFX_SystemHandler* m_pSystemHandler;
    229 };
    230 
    231 class CPWL_TimerHandler {
    232  public:
    233   CPWL_TimerHandler();
    234   virtual ~CPWL_TimerHandler();
    235 
    236   void BeginTimer(int32_t nElapse);
    237   void EndTimer();
    238   virtual void TimerProc();
    239   virtual CFX_SystemHandler* GetSystemHandler() const = 0;
    240 
    241  private:
    242   std::unique_ptr<CPWL_Timer> m_pTimer;
    243 };
    244 
    245 class CPWL_Wnd : public CPWL_TimerHandler {
    246  public:
    247   CPWL_Wnd();
    248   ~CPWL_Wnd() override;
    249 
    250   virtual CFX_ByteString GetClassName() const;
    251   virtual void InvalidateRect(CFX_FloatRect* pRect = nullptr);
    252 
    253   virtual bool OnKeyDown(uint16_t nChar, uint32_t nFlag);
    254   virtual bool OnChar(uint16_t nChar, uint32_t nFlag);
    255   virtual bool OnLButtonDblClk(const CFX_PointF& point, uint32_t nFlag);
    256   virtual bool OnLButtonDown(const CFX_PointF& point, uint32_t nFlag);
    257   virtual bool OnLButtonUp(const CFX_PointF& point, uint32_t nFlag);
    258   virtual bool OnRButtonDown(const CFX_PointF& point, uint32_t nFlag);
    259   virtual bool OnRButtonUp(const CFX_PointF& point, uint32_t nFlag);
    260   virtual bool OnMouseMove(const CFX_PointF& point, uint32_t nFlag);
    261   virtual bool OnMouseWheel(short zDelta,
    262                             const CFX_PointF& point,
    263                             uint32_t nFlag);
    264   virtual void OnNotify(CPWL_Wnd* pWnd,
    265                         uint32_t msg,
    266                         intptr_t wParam = 0,
    267                         intptr_t lParam = 0);
    268   virtual void SetFocus();
    269   virtual void KillFocus();
    270   virtual void SetCursor();
    271   virtual void SetVisible(bool bVisible);
    272   virtual void SetFontSize(FX_FLOAT fFontSize);
    273   virtual FX_FLOAT GetFontSize() const;
    274 
    275   virtual CFX_FloatRect GetFocusRect() const;
    276   virtual CFX_FloatRect GetClientRect() const;
    277 
    278   void InvalidateFocusHandler(IPWL_FocusHandler* handler);
    279   void InvalidateProvider(IPWL_Provider* provider);
    280   void Create(const PWL_CREATEPARAM& cp);
    281   void Destroy();
    282   void Move(const CFX_FloatRect& rcNew, bool bReset, bool bRefresh);
    283 
    284   void SetCapture();
    285   void ReleaseCapture();
    286 
    287   void DrawAppearance(CFX_RenderDevice* pDevice, CFX_Matrix* pUser2Device);
    288 
    289   CPWL_Color GetBackgroundColor() const;
    290   void SetBackgroundColor(const CPWL_Color& color);
    291   CPWL_Color GetBorderColor() const;
    292   CPWL_Color GetTextColor() const;
    293   void SetTextColor(const CPWL_Color& color);
    294   CPWL_Color GetBorderLeftTopColor(BorderStyle nBorderStyle) const;
    295   CPWL_Color GetBorderRightBottomColor(BorderStyle nBorderStyle) const;
    296 
    297   void SetBorderStyle(BorderStyle eBorderStyle);
    298   BorderStyle GetBorderStyle() const;
    299   const CPWL_Dash& GetBorderDash() const;
    300 
    301   int32_t GetBorderWidth() const;
    302   int32_t GetInnerBorderWidth() const;
    303   CFX_FloatRect GetWindowRect() const;
    304   CFX_PointF GetCenterPoint() const;
    305 
    306   bool IsVisible() const { return m_bVisible; }
    307   bool HasFlag(uint32_t dwFlags) const;
    308   void AddFlag(uint32_t dwFlags);
    309   void RemoveFlag(uint32_t dwFlags);
    310 
    311   void SetClipRect(const CFX_FloatRect& rect);
    312   const CFX_FloatRect& GetClipRect() const;
    313 
    314   CPWL_Wnd* GetParentWindow() const;
    315   void* GetAttachedData() const;
    316 
    317   bool WndHitTest(const CFX_PointF& point) const;
    318   bool ClientHitTest(const CFX_PointF& point) const;
    319   bool IsCaptureMouse() const;
    320 
    321   void EnableWindow(bool bEnable);
    322   bool IsEnabled() const { return m_bEnabled; }
    323   const CPWL_Wnd* GetFocused() const;
    324   bool IsFocused() const;
    325   bool IsReadOnly() const;
    326   CPWL_ScrollBar* GetVScrollBar() const;
    327 
    328   IPVT_FontMap* GetFontMap() const;
    329   IPWL_Provider* GetProvider() const;
    330   IPWL_FocusHandler* GetFocusHandler() const;
    331 
    332   int32_t GetTransparency();
    333   void SetTransparency(int32_t nTransparency);
    334 
    335   CFX_Matrix GetChildToRoot() const;
    336   CFX_Matrix GetChildMatrix() const;
    337   void SetChildMatrix(const CFX_Matrix& mt);
    338   CFX_Matrix GetWindowMatrix() const;
    339 
    340  protected:
    341   friend class CPWL_MsgControl;
    342 
    343   // CPWL_TimerHandler
    344   CFX_SystemHandler* GetSystemHandler() const override;
    345 
    346   virtual void CreateChildWnd(const PWL_CREATEPARAM& cp);
    347   virtual void RePosChildWnd();
    348   virtual void GetThisAppearanceStream(CFX_ByteTextBuf& sAppStream);
    349 
    350   virtual void DrawThisAppearance(CFX_RenderDevice* pDevice,
    351                                   CFX_Matrix* pUser2Device);
    352 
    353   virtual void OnCreate(PWL_CREATEPARAM& cp);
    354   virtual void OnCreated();
    355   virtual void OnDestroy();
    356 
    357   virtual void OnSetFocus();
    358   virtual void OnKillFocus();
    359 
    360   void GetAppearanceStream(CFX_ByteTextBuf& sAppStream);
    361   void SetNotifyFlag(bool bNotifying = true) { m_bNotifying = bNotifying; }
    362 
    363   bool IsValid() const;
    364   const PWL_CREATEPARAM& GetCreationParam() const;
    365   bool IsNotifying() const { return m_bNotifying; }
    366 
    367   void InvalidateRectMove(const CFX_FloatRect& rcOld,
    368                           const CFX_FloatRect& rcNew);
    369 
    370   bool IsWndCaptureMouse(const CPWL_Wnd* pWnd) const;
    371   bool IsWndCaptureKeyboard(const CPWL_Wnd* pWnd) const;
    372   const CPWL_Wnd* GetRootWnd() const;
    373 
    374   bool IsCTRLpressed(uint32_t nFlag) const;
    375   bool IsSHIFTpressed(uint32_t nFlag) const;
    376   bool IsALTpressed(uint32_t nFlag) const;
    377 
    378  private:
    379   CFX_PointF ParentToChild(const CFX_PointF& point) const;
    380   CFX_FloatRect ParentToChild(const CFX_FloatRect& rect) const;
    381 
    382   void GetChildAppearanceStream(CFX_ByteTextBuf& sAppStream);
    383   void DrawChildAppearance(CFX_RenderDevice* pDevice, CFX_Matrix* pUser2Device);
    384 
    385   FX_RECT PWLtoWnd(const CFX_FloatRect& rect) const;
    386 
    387   void AddChild(CPWL_Wnd* pWnd);
    388   void RemoveChild(CPWL_Wnd* pWnd);
    389 
    390   void CreateScrollBar(const PWL_CREATEPARAM& cp);
    391   void CreateVScrollBar(const PWL_CREATEPARAM& cp);
    392 
    393   void AdjustStyle();
    394   void CreateMsgControl();
    395   void DestroyMsgControl();
    396 
    397   CPWL_MsgControl* GetMsgControl() const;
    398 
    399   std::vector<CPWL_Wnd*> m_Children;
    400   PWL_CREATEPARAM m_sPrivateParam;
    401   CPWL_ScrollBar* m_pVScrollBar;
    402   CFX_FloatRect m_rcWindow;
    403   CFX_FloatRect m_rcClip;
    404   bool m_bCreated;
    405   bool m_bVisible;
    406   bool m_bNotifying;
    407   bool m_bEnabled;
    408 };
    409 
    410 #endif  // FPDFSDK_PDFWINDOW_PWL_WND_H_
    411