virtualkeyboard: some fixes

Add initialization helpers.
This commit is contained in:
Freeman Lou 2014-01-02 14:48:17 +00:00 committed by François Revol
parent c099b74eac
commit 2e4c86a4d2
5 changed files with 163 additions and 120 deletions

View File

@ -2,16 +2,14 @@ SubDir HAIKU_TOP src apps virtualkeyboard ;
UsePrivateHeaders interface shared ;
Application VirtualKeyboard :
Addon <input> VirtualKeyboard :
KeyboardLayout.cpp
KeyboardLayoutView.cpp
Keymap.cpp
KeymapListItem.cpp
VirtualKeyboardApp.cpp
VirtualKeyboardWindow.cpp
: be tracker localestub libshared.a $(TARGET_LIBSTDC++)
: VirtualKeyboard.rdef
: be tracker input_server localestub libshared.a $(TARGET_LIBSTDC++)
;
DoCatalogs Sudoku :
@ -21,6 +19,5 @@ DoCatalogs Sudoku :
KeyboardLayoutView.cpp
Keymap.cpp
KeymapListItem.cpp
VirtualKeyboardApp.cpp
VirtualKeyboardWindow.cpp
;

View File

@ -70,6 +70,13 @@ KeyboardLayoutView::SetKeymap(Keymap* keymap)
}
void
KeyboardLayoutView::SetEditable(bool editable)
{
fEditable = editable;
}
void
KeyboardLayoutView::SetTarget(BMessenger target)
{
@ -235,80 +242,7 @@ void
KeyboardLayoutView::MouseMoved(BPoint point, uint32 transit,
const BMessage* dragMessage)
{
if (fKeymap == NULL)
return;
// prevent dragging for tertiary mouse button
if ((fButtons & B_TERTIARY_MOUSE_BUTTON) != 0)
return;
if (dragMessage != NULL) {
if (fEditable) {
_InvalidateKey(fDropTarget);
fDropPoint = point;
_EvaluateDropTarget(point);
}
return;
}
int32 buttons;
if (Window()->CurrentMessage() == NULL
|| Window()->CurrentMessage()->FindInt32("buttons", &buttons) != B_OK
|| buttons == 0)
return;
if (fDragKey == NULL && (fabs(point.x - fClickPoint.x) > 4
|| fabs(point.y - fClickPoint.y) > 4)) {
// start dragging
Key* key = _KeyAt(fClickPoint);
if (key == NULL)
return;
BRect frame = _FrameFor(key);
BPoint offset = fClickPoint - frame.LeftTop();
frame.OffsetTo(B_ORIGIN);
BRect rect = frame;
rect.right--;
rect.bottom--;
BBitmap* bitmap = new BBitmap(rect, B_RGBA32, true);
bitmap->Lock();
BView* view = new BView(rect, "drag", B_FOLLOW_NONE, 0);
bitmap->AddChild(view);
view->SetHighColor(0, 0, 0, 0);
view->FillRect(view->Bounds());
view->SetDrawingMode(B_OP_ALPHA);
view->SetHighColor(0, 0, 0, 128);
// set the level of transparency by value
view->SetBlendingMode(B_CONSTANT_ALPHA, B_ALPHA_COMPOSITE);
_DrawKey(view, frame, key, frame, false);
view->Sync();
bitmap->Unlock();
BMessage drag(B_MIME_DATA);
drag.AddInt32("key", key->code);
char* string;
int32 numBytes;
fKeymap->GetChars(key->code, fModifiers, fDeadKey, &string,
&numBytes);
if (string != NULL) {
drag.AddData("text/plain", B_MIME_DATA, string, numBytes);
delete[] string;
}
DragMessage(&drag, bitmap, B_OP_ALPHA, offset);
fDragKey = key;
fDragModifiers = fModifiers;
fKeyState[key->code / 8] &= ~(1 << (7 - (key->code & 7)));
_InvalidateKey(key);
}
}

View File

@ -1,34 +0,0 @@
/*
* Copyright 2014 Freeman Lou <freemanlou2430@yahoo.com>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include <Application.h>
#include "VirtualKeyboardWindow.h"
class VirtualKeyboardApp : public BApplication{
public:
VirtualKeyboardApp();
private:
VirtualKeyboardWindow* fWindow;
};
VirtualKeyboardApp::VirtualKeyboardApp()
:
BApplication("application/x-vnd.VirtualKeyboard")
{
fWindow = new VirtualKeyboardWindow();
fWindow->Show();
}
int
main()
{
VirtualKeyboardApp app;
app.Run();
return 0;
}

View File

@ -4,27 +4,155 @@
*/
#include "VirtualKeyboardWindow.h"
#include <Directory.h>
#include <Entry.h>
#include <FindDirectory.h>
#include <GroupLayoutBuilder.h>
#include <ListView.h>
#include <Locale.h>
#include <Menu.h>
#include <MenuItem.h>
#include <Path.h>
#include <Screen.h>
#include "KeyboardLayoutView.h"
#include "KeymapListItem.h"
#include "Keymap.h"
static const uint32 kMsgMenuFontChange = 'MMFC';
static int
compare_key_list_items(const void* a, const void* b)
{
KeymapListItem* item1 = *(KeymapListItem**)a;
KeymapListItem* item2 = *(KeymapListItem**)b;
return BLocale::Default()->StringCompare(item1->Text(), item2->Text());
}
VirtualKeyboardWindow::VirtualKeyboardWindow()
:
BWindow(BRect(0,0,0,0),"Virtual Keyboard",
B_NO_BORDER_WINDOW_LOOK, B_FLOATING_ALL_WINDOW_FEEL,
B_NOT_RESIZABLE | B_WILL_ACCEPT_FIRST_CLICK)
B_WILL_ACCEPT_FIRST_CLICK | B_AVOID_FRONT)
{
BScreen screen;
BRect screenRect(screen.Frame());
ResizeTo(screenRect.Width(), screenRect.Height()/3);
MoveTo(0,screenRect.Height()-screenRect.Height()/3);
SetLayout(new BGroupLayout(B_VERTICAL));
//Add to an options window later, use as list for now
fMapListView = new BListView("Maps");
fFontMenu = new BMenu("Font");
fLayoutMenu = new BMenu("Layout");
_LoadMaps();
_LoadLayouts(fLayoutMenu);
_LoadFonts();
Keymap keymap;
KeymapListItem* current =
static_cast<KeymapListItem*>(fMapListView->FirstItem());
fCurrentKeymap.Load(current->EntryRef());
fKeyboardView = new KeyboardLayoutView("Keyboard");
fKeyboardView->SetKeymap(&keymap);
AddChild(fKeyboardView);
fKeyboardView->GetKeyboardLayout()->SetDefault();
fKeyboardView->SetEditable(false);
fKeyboardView->SetKeymap(&fCurrentKeymap);
AddChild(BGroupLayoutBuilder(B_VERTICAL)
.Add(fKeyboardView));
}
void
VirtualKeyboardWindow::_LoadLayouts(BMenu* menu)
{
directory_which dataDirectories[] = {
B_USER_NONPACKAGED_DATA_DIRECTORY,
B_USER_DATA_DIRECTORY,
B_SYSTEM_NONPACKAGED_DIRECTORY,
B_SYSTEM_DATA_DIRECTORY,
};
for (uint i = 0; i < sizeof(dataDirectories)/sizeof(directory_which); i++) {
BPath path;
if (find_directory(dataDirectories[i], &path) != B_OK)
continue;
path.Append("KeyboardLayouts");
BDirectory directory;
if (directory.SetTo(path.Path()) == B_OK)
_LoadLayoutMenu(menu, directory);
}
}
void
VirtualKeyboardWindow::_LoadLayoutMenu(BMenu* menu, BDirectory directory)
{
entry_ref ref;
while (directory.GetNextRef(&ref) == B_OK) {
if (menu->FindItem(ref.name) != NULL)
continue;
BDirectory subdirectory;
subdirectory.SetTo(&ref);
if (subdirectory.InitCheck() == B_OK) {
BMenu* submenu = new BMenu(ref.name);
_LoadLayoutMenu(submenu, subdirectory);
menu->AddItem(submenu);
} else {
//BMessage* message = new BMessage(kChangeKeyboardLayout);
//message->AddRed("ref",&ref);
menu->AddItem(new BMenuItem(ref.name, NULL));
}
}
}
void
VirtualKeyboardWindow::_LoadMaps()
{
BPath path;
if (find_directory(B_SYSTEM_DATA_DIRECTORY, &path) != B_OK)
return;
path.Append("Keymaps");
BDirectory directory;
entry_ref ref;
if (directory.SetTo(path.Path()) == B_OK) {
while (directory.GetNextRef(&ref) == B_OK) {
fMapListView->AddItem(new KeymapListItem(ref));
}
}
fMapListView->SortItems(&compare_key_list_items);
}
void
VirtualKeyboardWindow::_LoadFonts()
{
int32 numFamilies = count_font_families();
font_family family, currentFamily;
font_style currentStyle;
uint32 flags;
be_plain_font->GetFamilyAndStyle(&currentFamily,&currentStyle);
for (int32 i = 0; i< numFamilies; i++) {
if (get_font_family(i, &family, &flags) == B_OK) {
BMenuItem* item = new BMenuItem(family, NULL); //new BMessage(kMsgMenuFontChanged));
fFontMenu->AddItem(item);
if (!strcmp(family, currentFamily))
item->SetMarked(true);
}
}
}

View File

@ -7,15 +7,33 @@
#include <Window.h>
class KeyboardLayoutView;
#include <vector.h>
#include "Keymap.h"
class KeyboardLayoutView;
class Keymap;
class BDirectory;
class BListView;
class BMenu;
class VirtualKeyboardWindow : public BWindow{
public:
VirtualKeyboardWindow();
virtual void MessageReceived(BMessage* message);
virtual void MessageReceived(BMessage* message);
private:
KeyboardLayoutView* fKeyboardView;
BListView* fMapListView;
BMenu* fFontMenu;
BMenu* fLayoutMenu;
Keymap fCurrentKeymap;
private:
void _LoadLayouts(BMenu* menu);
void _LoadLayoutMenu(BMenu* menu, BDirectory directory);
void _LoadMaps();
void _LoadFonts();
};
#endif // VIRTUAL_KEYBOARD_WINDOW_H