1 // Licensed GNU LGPL v3 or later: http://www.gnu.org/licenses/lgpl.html 2 // 3 #ifndef SPECTMORPH_TOOL_BUTTON_HH 4 #define SPECTMORPH_TOOL_BUTTON_HH 5 6 #include "smdrawutils.hh" 7 8 namespace SpectMorph 9 { 10 11 class ToolButton : public Widget 12 { 13 bool highlight = false; 14 bool pressed = false; 15 char symbol; 16 17 public: 18 Signal<> signal_clicked; 19 20 ToolButton (Widget *parent, char symbol = 0) : 21 Widget (parent), 22 symbol (symbol) 23 { 24 } 25 void 26 draw (const DrawEvent& devent) override 27 { 28 cairo_t *cr = devent.cr; 29 DrawUtils du (cr); 30 31 double space = 4; 32 33 if (highlight) 34 cairo_set_source_rgb (cr, 0.9, 0.9, 0.9); 35 else 36 cairo_set_source_rgb (cr, 0.7, 0.7, 0.7); 37 38 if (symbol == 'v') /* draw triangle */ 39 { 40 cairo_move_to (cr, space, space); 41 cairo_line_to (cr, width() - space, space); 42 cairo_line_to (cr, width() / 2, space + (width() - 2 * space) * 0.8); 43 44 cairo_close_path (cr); 45 cairo_stroke_preserve (cr); 46 cairo_fill (cr); 47 } 48 else if (symbol == '>') 49 { 50 cairo_move_to (cr, space, space); 51 cairo_line_to (cr, space, height() - space); 52 cairo_line_to (cr, space + (height() - 2 * space) * 0.8, height() / 2); 53 54 cairo_close_path (cr); 55 cairo_stroke_preserve (cr); 56 cairo_fill (cr); 57 } 58 else if (symbol == 'x') 59 { 60 cairo_move_to (cr, space, space); 61 cairo_line_to (cr, width() - space, height() - space); 62 63 cairo_move_to (cr, space, height() - space); 64 cairo_line_to (cr, width() - space, space); 65 66 cairo_set_line_width (cr, 2.0); 67 cairo_stroke (cr); 68 } 69 } 70 void 71 enter_event() override 72 { 73 highlight = true; 74 update(); 75 } 76 void 77 mouse_press (const MouseEvent& event) override 78 { 79 if (event.button == LEFT_BUTTON) 80 { 81 pressed = true; 82 update(); 83 } 84 } 85 void 86 mouse_release (const MouseEvent& event) override 87 { 88 if (event.button != LEFT_BUTTON || !pressed) 89 return; 90 91 pressed = false; 92 update(); 93 94 if (event.x >= 0 && event.y >= 0 && event.x < width() && event.y < height()) 95 signal_clicked(); // this must be the last line, as deletion can occur afterwards 96 } 97 void 98 leave_event() override 99 { 100 highlight = false; 101 pressed = false; 102 update(); 103 } 104 void 105 set_symbol (char s) 106 { 107 symbol = s; 108 update(); 109 } 110 }; 111 112 } 113 114 #endif 115