File: toolbar.cpp

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (253 lines) | stat: -rw-r--r-- 5,773 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
/* 
 * Copyright (c) 2011, 2016, 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
 */

#include "mforms/mforms.h"
#include "base/log.h"

DEFAULT_LOG_DOMAIN("mforms");

using namespace mforms;

ToolBar::ToolBar(ToolBarType type)
: _type(type)
{
  _impl = &ControlFactory::get_instance()->_tool_bar_impl;
  _impl->create_tool_bar(this, type);
}


ToolBar::~ToolBar()
{
  for (std::vector<ToolBarItem*>::iterator iter = _items.begin(); iter != _items.end(); ++iter)
    (*iter)->release();
  _items.clear();
}
    
ToolBarItem *ToolBar::find_item(const std::string &name)
{
  for (std::vector<ToolBarItem*>::iterator iter = _items.begin(); iter != _items.end(); ++iter)
    if ((*iter)->get_name() == name)
      return *iter;
  return 0;
}


void ToolBar::validate()
{
  for (std::vector<ToolBarItem*>::iterator iter = _items.begin(); iter != _items.end(); ++iter)
    (*iter)->validate();
}


void ToolBar::set_item_enabled(const std::string &name, bool flag)
{
  ToolBarItem *item = find_item(name);
  if (item)
    item->set_enabled(flag);
}

void ToolBar::set_item_checked(const std::string &name, bool flag)
{
  ToolBarItem *item = find_item(name);
  if (item)
    item->set_checked(flag);
}

bool ToolBar::get_item_checked(const std::string &name)
{
  ToolBarItem *item = find_item(name);
  if (item)
    return item->get_checked();
  return false;
}


void ToolBar::add_item(ToolBarItem *item)
{
  insert_item(-1, item);
}


ToolBarItem *ToolBar::add_separator_item(const std::string &name)
{
  ToolBarItem *item = mforms::manage(new ToolBarItem(SeparatorItem));
  item->set_name(name);
  add_item(item);
  return item;
}


void ToolBar::insert_item(int index, ToolBarItem *item)
{
  assert(item->is_managed());

  if (index < 0 || index > (int) _items.size())
    index = (int)_items.size();
  _impl->insert_item(this, index, item);

  if (!item->_release_on_add) // Means: don't increase the ref count, the caller retained already, but won't release.
    item->retain();
  else
    item->_release_on_add = false;

  _items.push_back(item);
}

void ToolBar::remove_all()
{
  for (std::vector<ToolBarItem*>::iterator iter = _items.begin(); iter != _items.end(); ++iter)
  {
    _impl->remove_item(this, *iter);
    (*iter)->release();
  }
  _items.clear();
}

void ToolBar::remove_item(ToolBarItem *item)
{
  std::vector<ToolBarItem*>::iterator iter = std::find(_items.begin(), _items.end(), item);
  if (iter != _items.end())
  {
    _impl->remove_item(this, *iter);
    (*iter)->release();
    _items.erase(iter);
  }
}


ToolBarItem::ToolBarItem(ToolBarItemType type, const bool expandable)
: _type(type), _updating(false), _expandable(expandable)
{
  _impl = &mforms::ControlFactory::get_instance()->_tool_bar_impl;
  _impl->create_tool_item(this, type);
}

void ToolBarItem::set_text(const std::string &text)
{
  _updating = true;
  _impl->set_item_text(this, text);
  _updating = false;
}

std::string ToolBarItem::get_text()
{
  return _impl->get_item_text(this);
}
  
void ToolBarItem::set_tooltip(const std::string &text)
{
  _impl->set_item_tooltip(this, text);
}
  
void ToolBarItem::set_icon(const std::string &path)
{
  _icon = path;
  _impl->set_item_icon(this, path);
}

void ToolBarItem::set_alt_icon(const std::string &path)
{
  _alt_icon = path;
  _impl->set_item_alt_icon(this, path);
}

void ToolBarItem::set_enabled(bool flag)
{
  _impl->set_item_enabled(this, flag);
}

bool ToolBarItem::get_enabled()
{
  return _impl->get_item_enabled(this);
}


void ToolBarItem::set_checked(bool flag)
{
  _updating = true;
  _impl->set_item_checked(this, flag);
  _updating = false;
}


bool ToolBarItem::get_checked()
{
  return _impl->get_item_checked(this);
}


void ToolBarItem::set_name(const std::string &name)
{
  _name = name;
  if (_impl->set_item_name)
    _impl->set_item_name(this, name);
}

void ToolBarItem::set_selector_items(const std::vector<std::string>& values)
{
  _updating = true;
  _impl->set_selector_items(this, values);
  _updating = false;
}

void ToolBarItem::callback()
{
  try
  {
    if (!_updating)
      _clicked_signal(this);
  }
  catch (std::exception &exc)
  {
    log_error("Unhandled exception in toolbar callback for %s: %s\n", _name.c_str(), exc.what());
    mforms::Utilities::show_error("Unhandled Exception", exc.what(), "OK");
  }
}

void ToolBarItem::validate()
{
  if (_validate)
    set_enabled(_validate());
}

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

void ToolBarItem::search(const std::string& text)
{
  if (_search)
    _search(text);
}

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

void ToolBarItem::set_validator(const boost::function<bool ()> &slot)
{
  _validate = slot;
  validate();
}

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

void ToolBarItem::set_search_handler(const boost::function<void (const std::string&)> &slot)
{
  _search = slot;
}

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