root/BF_GUI_Resources.cpp

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. LoadResource
  2. LoadResource
  3. GetBitmapResource
  4. GetIconResource
  5. find_image

   1 #include "BL_Tools.h"
   2 #include "BF_GUI_Resources.h"
   3 
   4 #include <Autolock.h>
   5 #include <Bitmap.h>
   6 #include <Debug.h>
   7 #include <DataIO.h>
   8 #include <File.h>
   9 #include <SupportDefs.h>
  10 
  11 BF_GUI_Resources *poResources =  NULL;
  12 
  13 BF_GUI_Resources::BF_GUI_Resources()
  14 {
  15         image_id image = find_image(NULL);
  16         image_info info;
  17         if (get_image_info(image, &info) == B_OK) {
  18                 BFile file;
  19                 if (file.SetTo(&info.name[0], B_READ_ONLY) == B_OK) 
  20                         fResources.SetTo(&file);
  21         }       
  22 }
  23 
  24 const void *
  25 BF_GUI_Resources::LoadResource(type_code type, int32 id, size_t *out_size) const    /* [previous][next][first][last][top][bottom][index][help] */
  26 {       
  27         // Serialize execution.
  28         // Looks like BResources is not really thread safe. We should
  29         // clean that up in the future and remove the locking from here.
  30         BAutolock lock(fLock);
  31         if (!lock.IsLocked())
  32                 return 0;
  33         
  34         // Return the resource.  Because we never change the BResources
  35         // object, the returned data will not change until TTracker is
  36         // destroyed.
  37         return const_cast<BResources *>(&fResources)->LoadResource(type, id, out_size);
  38 }
  39 
  40 const void *
  41 BF_GUI_Resources::LoadResource(type_code type, const char *name, size_t *out_size) const    /* [previous][next][first][last][top][bottom][index][help] */
  42 {
  43         // Serialize execution.
  44         BAutolock lock(fLock);
  45         if (!lock.IsLocked())
  46                 return NULL;
  47         
  48         // Return the resource.  Because we never change the BResources
  49         // object, the returned data will not change until TTracker is
  50         // destroyed.
  51         return const_cast<BResources *>(&fResources)->LoadResource(type, name, out_size);
  52 }
  53 
  54 status_t
  55 BF_GUI_Resources::GetBitmapResource(type_code type, int32 id, BBitmap **out) const    /* [previous][next][first][last][top][bottom][index][help] */
  56 {
  57         *out = NULL;
  58         
  59         size_t len = 0;
  60         const void *data = LoadResource(type, id, &len);
  61 
  62         if (data == NULL) {
  63                 TRESPASS();
  64                 return B_ERROR;
  65         }
  66         
  67         BMemoryIO stream(data, len);
  68         
  69         // Try to read as an archived bitmap.
  70         stream.Seek(0, SEEK_SET);
  71         BMessage archive;
  72         status_t err = archive.Unflatten(&stream);
  73         if (err != B_OK)
  74                 return err;
  75 
  76         *out = new BBitmap(&archive);
  77         if (!*out)
  78                 return B_ERROR;
  79 
  80         err = (*out)->InitCheck();
  81         if (err != B_OK) {
  82                 delete *out;
  83                 *out = NULL;
  84         }
  85         
  86         return err;
  87 }
  88 
  89 status_t
  90 BF_GUI_Resources::GetIconResource(int32 id, icon_size size, BBitmap *dest) const    /* [previous][next][first][last][top][bottom][index][help] */
  91 {
  92         if (size != B_LARGE_ICON && size != B_MINI_ICON )
  93                 return B_ERROR;
  94         
  95         size_t len = 0;
  96         const void *data = LoadResource(size == B_LARGE_ICON ? 'ICON' : 'MICN',
  97                 id, &len);
  98 
  99         if (data == 0 || len != (size_t)(size == B_LARGE_ICON ? 1024 : 256)) {
 100                 TRESPASS();
 101                 return B_ERROR;
 102         }
 103         
 104         dest->SetBits(data, (int32)len, 0, kDefaultIconDepth);
 105         return B_OK;
 106 }
 107 
 108 
 109 
 110 
 111 image_id
 112 BF_GUI_Resources::find_image(void *memAddr) const    /* [previous][next][first][last][top][bottom][index][help] */
 113 {
 114         image_info info; 
 115         int32 cookie = 0; 
 116         while (get_next_image_info(0, &cookie, &info) == B_OK) 
 117                 if ((info.text <= memAddr && (((uint8 *)info.text)+info.text_size) > memAddr)
 118                         ||(info.data <= memAddr && (((uint8 *)info.data)+info.data_size) > memAddr)) 
 119                         // Found the image.
 120                         return info.id;
 121         
 122         return -1;

/* [previous][next][first][last][top][bottom][index][help] */