Make StyledEdit check its open windows before launching a new one for an entry_ref. Simplify the application class somewhat by removing DispatchMessage() and replacing the custom made ArgvReceivedEx() with a standard ArgvReceived(). Rely on BPath to take care of paths relative to the current working directory - don't do it manually.
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@30701 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
* Authors:
|
* Authors:
|
||||||
* Mattias Sundblad
|
* Mattias Sundblad
|
||||||
* Andrew Bachmann
|
* Andrew Bachmann
|
||||||
|
* Jonas Sundström
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
@@ -115,6 +116,7 @@ StyledEditApp::StyledEditApp()
|
|||||||
|
|
||||||
fWindowCount = 0;
|
fWindowCount = 0;
|
||||||
fNextUntitledWindow = 1;
|
fNextUntitledWindow = 1;
|
||||||
|
fBadArguments = false;
|
||||||
|
|
||||||
styled_edit_app = this;
|
styled_edit_app = this;
|
||||||
}
|
}
|
||||||
@@ -126,31 +128,6 @@ StyledEditApp::~StyledEditApp()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
StyledEditApp::DispatchMessage(BMessage *msg, BHandler *handler)
|
|
||||||
{
|
|
||||||
if (msg->what == B_ARGV_RECEIVED) {
|
|
||||||
int32 argc;
|
|
||||||
if (msg->FindInt32("argc", &argc) != B_OK)
|
|
||||||
argc = 0;
|
|
||||||
|
|
||||||
const char** argv = new const char*[argc];
|
|
||||||
for (int arg = 0; arg < argc; arg++) {
|
|
||||||
if (msg->FindString("argv", arg, &argv[arg]) != B_OK) {
|
|
||||||
argv[arg] = "";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const char* cwd;
|
|
||||||
if (msg->FindString("cwd", &cwd) != B_OK)
|
|
||||||
cwd = "";
|
|
||||||
|
|
||||||
ArgvReceivedEx(argc, argv, cwd);
|
|
||||||
delete[] argv;
|
|
||||||
} else
|
|
||||||
BApplication::DispatchMessage(msg, handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
StyledEditApp::MessageReceived(BMessage *message)
|
StyledEditApp::MessageReceived(BMessage *message)
|
||||||
{
|
{
|
||||||
@@ -188,12 +165,56 @@ StyledEditApp::OpenDocument()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
status_t
|
||||||
StyledEditApp::OpenDocument(entry_ref* ref)
|
StyledEditApp::OpenDocument(entry_ref* ref)
|
||||||
{
|
{
|
||||||
|
// traverse eventual symlink
|
||||||
|
BEntry entry(ref, true);
|
||||||
|
entry.GetRef(ref);
|
||||||
|
|
||||||
|
if (entry.IsDirectory()) {
|
||||||
|
BPath path(&entry);
|
||||||
|
fprintf(stderr,
|
||||||
|
"Can't open directory \"%s\" for editing.\n",
|
||||||
|
path.Path());
|
||||||
|
return B_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
BEntry parent;
|
||||||
|
entry.GetParent(&parent);
|
||||||
|
|
||||||
|
if (!entry.Exists() && !parent.Exists()) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"Can't create file. Missing parent directory.\n");
|
||||||
|
return B_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
BWindow* window = NULL;
|
||||||
|
StyledEditWindow* document = NULL;
|
||||||
|
|
||||||
|
for (int32 index = 0; ; index++) {
|
||||||
|
window = WindowAt(index);
|
||||||
|
if (window == NULL)
|
||||||
|
break;
|
||||||
|
|
||||||
|
document = dynamic_cast<StyledEditWindow*>(window);
|
||||||
|
if (document == NULL)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (document->IsDocumentEntryRef(ref)) {
|
||||||
|
if (document->Lock()) {
|
||||||
|
document->Activate();
|
||||||
|
document->Unlock();
|
||||||
|
return B_OK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cascade();
|
cascade();
|
||||||
new StyledEditWindow(gWindowRect, ref, fOpenAsEncoding);
|
new StyledEditWindow(gWindowRect, ref, fOpenAsEncoding);
|
||||||
fWindowCount++;
|
fWindowCount++;
|
||||||
|
|
||||||
|
return B_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -222,32 +243,18 @@ StyledEditApp::RefsReceived(BMessage *message)
|
|||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
StyledEditApp::ArgvReceivedEx(int32 argc, const char* argv[], const char* cwd)
|
StyledEditApp::ArgvReceived(int32 argc, char* argv[])
|
||||||
{
|
{
|
||||||
for (int i = 1 ; (i < argc) ; i++) {
|
for (int i = 1 ; (i < argc) ; i++) {
|
||||||
BPath path;
|
BPath path(argv[i]);
|
||||||
if (argv[i][0] == '/')
|
entry_ref ref;
|
||||||
path.SetTo(argv[i]);
|
get_ref_for_path(path.Path(), &ref);
|
||||||
else
|
|
||||||
path.SetTo(cwd, argv[i]);
|
status_t status;
|
||||||
|
status = OpenDocument(&ref);
|
||||||
|
|
||||||
if (path.InitCheck() != B_OK) {
|
if (status != B_OK && IsLaunching())
|
||||||
fprintf(stderr, "Setting path failed: \"");
|
fBadArguments = true;
|
||||||
if (argv[i][0] == '/')
|
|
||||||
fprintf(stderr, "%s\".\n", argv[i]);
|
|
||||||
else
|
|
||||||
fprintf(stderr, "%s/%s\".\n", cwd, argv[i]);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
entry_ref ref;
|
|
||||||
status_t status = get_ref_for_path(path.Path(), &ref);
|
|
||||||
if (status != B_OK) {
|
|
||||||
fprintf(stderr, "Could not open \"%s\": %s.\n", path.Path(), strerror(status));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
OpenDocument(&ref);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -255,7 +262,12 @@ StyledEditApp::ArgvReceivedEx(int32 argc, const char* argv[], const char* cwd)
|
|||||||
void
|
void
|
||||||
StyledEditApp::ReadyToRun()
|
StyledEditApp::ReadyToRun()
|
||||||
{
|
{
|
||||||
if (fWindowCount == 0)
|
if (fWindowCount > 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (fBadArguments)
|
||||||
|
Quit();
|
||||||
|
else
|
||||||
OpenDocument();
|
OpenDocument();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
* Authors:
|
* Authors:
|
||||||
* Mattias Sundblad
|
* Mattias Sundblad
|
||||||
* Andrew Bachmann
|
* Andrew Bachmann
|
||||||
|
* Jonas Sundström
|
||||||
*/
|
*/
|
||||||
#ifndef STYLED_EDIT_APP
|
#ifndef STYLED_EDIT_APP
|
||||||
#define STYLED_EDIT_APP
|
#define STYLED_EDIT_APP
|
||||||
@@ -28,14 +29,13 @@ class StyledEditApp : public BApplication {
|
|||||||
virtual ~StyledEditApp();
|
virtual ~StyledEditApp();
|
||||||
|
|
||||||
virtual void MessageReceived(BMessage *message);
|
virtual void MessageReceived(BMessage *message);
|
||||||
|
virtual void ArgvReceived(int32 argc, char** argv);
|
||||||
virtual void RefsReceived(BMessage *message);
|
virtual void RefsReceived(BMessage *message);
|
||||||
virtual void ReadyToRun();
|
virtual void ReadyToRun();
|
||||||
|
|
||||||
virtual void DispatchMessage(BMessage *an_event, BHandler *handler);
|
|
||||||
|
|
||||||
int32 NumberOfWindows();
|
int32 NumberOfWindows();
|
||||||
void OpenDocument();
|
void OpenDocument();
|
||||||
void OpenDocument(entry_ref *ref);
|
status_t OpenDocument(entry_ref *ref);
|
||||||
void CloseDocument();
|
void CloseDocument();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -47,7 +47,7 @@ class StyledEditApp : public BApplication {
|
|||||||
uint32 fOpenAsEncoding;
|
uint32 fOpenAsEncoding;
|
||||||
int32 fWindowCount;
|
int32 fWindowCount;
|
||||||
int32 fNextUntitledWindow;
|
int32 fNextUntitledWindow;
|
||||||
|
bool fBadArguments;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern StyledEditApp* styled_edit_app;
|
extern StyledEditApp* styled_edit_app;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
* Mattias Sundblad
|
* Mattias Sundblad
|
||||||
* Andrew Bachmann
|
* Andrew Bachmann
|
||||||
* Philippe Saint-Pierre
|
* Philippe Saint-Pierre
|
||||||
|
* Jonas Sundström
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "Constants.h"
|
#include "Constants.h"
|
||||||
@@ -27,6 +28,7 @@
|
|||||||
#include <Menu.h>
|
#include <Menu.h>
|
||||||
#include <MenuBar.h>
|
#include <MenuBar.h>
|
||||||
#include <MenuItem.h>
|
#include <MenuItem.h>
|
||||||
|
#include <Path.h>
|
||||||
#include <PrintJob.h>
|
#include <PrintJob.h>
|
||||||
#include <Rect.h>
|
#include <Rect.h>
|
||||||
#include <Roster.h>
|
#include <Roster.h>
|
||||||
@@ -1345,3 +1347,31 @@ StyledEditWindow::_ShowAlert(const BString& text, const BString& label,
|
|||||||
|
|
||||||
return alert->Go();
|
return alert->Go();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool
|
||||||
|
StyledEditWindow::IsDocumentEntryRef(const entry_ref* ref)
|
||||||
|
{
|
||||||
|
if (ref == NULL)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (fSaveMessage == NULL)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
entry_ref dir;
|
||||||
|
const char* name;
|
||||||
|
if (fSaveMessage->FindRef("directory", &dir) != B_OK
|
||||||
|
|| fSaveMessage->FindString("name", &name) != B_OK)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
entry_ref documentRef;
|
||||||
|
BPath documentPath(&dir);
|
||||||
|
documentPath.Append(name);
|
||||||
|
get_ref_for_path(documentPath.Path(), &documentRef);
|
||||||
|
|
||||||
|
if (*ref == documentRef)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
* Authors:
|
* Authors:
|
||||||
* Mattias Sundblad
|
* Mattias Sundblad
|
||||||
* Andrew Bachmann
|
* Andrew Bachmann
|
||||||
|
* Jonas Sundström
|
||||||
*/
|
*/
|
||||||
#ifndef STYLED_EDIT_WINDOW_H
|
#ifndef STYLED_EDIT_WINDOW_H
|
||||||
#define STYLED_EDIT_WINDOW_H
|
#define STYLED_EDIT_WINDOW_H
|
||||||
@@ -43,6 +44,7 @@ class StyledEditWindow : public BWindow {
|
|||||||
status_t PageSetup(const char *documentname);
|
status_t PageSetup(const char *documentname);
|
||||||
void Print(const char *documentname);
|
void Print(const char *documentname);
|
||||||
void SearchAllWindows(BString find, BString replace, bool casesens);
|
void SearchAllWindows(BString find, BString replace, bool casesens);
|
||||||
|
bool IsDocumentEntryRef(const entry_ref *ref);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void InitWindow(uint32 encoding = 0);
|
void InitWindow(uint32 encoding = 0);
|
||||||
|
|||||||
Reference in New Issue
Block a user