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
& bitmap
)
406 // Create a mask from a bitmap and a palette index indicating
407 // the transparent area
408 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
414 // Create a mask from a bitmap and a colour indicating
415 // the transparent area
416 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
426 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
428 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
433 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long type
,
434 int desiredWidth
, int desiredHeight
)
439 bool wxBitmapHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
448 class WXDLLEXPORT wxXBMFileHandler
: public wxBitmapHandler
450 DECLARE_DYNAMIC_CLASS(wxXBMFileHandler
)
452 inline wxXBMFileHandler()
456 m_type
= wxBITMAP_TYPE_XBM
;
459 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
460 int desiredWidth
, int desiredHeight
);
462 IMPLEMENT_DYNAMIC_CLASS(wxXBMFileHandler
, wxBitmapHandler
)
464 bool wxXBMFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
465 int desiredWidth
, int desiredHeight
)
467 M_BITMAPHANDLERDATA
->m_freePixmap
= TRUE
;
473 Display
*dpy
= (Display
*) wxGetDisplay();
474 M_BITMAPDATA
->m_display
= (WXDisplay
*) dpy
;
476 int value
= XReadBitmapFile (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
477 (char*) (const char*) name
, &w
, &h
, &pixmap
, &hotX
, &hotY
);
478 M_BITMAPHANDLERDATA
->m_width
= w
;
479 M_BITMAPHANDLERDATA
->m_height
= h
;
480 M_BITMAPHANDLERDATA
->m_depth
= 1;
481 M_BITMAPHANDLERDATA
->m_pixmap
= (WXPixmap
) pixmap
;
483 if ((value
== BitmapFileInvalid
) ||
484 (value
== BitmapOpenFailed
) ||
485 (value
== BitmapNoMemory
))
487 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
488 M_BITMAPHANDLERDATA
->m_pixmap
= (WXPixmap
) 0;
491 M_BITMAPHANDLERDATA
->m_ok
= TRUE
;
493 return M_BITMAPHANDLERDATA
->m_ok
;
496 class WXDLLEXPORT wxXBMDataHandler
: public wxBitmapHandler
498 DECLARE_DYNAMIC_CLASS(wxXBMDataHandler
)
500 inline wxXBMDataHandler()
504 m_type
= wxBITMAP_TYPE_XBM_DATA
;
507 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
, int width
, int height
, int depth
= 1);
509 IMPLEMENT_DYNAMIC_CLASS(wxXBMDataHandler
, wxBitmapHandler
)
511 bool wxXBMDataHandler::Create(wxBitmap
*bitmap
, void *data
, long flags
, int width
, int height
, int depth
)
513 M_BITMAPHANDLERDATA
->m_width
= width
;
514 M_BITMAPHANDLERDATA
->m_height
= height
;
515 M_BITMAPHANDLERDATA
->m_depth
= 1;
516 M_BITMAPHANDLERDATA
->m_freePixmap
= TRUE
;
518 Display
*dpy
= (Display
*) wxGetDisplay();
519 M_BITMAPHANDLERDATA
->m_display
= (WXDisplay
*) dpy
;
521 M_BITMAPHANDLERDATA
->m_pixmap
= (WXPixmap
) XCreateBitmapFromData (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)), (char*) data
, width
, height
);
522 M_BITMAPHANDLERDATA
->m_ok
= (M_BITMAPHANDLERDATA
->m_pixmap
!= (WXPixmap
) 0) ;
524 // code for wxControl. TODO: can we avoid doing this until we need it?
525 // E.g. have CreateButtonPixmaps which is called on demand.
526 XImage
* image
= (XImage
*) XtMalloc (sizeof (XImage
));
527 image
->width
= width
;
528 image
->height
= height
;
529 image
->data
= (char*) data
;
532 image
->format
= XYBitmap
;
533 image
->byte_order
= LSBFirst
;
534 image
->bitmap_unit
= 8;
535 image
->bitmap_bit_order
= LSBFirst
;
536 image
->bitmap_pad
= 8;
537 image
->bytes_per_line
= (width
+ 7) >> 3;
540 sprintf (tmp
, "Im%x", (unsigned int) image
);
541 XmInstallImage (image
, tmp
);
543 // Build our manually stipped pixmap.
545 int bpl
= (width
+ 7) / 8;
546 char *data1
= new char[height
* bpl
];
547 char* bits
= (char*) data
;
549 for (i
= 0; i
< height
; i
++)
551 int mask
= i
% 2 ? 0x55 : 0xaa;
553 for (j
= 0; j
< bpl
; j
++)
554 data1
[i
* bpl
+ j
] = bits
[i
* bpl
+ j
] & mask
;
556 XImage
* insensImage
= (XImage
*) XtMalloc (sizeof (XImage
));
557 insensImage
->width
= width
;
558 insensImage
->height
= height
;
559 insensImage
->data
= data1
;
560 insensImage
->depth
= 1;
561 insensImage
->xoffset
= 0;
562 insensImage
->format
= XYBitmap
;
563 insensImage
->byte_order
= LSBFirst
;
564 insensImage
->bitmap_unit
= 8;
565 insensImage
->bitmap_bit_order
= LSBFirst
;
566 insensImage
->bitmap_pad
= 8;
567 insensImage
->bytes_per_line
= bpl
;
569 sprintf (tmp
, "Not%x", (unsigned int)insensImage
);
570 XmInstallImage (insensImage
, tmp
);
572 M_BITMAPHANDLERDATA
->m_image
= (WXImage
*) image
;
573 M_BITMAPHANDLERDATA
->m_insensImage
= (WXImage
*) insensImage
;
579 class WXDLLEXPORT wxXPMFileHandler
: public wxBitmapHandler
581 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler
)
583 inline wxXPMFileHandler()
587 m_type
= wxBITMAP_TYPE_XPM
;
590 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
591 int desiredWidth
, int desiredHeight
);
592 virtual bool SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
= NULL
);
595 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
)
597 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
598 int desiredWidth
, int desiredHeight
)
600 Display
*dpy
= (Display
*) wxGetDisplay();
601 M_BITMAPHANDLERDATA
->m_display
= (WXDisplay
*) dpy
;
603 XpmAttributes xpmAttr
;
607 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
608 xpmAttr
.valuemask
= XpmReturnInfos
| XpmCloseness
;
609 xpmAttr
.closeness
= 40000;
610 int errorStatus
= XpmReadFileToPixmap(dpy
,
611 RootWindow(dpy
, DefaultScreen(dpy
)), (char*) (const char*) name
,
612 &pixmap
, &mask
, &xpmAttr
);
614 if (errorStatus
== XpmSuccess
)
616 M_BITMAPHANDLERDATA
->m_pixmap
= (WXPixmap
) pixmap
;
619 M_BITMAPHANDLERDATA
->m_bitmapMask
= new wxMask
;
620 M_BITMAPHANDLERDATA
->m_bitmapMask
->SetPixmap((WXPixmap
) mask
);
623 unsigned int depthRet
;
625 unsigned int widthRet
, heightRet
, borderWidthRet
;
626 Window rootWindowRet
;
627 XGetGeometry(dpy
, pixmap
, &rootWindowRet
, &xRet
, &yRet
,
628 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
630 M_BITMAPHANDLERDATA
->m_width
= xpmAttr
.width
;
631 M_BITMAPHANDLERDATA
->m_height
= xpmAttr
.height
;
634 if ( xpmAttr.npixels > 2 )
636 M_BITMAPHANDLERDATA->m_depth = 8; // TODO: next time not just a guess :-) ...
639 M_BITMAPHANDLERDATA->m_depth = 1; // mono
643 M_BITMAPHANDLERDATA
->m_depth
= depthRet
;
645 M_BITMAPHANDLERDATA
->m_numColors
= xpmAttr
.npixels
;
647 XpmFreeAttributes(&xpmAttr
);
649 M_BITMAPHANDLERDATA
->m_ok
= TRUE
;
653 // XpmDebugError(errorStatus, name);
654 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
659 bool wxXPMFileHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
661 if (M_BITMAPHANDLERDATA
->m_ok
&& M_BITMAPHANDLERDATA
->m_pixmap
)
663 Display
*dpy
= (Display
*) M_BITMAPHANDLERDATA
->m_display
;
664 int errorStatus
= XpmWriteFileFromPixmap(dpy
, (char*) (const char*) name
,
665 (Pixmap
) M_BITMAPHANDLERDATA
->m_pixmap
,
666 (M_BITMAPHANDLERDATA
->m_bitmapMask
? (Pixmap
) M_BITMAPHANDLERDATA
->m_bitmapMask
->GetPixmap() : (Pixmap
) 0),
667 (XpmAttributes
*) NULL
);
668 if (errorStatus
== XpmSuccess
)
677 class WXDLLEXPORT wxXPMDataHandler
: public wxBitmapHandler
679 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler
)
681 inline wxXPMDataHandler()
685 m_type
= wxBITMAP_TYPE_XPM_DATA
;
688 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
, int width
, int height
, int depth
= 1);
690 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
692 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
, void *data
, long flags
, int width
, int height
, int depth
)
694 M_BITMAPHANDLERDATA
->m_width
= width
;
695 M_BITMAPHANDLERDATA
->m_height
= height
;
696 M_BITMAPHANDLERDATA
->m_depth
= 1;
697 M_BITMAPHANDLERDATA
->m_freePixmap
= TRUE
;
699 Display
*dpy
= (Display
*) wxGetDisplay();
700 M_BITMAPHANDLERDATA
->m_display
= (WXDisplay
*) dpy
;
702 XpmAttributes xpmAttr
;
704 xpmAttr
.valuemask
= XpmReturnInfos
; /* nothing yet, but get infos back */
706 XpmColorSymbol symbolicColors
[4];
707 if (sg_Control
&& sg_Control
->GetMainWidget())
709 symbolicColors
[0].name
= "foreground";
710 symbolicColors
[0].value
= NULL
;
711 symbolicColors
[1].name
= "background";
712 symbolicColors
[1].value
= NULL
;
713 XtVaGetValues((Widget
) sg_Control
->GetMainWidget(),
714 XmNforeground
, &symbolicColors
[0].pixel
,
715 XmNbackground
, &symbolicColors
[1].pixel
,NULL
);
716 xpmAttr
.numsymbols
= 2;
717 xpmAttr
.colorsymbols
= symbolicColors
;
718 xpmAttr
.valuemask
|= XpmColorSymbols
; // add flag
723 int ErrorStatus
= XpmCreatePixmapFromData(dpy
, RootWindow(dpy
, DefaultScreen(dpy
)),
724 (char**) data
, &pixmap
, &mask
, &xpmAttr
);
725 if (ErrorStatus
== XpmSuccess
)
728 M_BITMAPHANDLERDATA
->m_width
= xpmAttr
.width
;
729 M_BITMAPHANDLERDATA
->m_height
= xpmAttr
.height
;
731 unsigned int depthRet
;
733 unsigned int widthRet
, heightRet
, borderWidthRet
;
734 Window rootWindowRet
;
735 XGetGeometry(dpy
, pixmap
, &rootWindowRet
, &xRet
, &yRet
,
736 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
739 if ( xpmAttr.npixels > 2 )
741 M_BITMAPHANDLERDATA->m_depth = 8; // next time not just a guess :-) ...
744 M_BITMAPHANDLERDATA->m_depth = 1; // mono
748 M_BITMAPHANDLERDATA
->m_depth
= depthRet
;
750 M_BITMAPHANDLERDATA
->m_numColors
= xpmAttr
.npixels
;
751 XpmFreeAttributes(&xpmAttr
);
752 M_BITMAPHANDLERDATA
->m_ok
= TRUE
;
753 M_BITMAPHANDLERDATA
->m_pixmap
= (WXPixmap
) pixmap
;
756 M_BITMAPHANDLERDATA
->m_bitmapMask
= new wxMask
;
757 M_BITMAPHANDLERDATA
->m_bitmapMask
->SetPixmap((WXPixmap
) mask
);
762 // XpmDebugError(ErrorStatus, NULL);
763 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
765 return M_BITMAPHANDLERDATA
->m_ok
;
770 void wxBitmap::CleanUpHandlers()
772 wxNode
*node
= sm_handlers
.First();
775 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
776 wxNode
*next
= node
->Next();
783 void wxBitmap::InitStandardHandlers()
785 // Initialize all standard bitmap or derived class handlers here.
786 AddHandler(new wxXBMFileHandler
);
787 AddHandler(new wxXBMDataHandler
);
789 // XPM is considered standard for Moif, although it can be omitted if absolutely
792 AddHandler(new wxXPMFileHandler
);
793 AddHandler(new wxXPMDataHandler
);
797 WXPixmap
wxBitmap::GetLabelPixmap (WXWidget w
)
799 if (M_BITMAPDATA
->m_image
== (WXPixmap
) 0)
800 return M_BITMAPDATA
->m_pixmap
;
802 Display
*dpy
= (Display
*) M_BITMAPDATA
->m_display
;
807 if (labelPixmap) return labelPixmap;
808 things can be wrong, because colors can have been changed.
812 XmDestroyPixmap(DefaultScreenOfDisplay(dpy),labelPixmap) ;
813 we got BadDrawable if the pixmap is referenced by multiples widgets
817 So, before doing thing really clean, I just do nothing; if the pixmap is
818 referenced by many widgets, Motif performs caching functions.
819 And if pixmap is referenced with multiples colors, we just have some
820 memory leaks... I hope we can deal with them...
822 // Must be destroyed, because colours can have been changed!
823 if (M_BITMAPDATA
->m_labelPixmap
)
824 XmDestroyPixmap (DefaultScreenOfDisplay (dpy
), M_BITMAPDATA
->m_labelPixmap
);
828 sprintf (tmp
, "Im%x", (unsigned int) M_BITMAPDATA
->m_image
);
831 Widget widget
= (Widget
) w
;
833 while (XmIsGadget ( widget
))
834 widget
= XtParent (widget
);
835 XtVaGetValues (widget
, XmNbackground
, &bg
, XmNforeground
, &fg
, NULL
);
837 M_BITMAPDATA
->m_labelPixmap
= (WXPixmap
) XmGetPixmap (DefaultScreenOfDisplay (dpy
), tmp
, fg
, bg
);
839 return M_BITMAPDATA
->m_labelPixmap
;
842 WXPixmap
wxBitmap::GetArmPixmap (WXWidget w
)
844 if (M_BITMAPDATA
->m_image
== (WXPixmap
) 0)
845 return M_BITMAPDATA
->m_pixmap
;
847 Display
*dpy
= (Display
*) M_BITMAPDATA
->m_display
;
849 See
GetLabelPixmap () comment
850 // Must be destroyed, because colours can have been changed!
851 if (M_BITMAPDATA
->m_armPixmap
)
852 XmDestroyPixmap (DefaultScreenOfDisplay (dpy
), M_BITMAPDATA
->m_armPixmap
);
856 sprintf (tmp
, "Im%x", (unsigned int) M_BITMAPDATA
->m_image
);
859 Widget widget
= (Widget
) w
;
861 XtVaGetValues (widget
, XmNarmColor
, &bg
, NULL
);
862 while (XmIsGadget (widget
))
863 widget
= XtParent (widget
);
864 XtVaGetValues (widget
, XmNforeground
, &fg
, NULL
);
866 M_BITMAPDATA
->m_armPixmap
= (WXPixmap
) XmGetPixmap (DefaultScreenOfDisplay (dpy
), tmp
, fg
, bg
);
868 return M_BITMAPDATA
->m_armPixmap
;
871 WXPixmap
wxBitmap::GetInsensPixmap (WXWidget w
)
873 Display
*dpy
= (Display
*) M_BITMAPDATA
->m_display
;
875 if (M_BITMAPDATA
->m_insensPixmap
)
876 return M_BITMAPDATA
->m_insensPixmap
;
880 M_BITMAPDATA
->m_insensPixmap
= (WXPixmap
) XCreateInsensitivePixmap(dpy
, (Pixmap
) M_BITMAPDATA
->m_pixmap
);
881 if (M_BITMAPDATA
->m_insensPixmap
)
882 return M_BITMAPDATA
->m_insensPixmap
;
884 return M_BITMAPDATA
->m_pixmap
;
887 if (M_BITMAPDATA
->m_insensImage
== (WXPixmap
) 0)
888 return M_BITMAPDATA
->m_pixmap
;
891 See
GetLabelPixmap () comment
892 // Must be destroyed, because colours can have been changed!
893 if (M_BITMAPDATA
->m_insensPixmap
)
894 XmDestroyPixmap (DefaultScreenOfDisplay (dpy
), (Pixmap
) M_BITMAPDATA
->m_insensPixmap
);
898 sprintf (tmp
, "Not%x", (unsigned int) M_BITMAPDATA
->m_insensImage
);
901 Widget widget
= (Widget
) w
;
903 while (XmIsGadget (widget
))
904 widget
= XtParent (widget
);
905 XtVaGetValues (widget
, XmNbackground
, &bg
, XmNforeground
, &fg
, NULL
);
907 M_BITMAPDATA
->m_insensPixmap
= (WXPixmap
) XmGetPixmap (DefaultScreenOfDisplay (dpy
), tmp
, fg
, bg
);
909 return M_BITMAPDATA
->m_insensPixmap
;
912 // We may need this sometime...
914 /****************************************************************************
917 XCreateInsensitivePixmap - create a grayed-out copy of a pixmap
920 Pixmap XCreateInsensitivePixmap( Display *display, Pixmap pixmap )
923 This function creates a grayed-out copy of the argument pixmap, suitable
924 for use as a XmLabel's XmNlabelInsensitivePixmap resource.
927 The return value is the new Pixmap id or zero on error. Errors include
928 a NULL display argument or an invalid Pixmap argument.
931 If one of the XLib functions fail, it will produce a X error. The
932 default X error handler prints a diagnostic and calls exit().
935 XCopyArea(3), XCreateBitmapFromData(3), XCreateGC(3), XCreatePixmap(3),
936 XFillRectangle(3), exit(2)
939 John R Veregge - john@puente.jpl.nasa.gov
940 Advanced Engineering and Prototyping Group (AEG)
941 Information Systems Technology Section (395)
942 Jet Propulsion Lab - Calif Institute of Technology
944 *****************************************************************************/
947 XCreateInsensitivePixmap( Display
*display
, Pixmap pixmap
)
951 char stipple_data
[] =
953 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA,
954 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA,
955 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA,
956 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA
959 Pixmap ipixmap
, stipple
;
960 unsigned width
, height
, depth
;
962 Window window
; /* These return values */
963 unsigned border
; /* from XGetGeometry() */
964 int x
, y
; /* are not needed. */
968 if ( NULL
== display
|| 0 == pixmap
)
971 if ( 0 == XGetGeometry( display
, pixmap
, &window
, &x
, &y
,
972 &width
, &height
, &border
, &depth
)
974 return ipixmap
; /* BadDrawable: probably an invalid pixmap */
976 /* Get the stipple pixmap to be used to 'gray-out' the argument pixmap.
978 stipple
= XCreateBitmapFromData( display
, pixmap
, stipple_data
, 16, 16 );
981 gc
= XCreateGC( display
, pixmap
, (XtGCMask
)0, (XGCValues
*)NULL
);
984 /* Create an identical copy of the argument pixmap.
986 ipixmap
= XCreatePixmap( display
, pixmap
, width
, height
, depth
);
989 /* Copy the argument pixmap into the new pixmap.
991 XCopyArea( display
, pixmap
, ipixmap
,
992 gc
, 0, 0, width
, height
, 0, 0 );
994 /* Refill the new pixmap using the stipple algorithm/pixmap.
996 XSetStipple( display
, gc
, stipple
);
997 XSetFillStyle( display
, gc
, FillStippled
);
998 XFillRectangle( display
, ipixmap
, gc
, 0, 0, width
, height
);
1000 XFreeGC( display
, gc
);
1002 XFreePixmap( display
, stipple
);
1007 // Creates a bitmap with transparent areas drawn in
1008 // the given colour.
1009 wxBitmap
wxCreateMaskedBitmap(wxBitmap
& bitmap
, wxColour
& colour
)
1011 wxBitmap
newBitmap(bitmap
.GetWidth(),
1016 srcDC
.SelectObject(bitmap
);
1017 destDC
.SelectObject(newBitmap
);
1019 wxBrush
brush(colour
, wxSOLID
);
1020 destDC
.SetOptimization(FALSE
);
1021 destDC
.SetBackground(brush
);
1023 destDC
.Blit(0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), & srcDC
, 0, 0, wxCOPY
, TRUE
);