📄 counter.cpp
字号:
#include <FL/Fl.H>#include "Counter.h"#include <FL/fl_draw.H>void Counter::draw() { int i; Fl_Boxtype boxtype[3]; //Fl_Color selcolor; boxtype[0] = box(); if (boxtype[0] == FL_UP_BOX) boxtype[0] = FL_DOWN_BOX; for (i=1; i<3; i++) if (mouseobj == i) boxtype[i] = down(box()); else boxtype[i] = box(); int xx[3], yy[3], ww[3], hh[3]; xx[1] = x()+75*w()/100;yy[1]=y(); ww[1]=25*w()/100 ;hh[1]=50*h()/100; xx[0] = x();yy[0]=y(); ww[0] = 75*w()/100;hh[0]=h(); xx[2] = x()+75*w()/100;yy[2]=y()+50*h()/100;ww[2]=25*w()/100;hh[2]=50*h()/100; draw_box(boxtype[0], xx[0], yy[0], ww[0], hh[0], FL_WHITE); fl_font(textfont(), textsize()); fl_color(active_r() ? textcolor() : inactive(textcolor())); char str[128]; format(str); fl_draw(str, xx[0], yy[0], ww[0], hh[0], FL_ALIGN_CENTER); if (!(damage()&FL_DAMAGE_ALL)) return; // only need to redraw text draw_box(boxtype[1], xx[1], yy[1], ww[1], hh[1], color()); fl_color(active_r() ? textcolor() : inactive(textcolor())); fl_draw("+", xx[1], yy[1], ww[1], hh[1], FL_ALIGN_CENTER); draw_box(boxtype[2], xx[2], yy[2], ww[2], hh[2], color()); fl_color(active_r() ? textcolor() : inactive(textcolor())); fl_draw("-", xx[2], yy[2], ww[2], hh[2], FL_ALIGN_CENTER);}void Counter::increment_cb() { if (!mouseobj) return; double v = value(); switch (mouseobj) { case 1: v += lstep_; break; case 2: v -= lstep_; break; } handle_drag(clamp(round(v)));}#define INITIALREPEAT .5#define REPEAT .1void Counter::repeat_callback(void* v) { Counter* b = (Counter*)v; if (b->mouseobj) { Fl::add_timeout(REPEAT, repeat_callback, b); b->increment_cb(); }}int Counter::calc_mouseobj() { double xx[3];double yy[3];double ww[3];double hh[3]; xx[1] = x()+75*w()/100;yy[1]=y(); ww[1]=25*w()/100 ;hh[1]=50*h()/100; xx[0] = x();yy[0]=y(); ww[0] = 75*w()/100;hh[0]=h(); xx[2] = x()+75*w()/100;yy[2]=y()+50*h()/100;ww[2]=25*w()/100;hh[2]=50*h()/100; if (Fl::event_inside((int)xx[1],(int)yy[1],(int)ww[1],(int)hh[1])) return 1; if (Fl::event_inside((int)xx[2],(int)yy[2],(int)ww[2],(int)hh[2])) return 2; return -1;}int Counter::handle(int event) { int i; switch (event) { case FL_RELEASE: if (mouseobj) { Fl::remove_timeout(repeat_callback, this); mouseobj = 0; redraw(); } handle_release(); return 1; case FL_PUSH: handle_push(); case FL_DRAG: i = calc_mouseobj(); if (i != mouseobj) { Fl::remove_timeout(repeat_callback, this); mouseobj = i; if (i) Fl::add_timeout(INITIALREPEAT, repeat_callback, this); increment_cb(); redraw(); } return 1; default: return 0; }}Counter::~Counter() { Fl::remove_timeout(repeat_callback, this);}Counter::Counter(int x, int y, int w, int h, const char* l) : Fl_Valuator(x, y, w, h, l) { box(FL_UP_BOX); selection_color(FL_INACTIVE_COLOR); // was FL_BLUE align(FL_ALIGN_BOTTOM); bounds(0, 100); Fl_Valuator::step(1, 1); lstep_ = 1; mouseobj = 0; textfont_ = FL_HELVETICA; textsize_ = FL_NORMAL_SIZE; textcolor_ = FL_BLACK;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -