Example #1
0
// Create a drag image from a bitmap and optional cursor
bool wxDragImage::Create(const wxBitmap& image, const wxCursor& cursor)
{
    if ( m_hImageList )
        ImageList_Destroy(GetHimageList());
    m_hImageList = 0;

#ifdef __WXWINCE__
    UINT flags = ILC_COLOR;
#else
    UINT flags wxDUMMY_INITIALIZE(0) ;
    if (image.GetDepth() <= 4)
        flags = ILC_COLOR4;
    else if (image.GetDepth() <= 8)
        flags = ILC_COLOR8;
    else if (image.GetDepth() <= 16)
        flags = ILC_COLOR16;
    else if (image.GetDepth() <= 24)
        flags = ILC_COLOR24;
    else
        flags = ILC_COLOR32;
#endif

    bool mask = (image.GetMask() != 0);

    // Curiously, even if the image doesn't have a mask,
    // we still have to use ILC_MASK or the image won't show
    // up when dragged.
//    if ( mask )
    flags |= ILC_MASK;

    m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1);

    int index;
    if (!mask)
    {
        HBITMAP hBitmap1 = (HBITMAP) image.GetHBITMAP();
        index = ImageList_Add(GetHimageList(), hBitmap1, 0);
    }
    else
    {
        HBITMAP hBitmap1 = (HBITMAP) image.GetHBITMAP();
        HBITMAP hBitmap2 = (HBITMAP) image.GetMask()->GetMaskBitmap();
        HBITMAP hbmpMask = wxInvertMask(hBitmap2);

        index = ImageList_Add(GetHimageList(), hBitmap1, hbmpMask);
        ::DeleteObject(hbmpMask);
    }
    if ( index == -1 )
    {
        wxLogError(_("Couldn't add an image to the image list."));
    }
    m_cursor = cursor; // Can only combine with drag image after calling BeginDrag.

    return (index != -1) ;
}
void TempCreateXpm( wxBitmap &bmp, const char *name )
{
    static char codes[] = " .-,`~{}=+&$*@#0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    static int reds[1000];
    static int greens[1000];
    static int blues[1000];
    int nbr_of=0;
    static char filename[100];
    sprintf( filename, "%s.xpm", name );

	// Get dimensions of the wxBitmap
	//bmp.GetBitmap( &info );
	BITMAP info;
    ::GetObject( bmp.GetHBITMAP(), sizeof(BITMAP), &info ); //@@
	int width_bytes  = info.bmWidthBytes;   // bytes
	int width        = info.bmWidth;        // pixels
  	assert( (info.bmWidthBytes%info.bmWidth) == 0 );
	int density      = info.bmWidthBytes/info.bmWidth;
	int height       = info.bmHeight;
    byte                *buf_bmp;
    buf_bmp = new byte[width_bytes*height];
    memset( buf_bmp, 0, width_bytes*height);
    ::GetBitmapBits((HBITMAP)(bmp.GetHBITMAP()), width_bytes*height, buf_bmp );
    FILE *f;
    f = fopen(filename,"wt");
    if( f )
    {
        byte r,g,b, *p;
        int row,col;
        for( row=0; row<height; row++ )
        {
            p = buf_bmp + row*width_bytes;
            for( col=0; col<width; col++ )
            {
                byte *q = p;
                b = *p++;
                g = *p++;
                r = *p++;
                p = q+density;
                bool found = false;
                for( int i=0; !found && i<nbr_of; i++ )
                {
                    if( reds[i]==r && greens[i]==g &&  blues[i]==b )
                        found = true;
                }
                if( !found )
                {
                    reds  [nbr_of] = r;
                    greens[nbr_of] = g;
                    blues [nbr_of] = b;
                    if( nbr_of+1 < nbrof(codes)-1 )
                        nbr_of++;
                }
            }
        }
    /*  for( int i=0; i<nbr_of; i++ )
        {
            double d1 = ((double)reds[i]);
            double d2 = ((double)greens[i]);
            double d3 = ((double)blues[i]);
            double d = d1*d1 + d2*d2 + d3*d3;
            double max_so_far = 0.0;
            int max_so_far_idx = 0;
            for( int j=i+1; j<nbr_of; j++ )
            {
                double e1 = ((double)reds[j]);
                double e2 = ((double)greens[j]);
                double e3 = ((double)blues[j]);
                double e = e1*e1 + e2*e2 + e3*e3;
                if( e > max_so_far )
                {
                    max_so_far = e;
                    max_so_far_idx = j;
                }
            }
            if( max_so_far > d )
            {
                int j = max_so_far_idx;
                int temp  = reds[i];
                reds[i]   = reds[j];
                reds[j]   = temp;
                temp      = greens[i];
                greens[i] = greens[j];
                greens[j] = temp;
                temp      = blues[i];
                blues[i]  = blues[j];
                blues[j]  = temp;
            }
            static char specials[] = " .-,`~{}=+&$*@#0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            if( reds[i]==0xff && blues[i]==0 && greens[i]==0 )
                codes[i] = 'R';
            else if( reds[i]==0 && blues[i]==0 && greens[i]==0xff )
                codes[i] = 'G';
            else if( reds[i]==0 && blues[i]==0xff && greens[i]==0 )
                codes[i] = 'B';
            else if( i >= strlen(specials) )
                codes[i] = 'a' + (i-strlen(specials));
            else
                codes[i] = specials[i];
        } */
        fprintf( f, "static char *%s_xpm[] = {\n", name );
        fprintf( f, "\"%d %d %d 1\",\n", width, height, nbr_of );
        for( int i=0; i<nbr_of; i++ )
        {
            fprintf( f, "\"%c c #%02x%02x%02x\",\n", codes[i], reds[i], greens[i], blues[i] );
        }
        for( row=0; row<height; row++ )
        {
            p = buf_bmp + row*width_bytes;
            for( col=0; col<width; col++ )
            {
                byte *q = p;
                b = *p++;
                g = *p++;
                r = *p++;
                p = q+density;
                int found_idx = 0;
                bool found = false;
                for( int i=0; i<nbr_of; i++ )
                {
                    if(  r == reds[i] &&
                         g == greens[i] &&
                         b == blues[i]
                      )
                    {
                        found = true;
                        found_idx = i;
                        break;
                    }
                }
                if( !found )
                {
                    double min_so_far = 1000000000.0;
                    for( int i=0; i<nbr_of; i++ )
                    {
                        double d1 = ((double)reds[i]   - (double)r );
                        double d2 = ((double)greens[i] - (double)g );
                        double d3 = ((double)blues[i]  - (double)b );
                        double d = d1*d1 + d2*d2 + d3*d3;
                        if( d < min_so_far )
                        {
                            min_so_far = d;
                            found_idx = i;
                            found = true;
                        }
                    }
                }
                if( col == 0 )
                    fprintf( f, "\"" );
                if( found )
                    fprintf( f, "%c", codes[found_idx] );
                else
                    fprintf( f, "[ #%02x%02x%02x ]", r, g, b );
            }
            if( row+1 < height )
                fprintf( f, "\",\n" );
            else
                fprintf( f, "\"\n" );
        }
        fprintf( f, "};\n" );
        fclose(f);
    }
    #if 0
    f = fopen("masks_black.txt","wt");
    if( f )
    {
        byte r,g,b, *p;
        int row,col;
        for( row=0; row<height; row++ )
        {
            p = buf_bmp + row*width_bytes;
            for( col=0; col<width; col++ )
            {
                b = *p++;
                g = *p++;
                r = *p++;
                if( col == 0 )
                    fprintf( f, "\"" );
                fprintf( f, "%c", (r==0xff && g==0 && b==0) ? '0' : '1' );
            }
            if( row+1 < height )
                fprintf( f, "\",\n" );
            else
                fprintf( f, "\"\n" );
        }
        fclose(f);
    }
    f = fopen("masks_white.txt","wt");
    if( f )
    {
        byte r,g,b, *p;
        int row,col;
        for( row=0; row<height; row++ )
        {
            p = buf_bmp + row*width_bytes;
            for( col=0; col<width; col++ )
            {
                b = *p++;
                g = *p++;
                r = *p++;
                if( col == 0 )
                    fprintf( f, "\"" );
                fprintf( f, "%c", (r==0 && g==0 && b==0xff) ? '0' : '1' );
            }
            if( row+1 < height )
                fprintf( f, "\",\n" );
            else
                fprintf( f, "\"\n" );
        }
        fclose(f);
    }
    #endif
    delete(buf_bmp);
}
Example #3
0
void wxMemoryDCImpl::DoSelect(
  const wxBitmap&                   rBitmap
)
{
    //
    // Select old bitmap out of the device context
    //
    if (m_hOldBitmap)
    {
        ::GpiSetBitmap(m_hPS, NULLHANDLE);
        if (m_vSelectedBitmap.IsOk())
        {
            m_vSelectedBitmap.SetSelectedInto(NULL);
            m_vSelectedBitmap = wxNullBitmap;
        }
    }

    //
    // Check for whether the bitmap is already selected into a device context
    //
    wxCHECK_RET( !rBitmap.GetSelectedInto() ||
                 (rBitmap.GetSelectedInto() == GetOwner()),
                 wxT("Bitmap is selected in another wxMemoryDC, delete the first wxMemoryDC or use SelectObject(NULL)") );

    WXHBITMAP                       hBmp = rBitmap.GetHBITMAP();

    if (!hBmp)
    {
        //
        // Bmps drawn to are upside down, so flip it before committing
        //
        POINTL                      vPoint[4] = { {0, m_vSelectedBitmap.GetHeight()}
                                                 ,{m_vSelectedBitmap.GetWidth(), 0}
                                                 ,{0, 0}
                                                 ,{m_vSelectedBitmap.GetWidth(), m_vSelectedBitmap.GetHeight()}
                                                };


        ::GpiBitBlt( m_hPS
                    ,m_hPS
                    ,4
                    ,vPoint
                    ,ROP_SRCCOPY
                    ,BBO_IGNORE
                   );
        m_vSelectedBitmap.SetSelectedInto(NULL);
    }

    m_vSelectedBitmap = rBitmap;


    if (!hBmp)
    {

        m_hOldBitmap = (WXHBITMAP)::GpiSetBitmap(m_hPS, NULLHANDLE);
        return;
    }
    m_vSelectedBitmap.SetSelectedInto(GetOwner());
    m_hOldBitmap = (WXHBITMAP)::GpiSetBitmap(m_hPS, (HBITMAP)hBmp);

    if (m_hOldBitmap == HBM_ERROR)
    {
        wxLogLastError(wxT("SelectObject(memDC, bitmap)"));
        wxFAIL_MSG(wxT("Couldn't select a bitmap into wxMemoryDC"));
    }
} // end of wxMemoryDC::SelectObject
Example #4
0
void wxIcon::CopyFromBitmap(
  const wxBitmap&                   rBmp
)
{
    wxMask*                         pMask = rBmp.GetMask();
    HBITMAP                         hBmp = NULLHANDLE;
    HBITMAP                         hBmpMask = NULLHANDLE;
    HBITMAP                         hOldBitmap = NULLHANDLE;
    ERRORID                         vError;
    wxString                        sError;
    LONG                            lHits;

    if (!pMask)
    {
        //
        // We must have a mask for an icon, so even if it's probably incorrect,
        // do create it (grey is the "standard" transparent colour)
        //
        pMask = new wxMask( rBmp
                           ,*wxLIGHT_GREY
                          );
    }

    BITMAPINFOHEADER2               vHeader;
    SIZEL                           vSize = {0, 0};
    DEVOPENSTRUC                    vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
    HDC                             hDCSrc = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
    HDC                             hDCDst = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
    HPS                             hPSSrc = ::GpiCreatePS(vHabmain, hDCSrc, &vSize, PU_PELS | GPIA_ASSOC);
    HPS                             hPSDst = ::GpiCreatePS(vHabmain, hDCDst, &vSize, PU_PELS | GPIA_ASSOC);
    POINTL                          vPoint[4] = { {0, 0}, {rBmp.GetWidth(), rBmp.GetHeight()},
                                                  {0, 0}, {rBmp.GetWidth(), rBmp.GetHeight()}
                                                };
    POINTL                          vPointMask[4] = { {0, 0}, {rBmp.GetWidth(), rBmp.GetHeight() * 2},
                                                      {0, 0}, {rBmp.GetWidth(), rBmp.GetHeight()}
                                                    };

    POINTERINFO                     vIconInfo;

    memset(&vIconInfo, '\0', sizeof(POINTERINFO));
    vIconInfo.fPointer = FALSE;  // we want an icon, not a pointer

    memset(&vHeader, '\0', 16);
    vHeader.cbFix           = 16;
    vHeader.cx              = (ULONG)rBmp.GetWidth();
    vHeader.cy              = (ULONG)rBmp.GetHeight();
    vHeader.cPlanes         = 1L;
    vHeader.cBitCount       = 24;

    hBmp = ::GpiCreateBitmap( hPSDst
                             ,&vHeader
                             ,0L
                             ,NULL
                             ,NULL
                            );

    if ((hOldBitmap = ::GpiSetBitmap(hPSDst, hBmp)) == HBM_ERROR)
    {
        vError = ::WinGetLastError(vHabmain);
        sError = wxPMErrorToStr(vError);
    }
    if ((hOldBitmap = ::GpiSetBitmap(hPSSrc, (HBITMAP)rBmp.GetHBITMAP())) == HBM_ERROR)
    {
        vError = ::WinGetLastError(vHabmain);
        sError = wxPMErrorToStr(vError);
    }
    if ((lHits = ::GpiBitBlt( hPSDst
                             ,hPSSrc
                             ,4L
                             ,vPoint
                             ,ROP_SRCCOPY
                             ,BBO_IGNORE
                            )) == GPI_ERROR)
    {
        vError = ::WinGetLastError(vHabmain);
        sError = wxPMErrorToStr(vError);
    }
    if ((hOldBitmap = ::GpiSetBitmap(hPSDst, NULLHANDLE)) == HBM_ERROR)
    {
        vError = ::WinGetLastError(vHabmain);
        sError = wxPMErrorToStr(vError);
    }
    if ((hOldBitmap = ::GpiSetBitmap(hPSSrc, NULLHANDLE)) == HBM_ERROR)
    {
        vError = ::WinGetLastError(vHabmain);
        sError = wxPMErrorToStr(vError);
    }
    vIconInfo.hbmColor = hBmp;

    vHeader.cy              = (ULONG)rBmp.GetHeight() * 2;
    hBmpMask = ::GpiCreateBitmap( hPSDst
                                 ,&vHeader
                                 ,0L
                                 ,NULL
                                 ,NULL
                                );

    if ((hOldBitmap = ::GpiSetBitmap(hPSDst, hBmpMask)) == HBM_ERROR)
    {
        vError = ::WinGetLastError(vHabmain);
        sError = wxPMErrorToStr(vError);
    }
    if ((hOldBitmap = ::GpiSetBitmap(hPSSrc, (HBITMAP)pMask->GetMaskBitmap())) == HBM_ERROR)
    {
        vError = ::WinGetLastError(vHabmain);
        sError = wxPMErrorToStr(vError);
    }
    if ((lHits = ::GpiBitBlt( hPSDst
                             ,hPSSrc
                             ,4L
                             ,vPointMask
                             ,ROP_SRCCOPY
                             ,BBO_IGNORE
                            )) == GPI_ERROR)
    {
        vError = ::WinGetLastError(vHabmain);
        sError = wxPMErrorToStr(vError);
    }
    if ((hOldBitmap = ::GpiSetBitmap(hPSSrc, NULLHANDLE)) == HBM_ERROR)
    {
        vError = ::WinGetLastError(vHabmain);
        sError = wxPMErrorToStr(vError);
    }
    if ((hOldBitmap = ::GpiSetBitmap(hPSDst, NULLHANDLE)) == HBM_ERROR)
    {
        vError = ::WinGetLastError(vHabmain);
        sError = wxPMErrorToStr(vError);
    }

    vIconInfo.hbmPointer = hBmpMask;

    HICON                           hIcon = ::WinCreatePointerIndirect( HWND_DESKTOP
                                                                       ,&vIconInfo
                                                                      );

    if (!hIcon)
    {
        wxLogLastError(wxT("WinCreatePointerIndirect"));
        vError = ::WinGetLastError(vHabmain);
        sError = wxPMErrorToStr(vError);
    }
    else
    {
        SetHICON((WXHICON)hIcon);
        SetSize( rBmp.GetWidth()
                ,rBmp.GetHeight()
               );
    }

    if (!rBmp.GetMask())
    {
        //
        // We created the mask, now delete it
        //
        delete pMask;
    }
    ::GpiSetBitmap(hPSSrc, NULL);
    ::GpiSetBitmap(hPSDst, NULL);
    ::GpiDestroyPS(hPSSrc);
    ::GpiDestroyPS(hPSDst);
    ::DevCloseDC(hDCSrc);
    ::DevCloseDC(hDCDst);
} // end of wxIcon::CopyFromBitmap