]>
Commit | Line | Data |
---|---|---|
1e6feb95 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/univ/control.cpp | |
3 | // Purpose: universal wxControl: adds handling of mnemonics | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 14.08.00 | |
7 | // RCS-ID: $Id$ | |
442b35b5 | 8 | // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com) |
65571936 | 9 | // Licence: wxWindows licence |
1e6feb95 VZ |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
1e6feb95 VZ |
16 | // ---------------------------------------------------------------------------- |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #if wxUSE_CONTROLS | |
27 | ||
93fbbe07 WS |
28 | #include "wx/control.h" |
29 | ||
1e6feb95 VZ |
30 | #ifndef WX_PRECOMP |
31 | #include "wx/app.h" | |
1e6feb95 VZ |
32 | #include "wx/dcclient.h" |
33 | #endif | |
34 | ||
35 | #include "wx/univ/renderer.h" | |
36 | #include "wx/univ/inphand.h" | |
37 | #include "wx/univ/theme.h" | |
38 | ||
39 | // ============================================================================ | |
40 | // implementation | |
41 | // ============================================================================ | |
42 | ||
43 | IMPLEMENT_DYNAMIC_CLASS(wxControl, wxWindow) | |
44 | ||
45 | BEGIN_EVENT_TABLE(wxControl, wxControlBase) | |
23645bfa | 46 | WX_EVENT_TABLE_INPUT_CONSUMER(wxControl) |
1e6feb95 VZ |
47 | END_EVENT_TABLE() |
48 | ||
23645bfa VS |
49 | WX_FORWARD_TO_INPUT_CONSUMER(wxControl) |
50 | ||
1e6feb95 VZ |
51 | // ---------------------------------------------------------------------------- |
52 | // creation | |
53 | // ---------------------------------------------------------------------------- | |
54 | ||
55 | void wxControl::Init() | |
56 | { | |
57 | m_indexAccel = -1; | |
1e6feb95 VZ |
58 | } |
59 | ||
60 | bool wxControl::Create(wxWindow *parent, | |
61 | wxWindowID id, | |
62 | const wxPoint& pos, | |
63 | const wxSize& size, | |
64 | long style, | |
65 | const wxValidator& validator, | |
66 | const wxString& name) | |
67 | { | |
e441e1f4 | 68 | if ( !wxControlBase::Create(parent, id, pos, size, style, validator, name) ) |
2387541f VZ |
69 | { |
70 | // underlying window creation failed? | |
a290fa5a | 71 | return false; |
2387541f | 72 | } |
1e6feb95 | 73 | |
a290fa5a | 74 | return true; |
1e6feb95 VZ |
75 | } |
76 | ||
77 | // ---------------------------------------------------------------------------- | |
78 | // mnemonics handling | |
79 | // ---------------------------------------------------------------------------- | |
80 | ||
81 | /* static */ | |
82 | int wxControl::FindAccelIndex(const wxString& label, wxString *labelOnly) | |
83 | { | |
84 | // the character following MNEMONIC_PREFIX is the accelerator for this | |
85 | // control unless it is MNEMONIC_PREFIX too - this allows to insert | |
86 | // literal MNEMONIC_PREFIX chars into the label | |
87 | static const wxChar MNEMONIC_PREFIX = _T('&'); | |
88 | ||
89 | if ( labelOnly ) | |
90 | { | |
91 | labelOnly->Empty(); | |
92 | labelOnly->Alloc(label.length()); | |
93 | } | |
94 | ||
95 | int indexAccel = -1; | |
96 | for ( const wxChar *pc = label; *pc != wxT('\0'); pc++ ) | |
97 | { | |
98 | if ( *pc == MNEMONIC_PREFIX ) | |
99 | { | |
100 | pc++; // skip it | |
101 | if ( *pc != MNEMONIC_PREFIX ) | |
102 | { | |
103 | if ( indexAccel == -1 ) | |
104 | { | |
105 | // remember it (-1 is for MNEMONIC_PREFIX itself | |
106 | indexAccel = pc - label.c_str() - 1; | |
107 | } | |
108 | else | |
109 | { | |
110 | wxFAIL_MSG(_T("duplicate accel char in control label")); | |
111 | } | |
112 | } | |
113 | } | |
114 | ||
115 | if ( labelOnly ) | |
116 | { | |
117 | *labelOnly += *pc; | |
118 | } | |
119 | } | |
120 | ||
121 | return indexAccel; | |
122 | } | |
123 | ||
124 | void wxControl::SetLabel(const wxString& label) | |
39bc0347 VZ |
125 | { |
126 | // save original label | |
127 | wxControlBase::SetLabel(label); | |
128 | ||
129 | UnivDoSetLabel(label); | |
130 | } | |
131 | ||
132 | void wxControl::UnivDoSetLabel(const wxString& label) | |
1e6feb95 VZ |
133 | { |
134 | wxString labelOld = m_label; | |
135 | m_indexAccel = FindAccelIndex(label, &m_label); | |
136 | ||
137 | if ( m_label != labelOld ) | |
138 | { | |
139 | Refresh(); | |
140 | } | |
141 | } | |
142 | ||
1e6feb95 | 143 | #endif // wxUSE_CONTROLS |