home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Common / DXUTgui.h < prev    next >
Encoding:
C/C++ Source or Header  |  2004-09-30  |  47.4 KB  |  1,114 lines

  1. //--------------------------------------------------------------------------------------
  2. // File: DXUTgui.h
  3. //
  4. // Desc: 
  5. //
  6. // Copyright (c) Microsoft Corporation. All rights reserved.
  7. //--------------------------------------------------------------------------------------
  8. #pragma once
  9. #ifndef DXUT_GUI_H
  10. #define DXUT_GUI_H
  11.  
  12. #include <usp10.h>
  13. #include <dimm.h>
  14.  
  15.  
  16. //--------------------------------------------------------------------------------------
  17. // Defines and macros 
  18. //--------------------------------------------------------------------------------------
  19. #define EVENT_BUTTON_CLICKED                0x0101
  20. #define EVENT_COMBOBOX_SELECTION_CHANGED    0x0201
  21. #define EVENT_RADIOBUTTON_CHANGED           0x0301
  22. #define EVENT_CHECKBOX_CHANGED              0x0401
  23. #define EVENT_SLIDER_VALUE_CHANGED          0x0501
  24. #define EVENT_EDITBOX_STRING                0x0601
  25. // EVENT_EDITBOX_CHANGE is sent when the listbox content changes
  26. // due to user input.
  27. #define EVENT_EDITBOX_CHANGE                0x0602
  28. #define EVENT_LISTBOX_ITEM_DBLCLK           0x0701
  29. // EVENT_LISTBOX_SELECTION is fired off when the selection changes in
  30. // a single selection list box.
  31. #define EVENT_LISTBOX_SELECTION             0x0702
  32.  
  33.  
  34. //--------------------------------------------------------------------------------------
  35. // Forward declarations
  36. //--------------------------------------------------------------------------------------
  37. class CDXUTControl;
  38. class CDXUTButton;
  39. class CDXUTStatic;
  40. class CDXUTCheckBox;
  41. class CDXUTRadioButton;
  42. class CDXUTComboBox;
  43. class CDXUTSlider;
  44. class CDXUTEditBox;
  45. class CDXUTIMEEditBox;
  46. class CDXUTListBox;
  47. class CDXUTScrollBar;
  48. class CDXUTElement;
  49. struct DXUTElementHolder;
  50. struct DXUTTextureNode;
  51. struct DXUTFontNode;
  52. typedef VOID (CALLBACK *PCALLBACKDXUTGUIEVENT) ( UINT nEvent, int nControlID, CDXUTControl* pControl );
  53.  
  54.  
  55. //--------------------------------------------------------------------------------------
  56. // Enums for pre-defined control types
  57. //--------------------------------------------------------------------------------------
  58. enum DXUT_CONTROL_TYPE 
  59.     DXUT_CONTROL_BUTTON, 
  60.     DXUT_CONTROL_STATIC, 
  61.     DXUT_CONTROL_CHECKBOX,
  62.     DXUT_CONTROL_RADIOBUTTON,
  63.     DXUT_CONTROL_COMBOBOX,
  64.     DXUT_CONTROL_SLIDER,
  65.     DXUT_CONTROL_EDITBOX,
  66.     DXUT_CONTROL_IMEEDITBOX,
  67.     DXUT_CONTROL_LISTBOX,
  68.     DXUT_CONTROL_SCROLLBAR,
  69. };
  70.  
  71. enum DXUT_CONTROL_STATE
  72. {
  73.     DXUT_STATE_NORMAL = 0,
  74.     DXUT_STATE_DISABLED,
  75.     DXUT_STATE_HIDDEN,
  76.     DXUT_STATE_FOCUS,
  77.     DXUT_STATE_MOUSEOVER,
  78.     DXUT_STATE_PRESSED,
  79. };
  80.  
  81. #define MAX_CONTROL_STATES 6
  82.  
  83. struct DXUTBlendColor
  84. {
  85.     void Init( D3DCOLOR defaultColor, D3DCOLOR disabledColor = D3DCOLOR_ARGB(200, 128, 128, 128), D3DCOLOR hiddenColor = 0 );
  86.     void Blend( UINT iState, float fElapsedTime, float fRate = 0.7f );
  87.     
  88.     D3DCOLOR  States[ MAX_CONTROL_STATES ]; // Modulate colors for all possible control states
  89.     D3DXCOLOR Current;
  90. };
  91.  
  92.  
  93. //-----------------------------------------------------------------------------
  94. // Contains all the display tweakables for a sub-control
  95. //-----------------------------------------------------------------------------
  96. class CDXUTElement
  97. {
  98. public:
  99.     void SetTexture( UINT iTexture, RECT* prcTexture, D3DCOLOR defaultTextureColor = D3DCOLOR_ARGB(255, 255, 255, 255) );
  100.     void SetFont( UINT iFont, D3DCOLOR defaultFontColor = D3DCOLOR_ARGB(255, 255, 255, 255), DWORD dwTextFormat = DT_CENTER | DT_VCENTER );
  101.     
  102.     void Refresh();
  103.     
  104.     UINT iTexture;          // Index of the texture for this Element 
  105.     UINT iFont;             // Index of the font for this Element
  106.     DWORD dwTextFormat;     // The format argument to DrawText 
  107.  
  108.     RECT rcTexture;         // Bounding rect of this element on the composite texture
  109.     
  110.     DXUTBlendColor TextureColor;
  111.     DXUTBlendColor FontColor;
  112. };
  113.  
  114.  
  115. //-----------------------------------------------------------------------------
  116. // All controls must be assigned to a dialog, which handles
  117. // input and rendering for the controls.
  118. //-----------------------------------------------------------------------------
  119. class CDXUTDialog
  120. {
  121. public:
  122.     CDXUTDialog();
  123.     ~CDXUTDialog();
  124.  
  125.     // Windows message handler
  126.     bool MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  127.  
  128.     // Control creation
  129.     HRESULT AddStatic( int ID, LPCWSTR strText, int x, int y, int width, int height, bool bIsDefault=false, CDXUTStatic** ppCreated=NULL );
  130.     HRESULT AddButton( int ID, LPCWSTR strText, int x, int y, int width, int height, UINT nHotkey=0, bool bIsDefault=false, CDXUTButton** ppCreated=NULL );
  131.     HRESULT AddCheckBox( int ID, LPCWSTR strText, int x, int y, int width, int height, bool bChecked=false, UINT nHotkey=0, bool bIsDefault=false, CDXUTCheckBox** ppCreated=NULL );
  132.     HRESULT AddRadioButton( int ID, UINT nButtonGroup, LPCWSTR strText, int x, int y, int width, int height, bool bChecked=false, UINT nHotkey=0, bool bIsDefault=false, CDXUTRadioButton** ppCreated=NULL );
  133.     HRESULT AddComboBox( int ID, int x, int y, int width, int height, UINT nHotKey=0, bool bIsDefault=false, CDXUTComboBox** ppCreated=NULL );
  134.     HRESULT AddSlider( int ID, int x, int y, int width, int height, int min=0, int max=100, int value=50, bool bIsDefault=false, CDXUTSlider** ppCreated=NULL );
  135.     HRESULT AddEditBox( int ID, LPCWSTR strText, int x, int y, int width, int height, bool bIsDefault=false, CDXUTEditBox** ppCreated=NULL );
  136.     HRESULT AddIMEEditBox( int ID, LPCWSTR strText, int x, int y, int width, int height, bool bIsDefault=false, CDXUTIMEEditBox** ppCreated=NULL );
  137.     HRESULT AddListBox( int ID, int x, int y, int width, int height, DWORD dwStyle=0, CDXUTListBox** ppCreated=NULL );
  138.     HRESULT AddControl( CDXUTControl* pControl );
  139.     HRESULT InitControl( CDXUTControl* pControl );
  140.  
  141.     // Control retrieval
  142.     CDXUTStatic*      GetStatic( int ID ) { return (CDXUTStatic*) GetControl( ID, DXUT_CONTROL_STATIC ); }
  143.     CDXUTButton*      GetButton( int ID ) { return (CDXUTButton*) GetControl( ID, DXUT_CONTROL_BUTTON ); }
  144.     CDXUTCheckBox*    GetCheckBox( int ID ) { return (CDXUTCheckBox*) GetControl( ID, DXUT_CONTROL_CHECKBOX ); }
  145.     CDXUTRadioButton* GetRadioButton( int ID ) { return (CDXUTRadioButton*) GetControl( ID, DXUT_CONTROL_RADIOBUTTON ); }
  146.     CDXUTComboBox*    GetComboBox( int ID ) { return (CDXUTComboBox*) GetControl( ID, DXUT_CONTROL_COMBOBOX ); }
  147.     CDXUTSlider*      GetSlider( int ID ) { return (CDXUTSlider*) GetControl( ID, DXUT_CONTROL_SLIDER ); }
  148.     CDXUTEditBox*     GetEditBox( int ID ) { return (CDXUTEditBox*) GetControl( ID, DXUT_CONTROL_EDITBOX ); }
  149.     CDXUTIMEEditBox*  GetIMEEditBox( int ID ) { return (CDXUTIMEEditBox*) GetControl( ID, DXUT_CONTROL_IMEEDITBOX ); }
  150.     CDXUTListBox*     GetListBox( int ID ) { return (CDXUTListBox*) GetControl( ID, DXUT_CONTROL_LISTBOX ); }
  151.  
  152.     CDXUTControl* GetControl( int ID );
  153.     CDXUTControl* GetControl( int ID, UINT nControlType );
  154.     CDXUTControl* GetControlAtPoint( POINT pt );
  155.  
  156.     bool GetControlEnabled( int ID );
  157.     void SetControlEnabled( int ID, bool bEnabled );
  158.  
  159.     void ClearRadioButtonGroup( UINT nGroup );
  160.     void ClearComboBox( int ID );
  161.  
  162.     // Access the default display Elements used when adding new controls
  163.     HRESULT       SetDefaultElement( UINT nControlType, UINT iElement, CDXUTElement* pElement );
  164.     CDXUTElement* GetDefaultElement( UINT nControlType, UINT iElement );
  165.  
  166.     // Methods called by controls
  167.     void SendEvent( UINT nEvent, bool bTriggeredByUser, CDXUTControl* pControl );
  168.     void RequestFocus( CDXUTControl* pControl );
  169.  
  170.     // Render helpers
  171.     HRESULT DrawRect( RECT* pRect, D3DCOLOR color );
  172.     HRESULT DrawPolyLine( POINT* apPoints, UINT nNumPoints, D3DCOLOR color );
  173.     HRESULT DrawSprite( CDXUTElement* pElement, RECT* prcDest );
  174.     HRESULT CalcTextRect( LPCWSTR strText, CDXUTElement* pElement, RECT* prcDest, int nCount = -1 );
  175.     HRESULT DrawText( LPCWSTR strText, CDXUTElement* pElement, RECT* prcDest, bool bShadow = false, int nCount = -1 );
  176.  
  177.     // Attributes
  178.     bool GetMinimized() { return m_bMinimized; }
  179.     void SetMinimized( bool bMinimized ) { m_bMinimized = bMinimized; }
  180.     void SetBackgroundColors( D3DCOLOR colorAllCorners ) { SetBackgroundColors( colorAllCorners, colorAllCorners, colorAllCorners, colorAllCorners ); }
  181.     void SetBackgroundColors( D3DCOLOR colorTopLeft, D3DCOLOR colorTopRight, D3DCOLOR colorBottomLeft, D3DCOLOR colorBottomRight );
  182.     void EnableCaption( bool bEnable ) { m_bCaption = bEnable; }
  183.     int GetCaptionHeight() const { return m_nCaptionHeight; }
  184.     void SetCaptionHeight( int nHeight ) { m_nCaptionHeight = nHeight; }
  185.     void SetCaptionText( const WCHAR *pwszText ) { wcsncpy( m_wszCaption, pwszText, sizeof(m_wszCaption)/sizeof(m_wszCaption[0]) ); m_wszCaption[sizeof(m_wszCaption)/sizeof(m_wszCaption[0])-1] = 0; }
  186.     void GetLocation( POINT &Pt ) const { Pt.x = m_x; Pt.y = m_y; }
  187.     void SetLocation( int x, int y ) { m_x = x; m_y = y; }
  188.     void SetSize( int width, int height ) { m_width = width; m_height = height;  }
  189.     int GetWidth() { return m_width; }
  190.     int GetHeight() { return m_height; }
  191.  
  192.     void SetNextDialog( CDXUTDialog* pNextDialog );
  193.  
  194.     static void SetRefreshTime( float fTime ){ s_fTimeRefresh = fTime; }
  195.  
  196.     static CDXUTControl* GetNextControl( CDXUTControl* pControl );
  197.     static CDXUTControl* GetPrevControl( CDXUTControl* pControl );
  198.  
  199.     void RemoveControl( int ID );
  200.     void RemoveAllControls();
  201.  
  202.     // Sets the callback used to notify the app of control events
  203.     void SetCallback( PCALLBACKDXUTGUIEVENT pCallback ) { m_pCallbackEvent = pCallback; }
  204.     void EnableNonUserEvents( bool bEnable ) { m_bNonUserEvents = bEnable; }
  205.     void EnableKeyboardInput( bool bEnable ) { m_bKeyboardInput = bEnable; }
  206.     void EnableMouseInput( bool bEnable ) { m_bMouseInput = bEnable; }
  207.  
  208.     // Device state notification
  209.     void Refresh();
  210.     HRESULT OnRender( float fElapsedTime );    
  211.  
  212.     // Shared resource access. Indexed fonts and textures are shared among
  213.     // all the controls.
  214.     HRESULT       SetFont( UINT index, LPCWSTR strFaceName, LONG height, LONG weight );
  215.     DXUTFontNode* GetFont( UINT index );
  216.  
  217.     HRESULT          SetTexture( UINT index, LPCWSTR strFilename );
  218.     DXUTTextureNode* GetTexture( UINT index );
  219.  
  220.     static void ClearFocus();
  221.     void FocusDefaultControl();
  222.  
  223.     bool m_bNonUserEvents;
  224.     bool m_bKeyboardInput;
  225.     bool m_bMouseInput;
  226.  
  227. private:
  228.     int m_nDefaultControlID;
  229.  
  230.     static double s_fTimeRefresh;
  231.     double m_fTimeLastRefresh;
  232.  
  233.     // Initialize default Elements
  234.     void InitDefaultElements();
  235.  
  236.     // Windows message handlers
  237.     void OnMouseMove( POINT pt );
  238.     void OnMouseUp( POINT pt );
  239.  
  240.     // Control events
  241.     void OnCycleFocus( bool bForward );
  242.     void OnMouseEnter( CDXUTControl* pControl );
  243.     void OnMouseLeave( CDXUTControl* pControl );
  244.  
  245.     static CDXUTControl* s_pControlFocus;        // The control which has focus
  246.     static CDXUTControl* s_pControlPressed;      // The control currently pressed
  247.  
  248.     CDXUTControl* m_pControlMouseOver;           // The control which is hovered over
  249.  
  250.     bool m_bCaption;
  251.     bool m_bMinimized;
  252.     WCHAR m_wszCaption[256];
  253.  
  254.     int m_x;
  255.     int m_y;
  256.     int m_width;
  257.     int m_height;
  258.     int m_nCaptionHeight;
  259.  
  260.     D3DCOLOR m_colorTopLeft;
  261.     D3DCOLOR m_colorTopRight;
  262.     D3DCOLOR m_colorBottomLeft;
  263.     D3DCOLOR m_colorBottomRight;
  264.  
  265.     PCALLBACKDXUTGUIEVENT m_pCallbackEvent;
  266.  
  267.     CGrowableArray< int > m_Textures;   // Index into m_TextureCache;
  268.     CGrowableArray< int > m_Fonts;      // Index into m_FontCache;
  269.  
  270.     CGrowableArray< CDXUTControl* > m_Controls;
  271.     CGrowableArray< DXUTElementHolder* > m_DefaultElements;
  272.  
  273.     CDXUTElement m_CapElement;  // Element for the caption
  274.  
  275.     CDXUTDialog* m_pNextDialog;
  276.     CDXUTDialog* m_pPrevDialog;
  277. };
  278.  
  279.  
  280. //--------------------------------------------------------------------------------------
  281. // Structs for shared resources
  282. //--------------------------------------------------------------------------------------
  283. struct DXUTTextureNode
  284. {
  285.     WCHAR strFilename[MAX_PATH];
  286.     IDirect3DTexture9* pTexture;
  287.     DWORD dwWidth;
  288.     DWORD dwHeight;
  289. };
  290.  
  291. struct DXUTFontNode
  292. {
  293.     WCHAR strFace[MAX_PATH];
  294.     ID3DXFont* pFont;
  295.     LONG  nHeight;
  296.     LONG  nWeight;
  297. };
  298.  
  299.  
  300. //-----------------------------------------------------------------------------
  301. // Manages shared resources of dialogs
  302. // Use DXUTGetGlobalDialogResourceManager() to get access to the global instance
  303. //-----------------------------------------------------------------------------
  304. class CDXUTDialogResourceManager
  305. {
  306. public:
  307.     int AddFont( LPCWSTR strFaceName, LONG height, LONG weight );
  308.     int AddTexture( LPCWSTR strFilename );
  309.  
  310.     DXUTFontNode*     GetFontNode( int iIndex )     { return m_FontCache.GetAt( iIndex ); };
  311.     DXUTTextureNode*  GetTextureNode( int iIndex )  { return m_TextureCache.GetAt( iIndex ); };    
  312.     IDirect3DDevice9* GetD3DDevice()                { return m_pd3dDevice; }
  313.  
  314.     // Shared between all dialogs
  315.     IDirect3DStateBlock9* m_pStateBlock;
  316.     ID3DXSprite*          m_pSprite;          // Sprite used for drawing
  317.  
  318.     ~CDXUTDialogResourceManager();
  319.  
  320. protected:
  321.     friend CDXUTDialogResourceManager* DXUTGetGlobalDialogResourceManager();
  322.     friend HRESULT DXUTInitialize3DEnvironment();
  323.     friend HRESULT DXUTReset3DEnvironment();
  324.     friend void DXUTCleanup3DEnvironment( bool bReleaseSettings );
  325.  
  326.     // Use DXUTGetGlobalDialogResourceManager() to get access to the global instance
  327.     CDXUTDialogResourceManager();
  328.  
  329.     // The sample framework uses the global instance and automatically calls the device events
  330.     HRESULT     OnCreateDevice( LPDIRECT3DDEVICE9 pd3dDevice );
  331.     HRESULT     OnResetDevice();
  332.     void        OnLostDevice();
  333.     void        OnDestroyDevice();
  334.  
  335.     CGrowableArray< DXUTTextureNode* > m_TextureCache;   // Shared textures
  336.     CGrowableArray< DXUTFontNode* > m_FontCache;         // Shared fonts
  337.     
  338.     IDirect3DDevice9* m_pd3dDevice;
  339.  
  340.     // Resource creation helpers
  341.     HRESULT CreateFont( UINT index );
  342.     HRESULT CreateTexture( UINT index );
  343. };
  344.  
  345. CDXUTDialogResourceManager* DXUTGetGlobalDialogResourceManager();
  346.  
  347.  
  348. //-----------------------------------------------------------------------------
  349. // Base class for controls
  350. //-----------------------------------------------------------------------------
  351. class CDXUTControl
  352. {
  353. public:
  354.     CDXUTControl( CDXUTDialog *pDialog = NULL );
  355.     virtual ~CDXUTControl();
  356.  
  357.     virtual HRESULT OnInit() { return S_OK; }
  358.     virtual void Refresh();
  359.     virtual void Render( IDirect3DDevice9* pd3dDevice, float fElapsedTime ) { };
  360.  
  361.     // Windows message handler
  362.     virtual bool MsgProc( UINT uMsg, WPARAM wParam, LPARAM lParam ) { return false; }
  363.  
  364.     virtual bool HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam ) { return false; }
  365.     virtual bool HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lParam ) { return false; }
  366.  
  367.     virtual bool CanHaveFocus() { return false; }
  368.     virtual void OnFocusIn() { m_bHasFocus = true; }
  369.     virtual void OnFocusOut() { m_bHasFocus = false; }
  370.     virtual void OnMouseEnter() { m_bMouseOver = true; }
  371.     virtual void OnMouseLeave() { m_bMouseOver = false; }
  372.     virtual void OnHotkey() {}
  373.  
  374.     virtual BOOL ContainsPoint( POINT pt ) { return PtInRect( &m_rcBoundingBox, pt ); }
  375.  
  376.     virtual void SetEnabled( bool bEnabled ) { m_bEnabled = bEnabled; }
  377.     virtual bool GetEnabled() { return m_bEnabled; }
  378.     virtual void SetVisible( bool bVisible ) { m_bVisible = bVisible; }
  379.     virtual bool GetVisible() { return m_bVisible; }
  380.  
  381.     UINT GetType() const { return m_Type; }
  382.  
  383.     int  GetID() const { return m_ID; }
  384.     void SetID( int ID ) { m_ID = ID; }
  385.  
  386.     void SetLocation( int x, int y ) { m_x = x; m_y = y; UpdateRects(); }
  387.     void SetSize( int width, int height ) { m_width = width; m_height = height; UpdateRects(); }
  388.  
  389.     void SetHotkey( UINT nHotkey ) { m_nHotkey = nHotkey; }
  390.     UINT GetHotkey() { return m_nHotkey; }
  391.  
  392.     void SetUserData( void *pUserData ) { m_pUserData = pUserData; }
  393.     void *GetUserData() const { return m_pUserData; }
  394.  
  395.     virtual void SetTextColor( D3DCOLOR Color );
  396.     CDXUTElement* GetElement( UINT iElement ) { return m_Elements.GetAt( iElement ); }
  397.     HRESULT SetElement( UINT iElement, CDXUTElement* pElement);
  398.  
  399.     bool m_bVisible;                // Shown/hidden flag
  400.     bool m_bMouseOver;              // Mouse pointer is above control
  401.     bool m_bHasFocus;               // Control has input focus
  402.     bool m_bIsDefault;              // Is the default control
  403.  
  404.     // Size, scale, and positioning members
  405.     int m_x, m_y;
  406.     int m_width, m_height;
  407.  
  408.     // These members are set by the container
  409.     CDXUTDialog* m_pDialog;    // Parent container
  410.     UINT m_Index;              // Index within the control list
  411.     
  412.     CGrowableArray< CDXUTElement* > m_Elements;  // All display elements
  413.  
  414. protected:
  415.     virtual void UpdateRects();
  416.  
  417.     int  m_ID;                 // ID number
  418.     DXUT_CONTROL_TYPE m_Type;  // Control type, set once in constructor  
  419.     UINT m_nHotkey;            // Virtual key code for this control's hotkey
  420.     void *m_pUserData;         // Data associated with this control that is set by user.
  421.     
  422.     bool m_bEnabled;           // Enabled/disabled flag
  423.     
  424.     RECT m_rcBoundingBox;      // Rectangle defining the active region of the control
  425. };
  426.  
  427.  
  428. //-----------------------------------------------------------------------------
  429. // Contains all the display information for a given control type
  430. //-----------------------------------------------------------------------------
  431. struct DXUTElementHolder
  432. {
  433.     UINT nControlType;
  434.     UINT iElement;
  435.  
  436.     CDXUTElement Element;
  437. };
  438.  
  439.  
  440. //-----------------------------------------------------------------------------
  441. // Static control
  442. //-----------------------------------------------------------------------------
  443. class CDXUTStatic : public CDXUTControl
  444. {
  445. public:
  446.     CDXUTStatic( CDXUTDialog *pDialog = NULL );
  447.  
  448.     virtual void Render( IDirect3DDevice9* pd3dDevice, float fElapsedTime );
  449.     virtual BOOL ContainsPoint( POINT pt ) { return false; }
  450.  
  451.     HRESULT GetTextCopy( LPWSTR strDest, UINT bufferCount );
  452.     LPCWSTR GetText() { return m_strText; }
  453.     HRESULT SetText( LPCWSTR strText );
  454.  
  455.  
  456. protected:
  457.     WCHAR m_strText[MAX_PATH];      // Window text  
  458. };
  459.  
  460.  
  461. //-----------------------------------------------------------------------------
  462. // Button control
  463. //-----------------------------------------------------------------------------
  464. class CDXUTButton : public CDXUTStatic
  465. {
  466. public:
  467.     CDXUTButton( CDXUTDialog *pDialog = NULL );
  468.  
  469.     virtual bool HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam );
  470.     virtual bool HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lParam );
  471.     virtual void OnHotkey() { m_pDialog->SendEvent( EVENT_BUTTON_CLICKED, true, this ); }
  472.     
  473.     virtual BOOL ContainsPoint( POINT pt ) { return PtInRect( &m_rcBoundingBox, pt ); }
  474.     virtual bool CanHaveFocus() { return (m_bVisible && m_bEnabled); }
  475.  
  476.     virtual void Render( IDirect3DDevice9* pd3dDevice, float fElapsedTime );
  477.  
  478. protected:
  479.     bool m_bPressed;
  480. };
  481.  
  482.  
  483. //-----------------------------------------------------------------------------
  484. // CheckBox control
  485. //-----------------------------------------------------------------------------
  486. class CDXUTCheckBox : public CDXUTButton
  487. {
  488. public:
  489.     CDXUTCheckBox( CDXUTDialog *pDialog = NULL );
  490.  
  491.     virtual bool HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam );
  492.     virtual bool HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lParam );
  493.     virtual void OnHotkey() { SetCheckedInternal( !m_bChecked, true ); }
  494.  
  495.     virtual BOOL ContainsPoint( POINT pt ); 
  496.     virtual void UpdateRects(); 
  497.  
  498.     virtual void Render( IDirect3DDevice9* pd3dDevice, float fElapsedTime );
  499.  
  500.     bool GetChecked() { return m_bChecked; }
  501.     void SetChecked( bool bChecked ) { SetCheckedInternal( bChecked, false ); }
  502.     
  503. protected:
  504.     virtual void SetCheckedInternal( bool bChecked, bool bFromInput );
  505.  
  506.     bool m_bChecked;
  507.     RECT m_rcButton;
  508.     RECT m_rcText;
  509. };
  510.  
  511.  
  512. //-----------------------------------------------------------------------------
  513. // RadioButton control
  514. //-----------------------------------------------------------------------------
  515. class CDXUTRadioButton : public CDXUTCheckBox
  516. {
  517. public:
  518.     CDXUTRadioButton( CDXUTDialog *pDialog = NULL );
  519.  
  520.     virtual bool HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam );
  521.     virtual bool HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lParam );
  522.     virtual void OnHotkey() { SetCheckedInternal( true, true, true ); }
  523.     
  524.     void SetChecked( bool bChecked, bool bClearGroup=true ) { SetCheckedInternal( bChecked, bClearGroup, false ); }
  525.     void SetButtonGroup( UINT nButtonGroup ) { m_nButtonGroup = nButtonGroup; }
  526.     UINT GetButtonGroup() { return m_nButtonGroup; }
  527.     
  528. protected:
  529.     virtual void SetCheckedInternal( bool bChecked, bool bClearGroup, bool bFromInput );
  530.     UINT m_nButtonGroup;
  531. };
  532.  
  533.  
  534. //-----------------------------------------------------------------------------
  535. // Scrollbar control
  536. //-----------------------------------------------------------------------------
  537. class CDXUTScrollBar : public CDXUTControl
  538. {
  539. public:
  540.     CDXUTScrollBar( CDXUTDialog *pDialog = NULL );
  541.     virtual ~CDXUTScrollBar();
  542.  
  543.     virtual bool    HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam );
  544.     virtual bool    HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lParam );
  545.  
  546.     virtual void Render( IDirect3DDevice9* pd3dDevice, float fElapsedTime );
  547.     virtual void    UpdateRects();
  548.  
  549.     void SetTrackRange( int nStart, int nEnd );
  550.     int GetTrackPos() { return m_nPosition; }
  551.     void SetTrackPos( int nPosition ) { m_nPosition = nPosition; Cap(); UpdateThumbRect(); }
  552.     int GetPageSize() { return m_nPageSize; }
  553.     void SetPageSize( int nPageSize ) { m_nPageSize = nPageSize; Cap(); UpdateThumbRect(); }
  554.  
  555.     void Scroll( int nDelta );    // Scroll by nDelta items (plus or minus)
  556.     void ShowItem( int nIndex );  // Ensure that item nIndex is displayed, scroll if necessary
  557.  
  558. protected:
  559.     // ARROWSTATE indicates the state of the arrow buttons.
  560.     // CLEAR            No arrow is down.
  561.     // CLICKED_UP       Up arrow is clicked.
  562.     // CLICKED_DOWN     Down arrow is clicked.
  563.     // HELD_UP          Up arrow is held down for sustained period.
  564.     // HELD_DOWN        Down arrow is held down for sustained period.
  565.     enum ARROWSTATE { CLEAR, CLICKED_UP, CLICKED_DOWN, HELD_UP, HELD_DOWN };
  566.  
  567.     void UpdateThumbRect();
  568.     void Cap();  // Clips position at boundaries. Ensures it stays within legal range.
  569.  
  570.     bool m_bShowThumb;
  571.     RECT m_rcUpButton;
  572.     RECT m_rcDownButton;
  573.     RECT m_rcTrack;
  574.     RECT m_rcThumb;
  575.     int m_nPosition;  // Position of the first displayed item
  576.     int m_nPageSize;  // How many items are displayable in one page
  577.     int m_nStart;     // First item
  578.     int m_nEnd;       // The index after the last item
  579.     POINT m_LastMouse;// Last mouse position
  580.     ARROWSTATE m_Arrow; // State of the arrows
  581.     double m_dArrowTS;  // Timestamp of last arrow event.
  582. };
  583.  
  584.  
  585. //-----------------------------------------------------------------------------
  586. // ListBox control
  587. //-----------------------------------------------------------------------------
  588. struct DXUTListBoxItem
  589. {
  590.     WCHAR strText[256];
  591.     void*  pData;
  592.  
  593.     RECT  rcActive;
  594.     bool  bSelected;
  595. };
  596.  
  597. class CDXUTListBox : public CDXUTControl
  598. {
  599. public:
  600.     CDXUTListBox( CDXUTDialog *pDialog = NULL );
  601.     virtual ~CDXUTListBox();
  602.  
  603.     virtual HRESULT OnInit() { return m_pDialog->InitControl( &m_ScrollBar ); }
  604.     virtual bool    CanHaveFocus() { return (m_bVisible && m_bEnabled); }
  605.     virtual bool    HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam );
  606.     virtual bool    HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lParam );
  607.  
  608.     virtual void Render( IDirect3DDevice9* pd3dDevice, float fElapsedTime );
  609.     virtual void    UpdateRects();
  610.  
  611.     DWORD GetStyle() const { return m_dwStyle; }
  612.     int GetSize() const { return m_Items.GetSize(); }
  613.     void SetStyle( DWORD dwStyle ) { m_dwStyle = dwStyle; }
  614.     void SetScrollBarWidth( int nWidth ) { m_nSBWidth = nWidth; UpdateRects(); }
  615.     void SetBorder( int nBorder, int nMargin ) { m_nBorder = nBorder; m_nMargin = nMargin; }
  616.     HRESULT AddItem( const WCHAR *wszText, void *pData );
  617.     HRESULT InsertItem( int nIndex, const WCHAR *wszText, void *pData );
  618.     void RemoveItem( int nIndex );
  619.     void RemoveItemByText( WCHAR *wszText );
  620.     void RemoveItemByData( void *pData );
  621.     void RemoveAllItems();
  622.  
  623.     DXUTListBoxItem *GetItem( int nIndex );
  624.     int GetSelectedIndex( int nPreviousSelected = -1 );
  625.     DXUTListBoxItem *GetSelectedItem( int nPreviousSelected = -1 ) { return GetItem( GetSelectedIndex( nPreviousSelected ) ); }
  626.     void SelectItem( int nNewIndex );
  627.  
  628.     enum STYLE { MULTISELECTION = 1 };
  629.  
  630. protected:
  631.     RECT m_rcText;      // Text rendering bound
  632.     RECT m_rcSelection; // Selection box bound
  633.     CDXUTScrollBar m_ScrollBar;
  634.     int m_nSBWidth;
  635.     int m_nBorder;
  636.     int m_nMargin;
  637.     int m_nTextHeight;  // Height of a single line of text
  638.     DWORD m_dwStyle;    // List box style
  639.     int m_nSelected;    // Index of the selected item for single selection list box
  640.     int m_nSelStart;    // Index of the item where selection starts (for handling multi-selection)
  641.     bool m_bDrag;       // Whether the user is dragging the mouse to select
  642.  
  643.     CGrowableArray< DXUTListBoxItem* > m_Items;
  644. };
  645.  
  646.  
  647. //-----------------------------------------------------------------------------
  648. // ComboBox control
  649. //-----------------------------------------------------------------------------
  650. struct DXUTComboBoxItem
  651. {
  652.     WCHAR strText[256];
  653.     void*  pData;
  654.  
  655.     RECT  rcActive;
  656.     bool  bVisible;
  657. };
  658.  
  659.  
  660. class CDXUTComboBox : public CDXUTButton
  661. {
  662. public:
  663.     CDXUTComboBox( CDXUTDialog *pDialog = NULL );
  664.     virtual ~CDXUTComboBox();
  665.     
  666.     virtual void SetTextColor( D3DCOLOR Color );
  667.     virtual HRESULT OnInit() { return m_pDialog->InitControl( &m_ScrollBar ); }
  668.  
  669.     virtual bool HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam );
  670.     virtual bool HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lParam );
  671.     virtual void OnHotkey();
  672.  
  673.     virtual bool CanHaveFocus() { return (m_bVisible && m_bEnabled); }
  674.     virtual void OnFocusOut();
  675.     virtual void Render( IDirect3DDevice9* pd3dDevice, float fElapsedTime );
  676.  
  677.     virtual void UpdateRects(); 
  678.  
  679.     HRESULT AddItem( const WCHAR* strText, void* pData );
  680.     void    RemoveAllItems();
  681.     void    RemoveItem( UINT index );
  682.     bool    ContainsItem( const WCHAR* strText, UINT iStart=0 );
  683.     int     FindItem( const WCHAR* strText, UINT iStart=0 );
  684.     void*   GetItemData( const WCHAR* strText );
  685.     void*   GetItemData( int nIndex );
  686.     void    SetDropHeight( UINT nHeight ) { m_nDropHeight = nHeight; UpdateRects(); }
  687.     void    SetScrollBarWidth( int nWidth ) { m_nSBWidth = nWidth; UpdateRects(); }
  688.  
  689.     void*   GetSelectedData();
  690.     DXUTComboBoxItem* GetSelectedItem();
  691.  
  692.     UINT    GetNumItems() { return m_Items.GetSize(); }
  693.     DXUTComboBoxItem* GetItem( UINT index ) { return m_Items.GetAt( index ); }
  694.  
  695.     HRESULT SetSelectedByIndex( UINT index );
  696.     HRESULT SetSelectedByText( const WCHAR* strText );
  697.     HRESULT SetSelectedByData( void* pData );  
  698.  
  699. protected:
  700.     int     m_iSelected;
  701.     int     m_iFocused;
  702.     int     m_nDropHeight;
  703.     CDXUTScrollBar m_ScrollBar;
  704.     int     m_nSBWidth;
  705.  
  706.     bool    m_bOpened;
  707.  
  708.     RECT m_rcText;
  709.     RECT m_rcButton;
  710.     RECT m_rcDropdown;
  711.     RECT m_rcDropdownText;
  712.  
  713.     
  714.     CGrowableArray< DXUTComboBoxItem* > m_Items;
  715. };
  716.  
  717.  
  718. //-----------------------------------------------------------------------------
  719. // Slider control
  720. //-----------------------------------------------------------------------------
  721. class CDXUTSlider : public CDXUTControl
  722. {
  723. public:
  724.     CDXUTSlider( CDXUTDialog *pDialog = NULL );
  725.  
  726.     virtual BOOL ContainsPoint( POINT pt ); 
  727.     virtual bool CanHaveFocus() { return (m_bVisible && m_bEnabled); }
  728.     virtual bool HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam );
  729.     virtual bool HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lParam );
  730.     
  731.     virtual void UpdateRects(); 
  732.  
  733.     virtual void Render( IDirect3DDevice9* pd3dDevice, float fElapsedTime );
  734.  
  735.     void SetValue( int nValue ) { SetValueInternal( nValue, false ); }
  736.     int  GetValue() { return m_nValue; };
  737.  
  738.     void SetRange( int nMin, int nMax );
  739.  
  740. protected:
  741.     void SetValueInternal( int nValue, bool bFromInput );
  742.     int  ValueFromPos( int x ); 
  743.  
  744.     int m_nValue;
  745.  
  746.     int m_nMin;
  747.     int m_nMax;
  748.  
  749.     int m_nDragX;      // Mouse position at start of drag
  750.     int m_nDragOffset; // Drag offset from the center of the button
  751.     int m_nButtonX;
  752.  
  753.     bool m_bPressed;
  754.     RECT m_rcButton;
  755. };
  756.  
  757.  
  758. //-----------------------------------------------------------------------------
  759. // EditBox control
  760. //-----------------------------------------------------------------------------
  761. #define UNISCRIBE_DLLNAME L"\\usp10.dll"
  762.  
  763. #define GETPROCADDRESS( Module, APIName, Temp ) \
  764.     Temp = GetProcAddress( Module, #APIName ); \
  765.     if( Temp ) \
  766.         *(FARPROC*)&_##APIName = Temp
  767.  
  768. #define PLACEHOLDERPROC( APIName ) \
  769.     _##APIName = Dummy_##APIName
  770.  
  771. class CDXUTEditBox : public CDXUTControl
  772. {
  773.     friend class CExternalApiInitializer;
  774.  
  775. public:
  776.     CDXUTEditBox( CDXUTDialog *pDialog = NULL );
  777.     virtual ~CDXUTEditBox();
  778.  
  779.     virtual bool HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam );
  780.     virtual bool HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lParam );
  781.     virtual bool MsgProc( UINT uMsg, WPARAM wParam, LPARAM lParam );
  782.     virtual void UpdateRects();
  783.     virtual bool CanHaveFocus() { return (m_bVisible && m_bEnabled); }
  784.     virtual void Render( IDirect3DDevice9* pd3dDevice, float fElapsedTime );
  785.     virtual void    OnFocusIn();
  786.  
  787.     void SetText( LPCWSTR wszText, bool bSelected = false );
  788.     LPCWSTR GetText() { return m_Buffer.GetBuffer(); }
  789.     int GetTextLength() { return m_Buffer.GetTextSize(); }  // Returns text length in chars excluding NULL.
  790.     HRESULT GetTextCopy( LPWSTR strDest, UINT bufferCount );
  791.     void ClearText();
  792.     virtual void SetTextColor( D3DCOLOR Color ) { m_TextColor = Color; }  // Text color
  793.     void SetSelectedTextColor( D3DCOLOR Color ) { m_SelTextColor = Color; }  // Selected text color
  794.     void SetSelectedBackColor( D3DCOLOR Color ) { m_SelBkColor = Color; }  // Selected background color
  795.     void SetCaretColor( D3DCOLOR Color ) { m_CaretColor = Color; }  // Caret color
  796.     void SetBorderWidth( int nBorder ) { m_nBorder = nBorder; UpdateRects(); }  // Border of the window
  797.     void SetSpacing( int nSpacing ) { m_nSpacing = nSpacing; UpdateRects(); }
  798.     void ParseFloatArray( float *pNumbers, int nCount );
  799.     void SetTextFloatArray( const float *pNumbers, int nCount );
  800.  
  801. public:
  802.     //-----------------------------------------------------------------------------
  803.     // CUniBuffer class for the edit control
  804.     //-----------------------------------------------------------------------------
  805.     class CUniBuffer
  806.     {
  807.         friend class CExternalApiInitializer;
  808.  
  809.         // static member
  810.  
  811.         // Empty implementation of the Uniscribe API
  812.         static HRESULT WINAPI Dummy_ScriptApplyDigitSubstitution( const SCRIPT_DIGITSUBSTITUTE*, SCRIPT_CONTROL*, SCRIPT_STATE* ) { return E_NOTIMPL; }
  813.         static HRESULT WINAPI Dummy_ScriptStringAnalyse( HDC, const void *, int, int, int, DWORD, int, SCRIPT_CONTROL*, SCRIPT_STATE*, const int*, SCRIPT_TABDEF*, const BYTE*, SCRIPT_STRING_ANALYSIS* ) { return E_NOTIMPL; }
  814.         static HRESULT WINAPI Dummy_ScriptStringCPtoX( SCRIPT_STRING_ANALYSIS, int, BOOL, int* ) { return E_NOTIMPL; }
  815.         static HRESULT WINAPI Dummy_ScriptStringXtoCP( SCRIPT_STRING_ANALYSIS, int, int*, int* ) { return E_NOTIMPL; }
  816.         static HRESULT WINAPI Dummy_ScriptStringFree( SCRIPT_STRING_ANALYSIS* ) { return E_NOTIMPL; }
  817.         static const SCRIPT_LOGATTR* WINAPI Dummy_ScriptString_pLogAttr( SCRIPT_STRING_ANALYSIS ) { return NULL; }
  818.         static const int* WINAPI Dummy_ScriptString_pcOutChars( SCRIPT_STRING_ANALYSIS ) { return NULL; }
  819.  
  820.         // Function pointers
  821.         static HRESULT (WINAPI *_ScriptApplyDigitSubstitution)( const SCRIPT_DIGITSUBSTITUTE*, SCRIPT_CONTROL*, SCRIPT_STATE* );
  822.         static HRESULT (WINAPI *_ScriptStringAnalyse)( HDC, const void *, int, int, int, DWORD, int, SCRIPT_CONTROL*, SCRIPT_STATE*, const int*, SCRIPT_TABDEF*, const BYTE*, SCRIPT_STRING_ANALYSIS* );
  823.         static HRESULT (WINAPI *_ScriptStringCPtoX)( SCRIPT_STRING_ANALYSIS, int, BOOL, int* );
  824.         static HRESULT (WINAPI *_ScriptStringXtoCP)( SCRIPT_STRING_ANALYSIS, int, int*, int* );
  825.         static HRESULT (WINAPI *_ScriptStringFree)( SCRIPT_STRING_ANALYSIS* );
  826.         static const SCRIPT_LOGATTR* (WINAPI *_ScriptString_pLogAttr)( SCRIPT_STRING_ANALYSIS );
  827.         static const int* (WINAPI *_ScriptString_pcOutChars)( SCRIPT_STRING_ANALYSIS );
  828.  
  829.         static void InitializeUniscribe();
  830.         static void UninitializeUniscribe();
  831.  
  832.         static HINSTANCE s_hDll;  // Uniscribe DLL handle
  833.  
  834.     // Instance data
  835.     private:
  836.         WCHAR *m_pwszBuffer;    // Buffer to hold text
  837.         int    m_nBufferSize;   // Size of the buffer allocated, in characters
  838.         int    m_nTextSize;     // Size of the text in buffer excluding
  839.                                 //   NULL terminator, in characters
  840.         // Uniscribe-specific
  841.         int  m_iFont;           // Font index of this buffer
  842.         bool m_bAnalyseRequired;// True if the string has changed since last analysis.
  843.         SCRIPT_STRING_ANALYSIS m_Analysis;  // Analysis for the current string
  844.  
  845.     private:
  846.         bool Grow( int nNewSize = -1 );
  847.  
  848.         // Uniscribe -- Analyse() analyses the string in the buffer
  849.         HRESULT Analyse();
  850.  
  851.     public:
  852.         CUniBuffer( int nInitialSize = 1 );
  853.         ~CUniBuffer();
  854.         int GetBufferSize() { return m_nBufferSize; }
  855.         bool SetBufferSize( int nSize );
  856.         int GetTextSize() { return m_nTextSize; }
  857.         const WCHAR *GetBuffer() { return m_pwszBuffer; }
  858.         const WCHAR &operator[]( int n ) const { return m_pwszBuffer[n]; }
  859.         WCHAR &operator[]( int n );
  860.         int GetFontIndex() { return m_iFont; }
  861.         void SetFontIndex( int iFont ) { m_iFont = iFont; }
  862.         void Clear();
  863.  
  864.         // Inserts the char at specified index.
  865.         // If nIndex == -1, insert to the end.
  866.         bool InsertChar( int nIndex, WCHAR wChar );
  867.  
  868.         // Removes the char at specified index.
  869.         // If nIndex == -1, remove the last char.
  870.         bool RemoveChar( int nIndex );
  871.  
  872.         // Inserts the first nCount characters of the string pStr at specified index.
  873.         // If nCount == -1, the entire string is inserted.
  874.         // If nIndex == -1, insert to the end.
  875.         bool InsertString( int nIndex, const WCHAR *pStr, int nCount = -1 );
  876.  
  877.         bool SetText( LPCWSTR wszText );
  878.  
  879.         // Uniscribe
  880.         HRESULT CPtoX( int nCP, BOOL bTrail, int *pX );
  881.         HRESULT XtoCP( int nX, int *pCP, int *pnTrail );
  882.         void GetPriorItemPos( int nCP, int *pPrior );
  883.         void GetNextItemPos( int nCP, int *pPrior );
  884.     };
  885.  
  886. protected:
  887.     void PlaceCaret( int nCP );
  888.     void DeleteSelectionText();
  889.     void ResetCaretBlink();
  890.     void CopyToClipboard();
  891.     void PasteFromClipboard();
  892.  
  893.     CUniBuffer m_Buffer;     // Buffer to hold text
  894.     int      m_nBorder;      // Border of the window
  895.     int      m_nSpacing;     // Spacing between the text and the edge of border
  896.     RECT     m_rcText;       // Bounding rectangle for the text
  897.     RECT     m_rcRender[9];  // Convenient rectangles for rendering elements
  898.     double   m_dfBlink;      // Caret blink time in milliseconds
  899.     double   m_dfLastBlink;  // Last timestamp of caret blink
  900.     bool     m_bCaretOn;     // Flag to indicate whether caret is currently visible
  901.     int      m_nCaret;       // Caret position, in characters
  902.     bool     m_bInsertMode;  // If true, control is in insert mode. Else, overwrite mode.
  903.     int      m_nSelStart;    // Starting position of the selection. The caret marks the end.
  904.     int      m_nFirstVisible;// First visible character in the edit control
  905.     D3DCOLOR m_TextColor;    // Text color
  906.     D3DCOLOR m_SelTextColor; // Selected text color
  907.     D3DCOLOR m_SelBkColor;   // Selected background color
  908.     D3DCOLOR m_CaretColor;   // Caret color
  909.  
  910.     // Mouse-specific
  911.     bool m_bMouseDrag;       // True to indicate drag in progress
  912.  
  913.     // Static
  914.     static bool     s_bHideCaret;   // If true, we don't render the caret.
  915. };
  916.  
  917.  
  918. //-----------------------------------------------------------------------------
  919. // IME-enabled EditBox control
  920. //-----------------------------------------------------------------------------
  921. #define IMM32_DLLNAME L"\\imm32.dll"
  922. #define VER_DLLNAME L"\\version.dll"
  923. #define MAX_CANDLIST 10
  924. #define MAX_COMPSTRING_SIZE 256
  925.  
  926. class CDXUTIMEEditBox : public CDXUTEditBox
  927. {
  928.     friend class CExternalApiInitializer;
  929.  
  930.     static void InitializeImm();
  931.     static void UninitializeImm();
  932.  
  933.     static HINSTANCE s_hDllImm32;  // IMM32 DLL handle
  934.     static HINSTANCE s_hDllVer;    // Version DLL handle
  935.     static HIMC      s_hImcDef;    // Default input context
  936.  
  937. protected:
  938.     // Empty implementation of the IMM32 API
  939.     static INPUTCONTEXT* WINAPI Dummy_ImmLockIMC( HIMC ) { return NULL; }
  940.     static BOOL WINAPI Dummy_ImmUnlockIMC( HIMC ) { return FALSE; }
  941.     static LPVOID WINAPI Dummy_ImmLockIMCC( HIMCC ) { return NULL; }
  942.     static BOOL WINAPI Dummy_ImmUnlockIMCC( HIMCC ) { return FALSE; }
  943.     static BOOL WINAPI Dummy_ImmDisableTextFrameService( DWORD ) { return TRUE; }
  944.     static LONG WINAPI Dummy_ImmGetCompositionStringW( HIMC, DWORD, LPVOID, DWORD ) { return IMM_ERROR_GENERAL; }
  945.     static DWORD WINAPI Dummy_ImmGetCandidateListW( HIMC, DWORD, LPCANDIDATELIST, DWORD ) { return 0; }
  946.     static HIMC WINAPI Dummy_ImmGetContext( HWND ) { return NULL; }
  947.     static BOOL WINAPI Dummy_ImmReleaseContext( HWND, HIMC ) { return FALSE; }
  948.     static HIMC WINAPI Dummy_ImmAssociateContext( HWND, HIMC ) { return NULL; }
  949.     static BOOL WINAPI Dummy_ImmGetOpenStatus( HIMC ) { return 0; }
  950.     static BOOL WINAPI Dummy_ImmSetOpenStatus( HIMC, BOOL ) { return 0; }
  951.     static BOOL WINAPI Dummy_ImmGetConversionStatus( HIMC, LPDWORD, LPDWORD ) { return 0; }
  952.     static HWND WINAPI Dummy_ImmGetDefaultIMEWnd( HWND ) { return NULL; }
  953.     static UINT WINAPI Dummy_ImmGetIMEFileNameA( HKL, LPSTR, UINT ) { return 0; }
  954.     static UINT WINAPI Dummy_ImmGetVirtualKey( HWND ) { return 0; }
  955.     static BOOL WINAPI Dummy_ImmNotifyIME( HIMC, DWORD, DWORD, DWORD ) { return FALSE; }
  956.     static BOOL WINAPI Dummy_ImmSetConversionStatus( HIMC, DWORD, DWORD ) { return FALSE; }
  957.     static BOOL WINAPI Dummy_ImmSimulateHotKey( HWND, DWORD ) { return FALSE; }
  958.     static BOOL WINAPI Dummy_ImmIsIME( HKL ) { return FALSE; }
  959.  
  960.     // Traditional Chinese IME
  961.     static UINT WINAPI Dummy_GetReadingString( HIMC, UINT, LPWSTR, PINT, BOOL*, PUINT ) { return 0; }
  962.     static BOOL WINAPI Dummy_ShowReadingWindow( HIMC, BOOL ) { return FALSE; }
  963.  
  964.     // Verion library imports
  965.     static BOOL APIENTRY Dummy_VerQueryValueA( const LPVOID, LPSTR, LPVOID *, PUINT ) { return 0; }
  966.     static BOOL APIENTRY Dummy_GetFileVersionInfoA( LPSTR, DWORD, DWORD, LPVOID ) { return 0; }
  967.     static DWORD APIENTRY Dummy_GetFileVersionInfoSizeA( LPSTR, LPDWORD ) { return 0; }
  968.  
  969.     // Function pointers: IMM32
  970.     static INPUTCONTEXT* (WINAPI * _ImmLockIMC)( HIMC );
  971.     static BOOL (WINAPI * _ImmUnlockIMC)( HIMC );
  972.     static LPVOID (WINAPI * _ImmLockIMCC)( HIMCC );
  973.     static BOOL (WINAPI * _ImmUnlockIMCC)( HIMCC );
  974.     static BOOL (WINAPI * _ImmDisableTextFrameService)( DWORD );
  975.     static LONG (WINAPI * _ImmGetCompositionStringW)( HIMC, DWORD, LPVOID, DWORD );
  976.     static DWORD (WINAPI * _ImmGetCandidateListW)( HIMC, DWORD, LPCANDIDATELIST, DWORD );
  977.     static HIMC (WINAPI * _ImmGetContext)( HWND );
  978.     static BOOL (WINAPI * _ImmReleaseContext)( HWND, HIMC );
  979.     static HIMC (WINAPI * _ImmAssociateContext)( HWND, HIMC );
  980.     static BOOL (WINAPI * _ImmGetOpenStatus)( HIMC );
  981.     static BOOL (WINAPI * _ImmSetOpenStatus)( HIMC, BOOL );
  982.     static BOOL (WINAPI * _ImmGetConversionStatus)( HIMC, LPDWORD, LPDWORD );
  983.     static HWND (WINAPI * _ImmGetDefaultIMEWnd)( HWND );
  984.     static UINT (WINAPI * _ImmGetIMEFileNameA)( HKL, LPSTR, UINT );
  985.     static UINT (WINAPI * _ImmGetVirtualKey)( HWND );
  986.     static BOOL (WINAPI * _ImmNotifyIME)( HIMC, DWORD, DWORD, DWORD );
  987.     static BOOL (WINAPI * _ImmSetConversionStatus)( HIMC, DWORD, DWORD );
  988.     static BOOL (WINAPI * _ImmSimulateHotKey)( HWND, DWORD );
  989.     static BOOL (WINAPI * _ImmIsIME)( HKL );
  990.  
  991.     // Function pointers: Traditional Chinese IME
  992.     static UINT (WINAPI * _GetReadingString)( HIMC, UINT, LPWSTR, PINT, BOOL*, PUINT );
  993.     static BOOL (WINAPI * _ShowReadingWindow)( HIMC, BOOL );
  994.  
  995.     // Function pointers: Verion library imports
  996.     static BOOL (APIENTRY * _VerQueryValueA)( const LPVOID, LPSTR, LPVOID *, PUINT );
  997.     static BOOL (APIENTRY * _GetFileVersionInfoA)( LPSTR, DWORD, DWORD, LPVOID );
  998.     static DWORD (APIENTRY * _GetFileVersionInfoSizeA)( LPSTR, LPDWORD );
  999.  
  1000. public:
  1001.     CDXUTIMEEditBox( CDXUTDialog *pDialog = NULL );
  1002.     virtual ~CDXUTIMEEditBox();
  1003.  
  1004.     static  HRESULT StaticOnCreateDevice();
  1005.     static  bool StaticMsgProc( UINT uMsg, WPARAM wParam, LPARAM lParam );
  1006.  
  1007.     static  void EnableImeSystem( bool bEnable );
  1008.  
  1009.     virtual void Render( IDirect3DDevice9* pd3dDevice, float fElapsedTime );
  1010.     virtual bool MsgProc( UINT uMsg, WPARAM wParam, LPARAM lParam );
  1011.     virtual bool HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lParam );
  1012.     virtual void UpdateRects();
  1013.     virtual void OnFocusIn();
  1014.     virtual void OnFocusOut();
  1015.  
  1016.     void PumpMessage();
  1017.  
  1018.     virtual void RenderCandidateReadingWindow( IDirect3DDevice9* pd3dDevice, float fElapsedTime, bool bReading );
  1019.     virtual void RenderComposition( IDirect3DDevice9* pd3dDevice, float fElapsedTime );
  1020.     virtual void RenderIndicator( IDirect3DDevice9* pd3dDevice, float fElapsedTime );
  1021.  
  1022. protected:
  1023.     static WORD GetLanguage() { return LOWORD( s_hklCurrent ); }
  1024.     static WORD GetPrimaryLanguage() { return PRIMARYLANGID( LOWORD( s_hklCurrent ) ); }
  1025.     static WORD GetSubLanguage() { return SUBLANGID( LOWORD( s_hklCurrent ) ); }
  1026.     static void SendKey( BYTE nVirtKey );
  1027.     static DWORD GetImeId( UINT uIndex = 0 );
  1028.     static void CheckInputLocale();
  1029.     static void CheckToggleState();
  1030.     static void SetupImeApi();
  1031.     static void ResetCompositionString();
  1032.     void TruncateCompString( bool bUseBackSpace = true, int iNewStrLen = 0 );
  1033.     void FinalizeString( bool bSend );
  1034.     static void GetReadingWindowOrientation( DWORD dwId );
  1035.     static void GetPrivateReadingString();
  1036.  
  1037.     void SendCompString();
  1038.  
  1039. protected:
  1040.     enum { INDICATOR_NON_IME, INDICATOR_CHS, INDICATOR_CHT, INDICATOR_KOREAN, INDICATOR_JAPANESE };
  1041.     enum IMESTATE { IMEUI_STATE_OFF, IMEUI_STATE_ON, IMEUI_STATE_ENGLISH };
  1042.  
  1043.     struct CCandList
  1044.     {
  1045.         WCHAR awszCandidate[MAX_CANDLIST][256];
  1046.         CUniBuffer HoriCand; // Candidate list string (for horizontal candidate window)
  1047.         int   nFirstSelected; // First character position of the selected string in HoriCand
  1048.         int   nHoriSelectedLen; // Length of the selected string in HoriCand
  1049.         DWORD dwCount;       // Number of valid entries in the candidate list
  1050.         DWORD dwSelection;   // Currently selected candidate entry relative to page top
  1051.         DWORD dwPageSize;
  1052.         int   nReadingError; // Index of the error character
  1053.         bool  bShowWindow;   // Whether the candidate list window is visible
  1054.         RECT  rcCandidate;   // Candidate rectangle computed and filled each time before rendered
  1055.     };
  1056.  
  1057.     struct CInputLocale
  1058.     {
  1059.         HKL   m_hKL;            // Keyboard layout
  1060.         WCHAR m_wszLangAbb[3];  // Language abbreviation
  1061.         WCHAR m_wszLang[64];    // Localized language name
  1062.     };
  1063.  
  1064.     // Application-wide data
  1065.     static HKL     s_hklCurrent;          // Current keyboard layout of the process
  1066.     static bool    s_bVerticalCand;       // Indicates that the candidates are listed vertically
  1067.     static LPWSTR  s_wszCurrIndicator;    // Points to an indicator string that corresponds to current input locale
  1068.     static WCHAR   s_aszIndicator[5][3];  // String to draw to indicate current input locale
  1069.     static bool    s_bInsertOnType;       // Insert the character as soon as a key is pressed (Korean behavior)
  1070.     static HINSTANCE s_hDllIme;           // Instance handle of the current IME module
  1071.     static IMESTATE  s_ImeState;          // IME global state
  1072.     static bool    s_bEnableImeSystem;    // Whether the IME system is active
  1073.     static POINT   s_ptCompString;        // Composition string position. Updated every frame.
  1074.     static int     s_nCompCaret;          // Caret position of the composition string
  1075.     static int     s_nFirstTargetConv;    // Index of the first target converted char in comp string.  If none, -1.
  1076.     static CUniBuffer s_CompString;       // Buffer to hold the composition string (we fix its length)
  1077.     static BYTE    s_abCompStringAttr[MAX_COMPSTRING_SIZE];
  1078.     static DWORD   s_adwCompStringClause[MAX_COMPSTRING_SIZE];
  1079.     static WCHAR   s_wszReadingString[32];// Used only with horizontal reading window (why?)
  1080.     static CCandList s_CandList;          // Data relevant to the candidate list
  1081.     static bool    s_bShowReadingWindow;  // Indicates whether reading window is visible
  1082.     static bool    s_bHorizontalReading;  // Indicates whether the reading window is vertical or horizontal
  1083.     static bool    s_bChineseIME;
  1084.     static CGrowableArray< CInputLocale > s_Locale; // Array of loaded keyboard layout on system
  1085.  
  1086.     // Color of various IME elements
  1087.     D3DCOLOR       m_ReadingColor;        // Reading string color
  1088.     D3DCOLOR       m_ReadingWinColor;     // Reading window color
  1089.     D3DCOLOR       m_ReadingSelColor;     // Selected character in reading string
  1090.     D3DCOLOR       m_ReadingSelBkColor;   // Background color for selected char in reading str
  1091.     D3DCOLOR       m_CandidateColor;      // Candidate string color
  1092.     D3DCOLOR       m_CandidateWinColor;   // Candidate window color
  1093.     D3DCOLOR       m_CandidateSelColor;   // Selected candidate string color
  1094.     D3DCOLOR       m_CandidateSelBkColor; // Selected candidate background color
  1095.     D3DCOLOR       m_CompColor;           // Composition string color
  1096.     D3DCOLOR       m_CompWinColor;        // Composition string window color
  1097.     D3DCOLOR       m_CompCaretColor;      // Composition string caret color
  1098.     D3DCOLOR       m_CompTargetColor;     // Composition string target converted color
  1099.     D3DCOLOR       m_CompTargetBkColor;   // Composition string target converted background
  1100.     D3DCOLOR       m_CompTargetNonColor;  // Composition string target non-converted color
  1101.     D3DCOLOR       m_CompTargetNonBkColor;// Composition string target non-converted background
  1102.     D3DCOLOR       m_IndicatorImeColor;   // Indicator text color for IME
  1103.     D3DCOLOR       m_IndicatorEngColor;   // Indicator text color for English
  1104.     D3DCOLOR       m_IndicatorBkColor;    // Indicator text background color
  1105.  
  1106.     // Edit-control-specific data
  1107.     int            m_nIndicatorWidth;     // Width of the indicator symbol
  1108.     RECT           m_rcIndicator;         // Rectangle for drawing the indicator button
  1109. };
  1110.  
  1111.  
  1112.  
  1113. #endif // DXUT_GUI_H