master chld/haikuprg / src / Pointer.cpp
  1#include <Application.h>
  2#include <Window.h>
  3#include <View.h>
  4
  5#include <Button.h>
  6#include <Message.h>
  7
  8#include <vector>
  9
 10class Dot:public BView
 11{
 12public: Dot(BRect f):BView(f, "dot", B_FOLLOW_ALL, B_WILL_DRAW){}
 13  void Draw(BRect r)
 14  {
 15    for(size_t s=0; s+1<fP.size(); ++s)
 16      {
 17	if (fP[s] == BPoint(-1, -1) || fP[s+1] == BPoint(-1 ,-1)) continue;
 18	    StrokeLine(fP[s], fP[s+1]);
 19      }
 20  }
 21
 22  void MouseMoved(BPoint w, uint32 c, const BMessage *m)
 23  {
 24    if (draw)
 25      {
 26	fP.push_back(w);
 27	Invalidate();
 28      }
 29  }
 30
 31  void MouseDown(BPoint w)
 32  {
 33    draw=true;
 34  }
 35
 36  void MouseUp(BPoint w)
 37  {
 38    draw=false;
 39
 40    /* prevent up connecting w down */
 41    fP.push_back(BPoint(-1, -1));
 42  }
 43
 44  void ClearAll()
 45  {
 46    fP.clear();
 47    Invalidate();
 48  }
 49
 50private:
 51  std::vector<BPoint> fP;
 52  std::vector<BPoint> sP;
 53  bool draw=false;
 54};
 55
 56class Win:public BWindow
 57{
 58public:Win(BRect f):
 59  BWindow(f, "Drawkun", B_TITLED_WINDOW, B_QUIT_ON_WINDOW_CLOSE){
 60  dot = new Dot(BRect(0, 30, f.Width(), f.Height()));
 61  AddChild(dot);
 62  dot->SetHighColor(0,0,0);
 63  dot->SetPenSize(10);
 64  dot->SetLineMode(B_ROUND_CAP, B_ROUND_JOIN);
 65
 66  BButton *cButton = new BButton(BRect(10, 10, 100, 20), "clear", "Clear", new BMessage('c'));
 67  AddChild(cButton);
 68
 69  BButton *eButton = new BButton(BRect(110, 10, 200, 20), "undo", "Undo", new BMessage('u'));
 70  AddChild(eButton);
 71}
 72  void MessageReceived(BMessage *m)
 73  {
 74    switch(m->what){
 75    case 'c':
 76      dot->ClearAll();
 77      break;
 78    case 'e':
 79      dot->Undo();
 80      break;
 81    default:
 82      BWindow::MessageReceived(m);
 83    }
 84  }
 85
 86private:
 87  Dot *dot;
 88  bool erase=false;
 89};
 90
 91class App:public BApplication
 92{
 93public:App():BApplication("application/x-vnd.example.blank")
 94  {
 95    BRect rect(100,100,640,480);
 96    Win *w = new Win(rect);
 97    w->Show();
 98  }
 99};
100
101int main(void)
102{
103  App a;
104  a.Run();
105  return 0;
106}