wmcube: imported Upstream version 0.99-pre1
[dockapps.git] / wmcube / wmapp / example1 / window1.cc
blob644cad57a48a63f2f23a8b52e0848a5efdc975a2
1 #include "../wmapp.h"
2 #include "../wmwindow.h"
3 #include "../wmframe.h"
4 #include "../wmcanvas.h"
5 #include "../wmbutton.h"
6 #include "../wmellipse.h"
7 #include "debian-tiny.xpm"
9 // This file creates a window with a small, very simple paint program.
11 // Define a class that will be a WMCanvas widget with callbacks
12 class WMPainter : public WMCallback, public WMCanvas, public WMEllipse {
13 public: WMPainter() : WMCallback(), WMCanvas() { }
16 // Make one of the buttons elliptical.
17 class WMEllipticalButton : public WMEllipse, public WMButton {
18 public: WMEllipticalButton() : WMEllipse(), WMButton() { }
21 // Callback to paint onto this widget when it's clicked on
22 void
23 paint(const WMApp *a, WMWidget *w, void *)
25 WMMouseClick click = a->mouseclick().b_relative_to(w);
26 WMPainter *p = dynamic_cast<WMPainter *>(w);
27 // draw a 3x3 square at the mouse location clicked upon
28 if (p) p->fill_rectangle(click.x - 2, click.y - 2, 3, 3);
29 a->repaint();
32 // Callbacks for the buttons -----------------------------------------------
34 // This callback changes the current paint color of the WMCanvas, depending
35 // upon which button was pressed.
36 void
37 changecolor(const WMApp *a, WMWidget *w, void *color)
39 WMColor::WMColor c = *static_cast<WMColor::WMColor *>(color);
40 WMPainter *wp = dynamic_cast<WMPainter *>(w);
41 if (wp) wp->setcolor(c);
44 // This callback will be attached to the big button
45 // on the second window. It switches between windows.
46 // Note: if the last statement in a callback function requests
47 // a switch to a new window, you don't need a "repaint()".
48 void
49 switch_to_0(const WMApp *a, void *)
50 { a->switch_to(0); }
52 void makewindow1(WMWindow *w1)
54 static WMEllipticalButton debianwin;
55 static WMEllipticalButton colorbutton[4];
56 static WMFrame top, topleft, bottom;
57 static WMPainter drawing;
58 static int colors[4] = { 0x000000, 0xFF0000, 0x00FF00, 0x0000FF };
60 w1->addchild(top);
61 w1->addchild(bottom);
62 w1->setorientation(Orientation::Vertical);
63 w1->setaspectratios(1, 3);
65 top.addchild(topleft);
66 top.addchild(debianwin);
67 top.setaspectratios(4, 1);
69 topleft.setpadding(0);
70 topleft.setborder(1);
71 topleft.settransparency(false);
73 for (unsigned int i = 0; i < 4; i++) {
74 colorbutton[i].setbgcolor(colors[i]);
75 colorbutton[i].addcallback(changecolor, &drawing, colors + i);
76 topleft.addchild(colorbutton[i]);
79 debianwin.seticon(debian_tiny_xpm);
80 debianwin.addcallback(switch_to_0, 0);
82 drawing.addcallback(paint, &drawing);
83 bottom.addchild(drawing);
84 drawing.setborder(4);