1 //
2 // "$Id$"
3 //
4 // shapedwindow example source file for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2014 by Bill Spitzak and others.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file. If this
10 // file is missing or damaged, see the license at:
11 //
12 // http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems to:
15 //
16 // http://www.fltk.org/str.php
17 //
18
19 #include <FL/Fl_Window.H>
20 #include <FL/Fl_Button.H>
21 #include <FL/Fl_Box.H>
22 #include <FL/Fl.H>
23 #include <FL/fl_draw.H>
24 #include <FL/Fl_Double_Window.H>
25 #include <FL/Fl_Image.H>
26 #include <FL/Fl_Tiled_Image.H>
27 #include <FL/Fl_Image_Surface.H>
28 #include "test/pixmaps/tile.xpm"
29
30
cb(Fl_Widget * w,void *)31 void cb(Fl_Widget *w, void *) {
32 w->window()->hide();
33 }
34
35 class dragbox : public Fl_Box {
36 public:
dragbox(int x,int y,int w,int h,const char * t=0)37 dragbox(int x, int y, int w, int h, const char *t=0) : Fl_Box(x,y,w,h,t) {};
handle(int event)38 int handle(int event) {
39 static int fromx, fromy, winx, winy;
40 if (event == FL_PUSH) {
41 fromx = Fl::event_x_root();
42 fromy = Fl::event_y_root();
43 winx = window()->x_root();
44 winy = window()->y_root();
45 return 1;
46 }
47 else if (event == FL_DRAG) {
48 int deltax = Fl::event_x_root() - fromx;
49 int deltay = Fl::event_y_root() - fromy;
50 window()->position(winx + deltax, winy + deltay);
51 return 1;
52 }
53 return Fl_Box::handle(event);
54 }
55 };
56
57 const float factor = 1.3;
58
shrink(Fl_Widget * wdgt,void * data)59 void shrink(Fl_Widget *wdgt, void *data)
60 {
61 Fl_Window *win = wdgt->window();
62 int old = win->w();
63 win->size(old/factor, old/factor);
64 if (win->w() <= *(int*)data) wdgt->deactivate();
65 }
66
enlarge(Fl_Widget * wdgt,void * data)67 void enlarge(Fl_Widget *wdgt, void *data)
68 {
69 Fl_Window *win = wdgt->window();
70 int old = win->w();
71 win->size(old*factor, old*factor);
72 ((Fl_Widget*)data)->activate();
73 }
74
prepare_shape(int w)75 Fl_RGB_Image* prepare_shape(int w)
76 {
77 // draw a white circle with a hole in it on black background
78 Fl_Image_Surface *surf = new Fl_Image_Surface(w, w);
79 Fl_Surface_Device* current = Fl_Surface_Device::surface();
80 surf->set_current();
81 fl_color(FL_BLACK);
82 fl_rectf(-1, -1, w+2, w+2);
83 fl_color(FL_WHITE);
84 fl_pie(2,2,w-4,w-4,0,360);
85 fl_color(FL_BLACK);
86 fl_pie(0.7*w,w/2,w/4,w/4,0,360);
87 Fl_RGB_Image* img = surf->image();
88 delete surf;
89 current->set_current();
90 return img; // return white image on black background
91 }
92
main(int argc,char ** argv)93 int main(int argc, char **argv) {
94 int dim = 200;
95 Fl_Double_Window *win = new Fl_Double_Window(100, 100, dim, dim, "Testing1");
96 Fl_RGB_Image *img = prepare_shape(dim);
97 win->shape(img);
98 dragbox *box = new dragbox(0, 0, win->w(), win->h());
99 box->image(new Fl_Tiled_Image(new Fl_Pixmap((const char * const *)tile_xpm)));
100 Fl_Group *g = new Fl_Group(10, 20, 80, 20);
101 g->box(FL_NO_BOX);
102 Fl_Button *b = new Fl_Button(10, 20, 80, 20, "Close");
103 b->callback(cb);
104 g->end();
105 g->resizable(NULL);
106 g = new Fl_Group(60, 70, 80, 40, "Drag me");
107 g->box(FL_NO_BOX);
108 g->align(FL_ALIGN_TOP);
109 Fl_Button *bs = new Fl_Button(60, 70, 80, 20, "Shrink");
110 bs->callback(shrink, &dim);
111 bs->deactivate();
112 Fl_Button *be = new Fl_Button(60, 90, 80, 20, "Enlarge");
113 be->callback(enlarge, bs);
114 g->end();
115 g->resizable(NULL);
116 win->end();
117 win->resizable(win);
118 win->show(argc, argv);
119 Fl::run();
120 delete win;
121 return 0;
122 }
123
124 //
125 // End of "$Id$".
126 //
127