Trating class new ( the revenge)

Trating class new ( the revenge)

Postby Silvio.Falconi » Sat Jan 03, 2015 12:49 am

Dear Antonio,

tonight I tried to create the trating class converting the test made from Uwe and I. on old topic (viewtopic.php?f=3&t=29832&start=45)

I can show the images, the caption sample "MyRating" and the test of reset as you can see on this picture

Image



the problem I have on Mousemove method is this :

I load the images and put them on array ::himage[n] and associated the image on a aItem array where I save the number of Image and the coordinates the images

to use ptinrect I must save on an array the coordinates of each bitmap

when I add on aItems the value I have problems

AAdd( ::aItems, {STR(i),.f.,{nRow, nCol,::nbottom-8,20} } )

aItem[1] = number of Image
aItem[2] = select or not select
aItem[3] = coordinates of bitmap x,y,z,k to use with ptinrect function on mousemove method

I used the bitmaps of Uwe test you can found at viewtopic.php?f=3&t=29832&start=45

If the coordinates are the same of Ptinrest it must load onHoverRate method


How I can make to resolve the problem into MouseMove method ?



the source of beta class


Code: Select all  Expand view



#include "Fivewin.ch"



Function Test()
Local oDlg,oFont
Local oRating

DEFINE FONT oFont  NAME "Segoe UI" SIZE 0, -12

Define Dialog oDlg  size 400,300



cBitmap1:=".\bitmaps\Empty6_L.bmp"
cBitmap2:=".\bitmaps\Empty6_R.bmp"
cBitmap3:=".\bitmaps\Full6_L.bmp"
cBitmap4:=".\bitmaps\Full6_R.bmp"

cCaption:="My Rating"

 oRating:=TRating():New(10,10,140,20,oDlg,cBitmap1,cBitmap2,cBitmap3,cBitmap4,cCaption,oFont)

*oRating:aRate:=




Activate Dialog oDlg center
return nil


//--------------------------------------------------------//



#define COLOR_BTNFACE   15

CLASS TRATING FROM TCONTROL


CLASSDATA lRegistered AS LOGICAL

DATA Empty_L, Empty_R, Full_L, Full_R
DATA nCount
DATA nPressed
DATA nValue
DATA cRate
DATA lSelected    AS LOGIC INIT .F.
DATA oReset
DATA ofont
DATA nClrBorder
DATA nClrText
DATA nClrReset
DATA hImage AS ARRAY INIT {}    // 10
DATA aItems  AS ARRAY INIT {}
DATA lBorder  AS LOGIC INIT .F.
DATA nOver
DATA lOverClose  AS LOGIC INIT .F.
DATA bAction
DATA nOption

METHOD New( nRow, nCol, nWidth, nHeight, oWnd,Empty_L,Empty_R,Full_L ,Full_R,cCaption,oFont)  CONSTRUCTOR
METHOD OnHoverRate( nSelected, nCount )
METHOD OnLeaveRate( nSelected, nCount)
METHOD OnSelectRate( nSelected, nCount)
METHOD Reset( nCount )
METHOD MouseMove( nRow, nCol, nKeyFlags )
METHOD MouseLeave( nRow, nCol, nFlags )
METHOD LButtonDown( nRow, nCol, nFlags )
METHOD LButtonUp  ( nRow, nCol, nFlags )
METHOD Paint()
METHOD Display() INLINE ::BeginPaint(), ::Paint(), ::EndPaint(), 0
METHOD EraseBkGnd( hDC ) INLINE 1
METHOD PaintReset( aRect)
METHOD LoadImage( cImage )
ENDCLASS


METHOD New( nRow, nCol, nWidth, nHeight, oWnd,Empty_L,Empty_R,Full_L ,Full_R,cCaption,oFont,nClrBorder,nClrText,nClrPane,lPixel,nClrReset,lBorder)

   DEFAULT oWnd         := GetWndDefault()
   DEFAULT nClrBorder   := rgb(192, 192, 192)
   DEFAULT nClrText     := CLR_BLACK
   DEFAULT nClrPane     := GetSysColor( COLOR_BTNFACE )
   DEFAULT lPixel       := .T.
   DEFAULT nClrReset    := CLR_RED
   DEFAULT cCaption     := "My Rating"
   DEFAULT lBorder      := .f.

   ::nTop    = nRow
   ::nLeft   = nCol
   ::nBottom = nHeight
   ::nRight  = nWidth
   ::oWnd    = oWnd
   ::cCaption   = cCaption
   ::nClrReset  = nClrReset
   ::nClrText   = nClrText

   ::lBorder    = lBorder

   ::nId  := ::GetNewId()
   ::nStyle  = nOr( WS_CHILD, WS_VISIBLE, WS_CLIPCHILDREN )
   ::nClrPane = GetSysColor( COLOR_BTNFACE )

   ::SetColor( nClrText, nClrPane )

   ::nCount       := 10


   ::Empty_L = Empty_L
   ::Empty_R = Empty_R
   ::Full_L  = Full_L
   ::Full_R  = Full_R
   ::hImage := array(10)
   ::oFont   = oFont
   ::lOverClose  := .f.
   ::nOver   := -1
   ::nOption   := 1


   ::Register( nOR( CS_VREDRAW, CS_HREDRAW ) )



   if oWnd:lVisible
      ::Create()
      ::Default()
      ::lVisible = .t.
      oWnd:AddControl( Self )
   else
      oWnd:DefControl( Self )
      ::lVisible  = .F.
   endif

return self
//---------------------------------------------------------------------------//




 METHOD Paint() CLASS TRating

   local aInfo := ::DispBegin()
   local aRect := GetClientRect(::hWnd)
   local hbrushBorder

   Local nBmpWidth,nCol,nRow
   Local nWidthCaption  := 35 + GetTextWidth(::hDC, ::cCaption, ::oFont:hFont )
   Local I := 1

   Local nPos := 1
   Local nLen := 10//len( ::nCount )



   FillSolidRect(::hDC, aRect, ::nClrPane )



   IF ::lBorder
        hBrushBorder      := CreateSolidBrush( ::nClrBorder )
        RoundRect( ::hDC, aRect[1], aRect[2], aRect[4], aRect[3]-1, 9, 9 )
        DeleteObject( hBrushBorder )
     Endif




    SetTextColor (::hDC, ::nClrText)
    SetBkMode( ::hDC, 1 )
    SelectObject( ::hDC, ::oFont:hFont )
    DrawTextEx( ::hDC, ::cCaption+" (", {aRect[1]+2,aRect[2],aRect[3],aRect[4]}, 0)
    ::PaintReset( aRect)
    DrawTextEx( ::hDC, ")", {aRect[1]+2,aRect[2]+nWidthCaption,aRect[3],aRect[4]}, 0)







if !Empty (::Empty_L) .or. !Empty (::Empty_R)

    nCol:= aRect[2]+nWidthCaption+30
    nRow := aRect[1]+1
     FOR i = 1 TO 10//::nCount
        IF nPos = 1
               ::hImage[i] := ::LoadImage( ::Empty_L )
                 DrawTransBmp( ::hDC, ::hImage[i], nRow, nCol, ::nbottom-8, 20 )
                 nPos := 2

             Else
               ::hImage[i] := ::LoadImage( ::Empty_R )
                DrawTransBmp( ::hDC, ::hImage[i], nRow, nCol, ::nbottom-8, 20 )
                nPos := 1

               ENDIF

               AAdd( ::aItems, {STR(i),.f.,{nRow, nCol,::nbottom-8,20}  } )

             nCol += 12

       NEXT

   Endif

      // wqout(i )

 ::DispEnd( aInfo )

 return 0

//--------------------------------------------------------------------//

METHOD LoadImage( cImage )  CLASS TRating

  local hImage := 0

  hImage := LoadBitmap( GetResources(), cImage )

  if hImage == 0
    hImage := ReadBitmap( 0, cImage )
  endif

return (hImage)

//--------------------------------------------------------------------//

METHOD PaintReset( aRect)   CLASS TRating
      Local  bAction := { || ( pressed := ::nCount, ::Reset(pressed) ) }
      Local nWidthCaption := 5 + GetTextWidth(::hDC, ::cCaption, ::oFont:hFont )

     ::oReset := TUrlLink():New( aRect[1]+2, aRect[2]+nWidthCaption+1, Self, .T., .F., ::oFont, "", "Reset" )  // ::cCaption
     ::oReset:SetColor( ::oReset:nClrText, ::oWnd:nClrPane )
     ::oReset:nClrOver = ::nClrReset
     ::oReset:bAction = bAction

  return 0

//--------------------------------------------------------------------//

METHOD LButtonDown( nRow, nCol, nFlags ) CLASS TRating

return 0

//--------------------------------------------------------------------//

METHOD LButtonUp  ( nRow, nCol, nFlags ) CLASS TRating
   if ::nOver > 0
   if ::lOverClose
      ::aItems[::nOver,2] := .t.
   else
      ::nOption := ::nOver
      if ::bAction != nil
         eval(::bAction, ::nOption, ::aItems[::nOption,1])
      endif
   endif
   ::Refresh()
endif
return NIL

//--------------------------------------------------------------------//

 METHOD MouseMove  ( nRow, nCol, nFlags ) CLASS TRating
local nOver := ::nOver
local n
local nLen := len(::aItems)
local lFind := .f.



   for n := 1 to 10//nLen
       if PtInRect( nRow, nCol,  ::aItems[n][3])
           lFind := .t.
            ::nOver := n
            if nOver != n
        endif
       exit
    endif
 next



::lOverClose := ::nOver > 0 .and. PtInRect( nRow, nCol, ::aItems[::nOver,3] )

*XBROWSER   ::aItems

if lFind
   if ::lOverClose
      CursorHand()
        ::OnHoverRate( ::aItems[::nOver], ::nCount )
     else
        CursorArrow()

   endif
 else
   ::nOver := -1
   CursorArrow()
endif

if nOver != ::nOver
   ::Refresh(.f.)
endif


 return NIL


//--------------------------------------------------------------------//
 METHOD MouseLeave( nRow, nCol, nFlags ) CLASS TRating


       *     ::pressed := 1

  //  ( pressed :=1,

   * ::OnLeaveRate(::pressed,::ncount)

 return NIL

//--------------------------------------------------------------------//


METHOD OnHoverRate( nSelected, nCount )
Local j := 1, nPos := 1

IF ::lSelected = .F.
   ::Reset( nCount )

   FOR j := 1 TO nSelected
      IF nPos = 1
        * ::hImage[j]:Load( ::Full_L )

          ::hImage[j] := ::LoadImage( ::Full_L )

         nPos := 2
      ELSE
       *  ::hImage[j]:LoadBmp( ::Full_R )
          ::hImage[j] := ::LoadImage( ::Full_R )
         nPos := 1
      ENDIF
   NEXT

  * nValue := nSelected * 10
  * cRate := ALLTRIM( STR( nSelected * 10 ) ) + "%"
  * oSay1:Refresh()
ENDIF

RETURN NIL
//--------------------------------------------------------------------//

METHOD OnLeaveRate( nSelected, nCount, oSay1 )
Local k, nPos := 1

IF ::lSelected = .F.
 *  nValue := 0
   IF nSelected == 0
      ::Reset( nCount )
   ELSE
      ::ONSELECTRATE( nSelected, nCount)
    *  nValue := nSelected * 10
   ENDIF
ENDIF

RETURN NIL

//--------------------------------------------------------------------//

METHOD   OnSelectRate( nSelected, nCount)
Local k, nPos := 1

IF nSelected > 0
   ::RESET(nCount)
ELSE
   IF nPos = 1
      ::hImage[k]:LoadBmp( ::Full_L )
      nPos := 2
   ELSE
      ::hImage[k]:LoadBmp( ::Full_R )
      nPos := 1
   ENDIF
ENDIF
*nValue := nSelected * 10
*cRate := ALLTRIM( STR( nSelected * 10 ) ) + "%"
*oSay1:Refresh()

RETURN NIL


METHOD  Reset( nCount )
Local x, nPos := 1

FOR x :=1 TO nCount
   IF nPos = 1
      ::hImage[x]:LoadBmp( ::Empty_L )
      nPos := 2
   ELSE
      ::hImage[x]:LoadBmp( ::Empty_R )
      nPos := 1
   ENDIF
NEXT
*nValue := 0
*cRate := "0%"
*oSay1 :Refresh()
::lSelected  := .F.

RETURN NIL

//--------------------------------------------------------------------//

static function DrawTransBmp( hDC, hBmp, nRow, nCol, nWidth, nHeight )

   local hDCMem, hBmpOld, nOldClr, nZeroZeroClr

   DEFAULT nWidth := nBmpWidth( hBmp ), nHeight := nBmpHeight( hBmp )

   hDCMem = CreateCompatibleDC( hDC )

   // we can not get nZeroZeroClr from hDC is possible hDC are locked by other SelectObject
   // An application cannot select a bitmap into more than one device context at a time.
   hBmpOld      = SelectObject( hDCMem, hBmp )
   nZeroZeroClr = GetPixel( hDCMem, 0, 0 )

   SelectObject( hDCMem, hBmpOld )
   DeleteDC( hDCMem )

   nOldClr = SetBkColor( hDC, nRGB( 255, 255, 255 ) )
   TransBmp( hBmp, nBmpWidth( hBmp ), nBmpHeight( hBmp ), nZeroZeroClr, hDC, ;
             nCol, nRow, nWidth, nHeight )
   SetBkColor( hDC, nOldClr )

return nil

 
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7061
Joined: Thu Oct 18, 2012 7:17 pm

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 67 guests