Пример #1
0
const char*
Utility::_GetMimeString(uint32 imageType) const
{
	const char *dummy = "";
	translator_id* translators = NULL;
	int32 numTranslators = 0;
	BTranslatorRoster* roster = BTranslatorRoster::Default();
	status_t status = roster->GetAllTranslators(&translators, &numTranslators);
	if (status != B_OK)
		return dummy;

	for (int32 x = 0; x < numTranslators; x++) {
		const translation_format* formats = NULL;
		int32 numFormats;

		if (roster->GetOutputFormats(x, &formats, &numFormats) == B_OK) {
			for (int32 i = 0; i < numFormats; ++i) {
				if (formats[i].type == imageType) {
					delete [] translators;
					return formats[i].MIME;
				}
			}
		}
	}
	delete [] translators;
	return dummy;
}
Пример #2
0
void
PictureView::_BeginDrag(BPoint sourcePoint)
{
	BBitmap* bitmap = _CopyPicture(128);
	if (bitmap == NULL)
		return;

	// fill the drag message
	BMessage drag(B_SIMPLE_DATA);
	drag.AddInt32("be:actions", B_COPY_TARGET);
	drag.AddInt32("be:actions", B_TRASH_TARGET);

	// name the clip after person name, if any
	BString name = B_TRANSLATE("%name% picture");
	name.ReplaceFirst("%name%", Window() ? Window()->Title() :
		B_TRANSLATE("Unnamed person"));
	drag.AddString("be:clip_name", name.String());

	BTranslatorRoster* roster = BTranslatorRoster::Default();
	if (roster == NULL) {
		delete bitmap;
		return;
	}

	int32 infoCount;
	translator_info* info;
	BBitmapStream stream(bitmap);
	if (roster->GetTranslators(&stream, NULL, &info, &infoCount) == B_OK) {
		for (int32 i = 0; i < infoCount; i++) {
			const translation_format* formats;
			int32 count;
			roster->GetOutputFormats(info[i].translator, &formats, &count);
			for (int32 j = 0; j < count; j++) {
				if (strcmp(formats[j].MIME, "image/x-be-bitmap") != 0) {
					// needed to send data in message
					drag.AddString("be:types", formats[j].MIME);
					// needed to pass data via file
					drag.AddString("be:filetypes", formats[j].MIME);
					drag.AddString("be:type_descriptions", formats[j].name);
				}
			}
		}
	}
	stream.DetachBitmap(&bitmap);

	// we also support "Passing Data via File" protocol
	drag.AddString("be:types", B_FILE_MIME_TYPE);

	sourcePoint -= fPictureRect.LeftTop();

	SetMouseEventMask(B_POINTER_EVENTS);

	DragMessage(&drag, bitmap, B_OP_ALPHA, sourcePoint);
	bitmap = NULL;
}
Пример #3
0
void
ShowImageWindow::_SaveToFile(BMessage* message)
{
	// Read in where the file should be saved
	entry_ref dirRef;
	if (message->FindRef("directory", &dirRef) != B_OK)
		return;

	const char* filename;
	if (message->FindString("name", &filename) != B_OK)
		return;

	// Read in the translator and type to be used
	// to save the output image
	translator_id outTranslator;
	uint32 outType;
	if (message->FindInt32(kTranslatorField,
			reinterpret_cast<int32 *>(&outTranslator)) != B_OK
		|| message->FindInt32(kTypeField,
			reinterpret_cast<int32 *>(&outType)) != B_OK)
		return;

	// Find the translator_format information needed to
	// write a MIME attribute for the image file
	BTranslatorRoster* roster = BTranslatorRoster::Default();
	const translation_format* outFormat = NULL;
	int32 outCount = 0;
	if (roster->GetOutputFormats(outTranslator, &outFormat, &outCount) != B_OK
		|| outCount < 1)
		return;

	int32 i;
	for (i = 0; i < outCount; i++) {
		if (outFormat[i].group == B_TRANSLATOR_BITMAP && outFormat[i].type
				== outType)
			break;
	}
	if (i == outCount)
		return;

	// Write out the image file
	BDirectory dir(&dirRef);
	fImageView->SaveToFile(&dir, filename, NULL, &outFormat[i]);

	// Store Save directory in settings;
	ShowImageSettings* settings = my_app->Settings();
	if (settings->Lock()) {
		BPath path(&dirRef);
		settings->SetString("SaveDirectory", path.Path());
		settings->Unlock();
	}
}
Пример #4
0
status_t
AddTranslationItems(BMenu* intoMenu, uint32 fromType)
{

	BTranslatorRoster* use;
	char* translatorTypeName;
	const char* translatorIdName;

	use = BTranslatorRoster::Default();
	translatorIdName = "be:translator";
	translatorTypeName = (char *)"be:type";
	translator_id* ids = NULL;
	int32 count = 0;

	status_t err = use->GetAllTranslators(&ids, &count);
	if (err < B_OK)
		return err;

	for (int tix = 0; tix < count; tix++) {
		const translation_format* formats = NULL;
		int32 num_formats = 0;
		bool ok = false;
		err = use->GetInputFormats(ids[tix], &formats, &num_formats);
		if (err == B_OK)
			for (int iix = 0; iix < num_formats; iix++) {
				if (formats[iix].type == fromType) {
					ok = true;
					break;
				}
			}

		if (!ok)
			continue;

		err = use->GetOutputFormats(ids[tix], &formats, &num_formats);
		if (err == B_OK)
			for (int oix = 0; oix < num_formats; oix++) {
 				if (formats[oix].type != fromType) {
					BMessage* itemmsg;
					itemmsg = new BMessage(msg_translate);
					itemmsg->AddInt32(translatorIdName, ids[tix]);
					itemmsg->AddInt32(translatorTypeName, formats[oix].type);
					intoMenu->AddItem(new BMenuItem(formats[oix].name, itemmsg));
				}
			}
	}
	delete[] ids;
	return B_OK;
}
Пример #5
0
void
PictureView::_HandleDrop(BMessage* msg)
{
	entry_ref dirRef;
	BString name, type;
	bool saveToFile = msg->FindString("be:filetypes", &type) == B_OK
		&& msg->FindRef("directory", &dirRef) == B_OK
		&& msg->FindString("name", &name) == B_OK;

	bool sendInMessage = !saveToFile
		&& msg->FindString("be:types", &type) == B_OK;

	if (!sendInMessage && !saveToFile)
		return;

	BBitmap* bitmap = fPicture;
	if (bitmap == NULL)
		return;

	BTranslatorRoster* roster = BTranslatorRoster::Default();
	if (roster == NULL)
		return;

	BBitmapStream stream(bitmap);

	// find translation format we're asked for
	translator_info* outInfo;
	int32 outNumInfo;
	bool found = false;
	translation_format format;

	if (roster->GetTranslators(&stream, NULL, &outInfo, &outNumInfo) == B_OK) {
		for (int32 i = 0; i < outNumInfo; i++) {
			const translation_format* formats;
			int32 formatCount;
			roster->GetOutputFormats(outInfo[i].translator, &formats,
					&formatCount);
			for (int32 j = 0; j < formatCount; j++) {
				if (strcmp(formats[j].MIME, type.String()) == 0) {
					format = formats[j];
					found = true;
					break;
				}
			}
		}
	}

	if (!found) {
		stream.DetachBitmap(&bitmap);
		return;
	}

	if (sendInMessage) {

		BMessage reply(B_MIME_DATA);
		BMallocIO memStream;
		if (roster->Translate(&stream, NULL, NULL, &memStream,
			format.type) == B_OK) {
			reply.AddData(format.MIME, B_MIME_TYPE, memStream.Buffer(),
				memStream.BufferLength());
			msg->SendReply(&reply);
		}

	} else {

		BDirectory dir(&dirRef);
		BFile file(&dir, name.String(), B_WRITE_ONLY | B_CREATE_FILE
			| B_ERASE_FILE);

		if (file.InitCheck() == B_OK
			&& roster->Translate(&stream, NULL, NULL, &file,
				format.type) == B_OK) {
			BNodeInfo nodeInfo(&file);
			if (nodeInfo.InitCheck() == B_OK)
				nodeInfo.SetType(type.String());
		} else {
			BString text = B_TRANSLATE("The file '%name%' could not "
				"be written.");
			text.ReplaceFirst("%name%", name);
			BAlert* alert = new BAlert(B_TRANSLATE("Error"), text.String(),
				B_TRANSLATE("OK"), NULL, NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT);
			alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
			alert->Go();
		}
	}

	// Detach, as we don't want our fPicture to be deleted
	stream.DetachBitmap(&bitmap);
}
Пример #6
0
BMessage *DragonView::_MakeDragMessage( void )
{
	DragonApp *app = dynamic_cast<DragonApp *>( be_app );

	BMessage *msg = new BMessage( B_SIMPLE_DATA );

	// We can handle any image type that's supported by the currently
	// installed Translators.
	//
	// A "real" application would want to add the Translators in order,
	// from best to worst using the quality and capability data in the
	// Translator information structure.  It would also want to make sure
	// that there weren't any duplicate types in the drag message.
	
	BTranslatorRoster *translators = BTranslatorRoster::Default();
	translator_id *all_translators = NULL;
	int32 num_translators = 0;
	
	status_t retval = translators->GetAllTranslators( &all_translators,
													  &num_translators );
	if( retval == B_OK ) {
		// Only add translators that support appropriate inputs/outputs.
		
		for( int32 idx = 0; idx < num_translators; idx++ ) {
			const translation_format *in_formats = NULL;
			int32 num_in = 0;
			
			// Get the list of input formats for this Translator.
			retval = translators->GetInputFormats( all_translators[idx],
												   &in_formats,
												   &num_in );
			if( retval != B_OK ) continue;
			
			// Make sure it supports BBitmap inputs.
			
			for( int32 in = 0; in < num_in; in++ ) {
				if( !strcmp( in_formats[in].MIME, "image/x-be-bitmap" ) ) {
					// Add this translator's output formats to the message.
					
					const translation_format *out_formats = NULL;
					int32 num_out = 0;
					
					retval = translators->GetOutputFormats( all_translators[idx],
															&out_formats,
															&num_out );
					if( retval != B_OK ) break;
					
					for( int32 out = 0; out < num_out; out++ ) {
						// Add every type except "image/x-be-bitmap",
						// which won't be of any use to us.

						if( strcmp( out_formats[out].MIME, "image/x-be-bitmap" ) ) {
							msg->AddString( "be:types", 
											out_formats[out].MIME );
							msg->AddString( "be:filetypes", 
											out_formats[out].MIME );
							msg->AddString( "be:type_descriptions",
											out_formats[out].name );
						}
					}
				}
			}
		}
	}

	// We can also handle raw data.

	msg->AddString( "be:types", B_FILE_MIME_TYPE );
	msg->AddString( "be:filetypes", B_FILE_MIME_TYPE );
	msg->AddString( "be:type_descriptions", 
					app->rsrc_strings->FindString( RSRC_Raw_Data ) );

	// Add the actions that we'll support.  B_LINK_TARGET doesn't make much
	// sense in this context, so we'll leave it out.  B_MOVE_TARGET is a
	// B_COPY_TARGET followed by B_TRASH_TARGET...

	msg->AddInt32( "be:actions", B_COPY_TARGET );
	msg->AddInt32( "be:actions", B_TRASH_TARGET );
	msg->AddInt32( "be:actions", B_MOVE_TARGET );

	// A file name for dropping onto things (like the Tracker) that create
	// files.
	
	msg->AddString( "be:clip_name", "Dropped Bitmap" );

	return msg;
}
void
ScreenshotWindow::_ShowSettings(bool activate)
{
	if (!fSettingsWindow && !activate)
		return;

	// Find a translator
	translator_id translator = 0;
	BTranslatorRoster *roster = BTranslatorRoster::Default();
	translator_id* translators = NULL;
	int32 numTranslators = 0;
	if (roster->GetAllTranslators(&translators, &numTranslators) != B_OK)
		return;
	bool foundTranslator = false;
	for (int32 x = 0; x < numTranslators; x++) {
		const translation_format* formats = NULL;
		int32 numFormats;
		if (roster->GetOutputFormats(translators[x], &formats,
			&numFormats) == B_OK) {
			for (int32 i = 0; i < numFormats; ++i) {
				if (formats[i].type == static_cast<uint32>(fImageFileType)) {
					translator = translators[x];
					foundTranslator = true;
					break;
				}
			}
		}
		if (foundTranslator)
			break;
	}
	delete [] translators;
	if (!foundTranslator)
		return;

	// Create a window with a configuration view
	BView *view;
	BRect rect(0, 0, 239, 239);

	status_t err = roster->MakeConfigurationView(translator, NULL, &view,
		&rect);
	if (err < B_OK || view == NULL) {
		BAlert *alert = new BAlert(NULL, strerror(err), "OK");
		alert->Go();
	} else {
		if (fSettingsWindow) {
			fSettingsWindow->RemoveChild(fSettingsWindow->ChildAt(0));
			float width, height;
			view->GetPreferredSize(&width, &height);
			fSettingsWindow->ResizeTo(width, height);
			fSettingsWindow->AddChild(view);
			if (activate)
				fSettingsWindow->Activate();
		} else {
			fSettingsWindow = new BWindow(rect,
				B_TRANSLATE("Translator Settings"),
				B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
				B_NOT_ZOOMABLE | B_NOT_RESIZABLE);
			fSettingsWindow->AddFilter(new QuitMessageFilter(this));
			fSettingsWindow->AddChild(view);
			fSettingsWindow->CenterOnScreen();
			fSettingsWindow->Show();
		}
	}
}
Пример #8
0
void SketchApp::ReadyToRun()
{
	// Build a menu into your filepanel for fun and profit!
	SketchWindow	*mainWindow = ((SketchWindow *)FindWindow("Etch-A-Sketch"B_UTF8_REGISTERED));
	//BFilePanel		*saver = NULL;
	BMenuField		*formatMenu = NULL;
	BMenu			*menu = NULL;
	BView			*saverView = NULL;
	BRect			rect, bounds;
	menu_info		m_info;
	int32			menubarheight=0;
	
	// get some menu measurements
	get_menu_info(&m_info);
	menubarheight = (int32)m_info.font_size + 8;
	
	// make the filepanel
	saver = new BFilePanel(B_SAVE_PANEL, NULL, NULL, B_FILE_NODE, false); // _ctor
	
	if(saver->Window()->Lock())
	{
		// get a pointer to a view
		saverView = (BView *)saver->Window()->ChildAt(0);
	
		// get a rect for the filepanel's window
		bounds = saverView->Bounds();
	
		// make a menu
		menu = new BMenu("Format");
	
		// construct the BMenuFeild for the menu
		rect.Set(bounds.right - 100, bounds.top + menubarheight, bounds.right - 40, bounds.top + menubarheight + 15);
		formatMenu = new BMenuField(rect, "format menu", "", menu);
	
		formatMenu->SetDivider(0);
		
		// Below was ganked from the BeBook
		//find all the translators
		BTranslatorRoster *roster = BTranslatorRoster::Default(); 
		int32 num_translators, i; 
		translator_id *translators; 
		const char *translator_name, *translator_info;
		int32 translator_version; 

		roster->GetAllTranslators(&translators, &num_translators); // function allocates the memory for translators
		for (i=0;i<num_translators;i++)
		{ 
			const translation_format *fmts; 
        	int32 num_fmts;
			roster->GetTranslatorInfo(translators[i], &translator_name, &translator_info, &translator_version); 
   			roster->GetOutputFormats(translators[i], &fmts, &num_fmts);
   			
   			if(fmts[0].group == B_TRANSLATOR_BITMAP) // make sure we only get bitmap translators
   			{
   				BMessage *message = new BMessage(TRANSLATOR);
   				message->AddString("save format", fmts[0].MIME);
   				if(i == 0)
   					mainWindow->PostMessage(message); // this will init 'format' for us
					menu->AddItem(new BMenuItem(translator_name, message));
			}
			//printf("%s: %s (%.2f)\n", translator_name, translator_info, translator_version/100.); 
		} 
   
		delete [] translators; // clean up our droppings
		// found em all
		
		menu->SetRadioMode(true);
		menu->ItemAt(0)->SetMarked(true);
		menu->SetTargetForItems(mainWindow);
	
	
		saverView->AddChild(formatMenu);
		saver->Window()->Unlock();
	}
}