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 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
|
/*
* Copyright (c) 2012, 2015, 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"
using namespace mforms;
TreeNodeSkeleton::TreeNodeSkeleton(const std::string& strcaption, const std::string& stricon, const std::string& strtag)
{
caption = strcaption;
icon = stricon;
tag = strtag;
}
TreeNodeCollectionSkeleton::TreeNodeCollectionSkeleton(const std::string& stricon)
{
icon = stricon;
}
TreeNodeRef::TreeNodeRef(TreeNode *anode)
: node(anode)
{
if (node)
node->retain();
}
TreeNodeRef::TreeNodeRef(const TreeNodeRef &other)
{
node = other.node;
if (node)
node->retain();
}
TreeNodeRef::~TreeNodeRef()
{
if (node)
node->release();
}
TreeNodeRef &TreeNodeRef::operator= (const TreeNodeRef &other)
{
if (node != other.node)
{
if (other.node)
other.node->retain();
if (node)
node->release();
node = other.node;
}
return *this;
}
TreeNode *TreeNodeRef::operator->() const
{
if (!node)
throw std::logic_error("Attempt to dereference NULL TreeNode");
return node;
}
TreeNode *TreeNodeRef::operator->()
{
if (!node)
throw std::logic_error("Attempt to dereference NULL TreeNode");
return node;
}
bool TreeNodeRef::operator == (const TreeNodeRef &other) const
{
if (node == other.node)
return true;
if (other.node && node)
return node->equals(*other.node);
return false;
}
bool TreeNodeRef::operator != (const TreeNodeRef &other) const
{
if (node == other.node)
return false;
if (other.node && node)
return !node->equals(*other.node);
return true;
}
bool mforms::TreeNodeRef::is_valid()
{
return node != NULL;
}
// -------------------------------------------------------------------------------------------------
void TreeNode::remove_children()
{
if (is_valid())
{
for (int i = count()-1; i >= 0; --i)
{
TreeNodeRef child(get_child(i));
if (child)
child->remove_from_parent();
}
}
}
TreeNodeRef TreeNode::find_child_with_tag(const std::string &tag)
{
for (int c = count(), i = 0; i < c; i++)
{
TreeNodeRef child(get_child(i));
if (child && child->get_tag() == tag)
return child;
}
return TreeNodeRef();
}
//--------------------------------------------------------------------------------------------------
void TreeNode::toggle()
{
if (can_expand())
{
if (!is_expanded())
expand();
else
collapse();
}
}
//----------------- TreeNodeView -------------------------------------------------------------------
TreeNodeView::TreeNodeView(TreeOptions options)
: _context_menu(0), _header_menu(0), _update_count(0), _clicked_header_column(0), _end_column_called(false)
{
_treeview_impl= &ControlFactory::get_instance()->_treenodeview_impl;
_index_on_tag = (options & TreeIndexOnTag) ? true : false;
_treeview_impl->create(this, options);
}
//--------------------------------------------------------------------------------------------------
TreeNodeView::~TreeNodeView()
{
_update_count++; // Avoid any callbacks/events.
}
//--------------------------------------------------------------------------------------------------
void TreeNodeView::set_context_menu(ContextMenu *menu)
{
_context_menu = menu;
}
//--------------------------------------------------------------------------------------------------
void TreeNodeView::set_header_menu(ContextMenu *menu)
{
_header_menu = menu;
}
//--------------------------------------------------------------------------------------------------
void TreeNodeView::header_clicked(int column)
{
_clicked_header_column = column;
}
//--------------------------------------------------------------------------------------------------
int TreeNodeView::add_column(TreeColumnType type, const std::string &name, int initial_width, bool editable, bool attributed)
{
if (_end_column_called)
throw std::logic_error("Add column called, after end_columns has been called");
_column_types.push_back(type);
#if defined(_WIN32)
return _treeview_impl->add_column(this, type, name, initial_width, editable);
#else
return _treeview_impl->add_column(this, type, name, initial_width, editable, attributed);
#endif
}
void TreeNodeView::set_column_title(int column, const std::string &title)
{
_treeview_impl->set_column_title(this, column, title);
}
TreeColumnType TreeNodeView::get_column_type(int column)
{
if (column >= 0 && column < (int)_column_types.size())
return _column_types[column];
return StringColumnType;
}
void TreeNodeView::set_allow_sorting(bool flag)
{
if (!_end_column_called)
throw std::logic_error("TreeNodeView::set_allow_sorting() must be called after end_columns()");
_treeview_impl->set_allow_sorting(this, flag);
}
void TreeNodeView::end_columns()
{
_end_column_called = true;
_treeview_impl->end_columns(this);
}
void TreeNodeView::clear()
{
_treeview_impl->clear(this);
}
TreeNodeRef TreeNodeView::root_node()
{
return _treeview_impl->root_node(this);
}
TreeNodeRef TreeNodeView::add_node()
{
return root_node()->add_child();
}
TreeNodeRef TreeNodeView::get_selected_node()
{
return _treeview_impl->get_selected_node(this);
}
void TreeNodeView::set_selection_mode(TreeSelectionMode mode)
{
_treeview_impl->set_selection_mode(this, mode);
}
TreeSelectionMode TreeNodeView::get_selection_mode()
{
return _treeview_impl->get_selection_mode(this);
}
int TreeNodeView::get_selected_row()
{
TreeNodeRef node(get_selected_node());
return row_for_node(node);
}
std::list<TreeNodeRef> TreeNodeView::get_selection()
{
return _treeview_impl->get_selection(this);
}
void TreeNodeView::clear_selection()
{
_treeview_impl->clear_selection(this);
}
void TreeNodeView::select_node(TreeNodeRef node)
{
if (node.is_valid())
{
_update_count++;
clear_selection();
_treeview_impl->set_selected(this, node, true);
_update_count--;
}
}
void TreeNodeView::set_node_selected(TreeNodeRef node, bool flag)
{
if (node.is_valid())
{
_update_count++;
_treeview_impl->set_selected(this, node, flag);
_update_count--;
}
}
void TreeNodeView::set_row_height(int height)
{
if (_treeview_impl->set_row_height)
_treeview_impl->set_row_height(this, height);
}
//--------------------------------------------------------------------------------------------------
void TreeNodeView::set_cell_edit_handler(const boost::function<void (TreeNodeRef, int, std::string)> &handler)
{
_cell_edited = handler;
}
//--------------------------------------------------------------------------------------------------
bool TreeNodeView::cell_edited(TreeNodeRef row, int column, const std::string &value)
{
if (_cell_edited)
{
_cell_edited(row, column, value);
return false;
}
return true;
}
//--------------------------------------------------------------------------------------------------
bool TreeNodeView::get_drag_data(DragDetails &details, void **data, std::string &format)
{
return false;
}
//--------------------------------------------------------------------------------------------------
void TreeNodeView::drag_finished(DragOperation operation)
{
}
//--------------------------------------------------------------------------------------------------
void TreeNodeView::column_resized(int column)
{
_signal_column_resized(column);
}
//--------------------------------------------------------------------------------------------------
void TreeNodeView::changed()
{
if (_update_count == 0)
_signal_changed();
}
//--------------------------------------------------------------------------------------------------
void TreeNodeView::node_activated(TreeNodeRef row, int column)
{
_signal_activated(row, column);
}
//--------------------------------------------------------------------------------------------------
void TreeNodeView::set_row_overlay_handler(const boost::function<std::vector<std::string> (TreeNodeRef)> &overlay_icons_for_node)
{
_overlay_icons_for_node = overlay_icons_for_node;
}
//--------------------------------------------------------------------------------------------------
std::vector<std::string> TreeNodeView::overlay_icons_for_node(TreeNodeRef row)
{
if (_overlay_icons_for_node)
return _overlay_icons_for_node(row);
return std::vector<std::string>();
}
//--------------------------------------------------------------------------------------------------
void TreeNodeView::overlay_icon_for_node_clicked(TreeNodeRef row, int index)
{
node_activated(row, -(index+1));
}
//--------------------------------------------------------------------------------------------------
/**
* Descendants can override this method to indicate expandability depending on other information.
*/
bool TreeNodeView::can_expand(TreeNodeRef row)
{
return row->count() > 0;
}
//--------------------------------------------------------------------------------------------------
void TreeNodeView::expand_toggle(TreeNodeRef row, bool expanded)
{
_signal_expand_toggle(row, expanded);
}
//--------------------------------------------------------------------------------------------------
void TreeNodeView::freeze_refresh()
{
_treeview_impl->freeze_refresh(this, true);
}
void TreeNodeView::thaw_refresh()
{
_treeview_impl->freeze_refresh(this, false);
}
int TreeNodeView::row_for_node(TreeNodeRef node)
{
return _treeview_impl->row_for_node(this, node);
}
TreeNodeRef TreeNodeView::node_at_row(int row)
{
return _treeview_impl->node_at_row(this, row);
}
mforms::TreeNodeRef mforms::TreeNodeView::node_at_position(base::Point position)
{
return _treeview_impl->node_at_position(this, position);
}
TreeNodeRef TreeNodeView::node_with_tag(const std::string &tag)
{
if (!_index_on_tag)
throw std::logic_error("Tree was not created with TreeIndexOnTag");
return _treeview_impl->node_with_tag(this, tag);
}
void TreeNodeView::set_column_visible(int column, bool flag)
{
if (_treeview_impl->set_column_visible)
_treeview_impl->set_column_visible(this, column, flag);
}
bool TreeNodeView::get_column_visible(int column)
{
if (_treeview_impl->get_column_visible)
return _treeview_impl->get_column_visible(this, column);
return true;
}
void TreeNodeView::set_column_width(int column, int width)
{
if (_treeview_impl->set_column_width)
_treeview_impl->set_column_width(this, column, width);
}
int TreeNodeView::get_column_width(int column)
{
if (_treeview_impl->get_column_width)
return _treeview_impl->get_column_width(this, column);
return 0;
}
double TreeNodeView::parse_string_with_unit(const char *s)
{
char *end = NULL;
double value = strtod(s, &end);
if (*end == ' ')
++end;
switch (*end)
{
case 'p': // pico
value /= 1000000000000.0;
break;
case 'n': // nano
value /= 1000000000.0;
break;
case 'u': // micro
value /= 1000000.0;
break;
case 'm': // milli
value /= 1000.0;
break;
case 'h': // special case for hours
value *= 3600.0;
break;
case 'K': // kilo
case 'k':
if (*(end+1) == 'i')
value *= 1024.0;
else
value *= 1000.0;
break;
case 'M': // mega
if (*(end+1) == 'i')
value *= 1048576.0;
else
value *= 1000000.0;
break;
case 'G': // giga
if (*(end+1) == 'i')
value *= 1073741824.0;
else
value *= 1000000000.0;
break;
case 'T': // tera
if (*(end+1) == 'i')
value *= 1099511627776.0;
else
value *= 1000000000000.0;
break;
case 'P': // peta
if (*(end+1) == 'i')
value *= 1125899906842624.0;
else
value *= 1000000000000000.0;
break;
}
return value;
}
|