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"
|
2002-07-22 23:59:33 +00:00
|
|
|
|
|
|
|
|
#include <Alert.h>
|
2006-02-06 19:54:01 +00:00
|
|
|
#include <Clipboard.h>
|
2002-07-22 23:59:33 +00:00
|
|
|
#include <FilePanel.h>
|
2006-02-06 19:54:01 +00:00
|
|
|
#include <Path.h>
|
2003-11-19 01:46:00 +00:00
|
|
|
#include <String.h>
|
2002-07-22 23:59:33 +00:00
|
|
|
|
2006-02-06 19:54:01 +00:00
|
|
|
#include <stdio.h>
|
2002-07-22 23:59:33 +00:00
|
|
|
|
|
|
|
|
|
2003-08-02 16:53:13 +00:00
|
|
|
#define WINDOWS_TO_IGNORE 1
|
|
|
|
|
|
2006-02-06 19:54:01 +00:00
|
|
|
extern const char *kApplicationSignature = "application/x-vnd.haiku-showimage";
|
|
|
|
|
|
|
|
|
|
|
2002-07-22 23:59:33 +00:00
|
|
|
ShowImageApp::ShowImageApp()
|
2006-02-06 19:54:01 +00:00
|
|
|
: BApplication(kApplicationSignature)
|
2003-08-02 16:53:13 +00:00
|
|
|
{
|
2003-11-19 10:41:51 +00:00
|
|
|
fPulseStarted = false;
|
|
|
|
|
fOpenPanel = new BFilePanel(B_OPEN_PANEL);
|
2003-08-02 16:53:13 +00:00
|
|
|
}
|
2002-07-22 23:59:33 +00:00
|
|
|
|
2006-02-06 19:54:01 +00:00
|
|
|
|
|
|
|
|
ShowImageApp::~ShowImageApp()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-08-02 20:04:53 +00:00
|
|
|
void
|
|
|
|
|
ShowImageApp::AboutRequested()
|
2002-07-22 23:59:33 +00:00
|
|
|
{
|
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();
|
2002-07-22 23:59:33 +00:00
|
|
|
}
|
|
|
|
|
|
2006-02-06 19:54:01 +00:00
|
|
|
|
2003-08-02 20:04:53 +00:00
|
|
|
void
|
|
|
|
|
ShowImageApp::ReadyToRun()
|
2002-07-22 23:59:33 +00:00
|
|
|
{
|
2003-08-02 16:53:13 +00:00
|
|
|
if (CountWindows() == WINDOWS_TO_IGNORE)
|
2003-11-19 10:41:51 +00:00
|
|
|
fOpenPanel->Show();
|
2006-02-06 19:54:01 +00:00
|
|
|
else {
|
2003-08-02 16:53:13 +00:00
|
|
|
// 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
|
|
|
}
|
2003-11-19 01:46:00 +00:00
|
|
|
|
|
|
|
|
be_clipboard->StartWatching(be_app_messenger);
|
|
|
|
|
// tell the clipboard to notify this app when its contents change
|
2003-08-02 16:53:13 +00:00
|
|
|
}
|
|
|
|
|
|
2006-02-06 19:54:01 +00:00
|
|
|
|
2003-08-02 16:53:13 +00:00
|
|
|
void
|
|
|
|
|
ShowImageApp::StartPulse()
|
|
|
|
|
{
|
2003-11-19 10:41:51 +00:00
|
|
|
if (!fPulseStarted) {
|
2003-08-02 16:53:13 +00:00
|
|
|
// Tell the app to begin checking
|
|
|
|
|
// for the number of open windows
|
2003-11-19 10:41:51 +00:00
|
|
|
fPulseStarted = true;
|
2003-08-02 16:53:13 +00:00
|
|
|
SetPulseRate(250000);
|
|
|
|
|
// Set pulse to every 1/4 second
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-06 19:54:01 +00:00
|
|
|
|
2003-08-02 16:53:13 +00:00
|
|
|
void
|
|
|
|
|
ShowImageApp::Pulse()
|
|
|
|
|
{
|
2003-11-25 17:03:14 +00:00
|
|
|
// 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) {
|
2003-08-02 16:53:13 +00:00
|
|
|
// 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
|
|
|
}
|
2003-11-27 18:35:54 +00:00
|
|
|
}
|
2002-07-22 23:59:33 +00:00
|
|
|
|
2006-02-06 19:54:01 +00:00
|
|
|
|
2003-08-02 20:04:53 +00:00
|
|
|
void
|
|
|
|
|
ShowImageApp::ArgvReceived(int32 argc, char **argv)
|
2002-07-22 23:59:33 +00:00
|
|
|
{
|
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 = "";
|
|
|
|
|
|
2003-08-02 20:04:53 +00:00
|
|
|
for (int32 i = 1; i < argc; i++) {
|
2006-02-07 13:14:19 +00:00
|
|
|
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
|
|
|
|
2002-07-22 23:59:33 +00:00
|
|
|
entry_ref ref;
|
2006-02-06 19:54:01 +00:00
|
|
|
status_t err = get_ref_for_path(path.Path(), &ref);
|
2002-07-22 23:59:33 +00:00
|
|
|
if (err == B_OK) {
|
2006-02-06 19:54:01 +00:00
|
|
|
message.AddRef("refs", &ref);
|
|
|
|
|
hasRefs = true;
|
2002-07-22 23:59:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-02-06 19:54:01 +00:00
|
|
|
|
|
|
|
|
if (hasRefs)
|
|
|
|
|
RefsReceived(&message);
|
2002-07-22 23:59:33 +00:00
|
|
|
}
|
|
|
|
|
|
2006-02-06 19:54:01 +00:00
|
|
|
|
2003-08-02 20:04:53 +00:00
|
|
|
void
|
2006-02-06 19:54:01 +00:00
|
|
|
ShowImageApp::MessageReceived(BMessage *message)
|
2002-07-22 23:59:33 +00:00
|
|
|
{
|
2006-02-06 19:54:01 +00:00
|
|
|
switch (message->what) {
|
2003-08-02 16:53:13 +00:00
|
|
|
case MSG_FILE_OPEN:
|
2003-11-19 10:41:51 +00:00
|
|
|
fOpenPanel->Show();
|
2003-08-02 16:53:13 +00:00
|
|
|
break;
|
2006-02-06 19:54:01 +00:00
|
|
|
|
2003-08-02 16:53:13 +00:00
|
|
|
case MSG_WINDOW_QUIT:
|
|
|
|
|
break;
|
2006-02-06 19:54:01 +00:00
|
|
|
|
2003-08-02 16:53:13 +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
|
|
|
|
2003-11-19 01:46:00 +00:00
|
|
|
case B_CLIPBOARD_CHANGED:
|
|
|
|
|
CheckClipboard();
|
|
|
|
|
break;
|
2002-07-22 23:59:33 +00:00
|
|
|
|
2003-08-02 16:53:13 +00:00
|
|
|
default:
|
2006-02-06 19:54:01 +00:00
|
|
|
BApplication::MessageReceived(message);
|
2003-08-02 16:53:13 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2002-07-22 23:59:33 +00:00
|
|
|
}
|
|
|
|
|
|
2006-02-06 19:54:01 +00:00
|
|
|
|
2003-08-02 20:04:53 +00:00
|
|
|
void
|
2006-02-06 19:54:01 +00:00
|
|
|
ShowImageApp::RefsReceived(BMessage *message)
|
2002-07-22 23:59:33 +00:00
|
|
|
{
|
2004-01-20 19:46:45 +00:00
|
|
|
// 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);
|
2004-01-20 19:46:45 +00:00
|
|
|
|
2002-07-22 23:59:33 +00:00
|
|
|
uint32 type;
|
|
|
|
|
int32 count;
|
2006-02-06 19:54:01 +00:00
|
|
|
status_t ret = message->GetInfo("refs", &type, &count);
|
2003-08-02 20:04:53 +00:00
|
|
|
if (ret != B_OK || type != B_REF_TYPE)
|
2002-07-22 23:59:33 +00:00
|
|
|
return;
|
2003-08-02 20:04:53 +00:00
|
|
|
|
|
|
|
|
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)
|
2002-07-22 23:59:33 +00:00
|
|
|
Open(&ref);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-06 19:54:01 +00:00
|
|
|
|
2003-08-02 20:04:53 +00:00
|
|
|
void
|
2006-02-06 19:54:01 +00:00
|
|
|
ShowImageApp::Open(const entry_ref *ref)
|
2002-07-22 23:59:33 +00:00
|
|
|
{
|
2006-02-06 19:54:01 +00:00
|
|
|
new ShowImageWindow(ref, fTrackerMessenger);
|
2002-07-22 23:59:33 +00:00
|
|
|
}
|
2003-11-09 07:57:55 +00:00
|
|
|
|
2006-02-06 19:54:01 +00:00
|
|
|
|
2003-11-19 01:46:00 +00:00
|
|
|
void
|
2006-02-06 19:54:01 +00:00
|
|
|
ShowImageApp::BroadcastToWindows(BMessage *message)
|
2003-11-19 01:46:00 +00:00
|
|
|
{
|
2006-02-06 19:54:01 +00:00
|
|
|
const int32 count = CountWindows();
|
|
|
|
|
for (int32 i = 0; i < count; i ++) {
|
2003-11-19 01:46:00 +00:00
|
|
|
// 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);
|
2003-11-19 01:46:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-06 19:54:01 +00:00
|
|
|
|
2003-11-19 01:46:00 +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;
|
2003-11-19 01:46:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-02-06 19:54:01 +00:00
|
|
|
|
2003-11-19 01:46:00 +00:00
|
|
|
be_clipboard->Unlock();
|
|
|
|
|
}
|
2006-02-06 19:54:01 +00:00
|
|
|
|
2003-11-19 01:46:00 +00:00
|
|
|
BMessage msg(MSG_CLIPBOARD_CHANGED);
|
2006-02-06 19:54:01 +00:00
|
|
|
msg.AddBool("data_available", dataAvailable);
|
2003-11-19 01:46:00 +00:00
|
|
|
BroadcastToWindows(&msg);
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-06 19:54:01 +00:00
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
ShowImageApp::QuitRequested()
|
2003-11-19 01:46:00 +00:00
|
|
|
{
|
|
|
|
|
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;
|
2003-11-19 01:46:00 +00:00
|
|
|
}
|
|
|
|
|
|