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

« back to all changes in this revision

Viewing changes to library/forms/winforms/src/wf_form.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, 2011, 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
 * Contains the implementation of the wrapper for a .NET form used by the mforms backend.
 
22
 */
 
23
 
 
24
#include "stdafx.h"
 
25
 
 
26
#include "wf_form.h"
 
27
#include "wf_utilities.h"
 
28
 
 
29
using namespace System;
 
30
using namespace System::IO;
 
31
using namespace Drawing;
 
32
using namespace Windows::Forms;
 
33
 
 
34
using namespace MySQL;
 
35
using namespace MySQL::Forms;
 
36
 
 
37
//--------------------------------------------------------------------------------------------------
 
38
 
 
39
bool FormFillLayout::Layout(Object^ container, LayoutEventArgs^ arguments)
 
40
{
 
41
  FillForm^ form = (FillForm^) container;
 
42
 
 
43
  ViewImpl::adjust_auto_resize_from_docking(form);
 
44
  System::Drawing::Size newSize = form->ComputeLayout(form->Size, true);
 
45
 
 
46
  if (newSize.Width < form->MinimumSize.Width)
 
47
    newSize.Width= form->MinimumSize.Width;
 
48
  if (newSize.Height < form->MinimumSize.Height)
 
49
    newSize.Height= form->MinimumSize.Height;
 
50
 
 
51
  // Finally adjust the container.
 
52
  bool parentLayoutNeeded= !form->Size.Equals(newSize);
 
53
  if (parentLayoutNeeded)
 
54
    ViewImpl::resize_with_docking(form, newSize);
 
55
 
 
56
  return parentLayoutNeeded;
 
57
}
 
58
 
 
59
//-------------------------------------------------------------------------------------------------
 
60
 
 
61
System::Drawing::Size FormFillLayout::GetPreferredSize(Control^ container, System::Drawing::Size proposedSize)
 
62
{
 
63
  FillForm^ form = (FillForm^) container;
 
64
  return form->ComputeLayout(proposedSize, false);
 
65
}
 
66
 
 
67
//----------------- FillForm -----------------------------------------------------------------------
 
68
 
 
69
/**
 
70
 * Computes the entire layout of the form.
 
71
 * 
 
72
 * @param proposedSize The size to start from layouting. Since super ordinated controls may impose
 
73
 *                     a layout size we need to honor that (especially important for auto wrapping
 
74
 *                     labels).
 
75
 * @param resizeChildren Tells the function whether the computed client control bounds should be applied
 
76
 *                      (when doing a relayout) or not (when computing the preferred size).
 
77
 * @return The resulting size of the table.
 
78
 */
 
79
System::Drawing::Size FillForm::ComputeLayout(System::Drawing::Size proposedSize, bool resizeChildren)
 
80
{
 
81
  // This layout is actually very simple. Resize the first (and only) child control so that
 
82
  // it fills the entire client area of the container. If enabled resize the container to fit the
 
83
  // (preferred) size of the content.
 
84
 
 
85
  // Exclude any space needed to draw decoration (e.g. border) from layout processing.
 
86
  System::Drawing::Rectangle inner= DisplayRectangle;
 
87
  System::Drawing::Size current_size= Size;
 
88
  int horizontal_padding = current_size.Width - inner.Width;
 
89
  int vertical_padding = current_size.Height - inner.Height;
 
90
 
 
91
  if (Controls->Count > 0)
 
92
  {
 
93
    Control^ content= Controls[0];
 
94
    proposedSize.Width -= horizontal_padding;
 
95
    proposedSize.Height -= vertical_padding;
 
96
 
 
97
    ViewImpl::set_full_auto_resize(content);
 
98
    System::Drawing::Size contentSize= content->GetPreferredSize(proposedSize);
 
99
 
 
100
    if (ViewImpl::use_min_width_for_layout(content))
 
101
      contentSize.Width= content->MinimumSize.Width;
 
102
    if (ViewImpl::use_min_height_for_layout(content))
 
103
      contentSize.Height= content->MinimumSize.Height;
 
104
 
 
105
    // Adjust width of the container if it is too small or auto resizing is enabled.
 
106
    // Don't auto size if this is a standalone window (has no parent).
 
107
    bool auto_resize= /*(Parent != nullptr) &&*/ ViewImpl::can_auto_resize_horizontally(this);
 
108
    if (proposedSize.Width < contentSize.Width || auto_resize)
 
109
      proposedSize.Width = contentSize.Width;
 
110
 
 
111
    // Adjust height of the container if it is too small or auto resizing is enabled.
 
112
    auto_resize= /*(Parent != nullptr) &&*/ ViewImpl::can_auto_resize_vertically(this);
 
113
    if (proposedSize.Height < contentSize.Height || auto_resize)
 
114
      proposedSize.Height = contentSize.Height;
 
115
 
 
116
    if (resizeChildren)
 
117
    {
 
118
      // Now stretch the client control to fill the entire display area.
 
119
      ViewImpl::remove_auto_resize(content, mforms::ResizeBoth);
 
120
      content->Bounds= System::Drawing::Rectangle(inner.Location, proposedSize);
 
121
    }
 
122
 
 
123
    proposedSize.Width += horizontal_padding;
 
124
    proposedSize.Height += vertical_padding;
 
125
  }
 
126
 
 
127
  return proposedSize;
 
128
}
 
129
 
 
130
//--------------------------------------------------------------------------------------------------
 
131
 
 
132
System::Drawing::Size FillForm::GetPreferredSize(System::Drawing::Size proposedSize)
 
133
{
 
134
  System::Drawing::Size size= layoutEngine->GetPreferredSize(this, proposedSize);
 
135
  if (size.Width < MinimumSize.Width)
 
136
    size.Width= MinimumSize.Width;
 
137
  if (size.Height < MinimumSize.Height)
 
138
    size.Height= MinimumSize.Height;
 
139
  return size;
 
140
}
 
141
 
 
142
//-------------------------------------------------------------------------------------------------
 
143
 
 
144
bool FormImpl::create(::mforms::Form *self, ::mforms::Form *owner, ::mforms::FormFlag flag)
 
145
{
 
146
  FormImpl ^form= gcnew FormImpl(self, owner, flag);
 
147
 
 
148
  if (form != nullptr)
 
149
  {
 
150
    Windows::Forms::Form ^f= ViewImpl::create<FillForm>(self, form);
 
151
    if (owner != NULL)
 
152
      f->StartPosition = FormStartPosition::CenterParent;
 
153
    else
 
154
      f->StartPosition= FormStartPosition::Manual;
 
155
    f->Disposed += gcnew System::EventHandler(&FormImpl::disposed);
 
156
    f->FormClosing += gcnew FormClosingEventHandler(&FormImpl::closing);
 
157
 
 
158
    if (File::Exists("images/icons/MySQLWorkbench.ico"))
 
159
      f->Icon= gcnew Icon("images/icons/MySQLWorkbench.ico", Size(16, 16));
 
160
 
 
161
    if ((flag & mforms::FormToolWindow) != 0)
 
162
    {
 
163
      if ((flag & mforms::FormResizable) != 0)
 
164
        f->FormBorderStyle= FormBorderStyle::SizableToolWindow;
 
165
      else
 
166
        f->FormBorderStyle= FormBorderStyle::FixedToolWindow;
 
167
    }
 
168
    else
 
169
      if ((flag & mforms::FormSingleFrame) != 0)
 
170
        f->FormBorderStyle= FormBorderStyle::FixedSingle;
 
171
      else
 
172
        if ((flag & mforms::FormDialogFrame) != 0)
 
173
          f->FormBorderStyle= FormBorderStyle::FixedDialog;
 
174
        else
 
175
          if ((flag & mforms::FormResizable) != 0)
 
176
            f->FormBorderStyle= FormBorderStyle::Sizable;
 
177
          else
 
178
            f->FormBorderStyle= FormBorderStyle::None;
 
179
 
 
180
    f->MinimizeBox= (flag & mforms::FormMinimizable) != 0;
 
181
    f->MaximizeBox= (flag & mforms::FormMinimizable) != 0;
 
182
    //f->TopMost=
 
183
    if ((flag & mforms::FormStayOnTop) != 0)
 
184
      f->Owner = UtilitiesImpl::get_mainform();
 
185
 
 
186
    form->_hide_on_close= (flag & mforms::FormHideOnClose) != 0;
 
187
    ViewImpl::remove_auto_resize(f, mforms::ResizeBoth);
 
188
 
 
189
    return true;
 
190
  }
 
191
  return false;
 
192
}
 
193
 
 
194
//--------------------------------------------------------------------------------------------------
 
195
 
 
196
void FormImpl::set_title(::mforms::Form *self, const std::string &title)
 
197
{
 
198
  FormImpl^ form= (FormImpl^)ObjectImpl::FromUnmanaged(self);
 
199
 
 
200
  if (form != nullptr)
 
201
    form->get_control<Windows::Forms::Form>()->Text= gcnew String(title.c_str());
 
202
}
 
203
 
 
204
//--------------------------------------------------------------------------------------------------
 
205
 
 
206
void FormImpl::show_modal(::mforms::Form *self, ::mforms::Button *accept,::mforms::Button *cancel)
 
207
{
 
208
  FormImpl^ form= (FormImpl^)ObjectImpl::FromUnmanaged(self);
 
209
 
 
210
  if (form != nullptr)
 
211
  {
 
212
    Windows::Forms::Form ^f= form->get_control<Windows::Forms::Form>();
 
213
 
 
214
    if (accept)
 
215
    {
 
216
      f->AcceptButton= ((ViewImpl^)ObjectImpl::FromUnmanaged(accept))->get_control<Windows::Forms::Button>();
 
217
      f->AcceptButton->DialogResult = Windows::Forms::DialogResult::OK;
 
218
    }
 
219
    if (cancel)
 
220
    {
 
221
      f->CancelButton= ((ViewImpl^)ObjectImpl::FromUnmanaged(cancel))->get_control<Windows::Forms::Button>();
 
222
      f->CancelButton->DialogResult = Windows::Forms::DialogResult::Cancel;
 
223
    }
 
224
    // Make the window top most (so it stays above all others), but non-blocking.
 
225
    //f->TopMost= true;
 
226
    f->Owner = UtilitiesImpl::get_mainform();
 
227
    if (f->InvokeRequired)
 
228
      f->BeginInvoke(gcnew MethodInvoker(f, &Control::Show));
 
229
    else
 
230
      f->Show();
 
231
  }
 
232
}
 
233
 
 
234
//--------------------------------------------------------------------------------------------------
 
235
 
 
236
bool FormImpl::run_modal(::mforms::Form *self, ::mforms::Button *accept,::mforms::Button *cancel)
 
237
{
 
238
  FormImpl^ form= (FormImpl^)ObjectImpl::FromUnmanaged(self);
 
239
 
 
240
  if (form != nullptr)
 
241
  {
 
242
    Windows::Forms::Form ^f= form->get_control<Windows::Forms::Form>();
 
243
 
 
244
    if (accept)
 
245
    {
 
246
      f->AcceptButton= ((ViewImpl^)ObjectImpl::FromUnmanaged(accept))->get_control<Windows::Forms::Button>();
 
247
      f->AcceptButton->DialogResult = Windows::Forms::DialogResult::OK;
 
248
    }
 
249
    if (cancel)
 
250
    {
 
251
      f->CancelButton= ((ViewImpl^)ObjectImpl::FromUnmanaged(cancel))->get_control<Windows::Forms::Button>();
 
252
      f->CancelButton->DialogResult = Windows::Forms::DialogResult::Cancel;
 
253
    }
 
254
 
 
255
    mforms::Utilities::enter_modal_loop();
 
256
 
 
257
    Windows::Forms::DialogResult dialog_result;
 
258
    if (f->InvokeRequired)
 
259
    {
 
260
      Object^ invocation_result= f->Invoke(gcnew FillForm::ShowModalDelegate(f, &Windows::Forms::Form::ShowDialog),
 
261
        gcnew array<Object^> {form->parent});
 
262
      dialog_result= *(Windows::Forms::DialogResult^) (invocation_result);
 
263
    }
 
264
    else
 
265
      dialog_result= f->ShowDialog(form->parent);
 
266
 
 
267
    bool result = (dialog_result == Windows::Forms::DialogResult::OK);
 
268
 
 
269
    mforms::Utilities::leave_modal_loop();
 
270
 
 
271
    f->Hide();
 
272
 
 
273
    return result;
 
274
  }
 
275
  return false;
 
276
}
 
277
 
 
278
//--------------------------------------------------------------------------------------------------
 
279
 
 
280
void FormImpl::end_modal(::mforms::Form *self, bool result)
 
281
{
 
282
  FormImpl^ form= (FormImpl^)ObjectImpl::FromUnmanaged(self);
 
283
  if (form != nullptr)
 
284
  {
 
285
    Windows::Forms::Form ^f= form->get_control<Windows::Forms::Form>();
 
286
    f->DialogResult = result ? Windows::Forms::DialogResult::OK : Windows::Forms::DialogResult::Cancel;
 
287
  }
 
288
}
 
289
 
 
290
//--------------------------------------------------------------------------------------------------
 
291
 
 
292
/**
 
293
 * Called by the backend when a form is closed from the application side.
 
294
 */
 
295
void FormImpl::close(::mforms::Form *self)
 
296
{
 
297
  FormImpl^ form= (FormImpl^)ObjectImpl::FromUnmanaged(self);
 
298
 
 
299
  if (form != nullptr)
 
300
    form->get_control<Windows::Forms::Form>()->Close();
 
301
}
 
302
 
 
303
//--------------------------------------------------------------------------------------------------
 
304
 
 
305
void FormImpl::set_content(::mforms::Form *self, ::mforms::View *view)
 
306
{
 
307
  FormImpl^ form= (FormImpl^)ObjectImpl::FromUnmanaged(self);
 
308
 
 
309
  if (form != nullptr)
 
310
  {
 
311
    ViewImpl^ child= (ViewImpl^)ObjectImpl::FromUnmanaged(view);
 
312
    Control^ ctl= child->get_control<Control>();
 
313
    form->get_control<Windows::Forms::Form>()->Controls->Add(ctl);
 
314
  }
 
315
}
 
316
 
 
317
//--------------------------------------------------------------------------------------------------
 
318
 
 
319
 
 
320
void FormImpl::center(::mforms::Form *self)
 
321
{
 
322
  FormImpl^ form= (FormImpl^)ObjectImpl::FromUnmanaged(self);
 
323
  if (form != nullptr)
 
324
    form->center();
 
325
}
 
326
 
 
327
//--------------------------------------------------------------------------------------------------
 
328
 
 
329
void FormImpl::flush_events(::mforms::Form *self)
 
330
{
 
331
  FormImpl^ form= (FormImpl^)ObjectImpl::FromUnmanaged(self);
 
332
  if (form != nullptr)
 
333
    form->flush_events();
 
334
}
 
335
 
 
336
//--------------------------------------------------------------------------------------------------
 
337
 
 
338
FormImpl::FormImpl(::mforms::Form *form, ::mforms::Form *owner, ::mforms::FormFlag flag)
 
339
  : ViewImpl(form)
 
340
{
 
341
  if (owner != NULL)
 
342
  {
 
343
    // Meant is the window parent here, not the real owner.
 
344
    if (owner == mforms::Form::main_form())
 
345
      parent = Application::OpenForms[0];
 
346
    else
 
347
    {
 
348
      FormImpl^ parent_form= (FormImpl^)ObjectImpl::FromUnmanaged(owner);
 
349
      parent = parent_form->get_control<Windows::Forms::Form>();
 
350
    }
 
351
  }
 
352
  else
 
353
    parent = nullptr;
 
354
}
 
355
 
 
356
//--------------------------------------------------------------------------------------------------
 
357
 
 
358
/**
 
359
 * Sets the startup position of the form so that it is centered over its parent when displayed.
 
360
 * If the form has no parent the desktop is used. This is also the default setting.
 
361
 */
 
362
void FormImpl::center()
 
363
{
 
364
  Windows::Forms::Form^ form= get_control<Windows::Forms::Form>();
 
365
  form->StartPosition= FormStartPosition::CenterParent;
 
366
}
 
367
 
 
368
//--------------------------------------------------------------------------------------------------
 
369
 
 
370
void FormImpl::flush_events()
 
371
{
 
372
  Application::DoEvents();
 
373
}
 
374
 
 
375
//--------------------------------------------------------------------------------------------------
 
376
 
 
377
void FormImpl::disposed(System::Object ^sender, System::EventArgs ^e)
 
378
{
 
379
  Windows::Forms::Form^ native_form= (Windows::Forms::Form^)sender;
 
380
 
 
381
  if (native_form->Tag != nullptr)
 
382
  {
 
383
    ::mforms::Form* form=ViewImpl::get_backend_control<mforms::Form>(native_form);
 
384
    form->release();
 
385
  }
 
386
}
 
387
 
 
388
//--------------------------------------------------------------------------------------------------
 
389
 
 
390
/**
 
391
 * Called by the OS if the form is closed by the user (e.g. via red cross close button).
 
392
 */
 
393
void FormImpl::closing(System::Object ^sender, FormClosingEventArgs  ^e)
 
394
{
 
395
  Windows::Forms::Form^ native_form= (Windows::Forms::Form^)sender;
 
396
  mforms::Form* be_form= ViewImpl::get_backend_control<mforms::Form>(native_form);
 
397
  FormImpl^ wrapper_form= (FormImpl^)ObjectImpl::FromUnmanaged(be_form);
 
398
  if (wrapper_form->hide_on_close())
 
399
  {
 
400
    e->Cancel= true;
 
401
    native_form->Hide();
 
402
  }
 
403
 
 
404
  be_form->was_closed();
 
405
 
 
406
  // Do nothing if hiding is not requested. In this case Windows will dispose of the form implicitly.
 
407
}
 
408
 
 
409
//--------------------------------------------------------------------------------------------------
 
410
 
 
411
bool FormImpl::hide_on_close()
 
412
{
 
413
  return _hide_on_close;
 
414
}
 
415
 
 
416
//--------------------------------------------------------------------------------------------------
 
417