| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: msw/gdiimage.cpp |
| 3 | // Purpose: wxGDIImage implementation |
| 4 | // Author: Vadim Zeitlin |
| 5 | // Modified by: |
| 6 | // Created: 20.11.99 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> |
| 9 | // Licence: wxWindows license |
| 10 | /////////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | |
| 20 | #ifdef __GNUG__ |
| 21 | #pragma implementation "gdiimage.h" |
| 22 | #endif |
| 23 | |
| 24 | // For compilers that support precompilation, includes "wx.h". |
| 25 | #include "wx/wxprec.h" |
| 26 | |
| 27 | |
| 28 | #ifndef WX_PRECOMP |
| 29 | #include "wx/string.h" |
| 30 | #endif // WX_PRECOMP |
| 31 | |
| 32 | #include "wx/os2/private.h" |
| 33 | #include "wx/app.h" |
| 34 | #include "wx/os2/gdiimage.h" |
| 35 | |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | // private classes |
| 38 | // ---------------------------------------------------------------------------- |
| 39 | |
| 40 | // all image handlers are declared/defined in this file because the outside |
| 41 | // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids) |
| 42 | |
| 43 | class WXDLLEXPORT wxBMPFileHandler : public wxBitmapHandler |
| 44 | { |
| 45 | public: |
| 46 | wxBMPFileHandler() : wxBitmapHandler(_T("Windows bitmap file"), _T("bmp"), |
| 47 | wxBITMAP_TYPE_BMP) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | virtual bool LoadFile( wxBitmap* pBitmap |
| 52 | ,const wxString& rName |
| 53 | ,HPS hPs |
| 54 | ,long lFlags |
| 55 | ,int nDesiredWidth |
| 56 | ,int nDesiredHeight |
| 57 | ); |
| 58 | virtual bool SaveFile( wxBitmap* pBitmap |
| 59 | ,const wxString& rName |
| 60 | ,int lType |
| 61 | ,const wxPalette* pPalette = NULL |
| 62 | ); |
| 63 | |
| 64 | private: |
| 65 | DECLARE_DYNAMIC_CLASS(wxBMPFileHandler) |
| 66 | }; |
| 67 | |
| 68 | class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler |
| 69 | { |
| 70 | public: |
| 71 | wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"), |
| 72 | wxEmptyString, |
| 73 | wxBITMAP_TYPE_BMP_RESOURCE) |
| 74 | { |
| 75 | } |
| 76 | |
| 77 | virtual bool LoadFile( wxBitmap* pBitmap |
| 78 | ,const wxString& rName |
| 79 | ,HPS hPs |
| 80 | ,long lFlags |
| 81 | ,int nDesiredWidth |
| 82 | ,int nDesiredHeight |
| 83 | ); |
| 84 | |
| 85 | private: |
| 86 | DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler) |
| 87 | }; |
| 88 | |
| 89 | class WXDLLEXPORT wxIconHandler : public wxGDIImageHandler |
| 90 | { |
| 91 | public: |
| 92 | wxIconHandler( const wxString& rName |
| 93 | ,const wxString& rExt |
| 94 | ,long lType |
| 95 | ) : wxGDIImageHandler( rName |
| 96 | ,rExt |
| 97 | ,lType |
| 98 | ) |
| 99 | { |
| 100 | } |
| 101 | |
| 102 | // creating and saving icons is not supported |
| 103 | virtual bool Create( wxGDIImage* WXUNUSED(pImage) |
| 104 | ,void* WXUNUSED(pData) |
| 105 | ,long WXUNUSED(lFlags) |
| 106 | ,int WXUNUSED(nWidth) |
| 107 | ,int WXUNUSED(nHeight) |
| 108 | ,int WXUNUSED(nDepth) = 1 |
| 109 | ) |
| 110 | { |
| 111 | return(FALSE); |
| 112 | } |
| 113 | |
| 114 | virtual bool Save( wxGDIImage* WXUNUSED(pImage) |
| 115 | ,const wxString& WXUNUSED(rName) |
| 116 | ,int WXUNUSED(nType) |
| 117 | ) |
| 118 | { |
| 119 | return(FALSE); |
| 120 | } |
| 121 | virtual bool Load( wxGDIImage* pImage |
| 122 | ,const wxString& rName |
| 123 | ,HPS hPs |
| 124 | ,long lFlags |
| 125 | ,int nDesiredWidth |
| 126 | ,int nDesiredHeight |
| 127 | ) |
| 128 | { |
| 129 | wxIcon* pIcon = wxDynamicCast(pImage, wxIcon); |
| 130 | wxCHECK_MSG(pIcon, FALSE, _T("wxIconHandler only works with icons")); |
| 131 | |
| 132 | return LoadIcon( pIcon |
| 133 | ,rName |
| 134 | ,hPs |
| 135 | ,lFlags |
| 136 | ,nDesiredWidth |
| 137 | ,nDesiredHeight |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | protected: |
| 142 | virtual bool LoadIcon( wxIcon* pIcon |
| 143 | ,const wxString& rName |
| 144 | ,HPS hPs |
| 145 | ,long lFlags |
| 146 | ,int nDesiredWidth = -1 |
| 147 | ,int nDesiredHeight = -1 |
| 148 | ) = 0; |
| 149 | }; |
| 150 | |
| 151 | class WXDLLEXPORT wxICOFileHandler : public wxIconHandler |
| 152 | { |
| 153 | public: |
| 154 | wxICOFileHandler() : wxIconHandler(_T("ICO icon file"), |
| 155 | _T("ico"), |
| 156 | wxBITMAP_TYPE_ICO) |
| 157 | { |
| 158 | } |
| 159 | |
| 160 | virtual bool LoadIcon( wxIcon * pIcon |
| 161 | ,const wxString& rName |
| 162 | ,HPS hPs |
| 163 | ,long lFlags |
| 164 | ,int nDesiredWidth = -1 |
| 165 | ,int nDesiredHeight = -1 |
| 166 | ); |
| 167 | |
| 168 | private: |
| 169 | DECLARE_DYNAMIC_CLASS(wxICOFileHandler) |
| 170 | }; |
| 171 | |
| 172 | class WXDLLEXPORT wxICOResourceHandler: public wxIconHandler |
| 173 | { |
| 174 | public: |
| 175 | wxICOResourceHandler() : wxIconHandler(_T("ICO resource"), |
| 176 | _T("ico"), |
| 177 | wxBITMAP_TYPE_ICO_RESOURCE) |
| 178 | { |
| 179 | } |
| 180 | |
| 181 | virtual bool LoadIcon( wxIcon* pIcon |
| 182 | ,const wxString& rName |
| 183 | ,HPS hPs |
| 184 | ,long lFlags |
| 185 | ,int nDesiredWidth = -1 |
| 186 | ,int nDesiredHeight = -1 |
| 187 | ); |
| 188 | |
| 189 | private: |
| 190 | DECLARE_DYNAMIC_CLASS(wxICOResourceHandler) |
| 191 | }; |
| 192 | |
| 193 | // ---------------------------------------------------------------------------- |
| 194 | // wxWin macros |
| 195 | // ---------------------------------------------------------------------------- |
| 196 | |
| 197 | #if !USE_SHARED_LIBRARIES |
| 198 | IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler, wxBitmapHandler) |
| 199 | IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler) |
| 200 | IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler, wxObject) |
| 201 | IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler, wxObject) |
| 202 | #endif |
| 203 | |
| 204 | // ---------------------------------------------------------------------------- |
| 205 | // private functions |
| 206 | // ---------------------------------------------------------------------------- |
| 207 | |
| 208 | static wxSize GetHiconSize(WXHICON hicon); |
| 209 | |
| 210 | // ============================================================================ |
| 211 | // implementation |
| 212 | // ============================================================================ |
| 213 | |
| 214 | wxList wxGDIImage::ms_handlers; |
| 215 | |
| 216 | // ---------------------------------------------------------------------------- |
| 217 | // wxGDIImage functions forwarded to wxGDIImageRefData |
| 218 | // ---------------------------------------------------------------------------- |
| 219 | |
| 220 | bool wxGDIImage::FreeResource( |
| 221 | bool WXUNUSED(bForce) |
| 222 | ) |
| 223 | { |
| 224 | if ( !IsNull() ) |
| 225 | { |
| 226 | GetGDIImageData()->Free(); |
| 227 | GetGDIImageData()->m_hHandle = 0; |
| 228 | } |
| 229 | |
| 230 | return(TRUE); |
| 231 | } |
| 232 | |
| 233 | WXHANDLE wxGDIImage::GetResourceHandle() |
| 234 | { |
| 235 | return GetHandle(); |
| 236 | } |
| 237 | |
| 238 | // ---------------------------------------------------------------------------- |
| 239 | // wxGDIImage handler stuff |
| 240 | // ---------------------------------------------------------------------------- |
| 241 | |
| 242 | void wxGDIImage::AddHandler( |
| 243 | wxGDIImageHandler* pHandler |
| 244 | ) |
| 245 | { |
| 246 | ms_handlers.Append(pHandler); |
| 247 | } |
| 248 | |
| 249 | void wxGDIImage::InsertHandler( |
| 250 | wxGDIImageHandler* pHandler |
| 251 | ) |
| 252 | { |
| 253 | ms_handlers.Insert(pHandler); |
| 254 | } |
| 255 | |
| 256 | bool wxGDIImage::RemoveHandler( |
| 257 | const wxString& rName |
| 258 | ) |
| 259 | { |
| 260 | wxGDIImageHandler* pHandler = FindHandler(rName); |
| 261 | |
| 262 | if (pHandler) |
| 263 | { |
| 264 | ms_handlers.DeleteObject(pHandler); |
| 265 | return(TRUE); |
| 266 | } |
| 267 | else |
| 268 | return(FALSE); |
| 269 | } |
| 270 | |
| 271 | wxGDIImageHandler* wxGDIImage::FindHandler( |
| 272 | const wxString& rName |
| 273 | ) |
| 274 | { |
| 275 | wxNode* pNode = ms_handlers.First(); |
| 276 | |
| 277 | while (pNode) |
| 278 | { |
| 279 | wxGDIImageHandler* pHandler = (wxGDIImageHandler *)pNode->Data(); |
| 280 | |
| 281 | if (pHandler->GetName() == rName) |
| 282 | return(pHandler); |
| 283 | pNode = pNode->Next(); |
| 284 | } |
| 285 | return(NULL); |
| 286 | } |
| 287 | |
| 288 | wxGDIImageHandler* wxGDIImage::FindHandler( |
| 289 | const wxString& rExtension |
| 290 | , long lType |
| 291 | ) |
| 292 | { |
| 293 | wxNode* pNode = ms_handlers.First(); |
| 294 | |
| 295 | while (pNode) |
| 296 | { |
| 297 | wxGDIImageHandler* pHandler = (wxGDIImageHandler *)pNode->Data(); |
| 298 | |
| 299 | if ((pHandler->GetExtension() = rExtension) && |
| 300 | (lType == -1 || pHandler->GetType() == lType)) |
| 301 | { |
| 302 | return(pHandler); |
| 303 | } |
| 304 | pNode = pNode->Next(); |
| 305 | } |
| 306 | return(NULL); |
| 307 | } |
| 308 | |
| 309 | wxGDIImageHandler* wxGDIImage::FindHandler( |
| 310 | long lType |
| 311 | ) |
| 312 | { |
| 313 | wxNode* pNode = ms_handlers.First(); |
| 314 | |
| 315 | while (pNode) |
| 316 | { |
| 317 | wxGDIImageHandler* pHandler = (wxGDIImageHandler *)pNode->Data(); |
| 318 | |
| 319 | if (pHandler->GetType() == lType) |
| 320 | return pHandler; |
| 321 | pNode = pNode->Next(); |
| 322 | } |
| 323 | return(NULL); |
| 324 | } |
| 325 | |
| 326 | void wxGDIImage::CleanUpHandlers() |
| 327 | { |
| 328 | wxNode* pNode = ms_handlers.First(); |
| 329 | |
| 330 | while (pNode) |
| 331 | { |
| 332 | wxGDIImageHandler* pHandler = (wxGDIImageHandler *)pNode->Data(); |
| 333 | wxNode* pNext = pNode->Next(); |
| 334 | |
| 335 | delete pHandler; |
| 336 | #if (!(defined(__VISAGECPP__) && (__IBMCPP__ < 400 || __IBMC__ < 400 ))) |
| 337 | delete pNode; |
| 338 | #endif |
| 339 | pNode = pNext; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | void wxGDIImage::InitStandardHandlers() |
| 344 | { |
| 345 | AddHandler(new wxBMPResourceHandler); |
| 346 | AddHandler(new wxBMPFileHandler); |
| 347 | AddHandler(new wxICOResourceHandler); |
| 348 | AddHandler(new wxICOFileHandler); |
| 349 | } |
| 350 | |
| 351 | // ---------------------------------------------------------------------------- |
| 352 | // wxBitmap handlers |
| 353 | // ---------------------------------------------------------------------------- |
| 354 | |
| 355 | bool wxBMPResourceHandler::LoadFile( |
| 356 | wxBitmap* pBitmap |
| 357 | , const wxString& rName |
| 358 | , HPS hPs |
| 359 | , long lFlags |
| 360 | , int nDesiredWidth |
| 361 | , int nDesiredHeight |
| 362 | ) |
| 363 | { |
| 364 | // TODO: load a bitmap from a file |
| 365 | /* |
| 366 | rBitmap->SetHBITMAP((WXHBITMAP)::LoadBitmap(wxGetInstance(), rName)); |
| 367 | |
| 368 | wxBitmapRefData* pData = bitmap->GetBitmapData(); |
| 369 | |
| 370 | if (pBitmap->Ok()) |
| 371 | { |
| 372 | BITMAP bm; |
| 373 | |
| 374 | if ( !::GetObject(GetHbitmapOf(*pBitmap), sizeof(BITMAP), (LPSTR) &bm) ) |
| 375 | { |
| 376 | wxLogLastError("GetObject(HBITMAP)"); |
| 377 | } |
| 378 | |
| 379 | data->m_width = bm.bmWidth; |
| 380 | data->m_height = bm.bmHeight; |
| 381 | data->m_depth = bm.bmBitsPixel; |
| 382 | } |
| 383 | else |
| 384 | { |
| 385 | // it's probably not found |
| 386 | wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."), |
| 387 | name.c_str()); |
| 388 | } |
| 389 | |
| 390 | return bitmap->Ok(); |
| 391 | */ |
| 392 | return(FALSE); |
| 393 | } |
| 394 | |
| 395 | bool wxBMPFileHandler::LoadFile( |
| 396 | wxBitmap* pBitmap |
| 397 | , const wxString& rName |
| 398 | , HPS hPs |
| 399 | , long WXUNUSED(lFlags) |
| 400 | , int WXUNUSED(nDesiredWidth) |
| 401 | , int WXUNUSED(nDesiredHeight) |
| 402 | ) |
| 403 | { |
| 404 | #if wxUSE_IMAGE_LOADING_IN_OS2 |
| 405 | wxPalette* pPalette = NULL; |
| 406 | |
| 407 | bool bSuccess = FALSE; /* wxLoadIntoBitmap( WXSTRINGCAST rName |
| 408 | ,pBitmap |
| 409 | ,&pPalette |
| 410 | ) != 0; */ |
| 411 | if (bSuccess && pPalette) |
| 412 | { |
| 413 | pBitmap->SetPalette(*pPalette); |
| 414 | } |
| 415 | |
| 416 | // it was copied by the bitmap if it was loaded successfully |
| 417 | delete pPalette; |
| 418 | |
| 419 | return(bSuccess); |
| 420 | #else |
| 421 | return(FALSE); |
| 422 | #endif |
| 423 | } |
| 424 | |
| 425 | bool wxBMPFileHandler::SaveFile( |
| 426 | wxBitmap* pBitmap |
| 427 | , const wxString& rName |
| 428 | , int WXUNUSED(nType) |
| 429 | , const wxPalette* pPal |
| 430 | ) |
| 431 | { |
| 432 | #if wxUSE_IMAGE_LOADING_IN_OS2 |
| 433 | wxPalette* pActualPalette = (wxPalette *)pPal; |
| 434 | |
| 435 | if (!pActualPalette) |
| 436 | pActualPalette = pBitmap->GetPalette(); |
| 437 | /* return(wxSaveBitmap( WXSTRINGCAST rName |
| 438 | ,pBitmap |
| 439 | ,pActualPalette |
| 440 | ) != 0); */ |
| 441 | return(FALSE); |
| 442 | #else |
| 443 | return(FALSE); |
| 444 | #endif |
| 445 | } |
| 446 | |
| 447 | // ---------------------------------------------------------------------------- |
| 448 | // wxIcon handlers |
| 449 | // ---------------------------------------------------------------------------- |
| 450 | |
| 451 | bool wxICOFileHandler::LoadIcon( |
| 452 | wxIcon* pIcon |
| 453 | , const wxString& rName |
| 454 | , HPS hPs |
| 455 | , long lFlags |
| 456 | , int nDesiredWidth |
| 457 | , int nDesiredHeight |
| 458 | ) |
| 459 | { |
| 460 | #if wxUSE_RESOURCE_LOADING_IN_OS2 |
| 461 | pIcon->UnRef(); |
| 462 | |
| 463 | // actual size |
| 464 | wxSize vSize; |
| 465 | |
| 466 | // TODO: load icon directly from a file |
| 467 | /* |
| 468 | #ifdef __WIN32__ |
| 469 | HICON hicon = ::ExtractIcon(wxGetInstance(), name, first); |
| 470 | if ( !hicon ) |
| 471 | { |
| 472 | wxLogSysError(_T("Failed to load icon from the file '%s'"), |
| 473 | name.c_str()); |
| 474 | |
| 475 | return FALSE; |
| 476 | } |
| 477 | |
| 478 | size = GetHiconSize(hicon); |
| 479 | #else // Win16 |
| 480 | HICON hicon = ReadIconFile((wxChar *)name.c_str(), |
| 481 | wxGetInstance(), |
| 482 | &size.x, &size.y); |
| 483 | #endif // Win32/Win16 |
| 484 | |
| 485 | if ( (desiredWidth != -1 && desiredWidth != size.x) || |
| 486 | (desiredHeight != -1 && desiredHeight != size.y) ) |
| 487 | { |
| 488 | wxLogDebug(_T("Returning FALSE from wxICOFileHandler::Load because " |
| 489 | "of the size mismatch: actual (%d, %d), " |
| 490 | "requested (%d, %d)"), |
| 491 | size.x, size.y, |
| 492 | desiredWidth, desiredHeight); |
| 493 | |
| 494 | ::DestroyIcon(hicon); |
| 495 | |
| 496 | return FALSE; |
| 497 | } |
| 498 | |
| 499 | icon->SetHICON((WXHICON)hicon); |
| 500 | icon->SetSize(size.x, size.y); |
| 501 | |
| 502 | return icon->Ok(); |
| 503 | */ |
| 504 | return(FALSE); |
| 505 | #else |
| 506 | return(FALSE); |
| 507 | #endif |
| 508 | } |
| 509 | |
| 510 | bool wxICOResourceHandler::LoadIcon( |
| 511 | wxIcon* pIcon |
| 512 | , const wxString& rName |
| 513 | , HPS hPs |
| 514 | , long lFlags |
| 515 | , int WXUNUSED(nDesiredWidth) |
| 516 | , int WXUNUSED(nDesiredHeight) |
| 517 | ) |
| 518 | { |
| 519 | HPOINTER hIcon; |
| 520 | |
| 521 | hIcon = ::WinLoadFileIcon( (PSZ)rName.c_str() |
| 522 | ,TRUE // load for private use |
| 523 | ); |
| 524 | |
| 525 | pIcon->SetSize(32, 32); // all OS/2 icons are 32 x 32 |
| 526 | |
| 527 | |
| 528 | pIcon->SetHICON((WXHICON)hIcon); |
| 529 | |
| 530 | return pIcon->Ok(); |
| 531 | } // end of wxICOResourceHandler::LoadIcon |
| 532 | |
| 533 | // ---------------------------------------------------------------------------- |
| 534 | // private functions |
| 535 | // ---------------------------------------------------------------------------- |
| 536 | |
| 537 | static wxSize GetHiconSize( |
| 538 | WXHICON hicon |
| 539 | ) |
| 540 | { |
| 541 | wxSize vSize(32, 32); // default |
| 542 | |
| 543 | // all OS/2 icons are 32x32 |
| 544 | return(vSize); |
| 545 | } |
| 546 | |