45
48
bear::gui::visual_component::visual_component( visual_component* owner )
46
49
: m_box(0, 0, 0, 0), m_owner(owner), m_focused_component(-1),
47
m_visible(true), m_input_priority(false), m_border_color(0, 0, 0, 0)
50
m_visible(true), m_input_priority(false), m_enabled(true),
51
m_top_left_border_color(0, 0, 0, 0),
52
m_bottom_right_border_color(0, 0, 0, 0), m_background_color(0, 0, 0, 0)
50
55
m_owner->add_component(this);
68
73
void bear::gui::visual_component::render
69
74
( std::list<visual::scene_element>& e ) const
79
scene_element_list sub_e;
81
// display the current component
84
// display the sub components
85
for ( component_list::const_iterator it=m_components.begin();
86
it!=m_components.end();
88
(*it)->render( sub_e );
90
// the scene elements are placed in the current component, so we adjust
91
// their position according to our position.
92
for ( scene_element_list::iterator it=sub_e.begin(); it!=sub_e.end(); ++it )
93
it->set_position( it->get_position() + m_box.bottom_left() );
95
e.insert( e.end(), sub_e.begin(), sub_e.end() );
97
// and we draw the borders and the background of the current component
98
visual::rectangle_type box
99
(visual::position_type(0, 0), visual::position_type( width(), height() ));
101
if ( get_border_size() != 0 )
73
scene_element_list sub_e;
75
// display the current component
78
// display the sub components
79
for ( component_list::const_iterator it=m_components.begin();
80
it!=m_components.end();
82
(*it)->render( sub_e );
84
// the scene elements are placed in the current component, so we adjust
85
// thein position according to our position.
86
for ( scene_element_list::iterator it=sub_e.begin(); it!=sub_e.end();
88
it->set_position( it->get_position() + m_box.position );
90
e.insert( e.end(), sub_e.begin(), sub_e.end() );
92
// and we draw the border of the current component
93
std::vector<visual::position_type> p(5, visual::position_type(0, 0));
100
e.push_back( visual::scene_line(left(), top(), m_border_color, p, 1) );
103
std::vector<visual::position_type> line(3);
104
line[0] = box.bottom_left();
105
line[1] = box.bottom_right();
106
line[2] = box.top_right();
108
if ( m_bottom_right_border_color.components.alpha != 0 )
111
(left(), bottom(), m_bottom_right_border_color, line,
112
get_border_size()) );
114
line[1] = box.top_left();
116
if ( m_top_left_border_color.components.alpha != 0 )
119
(left(), bottom(), m_top_left_border_color, line,
120
get_border_size()) );
123
if ( m_background_color.components.alpha != 0 )
125
( visual::scene_rectangle(left(), bottom(), m_background_color, box) );
102
126
} // visual_component::render()
104
128
/*----------------------------------------------------------------------------*/
228
264
/*----------------------------------------------------------------------------*/
266
* \brief Inform the component that a mouse button had been released.
267
* \param key The value of the released button.
268
* \param pos The current position of the cursor.
270
bool bear::gui::visual_component::mouse_released
271
( input::mouse::mouse_code key,
272
const claw::math::coordinate_2d<unsigned int>& pos )
279
if (m_input_priority)
281
result = on_mouse_released(key, pos);
284
result = broadcast_mouse_released(key, pos);
288
result = broadcast_mouse_released(key, pos);
291
result = on_mouse_released(key, pos);
295
} // visual_component::mouse_released()
297
/*----------------------------------------------------------------------------*/
299
* \brief Inform the component that a mouse button had been maintained.
300
* \param key The value of the maintained button.
301
* \param pos The current position of the cursor.
303
bool bear::gui::visual_component::mouse_maintained
304
( input::mouse::mouse_code key,
305
const claw::math::coordinate_2d<unsigned int>& pos )
312
if (m_input_priority)
314
result = on_mouse_maintained(key, pos);
317
result = broadcast_mouse_maintained(key, pos);
321
result = broadcast_mouse_maintained(key, pos);
324
result = on_mouse_maintained(key, pos);
328
} // visual_component::mouse_maintained()
330
/*----------------------------------------------------------------------------*/
230
332
* \brief Inform the component that the mouse has been moved.
231
333
* \param pos The new position of the cursor.
233
335
bool bear::gui::visual_component::mouse_move
234
336
( const claw::math::coordinate_2d<unsigned int>& pos )
238
343
if (m_input_priority)
256
361
/*----------------------------------------------------------------------------*/
363
* \brief Adjust the size of the component to fit its sub components.
364
* \param margin The margin around the children.
366
void bear::gui::visual_component::fit( visual::size_type margin )
368
size_type min_x( std::numeric_limits<size_type>::max() );
369
size_type min_y( std::numeric_limits<size_type>::max() );
370
size_type max_x( std::numeric_limits<size_type>::min() );
371
size_type max_y( std::numeric_limits<size_type>::min() );
373
component_list::const_iterator it;
374
for ( it=m_components.begin(); it!=m_components.end(); ++it )
376
min_x = std::min( min_x, (*it)->left() );
377
min_y = std::min( min_y, (*it)->bottom() );
378
max_x = std::max( max_x, (*it)->right() );
379
max_y = std::max( max_y, (*it)->top() );
382
const size_type delta_x = min_x - margin;
383
const size_type delta_y = min_y - margin;
385
for ( it=m_components.begin(); it!=m_components.end(); ++it )
386
(*it)->set_bottom_left
387
( (*it)->left() - delta_x, (*it)->bottom() - delta_y );
389
set_size( max_x - delta_x + margin, max_y - delta_y + margin );
390
} // visual_component::fit()
392
/*----------------------------------------------------------------------------*/
258
394
* \brief Set the size of the component to its maximum size.
260
396
void bear::gui::visual_component::set_size_maximum()
262
set_size( std::numeric_limits<unsigned int>::max(),
263
std::numeric_limits<unsigned int>::max() );
398
set_size( std::numeric_limits<size_type>::max(),
399
std::numeric_limits<size_type>::max() );
264
400
} // visual_component::set_size()
266
402
/*----------------------------------------------------------------------------*/
280
415
* \param w The new width.
281
416
* \param h The new height.
283
void bear::gui::visual_component::set_size( unsigned int w, unsigned int h )
418
void bear::gui::visual_component::set_size( size_type w, size_type h )
285
const unsigned int old_w = m_box.width;
286
const unsigned int old_h = m_box.height;
420
const size_type old_w = m_box.width();
421
const size_type old_h = m_box.height();
423
m_box.set( left(), bottom(), m_box.left() + w, m_box.bottom() + h );
293
if ( (old_w != m_box.width) || (old_h != m_box.height) )
427
if ( (old_w != m_box.width()) || (old_h != m_box.height()) )
295
429
} // visual_component::set_size()
379
506
/*----------------------------------------------------------------------------*/
508
* \brief Set the left position of the component in its owner.
509
* \param x The new position.
511
void bear::gui::visual_component::set_left( coordinate_type x )
513
set_bottom_left( x, bottom() );
514
} // visual_component::set_left()
516
/*----------------------------------------------------------------------------*/
518
* \brief Set the right position of the component in its owner.
519
* \param x The new position.
521
void bear::gui::visual_component::set_right( coordinate_type x )
523
set_bottom_left( x - width(), bottom() );
524
} // visual_component::set_right()
526
/*----------------------------------------------------------------------------*/
528
* \brief Set the top position of the component in its owner.
529
* \param y The new position.
531
void bear::gui::visual_component::set_top( coordinate_type y )
533
set_bottom_left( left(), y - height() );
534
} // visual_component::set_top()
536
/*----------------------------------------------------------------------------*/
538
* \brief Set the bottom position of the component in its owner.
539
* \param x The new position.
541
void bear::gui::visual_component::set_bottom( coordinate_type y )
543
set_bottom_left( left(), y );
544
} // visual_component::set_bottom()
546
/*----------------------------------------------------------------------------*/
548
* \brief Set the top-left position of the component in its owner.
549
* \param x The new x-position.
550
* \param y The new y-position.
552
void bear::gui::visual_component::set_top_left
553
( coordinate_type x, coordinate_type y )
555
set_bottom_left( x, y - height() );
556
} // visual_component::set_top_left()
558
/*----------------------------------------------------------------------------*/
560
* \brief Set the bottom-left position of the component in its owner.
561
* \param x The new x-position.
562
* \param y The new y-position.
564
void bear::gui::visual_component::set_bottom_left
565
( coordinate_type x, coordinate_type y )
567
const coordinate_type w = m_box.width();
568
const coordinate_type h = m_box.height();
574
if ( (w != m_box.width()) || (h != m_box.height()) )
576
} // visual_component::set_bottom_left()
578
/*----------------------------------------------------------------------------*/
580
* \brief Set the top-right position of the component in its owner.
581
* \param x The new x-position.
582
* \param y The new y-position.
584
void bear::gui::visual_component::set_top_right
585
( coordinate_type x, coordinate_type y )
587
set_bottom_left( x - width(), y - height() );
588
} // visual_component::set_top_right()
590
/*----------------------------------------------------------------------------*/
592
* \brief Set the bottom-right position of the component in its owner.
593
* \param x The new x-position.
594
* \param y The new y-position.
596
void bear::gui::visual_component::set_bottom_right
597
( coordinate_type x, coordinate_type y )
599
set_bottom_left( x - width(), y );
600
} // visual_component::set_bottom_right()
602
/*----------------------------------------------------------------------------*/
381
604
* \brief Get the width of the component.
383
unsigned int bear::gui::visual_component::width() const
606
bear::gui::size_type bear::gui::visual_component::width() const
608
return m_box.width();
386
609
} // visual_component::width()
388
611
/*----------------------------------------------------------------------------*/
390
613
* \brief Get the height of the component.
392
unsigned int bear::gui::visual_component::height() const
615
bear::gui::size_type bear::gui::visual_component::height() const
617
return m_box.height();
395
618
} // visual_component::height()
397
620
/*----------------------------------------------------------------------------*/
399
622
* \brief Get the x-coordinate of the component's left edge.
401
unsigned int bear::gui::visual_component::left() const
624
bear::gui::coordinate_type bear::gui::visual_component::left() const
403
626
return m_box.left();
404
627
} // visual_component::left()
435
658
* \brief Get the coordinates of the component's top left corner.
437
claw::math::coordinate_2d<unsigned int>
438
bear::gui::visual_component::top_left() const
660
bear::gui::position_type bear::gui::visual_component::top_left() const
440
return claw::math::coordinate_2d<unsigned int>( left(), top() );
662
return m_box.top_left();
441
663
} // visual_component::top_left()
443
665
/*----------------------------------------------------------------------------*/
445
667
* \brief Get the coordinates of the component's top right corner.
447
claw::math::coordinate_2d<unsigned int>
448
bear::gui::visual_component::top_right() const
669
bear::gui::position_type bear::gui::visual_component::top_right() const
450
return claw::math::coordinate_2d<unsigned int>( right(), top() );
671
return m_box.top_right();
451
672
} // visual_component::top_right()
453
674
/*----------------------------------------------------------------------------*/
455
676
* \brief Get the coordinates of the component's bottom left corner.
457
claw::math::coordinate_2d<unsigned int>
458
bear::gui::visual_component::bottom_left() const
678
bear::gui::position_type bear::gui::visual_component::bottom_left() const
460
return claw::math::coordinate_2d<unsigned int>( left(), bottom() );
680
return m_box.bottom_left();
461
681
} // visual_component::bottom_left()
463
683
/*----------------------------------------------------------------------------*/
465
685
* \brief Get the coordinates of the component's bottom right corner.
467
claw::math::coordinate_2d<unsigned int>
468
bear::gui::visual_component::bottom_right() const
687
bear::gui::position_type bear::gui::visual_component::bottom_right() const
470
return claw::math::coordinate_2d<unsigned int>( right(), bottom() );
689
return m_box.bottom_right();
471
690
} // visual_component::bottom_right()
473
692
/*----------------------------------------------------------------------------*/
475
694
* \brief Get the size of the component.
477
claw::math::coordinate_2d<unsigned int>
478
bear::gui::visual_component::get_size() const
696
bear::gui::size_box_type bear::gui::visual_component::get_size() const
480
return claw::math::coordinate_2d<unsigned int>( m_box.width, m_box.height );
481
699
} // visual_component::get_size()
483
701
/*----------------------------------------------------------------------------*/
485
703
* \brief Get the position of the component in its owner.
487
const claw::math::coordinate_2d<unsigned int>&
488
bear::gui::visual_component::get_position() const
705
bear::gui::position_type bear::gui::visual_component::get_position() const
490
return m_box.position;
707
return m_box.bottom_left();
491
708
} // visual_component::get_position()
493
710
/*----------------------------------------------------------------------------*/
495
712
* \brief Get the rectangle bounding this component.
497
const claw::math::rectangle<unsigned int>&
714
const bear::gui::rectangle_type&
498
715
bear::gui::visual_component::get_rectangle() const
512
729
/*----------------------------------------------------------------------------*/
731
* \brief Get the size of the borders.
733
bear::gui::size_type bear::gui::visual_component::get_border_size() const
736
} // visual_component::get_border_size()
738
/*----------------------------------------------------------------------------*/
514
740
* \brief Set the color of the border.
515
741
* \param clr The new color.
517
void bear::gui::visual_component::set_border_color
518
( const claw::graphic::rgba_pixel& clr )
743
void bear::gui::visual_component::set_border_color( const color_type& clr )
520
m_border_color = clr;
745
set_top_left_border_color( clr );
746
set_bottom_right_border_color( clr );
521
747
} // visual_component::set_border_color()
523
749
/*----------------------------------------------------------------------------*/
751
* \brief Set the color of the top and left borders.
752
* \param clr The new color.
755
bear::gui::visual_component::set_top_left_border_color( const color_type& clr )
757
m_top_left_border_color = clr;
758
} // visual_component::set_top_left_border_color()
760
/*----------------------------------------------------------------------------*/
762
* \brief Get the color of the top and left borders.
764
const bear:: gui::color_type&
765
bear::gui::visual_component::get_top_left_border_color() const
767
return m_top_left_border_color;
768
} // visual_component::get_top_left_border_color()
770
/*----------------------------------------------------------------------------*/
772
* \brief Set the color of the bottom and right borders.
773
* \param clr The new color.
775
void bear::gui::visual_component::set_bottom_right_border_color
776
( const color_type& clr )
778
m_bottom_right_border_color = clr;
779
} // visual_component::set_bottom_right_border_color()
781
/*----------------------------------------------------------------------------*/
783
* \brief Get the color of the bottom and right borders.
785
const bear::gui::color_type&
786
bear::gui::visual_component::get_bottom_right_border_color() const
788
return m_bottom_right_border_color;
789
} // visual_component::get_bottom_right_border_color()
791
/*----------------------------------------------------------------------------*/
793
* \brief Set the color of the background.
794
* \param clr The new color.
796
void bear::gui::visual_component::set_background_color( const color_type& clr )
798
m_background_color = clr;
799
} // visual_component::set_background_color()
801
/*----------------------------------------------------------------------------*/
803
* \brief Get the color of the background.
804
* \param clr The new color.
806
const bear::gui::color_type&
807
bear::gui::visual_component::get_background_color() const
809
return m_background_color;
810
} // visual_component::get_background_color()
812
/*----------------------------------------------------------------------------*/
814
* \brief Disable this component.
816
void bear::gui::visual_component::disable()
819
} // visual_component::disable()
821
/*----------------------------------------------------------------------------*/
823
* \brief Enable this component.
825
void bear::gui::visual_component::enable()
828
} // visual_component::enable()
830
/*----------------------------------------------------------------------------*/
832
* \brief Tell if this compontent is enabled.
834
bool bear::gui::visual_component::is_enabled() const
837
} // visual_component::is_enabled()
839
/*----------------------------------------------------------------------------*/
841
* \brief Set the focus on this component.
843
void bear::gui::visual_component::set_focus()
845
// all parent controls up to the top component
846
std::list<visual_component*> s;
849
for ( c = this; c!=NULL; c=c->m_owner )
852
std::list<visual_component*>::const_iterator it_parent(s.begin());
853
std::list<visual_component*>::const_iterator it_child(it_parent);
856
for ( ; it_child!=s.end(); ++it_parent, ++it_child )
857
(*it_parent)->set_focus(*it_child);
859
for ( it_parent=s.begin(); it_parent!=s.end(); ++it_parent )
860
(*it_parent)->on_focused();
861
} // visual_component::set_focus()
863
/*----------------------------------------------------------------------------*/
865
* \brief Get the child component having the focus.
867
bear::gui::visual_component* bear::gui::visual_component::get_focus() const
869
if ( m_focused_component < 0 )
872
return m_components[m_focused_component];
873
} // visual_component::get_focus()
875
/*----------------------------------------------------------------------------*/
525
877
* \brief Add a component in this component.
526
878
* \param that The component to add.
639
991
/*----------------------------------------------------------------------------*/
993
* \brief Tell the focused component that a mouse button has been released.
994
* \param key The pressed key.
995
* \param pos The current position of the cursor.
996
* \return true if the key has been processed.
998
bool bear::gui::visual_component::on_mouse_released
999
( input::mouse::mouse_code key,
1000
const claw::math::coordinate_2d<unsigned int>& pos )
1003
} // visual_component::on_mouse_released()
1005
/*----------------------------------------------------------------------------*/
1007
* \brief Tell the focused component that a mouse button has been maintained.
1008
* \param key The pressed key.
1009
* \param pos The current position of the cursor.
1010
* \return true if the key has been processed.
1012
bool bear::gui::visual_component::on_mouse_maintained
1013
( input::mouse::mouse_code key,
1014
const claw::math::coordinate_2d<unsigned int>& pos )
1017
} // visual_component::on_mouse_maintained()
1019
/*----------------------------------------------------------------------------*/
641
1021
* \brief Tell the component that the mouse has been moved.
642
1022
* \param pos The new position of the cursor.
643
1023
* \return true if the button has been processed.
690
1079
/*----------------------------------------------------------------------------*/
1081
* \brief Inform the sub components that a mouse button has been released.
1082
* \param button The pressed key.
1083
* \param pos The current position of the cursor.
1084
* \return true if the key has been processed.
1086
bool bear::gui::visual_component::broadcast_mouse_released
1087
( input::mouse::mouse_code button,
1088
const claw::math::coordinate_2d<unsigned int>& pos )
1091
component_list::iterator it;
1093
for (it=m_components.begin(); !result && (it!=m_components.end()); ++it)
1094
if ( (*it)->m_box.includes(pos) )
1095
result = (*it)->mouse_released( button, pos - (*it)->get_position() );
1098
} // visual_component::broadcast_mouse_released()
1100
/*----------------------------------------------------------------------------*/
1102
* \brief Inform the sub components that a mouse button has been maintained.
1103
* \param button The pressed key.
1104
* \param pos The current position of the cursor.
1105
* \return true if the key has been processed.
1107
bool bear::gui::visual_component::broadcast_mouse_maintained
1108
( input::mouse::mouse_code button,
1109
const claw::math::coordinate_2d<unsigned int>& pos )
1112
component_list::iterator it;
1114
for (it=m_components.begin(); !result && (it!=m_components.end()); ++it)
1115
if ( (*it)->m_box.includes(pos) )
1116
result = (*it)->mouse_maintained( button, pos - (*it)->get_position() );
1119
} // visual_component::broadcast_mouse_maintained()
1121
/*----------------------------------------------------------------------------*/
692
1123
* \brief Adjust the width and the height of the component to stay in the owner.
694
1125
void bear::gui::visual_component::stay_in_owner()
698
if ( m_box.position.x >= m_owner->width() )
699
m_box.position.x = m_owner->width() - 1;
701
if ( m_box.position.y >= m_owner->height() )
702
m_box.position.y = m_owner->height() - 1;
704
if ( m_box.right() >= m_owner->width() )
705
m_box.width = m_owner->width() - m_box.position.x;
707
if ( m_box.bottom() >= m_owner->height() )
708
m_box.height = m_owner->height() - m_box.position.y;
1129
coordinate_type n_left(m_box.left());
1130
coordinate_type n_right(m_box.right());
1131
coordinate_type n_bottom(m_box.bottom());
1132
coordinate_type n_top(m_box.top());
1134
if ( n_left >= m_owner->width() )
1135
n_left = m_owner->width();
1137
if ( n_bottom >= m_owner->height() )
1138
n_bottom = m_owner->height();
1140
if ( n_right >= m_owner->width() )
1141
n_right = m_owner->width();
1143
if ( n_top >= m_owner->height() )
1144
n_top = m_owner->height();
1146
m_box.set( n_left, n_bottom, n_right, n_top );
710
1148
} // visual_component::stay_in_owner()
1150
/*----------------------------------------------------------------------------*/
1152
* \brief Set the focus on a given component.
1153
* \param c The component on which the focus is set.
1155
void bear::gui::visual_component::set_focus( visual_component* c )
1159
m_focused_component = -1;
1161
for ( component_list::const_iterator it=m_components.begin();
1162
!stop && (it != m_components.end()); ++it, ++i )
1165
m_focused_component = i;
1168
} // visual_component::set_focus()