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