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