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,2005 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: FXImageFrame.cpp,v 1.10 2005/01/16 16:06:07 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 "FXImage.h"
39 #include "FXImageFrame.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(FXImageFrame) FXImageFrameMap[]={
61   FXMAPFUNC(SEL_PAINT,0,FXImageFrame::onPaint),
62   };
63 
64 
65 // Object implementation
FXIMPLEMENT(FXImageFrame,FXFrame,FXImageFrameMap,ARRAYNUMBER (FXImageFrameMap))66 FXIMPLEMENT(FXImageFrame,FXFrame,FXImageFrameMap,ARRAYNUMBER(FXImageFrameMap))
67 
68 
69 // Deserialization
70 FXImageFrame::FXImageFrame(){
71   image=NULL;
72   }
73 
74 
75 // Construct it
FXImageFrame(FXComposite * p,FXImage * img,FXuint opts,FXint x,FXint y,FXint w,FXint h,FXint pl,FXint pr,FXint pt,FXint pb)76 FXImageFrame::FXImageFrame(FXComposite* p,FXImage *img,FXuint opts,FXint x,FXint y,FXint w,FXint h,FXint pl,FXint pr,FXint pt,FXint pb):
77   FXFrame(p,opts,x,y,w,h,pl,pr,pt,pb){
78   image=img;
79   }
80 
81 
82 // Create it all
create()83 void FXImageFrame::create(){
84   FXFrame::create();
85   if(image) image->create();
86   }
87 
88 
89 // Get default width
getDefaultWidth()90 FXint FXImageFrame::getDefaultWidth(){
91   register FXint w=0;
92   if(image) w=image->getWidth();
93   return w+padleft+padright+(border<<1);
94   }
95 
96 
97 // Get default height
getDefaultHeight()98 FXint FXImageFrame::getDefaultHeight(){
99   register FXint h=0;
100   if(image) h=image->getHeight();
101   return h+padtop+padbottom+(border<<1);
102   }
103 
104 
105 // Draw the image
onPaint(FXObject *,FXSelector,void * ptr)106 long FXImageFrame::onPaint(FXObject*,FXSelector,void* ptr){
107   FXEvent *ev=(FXEvent*)ptr;
108   FXDCWindow dc(this,ev);
109   FXint imgx,imgy,imgw,imgh;
110   dc.setForeground(backColor);
111   if(image){
112     imgw=image->getWidth();
113     imgh=image->getHeight();
114     if(options&JUSTIFY_LEFT) imgx=padleft+border;
115     else if(options&JUSTIFY_RIGHT) imgx=width-padright-border-imgw;
116     else imgx=border+padleft+(width-padleft-padright-(border<<1)-imgw)/2;
117     if(options&JUSTIFY_TOP) imgy=padtop+border;
118     else if(options&JUSTIFY_BOTTOM) imgy=height-padbottom-border-imgh;
119     else imgy=border+padtop+(height-padbottom-padtop-(border<<1)-imgh)/2;
120     dc.fillRectangle(border,border,imgx-border,height-(border<<1));
121     dc.fillRectangle(imgx+imgw,border,width-border-imgx-imgw,height-(border<<1));
122     dc.fillRectangle(imgx,border,imgw,imgy-border);
123     dc.fillRectangle(imgx,imgy+imgh,imgw,height-border-imgy-imgh);
124     dc.drawImage(image,imgx,imgy);
125     }
126   else{
127     dc.fillRectangle(border,border,width-(border<<1),height-(border<<1));
128     }
129   drawFrame(dc,0,0,width,height);
130   return 1;
131   }
132 
133 
134 
135 // Change image
setImage(FXImage * img)136 void FXImageFrame::setImage(FXImage* img){
137   image=img;
138   recalc();
139   update();
140   }
141 
142 
143 // Set text justify style
setJustify(FXuint style)144 void FXImageFrame::setJustify(FXuint style){
145   FXuint opts=(options&~JUSTIFY_MASK) | (style&JUSTIFY_MASK);
146   if(options!=opts){
147     options=opts;
148     update();
149     }
150   }
151 
152 
153 // Get text justify style
getJustify() const154 FXuint FXImageFrame::getJustify() const {
155   return (options&JUSTIFY_MASK);
156   }
157 
158 
159 
160 // Save data
save(FXStream & store) const161 void FXImageFrame::save(FXStream& store) const {
162   FXFrame::save(store);
163   store << image;
164   }
165 
166 
167 // Load data
load(FXStream & store)168 void FXImageFrame::load(FXStream& store){
169   FXFrame::load(store);
170   store >> image;
171   }
172 
173 
174 // Destructor
~FXImageFrame()175 FXImageFrame::~FXImageFrame(){
176   image=(FXImage*)-1L;
177   }
178 
179 }
180