Module cSigCJImage_Manager_Mixin.pkg

     1//==============================================================================
     2// Project      : SigCj - VDF Classes for Codejock
     3// File         : cSigCJImage_Manager_Mixin.pkg
     4// Description  : VDF Class for Codejock control
     5//
     6// Revision     : $Rev: 685 $
     7//                $Date: 2011-03-14 12:19:00 -0400 (Mon, 14 Mar 2011) $
     8//                $Author: martin $ Martin Pincott
     9// 
    10// Requirements : Visual DataFlex 12.1+
    11//                Codejock SuitePro - Version 12.0.0+
    12//
    13// Copyright    : (c) 2008 VDF SIG UK
    14//                Visual DataFlex Special Interest Group UK.
    15//                http://www.vdfsig.co.uk/
    16//                dev@vdfsig.co.uk
    17//
    18//                This file is part of SigCj.
    19//
    20//                SigCj is free software: you can redistribute it and/or modify
    21//                it under the terms of the GNU Lesser General Public License 
    22//                as published by the Free Software Foundation, either version 
    23//                2.1 of the License, or (at your option) any later version.
    24//
    25//                SigCj is distributed in the hope that it will be useful, but 
    26//                WITHOUT ANY WARRANTY; without even the implied warranty of
    27//                MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    28//                GNU Lesser General Public License for more details.
    29//
    30//                If you have the complete SigCj workspace then a copy of the  
    31//                GNU Lesser General Public License is in the Docs folder. If  
    32//                not, see <http://www.gnu.org/licenses/>.
    33//    
    34//==============================================================================
    35
    36Use VDFBase.pkg
    37
    38Struct tdImageInfo
    39    String  sImageName
    40    Integer iImageId
    41    Variant vImageHandle
    42    Handle  hWnd
    43End_Struct
    44
    45Class cSigCJImage_Manager_Mixin is a Mixin
    46
    47    // Initialise, create properties etc
    48    Procedure Initialise_Image_Manager_Mixin 
    49
    50        { Category="CodeJock" }
    51        Property Handle phoImagelist
    52
    53        { Visibility=Private }
    54        Property Integer piLastImageId 5000
    55
    56        { Visibility=Private }
    57        Property tdImageInfo[] ptImageInfo
    58
    59    End_Procedure
    60
    61    // Creation method that creates the image list object for storage of images and icons
    62    { MethodType=Method }
    63    Procedure Create_Image_Manager
    64        Handle  hoImageManager
    65        Variant vImagelist
    66        
    67        Get Create U_cCJImageManager to hoImageManager
    68        Send CreateComObject of hoImageManager
    69        Get ComIcons of hoImageManager to vImagelist
    70        Set ComIcons to vImagelist
    71        Send destroy of hoImageManager
    72    End_Procedure  // Create_Image_Manager
    73
    74    // Hook for loading Images 
    75    { MethodType=Method }
    76    Procedure Load_Images
    77    End_Procedure
    78    
    79    {DocStart = Method}
    80    //---------------------------------------------------
    81    //
    82    // Function     : CompareImageName
    83    //
    84    // Scope        : Private 
    85    //
    86    // Purpose      : Used internally as part of a SearchArray call to compare the sImageName elements 
    87    //                of two tdImageInfo structs 
    88    //               
    89    //---------------------------------------------------
    90    {DocEnd} 
    91    
    92    {MethodType=Method Visibility=Private}    
    93    Function CompareImageName tdImageInfo tImage1 tdImageInfo tImage2 Returns Integer
    94        Integer iRetVal
    95        If (tImage1.sImageName = tImage2.sImageName) Begin
    96            Move (EQ) to iRetVal
    97        End  
    98        Function_Return iRetVal
    99    End_Function
   100
   101    {DocStart = Method}
   102    //---------------------------------------------------
   103    //
   104    // Function     : CompareImageID
   105    //
   106    // Scope        : Private 
   107    //
   108    // Purpose      : Used internally as part of a SearchArray call to compare the iImageId elements 
   109    //                of two tdImageInfo structs 
   110    //               
   111    //---------------------------------------------------
   112    {DocEnd} 
   113    
   114    {MethodType=Method Visibility=Private}       
   115    Function CompareImageID tdImageInfo tImage1 tdImageInfo tImage2 Returns Integer
   116        Integer iRetVal
   117        If (tImage1.iImageId = tImage2.iImageId) Begin
   118            Move (EQ) to iRetVal
   119        End  
   120        Function_Return iRetVal
   121    End_Function
   122
   123    {DocStart = Method}
   124    //---------------------------------------------------
   125    //
   126    // Function     : Find_Image
   127    //
   128    // Scope        : Public 
   129    //
   130    // Paramaters   : String sImage
   131    //
   132    // Returns      : Integer (Image ID)
   133    //
   134    // Purpose      : Searches the ptImageInfo property to see if an image, specified by name ("someicon.ico") has
   135    //                already been uploaded. If so, it returns the unique id to that image.            
   136    //               
   137    //---------------------------------------------------
   138    {DocEnd} 
   139        
   140    Function Find_Image String sImage Returns Integer
   141        Integer iImageId iSearchIndex        
   142        tdImageInfo[] taImageInfo
   143        tdImageInfo tImageInfo
   144        
   145        Get ptImageInfo to taImageInfo
   146        Move sImage to tImageInfo.sImageName
   147        Move (SearchArray(tImageInfo,taImageInfo,Self,get_CompareImageName)) to iSearchIndex
   148        If (iSearchIndex <> -1) Begin
   149            Move taImageInfo[iSearchIndex].iImageId to iImageId
   150        End   
   151        Function_Return iImageId
   152    End_Function
   153
   154    {DocStart = Method}
   155    //---------------------------------------------------
   156    //
   157    // Function     : Find_Image_Name
   158    //
   159    // Scope        : Public 
   160    //
   161    // Paramaters   : Integer iImageId
   162    //
   163    // Returns      : Handle
   164    //
   165    // Purpose      : Returns the Image file name as specified by its Image ID                  
   166    //               
   167    //---------------------------------------------------
   168    {DocEnd} 
   169        
   170    Function Find_Image_Name Integer iImageId Returns String
   171        String sRetVal
   172        Integer iSearchIndex        
   173        tdImageInfo[] taImageInfo
   174        tdImageInfo tImageInfo
   175        
   176        Get ptImageInfo to taImageInfo
   177        Move iImageId to tImageInfo.iImageId
   178        Move (SearchArray(tImageInfo,taImageInfo,Self,get_CompareImageID)) to iSearchIndex
   179        If (iSearchIndex <> -1) Begin
   180            Move taImageInfo[iSearchIndex].sImageName to sRetVal
   181        End                
   182        Function_Return sRetVal  
   183    End_Function
   184
   185    {DocStart = Method}
   186    //---------------------------------------------------
   187    //
   188    // Function     : ImageHwndFromId
   189    //
   190    // Scope        : Public 
   191    //
   192    // Paramaters   : Integer iImageId
   193    //
   194    // Returns      : Handle
   195    //
   196    // Purpose      : Returns the window handle to an image as specified by its Image ID                  
   197    //               
   198    //---------------------------------------------------
   199    {DocEnd} 
   200        
   201    Function ImageHwndFromId Integer iImageId Returns Handle
   202        Handle hRetVal
   203        Integer iSearchIndex        
   204        tdImageInfo[] taImageInfo
   205        tdImageInfo tImageInfo
   206        
   207        Get ptImageInfo to taImageInfo
   208        Move iImageId to tImageInfo.iImageId
   209        Move (SearchArray(tImageInfo,taImageInfo,Self,get_CompareImageID)) to iSearchIndex
   210        If (iSearchIndex <> -1) Begin
   211            Move taImageInfo[iSearchIndex].hWnd to hRetVal
   212        End                
   213        Function_Return hRetVal  
   214    End_Function
   215    
   216    {DocStart = Method}
   217    //---------------------------------------------------
   218    //
   219    // Function     : ImageFromId
   220    //
   221    // Scope        : Public 
   222    //
   223    // Paramaters   : Integer iImageId
   224    //
   225    // Returns      : variant (IDispatch* pointer to the image)
   226    //
   227    // Purpose      : Returns the IDispatch* pointer to the image as specified by its Image ID                  
   228    //               
   229    //---------------------------------------------------
   230    {DocEnd} 
   231    
   232    Function ImageFromId Integer iImageId Returns Variant
   233        Variant vImage
   234        Integer iSearchIndex        
   235        tdImageInfo[] taImageInfo
   236        tdImageInfo tImageInfo
   237        
   238        Get ptImageInfo to taImageInfo
   239        Move iImageId to tImageInfo.iImageId
   240        Move (SearchArray(tImageInfo,taImageInfo,Self,get_CompareImageID)) to iSearchIndex
   241        If (iSearchIndex <> -1) Begin
   242            Move taImageInfo[iSearchIndex].vImageHandle to vImage
   243        End  
   244        Else Begin
   245            Move (NullComObject()) to vImage
   246        End
   247        Function_Return vImage
   248    End_Function
   249        
   250
   251    {DocStart=Method}
   252    //-------------------------------------------------------------------------
   253    //
   254    //Purpose : Adds an image to the image manager
   255    //
   256    //Parameters : sImage - The file name of the image to load#
   257    //             iID - If zero the next ID will be allocated 
   258    //                   If NOT zero this will be used as the ID. If an image 
   259    //                   with this ID has already been loaded then it will be 
   260    //                   replaced
   261    //              eImageType - The usage type of the image i.e. normal, 
   262    //                           shadowed, checked etc.
   263    //
   264    //Returns : The image ID. 
   265    //
   266    //-------------------------------------------------------------------------
   267    {DocEnd}
   268    
   269//    Function AddImage String sImage Integer iId Integer eImageType Returns Integer
   270//        Boolean bIsIcon bIsBitmap bOK
   271//        Handle hImage
   272//        Variant vImageManager vImageHandle
   273//        Handle  hoImageIcons
   274//        Integer iVoid iNextImage iImageId
   275//        String sFileImage
   276//        tdImageInfo[] tImageInfo
   277//
   278//        //Have we already loaded this 
   279//        Get Find_Image sImage to iImageId
   280//        If (iImageId = 0) Begin
   281//
   282//            //What type of image...
   283//            //If .ICO try loading from file first then from resource (resource only allows one image)
   284//            //If .BMP try loading from resource first then from file (resource quicker)
   285//            //If any other load from file (resource only support .BMP & .ICO)
   286//            Move (Pos(".ico",Lowercase(sImage))>0) to bIsIcon
   287//            Move (Pos(".bmp",Lowercase(sImage))>0) to bIsBitMap
   288//            Move False to bOK
   289//
   290//            //If no ID passed use next else replace image for specified ID
   291//                If (iId=0) Begin
   292//                    Get piLastImageId to iImageId
   293//                    Increment iImageId
   294//                    Set piLastImageId to iImageId
   295//                End
   296//                Else Begin
   297//                    Move iId to iImageId
   298//                End
   299//
   300//            //Connect to the local Image manager
   301//                Get Create U_cCJImageManagerIcons to hoImageIcons
   302//                Get ComIcons to vImageManager
   303//                    Set pvComObject of hoImageIcons to vImageManager
   304//    
   305//            // if an icon we will always try to load from a file first. Loading from a file works better than
   306//            // the internal resource load because the internal load only finds the first image. If the ico has
   307//            // multiple images (which CJ can use) all of the extra images are lost
   308//    
   309//            // if an Icon - load from file first then load from resource
   310//            If bIsIcon Begin
   311//                Get_File_Path sImage to sFileImage // find path in DFPATH, if appropriate
   312//                If (sFileImage<>"") Begin
   313//                    Send ComLoadIcon of hoImageIcons sFileImage iImageId eImageType
   314//                    Move True to bOk
   315//                    End
   316//                    Else Begin
   317//                    Move (LoadImage(GetModuleHandle(0), sImage, IMAGE_ICON, 0, 0, 0)) to hImage
   318//                    If hImage Begin
   319//                        // this works with alpha blends - even when passed false
   320//                        Send ComAddIconHandle hImage iImageId eImageType False
   321//                        Move (DestroyIcon(hImage)) to iVoid
   322//                        Move True to bOk
   323//                    End
   324//                End
   325//            End //is icon
   326//            Else Begin
   327//                If bIsBitmap Begin // if a bitmap - load from resource first then from file
   328//                    Move (LoadImage(GetModuleHandle(0), sImage, IMAGE_BITMAP, 0, 0, 0)) to hImage
   329//                    If (hImage=0) Begin // the bitmap was not in the EXE resource
   330//                        Get_File_Path sImage to sFileImage // find path in DFPATH, if appropriate
   331//                        If (sFileImage <>"") Begin // The image was found!
   332//                            Move (LoadImage(0, sFileImage, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE)) to hImage
   333//                        End
   334//                    End
   335//
   336//                    If hImage Begin
   337//                        Send ComAddBitmap of hoImageIcons hImage iImageId eImageType False
   338//                        Move (DeleteObject(hImage)) to iVoid
   339//                        Move True to bOk
   340//                    End
   341//                End //is bitmap
   342//                Else Begin //other image type
   343//                    Get_File_Path sImage to sFileImage // find path in DFPATH, if appropriate
   344//                    If (sFileImage <>"") Begin // The image was found!
   345//                        Send ComLoadBitmap of hoImageIcons sFileImage iImageId eImageType
   346//                        Move True to bOk
   347//                    End
   348//                End
   349//            End //not icon
   350//
   351//                    Get ComGetImage of hoImageIcons iImageId 0 to vImageHandle
   352//                Send destroy of hoImageIcons
   353//
   354//            If bOK Begin
   355//                //Note what we have just added                
   356//                Get ptImageInfo to tImageInfo
   357//                Move (SizeOfArray(tImageInfo)) to iNextImage
   358//                Move sImage       to tImageInfo[iNextImage].sImageName
   359//                Move iImageId     to tImageInfo[iNextImage].iImageID
   360//                Move vImageHandle to tImageInfo[iNextImage].vImageHandle
   361//                Move hImage       to tImageInfo[iNextImage].hWnd
   362//                Set ptImageInfo   to tImageInfo
   363//            End
   364//        End
   365//        
   366//        Function_Return (If(bOk, iImageId, 0))
   367//    End_Procedure
   368    
   369    Function AddImage String sImage Integer iId Integer eImageType Returns Integer
   370        Boolean bIsIcon bOK 
   371        Handle  hBitmap 
   372        Handle  hoImageIcons
   373        Integer iVoid eType iNextImage iImageId
   374        String  sImagePath
   375        Variant vImageManager vImageHandle
   376        tdImageInfo[] tImageInfo
   377
   378        //Keep thing simple
   379        Move (Uppercase(sImage)) to sImage
   380
   381        //Have we already loaded this 
   382        Get Find_Image sImage to iImageId
   383        
   384        If (iImageId = 0) Begin
   385            //Are we looking for an Icon or a Bitmap
   386            Move (Pos(".ICO",sImage)>0) to bIsIcon
   387            Move (If(bIsIcon,IMAGE_ICON,IMAGE_BITMAP)) to eType
   388
   389            //Is it in the EXE as a resource
   390            Move (LoadImage(GetModuleHandle(0), sImage, eType, 0, 0, 0)) to hBitmap
   391            If (hBitmap =0) Begin // the bitmap was not in the EXE resource
   392                Get_File_Path sImage to sImagePath // find path in DFPATH, if appropriate
   393                If (sImagePath <> "") Begin // The image was found!
   394                    //If we use LoadImage to here we can ignore the "load from file"
   395                    //methods in the CJ Image Manager
   396                    Move (LoadImage(0, sImagePath, eType, 0, 0, LR_LOADFROMFILE)) to hBitmap
   397                End
   398            End
   399
   400            //If we have an image loaded into memeory, add it 
   401            If (hBitmap <> 0) Begin
   402
   403                //Sort out the ID
   404                If (iId=0) Begin
   405                    Get piLastImageId to iImageId
   406                    Increment iImageId
   407                    Set piLastImageId to iImageId
   408                End
   409                Else Begin
   410                    Move iId to iImageId
   411                End
   412
   413                Get ComIcons to vImageManager
   414            Get Create U_cCJImageManagerIcons to hoImageIcons
   415                    Set pvComObject of hoImageIcons to vImageManager
   416    
   417                    If (bIsIcon) Begin
   418                        Send ComAddIcon of hoImageIcons hBitmap iImageId eImageType 
   419                    End
   420                    Else Begin
   421                        Send ComAddBitmap of hoImageIcons hBitmap iImageId eImageType False
   422                        // this works with alpha blends - even when passed false
   423                    End
   424
   425                    Get ComGetImage of hoImageIcons iImageId 0 to vImageHandle
   426                Send destroy of hoImageIcons
   427
   428                Move (DeleteObject(hBitmap)) to iVoid
   429
   430                //Note what we have just added                
   431                Get ptImageInfo to tImageInfo
   432                Move (SizeOfArray(tImageInfo)) to iNextImage
   433                Move sImage       to tImageInfo[iNextImage].sImageName
   434                Move iImageId     to tImageInfo[iNextImage].iImageID
   435                Move vImageHandle to tImageInfo[iNextImage].vImageHandle
   436                Move hBitmap      to tImageInfo[iNextImage].hWnd
   437                Set ptImageInfo   to tImageInfo
   438            End
   439        End
   440        
   441        Function_Return iImageId
   442    End_Procedure
   443End_Class 
   444
   445//==============================================================================
   446//End of Package - cSigCJImage_Manager_Mixin.pkg
   447//==============================================================================
   448