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