File: view.cpp

package info (click to toggle)
mysql-workbench 5.2.40%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 53,880 kB
  • sloc: cpp: 419,850; yacc: 74,784; xml: 54,510; python: 31,455; sh: 9,423; ansic: 4,736; makefile: 2,442; php: 529; java: 237
file content (441 lines) | stat: -rw-r--r-- 11,663 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/* 
 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 of the
 * License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

/**
 * Implementation of the mforms view, which is the base for most of the visual controls in mforms.
 */

#include "stdafx.h"

using namespace mforms;
using namespace MySQL;

//--------------------------------------------------------------------------------------------------

View::View()
{
  _parent= 0;
  _view_impl= &ControlFactory::get_instance()->_view_impl;
  _resize_mode= ResizeBoth;
  _layout_dirty= true;
}

//--------------------------------------------------------------------------------------------------

View::~View()
{
  App::get()->view_destroyed(this);
  
  set_destroying();
  if (_parent)
    _parent->remove_subview(this);
  
  std::list<std::pair<View*,bool> >::iterator iter;
  
  while ((iter = _subviews.begin()) != _subviews.end())
  {
    bool managed = iter->second;
    View *sv = iter->first;
    sv->_parent = NULL;
    
    _subviews.erase(iter);
    if (managed)
    {
      sv->release();
    }
  }

  // Let the frontend delete all resources it allocated.
  if (_view_impl->destroy)
    _view_impl->destroy(this);
}

//--------------------------------------------------------------------------------------------------

// XXX: no longer needed, objects can be queried if they are managed.
void View::set_managed()
{
  Object::set_managed();
  if (_parent)
  {
    for (std::list<std::pair<View*,bool> > ::iterator iter= _parent->_subviews.begin(); iter != _parent->_subviews.end(); ++iter)
    {
      if (iter->first == this)
      {
        iter->second = true;
        break;
      }
    }    
  }
}

//--------------------------------------------------------------------------------------------------

// XXX: add and remove should retain and release. Note: some subclasses already do this on their own.
// This needs to be consolidated here.
void View::add_subview(View *sv)
{
  _subviews.push_back(std::make_pair(sv, sv->_managed));
}

//--------------------------------------------------------------------------------------------------

void View::remove_subview(View *sv)
{
  if (!is_destroying())
  {
    for (std::list<std::pair<View*,bool> > ::iterator iter= _subviews.begin(); iter != _subviews.end(); ++iter)
    {
      if (iter->first == sv)
      {
        _subviews.erase(iter);
        break;
      }
    }
  }
}

//--------------------------------------------------------------------------------------------------

/**
 * Searches for a subview with the given name in this view or any of its subviews using a depth-first search.
 */
View *View::find_subview(const std::string &name)
{
  for (std::list<std::pair<View*,bool> > ::const_iterator iter= _subviews.begin(); iter != _subviews.end(); ++iter)
  {
    if (iter->first->get_name() == name)
      return iter->first;
    
    View *sv= iter->first->find_subview(name);
    if (sv)
      return sv;
  }
  return 0;  
}

//--------------------------------------------------------------------------------------------------

int View::get_subview_index(View *sv)
{
  int i= 0;
  for (std::list<std::pair<View*,bool> > ::const_iterator iter= _subviews.begin(); iter != _subviews.end(); ++iter, ++i)
  {
    if (iter->first == sv)
      return i;
  }
  return -1;  
}

//--------------------------------------------------------------------------------------------------

View *View::get_subview_at_index(int index)
{
  int i= 0;
  for (std::list<std::pair<View*,bool> > ::const_iterator iter= _subviews.begin(); iter != _subviews.end(); ++iter, ++i)
  {
    if (i == index)
      return iter->first;
  }
  return 0;  
}

//--------------------------------------------------------------------------------------------------

/**
 * Returns true if the given subview is a direct child of this view.
 */
bool View::contains_subview(View* subview)
{
  return get_subview_index(subview) >= 0;
}

//--------------------------------------------------------------------------------------------------

AutoResizeMode View::get_resize_mode()
{ 
  return _resize_mode;
}

//--------------------------------------------------------------------------------------------------

void View::set_resize_mode(AutoResizeMode mode)
{
  _resize_mode= mode;
}

//--------------------------------------------------------------------------------------------------

void View::set_name(const std::string &name)
{
  _name= name;

  // Optional implementation.
  if (_view_impl->set_name)
    _view_impl->set_name(this, name);
}

//--------------------------------------------------------------------------------------------------

void View::set_tooltip(const std::string &text)
{
  _view_impl->set_tooltip(this, text);
}

//--------------------------------------------------------------------------------------------------

void View::set_font(const std::string &fontDescription)
{
  _view_impl->set_font(this, fontDescription);
}

//--------------------------------------------------------------------------------------------------

std::string View::get_name()
{ 
  return _name; 
}

//--------------------------------------------------------------------------------------------------

void View::set_parent(View *parent)
{
  _parent= parent;
  if (_managed)
    set_managed();
}

//--------------------------------------------------------------------------------------------------

View* View::get_parent() const
{
  return _parent; 
}

//--------------------------------------------------------------------------------------------------

Form *View::get_parent_form() const
{
  View *parent= get_parent();
  Form *form= 0;
  while (parent && (form = dynamic_cast<Form*>(parent)) == 0)
    parent= parent->get_parent();

  return form;
}

//--------------------------------------------------------------------------------------------------

int View::get_width()
{
  return (*_view_impl->get_width)(this);
}

//--------------------------------------------------------------------------------------------------

int View::get_height()
{
  return (*_view_impl->get_height)(this);
}

//--------------------------------------------------------------------------------------------------

int View::get_preferred_width()
{
  return (*_view_impl->get_preferred_width)(this);
}

//--------------------------------------------------------------------------------------------------

int View::get_preferred_height()
{
  return (*_view_impl->get_preferred_height)(this);
}

//--------------------------------------------------------------------------------------------------

int View::get_x()
{
  return (*_view_impl->get_x)(this);
}

//--------------------------------------------------------------------------------------------------

int View::get_y()
{
  return (*_view_impl->get_y)(this);
}

//--------------------------------------------------------------------------------------------------

void View::set_position(int x, int y)
{
  (*_view_impl->set_position)(this, x, y);
}

//--------------------------------------------------------------------------------------------------

void View::set_size(int width, int height)
{
  set_layout_dirty(true);
  (*_view_impl->set_size)(this, width, height);
}

//--------------------------------------------------------------------------------------------------

void View::client_to_screen(int& x, int& y)
{
  (*_view_impl->client_to_screen)(this, x, y);
}

//--------------------------------------------------------------------------------------------------

void View::show(bool flag)
{
  (*_view_impl->show)(this, flag);
}

//--------------------------------------------------------------------------------------------------

bool View::is_shown()
{
  return (*_view_impl->is_shown)(this);
}

//--------------------------------------------------------------------------------------------------

void View::set_enabled(bool flag)
{
  (*_view_impl->set_enabled)(this, flag);
}

//--------------------------------------------------------------------------------------------------

bool View::is_enabled()
{
  return (*_view_impl->is_enabled)(this);
}

//--------------------------------------------------------------------------------------------------


void View::set_needs_repaint()
{
  _view_impl->set_needs_repaint(this);
}

//--------------------------------------------------------------------------------------------------

void View::set_layout_dirty(bool value)
{
  _layout_dirty= value;
  if (_parent != NULL && value)
    _parent->set_layout_dirty(true);
}

//--------------------------------------------------------------------------------------------------

bool View::is_layout_dirty()
{
  return _layout_dirty;
}

//--------------------------------------------------------------------------------------------------

void View::relayout()
{
  if (_parent != NULL)
    _parent->relayout();
  (*_view_impl->relayout)(this);
}

//--------------------------------------------------------------------------------------------------

void View::suspend_layout()
{
  if (_view_impl->suspend_layout)
    _view_impl->suspend_layout(this, true);
}

//--------------------------------------------------------------------------------------------------

void View::resume_layout()
{
  if (_view_impl->suspend_layout)
    _view_impl->suspend_layout(this, false);
}

//--------------------------------------------------------------------------------------------------

void View::set_front_color(const std::string &color)
{
  _view_impl->set_front_color(this, color);
}

//--------------------------------------------------------------------------------------------------

std::string View::get_front_color()
{
  return _view_impl->get_front_color(this);
}

//--------------------------------------------------------------------------------------------------

void View::set_back_color(const std::string &color)
{
  _view_impl->set_back_color(this, color);
}

//--------------------------------------------------------------------------------------------------

std::string View::get_back_color()
{
  return _view_impl->get_back_color(this);
}

//--------------------------------------------------------------------------------------------------

void View::set_back_image(const std::string &path, ImageLayout layout)
{
  _view_impl->set_back_image(this, path, layout);
}

//--------------------------------------------------------------------------------------------------

void View::show_retain_counts(int depth)
{
  printf("%*s '%s' (%i)\n", depth, "--", get_name().c_str(), retain_count());

  for (std::list<std::pair<View*,bool> >::const_iterator iter= _subviews.begin();
       iter != _subviews.end(); ++iter)
  {
    iter->first->show_retain_counts(depth+1);
  }
}

//--------------------------------------------------------------------------------------------------

void View::flush_events()
{
  if (_view_impl->flush_events)
    _view_impl->flush_events(this);
}

//--------------------------------------------------------------------------------------------------