2
* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
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
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.
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
21
* Contains the implementation of the wrapper for a .NET form used by the mforms backend.
27
#include "wf_utilities.h"
29
using namespace System;
30
using namespace System::IO;
31
using namespace Drawing;
32
using namespace Windows::Forms;
34
using namespace MySQL;
35
using namespace MySQL::Forms;
37
//--------------------------------------------------------------------------------------------------
39
bool FormFillLayout::Layout(Object^ container, LayoutEventArgs^ arguments)
41
FillForm^ form = (FillForm^) container;
43
ViewImpl::adjust_auto_resize_from_docking(form);
44
System::Drawing::Size newSize = form->ComputeLayout(form->Size, true);
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;
51
// Finally adjust the container.
52
bool parentLayoutNeeded= !form->Size.Equals(newSize);
53
if (parentLayoutNeeded)
54
ViewImpl::resize_with_docking(form, newSize);
56
return parentLayoutNeeded;
59
//-------------------------------------------------------------------------------------------------
61
System::Drawing::Size FormFillLayout::GetPreferredSize(Control^ container, System::Drawing::Size proposedSize)
63
FillForm^ form = (FillForm^) container;
64
return form->ComputeLayout(proposedSize, false);
67
//----------------- FillForm -----------------------------------------------------------------------
70
* Computes the entire layout of the form.
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
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.
79
System::Drawing::Size FillForm::ComputeLayout(System::Drawing::Size proposedSize, bool resizeChildren)
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.
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;
91
if (Controls->Count > 0)
93
Control^ content= Controls[0];
94
proposedSize.Width -= horizontal_padding;
95
proposedSize.Height -= vertical_padding;
97
ViewImpl::set_full_auto_resize(content);
98
System::Drawing::Size contentSize= content->GetPreferredSize(proposedSize);
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;
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;
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;
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);
123
proposedSize.Width += horizontal_padding;
124
proposedSize.Height += vertical_padding;
130
//--------------------------------------------------------------------------------------------------
132
System::Drawing::Size FillForm::GetPreferredSize(System::Drawing::Size proposedSize)
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;
142
//-------------------------------------------------------------------------------------------------
144
bool FormImpl::create(::mforms::Form *self, ::mforms::Form *owner, ::mforms::FormFlag flag)
146
FormImpl ^form= gcnew FormImpl(self, owner, flag);
150
Windows::Forms::Form ^f= ViewImpl::create<FillForm>(self, form);
152
f->StartPosition = FormStartPosition::CenterParent;
154
f->StartPosition= FormStartPosition::Manual;
155
f->Disposed += gcnew System::EventHandler(&FormImpl::disposed);
156
f->FormClosing += gcnew FormClosingEventHandler(&FormImpl::closing);
158
if (File::Exists("images/icons/MySQLWorkbench.ico"))
159
f->Icon= gcnew Icon("images/icons/MySQLWorkbench.ico", Size(16, 16));
161
if ((flag & mforms::FormToolWindow) != 0)
163
if ((flag & mforms::FormResizable) != 0)
164
f->FormBorderStyle= FormBorderStyle::SizableToolWindow;
166
f->FormBorderStyle= FormBorderStyle::FixedToolWindow;
169
if ((flag & mforms::FormSingleFrame) != 0)
170
f->FormBorderStyle= FormBorderStyle::FixedSingle;
172
if ((flag & mforms::FormDialogFrame) != 0)
173
f->FormBorderStyle= FormBorderStyle::FixedDialog;
175
if ((flag & mforms::FormResizable) != 0)
176
f->FormBorderStyle= FormBorderStyle::Sizable;
178
f->FormBorderStyle= FormBorderStyle::None;
180
f->MinimizeBox= (flag & mforms::FormMinimizable) != 0;
181
f->MaximizeBox= (flag & mforms::FormMinimizable) != 0;
183
if ((flag & mforms::FormStayOnTop) != 0)
184
f->Owner = UtilitiesImpl::get_mainform();
186
form->_hide_on_close= (flag & mforms::FormHideOnClose) != 0;
187
ViewImpl::remove_auto_resize(f, mforms::ResizeBoth);
194
//--------------------------------------------------------------------------------------------------
196
void FormImpl::set_title(::mforms::Form *self, const std::string &title)
198
FormImpl^ form= (FormImpl^)ObjectImpl::FromUnmanaged(self);
201
form->get_control<Windows::Forms::Form>()->Text= gcnew String(title.c_str());
204
//--------------------------------------------------------------------------------------------------
206
void FormImpl::show_modal(::mforms::Form *self, ::mforms::Button *accept,::mforms::Button *cancel)
208
FormImpl^ form= (FormImpl^)ObjectImpl::FromUnmanaged(self);
212
Windows::Forms::Form ^f= form->get_control<Windows::Forms::Form>();
216
f->AcceptButton= ((ViewImpl^)ObjectImpl::FromUnmanaged(accept))->get_control<Windows::Forms::Button>();
217
f->AcceptButton->DialogResult = Windows::Forms::DialogResult::OK;
221
f->CancelButton= ((ViewImpl^)ObjectImpl::FromUnmanaged(cancel))->get_control<Windows::Forms::Button>();
222
f->CancelButton->DialogResult = Windows::Forms::DialogResult::Cancel;
224
// Make the window top most (so it stays above all others), but non-blocking.
226
f->Owner = UtilitiesImpl::get_mainform();
227
if (f->InvokeRequired)
228
f->BeginInvoke(gcnew MethodInvoker(f, &Control::Show));
234
//--------------------------------------------------------------------------------------------------
236
bool FormImpl::run_modal(::mforms::Form *self, ::mforms::Button *accept,::mforms::Button *cancel)
238
FormImpl^ form= (FormImpl^)ObjectImpl::FromUnmanaged(self);
242
Windows::Forms::Form ^f= form->get_control<Windows::Forms::Form>();
246
f->AcceptButton= ((ViewImpl^)ObjectImpl::FromUnmanaged(accept))->get_control<Windows::Forms::Button>();
247
f->AcceptButton->DialogResult = Windows::Forms::DialogResult::OK;
251
f->CancelButton= ((ViewImpl^)ObjectImpl::FromUnmanaged(cancel))->get_control<Windows::Forms::Button>();
252
f->CancelButton->DialogResult = Windows::Forms::DialogResult::Cancel;
255
mforms::Utilities::enter_modal_loop();
257
Windows::Forms::DialogResult dialog_result;
258
if (f->InvokeRequired)
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);
265
dialog_result= f->ShowDialog(form->parent);
267
bool result = (dialog_result == Windows::Forms::DialogResult::OK);
269
mforms::Utilities::leave_modal_loop();
278
//--------------------------------------------------------------------------------------------------
280
void FormImpl::end_modal(::mforms::Form *self, bool result)
282
FormImpl^ form= (FormImpl^)ObjectImpl::FromUnmanaged(self);
285
Windows::Forms::Form ^f= form->get_control<Windows::Forms::Form>();
286
f->DialogResult = result ? Windows::Forms::DialogResult::OK : Windows::Forms::DialogResult::Cancel;
290
//--------------------------------------------------------------------------------------------------
293
* Called by the backend when a form is closed from the application side.
295
void FormImpl::close(::mforms::Form *self)
297
FormImpl^ form= (FormImpl^)ObjectImpl::FromUnmanaged(self);
300
form->get_control<Windows::Forms::Form>()->Close();
303
//--------------------------------------------------------------------------------------------------
305
void FormImpl::set_content(::mforms::Form *self, ::mforms::View *view)
307
FormImpl^ form= (FormImpl^)ObjectImpl::FromUnmanaged(self);
311
ViewImpl^ child= (ViewImpl^)ObjectImpl::FromUnmanaged(view);
312
Control^ ctl= child->get_control<Control>();
313
form->get_control<Windows::Forms::Form>()->Controls->Add(ctl);
317
//--------------------------------------------------------------------------------------------------
320
void FormImpl::center(::mforms::Form *self)
322
FormImpl^ form= (FormImpl^)ObjectImpl::FromUnmanaged(self);
327
//--------------------------------------------------------------------------------------------------
329
void FormImpl::flush_events(::mforms::Form *self)
331
FormImpl^ form= (FormImpl^)ObjectImpl::FromUnmanaged(self);
333
form->flush_events();
336
//--------------------------------------------------------------------------------------------------
338
FormImpl::FormImpl(::mforms::Form *form, ::mforms::Form *owner, ::mforms::FormFlag flag)
343
// Meant is the window parent here, not the real owner.
344
if (owner == mforms::Form::main_form())
345
parent = Application::OpenForms[0];
348
FormImpl^ parent_form= (FormImpl^)ObjectImpl::FromUnmanaged(owner);
349
parent = parent_form->get_control<Windows::Forms::Form>();
356
//--------------------------------------------------------------------------------------------------
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.
362
void FormImpl::center()
364
Windows::Forms::Form^ form= get_control<Windows::Forms::Form>();
365
form->StartPosition= FormStartPosition::CenterParent;
368
//--------------------------------------------------------------------------------------------------
370
void FormImpl::flush_events()
372
Application::DoEvents();
375
//--------------------------------------------------------------------------------------------------
377
void FormImpl::disposed(System::Object ^sender, System::EventArgs ^e)
379
Windows::Forms::Form^ native_form= (Windows::Forms::Form^)sender;
381
if (native_form->Tag != nullptr)
383
::mforms::Form* form=ViewImpl::get_backend_control<mforms::Form>(native_form);
388
//--------------------------------------------------------------------------------------------------
391
* Called by the OS if the form is closed by the user (e.g. via red cross close button).
393
void FormImpl::closing(System::Object ^sender, FormClosingEventArgs ^e)
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())
404
be_form->was_closed();
406
// Do nothing if hiding is not requested. In this case Windows will dispose of the form implicitly.
409
//--------------------------------------------------------------------------------------------------
411
bool FormImpl::hide_on_close()
413
return _hide_on_close;
416
//--------------------------------------------------------------------------------------------------