00001 #include "../../Library/SDK_Manila_Sense.h"
00002
00003 HFONT MyLabel::fontTitle=NULL;
00004 HFONT MyLabel::fontNormal=NULL;
00005 HFONT MyLabel::fontItalic=NULL;
00006 HFONT MyLabel::fontSmall=NULL;
00007
00008 void MyLabel::loadSurf(){
00009 if(render==NULL)
00010 setText(myText,false);
00011
00012 DWORD dwWidth = GetSystemMetrics(SM_CXSCREEN);
00013 DWORD dwHeight = GetSystemMetrics(SM_CYSCREEN);
00014 if(this->getRight()==dwHeight){
00015 this->setRight(dwWidth);
00016 }
00017 }
00018
00019 void MyLabel::changeColor(DWORD color,int type){
00020 if(type<LABEL_STATE_NORMAL||type>LABEL_STATE_SELECTED)
00021 return;
00022 if(myColor[type]!=color){
00023 myColor[type]=color;
00024 setMustRedraw();
00025 }
00026 };
00027
00028 void MyLabel::initComponnents(int type,DWORD color){
00029 isVisible=true;
00030
00031
00032 if(fontTitle==NULL){
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 HDC hdc=gi->backBuffer->deviceInterface;
00048
00049
00050 fontTitle = (HFONT) GetCurrentObject(hdc, OBJ_FONT);
00051
00052
00053 LOGFONT LogFont;
00054 if (GetObject(fontTitle, sizeof(LOGFONT), &LogFont) == 0)
00055 return;
00056
00057
00058 LogFont.lfHeight = -((10 * GetDeviceCaps(hdc, LOGPIXELSY))/ 72);
00059
00060 LogFont.lfOutPrecision=OUT_RASTER_PRECIS;
00061 LogFont.lfQuality=CLEARTYPE_QUALITY|ANTIALIASED_QUALITY;
00062 LogFont.lfPitchAndFamily=FF_ROMAN|VARIABLE_PITCH;
00063 LogFont.lfCharSet=DEFAULT_CHARSET;
00064
00065 wcsncpy(LogFont.lfFaceName,L"Tahoma\0",32);
00066
00067
00068 fontTitle = CreateFontIndirect(&LogFont);
00069 LogFont.lfHeight = -((9 * GetDeviceCaps(hdc, LOGPIXELSY))/ 72);
00070 fontNormal = CreateFontIndirect(&LogFont);
00071 LogFont.lfHeight = -((7 * GetDeviceCaps(hdc, LOGPIXELSY))/ 72);
00072 fontSmall = CreateFontIndirect(&LogFont);
00073 LogFont.lfHeight = -((9 * GetDeviceCaps(hdc, LOGPIXELSY))/ 72);
00074 LogFont.lfItalic=true;
00075 fontItalic = CreateFontIndirect(&LogFont);
00076 }
00077 isClickable=false;
00078 backGrnd=NULL;
00079 state=0;
00080 positionAbsolute.left=0;
00081 positionAbsolute.top=0;
00082 positionAbsolute.right=0;
00083 positionAbsolute.bottom=0;
00084
00085 previousColor=~color;
00086 myColor[LABEL_STATE_NORMAL]=color;
00087 myColor[LABEL_STATE_HOVER]=RGB(196,196,196);
00088 myColor[LABEL_STATE_DSBL]=RGB(128,128,128);
00089 myColor[LABEL_STATE_SELECTED]=RGB(196,196,196);
00090 render=NULL;
00091 switch(type){
00092 case 1:
00093 myFont=fontTitle;
00094 break;
00095 case 2:
00096 myFont=fontItalic;
00097 break;
00098 case 3:
00099 myFont=fontSmall;
00100 break;
00101 default:
00102 myFont=fontNormal;
00103 }
00104
00105
00106 loadSurf();
00107
00108 paddingTop=0;
00109 paddingLeft=0;
00110 mustErase=true;
00111 lastWidth=0;
00112 };
00113
00114 MyLabel::MyLabel(int idElement, int x,int y,wstring text,MyPictureBox* image,int padL,int fontType,DWORD color):Element(idElement, x,y,TYPE_LABEL)
00115 {
00116 myText=text;
00117 backColor=DirectDrawWarp::background_color;
00118 this->setRight(gi->rs.right);
00119 initComponnents(fontType,color);
00120
00121 state=LABEL_STATE_NORMAL;
00122 isClickable=true;
00123
00124 backGrnd=image;
00125 if(backGrnd!=NULL){
00126 if(sizeSurface.bottom<backGrnd->getBottom())
00127 this->setHeight(backGrnd->getHeight());
00128 if(sizeSurface.right<backGrnd->getRight())
00129 this->setWidth(backGrnd->getWidth());
00130 }else{
00131 this->setWidth(sizeSurface.right);
00132 this->setHeight(sizeSurface.bottom);
00133 }
00134 paddingLeft=padL*gi->percentOfVGA;
00135 }
00136
00137 MyLabel::MyLabel(int idElement, int x,int y,wstring text,DIRECTDRAW_GDI *bH,int fontType,DWORD color):Element(idElement, x,y,TYPE_LABEL)
00138 {
00139 myText=text;
00140 backColor=DirectDrawWarp::background_color;
00141 this->setRight(gi->rs.right);
00142 initComponnents(fontType,color);
00143
00144 state=LABEL_STATE_NORMAL;
00145 isClickable=true;
00146
00147 if(bH!=NULL){
00148 backGrnd=new MyPictureBox(-1,x,y,bH,4);
00149 if(sizeSurface.bottom<backGrnd->getBottom())
00150 this->setHeight(backGrnd->getHeight());
00151 if(sizeSurface.right<backGrnd->getRight())
00152 this->setWidth(backGrnd->getWidth());
00153 }else{
00154 this->setWidth(sizeSurface.right);
00155 this->setHeight(sizeSurface.bottom);
00156 }
00157 }
00158
00159 MyLabel::MyLabel(int idElement, int x,int y,wstring text,int fontType,DWORD color,bool rP,DWORD backC):Element(idElement, x,y,TYPE_LABEL,rP)
00160 {
00161 if(backC==1)
00162 backColor=DirectDrawWarp::background_color;
00163 else
00164 backColor=backC;
00165 myText=text;
00166 this->setRight(gi->rs.right);
00167 initComponnents(fontType,color);
00168 this->setWidth(sizeSurface.right);
00169 this->setHeight(sizeSurface.bottom);
00170 }
00171
00172 MyLabel::MyLabel(int idElement, int x,int y,int width,int height,wstring text,int fontType,bool centered,DWORD color):Element(idElement, x,y,TYPE_LABEL)
00173 {
00174 isCenterH=centered;
00175 isCenterV=centered;
00176 width=width*gi->percentOfVGA;
00177 height=height*gi->percentOfVGA;
00178 myText=text;
00179 backColor=DirectDrawWarp::background_color;
00180
00181 this->setWidth(width);
00182 this->setHeight(height);
00183
00184 initComponnents(fontType,color);
00185
00186
00187 this->setWidth(width);
00188
00189
00190 this->myText=text;
00191 if(centered)
00192 this->center();
00193 }
00194
00195
00196
00197
00198 void MyLabel::centerV(int top,int bottom){
00199 isCenterV=true;
00200 if(top==-1&&bottom==-1){
00201 top=getTop();
00202 bottom=getBottom();
00203 }
00204 int height=sizeSurface.bottom-sizeSurface.top;
00205 int wantedHeight=bottom-top;
00206 paddingTop=(wantedHeight-height)/2;
00207 };
00208 void MyLabel::centerH(int left,int right){
00209 isCenterH=true;
00210 int width=sizeSurface.right-sizeSurface.left;
00211 int wantedWidth=right-left;
00212 paddingLeft=(wantedWidth-width)/2;
00213 };
00214
00215 void MyLabel::center(){
00216 centerH(getLeft(),getRight());
00217 centerV(getTop(),getBottom());
00218 };
00219
00220 void MyLabel::setText(wstring nT,bool redraw){
00221 myText=nT;
00222 DirectDrawWarp::deleteSurf(&render);
00223
00224 render=new DIRECTDRAW_GDI;
00225 previousColor=myColor[state];
00226
00227 sizeSurface.top=0;
00228 sizeSurface.left=0;
00229 sizeSurface.bottom=1;
00230 sizeSurface.right=this->getWidth();
00231
00232 HDC hdc=gi->backBuffer->deviceInterface;
00233 HFONT hOldFont = (HFONT) SelectObject(hdc, myFont);
00234
00235 DrawText(hdc,myText.c_str(),myText.length(),&sizeSurface,DT_CALCRECT|DT_WORDBREAK);
00236 SelectObject(hdc, hOldFont);
00237 sizeSurface.right+=5;
00238
00239 DDSURFACEDESC ddsd;
00240 memset(&ddsd, 0, sizeof(ddsd));
00241 ddsd.dwSize = sizeof(ddsd);
00242 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
00243 ddsd.dwWidth = sizeSurface.right;
00244 if(ddsd.dwWidth<16) ddsd.dwWidth=16;
00245 ddsd.dwHeight = sizeSurface.bottom;
00246 if(ddsd.dwHeight<16) ddsd.dwHeight=16;
00247
00248 HRESULT hRet = DirectDrawWarp::createSurf(&ddsd, render);
00249 if (hRet != DD_OK)
00250 LocalisedString::explainFailDirectDraw(hRet,L"MyLabel:setText CreateSurface");
00251
00252
00253 DDBLTFX ddbltfx;
00254 memset(&ddbltfx, 0, sizeof(ddbltfx));
00255 ddbltfx.dwSize = sizeof(ddbltfx);
00256 ddbltfx.dwFillColor = RGB(0,0,0);
00257 gi->window->BltFast(render,NULL, NULL,NULL, DDBLT_COLORFILL | DDBLT_WAITNOTBUSY, &ddbltfx);
00258
00259 hdc=render->deviceInterface;
00260
00261 hOldFont = (HFONT) SelectObject(hdc, myFont);
00262
00263 SetTextColor(hdc,0x00FFFFFF);
00264 SetBkColor(hdc,0);
00265
00266
00267 RECT pos={0,0,this->getWidth(),this->getHeight()};
00268 UINT flags=DT_WORDBREAK;
00269 if(isCenterH)
00270 flags=flags|DT_CENTER;
00271 DrawText(hdc,myText.c_str(),myText.length(),&sizeSurface,flags);
00272
00273 computeRealAlpha(previousColor);
00274 SelectObject(hdc,hOldFont);
00275
00276 if(owner!=NULL)
00277 owner->sizeChanged(this,sizeSurface.right,sizeSurface.bottom);
00278 if(sizeSurface.right>getWidth())
00279 this->setWidth(sizeSurface.right);
00280 if(sizeSurface.bottom>getHeight())
00281 this->setHeight(sizeSurface.bottom);
00282 if(redraw)
00283 this->setMustRedraw();
00284 mustErase=true;
00285 if(lastWidth<sizeSurface.right)
00286 lastWidth=sizeSurface.right;
00287 };
00288
00289
00290 MyLabel::~MyLabel(void)
00291 {
00292 DirectDrawWarp::deleteSurf(&render);
00293 }
00294
00295 Element* MyLabel::isClicked(int x,int y){
00296 if(isClickable){
00297 if(positionAbsolute.right==positionAbsolute.bottom&&
00298 positionAbsolute.bottom==0)
00299 return NULL;
00300
00301 positionAbsolute.right=positionAbsolute.left+this->getWidth();
00302 if(x>positionAbsolute.left-5&&x<positionAbsolute.right+5&&y>positionAbsolute.top-10-gi->rcVisibleDesktopTOP.top&&
00303 y<positionAbsolute.top+this->getHeight()-gi->rcVisibleDesktopTOP.top){
00304 this->isActivated=true;
00305 if(this->state==LABEL_STATE_NORMAL||this->state==LABEL_STATE_SELECTED){
00306 this->state=LABEL_STATE_HOVER;
00307 setMustRedraw();
00308 }
00309 return this;
00310 }
00311 }
00312 return NULL;
00313 };
00314 bool MyLabel::run(bool isRightClicked){
00315 Element::run(isRightClicked);
00316
00317 bool redraw=this->state==LABEL_STATE_HOVER;
00318 this->state=LABEL_STATE_SELECTED;
00319
00320 if(redraw)
00321 setMustRedraw();
00322 return false;
00323 };
00324
00325 void MyLabel::unClick(){
00326 if(this->state==LABEL_STATE_SELECTED||this->state==LABEL_STATE_HOVER)
00327 setMustRedraw();
00328 this->state=LABEL_STATE_NORMAL;
00329 };
00330
00331 void MyLabel::paintColor(DIRECTDRAW_GDI* lpDDraw,int x,int y,COLORREF color,COLORREF background){
00332
00333
00334
00335 positionAbsolute.top=this->getTop()+paddingTop+y;
00336 positionAbsolute.bottom=positionAbsolute.top+sizeSurface.bottom;
00337 positionAbsolute.left=this->getLeft()+paddingLeft+x;
00338 positionAbsolute.right=positionAbsolute.left+sizeSurface.right;
00339
00340 int posTop=positionAbsolute.top;
00341 int posBottom=positionAbsolute.bottom;
00342 if(backGrnd!=NULL){
00343 posTop=this->getTop()+y;
00344 posBottom=posTop+backGrnd->getHeight();
00345 }
00346
00347
00348
00349 if(positionAbsolute.top>=gi->rcUpdateRegion.bottom||positionAbsolute.bottom<=gi->rcUpdateRegion.top||
00350 positionAbsolute.left>=gi->rcUpdateRegion.right||positionAbsolute.right<=gi->rcUpdateRegion.left)
00351 return;
00352
00353
00354
00355 if(positionAbsolute.top<gi->rcUpdateRegion.top){
00356 clipping.top=gi->rcUpdateRegion.top-positionAbsolute.top;
00357 positionAbsolute.top=gi->rcUpdateRegion.top;
00358 }else{
00359 clipping.top=0;
00360 }
00361 if(positionAbsolute.bottom>gi->rcUpdateRegion.bottom){
00362 clipping.bottom=sizeSurface.bottom-(positionAbsolute.bottom-gi->rcUpdateRegion.bottom);
00363 positionAbsolute.bottom=gi->rcUpdateRegion.bottom;
00364 }else{
00365 clipping.bottom=sizeSurface.bottom;
00366 }
00367 if(positionAbsolute.left<gi->rcUpdateRegion.left){
00368 clipping.left=gi->rcUpdateRegion.left-positionAbsolute.left;
00369 positionAbsolute.left=gi->rcUpdateRegion.left;
00370 }else{
00371 clipping.left=0;
00372 }
00373 if(positionAbsolute.right>gi->rcUpdateRegion.right){
00374 clipping.right=sizeSurface.right-(positionAbsolute.right-gi->rcUpdateRegion.right);
00375 positionAbsolute.right=gi->rcUpdateRegion.right;
00376 }else{
00377 clipping.right=sizeSurface.right;
00378 }
00379
00380 HRESULT hres;
00381 if(isClickable){
00382 if(backGrnd!=NULL){
00383 backGrnd->setNumImage(state);
00384
00385 backGrnd->paint(lpDDraw,x,y);
00386 }
00387 }
00388
00389 if(clipping.right-clipping.left>0&&clipping.bottom-clipping.top>0){
00390 bool shouldRepaint=false;
00391 if(color!=previousColor){
00392 myColor[state]=color;
00393 this->setText(myText,false);
00394 }
00395
00396
00397 hres= gi->window->BltFast(lpDDraw,&positionAbsolute, render,&clipping,NULL,NULL);
00398 if (hres != DD_OK)
00399 LocalisedString::explainFailDirectDraw(hres,L"MyLabel Blt(previousColor);");
00400 }
00401 };
00402 void MyLabel::computeRealAlpha( COLORREF color )
00403 {
00404
00405 SelectObject(render->deviceInterface, render->oldObj);
00406
00407 BITMAP bmData;
00408 GetObject(render->bitmap, sizeof(bmData), &bmData);
00409
00410 if(bmData.bmBits==NULL){
00411
00412 Element::ConvertToDIB(render->bitmap);
00413 GetObject(render->bitmap, sizeof(bmData), &bmData);
00414 }
00415
00416 DWORD alphaValue=0;
00417 if(bmData.bmBitsPixel>16){
00418
00419 return;
00420 }
00421
00422 PixelFormat pixelFormat;
00423 BitmapData bmDataRGBA = {0};
00424 int nStride = bmData.bmWidth * 4;
00425
00426 bmDataRGBA.Width = bmData.bmWidth;
00427 bmDataRGBA.Height = bmData.bmHeight;
00428 bmDataRGBA.Stride = nStride;
00429 int widthS=bmDataRGBA.Stride/4;
00430 bmDataRGBA.PixelFormat=PixelFormat32bppARGB;
00431
00432 const WCHAR* tmp=myText.c_str();
00433
00434 DWORD* bitsRGBA=new DWORD[widthS*bmDataRGBA.Height];
00435
00436 uchar* bits=((uchar*)bmData.bmBits);
00437 DWORD alphaMix=0;
00438 WORD red,green,blue,alphaPreMult;
00439 red=((color&0x00FF0000)>>16);
00440 green=((color&0x0000FF00)>>8);
00441 blue=((color&0x000000FF));
00442 for (int i=0;i<bmData.bmHeight;i++)
00443 {
00444 for(int j=0;j<widthS;j++){
00445 alphaValue=*((WORD*)(&bits[i*bmData.bmWidthBytes+j*sizeof(WORD)]));
00446 if(alphaValue!=0){
00447 alphaPreMult=((((DWORD)(0x7C00&alphaValue)) >> 10)+(((DWORD)(0x03E0&alphaValue)) >>5)+(((DWORD)(0x001F&alphaValue))))/3.0;
00448 alphaPreMult=alphaPreMult<<3;
00449 alphaMix=alphaPreMult<<24;
00450 bitsRGBA[i*bmData.bmWidth+j]=alphaMix|RGB(red*alphaPreMult/0xFF,green*alphaPreMult/0xFF,blue*alphaPreMult/0xFF);
00451 }else
00452 bitsRGBA[i*bmData.bmWidth+j]=(DWORD)0;
00453 }
00454 }
00455 bmDataRGBA.Scan0 = bitsRGBA;
00456
00457 DirectDrawWarp::deleteSurf(&render);
00458 render=new DIRECTDRAW_GDI;
00459 render->directDrawSurf=NULL;
00460
00461 render->bitmap = CreateBitmap( bmDataRGBA.Width, bmDataRGBA.Height, 1, GetPixelFormatSize( bmDataRGBA.PixelFormat ), bmDataRGBA.Scan0 );
00462 render->nbBPP=32;
00463
00464 render->deviceInterface = CreateCompatibleDC(NULL);
00465 if (!render->deviceInterface) {
00466 LocalisedString::explainFail(LocalisedString::szGetDC,L"computeRealAlpha: CreateCompatibleDC(NULL);");
00467 return;
00468 }
00469 render->oldObj=SelectObject(render->deviceInterface, render->bitmap);
00470 delete []bitsRGBA;
00471 }
00472
00473 void MyLabel::paint(DIRECTDRAW_GDI* lpDDraw,int x,int y){
00474 if(!isVisible)
00475 return;
00476 this->paintColor(lpDDraw,x,y,myColor[state],NULL);
00477
00478
00479 positionAbsolute.top=this->getTop()+y;
00480 positionAbsolute.left=this->getLeft()+x;
00481 };
00482
00483
00484 void MyLabel::setBkg(DIRECTDRAW_GDI *surf){
00485 if(backGrnd!=NULL)
00486 backGrnd->changeImg(surf);
00487 else{
00488 backGrnd=NULL;
00489 }
00490 };
00491
00492 void MyLabel::changeVisibility(bool vis,bool eraseBckground){
00493 if(isVisible!=vis){
00494 isVisible=vis;
00495 if(vis)
00496 vis=false;
00497 positionAbsolute.right=positionAbsolute.left+this->getWidth()+paddingLeft;
00498 if(positionAbsolute.right>GetSystemMetrics(SM_CXSCREEN))
00499 positionAbsolute.right=GetSystemMetrics(SM_CXSCREEN);
00500 if(positionAbsolute.left<0)
00501 positionAbsolute.left=0;
00502 positionAbsolute.bottom=positionAbsolute.top+this->getHeight()+paddingTop;
00503 if(positionAbsolute.bottom>GetSystemMetrics(SM_CYSCREEN))
00504 positionAbsolute.bottom=GetSystemMetrics(SM_CYSCREEN);
00505 if(positionAbsolute.top<gi->rs.top)
00506 positionAbsolute.top=gi->rs.top;
00507 if(positionAbsolute.bottom-positionAbsolute.top>0&&
00508 positionAbsolute.right-positionAbsolute.left>0){
00509 gi->window->addToRedraw(positionAbsolute,eraseBckground);
00510 }
00511 }
00512 };