1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "bitmap.h"
18 #include "wx/palette.h"
19 #include "wx/bitmap.h"
22 #include "wx/control.h"
23 #include "wx/dcmemory.h"
27 #pragma message disable nosimpint
31 #pragma message enable nosimpint
34 #include "wx/motif/private.h"
40 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
41 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
43 wxBitmapRefData::wxBitmapRefData()
53 m_pixmap
= (WXPixmap
) 0;
54 m_display
= (WXDisplay
*) 0;
56 m_freePixmap
= TRUE
; //TODO: necessary?
57 m_freeColors
= (unsigned long*) 0;
58 m_freeColorsCount
= 0;
60 // These 5 variables are for wxControl
61 m_insensPixmap
= (WXPixmap
) 0;
62 m_labelPixmap
= (WXPixmap
) 0;
63 m_armPixmap
= (WXPixmap
) 0;
64 m_image
= (WXImage
*) 0;
65 m_insensImage
= (WXImage
*) 0;
68 wxBitmapRefData::~wxBitmapRefData()
71 XmDestroyPixmap (DefaultScreenOfDisplay ((Display
*) m_display
), (Pixmap
) m_labelPixmap
);
74 XmDestroyPixmap (DefaultScreenOfDisplay ((Display
*) m_display
), (Pixmap
) m_armPixmap
);
77 XmDestroyPixmap (DefaultScreenOfDisplay ((Display
*) m_display
), (Pixmap
) m_insensPixmap
);
81 XmUninstallImage ((XImage
*) m_image
);
82 XtFree ((char *) (XImage
*) m_image
);
87 XmUninstallImage ((XImage
*) m_insensImage
);
88 delete[] ((XImage
*) m_insensImage
)->data
;
89 XtFree ((char *) (XImage
*) m_insensImage
);
91 if (m_pixmap
&& m_freePixmap
)
92 XFreePixmap ((Display
*) m_display
, (Pixmap
) m_pixmap
);
96 int screen
= DefaultScreen((Display
*) m_display
);
97 Colormap cmp
= DefaultColormap((Display
*) m_display
,screen
);
99 for(llp
= 0;llp
< m_freeColorsCount
;llp
++)
100 XFreeColors((Display
*) m_display
, cmp
, &m_freeColors
[llp
], 1, 0L);
109 wxList
wxBitmap::sm_handlers
;
111 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
117 if ( wxTheBitmapList
)
118 wxTheBitmapList
->AddBitmap(this);
121 wxBitmap::~wxBitmap()
124 wxTheBitmapList
->DeleteObject(this);
127 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
129 m_refData
= new wxBitmapRefData
;
131 (void) Create((void*) bits
, wxBITMAP_TYPE_XBM_DATA
, width
, height
, depth
);
133 if ( wxTheBitmapList
)
134 wxTheBitmapList
->AddBitmap(this);
137 wxBitmap::wxBitmap(int w
, int h
, int d
)
139 (void)Create(w
, h
, d
);
141 if ( wxTheBitmapList
)
142 wxTheBitmapList
->AddBitmap(this);
145 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
147 (void) Create(data
, type
, width
, height
, depth
);
149 if ( wxTheBitmapList
)
150 wxTheBitmapList
->AddBitmap(this);
153 wxBitmap::wxBitmap(const wxString
& filename
, long type
)
155 LoadFile(filename
, (int)type
);
157 if ( wxTheBitmapList
)
158 wxTheBitmapList
->AddBitmap(this);
161 // Create from XPM data
162 static wxControl
* sg_Control
= NULL
;
163 wxBitmap::wxBitmap(char **data
, wxControl
* control
)
165 // Pass the control to the Create function using a global
166 sg_Control
= control
;
168 (void) Create((void *)data
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
170 sg_Control
= (wxControl
*) NULL
;
173 bool wxBitmap::CreateFromXpm(const char **bits
)
175 wxCHECK_MSG( bits
, FALSE
, _T("NULL pointer in wxBitmap::CreateFromXpm") );
177 return Create(bits
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
180 bool wxBitmap::Create(int w
, int h
, int d
)
184 m_refData
= new wxBitmapRefData
;
187 d
= wxDisplayDepth();
189 M_BITMAPDATA
->m_width
= w
;
190 M_BITMAPDATA
->m_height
= h
;
191 M_BITMAPDATA
->m_depth
= d
;
192 M_BITMAPDATA
->m_freePixmap
= TRUE
;
194 Display
*dpy
= (Display
*) wxGetDisplay();
196 M_BITMAPDATA
->m_display
= dpy
; /* MATTHEW: [4] Remember the display */
198 M_BITMAPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
201 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_pixmap
!= (WXPixmap
) 0) ;
202 return M_BITMAPDATA
->m_ok
;
205 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
209 m_refData
= new wxBitmapRefData
;
211 wxBitmapHandler
*handler
= FindHandler(type
);
213 if ( handler
== NULL
) {
215 if (!image
.LoadFile( filename
, type
)) return FALSE
;
218 *this = image
.ConvertToBitmap();
224 return handler
->LoadFile(this, filename
, type
, -1, -1);
227 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
231 m_refData
= new wxBitmapRefData
;
233 wxBitmapHandler
*handler
= FindHandler(type
);
235 if ( handler
== NULL
) {
236 wxLogWarning("no data bitmap handler for type %d defined.", type
);
241 return handler
->Create(this, data
, type
, width
, height
, depth
);
244 bool wxBitmap::SaveFile(const wxString
& filename
, int type
, const wxPalette
*palette
)
246 wxBitmapHandler
*handler
= FindHandler(type
);
248 if ( handler
== NULL
) { // try wxImage
249 wxImage
image( *this );
250 if (image
.Ok()) return image
.SaveFile( filename
, type
);
254 return handler
->SaveFile(this, filename
, type
, palette
);
257 void wxBitmap::SetWidth(int w
)
260 m_refData
= new wxBitmapRefData
;
262 M_BITMAPDATA
->m_width
= w
;
265 void wxBitmap::SetHeight(int h
)
268 m_refData
= new wxBitmapRefData
;
270 M_BITMAPDATA
->m_height
= h
;
273 void wxBitmap::SetDepth(int d
)
276 m_refData
= new wxBitmapRefData
;
278 M_BITMAPDATA
->m_depth
= d
;
281 void wxBitmap::SetQuality(int q
)
284 m_refData
= new wxBitmapRefData
;
286 M_BITMAPDATA
->m_quality
= q
;
289 void wxBitmap::SetOk(bool isOk
)
292 m_refData
= new wxBitmapRefData
;
294 M_BITMAPDATA
->m_ok
= isOk
;
297 void wxBitmap::SetPalette(const wxPalette
& palette
)
300 m_refData
= new wxBitmapRefData
;
302 M_BITMAPDATA
->m_bitmapPalette
= palette
;
305 void wxBitmap::SetMask(wxMask
*mask
)
308 m_refData
= new wxBitmapRefData
;
310 M_BITMAPDATA
->m_bitmapMask
= mask
;
313 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
316 (rect
.x
>= 0) && (rect
.y
>= 0) &&
317 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
318 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
320 wxBitmap
ret( rect
.width
, rect
.height
, 0 );
321 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
323 // The remaining still TODO
327 void wxBitmap::AddHandler(wxBitmapHandler
*handler
)
329 sm_handlers
.Append(handler
);
332 void wxBitmap::InsertHandler(wxBitmapHandler
*handler
)
334 sm_handlers
.Insert(handler
);
337 bool wxBitmap::RemoveHandler(const wxString
& name
)
339 wxBitmapHandler
*handler
= FindHandler(name
);
342 sm_handlers
.DeleteObject(handler
);
349 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& name
)
351 wxNode
*node
= sm_handlers
.First();
354 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
355 if ( handler
->GetName() == name
)
362 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& extension
, long bitmapType
)
364 wxNode
*node
= sm_handlers
.First();
367 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
368 if ( handler
->GetExtension() == extension
&&
369 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
376 wxBitmapHandler
*wxBitmap::FindHandler(long bitmapType
)
378 wxNode
*node
= sm_handlers
.First();
381 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
382 if (handler
->GetType() == bitmapType
)
395 m_pixmap
= (WXPixmap
) 0;
398 // Construct a mask from a bitmap and a colour indicating
399 // the transparent area
400 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
402 m_pixmap
= (WXPixmap
) 0;
404 Create(bitmap
, colour
);
407 // Construct a mask from a bitmap and a palette index indicating
408 // the transparent area
409 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
411 m_pixmap
= (WXPixmap
) 0;
413 Create(bitmap
, paletteIndex
);
416 // Construct a mask from a mono bitmap (copies the bitmap).
417 wxMask::wxMask(const wxBitmap
& bitmap
)
419 m_pixmap
= (WXPixmap
) 0;
426 // TODO: this may be the wrong display
428 XFreePixmap ((Display
*) wxGetDisplay(), (Pixmap
) m_pixmap
);
431 // Create a mask from a mono bitmap (copies the bitmap).
432 bool wxMask::Create(const wxBitmap
& WXUNUSED(bitmap
))
438 // Create a mask from a bitmap and a palette index indicating
439 // the transparent area
440 bool wxMask::Create(const wxBitmap
& WXUNUSED(bitmap
), int WXUNUSED(paletteIndex
))
446 // Create a mask from a bitmap and a colour indicating
447 // the transparent area
448 bool wxMask::Create(const wxBitmap
& WXUNUSED(bitmap
), const wxColour
& WXUNUSED(colour
))
458 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
460 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
), void *WXUNUSED(data
), long WXUNUSED(type
),
461 int WXUNUSED(width
), int WXUNUSED(height
), int WXUNUSED(depth
))
466 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
), const wxString
& WXUNUSED(name
), long WXUNUSED(type
),
467 int WXUNUSED(desiredWidth
), int WXUNUSED(desiredHeight
))
472 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
), const wxString
& WXUNUSED(name
), int WXUNUSED(type
),
473 const wxPalette
*WXUNUSED(palette
))
482 class WXDLLEXPORT wxXBMFileHandler
: public wxBitmapHandler
484 DECLARE_DYNAMIC_CLASS(wxXBMFileHandler
)
486 inline wxXBMFileHandler()
490 m_type
= wxBITMAP_TYPE_XBM
;
493 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
494 int desiredWidth
, int desiredHeight
);
496 IMPLEMENT_DYNAMIC_CLASS(wxXBMFileHandler
, wxBitmapHandler
)
498 bool wxXBMFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long WXUNUSED(flags
),
499 int WXUNUSED(desiredWidth
), int WXUNUSED(desiredHeight
))
501 M_BITMAPHANDLERDATA
->m_freePixmap
= TRUE
;
507 Display
*dpy
= (Display
*) wxGetDisplay();
508 M_BITMAPDATA
->m_display
= (WXDisplay
*) dpy
;
510 int value
= XReadBitmapFile (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
511 (char*) (const char*) name
, &w
, &h
, &pixmap
, &hotX
, &hotY
);
512 M_BITMAPHANDLERDATA
->m_width
= w
;
513 M_BITMAPHANDLERDATA
->m_height
= h
;
514 M_BITMAPHANDLERDATA
->m_depth
= 1;
515 M_BITMAPHANDLERDATA
->m_pixmap
= (WXPixmap
) pixmap
;
517 if ((value
== BitmapFileInvalid
) ||
518 (value
== BitmapOpenFailed
) ||
519 (value
== BitmapNoMemory
))
521 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
522 M_BITMAPHANDLERDATA
->m_pixmap
= (WXPixmap
) 0;
525 M_BITMAPHANDLERDATA
->m_ok
= TRUE
;
527 return M_BITMAPHANDLERDATA
->m_ok
;
530 class WXDLLEXPORT wxXBMDataHandler
: public wxBitmapHandler
532 DECLARE_DYNAMIC_CLASS(wxXBMDataHandler
)
534 inline wxXBMDataHandler()
538 m_type
= wxBITMAP_TYPE_XBM_DATA
;
541 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
, int width
, int height
, int depth
= 1);
543 IMPLEMENT_DYNAMIC_CLASS(wxXBMDataHandler
, wxBitmapHandler
)
545 bool wxXBMDataHandler::Create( wxBitmap
*bitmap
, void *data
, long WXUNUSED(flags
),
546 int width
, int height
, int WXUNUSED(depth
))
548 M_BITMAPHANDLERDATA
->m_width
= width
;
549 M_BITMAPHANDLERDATA
->m_height
= height
;
550 M_BITMAPHANDLERDATA
->m_depth
= 1;
551 M_BITMAPHANDLERDATA
->m_freePixmap
= TRUE
;
553 Display
*dpy
= (Display
*) wxGetDisplay();
554 M_BITMAPHANDLERDATA
->m_display
= (WXDisplay
*) dpy
;
556 M_BITMAPHANDLERDATA
->m_pixmap
= (WXPixmap
) XCreateBitmapFromData (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)), (char*) data
, width
, height
);
557 M_BITMAPHANDLERDATA
->m_ok
= (M_BITMAPHANDLERDATA
->m_pixmap
!= (WXPixmap
) 0) ;
559 // code for wxControl. TODO: can we avoid doing this until we need it?
560 // E.g. have CreateButtonPixmaps which is called on demand.
561 XImage
* image
= (XImage
*) XtMalloc (sizeof (XImage
));
562 image
->width
= width
;
563 image
->height
= height
;
564 image
->data
= (char*) data
;
567 image
->format
= XYBitmap
;
568 image
->byte_order
= LSBFirst
;
569 image
->bitmap_unit
= 8;
570 image
->bitmap_bit_order
= LSBFirst
;
571 image
->bitmap_pad
= 8;
572 image
->bytes_per_line
= (width
+ 7) >> 3;
575 sprintf (tmp
, "Im%x", (unsigned int) image
);
576 XmInstallImage (image
, tmp
);
578 // Build our manually stipped pixmap.
580 int bpl
= (width
+ 7) / 8;
581 char *data1
= new char[height
* bpl
];
582 char* bits
= (char*) data
;
584 for (i
= 0; i
< height
; i
++)
586 int mask
= i
% 2 ? 0x55 : 0xaa;
588 for (j
= 0; j
< bpl
; j
++)
589 data1
[i
* bpl
+ j
] = bits
[i
* bpl
+ j
] & mask
;
591 XImage
* insensImage
= (XImage
*) XtMalloc (sizeof (XImage
));
592 insensImage
->width
= width
;
593 insensImage
->height
= height
;
594 insensImage
->data
= data1
;
595 insensImage
->depth
= 1;
596 insensImage
->xoffset
= 0;
597 insensImage
->format
= XYBitmap
;
598 insensImage
->byte_order
= LSBFirst
;
599 insensImage
->bitmap_unit
= 8;
600 insensImage
->bitmap_bit_order
= LSBFirst
;
601 insensImage
->bitmap_pad
= 8;
602 insensImage
->bytes_per_line
= bpl
;
604 sprintf (tmp
, "Not%x", (unsigned int)insensImage
);
605 XmInstallImage (insensImage
, tmp
);
607 M_BITMAPHANDLERDATA
->m_image
= (WXImage
*) image
;
608 M_BITMAPHANDLERDATA
->m_insensImage
= (WXImage
*) insensImage
;
614 class WXDLLEXPORT wxXPMFileHandler
: public wxBitmapHandler
616 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler
)
618 inline wxXPMFileHandler()
622 m_type
= wxBITMAP_TYPE_XPM
;
625 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
626 int desiredWidth
, int desiredHeight
);
627 virtual bool SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
= NULL
);
630 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
)
632 bool wxXPMFileHandler::LoadFile( wxBitmap
*bitmap
, const wxString
& name
, long WXUNUSED(flags
),
633 int WXUNUSED(desiredWidth
), int WXUNUSED(desiredHeight
) )
635 Display
*dpy
= (Display
*) wxGetDisplay();
636 M_BITMAPHANDLERDATA
->m_display
= (WXDisplay
*) dpy
;
638 XpmAttributes xpmAttr
;
642 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
643 xpmAttr
.valuemask
= XpmReturnInfos
| XpmCloseness
;
644 xpmAttr
.closeness
= 40000;
645 int errorStatus
= XpmReadFileToPixmap(dpy
,
646 RootWindow(dpy
, DefaultScreen(dpy
)), (char*) (const char*) name
,
647 &pixmap
, &mask
, &xpmAttr
);
649 if (errorStatus
== XpmSuccess
)
651 M_BITMAPHANDLERDATA
->m_pixmap
= (WXPixmap
) pixmap
;
654 M_BITMAPHANDLERDATA
->m_bitmapMask
= new wxMask
;
655 M_BITMAPHANDLERDATA
->m_bitmapMask
->SetPixmap((WXPixmap
) mask
);
658 unsigned int depthRet
;
660 unsigned int widthRet
, heightRet
, borderWidthRet
;
661 Window rootWindowRet
;
662 XGetGeometry(dpy
, pixmap
, &rootWindowRet
, &xRet
, &yRet
,
663 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
665 M_BITMAPHANDLERDATA
->m_width
= xpmAttr
.width
;
666 M_BITMAPHANDLERDATA
->m_height
= xpmAttr
.height
;
669 if ( xpmAttr.npixels > 2 )
671 M_BITMAPHANDLERDATA->m_depth = 8; // TODO: next time not just a guess :-) ...
674 M_BITMAPHANDLERDATA->m_depth = 1; // mono
678 M_BITMAPHANDLERDATA
->m_depth
= depthRet
;
680 M_BITMAPHANDLERDATA
->m_numColors
= xpmAttr
.npixels
;
682 XpmFreeAttributes(&xpmAttr
);
684 M_BITMAPHANDLERDATA
->m_ok
= TRUE
;
688 // XpmDebugError(errorStatus, name);
689 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
694 bool wxXPMFileHandler::SaveFile( wxBitmap
*bitmap
, const wxString
& name
, int WXUNUSED(type
),
695 const wxPalette
*WXUNUSED(palette
))
697 if (M_BITMAPHANDLERDATA
->m_ok
&& M_BITMAPHANDLERDATA
->m_pixmap
)
699 Display
*dpy
= (Display
*) M_BITMAPHANDLERDATA
->m_display
;
700 int errorStatus
= XpmWriteFileFromPixmap(dpy
, (char*) (const char*) name
,
701 (Pixmap
) M_BITMAPHANDLERDATA
->m_pixmap
,
702 (M_BITMAPHANDLERDATA
->m_bitmapMask
? (Pixmap
) M_BITMAPHANDLERDATA
->m_bitmapMask
->GetPixmap() : (Pixmap
) 0),
703 (XpmAttributes
*) NULL
);
704 if (errorStatus
== XpmSuccess
)
713 class WXDLLEXPORT wxXPMDataHandler
: public wxBitmapHandler
715 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler
)
717 inline wxXPMDataHandler()
721 m_type
= wxBITMAP_TYPE_XPM_DATA
;
724 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
, int width
, int height
, int depth
= 1);
726 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
728 bool wxXPMDataHandler::Create( wxBitmap
*bitmap
, void *data
, long WXUNUSED(flags
),
729 int width
, int height
, int WXUNUSED(depth
))
731 M_BITMAPHANDLERDATA
->m_width
= width
;
732 M_BITMAPHANDLERDATA
->m_height
= height
;
733 M_BITMAPHANDLERDATA
->m_depth
= 1;
734 M_BITMAPHANDLERDATA
->m_freePixmap
= TRUE
;
736 Display
*dpy
= (Display
*) wxGetDisplay();
737 M_BITMAPHANDLERDATA
->m_display
= (WXDisplay
*) dpy
;
739 XpmAttributes xpmAttr
;
741 xpmAttr
.valuemask
= XpmReturnInfos
; /* nothing yet, but get infos back */
743 XpmColorSymbol symbolicColors
[4];
744 if (sg_Control
&& sg_Control
->GetMainWidget())
746 symbolicColors
[0].name
= "foreground";
747 symbolicColors
[0].value
= NULL
;
748 symbolicColors
[1].name
= "background";
749 symbolicColors
[1].value
= NULL
;
750 XtVaGetValues((Widget
) sg_Control
->GetMainWidget(),
751 XmNforeground
, &symbolicColors
[0].pixel
,
752 XmNbackground
, &symbolicColors
[1].pixel
,NULL
);
753 xpmAttr
.numsymbols
= 2;
754 xpmAttr
.colorsymbols
= symbolicColors
;
755 xpmAttr
.valuemask
|= XpmColorSymbols
; // add flag
760 int ErrorStatus
= XpmCreatePixmapFromData(dpy
, RootWindow(dpy
, DefaultScreen(dpy
)),
761 (char**) data
, &pixmap
, &mask
, &xpmAttr
);
762 if (ErrorStatus
== XpmSuccess
)
765 M_BITMAPHANDLERDATA
->m_width
= xpmAttr
.width
;
766 M_BITMAPHANDLERDATA
->m_height
= xpmAttr
.height
;
768 unsigned int depthRet
;
770 unsigned int widthRet
, heightRet
, borderWidthRet
;
771 Window rootWindowRet
;
772 XGetGeometry(dpy
, pixmap
, &rootWindowRet
, &xRet
, &yRet
,
773 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
776 if ( xpmAttr.npixels > 2 )
778 M_BITMAPHANDLERDATA->m_depth = 8; // next time not just a guess :-) ...
781 M_BITMAPHANDLERDATA->m_depth = 1; // mono
785 M_BITMAPHANDLERDATA
->m_depth
= depthRet
;
787 M_BITMAPHANDLERDATA
->m_numColors
= xpmAttr
.npixels
;
788 XpmFreeAttributes(&xpmAttr
);
789 M_BITMAPHANDLERDATA
->m_ok
= TRUE
;
790 M_BITMAPHANDLERDATA
->m_pixmap
= (WXPixmap
) pixmap
;
793 M_BITMAPHANDLERDATA
->m_bitmapMask
= new wxMask
;
794 M_BITMAPHANDLERDATA
->m_bitmapMask
->SetPixmap((WXPixmap
) mask
);
799 // XpmDebugError(ErrorStatus, NULL);
800 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
802 return M_BITMAPHANDLERDATA
->m_ok
;
805 #endif // wxHAVE_LIB_XPM
807 void wxBitmap::CleanUpHandlers()
809 wxNode
*node
= sm_handlers
.First();
812 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
813 wxNode
*next
= node
->Next();
820 void wxBitmap::InitStandardHandlers()
822 // Initialize all standard bitmap or derived class handlers here.
823 AddHandler(new wxXBMFileHandler
);
824 AddHandler(new wxXBMDataHandler
);
826 // XPM is considered standard for Motif, although it can be omitted if
827 // libXpm is not installed
829 AddHandler(new wxXPMFileHandler
);
830 AddHandler(new wxXPMDataHandler
);
831 #endif // wxHAVE_LIB_XPM
834 WXPixmap
wxBitmap::GetLabelPixmap (WXWidget w
)
836 if (M_BITMAPDATA
->m_image
== (WXPixmap
) 0)
837 return M_BITMAPDATA
->m_pixmap
;
839 Display
*dpy
= (Display
*) M_BITMAPDATA
->m_display
;
844 if (labelPixmap) return labelPixmap;
845 things can be wrong, because colors can have been changed.
849 XmDestroyPixmap(DefaultScreenOfDisplay(dpy),labelPixmap) ;
850 we got BadDrawable if the pixmap is referenced by multiples widgets
854 So, before doing thing really clean, I just do nothing; if the pixmap is
855 referenced by many widgets, Motif performs caching functions.
856 And if pixmap is referenced with multiples colors, we just have some
857 memory leaks... I hope we can deal with them...
859 // Must be destroyed, because colours can have been changed!
860 if (M_BITMAPDATA
->m_labelPixmap
)
861 XmDestroyPixmap (DefaultScreenOfDisplay (dpy
), M_BITMAPDATA
->m_labelPixmap
);
865 sprintf (tmp
, "Im%x", (unsigned int) M_BITMAPDATA
->m_image
);
868 Widget widget
= (Widget
) w
;
870 while (XmIsGadget ( widget
))
871 widget
= XtParent (widget
);
872 XtVaGetValues (widget
, XmNbackground
, &bg
, XmNforeground
, &fg
, NULL
);
874 M_BITMAPDATA
->m_labelPixmap
= (WXPixmap
) XmGetPixmap (DefaultScreenOfDisplay (dpy
), tmp
, fg
, bg
);
876 return M_BITMAPDATA
->m_labelPixmap
;
879 WXPixmap
wxBitmap::GetArmPixmap (WXWidget w
)
881 if (M_BITMAPDATA
->m_image
== (WXPixmap
) 0)
882 return M_BITMAPDATA
->m_pixmap
;
884 Display
*dpy
= (Display
*) M_BITMAPDATA
->m_display
;
886 // See GetLabelPixmap () comment
888 // Must be destroyed, because colours can have been changed!
889 if (M_BITMAPDATA
->m_armPixmap
)
890 XmDestroyPixmap (DefaultScreenOfDisplay (dpy
), M_BITMAPDATA
->m_armPixmap
);
894 sprintf (tmp
, "Im%x", (unsigned int) M_BITMAPDATA
->m_image
);
897 Widget widget
= (Widget
) w
;
899 XtVaGetValues (widget
, XmNarmColor
, &bg
, NULL
);
900 while (XmIsGadget (widget
))
901 widget
= XtParent (widget
);
902 XtVaGetValues (widget
, XmNforeground
, &fg
, NULL
);
904 M_BITMAPDATA
->m_armPixmap
= (WXPixmap
) XmGetPixmap (DefaultScreenOfDisplay (dpy
), tmp
, fg
, bg
);
906 return M_BITMAPDATA
->m_armPixmap
;
909 WXPixmap
wxBitmap::GetInsensPixmap (WXWidget w
)
911 Display
*dpy
= (Display
*) M_BITMAPDATA
->m_display
;
913 if (M_BITMAPDATA
->m_insensPixmap
)
914 return M_BITMAPDATA
->m_insensPixmap
;
918 M_BITMAPDATA
->m_insensPixmap
= (WXPixmap
) XCreateInsensitivePixmap(dpy
, (Pixmap
) M_BITMAPDATA
->m_pixmap
);
919 if (M_BITMAPDATA
->m_insensPixmap
)
920 return M_BITMAPDATA
->m_insensPixmap
;
922 return M_BITMAPDATA
->m_pixmap
;
925 if (M_BITMAPDATA
->m_insensImage
== (WXPixmap
) 0)
926 return M_BITMAPDATA
->m_pixmap
;
929 See
GetLabelPixmap () comment
930 // Must be destroyed, because colours can have been changed!
931 if (M_BITMAPDATA
->m_insensPixmap
)
932 XmDestroyPixmap (DefaultScreenOfDisplay (dpy
), (Pixmap
) M_BITMAPDATA
->m_insensPixmap
);
936 sprintf (tmp
, "Not%x", (unsigned int) M_BITMAPDATA
->m_insensImage
);
939 Widget widget
= (Widget
) w
;
941 while (XmIsGadget (widget
))
942 widget
= XtParent (widget
);
943 XtVaGetValues (widget
, XmNbackground
, &bg
, XmNforeground
, &fg
, NULL
);
945 M_BITMAPDATA
->m_insensPixmap
= (WXPixmap
) XmGetPixmap (DefaultScreenOfDisplay (dpy
), tmp
, fg
, bg
);
947 return M_BITMAPDATA
->m_insensPixmap
;
950 // We may need this sometime...
952 /****************************************************************************
955 XCreateInsensitivePixmap - create a grayed-out copy of a pixmap
958 Pixmap XCreateInsensitivePixmap( Display *display, Pixmap pixmap )
961 This function creates a grayed-out copy of the argument pixmap, suitable
962 for use as a XmLabel's XmNlabelInsensitivePixmap resource.
965 The return value is the new Pixmap id or zero on error. Errors include
966 a NULL display argument or an invalid Pixmap argument.
969 If one of the XLib functions fail, it will produce a X error. The
970 default X error handler prints a diagnostic and calls exit().
973 XCopyArea(3), XCreateBitmapFromData(3), XCreateGC(3), XCreatePixmap(3),
974 XFillRectangle(3), exit(2)
977 John R Veregge - john@puente.jpl.nasa.gov
978 Advanced Engineering and Prototyping Group (AEG)
979 Information Systems Technology Section (395)
980 Jet Propulsion Lab - Calif Institute of Technology
982 *****************************************************************************/
985 XCreateInsensitivePixmap( Display
*display
, Pixmap pixmap
)
988 static char stipple_data
[] =
990 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA,
991 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA,
992 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA,
993 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA
996 Pixmap ipixmap
, stipple
;
997 unsigned width
, height
, depth
;
999 Window window
; /* These return values */
1000 unsigned border
; /* from XGetGeometry() */
1001 int x
, y
; /* are not needed. */
1005 if ( NULL
== display
|| 0 == pixmap
)
1008 if ( 0 == XGetGeometry( display
, pixmap
, &window
, &x
, &y
,
1009 &width
, &height
, &border
, &depth
)
1011 return ipixmap
; /* BadDrawable: probably an invalid pixmap */
1013 /* Get the stipple pixmap to be used to 'gray-out' the argument pixmap.
1015 stipple
= XCreateBitmapFromData( display
, pixmap
, stipple_data
, 16, 16 );
1018 gc
= XCreateGC( display
, pixmap
, (XtGCMask
)0, (XGCValues
*)NULL
);
1021 /* Create an identical copy of the argument pixmap.
1023 ipixmap
= XCreatePixmap( display
, pixmap
, width
, height
, depth
);
1026 /* Copy the argument pixmap into the new pixmap.
1028 XCopyArea( display
, pixmap
, ipixmap
,
1029 gc
, 0, 0, width
, height
, 0, 0 );
1031 /* Refill the new pixmap using the stipple algorithm/pixmap.
1033 XSetStipple( display
, gc
, stipple
);
1034 XSetFillStyle( display
, gc
, FillStippled
);
1035 XFillRectangle( display
, ipixmap
, gc
, 0, 0, width
, height
);
1037 XFreeGC( display
, gc
);
1039 XFreePixmap( display
, stipple
);
1044 // Creates a bitmap with transparent areas drawn in
1045 // the given colour.
1046 wxBitmap
wxCreateMaskedBitmap(const wxBitmap
& bitmap
, wxColour
& colour
)
1048 wxBitmap
newBitmap(bitmap
.GetWidth(),
1053 srcDC
.SelectObject(bitmap
);
1054 destDC
.SelectObject(newBitmap
);
1056 wxBrush
brush(colour
, wxSOLID
);
1057 destDC
.SetOptimization(FALSE
);
1058 destDC
.SetBackground(brush
);
1060 destDC
.Blit(0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), & srcDC
, 0, 0, wxCOPY
, TRUE
);