* Added support for copying characters into the clipboard via Command-C, and

a popup menu.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@29852 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2009-04-01 15:53:53 +00:00
parent 7e5ffaa223
commit a9fcf35b98
3 changed files with 95 additions and 2 deletions
+88 -2
View File
@@ -10,12 +10,19 @@
#include <string.h>
#include <Bitmap.h>
#include <Clipboard.h>
#include <LayoutUtils.h>
#include <MenuItem.h>
#include <PopUpMenu.h>
#include <ScrollBar.h>
#include <Window.h>
#include "UnicodeBlocks.h"
static const uint32 kMsgCopyAsEscapedString = 'cesc';
CharacterView::CharacterView(const char* name)
: BView(name, B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE | B_FRAME_EVENTS),
fTargetCommand(0),
@@ -150,9 +157,43 @@ CharacterView::UnicodeToUTF8Hex(uint32 c, char* text, size_t textSize)
}
void
CharacterView::MessageReceived(BMessage* message)
{
switch (message->what) {
case kMsgCopyAsEscapedString:
case B_COPY:
{
uint32 character;
if (message->FindInt32("character", (int32*)&character) != B_OK) {
if (!fHasCharacter)
break;
character = fCurrentCharacter;
}
char text[16];
if (message->what == kMsgCopyAsEscapedString)
UnicodeToUTF8Hex(character, text, sizeof(text));
else
UnicodeToUTF8(character, text, sizeof(text));
_CopyToClipboard(text);
break;
}
default:
BView::MessageReceived(message);
break;
}
}
void
CharacterView::AttachedToWindow()
{
Window()->AddShortcut('C', B_SHIFT_KEY,
new BMessage(kMsgCopyAsEscapedString), this);
}
@@ -180,7 +221,34 @@ CharacterView::FrameResized(float width, float height)
void
CharacterView::MouseDown(BPoint where)
{
fClickPoint = where;
int32 buttons;
if (!fHasCharacter
|| Window()->CurrentMessage() == NULL
|| Window()->CurrentMessage()->FindInt32("buttons", &buttons) != B_OK
|| (buttons & B_SECONDARY_MOUSE_BUTTON) == 0) {
// Memorize click point for dragging
fClickPoint = where;
return;
}
// Open pop-up menu
BPopUpMenu *menu = new BPopUpMenu(B_EMPTY_STRING, false, false);
menu->SetFont(be_plain_font);
BMessage* message = new BMessage(B_COPY);
message->AddInt32("character", fCurrentCharacter);
menu->AddItem(new BMenuItem("Copy Character", message, 'C'));
message = new BMessage(kMsgCopyAsEscapedString);
message->AddInt32("character", fCurrentCharacter);
menu->AddItem(new BMenuItem("Copy As Escaped Byte String",
message, 'C', B_SHIFT_KEY));
menu->SetTargetForItems(this);
ConvertToScreen(&where);
menu->Go(where, true, true, true);
}
@@ -404,7 +472,7 @@ CharacterView::_UpdateSize()
font_height fontHeight;
GetFontHeight(&fontHeight);
fTitleHeight = (int32)ceilf(fontHeight.ascent + fontHeight.descent
+ fontHeight.leading);
+ fontHeight.leading) + 2;
fTitleBase = (int32)ceilf(fontHeight.ascent);
// Find widest character
@@ -468,3 +536,21 @@ CharacterView::_UpdateSize()
Invalidate();
}
void
CharacterView::_CopyToClipboard(const char* text)
{
if (!be_clipboard->Lock())
return;
be_clipboard->Clear();
BMessage* clip = be_clipboard->Data();
if (clip != NULL) {
clip->AddData("text/plain", B_MIME_TYPE, text, strlen(text));
be_clipboard->Commit();
}
be_clipboard->Unlock();
}
+3
View File
@@ -38,6 +38,8 @@ public:
size_t textSize);
protected:
virtual void MessageReceived(BMessage* message);
virtual void AttachedToWindow();
virtual void DetachedFromWindow();
@@ -58,6 +60,7 @@ private:
bool _GetCharacterAt(BPoint point, uint32& character,
BRect* _frame = NULL);
void _UpdateSize();
void _CopyToClipboard(const char* text);
private:
BMessenger fTarget;
@@ -181,6 +181,10 @@ void
CharacterWindow::MessageReceived(BMessage* message)
{
switch (message->what) {
case B_COPY:
PostMessage(message, fCharacterView);
break;
case kMsgUnicodeBlockSelected:
{
int32 index;