1 #include <iostream>
2 #include "wmimage.h"
3 #include "wmwindow.h"
4 #include "wmapp.h"
5 
6 using std::cerr;
7 using std::endl;
8 
9 // functions for WMImage -------------------------------------------------
10 
WMImage(const WMPixmap * pm)11 WMImage::WMImage(const WMPixmap *pm)
12 : wBGColor(WMColor(Background)) { seticon(pm); }
13 
WMImage(const WMPixmap & pm)14 WMImage::WMImage(const WMPixmap &pm)
15 : wBGColor(WMColor(Background)) { seticon(pm); }
16 
WMImage(char * xpm[])17 WMImage::WMImage(char *xpm[])
18 : wBGColor(WMColor(Background)) { seticon(xpm); }
19 
~WMImage()20 WMImage::~WMImage()
21 { if (icon()) WMApp::Xw.free_pixmap(wIcon); }
22 
23 void
seticon(bool dodisplay)24 WMImage::seticon(bool dodisplay)
25 {
26   if (icon()) WMApp::Xw.free_pixmap(wIcon);
27   if(WMApp::Xw.create_pixmap(wIcon, b_width(), b_height()))
28   {
29     WMApp::Xw.fill_rectangle(wIcon, 0, 0, b_width(), b_height(), bgcolor());
30     wIconPtr = &wIcon;
31     if (dodisplay) display();
32   }
33   else wIconPtr = 0;
34 }
35 
36 void
seticon(const WMPixmap & pm,bool dodisplay)37 WMImage::seticon(const WMPixmap &pm, bool dodisplay)
38 {
39   if (icon()) WMApp::Xw.free_pixmap(wIcon);
40   if (WMApp::Xw.create_pixmap(wIcon, pm))
41     { wIconPtr = &wIcon; if (dodisplay) display(); }
42   else wIconPtr = 0;
43 }
44 
45 void
seticon(char * xpm[],bool dodisplay)46 WMImage::seticon(char *xpm[], bool dodisplay)
47 {
48   if (icon()) WMApp::Xw.free_pixmap(wIcon);
49   if (WMApp::Xw.create_pixmap(wIcon, xpm))
50     { wIconPtr = &wIcon; if (dodisplay) display(); }
51   else wIconPtr = 0;
52 }
53 
54 void
real_display()55 WMImage::real_display()
56 {
57   const int imgwidth = icon() ? icon()->attr.width : 0;
58   const int imgheight = icon() ? icon()->attr.height : 0;
59 
60   // if no icon, create an icon that consists only of the background color
61   if (! icon())
62     seticon(false);
63 
64   // if widget is not big enough to hold icon, complain about it
65   else if (imgwidth > b_width() || imgheight > b_height()) {
66     cerr << "WMError: Image " << this << " too small for icon" << endl;
67     return;
68   }
69 
70   // if icon is smaller than widget, color in the rest with background color
71   else if (imgwidth < b_width() || imgheight < b_height()) {
72     WMPixmap temp;
73 
74     // Blit background color onto rectangle
75     WMApp::Xw.create_pixmap(temp, b_width(), b_height());
76     WMApp::Xw.fill_rectangle(temp, 0, 0, b_width(), b_height(), bgcolor());
77     // center icon onto background
78     WMApp::Xw.copy_rectangle(*icon(), temp, 0, 0, imgwidth, imgheight,
79 		     (b_width() - imgwidth) / 2, (b_height() - imgheight) / 2);
80     // swap pixmaps
81     WMApp::Xw.free_pixmap(wIcon);
82     WMApp::Xw.create_pixmap(wIcon, temp);
83     WMApp::Xw.free_pixmap(temp);
84   }
85 
86   // if we get here, widget (minus border) and icon are the same size -
87   // hallelujah!
88   WMApp::Xw.copy_rectangle(*icon(), window()->pixmap(), 0, 0,
89 		  	   b_width(), b_height(), b_left(), b_top());
90 }
91 
92