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