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 #include "wx/motif/private.h"
29 // TODO: correct symbol, path?
34 #if !USE_SHARED_LIBRARIES
35 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
36 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
39 wxBitmapRefData::wxBitmapRefData()
49 m_pixmap
= (WXPixmap
) 0;
50 m_display
= (WXDisplay
*) 0;
52 m_freePixmap
= TRUE
; //TODO: necessary?
53 m_freeColors
= (unsigned long*) 0;
54 m_freeColorsCount
= 0;
56 // These 5 variables are for wxControl
57 m_insensPixmap
= (WXPixmap
) 0;
58 m_labelPixmap
= (WXPixmap
) 0;
59 m_armPixmap
= (WXPixmap
) 0;
60 m_image
= (WXImage
*) 0;
61 m_insensImage
= (WXImage
*) 0;
64 wxBitmapRefData::~wxBitmapRefData()
67 XmDestroyPixmap (DefaultScreenOfDisplay ((Display
*) m_display
), (Pixmap
) m_labelPixmap
);
70 XmDestroyPixmap (DefaultScreenOfDisplay ((Display
*) m_display
), (Pixmap
) m_armPixmap
);
73 XmDestroyPixmap (DefaultScreenOfDisplay ((Display
*) m_display
), (Pixmap
) m_insensPixmap
);
77 XmUninstallImage ((XImage
*) m_image
);
78 XtFree ((char *) (XImage
*) m_image
);
83 XmUninstallImage ((XImage
*) m_insensImage
);
84 delete[] ((XImage
*) m_insensImage
)->data
;
85 XtFree ((char *) (XImage
*) m_insensImage
);
87 if (m_pixmap
&& m_freePixmap
)
88 XFreePixmap ((Display
*) m_display
, (Pixmap
) m_pixmap
);
92 int screen
= DefaultScreen((Display
*) m_display
);
93 Colormap cmp
= DefaultColormap((Display
*) m_display
,screen
);
95 for(llp
= 0;llp
< m_freeColorsCount
;llp
++)
96 XFreeColors((Display
*) m_display
, cmp
, &m_freeColors
[llp
], 1, 0L);
105 wxList
wxBitmap::sm_handlers
;
111 if ( wxTheBitmapList
)
112 wxTheBitmapList
->AddBitmap(this);
115 wxBitmap::~wxBitmap()
118 wxTheBitmapList
->DeleteObject(this);
121 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
123 m_refData
= new wxBitmapRefData
;
125 (void) Create((void*) bits
, wxBITMAP_TYPE_XBM_DATA
, width
, height
, depth
);
127 if ( wxTheBitmapList
)
128 wxTheBitmapList
->AddBitmap(this);
131 wxBitmap::wxBitmap(int w
, int h
, int d
)
133 (void)Create(w
, h
, d
);
135 if ( wxTheBitmapList
)
136 wxTheBitmapList
->AddBitmap(this);
139 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
141 (void) Create(data
, type
, width
, height
, depth
);
143 if ( wxTheBitmapList
)
144 wxTheBitmapList
->AddBitmap(this);
147 wxBitmap::wxBitmap(const wxString
& filename
, long type
)
149 LoadFile(filename
, (int)type
);
151 if ( wxTheBitmapList
)
152 wxTheBitmapList
->AddBitmap(this);
155 // Create from XPM data
156 static wxControl
* sg_Control
= NULL
;
157 wxBitmap::wxBitmap(char **data
, wxControl
* control
)
159 // Pass the control to the Create function using a global
160 sg_Control
= control
;
162 (void) Create((void *)data
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
164 sg_Control
= (wxControl
*) NULL
;
167 bool wxBitmap::Create(int w
, int h
, int d
)
171 m_refData
= new wxBitmapRefData
;
174 d
= wxDisplayDepth();
176 M_BITMAPDATA
->m_width
= w
;
177 M_BITMAPDATA
->m_height
= h
;
178 M_BITMAPDATA
->m_depth
= d
;
179 M_BITMAPDATA
->m_freePixmap
= TRUE
;
181 Display
*dpy
= (Display
*) wxGetDisplay();
183 M_BITMAPDATA
->m_display
= dpy
; /* MATTHEW: [4] Remember the display */
185 M_BITMAPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
188 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_pixmap
!= (WXPixmap
) 0) ;
189 return M_BITMAPDATA
->m_ok
;
192 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
196 m_refData
= new wxBitmapRefData
;
198 wxBitmapHandler
*handler
= FindHandler(type
);
200 if ( handler
== NULL
) {
201 wxLogWarning("no bitmap handler for type %d defined.", type
);
206 return handler
->LoadFile(this, filename
, type
, -1, -1);
209 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
213 m_refData
= new wxBitmapRefData
;
215 wxBitmapHandler
*handler
= FindHandler(type
);
217 if ( handler
== NULL
) {
218 wxLogWarning("no bitmap handler for type %d defined.", type
);
223 return handler
->Create(this, data
, type
, width
, height
, depth
);
226 bool wxBitmap::SaveFile(const wxString
& filename
, int type
, const wxPalette
*palette
)
228 wxBitmapHandler
*handler
= FindHandler(type
);
230 if ( handler
== NULL
) {
231 wxLogWarning("no bitmap handler for type %d defined.", type
);
236 return handler
->SaveFile(this, filename
, type
, palette
);
239 void wxBitmap::SetWidth(int w
)
242 m_refData
= new wxBitmapRefData
;
244 M_BITMAPDATA
->m_width
= w
;
247 void wxBitmap::SetHeight(int h
)
250 m_refData
= new wxBitmapRefData
;
252 M_BITMAPDATA
->m_height
= h
;
255 void wxBitmap::SetDepth(int d
)
258 m_refData
= new wxBitmapRefData
;
260 M_BITMAPDATA
->m_depth
= d
;
263 void wxBitmap::SetQuality(int q
)
266 m_refData
= new wxBitmapRefData
;
268 M_BITMAPDATA
->m_quality
= q
;
271 void wxBitmap::SetOk(bool isOk
)
274 m_refData
= new wxBitmapRefData
;
276 M_BITMAPDATA
->m_ok
= isOk
;
279 void wxBitmap::SetPalette(const wxPalette
& palette
)
282 m_refData
= new wxBitmapRefData
;
284 M_BITMAPDATA
->m_bitmapPalette
= palette
;
287 void wxBitmap::SetMask(wxMask
*mask
)
290 m_refData
= new wxBitmapRefData
;
292 M_BITMAPDATA
->m_bitmapMask
= mask
;
295 void wxBitmap::AddHandler(wxBitmapHandler
*handler
)
297 sm_handlers
.Append(handler
);
300 void wxBitmap::InsertHandler(wxBitmapHandler
*handler
)
302 sm_handlers
.Insert(handler
);
305 bool wxBitmap::RemoveHandler(const wxString
& name
)
307 wxBitmapHandler
*handler
= FindHandler(name
);
310 sm_handlers
.DeleteObject(handler
);
317 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& name
)
319 wxNode
*node
= sm_handlers
.First();
322 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
323 if ( handler
->GetName() == name
)
330 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& extension
, long bitmapType
)
332 wxNode
*node
= sm_handlers
.First();
335 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
336 if ( handler
->GetExtension() == extension
&&
337 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
344 wxBitmapHandler
*wxBitmap::FindHandler(long bitmapType
)
346 wxNode
*node
= sm_handlers
.First();
349 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
350 if (handler
->GetType() == bitmapType
)
363 m_pixmap
= (WXPixmap
) 0;
366 // Construct a mask from a bitmap and a colour indicating
367 // the transparent area
368 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
370 m_pixmap
= (WXPixmap
) 0;
372 Create(bitmap
, colour
);
375 // Construct a mask from a bitmap and a palette index indicating
376 // the transparent area
377 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
379 m_pixmap
= (WXPixmap
) 0;
381 Create(bitmap
, paletteIndex
);
384 // Construct a mask from a mono bitmap (copies the bitmap).
385 wxMask::wxMask(const wxBitmap
& bitmap
)
387 m_pixmap
= (WXPixmap
) 0;
394 // TODO: this may be the wrong display
396 XFreePixmap ((Display
*) wxGetDisplay(), (Pixmap
) m_pixmap
);
399 // Create a mask from a mono bitmap (copies the bitmap).
400 bool wxMask::Create(const wxBitmap
& WXUNUSED(bitmap
))
406 // Create a mask from a bitmap and a palette index indicating
407 // the transparent area
408 bool wxMask::Create(const wxBitmap
& WXUNUSED(bitmap
), int WXUNUSED(paletteIndex
))
414 // Create a mask from a bitmap and a colour indicating
415 // the transparent area
416 bool wxMask::Create(const wxBitmap
& WXUNUSED(bitmap
), const wxColour
& WXUNUSED(colour
))
426 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
428 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
), void *WXUNUSED(data
), long WXUNUSED(type
),
429 int WXUNUSED(width
), int WXUNUSED(height
), int WXUNUSED(depth
))
434 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
), const wxString
& WXUNUSED(name
), long WXUNUSED(type
),
435 int WXUNUSED(desiredWidth
), int WXUNUSED(desiredHeight
))
440 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
), const wxString
& WXUNUSED(name
), int WXUNUSED(type
),
441 const wxPalette
*WXUNUSED(palette
))
450 class WXDLLEXPORT wxXBMFileHandler
: public wxBitmapHandler
452 DECLARE_DYNAMIC_CLASS(wxXBMFileHandler
)
454 inline wxXBMFileHandler()
458 m_type
= wxBITMAP_TYPE_XBM
;
461 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
462 int desiredWidth
, int desiredHeight
);
464 IMPLEMENT_DYNAMIC_CLASS(wxXBMFileHandler
, wxBitmapHandler
)
466 bool wxXBMFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long WXUNUSED(flags
),
467 int WXUNUSED(desiredWidth
), int WXUNUSED(desiredHeight
))
469 M_BITMAPHANDLERDATA
->m_freePixmap
= TRUE
;
475 Display
*dpy
= (Display
*) wxGetDisplay();
476 M_BITMAPDATA
->m_display
= (WXDisplay
*) dpy
;
478 int value
= XReadBitmapFile (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
479 (char*) (const char*) name
, &w
, &h
, &pixmap
, &hotX
, &hotY
);
480 M_BITMAPHANDLERDATA
->m_width
= w
;
481 M_BITMAPHANDLERDATA
->m_height
= h
;
482 M_BITMAPHANDLERDATA
->m_depth
= 1;
483 M_BITMAPHANDLERDATA
->m_pixmap
= (WXPixmap
) pixmap
;
485 if ((value
== BitmapFileInvalid
) ||
486 (value
== BitmapOpenFailed
) ||
487 (value
== BitmapNoMemory
))
489 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
490 M_BITMAPHANDLERDATA
->m_pixmap
= (WXPixmap
) 0;
493 M_BITMAPHANDLERDATA
->m_ok
= TRUE
;
495 return M_BITMAPHANDLERDATA
->m_ok
;
498 class WXDLLEXPORT wxXBMDataHandler
: public wxBitmapHandler
500 DECLARE_DYNAMIC_CLASS(wxXBMDataHandler
)
502 inline wxXBMDataHandler()
506 m_type
= wxBITMAP_TYPE_XBM_DATA
;
509 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
, int width
, int height
, int depth
= 1);
511 IMPLEMENT_DYNAMIC_CLASS(wxXBMDataHandler
, wxBitmapHandler
)
513 bool wxXBMDataHandler::Create( wxBitmap
*bitmap
, void *data
, long WXUNUSED(flags
),
514 int width
, int height
, int WXUNUSED(depth
))
516 M_BITMAPHANDLERDATA
->m_width
= width
;
517 M_BITMAPHANDLERDATA
->m_height
= height
;
518 M_BITMAPHANDLERDATA
->m_depth
= 1;
519 M_BITMAPHANDLERDATA
->m_freePixmap
= TRUE
;
521 Display
*dpy
= (Display
*) wxGetDisplay();
522 M_BITMAPHANDLERDATA
->m_display
= (WXDisplay
*) dpy
;
524 M_BITMAPHANDLERDATA
->m_pixmap
= (WXPixmap
) XCreateBitmapFromData (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)), (char*) data
, width
, height
);
525 M_BITMAPHANDLERDATA
->m_ok
= (M_BITMAPHANDLERDATA
->m_pixmap
!= (WXPixmap
) 0) ;
527 // code for wxControl. TODO: can we avoid doing this until we need it?
528 // E.g. have CreateButtonPixmaps which is called on demand.
529 XImage
* image
= (XImage
*) XtMalloc (sizeof (XImage
));
530 image
->width
= width
;
531 image
->height
= height
;
532 image
->data
= (char*) data
;
535 image
->format
= XYBitmap
;
536 image
->byte_order
= LSBFirst
;
537 image
->bitmap_unit
= 8;
538 image
->bitmap_bit_order
= LSBFirst
;
539 image
->bitmap_pad
= 8;
540 image
->bytes_per_line
= (width
+ 7) >> 3;
543 sprintf (tmp
, "Im%x", (unsigned int) image
);
544 XmInstallImage (image
, tmp
);
546 // Build our manually stipped pixmap.
548 int bpl
= (width
+ 7) / 8;
549 char *data1
= new char[height
* bpl
];
550 char* bits
= (char*) data
;
552 for (i
= 0; i
< height
; i
++)
554 int mask
= i
% 2 ? 0x55 : 0xaa;
556 for (j
= 0; j
< bpl
; j
++)
557 data1
[i
* bpl
+ j
] = bits
[i
* bpl
+ j
] & mask
;
559 XImage
* insensImage
= (XImage
*) XtMalloc (sizeof (XImage
));
560 insensImage
->width
= width
;
561 insensImage
->height
= height
;
562 insensImage
->data
= data1
;
563 insensImage
->depth
= 1;
564 insensImage
->xoffset
= 0;
565 insensImage
->format
= XYBitmap
;
566 insensImage
->byte_order
= LSBFirst
;
567 insensImage
->bitmap_unit
= 8;
568 insensImage
->bitmap_bit_order
= LSBFirst
;
569 insensImage
->bitmap_pad
= 8;
570 insensImage
->bytes_per_line
= bpl
;
572 sprintf (tmp
, "Not%x", (unsigned int)insensImage
);
573 XmInstallImage (insensImage
, tmp
);
575 M_BITMAPHANDLERDATA
->m_image
= (WXImage
*) image
;
576 M_BITMAPHANDLERDATA
->m_insensImage
= (WXImage
*) insensImage
;
582 class WXDLLEXPORT wxXPMFileHandler
: public wxBitmapHandler
584 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler
)
586 inline wxXPMFileHandler()
590 m_type
= wxBITMAP_TYPE_XPM
;
593 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
594 int desiredWidth
, int desiredHeight
);
595 virtual bool SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
= NULL
);
598 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
)
600 bool wxXPMFileHandler::LoadFile( wxBitmap
*bitmap
, const wxString
& name
, long WXUNUSED(flags
),
601 int WXUNUSED(desiredWidth
), int WXUNUSED(desiredHeight
) )
603 Display
*dpy
= (Display
*) wxGetDisplay();
604 M_BITMAPHANDLERDATA
->m_display
= (WXDisplay
*) dpy
;
606 XpmAttributes xpmAttr
;
610 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
611 xpmAttr
.valuemask
= XpmReturnInfos
| XpmCloseness
;
612 xpmAttr
.closeness
= 40000;
613 int errorStatus
= XpmReadFileToPixmap(dpy
,
614 RootWindow(dpy
, DefaultScreen(dpy
)), (char*) (const char*) name
,
615 &pixmap
, &mask
, &xpmAttr
);
617 if (errorStatus
== XpmSuccess
)
619 M_BITMAPHANDLERDATA
->m_pixmap
= (WXPixmap
) pixmap
;
622 M_BITMAPHANDLERDATA
->m_bitmapMask
= new wxMask
;
623 M_BITMAPHANDLERDATA
->m_bitmapMask
->SetPixmap((WXPixmap
) mask
);
626 unsigned int depthRet
;
628 unsigned int widthRet
, heightRet
, borderWidthRet
;
629 Window rootWindowRet
;
630 XGetGeometry(dpy
, pixmap
, &rootWindowRet
, &xRet
, &yRet
,
631 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
633 M_BITMAPHANDLERDATA
->m_width
= xpmAttr
.width
;
634 M_BITMAPHANDLERDATA
->m_height
= xpmAttr
.height
;
637 if ( xpmAttr.npixels > 2 )
639 M_BITMAPHANDLERDATA->m_depth = 8; // TODO: next time not just a guess :-) ...
642 M_BITMAPHANDLERDATA->m_depth = 1; // mono
646 M_BITMAPHANDLERDATA
->m_depth
= depthRet
;
648 M_BITMAPHANDLERDATA
->m_numColors
= xpmAttr
.npixels
;
650 XpmFreeAttributes(&xpmAttr
);
652 M_BITMAPHANDLERDATA
->m_ok
= TRUE
;
656 // XpmDebugError(errorStatus, name);
657 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
662 bool wxXPMFileHandler::SaveFile( wxBitmap
*bitmap
, const wxString
& name
, int WXUNUSED(type
),
663 const wxPalette
*WXUNUSED(palette
))
665 if (M_BITMAPHANDLERDATA
->m_ok
&& M_BITMAPHANDLERDATA
->m_pixmap
)
667 Display
*dpy
= (Display
*) M_BITMAPHANDLERDATA
->m_display
;
668 int errorStatus
= XpmWriteFileFromPixmap(dpy
, (char*) (const char*) name
,
669 (Pixmap
) M_BITMAPHANDLERDATA
->m_pixmap
,
670 (M_BITMAPHANDLERDATA
->m_bitmapMask
? (Pixmap
) M_BITMAPHANDLERDATA
->m_bitmapMask
->GetPixmap() : (Pixmap
) 0),
671 (XpmAttributes
*) NULL
);
672 if (errorStatus
== XpmSuccess
)
681 class WXDLLEXPORT wxXPMDataHandler
: public wxBitmapHandler
683 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler
)
685 inline wxXPMDataHandler()
689 m_type
= wxBITMAP_TYPE_XPM_DATA
;
692 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
, int width
, int height
, int depth
= 1);
694 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
696 bool wxXPMDataHandler::Create( wxBitmap
*bitmap
, void *data
, long WXUNUSED(flags
),
697 int width
, int height
, int WXUNUSED(depth
))
699 M_BITMAPHANDLERDATA
->m_width
= width
;
700 M_BITMAPHANDLERDATA
->m_height
= height
;
701 M_BITMAPHANDLERDATA
->m_depth
= 1;
702 M_BITMAPHANDLERDATA
->m_freePixmap
= TRUE
;
704 Display
*dpy
= (Display
*) wxGetDisplay();
705 M_BITMAPHANDLERDATA
->m_display
= (WXDisplay
*) dpy
;
707 XpmAttributes xpmAttr
;
709 xpmAttr
.valuemask
= XpmReturnInfos
; /* nothing yet, but get infos back */
711 XpmColorSymbol symbolicColors
[4];
712 if (sg_Control
&& sg_Control
->GetMainWidget())
714 symbolicColors
[0].name
= "foreground";
715 symbolicColors
[0].value
= NULL
;
716 symbolicColors
[1].name
= "background";
717 symbolicColors
[1].value
= NULL
;
718 XtVaGetValues((Widget
) sg_Control
->GetMainWidget(),
719 XmNforeground
, &symbolicColors
[0].pixel
,
720 XmNbackground
, &symbolicColors
[1].pixel
,NULL
);
721 xpmAttr
.numsymbols
= 2;
722 xpmAttr
.colorsymbols
= symbolicColors
;
723 xpmAttr
.valuemask
|= XpmColorSymbols
; // add flag
728 int ErrorStatus
= XpmCreatePixmapFromData(dpy
, RootWindow(dpy
, DefaultScreen(dpy
)),
729 (char**) data
, &pixmap
, &mask
, &xpmAttr
);
730 if (ErrorStatus
== XpmSuccess
)
733 M_BITMAPHANDLERDATA
->m_width
= xpmAttr
.width
;
734 M_BITMAPHANDLERDATA
->m_height
= xpmAttr
.height
;
736 unsigned int depthRet
;
738 unsigned int widthRet
, heightRet
, borderWidthRet
;
739 Window rootWindowRet
;
740 XGetGeometry(dpy
, pixmap
, &rootWindowRet
, &xRet
, &yRet
,
741 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
744 if ( xpmAttr.npixels > 2 )
746 M_BITMAPHANDLERDATA->m_depth = 8; // next time not just a guess :-) ...
749 M_BITMAPHANDLERDATA->m_depth = 1; // mono
753 M_BITMAPHANDLERDATA
->m_depth
= depthRet
;
755 M_BITMAPHANDLERDATA
->m_numColors
= xpmAttr
.npixels
;
756 XpmFreeAttributes(&xpmAttr
);
757 M_BITMAPHANDLERDATA
->m_ok
= TRUE
;
758 M_BITMAPHANDLERDATA
->m_pixmap
= (WXPixmap
) pixmap
;
761 M_BITMAPHANDLERDATA
->m_bitmapMask
= new wxMask
;
762 M_BITMAPHANDLERDATA
->m_bitmapMask
->SetPixmap((WXPixmap
) mask
);
767 // XpmDebugError(ErrorStatus, NULL);
768 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
770 return M_BITMAPHANDLERDATA
->m_ok
;
775 void wxBitmap::CleanUpHandlers()
777 wxNode
*node
= sm_handlers
.First();
780 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
781 wxNode
*next
= node
->Next();
788 void wxBitmap::InitStandardHandlers()
790 // Initialize all standard bitmap or derived class handlers here.
791 AddHandler(new wxXBMFileHandler
);
792 AddHandler(new wxXBMDataHandler
);
794 // XPM is considered standard for Moif, although it can be omitted if absolutely
797 AddHandler(new wxXPMFileHandler
);
798 AddHandler(new wxXPMDataHandler
);
802 WXPixmap
wxBitmap::GetLabelPixmap (WXWidget w
)
804 if (M_BITMAPDATA
->m_image
== (WXPixmap
) 0)
805 return M_BITMAPDATA
->m_pixmap
;
807 Display
*dpy
= (Display
*) M_BITMAPDATA
->m_display
;
812 if (labelPixmap) return labelPixmap;
813 things can be wrong, because colors can have been changed.
817 XmDestroyPixmap(DefaultScreenOfDisplay(dpy),labelPixmap) ;
818 we got BadDrawable if the pixmap is referenced by multiples widgets
822 So, before doing thing really clean, I just do nothing; if the pixmap is
823 referenced by many widgets, Motif performs caching functions.
824 And if pixmap is referenced with multiples colors, we just have some
825 memory leaks... I hope we can deal with them...
827 // Must be destroyed, because colours can have been changed!
828 if (M_BITMAPDATA
->m_labelPixmap
)
829 XmDestroyPixmap (DefaultScreenOfDisplay (dpy
), M_BITMAPDATA
->m_labelPixmap
);
833 sprintf (tmp
, "Im%x", (unsigned int) M_BITMAPDATA
->m_image
);
836 Widget widget
= (Widget
) w
;
838 while (XmIsGadget ( widget
))
839 widget
= XtParent (widget
);
840 XtVaGetValues (widget
, XmNbackground
, &bg
, XmNforeground
, &fg
, NULL
);
842 M_BITMAPDATA
->m_labelPixmap
= (WXPixmap
) XmGetPixmap (DefaultScreenOfDisplay (dpy
), tmp
, fg
, bg
);
844 return M_BITMAPDATA
->m_labelPixmap
;
847 WXPixmap
wxBitmap::GetArmPixmap (WXWidget w
)
849 if (M_BITMAPDATA
->m_image
== (WXPixmap
) 0)
850 return M_BITMAPDATA
->m_pixmap
;
852 Display
*dpy
= (Display
*) M_BITMAPDATA
->m_display
;
854 See
GetLabelPixmap () comment
855 // Must be destroyed, because colours can have been changed!
856 if (M_BITMAPDATA
->m_armPixmap
)
857 XmDestroyPixmap (DefaultScreenOfDisplay (dpy
), M_BITMAPDATA
->m_armPixmap
);
861 sprintf (tmp
, "Im%x", (unsigned int) M_BITMAPDATA
->m_image
);
864 Widget widget
= (Widget
) w
;
866 XtVaGetValues (widget
, XmNarmColor
, &bg
, NULL
);
867 while (XmIsGadget (widget
))
868 widget
= XtParent (widget
);
869 XtVaGetValues (widget
, XmNforeground
, &fg
, NULL
);
871 M_BITMAPDATA
->m_armPixmap
= (WXPixmap
) XmGetPixmap (DefaultScreenOfDisplay (dpy
), tmp
, fg
, bg
);
873 return M_BITMAPDATA
->m_armPixmap
;
876 WXPixmap
wxBitmap::GetInsensPixmap (WXWidget w
)
878 Display
*dpy
= (Display
*) M_BITMAPDATA
->m_display
;
880 if (M_BITMAPDATA
->m_insensPixmap
)
881 return M_BITMAPDATA
->m_insensPixmap
;
885 M_BITMAPDATA
->m_insensPixmap
= (WXPixmap
) XCreateInsensitivePixmap(dpy
, (Pixmap
) M_BITMAPDATA
->m_pixmap
);
886 if (M_BITMAPDATA
->m_insensPixmap
)
887 return M_BITMAPDATA
->m_insensPixmap
;
889 return M_BITMAPDATA
->m_pixmap
;
892 if (M_BITMAPDATA
->m_insensImage
== (WXPixmap
) 0)
893 return M_BITMAPDATA
->m_pixmap
;
896 See
GetLabelPixmap () comment
897 // Must be destroyed, because colours can have been changed!
898 if (M_BITMAPDATA
->m_insensPixmap
)
899 XmDestroyPixmap (DefaultScreenOfDisplay (dpy
), (Pixmap
) M_BITMAPDATA
->m_insensPixmap
);
903 sprintf (tmp
, "Not%x", (unsigned int) M_BITMAPDATA
->m_insensImage
);
906 Widget widget
= (Widget
) w
;
908 while (XmIsGadget (widget
))
909 widget
= XtParent (widget
);
910 XtVaGetValues (widget
, XmNbackground
, &bg
, XmNforeground
, &fg
, NULL
);
912 M_BITMAPDATA
->m_insensPixmap
= (WXPixmap
) XmGetPixmap (DefaultScreenOfDisplay (dpy
), tmp
, fg
, bg
);
914 return M_BITMAPDATA
->m_insensPixmap
;
917 // We may need this sometime...
919 /****************************************************************************
922 XCreateInsensitivePixmap - create a grayed-out copy of a pixmap
925 Pixmap XCreateInsensitivePixmap( Display *display, Pixmap pixmap )
928 This function creates a grayed-out copy of the argument pixmap, suitable
929 for use as a XmLabel's XmNlabelInsensitivePixmap resource.
932 The return value is the new Pixmap id or zero on error. Errors include
933 a NULL display argument or an invalid Pixmap argument.
936 If one of the XLib functions fail, it will produce a X error. The
937 default X error handler prints a diagnostic and calls exit().
940 XCopyArea(3), XCreateBitmapFromData(3), XCreateGC(3), XCreatePixmap(3),
941 XFillRectangle(3), exit(2)
944 John R Veregge - john@puente.jpl.nasa.gov
945 Advanced Engineering and Prototyping Group (AEG)
946 Information Systems Technology Section (395)
947 Jet Propulsion Lab - Calif Institute of Technology
949 *****************************************************************************/
952 XCreateInsensitivePixmap( Display
*display
, Pixmap pixmap
)
956 char stipple_data
[] =
958 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA,
959 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA,
960 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA,
961 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA
964 Pixmap ipixmap
, stipple
;
965 unsigned width
, height
, depth
;
967 Window window
; /* These return values */
968 unsigned border
; /* from XGetGeometry() */
969 int x
, y
; /* are not needed. */
973 if ( NULL
== display
|| 0 == pixmap
)
976 if ( 0 == XGetGeometry( display
, pixmap
, &window
, &x
, &y
,
977 &width
, &height
, &border
, &depth
)
979 return ipixmap
; /* BadDrawable: probably an invalid pixmap */
981 /* Get the stipple pixmap to be used to 'gray-out' the argument pixmap.
983 stipple
= XCreateBitmapFromData( display
, pixmap
, stipple_data
, 16, 16 );
986 gc
= XCreateGC( display
, pixmap
, (XtGCMask
)0, (XGCValues
*)NULL
);
989 /* Create an identical copy of the argument pixmap.
991 ipixmap
= XCreatePixmap( display
, pixmap
, width
, height
, depth
);
994 /* Copy the argument pixmap into the new pixmap.
996 XCopyArea( display
, pixmap
, ipixmap
,
997 gc
, 0, 0, width
, height
, 0, 0 );
999 /* Refill the new pixmap using the stipple algorithm/pixmap.
1001 XSetStipple( display
, gc
, stipple
);
1002 XSetFillStyle( display
, gc
, FillStippled
);
1003 XFillRectangle( display
, ipixmap
, gc
, 0, 0, width
, height
);
1005 XFreeGC( display
, gc
);
1007 XFreePixmap( display
, stipple
);
1012 // Creates a bitmap with transparent areas drawn in
1013 // the given colour.
1014 wxBitmap
wxCreateMaskedBitmap(wxBitmap
& bitmap
, wxColour
& colour
)
1016 wxBitmap
newBitmap(bitmap
.GetWidth(),
1021 srcDC
.SelectObject(bitmap
);
1022 destDC
.SelectObject(newBitmap
);
1024 wxBrush
brush(colour
, wxSOLID
);
1025 destDC
.SetOptimization(FALSE
);
1026 destDC
.SetBackground(brush
);
1028 destDC
.Blit(0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), & srcDC
, 0, 0, wxCOPY
, TRUE
);