Back to index

texmacs  1.0.7.15
toggle_widget.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : toggle_widget.cpp
00004 * DESCRIPTION: Toggles
00005 * COPYRIGHT  : (C) 2012  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "renderer.hpp"
00013 #include "Widkit/layout.hpp"
00014 #include "Widkit/basic_widget.hpp"
00015 #include "scheme.hpp"
00016 
00017 /******************************************************************************
00018 * Command buttons
00019 ******************************************************************************/
00020 
00021 class toggle_widget_rep: public basic_widget_rep {
00022   command cmd;
00023   bool on;
00024   int style;
00025   SI sz;
00026   SI dy;
00027 public:
00028   toggle_widget_rep (command cmd, bool on, int style);
00029   operator tree () { return tree ("toggle_widget"); }
00030   void handle_get_size (get_size_event ev);
00031   void handle_repaint (repaint_event ev);
00032   void handle_mouse (mouse_event ev);
00033 };
00034 
00035 toggle_widget_rep::toggle_widget_rep (command cmd2, bool on2, int style2):
00036   basic_widget_rep (south_west),
00037   cmd (cmd2), on (on2), style (style2), dy (3*PIXEL)
00038 {
00039   bool mini= (style & WIDGET_STYLE_MINI) != 0;
00040   if (mini) sz= 12*PIXEL;
00041   else sz= 15*PIXEL;
00042 }
00043 
00044 void
00045 toggle_widget_rep::handle_get_size (get_size_event ev) {
00046   ev->w= sz;
00047   ev->h= sz + dy;
00048 }
00049 
00050 void
00051 toggle_widget_rep::handle_repaint (repaint_event ev) { (void) ev;
00052   renderer ren= win->get_renderer ();
00053   layout_default (ren, 0, 0, w, h);
00054   layout_pastel_lower (ren, 0, dy, sz, dy + sz);
00055   if (on) {
00056     color dark= layout_dark (ren);
00057     ren->set_color (dark);
00058     ren->line (3*PIXEL, dy + 3*PIXEL, sz - 4*PIXEL, dy + sz - 4*PIXEL);
00059     ren->line (3*PIXEL, dy + sz - 4*PIXEL, sz - 4*PIXEL, dy + 3*PIXEL);
00060   }
00061 }
00062 
00063 void
00064 toggle_widget_rep::handle_mouse (mouse_event ev) {
00065   string type= ev->type;
00066   SI     x= ev->x, y= ev->y;
00067   bool   inside= (y>=0) && (y<h) && (x>=0) && (x<w);
00068   bool   pressed= (type == "release-left" || type == "release-right");
00069   if (inside && pressed) {
00070     on= !on;
00071     if (attached ()) this << emit_invalidate_all ();
00072     if (!is_nil (cmd)) cmd (list_object (object (on)));
00073   }
00074 }
00075 
00076 /******************************************************************************
00077 * Interface
00078 ******************************************************************************/
00079 
00080 wk_widget
00081 toggle_wk_widget (command cmd, bool on, int style) {
00082   return tm_new<toggle_widget_rep> (cmd, on, style);
00083 }