Help For create a Class

Help For create a Class

Postby Silvio » Wed Sep 24, 2008 10:15 am

Dear Antonio and All..
I 'm trying to create a new fwh class
I want create a list of Check box and insert it on alistbox as Items
I create it but there something not run also
Can you help me please

Code: Select all  Expand view
#include "fivewin.ch"

#define LB_SETITEMHEIGHT        0x01A0
#define LB_SETCOLUMNWIDTH       0x0195
#define LBS_MULTICOLUMN         0x0200
#define LBS_OWNERDRAWVARIABLE   0x0020
#define SRCCOPY 13369376
#define ODT_MENU        1
#define ODT_LISTBOX     2
#define ODT_COMBOBOX    3
#define ODT_BUTTON      4
#define ODA_DRAWENTIRE  0x0001
#define ODA_SELECT      0x0002
#define ODA_FOCUS       0x0004
#define ODS_SELECTED    0x0001
#define ODS_GRAYED      0x0002
#define ODS_DISABLED    0x0004
#define ODS_CHECKED     0x0008
#define ODS_FOCUS       0x0010
#define ODS_DEFAULT         0x0020
#define ODS_COMBOBOXEDIT    0x1000
#define LISTBOX_BASE    383
#define LB_ADDSTRING         ( LISTBOX_BASE +  1 )
#define LB_INSERTSTRING      ( LISTBOX_BASE +  2 )
#define LB_DELETESTRING      ( LISTBOX_BASE +  3 )
#define LB_RESETCONTENT      ( LISTBOX_BASE +  5 )
#define LB_SETSEL            ( LISTBOX_BASE +  6 )
#define LB_SETCURSEL         ( LISTBOX_BASE +  7 )
#define LB_GETSEL            ( LISTBOX_BASE +  8 )
#define LB_GETCURSEL         ( LISTBOX_BASE +  9 )
#define LB_GETCOUNT          ( LISTBOX_BASE + 12 )
#define LB_DIR               ( LISTBOX_BASE + 14 )
#define LB_GETSELCOUNT       ( LISTBOX_BASE + 17 )
#define LB_GETSELITEMS       ( LISTBOX_BASE + 18 )
#define LB_ERR                           -1

#define COLOR_WINDOW       5
#define COLOR_WINDOWTEXT   8


Static oWnd

FUNCTION Main()
Local oList

  DEFINE WINDOW oWnd

  aItems := { "Silvio Falconi","Enrico Maria Giordano", "Antonio Linares", "James Bott", ;
            "Big Luciano", "Bill Gates", "Bin laden", "Obama", "Bush", "Clinton", "Micky Mouse" }

  oList:= TListCheck():new( oWnd, 10, 10, 200, 200 )
  oList:add(aItems )
  oList:GoTop()

  ACTIVATE WINDOW oWnd

RETURN




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

//Beta version

CLASS TListCheck FROM TListBox

DATA aCheckBox AS ARRAY INIT {}

DATA nHeightItem // nheight for items

METHOD New( oWnd,nTop, nLeft, nWidth, nHeight, nHeightItem,nClrFore, nClrBack, lPixel, lDesign)  CONSTRUCTOR

METHOD Initiate( hDlg ) INLINE Super:Initiate( hDlg ), ::Default()

METHOD Default()

METHOD Display() INLINE ::BeginPaint(), ::Paint(), ::EndPaint(), 0

METHOD Paint()

METHOD DrawCheck()

METHOD AddItem   ( cItem, lValue )   INLINE ::Add( cItem,lValue )

METHOD SetCheck( nItem, lValue ) INLINE ::aCheckBoxs[ nItem ] := lValue, if( ::hWnd != nil, ::Refresh(),)

METHOD lselected( nItem ) INLINE ::aCheckBox[ nItem ]

METHOD SetHeightItem( nHItem ) INLINE ::nHeightItem := nHeightItem, ::SendMsg( LB_SETITEMHEIGHT, 0, ::nHeighItem )

ENDCLASS


METHOD New( oWnd,nTop, nLeft, nWidth, nHeight, nHeightItem,nClrFore, nClrBack, lPixel, lDesign) CLASS TListCheck

    if nClrFore == nil
      nClrBack := GetSysColor( COLOR_WINDOW )
   endif


  DEFAULT  nTop     := 0, nLeft := 0,;
           aItems   := {}, nWidth := 40, nHeight := 40,;
           nClrFore := GetSysColor( COLOR_WINDOWTEXT ),;
           lPixel   := .f., lDesign := .f.,;
           oWnd     := GetWndDefault()



::bSetGet  := { || nil } // from listbox class

::aItems     :=  {}



   ::cCaption   := ""
   ::nTop       := nTop
   ::nLeft      := nLeft
   ::nBottom    := ::nTop  + nHeight - 1
   ::nRight     := ::nLeft + nWidth - 1




   ::oWnd       = oWnd
   ::oFont      = TFont():New( GetSysFont(), 0, -9,, .t. )

   ::lOwnerDraw = .T.

   ::nStyle     = nOR( LBS_NOTIFY , LBS_OWNERDRAWFIXED , WS_CHILD , WS_VISIBLE ,;
                       WS_BORDER , WS_VSCROLL, WS_HSCROLL, WS_TABSTOP )


   if nHeightItem == nil
      nHeightItem := 21
   endif

   ::SetColor( nClrFore, nClrBack )

   if ! Empty( oWnd:hWnd )
      ::Create( "LISTBOX" )
      ::Default()
      if oFont != nil
         ::SetFont( oFont )
      endif
      oWnd:AddControl( Self )
   else
      oWnd:DefControl( Self )
   endif

     if lDesign
      ::CheckDots()
   endif

return Self


METHOD Paint() CLASS TListCheck

   local aInfo := ::DispBegin()

   if ::oBrush != nil
      FillRect( ::hDC, GetClientRect( ::hWnd ), ::oBrush:hBrush )
   else
      CallWindowProc( ::nOldProc, ::hWnd, WM_ERASEBKGND, ::hDC, 0 )
   endif

   CallWindowProc( ::nOldProc, ::hWnd, WM_PAINT, ::hDC, 0 )

   ::DrawCheck()

   ::DispEnd( aInfo )

return 1



METHOD Default() CLASS TListCheck

local nAt
   local cStart := Eval( ::bSetGet )
   local aFiles

   DEFAULT cStart := ""

   ::SetHeightItem( ::nHeightItem )  // to set the Height for each Items


   if ! Empty( ::cFileSpec )
      aFiles = Directory( ::cFileSpec )
      for nAt = 1 to Len( aFiles )
         AAdd( ::aItems, Lower( aFiles[ nAt ][ 1 ] ) )
      next
      ASort( ::aItems )
   endif

   AEval( ::aItems,;
          { | cItem, nAt | If( cItem == nil, ::aItems[ nAt ] := "",),;
                               ::SendMsg( LB_ADDSTRING, 0,;
                               If( cItem == nil, "", cItem ) ) } )

   if ValType( cStart ) != "N"
      nAt = AScan( ::aItems, { | cItem | Upper( AllTrim( cItem ) ) == ;
                                         Upper( AllTrim( cStart ) ) } )
   else
      nAt = cStart
   endif

   if nAt != 0
      ::SendMsg( LB_SETCURSEL, nAt - 1 )
   else
      ::SendMsg( LB_SETCURSEL, 0 )
   endif

  ::SetHeightItem( ::nHeightItem )  // to set the Height for each Items

return nil


METHOD DrawCheck()  CLASS TListCheck
    LOCAL i, obj, nFila
    nFila := 1

    FOR i := 1 TO Len( ::aItems )
        oChk := TCheckBox():New( nFila, ::nLeft, aItems[ i ], , oWnd)
        Aadd( ::aCheckBox, oChk )
    nFila += 21
  NEXT

return nil


Last edited by Silvio on Wed Sep 24, 2008 11:48 am, edited 1 time in total.
Best Regards, Saludos

Falconi Silvio
User avatar
Silvio
 
Posts: 3107
Joined: Fri Oct 07, 2005 6:28 pm
Location: Teramo,Italy

Postby Antonio Linares » Wed Sep 24, 2008 10:39 am

Silvio,

You should not create checkboxes from the method Paint(). Thats will consume lots of GDI resources!

You may create them just once from another method and then reuse them from Paint().
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42080
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Help For create a Class

Postby Enrico Maria Giordano » Wed Sep 24, 2008 10:40 am

As the first step, please fix these warnings:

LISTCHECK.prg(49) Warning W0001 Ambiguous reference: 'AITEMS'
LISTCHECK.prg(51) Warning W0001 Ambiguous reference: 'OLIST'
LISTCHECK.prg(52) Warning W0001 Ambiguous reference: 'OLIST'
LISTCHECK.prg(52) Warning W0001 Ambiguous reference: 'AITEMS'
LISTCHECK.prg(53) Warning W0001 Ambiguous reference: 'OLIST'
LISTCHECK.prg(57) Warning W0005 RETURN statement with no return value in function
LISTCHECK.prg(66) Warning W0003 Variable: 'LIST' declared but not used in function: 'MAIN(44)'
LISTCHECK.prg(90) Warning W0001 Ambiguous reference: 'NHEIGHTITEM'
LISTCHECK.prg(106) Warning W0001 Ambiguous reference: 'AITEMS'
LISTCHECK.prg(106) Warning W0001 Ambiguous reference: 'AITEMS'
LISTCHECK.prg(143) Warning W0001 Ambiguous reference: 'OFONT'
LISTCHECK.prg(144) Warning W0001 Ambiguous reference: 'OFONT'
LISTCHECK.prg(165) Warning W0001 Ambiguous reference: 'WM_ERASEBKGND'
LISTCHECK.prg(225) Warning W0002 Ambiguous reference, assuming memvar: 'AITEMS'
LISTCHECK.prg(225) Warning W0001 Ambiguous reference: 'OCHK'
LISTCHECK.prg(226) Warning W0001 Ambiguous reference: 'OCHK'
LISTCHECK.prg(231) Warning W0003 Variable: 'OBJ' declared but not used in function: 'TLISTCHECK_DRAWCHECK(221)'


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8710
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Postby Silvio » Wed Sep 24, 2008 11:53 am

EMG
olist variable is init

this code is correctd
Code: Select all  Expand view
#include "fivewin.ch"

#define LB_SETITEMHEIGHT        0x01A0
#define LB_SETCOLUMNWIDTH       0x0195
#define LBS_MULTICOLUMN         0x0200
#define LBS_OWNERDRAWVARIABLE   0x0020
#define SRCCOPY 13369376
#define ODT_MENU        1
#define ODT_LISTBOX     2
#define ODT_COMBOBOX    3
#define ODT_BUTTON      4
#define ODA_DRAWENTIRE  0x0001
#define ODA_SELECT      0x0002
#define ODA_FOCUS       0x0004
#define ODS_SELECTED    0x0001
#define ODS_GRAYED      0x0002
#define ODS_DISABLED    0x0004
#define ODS_CHECKED     0x0008
#define ODS_FOCUS       0x0010
#define ODS_DEFAULT         0x0020
#define ODS_COMBOBOXEDIT    0x1000
#define LISTBOX_BASE    383
#define LB_ADDSTRING         ( LISTBOX_BASE +  1 )
#define LB_INSERTSTRING      ( LISTBOX_BASE +  2 )
#define LB_DELETESTRING      ( LISTBOX_BASE +  3 )
#define LB_RESETCONTENT      ( LISTBOX_BASE +  5 )
#define LB_SETSEL            ( LISTBOX_BASE +  6 )
#define LB_SETCURSEL         ( LISTBOX_BASE +  7 )
#define LB_GETSEL            ( LISTBOX_BASE +  8 )
#define LB_GETCURSEL         ( LISTBOX_BASE +  9 )
#define LB_GETCOUNT          ( LISTBOX_BASE + 12 )
#define LB_DIR               ( LISTBOX_BASE + 14 )
#define LB_GETSELCOUNT       ( LISTBOX_BASE + 17 )
#define LB_GETSELITEMS       ( LISTBOX_BASE + 18 )
#define LB_ERR                           -1

#define COLOR_WINDOW       5
#define COLOR_WINDOWTEXT   8


Static oWnd

FUNCTION Main()
Local oList,aItems

  DEFINE WINDOW oWnd

  aItems := { "Silvio Falconi","Enrico Maria Giordano", "Antonio Linares", "James Bott", ;
            "Big Luciano", "Bill Gates", "Bin laden", "Obama", "Bush", "Clinton", "Micky Mouse" }

  oList:= TListCheck():new( oWnd, 10, 10, 200, 200 )
  oList:add(aItems )
  oList:GoTop()

  ACTIVATE WINDOW oWnd

RETURN NIL




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

//Beta version

CLASS TListCheck FROM TListBox

DATA aCheckBox AS ARRAY INIT {}

DATA nHeightItem // nheight for items


METHOD New( oWnd,nTop, nLeft, nWidth, nHeight, nHeightItem,nClrFore, nClrBack, lPixel, lDesign)  CONSTRUCTOR

METHOD Initiate( hDlg ) INLINE Super:Initiate( hDlg ), ::Default()

METHOD Default()

METHOD Display() INLINE ::BeginPaint(), ::Paint(), ::EndPaint(), 0

METHOD Paint()

METHOD DrawCheck()

METHOD AddItem   ( cItem, lValue )   INLINE ::Add( cItem,lValue )

METHOD lselected( nItem ) INLINE ::aCheckBox[ nItem ]



METHOD SetHeightItem( nHeightItem ) INLINE ::nHeightItem := nHeightItem, ::SendMsg( LB_SETITEMHEIGHT, 0, ::nHeighItem )

ENDCLASS


METHOD New( oWnd,nTop, nLeft, nWidth, nHeight, nHeightItem,nClrFore, nClrBack, lPixel, lDesign) CLASS TListCheck

    if nClrFore == nil
      nClrBack := GetSysColor( COLOR_WINDOW )
   endif


  DEFAULT  nTop     := 0, nLeft := 0,;
           ::aItems   := {}, nWidth := 40, nHeight := 40,;
           nClrFore := GetSysColor( COLOR_WINDOWTEXT ),;
           lPixel   := .f., lDesign := .f.,;
           oWnd     := GetWndDefault()



::bSetGet  := { || nil } // from listbox class

::aItems     :=  {}



   ::cCaption   := ""
   ::nTop       := nTop
   ::nLeft      := nLeft
   ::nBottom    := ::nTop  + nHeight - 1
   ::nRight     := ::nLeft + nWidth - 1




   ::oWnd       = oWnd
   ::oFont      = TFont():New( GetSysFont(), 0, -9,, .t. )

   ::lOwnerDraw = .T.

   ::nStyle     = nOR( LBS_NOTIFY , LBS_OWNERDRAWFIXED , WS_CHILD , WS_VISIBLE ,;
                       WS_BORDER , WS_VSCROLL, WS_HSCROLL, WS_TABSTOP )


   if nHeightItem == nil
      nHeightItem := 21
   endif

   ::SetColor( nClrFore, nClrBack )

   if ! Empty( oWnd:hWnd )
      ::Create( "LISTBOX" )
      ::Default()

      oWnd:AddControl( Self )
   else
      oWnd:DefControl( Self )
   endif

     if lDesign
      ::CheckDots()
   endif

return Self


METHOD Paint() CLASS TListCheck

   local aInfo := ::DispBegin()

   if ::oBrush != nil
      FillRect( ::hDC, GetClientRect( ::hWnd ), ::oBrush:hBrush )
   else
    //  CallWindowProc( ::nOldProc, ::hWnd, WM_ERASEBKGND, ::hDC, 0 )
   endif
   CallWindowProc( ::nOldProc, ::hWnd, WM_PAINT, ::hDC, 0 )
     ::DrawCheck()
   ::DispEnd( aInfo )

return 1



METHOD Default() CLASS TListCheck

local nAt
   local cStart := Eval( ::bSetGet )
   local aFiles

   DEFAULT cStart := ""

   ::SetHeightItem( ::nHeightItem )  // to set the Height for each Items


   if ! Empty( ::cFileSpec )
      aFiles = Directory( ::cFileSpec )
      for nAt = 1 to Len( aFiles )
         AAdd( ::aItems, Lower( aFiles[ nAt ][ 1 ] ) )
      next
      ASort( ::aItems )
   endif

   AEval( ::aItems,;
          { | cItem, nAt | If( cItem == nil, ::aItems[ nAt ] := "",),;
                               ::SendMsg( LB_ADDSTRING, 0,;
                               If( cItem == nil, "", cItem ) ) } )

   if ValType( cStart ) != "N"
      nAt = AScan( ::aItems, { | cItem | Upper( AllTrim( cItem ) ) == ;
                                         Upper( AllTrim( cStart ) ) } )
   else
      nAt = cStart
   endif

   if nAt != 0
      ::SendMsg( LB_SETCURSEL, nAt - 1 )
   else
      ::SendMsg( LB_SETCURSEL, 0 )
   endif

  ::SetHeightItem( ::nHeightItem )  // to set the Height for each Items

return nil


METHOD DrawCheck()  CLASS TListCheck
    LOCAL i, ochk, nFila
    nFila := 1

    FOR i := 1 TO Len( ::aItems )
        oChk := TCheckBox():New( nFila, ::nLeft, ::aItems[ i ], , oWnd)
        Aadd( ::aCheckBox, oChk )
    nFila += 21
  NEXT

return nil









FOR ANTONIO
I create CHECKBOX on DrawCheck() method and then I call it from Paint method


IDEA
WHY we cannot USE XBROWSE instead of LISTBOX ?
Best Regards, Saludos

Falconi Silvio
User avatar
Silvio
 
Posts: 3107
Joined: Fri Oct 07, 2005 6:28 pm
Location: Teramo,Italy

Postby Antonio Linares » Wed Sep 24, 2008 12:18 pm

Silvio,

> WHY we cannot USE XBROWSE instead of LISTBOX ?

You can use a xbrowse and use two bitmaps, one for a checked checkbox and another for an unchecked one, and select the one to paint based on the field value, etc.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42080
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Silvio » Wed Sep 24, 2008 12:25 pm

Dear Antonio,
I allready thinked U sad me to use two bitmaps : why we can use checkbox system ?
Best Regards, Saludos

Falconi Silvio
User avatar
Silvio
 
Posts: 3107
Joined: Fri Oct 07, 2005 6:28 pm
Location: Teramo,Italy

Postby Antonio Linares » Wed Sep 24, 2008 1:45 pm

Silvio,

If you want to use a real checkbox then it has to be created as a real control, and you need one for each visible row.

Also, when the browse scrolls or goes top or bottom, all checkboxes should be repositioned and updated their values. It is much more complex that just use two bitmaps.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42080
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Enrico Maria Giordano » Wed Sep 24, 2008 2:16 pm

This is a sample using two bitmaps (16 x 16) to simulate a checkbox:

Code: Select all  Expand view
#include "Fivewin.ch"


FUNCTION MAIN()

    LOCAL oDlg, oBrw

    LOCAL hBmp1 := READBITMAP( 0, "SELECT.BMP" )
    LOCAL hBmp2 := READBITMAP( 0, "UNSELECT.BMP" )

    USE TEST

    DEFINE DIALOG oDlg SIZE 400, 300

    @ 0, 0 LISTBOX oBrw FIELDS FIELD -> last,;
                               FIELD -> first,;
                               IF( FIELD -> married, hBmp1, hBmp2 ),;
                               "";
           HEADERS "LAST", "FIRST", "M", "";
           SIZES 100, 100, 16, 0

    oBrw:bLClicked = { | nRow, nCol | Cambia( oBrw, nCol ) }

    ACTIVATE DIALOG oDlg;
             ON INIT oDlg:SetControl( oBrw );
             CENTER

    CLOSE

    DELETEOBJECT( hBmp1 )
    DELETEOBJECT( hBmp2 )

    RETURN NIL


STATIC FUNCTION CAMBIA( oBrw, nCol )

    IF oBrw:nAtCol( nCol ) = 3
        REPLACE FIELD -> married WITH !FIELD -> married
        oBrw:Refresh( .F. )
    ENDIF

    RETURN NIL


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8710
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Postby Silvio » Wed Sep 24, 2008 6:24 pm

I not understand ... why You offer me the solution with bitmaps.

I have allreay this type of solution.

I not understand why we cannot use checkboxs into listbox there was any problems ?
Best Regards, Saludos

Falconi Silvio
User avatar
Silvio
 
Posts: 3107
Joined: Fri Oct 07, 2005 6:28 pm
Location: Teramo,Italy

Postby Enrico Maria Giordano » Wed Sep 24, 2008 7:12 pm

Antonio already explained the reason in a crystal clear form.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8710
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Postby Silvio » Thu Sep 25, 2008 4:03 pm

sorry ...
but someone( *A*O of C*n*l5) have make a lbxcheck no consume lots of GDI resources
Best Regards, Saludos

Falconi Silvio
User avatar
Silvio
 
Posts: 3107
Joined: Fri Oct 07, 2005 6:28 pm
Location: Teramo,Italy

Postby Antonio Linares » Thu Sep 25, 2008 4:24 pm

Silvio,

You can also use a ownerdraw ListBox.

Please review FWH\samples\ownerdra.prg
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42080
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 51 guests