0023426: Tool to compare two runs of tests on the same station
[occt.git] / src / WNT / WNT_IconBox.cxx
1 // Copyright (c) 1996-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 //
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.
8 //
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.
11 //
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.
18
19 // include windows.h first to have all definitions available
20 #include <windows.h>
21 #include <windowsx.h>
22
23 #include <WNT_IconBox.ixx>
24
25 #include <WNT_WClass.hxx>
26 #include <WNT_ImageManager.hxx>
27 #include <WNT_Icon.hxx>
28
29 //***//
30 static void __fastcall _get_filename ( char*, int, char*, char* );
31 //***//
32 HBITMAP LoadImageFromFile (  Handle( WNT_GraphicDevice )&, char*, HDC = NULL  );
33 int     SaveBitmapToFile (
34          Handle( WNT_GraphicDevice )& gDev,
35          HBITMAP, char*, int, int, int, int
36             );
37 LRESULT CALLBACK WNT_IconBoxWndProc ( HWND, UINT, WPARAM, LPARAM );
38 //***//
39 //*************************** Constructor *******************************//
40 //***//
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
48                ) : WNT_Window (
49                     aDevice, aName,
50                     new WNT_WClass (
51                          "WNT_IconBoxClass", WNT_IconBoxWndProc,
52                          CS_HREDRAW | CS_VREDRAW
53                         ),
54                     aStyle | WS_VSCROLL,
55                     Xc, Yc, 0.5, 0.2, aBkColor
56                    ) {
57
58   if (  LoadIcons ( aName ) == 0  )
59
60    Aspect_WindowDefinitionError :: Raise ( "Unable to load icons" );
61
62   myFont = NULL;
63   SetDim ( 70, 70 );
64   myStart = 1;
65
66   myPen = CreatePen (  PS_SOLID, 3, RGB( 0, 255, 0 )  );
67
68 }  // end constructor
69 //***//
70 //********************************* Destroy *****************************//
71 //***//
72 void WNT_IconBox :: Destroy () {
73
74  if ( myFont != NULL )
75
76   DeleteObject ( myFont );
77
78  if ( myPen != NULL )
79
80   DeleteObject ( myPen );
81
82 }  // end WNT_IconBox :: Destroy
83 //***//
84 //******************************* LoadIcons *****************************//
85 //***//
86 Standard_Integer WNT_IconBox :: LoadIcons (
87                                  const Standard_CString Name
88                                 ) {
89
90  int                len;
91  Standard_Integer   retVal = 0;
92  ifstream           is;
93  char               fileName[ MAX_PATH ];
94  char               fName[ MAX_PATH ];
95  char               iName[ MAX_PATH ];
96  Standard_Integer   iHCode;
97  HBITMAP            hBmp;
98  Handle( WNT_Icon )     icon;
99
100  Handle( WNT_GraphicDevice ) gDev = Handle( WNT_GraphicDevice ) ::
101                                      DownCast (  GraphicDevice ()  );
102
103  _get_filename ( fileName, MAX_PATH, (Standard_PCharacter)Name, "ifd" );
104
105  is.open ( fileName );
106
107  if ( is ) {
108
109   while (  !is.eof ()  ) {
110
111    is >> fName >> iName;
112
113    if (  is.bad ()  ) break;
114
115    if ( fName[ 0 ] == '\x00' || iName[ 0 ] == '\x00' ) continue;
116
117    if (  fName[ 0 ] == '#' ) continue;
118
119    _get_filename ( fileName, MAX_PATH, fName, "" );
120
121    iHCode = myImages -> StringHashCode ( fileName );
122    len    = myImages -> Size ();
123    int i;
124
125    for ( i = 1; i <= len; ++i )
126
127     if (  myImages -> HashCode ( i ) == iHCode  ) {
128
129      icon = Handle( WNT_Icon ) :: DownCast (  myImages -> Image ( i )  );
130
131      if (  lstrcmp ( iName, icon -> myName )  ) {
132
133       i = len + 1;
134       break;
135
136      }  /* end if */
137
138     }  /* end if */
139
140    if ( i > len ) {
141
142     hBmp = LoadImageFromFile ( gDev, fileName );
143
144     if ( hBmp ) {
145
146      icon = new WNT_Icon ( iName, hBmp, iHCode );
147      myImages -> Add ( icon );
148
149     }  // end if
150
151    }  // end if ( i > len . . . )
152
153    fName[ 0 ] = iName[ 0 ] = '\x00';
154
155   }  // end while
156
157   retVal = myImages -> Size ();
158
159  }  // end if
160
161  return retVal;
162
163 }  // end WNT_IconBox :: LoadIcons
164 //***//
165 //********************************** Show *******************************//
166 //***//
167 void WNT_IconBox :: Show () const {
168
169  Map ();
170
171 }  // end WNT_IconBox :: Show
172 //***//
173 //******************************* UnloadIcons ***************************//
174 //***//
175 Standard_Integer WNT_IconBox :: UnloadIcons (
176                                  const Standard_CString Name
177                                 ) {
178
179  Standard_Integer   retVal = 0;
180  ifstream           is;
181  char               fileName[ MAX_PATH ];
182  char               fName[ MAX_PATH ];
183  char               iName[ MAX_PATH ];
184  Handle( WNT_Icon )     icon;
185
186  Handle( WNT_GraphicDevice ) gDev = Handle( WNT_GraphicDevice ) ::
187                                      DownCast (  GraphicDevice ()  );
188
189  _get_filename ( fileName, MAX_PATH, (Standard_PCharacter)Name, "ifd" );
190
191  is.open ( fileName );
192
193  if ( is ) {
194
195   while (  !is.eof ()  ) {
196
197    is >> fName >> iName;
198
199    if (  is.bad ()  ) break;
200
201    if (  fName[ 0 ] == '#' ) continue;
202
203    for ( int i = 1; i <= myImages -> Size (); ++i ) {
204
205     icon = Handle( WNT_Icon ) :: DownCast (  myImages -> Image ( i )  );
206
207         if (  icon.IsNull ()  ) continue;
208
209         if (  lstrcmp ( icon -> myName, iName )  ) continue;
210
211         myImages -> Delete ( i );
212         ++retVal;
213
214    }  // end for
215
216   }  // end while
217
218  }  // end if
219
220  return retVal;
221
222 }  // end WNT_IconBox :: UnloadIcons
223 //***//
224 //********************************* AddIcon *****************************//
225 //***//
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
231                     ) {
232
233   Handle( WNT_Icon ) icon = W -> myIcon;
234
235   if (  !icon.IsNull ()  )
236
237    myImages -> Add ( W -> myIcon );
238
239 }  // end WNT_IconBox :: AddIcon
240 //***//
241 //********************************* SaveIcons ***************************//
242 //***//
243 Standard_Integer WNT_IconBox :: SaveIcons () const {
244
245  int                w, h;
246  Standard_Integer   retVal = 0;
247  char               ext[ 5 ];
248  char               fName[ MAX_PATH ];
249  Handle( WNT_Icon )     icon;
250
251  Handle( WNT_GraphicDevice ) gDev = Handle( WNT_GraphicDevice ) ::
252                                      DownCast (  GraphicDevice ()  );
253
254  switch ( myFormat ) {
255
256   case WNT_TOI_XWD:
257
258    strcpy ( ext, ".xwd" );
259
260   break;
261
262   case WNT_TOI_BMP:
263
264    strcpy ( ext, ".bmp" );
265
266   break;
267
268   case WNT_TOI_GIF:
269
270    strcpy ( ext, ".gif" );
271
272  }  // end switch
273
274  for ( int i = 1; i <= myImages -> Size (); ++i ) {
275
276   icon = Handle( WNT_Icon ) :: DownCast (  myImages -> Image ( i )  );
277
278   if (  icon.IsNull ()  ) continue;
279
280   strcpy ( fName, icon -> myName );
281
282   if (  strchr ( fName, '.' ) == NULL  )
283
284    strcat ( fName, ext );
285
286   myImages -> Dim ( i, w, h );
287
288   if (  SaveBitmapToFile (
289          gDev, ( HBITMAP )(  myImages -> ImageHandle ( i )  ), fName,
290                  0, 0, w, h
291                 )
292   )
293
294    ++retVal;
295
296  }  // end for
297
298  return retVal;
299
300 }  // end WNT_IconBox :: SaveIcons
301 //***//
302 //********************************* IconNumber **************************//
303 //***//
304 Standard_Integer WNT_IconBox :: IconNumber () const {
305
306  return myImages -> Size ();
307
308 }  // end WNT_IconBox :: IconNumber
309 //***//
310 //********************************* IconName ****************************//
311 //***//
312 Standard_CString WNT_IconBox :: IconName (
313                                  const Standard_Integer Index
314                                 ) const {
315
316  Handle( WNT_Icon ) icon = Handle( WNT_Icon ) :: DownCast (
317                                                   myImages -> Image ( Index )
318                                                                                                  );
319
320  return icon -> myName;
321
322 }  // end WNT_IconBox :: IconName
323 //***//
324 //********************************* IconSize ****************************//
325 //***//
326 Standard_Boolean WNT_IconBox :: IconSize (
327                                  const Standard_CString Name,
328                                  Standard_Integer&      Width,
329                                  Standard_Integer&      Height
330                                 ) const {
331
332  int                i, len = myImages -> Size ();
333  Handle( WNT_Icon ) icon;
334
335  for ( i = 1; i <= len; ++i ) {
336
337   icon = Handle( WNT_Icon ) :: DownCast (  myImages -> Image ( i )  );
338
339   if (  !strcmp ( icon -> myName, Name )  ) break;
340
341  }  // end for
342
343  return ( i > len ) ? Standard_False :
344                       myImages -> Dim ( i, Width, Height ), Standard_True;
345
346 }  // end WNT_IconBox :: IconSize
347 //***//
348 //********************************* IconPixmap (1) **********************//
349 //***//
350 Aspect_Handle WNT_IconBox :: IconPixmap (
351                             const Standard_CString Name
352                           ) const {
353
354  int                i, len = myImages -> Size ();
355  Handle( WNT_Icon ) icon;
356
357  for ( i = 1; i <= len; ++i ) {
358
359   icon = Handle( WNT_Icon ) :: DownCast (  myImages -> Image ( i )  );
360
361   if (  !strcmp ( icon -> myName, Name )  ) break;
362
363  }  // end for
364
365  return ( i > len ) ? NULL : myImages -> ImageHandle ( i );
366
367 }  // end WNT_IconBox :: IconPixmap
368 //***//
369 //********************************* IconPixmap (2) **********************//
370 //***//
371 Aspect_Handle WNT_IconBox :: IconPixmap (
372                            const Standard_CString Name,
373                            const Standard_Integer Width,
374                            const Standard_Integer Height
375                           ) const {
376
377  HBITMAP            retVal = NULL;
378  int                len = myImages -> Size ();
379  int                i, w, h;
380  Handle( WNT_Icon ) icon;
381  HPALETTE           hOldPal;
382
383  Handle( WNT_GraphicDevice ) gDev = Handle( WNT_GraphicDevice ) ::
384                                      DownCast (  GraphicDevice ()  );
385
386  for ( i = 1; i <= len; ++i ) {
387
388   icon = Handle( WNT_Icon ) :: DownCast (  myImages -> Image ( i )  );
389
390   if (  !strcmp ( icon -> myName, Name )  ) break;
391
392  }  // end for
393
394  if ( i <= len ) {
395
396   HDC hDC, hDCmemSrc, hDCmemDst;
397
398   hDC = GetDC ( NULL );
399
400    if (  gDev -> IsPaletteDevice ()  ) {
401
402         hOldPal = SelectPalette ( hDC, ( HPALETTE )(  gDev -> HPalette ()  ), FALSE );
403         RealizePalette ( hDC );
404
405    }  // end if
406
407    hDCmemSrc = CreateCompatibleDC ( hDC );
408    hDCmemDst = CreateCompatibleDC ( hDC );
409
410    retVal = CreateCompatibleBitmap ( hDC, Width, Height );
411
412    if ( retVal != NULL ) {
413
414         myImages -> Dim ( i, w, h );
415
416         SelectBitmap (  hDCmemSrc, myImages -> ImageHandle ( i )  );
417         SelectBitmap (  hDCmemDst, retVal );
418
419         BitBlt (
420          hDCmemDst, 0, 0, Width, Height, hDCmemSrc,
421          w / 2 - Width / 2, h / 2 - Height / 2, SRCCOPY
422         );
423
424    }  // end if
425
426    DeleteDC ( hDCmemDst );
427    DeleteDC ( hDCmemSrc );
428
429    if (  gDev -> IsPaletteDevice ()  )
430
431     SelectPalette ( hDC, hOldPal, FALSE );
432
433   ReleaseDC ( NULL, hDC );
434
435  }  // end if
436
437  return retVal;
438
439 }  // end WNT_IconBox :: IconPixmap
440 //***//
441 //******************************* SetDim ********************************//
442 //***//
443 void WNT_IconBox :: SetDim (
444                      const Standard_Integer aWidth,
445                      const Standard_Integer aHeight
446                     ) {
447
448  LOGFONT lf;
449
450  myIconWidth  = aWidth;
451  myIconHeight = aHeight;
452
453  ZeroMemory (  &lf, sizeof ( LOGFONT )  );
454
455  lf.lfHeight = myIconHeight / 5;
456  strcpy ( lf.lfFaceName, "Modern" );
457
458  if ( myFont != NULL ) DeleteObject ( myFont );
459
460  myFont = CreateFontIndirect ( &lf );
461
462 }  // end WNT_IconBox :: SetDim
463 //***//
464 //***********************************************************************//
465 //***//
466 static void __fastcall _get_filename ( char* retVal, int len, char* name, char* ext ) {
467
468  char* ptr, *ptr1;
469  char  eNameVal[ MAX_PATH ];
470  char  fNameVal[ MAX_PATH ];
471  DWORD eNameLen;
472
473  len = Min ( MAX_PATH, len );
474
475  ZeroMemory (  ( PVOID )eNameVal, sizeof ( eNameVal )  );
476  ZeroMemory (  ( PVOID )fNameVal, sizeof ( fNameVal )  );
477
478  if ( name != NULL ) {
479
480   strncpy ( fNameVal, name, len );
481
482   if ( *name == '$' ) {
483
484    ptr = strpbrk ( fNameVal, "\\/" );
485
486    if ( ptr != NULL ) {
487
488         *ptr++ = '\x00';
489         ptr1 = fNameVal + 1;
490
491         if (
492          (  eNameLen = GetEnvironmentVariable ( ptr1, eNameVal, MAX_PATH )  ) != 0
493         )
494
495          strncpy ( retVal, eNameVal, len );
496
497         strncat ( retVal, "\\", len );
498
499    } else
500
501     ptr = fNameVal;
502
503    strncat ( retVal, ptr, len );
504
505   } else
506
507    strncpy ( retVal, fNameVal, len );
508
509   if (  strpbrk ( retVal, "." ) == NULL  ) {
510
511    strncat ( retVal, ".", len );
512    strncat ( retVal, ext, len );
513
514   }  // end if
515
516   ptr = retVal;
517
518   for ( int i = 0; i < ( int )(  strlen ( retVal )  ); ++i )
519
520    if ( ptr[ i ] == '/' ) ptr[ i ] = '\\';
521
522  }  // end if
523
524 }  // end _get_filename
525 //***//
526 //***********************************************************************//
527 //***//
528 LRESULT CALLBACK WNT_IconBoxWndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
529 {
530   WINDOW_DATA* wd = (WINDOW_DATA* )GetWindowLongPtr (hwnd, GWLP_USERDATA);
531   if (wd != NULL)
532   {
533     WNT_IconBox* ib = (WNT_IconBox* )(wd->WNT_Window_Ptr);
534     return ib->HandleEvent (hwnd, uMsg, wParam, lParam);
535   }
536   else
537   {
538     return DefWindowProc (hwnd, uMsg, wParam, lParam);
539   }
540 }  // end WNT_IconBoxWndProc
541 //***//
542 //***********************************************************************//