1 // Copyright (c) 1996-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
19 // include windows.h first to have all definitions available
23 #include <WNT_IconBox.ixx>
25 #include <WNT_WClass.hxx>
26 #include <WNT_ImageManager.hxx>
27 #include <WNT_Icon.hxx>
30 static void __fastcall _get_filename ( char*, int, char*, char* );
32 HBITMAP LoadImageFromFile ( Handle( WNT_GraphicDevice )&, char*, HDC = NULL );
33 int SaveBitmapToFile (
34 Handle( WNT_GraphicDevice )& gDev,
35 HBITMAP, char*, int, int, int, int
37 LRESULT CALLBACK WNT_IconBoxWndProc ( HWND, UINT, WPARAM, LPARAM );
39 //*************************** Constructor *******************************//
41 WNT_IconBox :: WNT_IconBox (
42 const Handle( WNT_GraphicDevice )& aDevice,
43 const Standard_CString aName,
44 const WNT_Dword& aStyle,
45 const Standard_Real Xc,
46 const Standard_Real Yc,
47 const Quantity_NameOfColor aBkColor
51 "WNT_IconBoxClass", WNT_IconBoxWndProc,
52 CS_HREDRAW | CS_VREDRAW
55 Xc, Yc, 0.5, 0.2, aBkColor
58 if ( LoadIcons ( aName ) == 0 )
60 Aspect_WindowDefinitionError :: Raise ( "Unable to load icons" );
66 myPen = CreatePen ( PS_SOLID, 3, RGB( 0, 255, 0 ) );
70 //********************************* Destroy *****************************//
72 void WNT_IconBox :: Destroy () {
76 DeleteObject ( myFont );
80 DeleteObject ( myPen );
82 } // end WNT_IconBox :: Destroy
84 //******************************* LoadIcons *****************************//
86 Standard_Integer WNT_IconBox :: LoadIcons (
87 const Standard_CString Name
91 Standard_Integer retVal = 0;
93 char fileName[ MAX_PATH ];
94 char fName[ MAX_PATH ];
95 char iName[ MAX_PATH ];
96 Standard_Integer iHCode;
98 Handle( WNT_Icon ) icon;
100 Handle( WNT_GraphicDevice ) gDev = Handle( WNT_GraphicDevice ) ::
101 DownCast ( GraphicDevice () );
103 _get_filename ( fileName, MAX_PATH, (Standard_PCharacter)Name, "ifd" );
105 is.open ( fileName );
109 while ( !is.eof () ) {
111 is >> fName >> iName;
113 if ( is.bad () ) break;
115 if ( fName[ 0 ] == '\x00' || iName[ 0 ] == '\x00' ) continue;
117 if ( fName[ 0 ] == '#' ) continue;
119 _get_filename ( fileName, MAX_PATH, fName, "" );
121 iHCode = myImages -> StringHashCode ( fileName );
122 len = myImages -> Size ();
125 for ( i = 1; i <= len; ++i )
127 if ( myImages -> HashCode ( i ) == iHCode ) {
129 icon = Handle( WNT_Icon ) :: DownCast ( myImages -> Image ( i ) );
131 if ( lstrcmp ( iName, icon -> myName ) ) {
142 hBmp = LoadImageFromFile ( gDev, fileName );
146 icon = new WNT_Icon ( iName, hBmp, iHCode );
147 myImages -> Add ( icon );
151 } // end if ( i > len . . . )
153 fName[ 0 ] = iName[ 0 ] = '\x00';
157 retVal = myImages -> Size ();
163 } // end WNT_IconBox :: LoadIcons
165 //********************************** Show *******************************//
167 void WNT_IconBox :: Show () const {
171 } // end WNT_IconBox :: Show
173 //******************************* UnloadIcons ***************************//
175 Standard_Integer WNT_IconBox :: UnloadIcons (
176 const Standard_CString Name
179 Standard_Integer retVal = 0;
181 char fileName[ MAX_PATH ];
182 char fName[ MAX_PATH ];
183 char iName[ MAX_PATH ];
184 Handle( WNT_Icon ) icon;
186 Handle( WNT_GraphicDevice ) gDev = Handle( WNT_GraphicDevice ) ::
187 DownCast ( GraphicDevice () );
189 _get_filename ( fileName, MAX_PATH, (Standard_PCharacter)Name, "ifd" );
191 is.open ( fileName );
195 while ( !is.eof () ) {
197 is >> fName >> iName;
199 if ( is.bad () ) break;
201 if ( fName[ 0 ] == '#' ) continue;
203 for ( int i = 1; i <= myImages -> Size (); ++i ) {
205 icon = Handle( WNT_Icon ) :: DownCast ( myImages -> Image ( i ) );
207 if ( icon.IsNull () ) continue;
209 if ( lstrcmp ( icon -> myName, iName ) ) continue;
211 myImages -> Delete ( i );
222 } // end WNT_IconBox :: UnloadIcons
224 //********************************* AddIcon *****************************//
226 void WNT_IconBox :: AddIcon (
227 const Handle( WNT_Window )& W,
228 const Standard_CString Name,
229 const Standard_Integer aWidth,
230 const Standard_Integer aHeight
233 Handle( WNT_Icon ) icon = W -> myIcon;
235 if ( !icon.IsNull () )
237 myImages -> Add ( W -> myIcon );
239 } // end WNT_IconBox :: AddIcon
241 //********************************* SaveIcons ***************************//
243 Standard_Integer WNT_IconBox :: SaveIcons () const {
246 Standard_Integer retVal = 0;
248 char fName[ MAX_PATH ];
249 Handle( WNT_Icon ) icon;
251 Handle( WNT_GraphicDevice ) gDev = Handle( WNT_GraphicDevice ) ::
252 DownCast ( GraphicDevice () );
254 switch ( myFormat ) {
258 strcpy ( ext, ".xwd" );
264 strcpy ( ext, ".bmp" );
270 strcpy ( ext, ".gif" );
274 for ( int i = 1; i <= myImages -> Size (); ++i ) {
276 icon = Handle( WNT_Icon ) :: DownCast ( myImages -> Image ( i ) );
278 if ( icon.IsNull () ) continue;
280 strcpy ( fName, icon -> myName );
282 if ( strchr ( fName, '.' ) == NULL )
284 strcat ( fName, ext );
286 myImages -> Dim ( i, w, h );
288 if ( SaveBitmapToFile (
289 gDev, ( HBITMAP )( myImages -> ImageHandle ( i ) ), fName,
300 } // end WNT_IconBox :: SaveIcons
302 //********************************* IconNumber **************************//
304 Standard_Integer WNT_IconBox :: IconNumber () const {
306 return myImages -> Size ();
308 } // end WNT_IconBox :: IconNumber
310 //********************************* IconName ****************************//
312 Standard_CString WNT_IconBox :: IconName (
313 const Standard_Integer Index
316 Handle( WNT_Icon ) icon = Handle( WNT_Icon ) :: DownCast (
317 myImages -> Image ( Index )
320 return icon -> myName;
322 } // end WNT_IconBox :: IconName
324 //********************************* IconSize ****************************//
326 Standard_Boolean WNT_IconBox :: IconSize (
327 const Standard_CString Name,
328 Standard_Integer& Width,
329 Standard_Integer& Height
332 int i, len = myImages -> Size ();
333 Handle( WNT_Icon ) icon;
335 for ( i = 1; i <= len; ++i ) {
337 icon = Handle( WNT_Icon ) :: DownCast ( myImages -> Image ( i ) );
339 if ( !strcmp ( icon -> myName, Name ) ) break;
343 return ( i > len ) ? Standard_False :
344 myImages -> Dim ( i, Width, Height ), Standard_True;
346 } // end WNT_IconBox :: IconSize
348 //********************************* IconPixmap (1) **********************//
350 Aspect_Handle WNT_IconBox :: IconPixmap (
351 const Standard_CString Name
354 int i, len = myImages -> Size ();
355 Handle( WNT_Icon ) icon;
357 for ( i = 1; i <= len; ++i ) {
359 icon = Handle( WNT_Icon ) :: DownCast ( myImages -> Image ( i ) );
361 if ( !strcmp ( icon -> myName, Name ) ) break;
365 return ( i > len ) ? NULL : myImages -> ImageHandle ( i );
367 } // end WNT_IconBox :: IconPixmap
369 //********************************* IconPixmap (2) **********************//
371 Aspect_Handle WNT_IconBox :: IconPixmap (
372 const Standard_CString Name,
373 const Standard_Integer Width,
374 const Standard_Integer Height
377 HBITMAP retVal = NULL;
378 int len = myImages -> Size ();
380 Handle( WNT_Icon ) icon;
383 Handle( WNT_GraphicDevice ) gDev = Handle( WNT_GraphicDevice ) ::
384 DownCast ( GraphicDevice () );
386 for ( i = 1; i <= len; ++i ) {
388 icon = Handle( WNT_Icon ) :: DownCast ( myImages -> Image ( i ) );
390 if ( !strcmp ( icon -> myName, Name ) ) break;
396 HDC hDC, hDCmemSrc, hDCmemDst;
398 hDC = GetDC ( NULL );
400 if ( gDev -> IsPaletteDevice () ) {
402 hOldPal = SelectPalette ( hDC, ( HPALETTE )( gDev -> HPalette () ), FALSE );
403 RealizePalette ( hDC );
407 hDCmemSrc = CreateCompatibleDC ( hDC );
408 hDCmemDst = CreateCompatibleDC ( hDC );
410 retVal = CreateCompatibleBitmap ( hDC, Width, Height );
412 if ( retVal != NULL ) {
414 myImages -> Dim ( i, w, h );
416 SelectBitmap ( hDCmemSrc, myImages -> ImageHandle ( i ) );
417 SelectBitmap ( hDCmemDst, retVal );
420 hDCmemDst, 0, 0, Width, Height, hDCmemSrc,
421 w / 2 - Width / 2, h / 2 - Height / 2, SRCCOPY
426 DeleteDC ( hDCmemDst );
427 DeleteDC ( hDCmemSrc );
429 if ( gDev -> IsPaletteDevice () )
431 SelectPalette ( hDC, hOldPal, FALSE );
433 ReleaseDC ( NULL, hDC );
439 } // end WNT_IconBox :: IconPixmap
441 //******************************* SetDim ********************************//
443 void WNT_IconBox :: SetDim (
444 const Standard_Integer aWidth,
445 const Standard_Integer aHeight
450 myIconWidth = aWidth;
451 myIconHeight = aHeight;
453 ZeroMemory ( &lf, sizeof ( LOGFONT ) );
455 lf.lfHeight = myIconHeight / 5;
456 strcpy ( lf.lfFaceName, "Modern" );
458 if ( myFont != NULL ) DeleteObject ( myFont );
460 myFont = CreateFontIndirect ( &lf );
462 } // end WNT_IconBox :: SetDim
464 //***********************************************************************//
466 static void __fastcall _get_filename ( char* retVal, int len, char* name, char* ext ) {
469 char eNameVal[ MAX_PATH ];
470 char fNameVal[ MAX_PATH ];
473 len = Min ( MAX_PATH, len );
475 ZeroMemory ( ( PVOID )eNameVal, sizeof ( eNameVal ) );
476 ZeroMemory ( ( PVOID )fNameVal, sizeof ( fNameVal ) );
478 if ( name != NULL ) {
480 strncpy ( fNameVal, name, len );
482 if ( *name == '$' ) {
484 ptr = strpbrk ( fNameVal, "\\/" );
492 ( eNameLen = GetEnvironmentVariable ( ptr1, eNameVal, MAX_PATH ) ) != 0
495 strncpy ( retVal, eNameVal, len );
497 strncat ( retVal, "\\", len );
503 strncat ( retVal, ptr, len );
507 strncpy ( retVal, fNameVal, len );
509 if ( strpbrk ( retVal, "." ) == NULL ) {
511 strncat ( retVal, ".", len );
512 strncat ( retVal, ext, len );
518 for ( int i = 0; i < ( int )( strlen ( retVal ) ); ++i )
520 if ( ptr[ i ] == '/' ) ptr[ i ] = '\\';
524 } // end _get_filename
526 //***********************************************************************//
528 LRESULT CALLBACK WNT_IconBoxWndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
530 WINDOW_DATA* wd = (WINDOW_DATA* )GetWindowLongPtr (hwnd, GWLP_USERDATA);
533 WNT_IconBox* ib = (WNT_IconBox* )(wd->WNT_Window_Ptr);
534 return ib->HandleEvent (hwnd, uMsg, wParam, lParam);
538 return DefWindowProc (hwnd, uMsg, wParam, lParam);
540 } // end WNT_IconBoxWndProc
542 //***********************************************************************//