1/*
2 * Copyright 2009, Axel D��rfler, axeld@pinc-software.de.
3 * Copyright 2013-2014 Haiku, Inc. All rights reserved.
4 * Distributed under the terms of the MIT License.
5 *
6 * Authors:
7 *		Axel D��rfler, axeld@pinc-software.de
8 *		John Scipione, jscipione@gmail.com
9 */
10#ifndef KEYBOARD_LAYOUT_VIEW_H
11#define KEYBOARD_LAYOUT_VIEW_H
12
13
14#include <Messenger.h>
15#include <View.h>
16
17#include "KeyboardLayout.h"
18
19
20class BInputServerDevice;
21class BMenuItem;
22class Keymap;
23
24
25class KeyboardLayoutView : public BView {
26public:
27								KeyboardLayoutView(const char* name,
28									BInputServerDevice* dev = NULL);
29								~KeyboardLayoutView();
30
31			void				SetKeyboardLayout(KeyboardLayout* layout);
32			void				SetKeymap(Keymap* keymap);
33			void				SetTarget(BMessenger target);
34
35			KeyboardLayout*		GetKeyboardLayout() { return fLayout; }
36
37			void				SetBaseFont(const BFont& font);
38
39			void				SetEditable(bool editable);
40
41protected:
42	virtual void				AttachedToWindow();
43	virtual void				FrameResized(float width, float height);
44	virtual BSize				MinSize();
45
46	virtual void				KeyDown(const char* bytes, int32 numBytes);
47	virtual void				KeyUp(const char* bytes, int32 numBytes);
48	virtual void				MouseDown(BPoint point);
49	virtual void				MouseUp(BPoint point);
50	virtual void				MouseMoved(BPoint point, uint32 transit,
51									const BMessage* dragMessage);
52
53	virtual void				Draw(BRect updateRect);
54	virtual void				MessageReceived(BMessage* message);
55	virtual void				WindowActivated(bool active);
56
57private:
58			enum key_kind {
59				kNormalKey,
60				kSpecialKey,
61				kSymbolKey,
62				kIndicator
63			};
64
65			void				_LayoutKeyboard();
66			void				_DrawKeyButton(BView* view, BRect& rect,
67									BRect updateRect, rgb_color base,
68									rgb_color background, bool pressed);
69			void				_DrawKey(BView* view, BRect updateRect,
70									const Key* key, BRect frame, bool pressed);
71			void				_DrawIndicator(BView* view, BRect updateRect,
72									const Indicator* indicator, BRect rect,
73									bool lit);
74			const char*			_SpecialKeyLabel(const key_map& map,
75									uint32 code, bool abbreviated = false);
76			const char*			_SpecialMappedKeySymbol(const char* bytes,
77									size_t numBytes);
78			const char*			_SpecialMappedKeyLabel(const char* bytes,
79									size_t numBytes, bool abbreviated = false);
80			bool				_FunctionKeyLabel(uint32 code, char* text,
81									size_t textSize);
82			void				_GetAbbreviatedKeyLabelIfNeeded(BView* view,
83									BRect rect, const Key* key, char* text,
84									size_t textSize);
85			void				_GetKeyLabel(const Key* key, char* text,
86									size_t textSize, key_kind& keyKind);
87			bool				_IsKeyPressed(uint32 code);
88			bool				_KeyState(uint32 code) const;
89			void				_SetKeyState(uint32 code, bool pressed);
90			Key*				_KeyForCode(uint32 code);
91			void				_InvalidateKey(uint32 code);
92			void				_InvalidateKey(const Key* key);
93			bool				_HandleDeadKey(uint32 key, int32 modifiers);
94			void				_KeyChanged(const BMessage* message);
95			Key*				_KeyAt(BPoint point);
96			BRect				_FrameFor(BRect keyFrame);
97			BRect				_FrameFor(const Key* key);
98			void				_SetFontSize(BView* view, key_kind keyKind);
99			void				_EvaluateDropTarget(BPoint point);
100			void				_SendKeyDown(const Key* key);
101
102			BMenuItem*			_CreateSwapModifiersMenuItem(uint32 modifier,
103									uint32 displayModifier, uint32 oldCode,
104									uint32 newCode);
105			const char*			_NameForModifier(uint32 modifier, bool pretty);
106
107			KeyboardLayout*		fLayout;
108			Keymap*				fKeymap;
109			BMessenger			fTarget;
110			bool				fEditable;
111
112			uint8				fKeyState[16];
113			int32				fModifiers;
114			int32				fDeadKey;
115			int32				fButtons;
116
117			BPoint				fClickPoint;
118			Key*				fDragKey;
119			int32				fDragModifiers;
120			Key*				fDropTarget;
121			BPoint				fDropPoint;
122
123			BSize				fOldSize;
124			BFont				fBaseFont;
125			BFont				fSpecialFont;
126			float				fBaseFontHeight;
127			float				fBaseFontSize;
128			BPoint				fOffset;
129			float				fFactor;
130			float				fGap;
131
132			BInputServerDevice*	fDevice;
133};
134
135
136#endif	// KEYBOARD_LAYOUT_VIEW_H
137