2 * Copyright 2002-2016, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
12 #include "Constants.h"
13 #include "StyledEditApp.h"
14 #include "StyledEditWindow.h"
21 #include <CharacterSet.h>
22 #include <CharacterSetRoster.h>
23 #include <FilePanel.h>
32 using namespace BPrivate
;
35 static BRect
sWindowRect(7, 26, 507, 426);
36 static float sCascadeOffset
= 15;
37 static BPoint sTopLeft
= BPoint(7, 26);
46 BRect screenBorder
= screen
.Frame();
47 float left
= sWindowRect
.left
+ sCascadeOffset
;
48 if (left
+ sWindowRect
.Width() > screenBorder
.right
)
51 float top
= sWindowRect
.top
+ sCascadeOffset
;
52 if (top
+ sWindowRect
.Height() > screenBorder
.bottom
)
55 sWindowRect
.OffsetTo(BPoint(left
, top
));
63 BRect screenBorder
= screen
.Frame();
65 float left
= sWindowRect
.left
- sCascadeOffset
;
66 if (left
< sTopLeft
.x
) {
67 left
= screenBorder
.right
- sWindowRect
.Width() - sTopLeft
.x
;
68 left
= left
- ((int)left
% (int)sCascadeOffset
) + sTopLeft
.x
;
71 float top
= sWindowRect
.top
- sCascadeOffset
;
72 if (top
< sTopLeft
.y
) {
73 top
= screenBorder
.bottom
- sWindowRect
.Height() - sTopLeft
.y
;
74 top
= top
- ((int)left
% (int)sCascadeOffset
) + sTopLeft
.y
;
77 sWindowRect
.OffsetTo(BPoint(left
, top
));
85 #undef B_TRANSLATION_CONTEXT
86 #define B_TRANSLATION_CONTEXT "Open_and_SaveAsPanel"
89 StyledEditApp::StyledEditApp()
91 BApplication(APP_SIGNATURE
),
94 B_TRANSLATE_MARK_SYSTEM_NAME_VOID("StyledEdit");
96 fOpenPanel
= new BFilePanel();
100 = dynamic_cast<BMenuBar
*>(fOpenPanel
->Window()->FindView("MenuBar"));
101 if (menuBar
!= NULL
) {
102 fOpenPanelEncodingMenu
= new BMenu(B_TRANSLATE("Encoding"));
103 fOpenPanelEncodingMenu
->SetRadioMode(true);
105 menuBar
->AddItem(fOpenPanelEncodingMenu
);
107 BCharacterSetRoster roster
;
108 BCharacterSet charset
;
109 while (roster
.GetNextCharacterSet(&charset
) == B_NO_ERROR
) {
111 if (charset
.GetFontID() == B_UNICODE_UTF8
)
112 name
= B_TRANSLATE("Default");
114 name
= charset
.GetPrintName();
116 const char* mime
= charset
.GetMIMEName();
123 = new BMenuItem(name
.String(), new BMessage(OPEN_AS_ENCODING
));
124 item
->SetTarget(this);
125 fOpenPanelEncodingMenu
->AddItem(item
);
126 if (charset
.GetFontID() == fOpenAsEncoding
)
127 item
->SetMarked(true);
130 fOpenPanelEncodingMenu
= NULL
;
133 fNextUntitledWindow
= 1;
134 fBadArguments
= false;
136 float factor
= be_plain_font
->Size() / 12.0f
;
137 sCascadeOffset
*= factor
;
138 sTopLeft
.x
*= factor
;
139 sTopLeft
.y
*= factor
;
140 sWindowRect
.left
*= factor
;
141 sWindowRect
.top
*= factor
;
142 sWindowRect
.right
*= factor
;
143 sWindowRect
.bottom
*= factor
;
147 StyledEditApp::~StyledEditApp()
154 StyledEditApp::MessageReceived(BMessage
* message
)
156 switch (message
->what
) {
163 case B_SILENT_RELAUNCH
:
166 case OPEN_AS_ENCODING
:
168 if (message
->FindPointer("source", &ptr
) == B_OK
169 && fOpenPanelEncodingMenu
!= NULL
) {
170 fOpenAsEncoding
= (uint32
)fOpenPanelEncodingMenu
->IndexOf(
176 BApplication::MessageReceived(message
);
183 StyledEditApp::OpenDocument()
185 new StyledEditWindow(sWindowRect
, fNextUntitledWindow
++, fOpenAsEncoding
);
192 StyledEditApp::OpenDocument(entry_ref
* ref
, BMessage
* message
)
194 // traverse eventual symlink
195 BEntry
entry(ref
, true);
198 if (entry
.IsDirectory()) {
201 "Can't open directory \"%s\" for editing.\n",
207 entry
.GetParent(&parent
);
209 if (!entry
.Exists() && !parent
.Exists()) {
211 "Can't create file. Missing parent directory.\n");
215 BWindow
* window
= NULL
;
216 StyledEditWindow
* document
= NULL
;
218 for (int32 index
= 0; ; index
++) {
219 window
= WindowAt(index
);
223 document
= dynamic_cast<StyledEditWindow
*>(window
);
224 if (document
== NULL
)
227 if (document
->IsDocumentEntryRef(ref
)) {
228 if (document
->Lock()) {
229 document
->Activate();
232 document
->PostMessage(message
);
238 document
= new StyledEditWindow(sWindowRect
, ref
, fOpenAsEncoding
);
242 document
->PostMessage(message
);
251 StyledEditApp::CloseDocument()
255 if (fWindowCount
== 0) {
256 BAutolock
lock(this);
263 StyledEditApp::RefsReceived(BMessage
* message
)
268 while (message
->FindRef("refs", index
, &ref
) == B_OK
) {
270 if (message
->FindInt32("be:line", index
, &line
) != B_OK
)
273 if (message
->FindInt32("be:selection_length", index
, &length
) != B_OK
274 || message
->FindInt32("be:selection_offset", index
, &start
) != B_OK
)
280 BMessage
* selection
= NULL
;
281 if (line
>= 0 || (start
>= 0 && length
>= 0)) {
282 selection
= new BMessage(UPDATE_LINE_SELECTION
);
284 selection
->AddInt32("be:line", line
);
286 selection
->AddInt32("be:selection_offset", start
);
287 selection
->AddInt32("be:selection_length", max_c(0, length
));
291 OpenDocument(&ref
, selection
);
298 StyledEditApp::ArgvReceived(int32 argc
, char* argv
[])
300 // If StyledEdit is already running and gets invoked again
301 // we need to account for a possible mismatch in current
302 // working directory. The paths of the new arguments are
303 // relative to the cwd of the invocation, if they are not
304 // absolute. This cwd we find as a string named "cwd" in
305 // the BLooper's current message.
307 const char* cwd
= "";
308 BMessage
* message
= CurrentMessage();
310 if (message
!= NULL
) {
311 if (message
->FindString("cwd", &cwd
) != B_OK
)
315 for (int i
= 1 ; (i
< argc
) ; i
++) {
317 if (argv
[i
][0] == '/') {
320 path
.SetTo(cwd
, argv
[i
]);
321 // patch relative paths only
325 get_ref_for_path(path
.Path(), &ref
);
328 status
= OpenDocument(&ref
);
330 if (status
!= B_OK
&& IsLaunching())
331 fBadArguments
= true;
337 StyledEditApp::ReadyToRun()
339 if (fWindowCount
> 0)
350 StyledEditApp::NumberOfWindows()
360 main(int argc
, char** argv
)
362 StyledEditApp styledEdit
;