1 /********************************************************************************
2 *                                                                               *
3 *                       I m a g e   F r a m e   W i d g e t                     *
4 *                                                                               *
5 *********************************************************************************
6 * Copyright (C) 2001,2021 by H. J. Daniel III. All Rights Reserved.             *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify          *
9 * it under the terms of the GNU Lesser General Public License as published by   *
10 * the Free Software Foundation; either version 3 of the License, or             *
11 * (at your option) any later version.                                           *
12 *                                                                               *
13 * This library is distributed in the hope that it will be useful,               *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
16 * GNU Lesser General Public License for more details.                           *
17 *                                                                               *
18 * You should have received a copy of the GNU Lesser General Public License      *
19 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
20 ********************************************************************************/
21 #include "xincs.h"
22 #include "fxver.h"
23 #include "fxdefs.h"
24 #include "fxmath.h"
25 #include "FXArray.h"
26 #include "FXHash.h"
27 #include "FXMutex.h"
28 #include "FXStream.h"
29 #include "FXString.h"
30 #include "FXSize.h"
31 #include "FXPoint.h"
32 #include "FXRectangle.h"
33 #include "FXStringDictionary.h"
34 #include "FXSettings.h"
35 #include "FXRegistry.h"
36 #include "FXEvent.h"
37 #include "FXWindow.h"
38 #include "FXDCWindow.h"
39 #include "FXApp.h"
40 #include "FXAccelTable.h"
41 #include "FXBitmap.h"
42 #include "FXBitmapFrame.h"
43 
44 
45 /*
46   Notes:
47   - Hacked around so as to support padding also.
48   - Fixed layout to center image if frame is larger.
49   - Fixed serialization also.
50   - Now supports various justification modes
51 */
52 
53 
54 #define JUSTIFY_MASK    (JUSTIFY_HZ_APART|JUSTIFY_VT_APART)
55 
56 using namespace FX;
57 
58 /*******************************************************************************/
59 
60 namespace FX {
61 
62 // Map
63 FXDEFMAP(FXBitmapFrame) FXBitmapFrameMap[]={
64   FXMAPFUNC(SEL_PAINT,0,FXBitmapFrame::onPaint),
65   };
66 
67 
68 // Object implementation
FXIMPLEMENT(FXBitmapFrame,FXFrame,FXBitmapFrameMap,ARRAYNUMBER (FXBitmapFrameMap))69 FXIMPLEMENT(FXBitmapFrame,FXFrame,FXBitmapFrameMap,ARRAYNUMBER(FXBitmapFrameMap))
70 
71 
72 // Deserialization
73 FXBitmapFrame::FXBitmapFrame(){
74   bitmap=NULL;
75   onColor=0;
76   offColor=0;
77   }
78 
79 
80 // Construct it
FXBitmapFrame(FXComposite * p,FXBitmap * bmp,FXuint opts,FXint x,FXint y,FXint w,FXint h,FXint pl,FXint pr,FXint pt,FXint pb)81 FXBitmapFrame::FXBitmapFrame(FXComposite* p,FXBitmap *bmp,FXuint opts,FXint x,FXint y,FXint w,FXint h,FXint pl,FXint pr,FXint pt,FXint pb):
82   FXFrame(p,opts,x,y,w,h,pl,pr,pt,pb){
83   bitmap=bmp;
84   onColor=FXRGB(0,0,0);
85   offColor=backColor;
86   }
87 
88 
89 // Create it all
create()90 void FXBitmapFrame::create(){
91   FXFrame::create();
92   if(bitmap) bitmap->create();
93   }
94 
95 
96 // Get default width
getDefaultWidth()97 FXint FXBitmapFrame::getDefaultWidth(){
98   FXint w=0;
99   if(bitmap) w=bitmap->getWidth();
100   return w+padleft+padright+(border<<1);
101   }
102 
103 
104 // Get default height
getDefaultHeight()105 FXint FXBitmapFrame::getDefaultHeight(){
106   FXint h=0;
107   if(bitmap) h=bitmap->getHeight();
108   return h+padtop+padbottom+(border<<1);
109   }
110 
111 
112 // Draw the image
onPaint(FXObject *,FXSelector,void * ptr)113 long FXBitmapFrame::onPaint(FXObject*,FXSelector,void* ptr){
114   FXEvent *ev=(FXEvent*)ptr;
115   FXDCWindow dc(this,ev);
116   FXint imgx,imgy,imgw,imgh;
117   dc.setForeground(backColor);
118   if(bitmap){
119     imgw=bitmap->getWidth();
120     imgh=bitmap->getHeight();
121     if(options&JUSTIFY_LEFT) imgx=padleft+border;
122     else if(options&JUSTIFY_RIGHT) imgx=width-padright-border-imgw;
123     else imgx=border+padleft+(width-padleft-padright-(border<<1)-imgw)/2;
124     if(options&JUSTIFY_TOP) imgy=padtop+border;
125     else if(options&JUSTIFY_BOTTOM) imgy=height-padbottom-border-imgh;
126     else imgy=border+padtop+(height-padbottom-padtop-(border<<1)-imgh)/2;
127     dc.fillRectangle(border,border,imgx-border,height-(border<<1));
128     dc.fillRectangle(imgx+imgw,border,width-border-imgx-imgw,height-(border<<1));
129     dc.fillRectangle(imgx,border,imgw,imgy-border);
130     dc.fillRectangle(imgx,imgy+imgh,imgw,height-border-imgy-imgh);
131     dc.setForeground(onColor);
132     dc.setBackground(offColor);
133     dc.drawBitmap(bitmap,imgx,imgy);
134     }
135   else{
136     dc.fillRectangle(border,border,width-(border<<1),height-(border<<1));
137     }
138   drawFrame(dc,0,0,width,height);
139   return 1;
140   }
141 
142 
143 
144 // Change image
setBitmap(FXBitmap * bmp)145 void FXBitmapFrame::setBitmap(FXBitmap* bmp){
146   bitmap=bmp;
147   recalc();
148   update();
149   }
150 
151 
152 // Set on color
setOnColor(FXColor clr)153 void FXBitmapFrame::setOnColor(FXColor clr){
154   if(clr!=onColor){
155     onColor=clr;
156     update();
157     }
158   }
159 
160 
161 // Set off color
setOffColor(FXColor clr)162 void FXBitmapFrame::setOffColor(FXColor clr){
163   if(clr!=offColor){
164     offColor=clr;
165     update();
166     }
167   }
168 
169 
170 
171 // Set text justify style
setJustify(FXuint style)172 void FXBitmapFrame::setJustify(FXuint style){
173   FXuint opts=(options&~JUSTIFY_MASK) | (style&JUSTIFY_MASK);
174   if(options!=opts){
175     options=opts;
176     update();
177     }
178   }
179 
180 
181 // Get text justify style
getJustify() const182 FXuint FXBitmapFrame::getJustify() const {
183   return (options&JUSTIFY_MASK);
184   }
185 
186 
187 
188 // Save data
save(FXStream & store) const189 void FXBitmapFrame::save(FXStream& store) const {
190   FXFrame::save(store);
191   store << bitmap;
192   }
193 
194 
195 // Load data
load(FXStream & store)196 void FXBitmapFrame::load(FXStream& store){
197   FXFrame::load(store);
198   store >> bitmap;
199   }
200 
201 
202 // Destructor
~FXBitmapFrame()203 FXBitmapFrame::~FXBitmapFrame(){
204   bitmap=(FXBitmap*)-1L;
205   }
206 
207 }
208