xref: /haiku/src/preferences/keymap/KeyboardLayoutView.h (revision 8c78892580f132d10e624aef96f835df8d94bf19)
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 
20 class BMenuItem;
21 class Keymap;
22 
23 
24 class KeyboardLayoutView : public BView {
25 public:
26 								KeyboardLayoutView(const char* name);
27 								~KeyboardLayoutView();
28 
29 			void				SetKeyboardLayout(KeyboardLayout* layout);
30 			void				SetKeymap(Keymap* keymap);
31 			void				SetTarget(BMessenger target);
32 
33 			KeyboardLayout*		GetKeyboardLayout() { return fLayout; }
34 
35 			void				SetBaseFont(const BFont& font);
36 
37 			void				SetEditable(bool editable);
38 
39 protected:
40 	virtual void				AttachedToWindow();
41 	virtual void				FrameResized(float width, float height);
42 	virtual BSize				MinSize();
43 
44 	virtual void				KeyDown(const char* bytes, int32 numBytes);
45 	virtual void				KeyUp(const char* bytes, int32 numBytes);
46 	virtual void				MouseDown(BPoint point);
47 	virtual void				MouseUp(BPoint point);
48 	virtual void				MouseMoved(BPoint point, uint32 transit,
49 									const BMessage* dragMessage);
50 
51 	virtual void				Draw(BRect updateRect);
52 	virtual void				MessageReceived(BMessage* message);
53 	virtual void				WindowActivated(bool active);
54 
55 private:
56 			enum key_kind {
57 				kNormalKey,
58 				kSpecialKey,
59 				kSymbolKey,
60 				kIndicator
61 			};
62 
63 			void				_LayoutKeyboard();
64 			void				_DrawKeyButton(BView* view, BRect& rect,
65 									BRect updateRect, rgb_color base,
66 									rgb_color background, bool pressed);
67 			void				_DrawKey(BView* view, BRect updateRect,
68 									const Key* key, BRect frame, bool pressed);
69 			void				_DrawIndicator(BView* view, BRect updateRect,
70 									const Indicator* indicator, BRect rect,
71 									bool lit);
72 			const char*			_SpecialKeyLabel(const key_map& map,
73 									uint32 code, bool abbreviated = false);
74 			const char*			_SpecialMappedKeySymbol(const char* bytes,
75 									size_t numBytes);
76 			const char*			_SpecialMappedKeyLabel(const char* bytes,
77 									size_t numBytes, bool abbreviated = false);
78 			bool				_FunctionKeyLabel(uint32 code, char* text,
79 									size_t textSize);
80 			void				_GetAbbreviatedKeyLabelIfNeeded(BView* view,
81 									BRect rect, const Key* key, char* text,
82 									size_t textSize);
83 			void				_GetKeyLabel(const Key* key, char* text,
84 									size_t textSize, key_kind& keyKind);
85 			bool				_IsKeyPressed(uint32 code);
86 			bool				_KeyState(uint32 code) const;
87 			void				_SetKeyState(uint32 code, bool pressed);
88 			Key*				_KeyForCode(uint32 code);
89 			void				_InvalidateKey(uint32 code);
90 			void				_InvalidateKey(const Key* key);
91 			bool				_HandleDeadKey(uint32 key, int32 modifiers);
92 			void				_KeyChanged(const BMessage* message);
93 			Key*				_KeyAt(BPoint point);
94 			BRect				_FrameFor(BRect keyFrame);
95 			BRect				_FrameFor(const Key* key);
96 			void				_SetFontSize(BView* view, key_kind keyKind);
97 			void				_EvaluateDropTarget(BPoint point);
98 			void				_SendFakeKeyDown(const Key* key);
99 
100 			BMenuItem*			_CreateSwapModifiersMenuItem(uint32 modifier,
101 									uint32 displayModifier, uint32 oldCode,
102 									uint32 newCode);
103 			const char*			_NameForModifier(uint32 modifier, bool pretty);
104 
105 			KeyboardLayout*		fLayout;
106 			Keymap*				fKeymap;
107 			BMessenger			fTarget;
108 			bool				fEditable;
109 
110 			uint8				fKeyState[16];
111 			int32				fModifiers;
112 			int32				fDeadKey;
113 			int32				fButtons;
114 
115 			BPoint				fClickPoint;
116 			Key*				fDragKey;
117 			int32				fDragModifiers;
118 			Key*				fDropTarget;
119 			BPoint				fDropPoint;
120 
121 			BSize				fOldSize;
122 			BFont				fBaseFont;
123 			BFont				fSpecialFont;
124 			float				fBaseFontHeight;
125 			float				fBaseFontSize;
126 			BPoint				fOffset;
127 			float				fFactor;
128 			float				fGap;
129 };
130 
131 
132 #endif	// KEYBOARD_LAYOUT_VIEW_H
133