Created
October 24, 2009 16:01
-
-
Save dchiji/217600 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// name: curve.cpp | |
// compile: $ g++ curve.cpp `pkg-config gtkmm-2.4 --cflags --libs` | |
#include <cairomm/context.h> | |
#include <gtkmm/drawingarea.h> | |
#include <gtkmm/main.h> | |
#include <gtkmm/window.h> | |
#include <gtkmm.h> | |
#include <iostream> | |
class MyArea : public Gtk::DrawingArea | |
{ | |
public: | |
MyArea(); | |
virtual ~MyArea(); | |
protected: | |
virtual bool on_expose_event(GdkEventExpose* event); | |
virtual bool on_button_press_event(GdkEventButton *event); | |
void draw(Cairo::RefPtr<Cairo::Context> cr); | |
void draw_curve(Cairo::RefPtr<Cairo::Context> cr); | |
void draw_shaft(Cairo::RefPtr<Cairo::Context> cr); | |
private: | |
int width; | |
int height; | |
double x0, y0, // start point | |
x1, y1, // control point #1 | |
x2, y2, // control point #2 | |
x3, y3; // end point | |
signed int state; | |
double variation; | |
}; | |
MyArea::MyArea() | |
{ | |
add_events( Gdk::BUTTON_PRESS_MASK | Gdk::POINTER_MOTION_MASK ); | |
width = 0; | |
height = 0; | |
x0=0.1; y0=0.5; | |
x1=0.4; y1=0.9; | |
x2=0.6; y2=0.1; | |
x3=0.9; y3=0.5; | |
state = -1; | |
} | |
MyArea::~MyArea() | |
{ | |
} | |
bool MyArea::on_button_press_event(GdkEventButton* event) | |
{ | |
Glib::RefPtr<Gdk::Window> window = get_window(); | |
if(window) | |
{ | |
state = 0; | |
variation = 0.005; | |
Gtk::Allocation allocation = get_allocation(); | |
Gdk::Rectangle r(0, 0, allocation.get_width(), allocation.get_height()); | |
window->invalidate_rect(r, false); | |
} | |
return true; | |
} | |
bool MyArea::on_expose_event(GdkEventExpose* event) | |
{ | |
Gtk::Allocation allocation = get_allocation(); | |
if(-1 < state) { | |
x1 += variation; y1 += variation; x2 -= variation; y2 -= variation; | |
if(7 < state) { | |
state = -1; | |
} else { | |
state++; | |
variation += 0.001; | |
} | |
Gdk::Rectangle r(0, 0, allocation.get_width(), allocation.get_height()); | |
get_window()->invalidate_rect(r, false); | |
} | |
Glib::RefPtr<Gdk::Window> window = get_window(); | |
if(window) | |
{ | |
Gtk::Allocation allocation = get_allocation(); | |
width = allocation.get_width(); | |
height = allocation.get_height(); | |
Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context(); | |
cr->rectangle(event->area.x, event->area.y, event->area.width, event->area.height); | |
cr->clip(); | |
cr->set_source_rgb(0, 0, 0); | |
cr->paint(); | |
cr->set_source_rgba(1, 1, 1, 0.9); | |
draw(cr); | |
} | |
return true; | |
} | |
void MyArea::draw(Cairo::RefPtr<Cairo::Context> cr) | |
{ | |
cr->set_line_width(0.05); | |
cr->set_source_rgba(1, 1, 1, 0.9); | |
cr->scale(width, height); | |
draw_curve(cr); | |
draw_shaft(cr); | |
} | |
void MyArea::draw_curve(Cairo::RefPtr<Cairo::Context> cr) | |
{ | |
cr->move_to(x0, y0); | |
cr->curve_to(x1, y1, x2, y2, x3, y3); | |
cr->stroke(); | |
} | |
void MyArea::draw_shaft(Cairo::RefPtr<Cairo::Context> cr) | |
{ | |
cr->set_source_rgba(1, 0.2, 0.2, 0.6); | |
cr->move_to(x0, y0); | |
cr->line_to (x1, y1); | |
cr->move_to(x2, y2); | |
cr->line_to (x3, y3); | |
cr->stroke(); | |
} | |
int main(int argc, char** argv) | |
{ | |
Gtk::Main kit(argc, argv); | |
Gtk::Window win; | |
win.set_title("DrawingArea"); | |
MyArea area; | |
win.add(area); | |
area.show(); | |
Gtk::Main::run(win); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment