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
|
// $Id: Flu_Combo_Box.cxx 672 2007-09-02 15:47:45Z Larry $
/***************************************************************
* FLU - FLTK Utility Widgets
* Copyright (C) 2002 Ohio Supercomputer Center, Ohio State University
*
* This file and its content is protected by a software license.
* You should have received a copy of this license with this file.
* If not, please contact the Ohio Supercomputer Center immediately:
* Attn: Jason Bryan Re: FLU 1224 Kinnear Rd, Columbus, Ohio 43212
*
***************************************************************/
#include <stdio.h>
#include <FL/Fl.H>
#include <FL/fl_draw.H>
#include <string.h>
#include <stdlib.h>
#include <FL/math.h>
#include "Flu_Combo_Box.h"
Flu_Combo_Box :: Flu_Combo_Box( int X, int Y, int W, int H, const char* l )
: Fl_Group( X, Y, W, H, l ), input( X, Y, W, H )
{
box( FL_DOWN_BOX );
align( FL_ALIGN_LEFT );
pop_height( 100 );
_cbox = NULL;
_valbox = FL_UP_BOX;
input_callback( NULL );
input.box( FL_FLAT_BOX );
input.callback( input_cb, this );
input.when( FL_WHEN_ENTER_KEY_ALWAYS );
input.color( FL_WHITE, selection_color());
input.textfont( FL_HELVETICA );
input.textsize( FL_NORMAL_SIZE );
input.textcolor( FL_FOREGROUND_COLOR );
input.resize( X+Fl::box_dx(box()), Y+Fl::box_dy(box()),
W-18-Fl::box_dw(box()), H-Fl::box_dh(box()) );
editable( true );
end();
}
Flu_Combo_Box::~Flu_Combo_Box()
{
}
void Flu_Combo_Box :: set_combo_widget( Fl_Widget *w )
{
_cbox = w;
this->add( w );
}
void Flu_Combo_Box :: input_cb( Fl_Widget*, void* v )
{
// taken from Fl_Counter.cxx
Flu_Combo_Box& t = *(Flu_Combo_Box*)v;
if( strcmp( t.input.value(), t.value() )!=0 || t.input.when() & FL_WHEN_NOT_CHANGED)
{
if( t.when() )
{
t.clear_changed();
if( t._inputCB )
t._inputCB( &t, t._inputCBD );
else
t.do_callback();
}
else
{
t.set_changed();
}
}
}
void Flu_Combo_Box :: resize( int X, int Y, int W, int H )
{
Fl_Group::resize( X, Y, W, H );
input.resize( X+Fl::box_dx(box()), Y+Fl::box_dy(box()),
W-18-Fl::box_dw(box()), H-Fl::box_dh(box()) );
}
void Flu_Combo_Box :: draw()
{
int W = 18, H = h()-4;
int X = x()+w()-W-2, Y = y()+2;
fl_draw_box( box(), x(), y(), w(), h(), color() );
// draw the arrow button
fl_draw_box( (Fl_Boxtype)_valbox, X, Y, W, H, color() );
fl_color( active_r() ? FL_FOREGROUND_COLOR : fl_inactive(FL_FOREGROUND_COLOR) );
fl_polygon( X+W/2-5, Y+H/2-2, X+W/2+3, Y+H/2-2, X+W/2-1, Y+H/2+2 );
draw_child( input );
if( Fl::focus() == this )
draw_focus( FL_NO_BOX, input.x(), input.y(), input.w(), input.h() );
}
int global_x( Fl_Widget *w )
{
int x = Fl::x()+w->x();
Fl_Widget *o = w->parent();
while( o )
{
if( o->type() >= FL_WINDOW ) x += o->x();
o = o->parent();
}
return x;
}
int global_y( Fl_Widget *w )
{
int y = Fl::y()+w->y();
Fl_Widget *o = w->parent();
while( o )
{
if( o->type() >= FL_WINDOW ) y += o->y();
o = o->parent();
}
return y;
}
Flu_Combo_Box::Popup :: Popup( Flu_Combo_Box *b, Fl_Widget *c, int H )
: Fl_Double_Window( global_x(b)-2, //Fl::x()+b->window()->x()+b->x()-2,
global_y(b)+b->h()-2, //Fl::y()+b->window()->y()+b->y()+b->h()-2,
b->w()+4, H, 0 )
{
combo = b;
dragging = false;
selected = NULL;
box( FL_BORDER_FRAME );
border( 0 );
add( c );
end();
//set_non_modal();
set_modal();
c->resize( 1, 1, w()-2, h()-2 );
}
Flu_Combo_Box::Popup :: ~Popup()
{
while( children() )
remove( child(0) );
}
void Flu_Combo_Box :: value( const char *v )
{
if( _value( v ) )
input.value( v );
}
void Flu_Combo_Box :: selected( const char *v )
{
if( v )
input.value( v );
_popped = false;
do_callback();
}
int Flu_Combo_Box::Popup :: handle( int event )
{
if( event == FL_MOVE )
{
// FL_MOVE is also generated while the window is moving
// this attempts to keep the popup window moving with the enclosing window
//position( combo->window()->x()+combo->x()-2, combo->window()->y()+combo->y()+combo->h()-2 );
position( global_x(combo)-2, global_y(combo)+combo->h()-2 );
// this lets the mouse move event also move the selected item
combo->_hilight( Fl::event_x(), Fl::event_y() );
}
if( event == FL_DRAG )
dragging = true;
// if push outside the popup window, popdown
if( event == FL_PUSH &&
!Fl::event_inside( child(0) ) )
{
combo->_popped = false;
return 0;
}
// if release after dragging outside the popup window, popdown
if( event == FL_RELEASE && dragging &&
!Fl::event_inside( child(0) ) )
{
combo->_popped = false;
return 0;
}
if( event == FL_KEYDOWN )
{
if( Fl::event_key( FL_Escape ) )
{
combo->_popped = false;
return 0;
}
else if( Fl::event_key( FL_Up ) )
{
const char *s = combo->_previous();
if( s )
selected = s;
return 1;
}
else if( Fl::event_key( FL_Down ) )
{
const char *s = combo->_next();
if( s )
selected = s;
return 1;
}
else if( Fl::event_key( FL_Enter ) || Fl::event_key( ' ' ) )
{
if( selected )
{
combo->value( selected );
combo->selected( selected );
}
combo->_popped = false;
return 1;
}
}
return Fl_Double_Window::handle( event );
}
int Flu_Combo_Box :: handle( int event )
{
if( event == FL_KEYDOWN && Fl::event_key( FL_Tab ) )
return Fl_Group::handle( event );
// is it time to popup?
bool open = ( event == FL_PUSH ) &&
(!Fl::event_inside( &input ) || ( !editable() && Fl::event_inside( &input ) ) );
open |= ( event == FL_KEYDOWN ) && Fl::event_key( ' ' );
if( open )
{
fl_cursor( FL_CURSOR_DEFAULT );
_valbox = FL_THIN_DOWN_BOX;
redraw();
// remember old current group
Fl_Group *c = Fl_Group::current();
// set current group to 0 so this is a top level popup window
Fl_Group::current( 0 );
Popup *_popup = new Popup( this, _cbox, popHeight );
// show it and make FLTK send all events there
value( value() );
_popup->show();
Fl::grab( *_popup );
Fl::focus( _cbox );
_popped = true;
Fl::pushed( _cbox );
// wait for a selection to be made
while( _popped )
Fl::wait();
// restore things and delete the popup
_popup->hide();
Fl::grab( 0 );
delete _popup;
Fl_Group::current( c );
Fl::focus( this );
_valbox = FL_UP_BOX;
redraw();
return 1;
}
if( input.handle(event) )
{
if( !editable() && ( event == FL_ENTER || event == FL_LEAVE ) )
fl_cursor( FL_CURSOR_DEFAULT );
return 1;
}
else
return 0;
}
|