xref: /aoo41x/main/vcl/source/window/keycod.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_vcl.hxx"
30 
31 #include <salinst.hxx>
32 #include <salframe.hxx>
33 #include <svdata.hxx>
34 
35 #include <vcl/window.hxx>
36 #include <vcl/keycod.hxx>
37 
38 #include <tools/rc.h>
39 
40 
41 // =======================================================================
42 static sal_uInt16 aImplKeyFuncTab[(KEYFUNC_FRONT+1)*4] =
43 {
44 	0, 0, 0, 0,                                                    // KEYFUNC_DONTKNOW
45 	KEY_N | KEY_MOD1, 0, 0, 0,                                     // KEYFUNC_NEW
46 	KEY_O | KEY_MOD1, KEY_OPEN, 0, 0,                              // KEYFUNC_OPEN
47 	KEY_S | KEY_MOD1, 0, 0, 0,                                     // KEYFUNC_SAVE
48 	0, 0, 0, 0,                                                    // KEYFUNC_SAVEAS
49 	KEY_P | KEY_MOD1, 0, 0, 0,                                     // KEYFUNC_PRINT
50 	KEY_W | KEY_MOD1, KEY_F4 | KEY_MOD1, 0, 0,                     // KEYFUNC_CLOSE
51 	KEY_Q | KEY_MOD1, KEY_F4 | KEY_MOD2, 0, 0,                     // KEYFUNC_QUIT
52 	KEY_X | KEY_MOD1, KEY_DELETE | KEY_SHIFT, KEY_CUT, 0,          // KEYFUNC_CUT
53 	KEY_C | KEY_MOD1, KEY_INSERT | KEY_MOD1, KEY_COPY, 0,          // KEYFUNC_COPY
54 	KEY_V | KEY_MOD1, KEY_INSERT | KEY_SHIFT, KEY_PASTE, 0,        // KEYFUNC_PASTE
55 	KEY_Z | KEY_MOD1, KEY_BACKSPACE | KEY_MOD2, KEY_UNDO, 0,       // KEYFUNC_UNDO
56 	0, 0, 0, 0,                                                    // KEYFUNC_REDO
57 	KEY_DELETE, 0, 0, 0,                                           // KEYFUNC_DELETE
58 	KEY_REPEAT, 0, 0, 0,                                           // KEYFUNC_REPEAT
59 	KEY_F | KEY_MOD1, KEY_FIND, 0, 0,                              // KEYFUNC_FIND
60 	KEY_F | KEY_SHIFT | KEY_MOD1, KEY_SHIFT | KEY_FIND, 0, 0,      // KEYFUNC_FINDBACKWARD
61 	KEY_RETURN | KEY_MOD2, 0, 0, 0,                                // KEYFUNC_PROPERTIES
62 	0, 0, 0, 0                                                     // KEYFUNC_FRONT
63 };
64 
65 // -----------------------------------------------------------------------
66 
67 void ImplGetKeyCode( KeyFuncType eFunc, sal_uInt16& rCode1, sal_uInt16& rCode2, sal_uInt16& rCode3, sal_uInt16& rCode4 )
68 {
69 	sal_uInt16 nIndex = (sal_uInt16)eFunc;
70 	nIndex *= 4;
71 	rCode1 = aImplKeyFuncTab[nIndex];
72 	rCode2 = aImplKeyFuncTab[nIndex+1];
73 	rCode3 = aImplKeyFuncTab[nIndex+2];
74         rCode4 = aImplKeyFuncTab[nIndex+3];
75 }
76 
77 // =======================================================================
78 
79 KeyCode::KeyCode( KeyFuncType eFunction )
80 {
81 	sal_uInt16 nDummy;
82 	ImplGetKeyCode( eFunction, nCode, nDummy, nDummy, nDummy );
83 	eFunc = eFunction;
84 }
85 
86 // -----------------------------------------------------------------------
87 
88 KeyCode::KeyCode( const ResId& rResId )
89 {
90 	rResId.SetRT( RSC_KEYCODE );
91 
92 	ResMgr* pResMgr = rResId.GetResMgr();
93 	if ( pResMgr && pResMgr->GetResource( rResId ) )
94 	{
95 		pResMgr->Increment( sizeof( RSHEADER_TYPE ) );
96 
97 		sal_uLong nKeyCode 	= pResMgr->ReadLong();
98 		sal_uLong nModifier	= pResMgr->ReadLong();
99 		sal_uLong nKeyFunc 	= pResMgr->ReadLong();
100 
101 		eFunc = (KeyFuncType)nKeyFunc;
102 		if ( eFunc != KEYFUNC_DONTKNOW )
103 		{
104 			sal_uInt16 nDummy;
105 			ImplGetKeyCode( eFunc, nCode, nDummy, nDummy, nDummy );
106 		}
107 		else
108 			nCode = sal::static_int_cast<sal_uInt16>(nKeyCode | nModifier);
109 	}
110 }
111 
112 // -----------------------------------------------------------------------
113 
114 XubString KeyCode::GetName( Window* pWindow ) const
115 {
116 	if ( !pWindow )
117 		pWindow = ImplGetDefaultWindow();
118 	return pWindow ? pWindow->ImplGetFrame()->GetKeyName( GetFullCode() ) : XubString();
119 }
120 
121 // -----------------------------------------------------------------------
122 
123 XubString KeyCode::GetSymbolName( const XubString& rFontName, Window* pWindow ) const
124 {
125 	if ( !pWindow )
126 		pWindow = ImplGetDefaultWindow();
127 	return pWindow ? pWindow->ImplGetFrame()->GetSymbolKeyName( rFontName, GetFullCode() ) : XubString();
128 }
129 
130 // -----------------------------------------------------------------------
131 
132 KeyFuncType KeyCode::GetFunction() const
133 {
134 	if ( eFunc != KEYFUNC_DONTKNOW )
135 		return eFunc;
136 
137 	sal_uInt16 nCompCode = GetModifier() | GetCode();
138 	if ( nCompCode )
139 	{
140 		for ( sal_uInt16 i = (sal_uInt16)KEYFUNC_NEW; i < (sal_uInt16)KEYFUNC_FRONT; i++ )
141 		{
142 			sal_uInt16 nKeyCode1;
143 			sal_uInt16 nKeyCode2;
144 			sal_uInt16 nKeyCode3;
145                         sal_uInt16 nKeyCode4;
146 			ImplGetKeyCode( (KeyFuncType)i, nKeyCode1, nKeyCode2, nKeyCode3, nKeyCode4 );
147 			if ( (nCompCode == nKeyCode1) || (nCompCode == nKeyCode2) || (nCompCode == nKeyCode3) || (nCompCode == nKeyCode4) )
148 				return (KeyFuncType)i;
149 		}
150 	}
151 
152 	return KEYFUNC_DONTKNOW;
153 }
154