Guest User

Button class

a guest
May 25th, 2020
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 5.83 KB | None | 0 0
  1. module winglib.buttons;
  2. //---------------------------------------------------
  3. private import core.runtime;
  4. private import core.sys.windows.windows ;
  5. private import core.sys.windows.commctrl ;
  6. private import std.stdio ;
  7. private import std.string ;
  8. private import std.conv;
  9. //----------------------------------------------------
  10. private import winglib.commons ;
  11. private import winglib.controls : Control;
  12. private import winglib.events;
  13.  
  14. //------------------------------------------------------
  15.  
  16. private DWORD btnStyle = WS_CHILD | BS_NOTIFY | WS_TABSTOP | WS_VISIBLE;
  17. private DWORD btnExStyle = 0;
  18. private Button[] buttonList;
  19. private static int btnNumber = 1;
  20. private static int mIdNum = 101;  
  21. private static int subClsID = 1001;
  22. //---------------------------------------------------------
  23.  
  24. class Button : Control {
  25.    
  26.     import winglib.window : Window;  
  27.     import winglib.application : hInst = gModuleInstance;
  28.     import winglib.enums : ControlType ;
  29.     //-----------------------------------------------------------------------
  30.  
  31.     ButtonEvents events ;
  32.     alias events this;
  33.  
  34.     this(Window pParent){
  35.         with(this){
  36.             mText = format("Button%s", btnNumber.to!string());
  37.             mWidth = 100;
  38.             mHeight = 35;
  39.             mXpos = 30;
  40.             mYpos = 30;
  41.             mParent = pParent;
  42.             this.mFont = this.mParent.font;
  43.             this.mControlType = ControlType.button;        
  44.             ++btnNumber;
  45.         }
  46.     }
  47. //--------------------------------------------------------------------------------------------
  48.     this(Window pParent, string btnText, int x, int y, int pWidth = 100, int pHeight = 35){
  49.         with(this){
  50.             mText = btnText;
  51.             mWidth = pWidth;
  52.             mHeight = pHeight;
  53.             mXpos = x;
  54.             mYpos = y;
  55.             mParent = pParent;
  56.             this.mFont = this.mParent.font;          
  57.             ++btnNumber;
  58.             this.create();
  59.         }
  60.     }
  61. //------------------------------------------------------------------------------------------
  62.     ~this() {    
  63.         RemoveWindowSubclass(this.handle, this.btnWindProc, this.ownSubClsId);
  64.         writeln("A button's subclass data is removed");          
  65.     }
  66. //------------------------------------------------------------------------
  67.    
  68.     HWND create() {
  69.         this.mHandle = CreateWindowEx(  btnExStyle,
  70.                                         cast(LPCWSTR) WC_BUTTON,
  71.                                         toUString(this.mText),
  72.                                         btnStyle,
  73.                                         this.mXpos,
  74.                                         this.mYpos,
  75.                                         this.mWidth,
  76.                                         this.mHeight,
  77.                                         this.mParent.handle,
  78.                                         cast(HMENU) mIdNum,
  79.                                         hInst,
  80.                                         null);
  81.         this.misHandleCreated = true;
  82.         this.setFont();
  83.         ++mIdNum;
  84.         setSubClass();      \\-------------------This is the function which causes error.
  85.        
  86.         this.mParent.controls ~= this;
  87.         buttonList~= this;
  88.         return this.mHandle;
  89.     }
  90.     //-------------------------------------------------------------------------
  91.     protected MsgHandler[] msgHandlerList;
  92.    
  93.     /// Adding event handler functions to button events.
  94.     void addHandler(uint evt, EvtFuncPtr fnp){
  95.         auto mh = MsgHandler();
  96.         mh.handle = this.mHandle;
  97.         mh.message = evt;
  98.         mh.fnPtr = fnp;
  99.         mh.isActive = true;
  100.         this.msgHandlerList ~= mh;      
  101.     }    
  102.     //---------------------------------------------------------------------------------
  103.     private :  
  104.     Button me;
  105.     SUBCLASSPROC btnWindProc;
  106.     int ownSubClsId ;
  107.     //-----------------------------------------------------------------------
  108.     /// Find the correct MsgHandler struct for given message and hwnd.
  109.     MsgHandler findHandler(HWND hw, uint uMsg) {
  110.         MsgHandler result ;
  111.         foreach (MsgHandler mh; this.msgHandlerList) {
  112.             if (mh.handle == hw && mh.message == uMsg) {                
  113.                 result = mh;
  114.                 break;
  115.             }        
  116.         }
  117.         return result;
  118.     }      
  119.     //------------------------------------------------------------------------
  120.  
  121.     void setSubClass() {          
  122.         this.btnWindProc = &btnWndProc;
  123.         this.ownSubClsId = subClsID;
  124.         SetWindowSubclass(this.mHandle, SUBCLASSPROC(&btnWndProc), UINT_PTR(subClsID), cast(DWORD_PTR) &this);
  125.         ++subClsID;            
  126.     }
  127.     //------------------------------------------------------------------------
  128. }// End Button Class---------------------------------------------------------
  129.  
  130.  
  131. extern(Windows)
  132. private LRESULT btnWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, UINT_PTR scID, DWORD_PTR refData) {
  133.     try  {  
  134.          
  135.         Button thisBtn = *cast(Button*) refData; //findButton(hWnd);
  136.         if (thisBtn.msgHandlerList.length > 0) {
  137.             MsgHandler mh = thisBtn.findHandler(hWnd, message);
  138.             if(mh.isActive) {
  139.                 EventArgs e = EventArgs(hWnd, message, wParam, lParam);
  140.                 with(e) {
  141.                     // msg = message;
  142.                     // handle = hWnd;
  143.                     wpm = wParam;
  144.                     lpm = lParam;
  145.                 }
  146.                 mh.fnPtr(e);
  147.                 return 0 ;
  148.             }
  149.     }  
  150. }
  151.     catch (Exception e) {}    
  152.     return DefSubclassProc(hWnd, message, wParam, lParam);
  153. }
  154.  
  155. struct ButtonEvents {
  156.     uint click = WM_LBUTTONDOWN;
  157.     uint mouseEnter;
  158.     uint mouseMove = WM_MOUSEMOVE;
  159.     uint mouseLeave = WM_MOUSELEAVE;
  160. }
Add Comment
Please, Sign In to add comment