xref: /haiku/src/preferences/keymap/KeyboardLayoutView.h (revision 18027fff34af4a666c1e62254b462cbaeae1859e)
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				_InitOffscreen();
64 			void				_LayoutKeyboard();
65 			void				_DrawKeyButton(BView* view, BRect& rect,
66 									BRect updateRect, rgb_color base,
67 									rgb_color background, bool pressed);
68 			void				_DrawKey(BView* view, BRect updateRect,
69 									const Key* key, BRect frame, bool pressed);
70 			void				_DrawIndicator(BView* view, BRect updateRect,
71 									const Indicator* indicator, BRect rect,
72 									bool lit);
73 			const char*			_SpecialKeyLabel(const key_map& map,
74 									uint32 code, bool abbreviated = false);
75 			const char*			_SpecialMappedKeySymbol(const char* bytes,
76 									size_t numBytes);
77 			const char*			_SpecialMappedKeyLabel(const char* bytes,
78 									size_t numBytes, bool abbreviated = false);
79 			bool				_FunctionKeyLabel(uint32 code, char* text,
80 									size_t textSize);
81 			void				_GetAbbreviatedKeyLabelIfNeeded(BView* view,
82 									BRect rect, const Key* key, char* text,
83 									size_t textSize);
84 			void				_GetKeyLabel(const Key* key, char* text,
85 									size_t textSize, key_kind& keyKind);
86 			bool				_IsKeyPressed(uint32 code);
87 			bool				_KeyState(uint32 code) const;
88 			void				_SetKeyState(uint32 code, bool pressed);
89 			Key*				_KeyForCode(uint32 code);
90 			void				_InvalidateKey(uint32 code);
91 			void				_InvalidateKey(const Key* key);
92 			bool				_HandleDeadKey(uint32 key, int32 modifiers);
93 			void				_KeyChanged(const BMessage* message);
94 			Key*				_KeyAt(BPoint point);
95 			BRect				_FrameFor(BRect keyFrame);
96 			BRect				_FrameFor(const Key* key);
97 			void				_SetFontSize(BView* view, key_kind keyKind);
98 			void				_EvaluateDropTarget(BPoint point);
99 			void				_SendFakeKeyDown(const Key* key);
100 
101 			BMenuItem*			_CreateSwapModifiersMenuItem(uint32 modifier,
102 									uint32 displayModifier, uint32 oldCode,
103 									uint32 newCode);
104 			const char*			_NameForModifier(uint32 modifier, bool pretty);
105 
106 			BBitmap*			fOffscreenBitmap;
107 			BView*				fOffscreenView;
108 
109 			KeyboardLayout*		fLayout;
110 			Keymap*				fKeymap;
111 			BMessenger			fTarget;
112 			bool				fEditable;
113 
114 			uint8				fKeyState[16];
115 			int32				fModifiers;
116 			int32				fDeadKey;
117 			int32				fButtons;
118 
119 			BPoint				fClickPoint;
120 			Key*				fDragKey;
121 			int32				fDragModifiers;
122 			Key*				fDropTarget;
123 			BPoint				fDropPoint;
124 
125 			BSize				fOldSize;
126 			BFont				fBaseFont;
127 			BFont				fSpecialFont;
128 			float				fBaseFontHeight;
129 			float				fBaseFontSize;
130 			BPoint				fOffset;
131 			float				fFactor;
132 			float				fGap;
133 };
134 
135 
136 #endif	// KEYBOARD_LAYOUT_VIEW_H
137