Files
haiku-beta6/src/apps/showimage/ShowImageApp.cpp
T

249 lines
4.7 KiB
C++
Raw Normal View History

2006-02-06 19:54:01 +00:00
/*
* Copyright 2003-2006, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Fernando Francisco de Oliveira
* Michael Wilber
* Michael Pfeiffer
*/
#include "ShowImageApp.h"
#include "ShowImageConstants.h"
#include "ShowImageWindow.h"
#include <Alert.h>
2006-02-06 19:54:01 +00:00
#include <Clipboard.h>
#include <FilePanel.h>
2006-02-06 19:54:01 +00:00
#include <Path.h>
#include <String.h>
2006-02-06 19:54:01 +00:00
#include <stdio.h>
#define WINDOWS_TO_IGNORE 1
2006-02-06 19:54:01 +00:00
extern const char *kApplicationSignature = "application/x-vnd.haiku-showimage";
ShowImageApp::ShowImageApp()
2006-02-06 19:54:01 +00:00
: BApplication(kApplicationSignature)
{
fPulseStarted = false;
fOpenPanel = new BFilePanel(B_OPEN_PANEL);
}
2006-02-06 19:54:01 +00:00
ShowImageApp::~ShowImageApp()
{
}
void
ShowImageApp::AboutRequested()
{
2006-02-06 19:54:01 +00:00
BAlert* alert = new BAlert("About ShowImage",
2006-04-26 03:05:50 +00:00
"Haiku ShowImage\n\nby Fernando F. Oliveira, Michael Wilber, Michael Pfeiffer and Ryan Leavengood", "OK");
2006-02-06 19:54:01 +00:00
alert->Go();
}
2006-02-06 19:54:01 +00:00
void
ShowImageApp::ReadyToRun()
{
if (CountWindows() == WINDOWS_TO_IGNORE)
fOpenPanel->Show();
2006-02-06 19:54:01 +00:00
else {
// If image windows are already open
// (paths supplied on the command line)
// start checking the number of open windows
StartPulse();
2006-02-06 19:54:01 +00:00
}
be_clipboard->StartWatching(be_app_messenger);
// tell the clipboard to notify this app when its contents change
}
2006-02-06 19:54:01 +00:00
void
ShowImageApp::StartPulse()
{
if (!fPulseStarted) {
// Tell the app to begin checking
// for the number of open windows
fPulseStarted = true;
SetPulseRate(250000);
// Set pulse to every 1/4 second
}
}
2006-02-06 19:54:01 +00:00
void
ShowImageApp::Pulse()
{
// Bug: The BFilePanel is automatically closed if the volume that
// is displayed is unmounted.
2006-02-06 19:54:01 +00:00
if (!IsLaunching() && CountWindows() <= WINDOWS_TO_IGNORE) {
// If the application is not launching and
// all windows are closed except for the file open panel,
// quit the application
PostMessage(B_QUIT_REQUESTED);
2006-02-06 19:54:01 +00:00
}
}
2006-02-06 19:54:01 +00:00
void
ShowImageApp::ArgvReceived(int32 argc, char **argv)
{
2006-02-06 19:54:01 +00:00
BMessage message;
bool hasRefs = false;
// get current working directory
const char *cwd;
if (CurrentMessage() == NULL || CurrentMessage()->FindString("cwd", &cwd) != B_OK)
cwd = "";
for (int32 i = 1; i < argc; i++) {
BPath path;
if (argv[i][0] == '/') {
// absolute path
path.SetTo(argv[i]);
} else {
// relative path
path.SetTo(cwd);
path.Append(argv[i]);
}
2006-02-06 19:54:01 +00:00
entry_ref ref;
2006-02-06 19:54:01 +00:00
status_t err = get_ref_for_path(path.Path(), &ref);
if (err == B_OK) {
2006-02-06 19:54:01 +00:00
message.AddRef("refs", &ref);
hasRefs = true;
}
}
2006-02-06 19:54:01 +00:00
if (hasRefs)
RefsReceived(&message);
}
2006-02-06 19:54:01 +00:00
void
2006-02-06 19:54:01 +00:00
ShowImageApp::MessageReceived(BMessage *message)
{
2006-02-06 19:54:01 +00:00
switch (message->what) {
case MSG_FILE_OPEN:
fOpenPanel->Show();
break;
2006-02-06 19:54:01 +00:00
case MSG_WINDOW_QUIT:
break;
2006-02-06 19:54:01 +00:00
case B_CANCEL:
// File open panel was closed,
// start checking count of open windows
StartPulse();
break;
2006-02-06 19:54:01 +00:00
case B_CLIPBOARD_CHANGED:
CheckClipboard();
break;
default:
2006-02-06 19:54:01 +00:00
BApplication::MessageReceived(message);
break;
}
}
2006-02-06 19:54:01 +00:00
void
2006-02-06 19:54:01 +00:00
ShowImageApp::RefsReceived(BMessage *message)
{
// If a tracker window opened me, get a messenger from it.
2006-02-06 19:54:01 +00:00
if (message->HasMessenger("TrackerViewToken"))
message->FindMessenger("TrackerViewToken", &fTrackerMessenger);
uint32 type;
int32 count;
2006-02-06 19:54:01 +00:00
status_t ret = message->GetInfo("refs", &type, &count);
if (ret != B_OK || type != B_REF_TYPE)
return;
entry_ref ref;
for (int32 i = 0; i < count; i++) {
2006-02-06 19:54:01 +00:00
if (message->FindRef("refs", i, &ref) == B_OK)
Open(&ref);
}
}
2006-02-06 19:54:01 +00:00
void
2006-02-06 19:54:01 +00:00
ShowImageApp::Open(const entry_ref *ref)
{
2006-02-06 19:54:01 +00:00
new ShowImageWindow(ref, fTrackerMessenger);
}
2003-11-09 07:57:55 +00:00
2006-02-06 19:54:01 +00:00
void
2006-02-06 19:54:01 +00:00
ShowImageApp::BroadcastToWindows(BMessage *message)
{
2006-02-06 19:54:01 +00:00
const int32 count = CountWindows();
for (int32 i = 0; i < count; i ++) {
// BMessenger checks for us if BWindow is still a valid object
BMessenger msgr(WindowAt(i));
2006-02-06 19:54:01 +00:00
msgr.SendMessage(message);
}
}
2006-02-06 19:54:01 +00:00
void
ShowImageApp::CheckClipboard()
{
// Determines if the contents of the clipboard contain
// data that is useful to this application.
// After checking the clipboard, a message is sent to
// all windows indicating that the clipboard has changed
// and whether or not the clipboard contains useful data.
2006-02-06 19:54:01 +00:00
bool dataAvailable = false;
if (be_clipboard->Lock()) {
BMessage *clip = be_clipboard->Data();
if (clip != NULL) {
BString className;
if (clip->FindString("class", &className) == B_OK) {
if (className == "BBitmap")
dataAvailable = true;
}
}
2006-02-06 19:54:01 +00:00
be_clipboard->Unlock();
}
2006-02-06 19:54:01 +00:00
BMessage msg(MSG_CLIPBOARD_CHANGED);
2006-02-06 19:54:01 +00:00
msg.AddBool("data_available", dataAvailable);
BroadcastToWindows(&msg);
}
2006-02-06 19:54:01 +00:00
bool
ShowImageApp::QuitRequested()
{
be_clipboard->StopWatching(be_app_messenger);
// tell clipboard we don't want anymore notification
2006-02-06 19:54:01 +00:00
return true;
}
// #pragma mark -
int
main(int, char **)
{
ShowImageApp theApp;
theApp.Run();
return 0;
}