// // "$Id: SpringWindow.cxx 538 2008-09-07 18:19:38Z mike $" // // Spring-loaded window implementation for FLTK. // // Copyright 2006 by Michael R Sweet. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU Library General Public License as // published by the Free Software Foundation; either version 2, or (at // your option) any later version. // // 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 Library General Public License for more details. // // Contents: // // SpringWindow::SpringWindow() - Create a spring-loaded window. // SpringWindow::~SpringWindow() - Destroy a spring-loaded window. // SpringWindow::resize() - Resize/reposition the window via animation. // SpringWindow::resize_cb() - Animate a resize. // // // Include necessary headers... // #include #include "SpringWindow.h" // // 'SpringWindow::SpringWindow()' - Create a spring-loaded window. // SpringWindow::SpringWindow( int X, // I - X position int Y, // I - Y position int W, // I - Width int H, // I - Height const char *L) // I - Window title : Fl_Double_Window(X, Y, W, H, L) { target_w_ = 0; target_h_ = 0; in_resize_cb_ = false; } SpringWindow::SpringWindow( int W, // I - Width int H, // I - Height const char *L) // I - Window title : Fl_Double_Window(W, H, L) { target_w_ = 0; target_h_ = 0; in_resize_cb_ = false; } // // 'SpringWindow::~SpringWindow()' - Destroy a spring-loaded window. // SpringWindow::~SpringWindow() { Fl::remove_timeout(resize_cb, this); } // // 'SpringWindow::resize()' - Resize/reposition the window via animation. // void SpringWindow::resize(int X, // I - X position int Y, // I - Y position int W, // I - Width int H) // I - Height { if (!in_resize_cb_ && shown() && visible() && X == x() && Y == y() && (W != w() || H != h())) { target_w_ = W; target_h_ = H; Fl::remove_timeout(resize_cb, this); Fl::add_timeout(0.05, resize_cb, this); } else Fl_Double_Window::resize(X, Y, W, H); } // // 'SpringWindow::resize_cb()' - Animate a resize. // void SpringWindow::resize_cb(void *data) // I - Spring-loaded window { SpringWindow *sw; // Spring-loaded window int dW, dH; // Change in size int X, Y, W, H; // New size and position sw = (SpringWindow *)data; dW = sw->target_w_ - sw->w(); dH = sw->target_h_ - sw->h(); if (dW < -1 || dW > 1) dW /= 2; if (dH < -1 || dH > 1) dH /= 2; if (dW || dH) { X = sw->x() - dW / 2; #ifdef OFFSET_Y Y = sw->y() - dH / 2; #else Y = sw->y(); #endif // OFFSET_Y W = sw->w() + dW; H = sw->h() + dH; if (X < 0) X = 0; else if ((X + W) > Fl::w()) X = Fl::w() - W; #ifdef OFFSET_Y if (Y < 0) Y = 0; else if ((Y + H) > Fl::h()) Y = Fl::h() - H; #endif // OFFSET_Y sw->in_resize_cb_ = true; sw->size_range(W, H, W, H); ((Fl_Double_Window *)sw)->resize(X, Y, W, H); sw->in_resize_cb_ = false; if (W != sw->target_w_ || H != sw->target_h_) Fl::add_timeout(0.05, resize_cb, data); } } //// Test code #ifdef TEST # include # include void button_cb(Fl_Widget *wi, // I - Button widget void *d) // I - Data (unused) { SpringWindow *sw = (SpringWindow *)wi->window(); sw->size(wi->w() * 5, wi->h() * 5); } int // O - Exit status main(int argc, // I - Number of command-line args char *argv[]) // I - Command-line args { SpringWindow win(200, 200, "SpringWindow"); Fl_Button b1(10, 10, 40, 40); b1.callback(button_cb); Fl_Button b2(100, 10, 80, 40); b2.callback(button_cb); Fl_Button b3(10, 100, 40, 80); b3.callback(button_cb); Fl_Button b4(100, 100, 80, 80); b4.callback(button_cb); Fl_Box box(190, 190, 10, 10); win.resizable(&box); win.end(); win.show(argc, argv); return (Fl::run()); } #endif // TEST // // End of "$Id: SpringWindow.cxx 538 2008-09-07 18:19:38Z mike $". //