6 Copyright (c) 1991-2001, Be Incorporated. All rights reserved.
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN
23 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
29 BeMail(TM), Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or
30 registered trademarks of Be Incorporated in the United States and other
31 countries. Other brand product names are registered trademarks or trademarks
32 of their respective holders. All rights reserved.
36 #include "Signature.h"
42 #include <Clipboard.h>
43 #include <Directory.h>
44 #include <LayoutBuilder.h>
46 #include <ScrollView.h>
47 #include <StringView.h>
50 #include "MailPopUpMenu.h"
51 #include "MailSupport.h"
52 #include "MailWindow.h"
56 #define B_TRANSLATION_CONTEXT "Mail"
59 const float kSigHeight
= 250;
60 const float kSigWidth
= 300;
62 extern const char* kUndoStrings
[];
63 extern const char* kRedoStrings
[];
66 TSignatureWindow::TSignatureWindow(BRect rect
)
68 BWindow(rect
, B_TRANSLATE("Signatures"), B_TITLED_WINDOW
,
69 B_AUTO_UPDATE_SIZE_LIMITS
),
75 BMenuBar
* menuBar
= new BMenuBar("MenuBar");
76 BMenu
* menu
= new BMenu(B_TRANSLATE("Signature"));
77 menu
->AddItem(fNew
= new BMenuItem(B_TRANSLATE("New"),
78 new BMessage(M_NEW
), 'N'));
79 fSignature
= new TMenu(B_TRANSLATE("Open"), INDEX_SIGNATURE
, M_SIGNATURE
);
80 menu
->AddItem(new BMenuItem(fSignature
));
81 menu
->AddSeparatorItem();
82 menu
->AddItem(fSave
= new BMenuItem(B_TRANSLATE("Save"),
83 new BMessage(M_SAVE
), 'S'));
84 menu
->AddItem(fDelete
= new BMenuItem(B_TRANSLATE("Delete"),
85 new BMessage(M_DELETE
), 'T'));
86 menuBar
->AddItem(menu
);
88 menu
= new BMenu(B_TRANSLATE("Edit"));
89 menu
->AddItem(fUndo
= new BMenuItem(B_TRANSLATE("Undo"),
90 new BMessage(B_UNDO
), 'Z'));
91 fUndo
->SetTarget(NULL
, this);
92 menu
->AddSeparatorItem();
93 menu
->AddItem(fCut
= new BMenuItem(B_TRANSLATE("Cut"),
94 new BMessage(B_CUT
), 'X'));
95 fCut
->SetTarget(NULL
, this);
96 menu
->AddItem(fCopy
= new BMenuItem(B_TRANSLATE("Copy"),
97 new BMessage(B_COPY
), 'C'));
98 fCopy
->SetTarget(NULL
, this);
99 menu
->AddItem(fPaste
= new BMenuItem(B_TRANSLATE("Paste"),
100 new BMessage(B_PASTE
), 'V'));
101 fPaste
->SetTarget(NULL
, this);
102 menu
->AddSeparatorItem();
103 menu
->AddItem(item
= new BMenuItem(B_TRANSLATE("Select all"),
104 new BMessage(M_SELECT
), 'A'));
105 item
->SetTarget(NULL
, this);
106 menuBar
->AddItem(menu
);
108 fSigView
= new TSignatureView();
110 BLayoutBuilder::Group
<>(this, B_VERTICAL
, 0)
114 if (!rect
.IsValid()) {
115 float fontFactor
= be_plain_font
->Size() / 12.0f
;
116 ResizeTo(kSigWidth
* fontFactor
, kSigHeight
* fontFactor
);
117 // TODO: this should work, too, but doesn't
118 //ResizeToPreferred();
123 TSignatureWindow::~TSignatureWindow()
129 TSignatureWindow::MenusBeginning()
131 fDelete
->SetEnabled(fFile
);
132 fSave
->SetEnabled(IsDirty());
133 fUndo
->SetEnabled(false); // ***TODO***
135 BTextView
* textView
= fSigView
->fName
->TextView();
138 if (textView
->IsFocus())
139 textView
->GetSelection(&start
, &finish
);
141 fSigView
->fTextView
->GetSelection(&start
, &finish
);
143 fCut
->SetEnabled(start
!= finish
);
144 fCopy
->SetEnabled(start
!= finish
);
146 fNew
->SetEnabled(textView
->TextLength()
147 | fSigView
->fTextView
->TextLength());
148 be_clipboard
->Lock();
149 fPaste
->SetEnabled(be_clipboard
->Data()->HasData("text/plain",
151 be_clipboard
->Unlock();
155 undo_state undoState
= B_UNDO_UNAVAILABLE
;
157 BTextView
*focusTextView
= dynamic_cast<BTextView
*>(CurrentFocus());
158 if (focusTextView
!= NULL
)
159 undoState
= focusTextView
->UndoState(&isRedo
);
161 fUndo
->SetLabel(isRedo
? kRedoStrings
[undoState
] : kUndoStrings
[undoState
]);
162 fUndo
->SetEnabled(undoState
!= B_UNDO_UNAVAILABLE
);
167 TSignatureWindow::MessageReceived(BMessage
* msg
)
173 msg
->FindPointer("font", (void **)&font
);
174 fSigView
->fTextView
->SetFontAndColor(font
);
175 fSigView
->fTextView
->Invalidate(fSigView
->fTextView
->Bounds());
181 fSigView
->fName
->SetText("");
182 fSigView
->fTextView
->SetText("");
183 fSigView
->fName
->MakeFocus(true);
192 BAlert
* alert
= new BAlert("",
193 B_TRANSLATE("Really delete this signature? This cannot "
195 B_TRANSLATE("Cancel"),
196 B_TRANSLATE("Delete"),
197 NULL
, B_WIDTH_AS_USUAL
, B_WARNING_ALERT
);
198 alert
->SetShortcut(0, B_ESCAPE
);
199 int32 choice
= alert
->Go();
208 fSigView
->fName
->SetText("");
209 fSigView
->fTextView
->SetText(NULL
, (int32
)0);
210 fSigView
->fName
->MakeFocus(true);
217 msg
->FindRef("ref", &ref
);
219 fFile
= new BFile(&ref
, O_RDWR
);
220 if (fFile
->InitCheck() == B_OK
) {
221 char name
[B_FILE_NAME_LENGTH
];
222 fFile
->ReadAttr(INDEX_SIGNATURE
, B_STRING_TYPE
, 0, name
,
224 fSigView
->fName
->SetText(name
);
227 fFile
->GetSize(&size
);
228 char* sig
= (char*)malloc(size
);
232 size
= fFile
->Read(sig
, size
);
233 fSigView
->fTextView
->SetText(sig
, size
);
234 fSigView
->fName
->MakeFocus(true);
235 BTextView
* textView
= fSigView
->fName
->TextView();
236 textView
->Select(0, textView
->TextLength());
237 fSigView
->fTextView
->fDirty
= false;
241 BAlert
* alert
= new BAlert("",
242 B_TRANSLATE("Couldn't open this signature. Sorry."),
244 alert
->SetFlags(alert
->Flags() | B_CLOSE_ON_ESCAPE
);
251 BWindow::MessageReceived(msg
);
257 TSignatureWindow::QuitRequested()
260 BMessage
msg(WINDOW_CLOSED
);
261 msg
.AddInt32("kind", SIG_WINDOW
);
262 msg
.AddRect("window frame", Frame());
264 be_app
->PostMessage(&msg
);
272 TSignatureWindow::FrameResized(float width
, float height
)
274 fSigView
->FrameResized(width
, height
);
279 TSignatureWindow::Show()
282 BTextView
* textView
= (BTextView
*)fSigView
->fName
->TextView();
283 fSigView
->fName
->MakeFocus(true);
284 textView
->Select(0, textView
->TextLength());
292 TSignatureWindow::Clear()
296 BAlert
*alert
= new BAlert("",
297 B_TRANSLATE("Save changes to this signature?"),
298 B_TRANSLATE("Cancel"),
299 B_TRANSLATE("Don't save"),
301 B_WIDTH_AS_USUAL
, B_OFFSET_SPACING
, B_WARNING_ALERT
);
302 alert
->SetShortcut(0, B_ESCAPE
);
303 alert
->SetShortcut(1, 'd');
304 alert
->SetShortcut(2, 's');
305 int32 result
= alert
->Go();
314 fSigView
->fTextView
->fDirty
= false;
320 TSignatureWindow::IsDirty()
323 char name
[B_FILE_NAME_LENGTH
];
324 fFile
->ReadAttr(INDEX_SIGNATURE
, B_STRING_TYPE
, 0, name
, sizeof(name
));
325 if (strcmp(name
, fSigView
->fName
->Text()) != 0
326 || fSigView
->fTextView
->fDirty
) {
329 } else if (fSigView
->fName
->Text()[0] != '\0'
330 || fSigView
->fTextView
->TextLength() != 0) {
338 TSignatureWindow::Save()
340 char name
[B_FILE_NAME_LENGTH
];
349 find_directory(B_USER_SETTINGS_DIRECTORY
, &path
, true);
350 dir
.SetTo(path
.Path());
352 if (dir
.FindEntry("Mail", &entry
) == B_NO_ERROR
)
355 dir
.CreateDirectory("Mail", &dir
);
357 if (dir
.InitCheck() != B_NO_ERROR
)
360 if (dir
.FindEntry("signatures", &entry
) == B_NO_ERROR
)
363 dir
.CreateDirectory("signatures", &dir
);
365 if (dir
.InitCheck() != B_NO_ERROR
)
370 sprintf(name
, "signature_%" B_PRId32
, index
++);
371 if ((result
= dir
.CreateFile(name
, fFile
, true)) == B_NO_ERROR
)
373 if (result
!= EEXIST
)
376 dir
.FindEntry(name
, &fEntry
);
377 node
= new BNodeInfo(fFile
);
378 node
->SetType("text/plain");
382 fSigView
->fTextView
->fDirty
= false;
384 fFile
->Write(fSigView
->fTextView
->Text(),
385 fSigView
->fTextView
->TextLength());
386 fFile
->SetSize(fFile
->Position());
387 fFile
->WriteAttr(INDEX_SIGNATURE
, B_STRING_TYPE
, 0, fSigView
->fName
->Text(),
388 strlen(fSigView
->fName
->Text()) + 1);
393 BAlert
* alert
= new BAlert("",
394 B_TRANSLATE("An error occurred trying to save this signature."),
395 B_TRANSLATE("Sorry"));
396 alert
->SetFlags(alert
->Flags() | B_CLOSE_ON_ESCAPE
);
404 TSignatureView::TSignatureView()
408 GridLayout()->SetInsets(B_USE_DEFAULT_SPACING
);
410 BStringView
* nameLabel
= new BStringView("NameLabel",
411 B_TRANSLATE("Title:"));
412 nameLabel
->SetAlignment(B_ALIGN_RIGHT
);
413 GridLayout()->AddView(nameLabel
, 0, 0);
415 fName
= new TNameControl("", new BMessage(NAME_FIELD
));
416 GridLayout()->AddItem(fName
->CreateTextViewLayoutItem(), 1, 0);
418 BStringView
* signatureLabel
= new BStringView("SigLabel",
419 B_TRANSLATE("Signature:"));
420 signatureLabel
->SetAlignment(B_ALIGN_RIGHT
);
421 GridLayout()->AddView(signatureLabel
, 0, 1);
423 fTextView
= new TSigTextView();
425 font_height fontHeight
;
426 fTextView
->GetFontHeight(&fontHeight
);
427 float lineHeight
= ceilf(fontHeight
.ascent
) + ceilf(fontHeight
.descent
);
429 BScrollView
* scroller
= new BScrollView("SigScroller", fTextView
, 0,
431 scroller
->SetExplicitPreferredSize(
432 BSize(fTextView
->StringWidth("W") * 30, lineHeight
* 6));
434 GridLayout()->AddView(scroller
, 1, 1, 1, 2);
436 GridLayout()->AddItem(BSpaceLayoutItem::CreateGlue(), 0, 2);
441 TSignatureView::AttachedToWindow()
449 TNameControl::TNameControl(const char* label
, BMessage
* invocationMessage
)
451 BTextControl("", label
, "", invocationMessage
)
453 strcpy(fLabel
, label
);
458 TNameControl::AttachedToWindow()
460 BTextControl::AttachedToWindow();
462 TextView()->SetMaxBytes(B_FILE_NAME_LENGTH
- 1);
467 TNameControl::MessageReceived(BMessage
* msg
)
471 TextView()->Select(0, TextView()->TextLength());
475 BTextControl::MessageReceived(msg
);
483 TSigTextView::TSigTextView()
485 BTextView("SignatureView", B_NAVIGABLE
| B_WILL_DRAW
)
493 TSigTextView::DeleteText(int32 offset
, int32 len
)
496 BTextView::DeleteText(offset
, len
);
501 TSigTextView::InsertText(const char *text
, int32 len
, int32 offset
,
502 const text_run_array
*runs
)
505 BTextView::InsertText(text
, len
, offset
, runs
);
510 TSigTextView::KeyDown(const char *key
, int32 count
)
523 Select(TextLength(), TextLength());
531 height
= (int32
)((up
? r
.top
- r
.bottom
: r
.bottom
- r
.top
) - 25);
532 if ((up
) && (!r
.top
))
538 BTextView::KeyDown(key
, count
);
544 TSigTextView::MessageReceived(BMessage
*msg
)
546 char type
[B_FILE_NAME_LENGTH
];
557 if (msg
->HasRef("refs")) {
558 msg
->FindRef("refs", &ref
);
559 file
.SetTo(&ref
, O_RDONLY
);
560 if (file
.InitCheck() == B_NO_ERROR
) {
561 node
= new BNodeInfo(&file
);
565 if ((!strncasecmp(type
, "text/", 5)) && (size
)) {
566 text
= (char *)malloc(size
);
567 file
.Read(text
, size
);
569 GetSelection(&start
, &end
);
571 Select(start
, start
+ size
);
577 BTextView::MessageReceived(msg
);
582 Select(0, TextLength());
586 BTextView::MessageReceived(msg
);