1 //-------------------------------------------------------------------- 2 // 3 // TestMenuBuilder.cpp 4 // 5 // Written by: Owen Smith 6 // 7 //-------------------------------------------------------------------- 8 9 /* 10 Copyright 1999, Be Incorporated. All Rights Reserved. 11 This file may be used under the terms of the Be Sample Code License. 12 */ 13 14 #include <Font.h> 15 #include <Menu.h> 16 #include <MenuItem.h> 17 #include <Message.h> 18 #include <stdio.h> 19 #include <string.h> 20 21 #include "constants.h" 22 #include "TestIcons.h" 23 #include "TestMenuBuilder.h" 24 #include "BitmapMenuItem.h" 25 26 //==================================================================== 27 // TestMenuBuilder Implementation 28 29 #define TEST_NAME_LENGTH 20 30 31 32 33 //-------------------------------------------------------------------- 34 // TestMenuBuilder entry point 35 36 BMenu* TestMenuBuilder::BuildTestMenu(icon_size size) 37 { 38 BitmapMenuItem *pItemArray[NUM_TEST_ITEMS_DOWN][NUM_TEST_ITEMS_ACROSS]; 39 BitmapMenuItem *pTypicalItem = NULL; 40 41 // fill the array of bitmap menu items 42 for (int32 i=0; i<NUM_TEST_ITEMS_DOWN; i++) { 43 for (int32 j=0; j<NUM_TEST_ITEMS_ACROSS; j++) { 44 BBitmap *pBitmap = LoadTestBitmap(i, j, size); 45 if (! pBitmap) { 46 pItemArray[i][j] = NULL; 47 } else { 48 int32 itemIndex = NUM_TEST_ITEMS_ACROSS*i + j + 1; 49 char name[TEST_NAME_LENGTH]; 50 sprintf(name, "%s %ld", STR_MNU_TEST_ITEM, itemIndex); 51 BMessage* pTestMsg = new BMessage(MSG_TEST_ITEM); 52 pTestMsg->AddInt32("Item Index", itemIndex); 53 char shortcut = '0' + itemIndex; // the item's number 54 BitmapMenuItem *pItem = new BitmapMenuItem(name, *pBitmap, 55 pTestMsg, shortcut, 0); 56 pItemArray[i][j] = pItem; 57 pTypicalItem = pItem; 58 } 59 delete pBitmap; 60 } 61 } 62 63 float menuHeight, menuWidth; 64 65 // Simplifying assumption: All test items have same width and height. 66 // Use pTypicalItem to calculate frames. 67 if (! pTypicalItem) { 68 // no items to put in a menu 69 return NULL; 70 } 71 float itemHeight, itemWidth; 72 pTypicalItem->GetBitmapSize(&itemWidth, &itemHeight); 73 itemWidth++; // take space between items into account 74 itemHeight++; 75 76 menuWidth = NUM_TEST_ITEMS_ACROSS*itemWidth; 77 menuHeight = NUM_TEST_ITEMS_DOWN*itemHeight; 78 79 // create menu 80 float left, top; 81 BRect frame; 82 BMenu* pMenu = new BMenu(STR_MNU_TEST, menuWidth, menuHeight); 83 for (int32 i=0; i<NUM_TEST_ITEMS_DOWN; i++) { 84 for (int32 j=0; j<NUM_TEST_ITEMS_ACROSS; j++) { 85 BitmapMenuItem* pItem = pItemArray[i][j]; 86 if (pItem) { 87 left = j*itemWidth; 88 top = i*itemHeight; 89 frame.Set(left, top, left + itemWidth - 1, 90 top + itemHeight - 1); 91 pMenu->AddItem(pItem, frame); 92 } 93 } 94 } 95 96 return pMenu; 97 } 98 99 //-------------------------------------------------------------------- 100 // TestMenuBuilder implementation member functions 101 102 BBitmap* TestMenuBuilder::LoadTestBitmap(int32 i, int32 j, icon_size size) 103 { 104 BBitmap* pBitmap; 105 const uint8* bits; 106 uint32 byteLen; 107 color_space iconColorSpace; 108 BRect iconBounds(0,0,0,0); 109 110 if ((i < 0) || (j < 0)) { 111 return NULL; 112 } 113 114 if ((i >= NUM_TEST_ITEMS_DOWN) || (j >= NUM_TEST_ITEMS_ACROSS)) { 115 return NULL; 116 } 117 118 if (size == B_LARGE_ICON) { 119 bits = kLargeTestIcons[i][j]; 120 byteLen = LARGE_ICON_BYTES; 121 iconBounds.right = kLargeIconWidth - 1; // 0 counts as a pixel! 122 iconBounds.bottom = kLargeIconHeight - 1; 123 iconColorSpace = kLargeIconColorSpace; 124 } else { 125 bits = kMiniTestIcons[i][j]; 126 byteLen = MINI_ICON_BYTES; 127 iconBounds.right = kMiniIconWidth - 1; 128 iconBounds.bottom = kMiniIconHeight - 1; 129 iconColorSpace = kMiniIconColorSpace; 130 } 131 132 pBitmap = new BBitmap(iconBounds, iconColorSpace); 133 uint8* destBits = (uint8*)pBitmap->Bits(); 134 memcpy(destBits, bits, byteLen); 135 136 return pBitmap; 137 } 138