~ubuntu-branches/ubuntu/quantal/mysql-workbench/quantal

« back to all changes in this revision

Viewing changes to library/forms/winforms/src/wf_textbox.cpp

  • Committer: Package Import Robot
  • Author(s): Dmitry Smirnov
  • Date: 2012-03-01 21:57:30 UTC
  • Revision ID: package-import@ubuntu.com-20120301215730-o7y8av8y38n162ro
Tags: upstream-5.2.38+dfsg
ImportĀ upstreamĀ versionĀ 5.2.38+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License as
 
6
 * published by the Free Software Foundation; version 2 of the
 
7
 * License.
 
8
 * 
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
12
 * GNU General Public License for more details.
 
13
 * 
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
17
 * 02110-1301  USA
 
18
 */
 
19
 
 
20
/**
 
21
 * Implementation of the textbox (multi line edit) wrapper for the mforms library.
 
22
 */
 
23
 
 
24
#include "stdafx.h"
 
25
#include "wf_textbox.h"
 
26
 
 
27
using namespace MySQL::Forms;
 
28
 
 
29
using namespace System::Windows::Forms;
 
30
using namespace System::Text;
 
31
 
 
32
DEFAULT_LOG_DOMAIN(DOMAIN_MFORMS_WRAPPER)
 
33
 
 
34
//----------------- TextBoxEx ----------------------------------------------------------------------
 
35
 
 
36
bool TextBoxEx::ProcessCmdKey(Message% msg, Keys keyData)
 
37
{
 
38
  // In order to be able to determine certain special keys we have to hook into the chain before any
 
39
  // other key handling is performed.
 
40
  if (msg.Msg == WM_KEYDOWN)
 
41
  {
 
42
    switch (msg.WParam.ToInt32())
 
43
    {
 
44
    case Keys::Return:
 
45
      {
 
46
        bool result;
 
47
        mforms::TextBox* backend = ObjectImpl::get_backend_control<mforms::TextBox>(this);
 
48
        if (((msg.LParam.ToInt32() >> 16) & KF_EXTENDED) == KF_EXTENDED)
 
49
          result = backend->key_event(mforms::KeyReturn, getModifiers(keyData), "");
 
50
        else
 
51
          result = backend->key_event(mforms::KeyEnter, getModifiers(keyData), "");
 
52
 
 
53
        if (result)
 
54
          return TextBox::ProcessCmdKey(msg, keyData);
 
55
        else
 
56
          return false;
 
57
 
 
58
        break;
 
59
      }
 
60
 
 
61
    default:
 
62
      return TextBox::ProcessCmdKey(msg, keyData);
 
63
    }
 
64
  }
 
65
  else
 
66
    return TextBox::ProcessCmdKey(msg, keyData);
 
67
}
 
68
 
 
69
//--------------------------------------------------------------------------------------------------
 
70
 
 
71
mforms::ModifierKey TextBoxEx::getModifiers(Keys keyData)
 
72
{
 
73
  mforms::ModifierKey modifiers = mforms::ModifierNoModifier;
 
74
  if ((keyData & Keys::Control) == Keys::Control)
 
75
    modifiers = modifiers | mforms::ModifierControl;
 
76
  if ((keyData & Keys::Alt) == Keys::Alt)
 
77
    modifiers = modifiers | mforms::ModifierAlt;
 
78
  if ((keyData & Keys::Shift) == Keys::Shift)
 
79
    modifiers = modifiers | mforms::ModifierShift;
 
80
  if ((keyData & Keys::LWin) == Keys::LWin)
 
81
    modifiers = modifiers | mforms::ModifierCommand;
 
82
 
 
83
  return modifiers;
 
84
}
 
85
 
 
86
//----------------- TextBoxImpl --------------------------------------------------------------------
 
87
 
 
88
bool TextBoxImpl::create(::mforms::TextBox *self, ::mforms::ScrollBars scroll_bars)
 
89
{
 
90
  TextBoxImpl ^text= gcnew TextBoxImpl(self);
 
91
 
 
92
  if (text != nullptr)
 
93
  {
 
94
    TextBox ^textbox= ViewImpl::create<TextBoxEx>(self, text);
 
95
    textbox->ForeColor = Color::Black;
 
96
    textbox->Multiline= true;
 
97
    textbox->AcceptsReturn= true;
 
98
    ScrollBars native_scrollbars = ScrollBars::None;
 
99
    if ((scroll_bars & mforms::HorizontalScrollBar) != 0)
 
100
    {
 
101
      if ((scroll_bars & mforms::VerticalScrollBar) != 0)
 
102
        native_scrollbars = ScrollBars::Both;
 
103
      else
 
104
        native_scrollbars = ScrollBars::Horizontal;
 
105
    }
 
106
    else
 
107
      if ((scroll_bars & mforms::VerticalScrollBar) != 0)
 
108
        native_scrollbars = ScrollBars::Vertical;
 
109
    textbox->ScrollBars= native_scrollbars;
 
110
    textbox->TextChanged += gcnew EventHandler(text, &TextBoxImpl::OnChange);
 
111
    textbox->KeyDown += gcnew KeyEventHandler(text, &TextBoxImpl::OnKeyDown);
 
112
    textbox->KeyPress += gcnew KeyPressEventHandler(text, &TextBoxImpl::OnKeyPress);
 
113
    textbox->Size= Size(100, textbox->PreferredSize.Height); // DefaultSize is not accessible here. 
 
114
    return true;
 
115
  }
 
116
  return false;
 
117
}
 
118
 
 
119
//--------------------------------------------------------------------------------------------------
 
120
 
 
121
void TextBoxImpl::OnChange(Object^ sender, EventArgs^ args)
 
122
{
 
123
  TextBox^ textbox= (TextBox^) sender;
 
124
 
 
125
  if (textbox->Tag != nullptr)
 
126
  {
 
127
    ::mforms::TextBox* box= ViewImpl::get_backend_control<::mforms::TextBox>(textbox);
 
128
    if (box != NULL)
 
129
      box->callback();
 
130
  }
 
131
}
 
132
 
 
133
//--------------------------------------------------------------------------------------------------
 
134
 
 
135
void TextBoxImpl::OnKeyDown(Object^ sender, KeyEventArgs^ args)
 
136
{
 
137
  // Don't call the back end for the return key. We have already done that.
 
138
  if (args->KeyCode != Keys::Return)
 
139
  {
 
140
    TextBoxEx^ textbox= (TextBoxEx^) sender;
 
141
    if (textbox->Tag != nullptr)
 
142
    {
 
143
      ::mforms::TextBox* box= ViewImpl::get_backend_control<::mforms::TextBox>(textbox);
 
144
 
 
145
      modifiers = textbox->getModifiers(args->KeyData);
 
146
      mforms::KeyCode code = mforms::KeyNone;
 
147
      switch (args->KeyCode & Keys::KeyCode)
 
148
      {
 
149
      case Keys::Home:
 
150
        code = mforms::KeyHome;
 
151
        break;
 
152
 
 
153
      case Keys::End:
 
154
        code = mforms::KeyEnd;
 
155
        break;
 
156
 
 
157
      case Keys::Prior:
 
158
        code = mforms::KeyPrevious;
 
159
        break;
 
160
 
 
161
      case Keys::Next:
 
162
        code = mforms::KeyNext;
 
163
        break;
 
164
 
 
165
      case Keys::ShiftKey:
 
166
      case Keys::ControlKey:
 
167
      case Keys::Menu: // Alt key
 
168
      case Keys::LWin: // Command on Mac.
 
169
        code = mforms::KeyModifierOnly;
 
170
        break;
 
171
      }
 
172
 
 
173
      if (code != mforms::KeyNone)
 
174
        if (!box->key_event(code, modifiers, ""))
 
175
          args->Handled = true; // If the backend consumed the key (by returning false) then we stop
 
176
                                // further processing of this event.
 
177
    }
 
178
  }
 
179
}
 
180
 
 
181
//--------------------------------------------------------------------------------------------------
 
182
 
 
183
void TextBoxImpl::OnKeyPress(Object^ sender, KeyPressEventArgs^ args)
 
184
{
 
185
  // Don't call the back end for the return key. We have already done that.
 
186
  if (args->KeyChar != '\r')
 
187
  {
 
188
    TextBoxEx^ textbox= (TextBoxEx^) sender;
 
189
    if (textbox->Tag != nullptr)
 
190
    {
 
191
      ::mforms::TextBox* box= ViewImpl::get_backend_control<::mforms::TextBox>(textbox);
 
192
      String^ string = gcnew String(args->KeyChar, 1);
 
193
      if (!box->key_event(mforms::KeyChar, modifiers, NativeToCppString(string)))
 
194
        args->Handled = true;
 
195
    }
 
196
  }
 
197
}
 
198
 
 
199
//--------------------------------------------------------------------------------------------------
 
200
 
 
201
void TextBoxImpl::set_bordered(::mforms::TextBox *self, bool bordered)
 
202
{
 
203
  TextBoxImpl^ textbox= (TextBoxImpl^)ObjectImpl::FromUnmanaged(self);
 
204
 
 
205
  if (textbox != nullptr)
 
206
    textbox->get_control<TextBox>()->BorderStyle= bordered ? BorderStyle::FixedSingle : BorderStyle::None;
 
207
}
 
208
 
 
209
//--------------------------------------------------------------------------------------------------
 
210
 
 
211
void TextBoxImpl::set_text(::mforms::TextBox *self, const std::string &text)
 
212
{
 
213
  TextBoxImpl^ textbox= (TextBoxImpl^)ObjectImpl::FromUnmanaged(self);
 
214
 
 
215
  if (textbox != nullptr)
 
216
  {
 
217
    // Convert LF only line breaks into Windows line breaks.
 
218
    StringBuilder^ builder= gcnew StringBuilder(CppStringToNative(text));
 
219
    builder->Replace("\n", Environment::NewLine);
 
220
 
 
221
    textbox->get_control<Control>()->Text= builder->ToString();
 
222
  }
 
223
}
 
224
 
 
225
//--------------------------------------------------------------------------------------------------
 
226
 
 
227
void TextBoxImpl::append_text(::mforms::TextBox *self, const std::string &text, bool scroll_to_end)
 
228
{
 
229
  TextBoxImpl^ textbox= (TextBoxImpl^)ObjectImpl::FromUnmanaged(self);
 
230
 
 
231
  if (textbox != nullptr)
 
232
  {
 
233
    // Convert LF only line breaks into Windows line breaks.
 
234
    StringBuilder^ builder= gcnew StringBuilder(CppStringToNative(text));
 
235
    builder->Replace("\n", Environment::NewLine);
 
236
 
 
237
    TextBox^ native_box= textbox->get_control<TextBox>();
 
238
    native_box->AppendText(builder->ToString());
 
239
    if (scroll_to_end && native_box->Text->Length > 0)
 
240
    {
 
241
      native_box->Select(native_box->Text->Length - 1, 0);
 
242
      native_box->ScrollToCaret();
 
243
    }
 
244
  }
 
245
}
 
246
 
 
247
//--------------------------------------------------------------------------------------------------
 
248
 
 
249
std::string TextBoxImpl::get_text(::mforms::TextBox *self)
 
250
{
 
251
  TextBoxImpl^ textbox= (TextBoxImpl^)ObjectImpl::FromUnmanaged(self);
 
252
 
 
253
  if (textbox != nullptr)
 
254
  {
 
255
    // Convert Windows line breaks to LF.
 
256
    StringBuilder^ builder= gcnew StringBuilder(textbox->get_control<Control>()->Text);
 
257
    builder->Replace(Environment::NewLine, "\n");
 
258
    return NativeToCppString(builder->ToString());
 
259
  }
 
260
  return "";
 
261
}
 
262
 
 
263
//--------------------------------------------------------------------------------------------------
 
264
 
 
265
void TextBoxImpl::set_read_only(::mforms::TextBox *self, bool flag)
 
266
{
 
267
   TextBoxImpl^ textbox= (TextBoxImpl^)ObjectImpl::FromUnmanaged(self);
 
268
 
 
269
  if (textbox != nullptr)
 
270
    textbox->get_control<TextBox>()->ReadOnly= flag;
 
271
}
 
272
 
 
273
//--------------------------------------------------------------------------------------------------
 
274
 
 
275
void TextBoxImpl::set_padding(::mforms::TextBox *self, int pad)
 
276
{
 
277
  TextBoxImpl^ textbox= (TextBoxImpl^)ObjectImpl::FromUnmanaged(self);
 
278
 
 
279
  if (textbox != nullptr)
 
280
    textbox->get_control<TextBox>()->Padding = Padding(pad); // Doesn't have any effect.
 
281
}
 
282
 
 
283
//--------------------------------------------------------------------------------------------------
 
284
 
 
285
void TextBoxImpl::clear(::mforms::TextBox *self)
 
286
{
 
287
  TextBoxImpl^ textbox= (TextBoxImpl^)ObjectImpl::FromUnmanaged(self);
 
288
 
 
289
  if (textbox != nullptr)
 
290
    textbox->get_control<TextBox>()->Clear();
 
291
}
 
292
 
 
293
//--------------------------------------------------------------------------------------------------
 
294
 
 
295
void TextBoxImpl::set_monospaced(::mforms::TextBox *self, bool flag)
 
296
{
 
297
  TextBoxImpl^ textbox= (TextBoxImpl^)ObjectImpl::FromUnmanaged(self);
 
298
 
 
299
  if (textbox != nullptr)
 
300
  {
 
301
    if (flag)
 
302
      try
 
303
      {
 
304
        textbox->get_control<TextBox>()->Font = gcnew System::Drawing::Font("Bitstream Vera Sans Mono", textbox->get_control<TextBox>()->Font->Size - 2);
 
305
      }
 
306
      catch (System::ArgumentException^ e)
 
307
      {
 
308
        // Argument exception pops up when the system cannot find the Regular font style (corrupt font).
 
309
        log_error("TextBoxImpl::set_monospaced failed. %s\n", e->Message);
 
310
      }
 
311
    else
 
312
      textbox->get_control<TextBox>()->ResetFont();
 
313
  }
 
314
}
 
315
 
 
316
//--------------------------------------------------------------------------------------------------
 
317
 
 
318
void TextBoxImpl::get_selected_range(::mforms::TextBox *self, int &start, int &end)
 
319
{
 
320
  TextBoxImpl^ textbox= (TextBoxImpl^)ObjectImpl::FromUnmanaged(self);
 
321
 
 
322
  if (textbox != nullptr)
 
323
  {
 
324
    start = textbox->get_control<TextBox>()->SelectionStart;
 
325
    end = start + textbox->get_control<TextBox>()->SelectionLength;
 
326
  }
 
327
}
 
328
 
 
329
//--------------------------------------------------------------------------------------------------
 
330
 
 
331
TextBoxImpl::TextBoxImpl(::mforms::TextBox *text)
 
332
  : ViewImpl(text)
 
333
{
 
334
}
 
335
 
 
336
//--------------------------------------------------------------------------------------------------