Be Coding Tips/Trick

PR Release (DR9)

Contents

General

  1. Recompiling BeHeaders.pch++

GUI

  1. Snapping a window to an 8 byte boundary
  2. Application icon as a menu Label
  3. Retreiving application icon

STL

  1. Trouble Compiling/Linking STL

Shared Libraries

  1. Creating a shared library
  2. Exporting from a shared library

 


General

Recompiling BeHeaders.pch++

(As compiled by Ed Musgrove/Corrected by John Watte)


1) create a new BeIDE project (Or open up any BeIDE project)
2) go to settings and set all the warnings ON (not necessary)
3) add BeHeaders.pch++, after you find it in /?/develop/headers/be
4) select the file so it is highlighted (which de-ghosts the "precompile" menu item.
5) issue menu "precompile".


GUI

Snapping a window to an 8 byte boundary

(Info by Chris Herborth)

Ok, here's the code I use to snap a window's position to a multiple of 8
bytes:

void DispWin::FrameMoved( BPoint new_position )
{
	if( MessageQueue()->FindMessage( B_WINDOW_MOVED, 1 ) == NULL ) {
		// do the move, adjusting so that it's always on an 8-byte
		// boundary.
		double dx = fmod( new_position.x, boundry );
		double dy = fmod( new_position.y, boundry );

		if( dx > 0.0 && dy > 0.0 ) {
			MoveBy( -dx, -dy );
		}
	}
}

"boundry"is a member that's set to 8 for 8-bit screens or 2 for 32-bit screens. Basically, it waits for the _last_ FrameMoved() message, and then does the move.

 

Application icon as a menu label

Retreiving application icon

(Info by Jens Kilian)

Somebody recently asked for code to create a replacement application menu, using the application's icon as a menu label. I don't much like to criticize our resident wizard, but the code posted by Jon Watte made me shudder. My solution is below; it is a specialized class derived from BMenuItem, the way Be recommends it - at least in the DR8 BeBook.

Here's how I use it:

BBitmap *pAppIcon = MyApplication::GetIcon();
pMenu = new BMenu(kAppMenuLabel);
if (pAppIcon) {
	pMenuItem = new BitmapMenuItem(pAppIcon, pMenu);
} else {
	pMenuItem = new BMenuItem(pMenu);
}
pMenuBar->AddItem(pMenuItem);

And here's a function for retreiving the application icon:

#include <Application.h>
#include <AppFileInfo.h>
#include <Bitmap.h>
#include <File.h>
#include <Mime.h>
#include <Roster.h>

BBitmap *MyApplication::GetIcon(void)
{
	app_info info;
	if (be_app->GetAppInfo(&info) != B_NO_ERROR) {
		return 0;
}

BFile appFile(&(info.ref), O_RDONLY);
BAppFileInfo appFileInfo(&appFile);

BBitmap *pIcon = new BBitmap(BRect(0, 0, 15, 15), B_COLOR_8_BIT);
if (appFileInfo.GetIcon(pIcon, B_MINI_ICON) != B_NO_ERROR) {
	delete pIcon;
	return 0;
}

return pIcon;
}

STL

Trouble Compiling/Linking with STL

  1. Open up SupportDefs.h
  2. Comment out the following lines:#ifndef NIL
    #define NIL (0)
    #endif
  3. Precompile BeHeaders again
  4. Link with libmslcpp.a

Shared Libraries

Creating a shared library

  1. Set linker options to "use #pragma"
  2. Remove the __start entry poin from the Linker settings.
  3. Change the Project type to Shared Library

Exporting from a shared library

  1. Set linker options to "use #pragma"
  2. wrap what you want to export in an #pragma export on/reset pair.

 


Other Be related Links: