remove whitespace at eol
[goodguy/history.git] / cinelerra-5.1 / guicast / bcdragwindow.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "bcdragwindow.h"
23 #include "bcpixmap.h"
24
25 #include "vframe.h"
26 #include <unistd.h>
27
28 // Icon has to be offset so the cursor isn't directly over it.
29 // The cursor has to be over the target window for X to detect the right window.
30 #define DRAG_OFFSET_X 16
31 #define DRAG_OFFSET_Y 16
32
33 BC_DragWindow::BC_DragWindow(BC_WindowBase *parent_window,
34         BC_Pixmap *pixmap /*,
35         int icon_x,
36         int icon_y */)
37  : BC_Popup(parent_window,
38 //      icon_x,
39 //      icon_y,
40         parent_window->get_abs_cursor_x(0) + DRAG_OFFSET_X,
41         parent_window->get_abs_cursor_y(0) + DRAG_OFFSET_Y,
42         pixmap->get_w(),
43         pixmap->get_h(),
44         -1,
45         0,
46         pixmap)
47 {
48         temp_frame = 0;
49         drag_pixmap = 0;
50 //      init_x = icon_x;
51 //      init_y = icon_y;
52         init_x = parent_window->get_abs_cursor_x(0) + DRAG_OFFSET_X;
53         init_y = parent_window->get_abs_cursor_y(0) + DRAG_OFFSET_Y;
54         end_x = BC_INFINITY;
55         end_y = BC_INFINITY;
56         icon_offset_x = init_x - parent_window->get_abs_cursor_x(0);
57         icon_offset_y = init_y - parent_window->get_abs_cursor_y(0);
58 //printf("BC_DragWindow::BC_DragWindow 1 %d %d\n", icon_offset_x, icon_offset_y);
59         do_animation = 1;
60 }
61
62
63 BC_DragWindow::BC_DragWindow(BC_WindowBase *parent_window,
64         VFrame *frame /*,
65         int icon_x,
66         int icon_y */)
67  : BC_Popup(parent_window,
68 //      icon_x,
69 //      icon_y,
70         parent_window->get_abs_cursor_x(0) + DRAG_OFFSET_X,
71         parent_window->get_abs_cursor_y(0) + DRAG_OFFSET_Y,
72         frame->get_w(),
73         frame->get_h(),
74         -1,
75         0,
76         prepare_frame(frame, parent_window))
77 {
78         delete temp_frame;  // created in prepare_frame inside constructor
79         temp_frame = 0;
80 //      init_x = icon_x;
81 //      init_y = icon_y;
82         init_x = parent_window->get_abs_cursor_x(0) + DRAG_OFFSET_X;
83         init_y = parent_window->get_abs_cursor_y(0) + DRAG_OFFSET_Y;
84         end_x = BC_INFINITY;
85         end_y = BC_INFINITY;
86         icon_offset_x = init_x - parent_window->get_abs_cursor_x(0);
87         icon_offset_y = init_y - parent_window->get_abs_cursor_y(0);
88 //printf("BC_DragWindow::BC_DragWindow 1 %d %d\n", icon_offset_x, icon_offset_y);
89         do_animation = 1;
90 }
91
92 BC_DragWindow::~BC_DragWindow()
93 {
94         delete drag_pixmap;
95 }
96
97 int BC_DragWindow::get_init_x(BC_WindowBase *parent_window, int icon_x)
98 {
99         int output_x, temp = 0;
100         Window tempwin;
101         XTranslateCoordinates(parent_window->top_level->display,
102                 parent_window->win,
103                 parent_window->top_level->rootwin,
104                 icon_x,
105                 temp,
106                 &output_x,
107                 &temp,
108                 &tempwin);
109         return output_x;
110 }
111
112 int BC_DragWindow::get_init_y(BC_WindowBase *parent_window, int icon_y)
113 {
114         int output_y, temp = 0;
115         Window tempwin;
116         XTranslateCoordinates(parent_window->top_level->display,
117                 parent_window->win,
118                 parent_window->top_level->rootwin,
119                 temp,
120                 icon_y,
121                 &temp,
122                 &output_y,
123                 &tempwin);
124         return output_y;
125 }
126
127 int BC_DragWindow::cursor_motion_event()
128 {
129         reposition_window(get_abs_cursor_x(0) + icon_offset_x,
130                 get_abs_cursor_y(0) + icon_offset_y,
131                 get_w(),
132                 get_h());
133         flush();
134         return 1;
135 }
136
137 int BC_DragWindow::get_offset_x()
138 {
139         return icon_offset_x;
140 }
141
142 int BC_DragWindow::get_offset_y()
143 {
144         return icon_offset_y;
145 }
146
147 int BC_DragWindow::drag_failure_event()
148 {
149         if(!do_animation) return 0;
150
151         if(end_x == BC_INFINITY)
152         {
153                 end_x = get_x();
154                 end_y = get_y();
155         }
156
157         for(int i = 0; i < 10; i++)
158         {
159                 int new_x = end_x + (init_x - end_x) * i / 10;
160                 int new_y = end_y + (init_y - end_y) * i / 10;
161
162                 reposition_window(new_x,
163                         new_y,
164                         get_w(),
165                         get_h());
166                 flush();
167                 usleep(1000);
168         }
169         return 0;
170 }
171
172 void BC_DragWindow::set_animation(int value)
173 {
174         this->do_animation = value;
175 }
176
177 BC_Pixmap *BC_DragWindow::prepare_frame(VFrame *frame, BC_WindowBase *parent_window)
178 {
179         temp_frame = 0;
180
181         if(frame->get_color_model() == BC_RGBA8888)
182         {
183                 temp_frame = new VFrame(*frame);
184         }
185         else
186         {
187                 temp_frame = new VFrame;
188                 temp_frame->set_use_shm(0);
189                 temp_frame->reallocate(0,
190                                         -1,
191                                         0,
192                                         0,
193                                         0,
194                                         frame->get_w(),
195                                         frame->get_h(),
196                                         BC_RGBA8888,
197                                         -1);
198
199                 BC_CModels::transfer(temp_frame->get_rows(), frame->get_rows(),
200                         0, 0, 0, 0, 0, 0,
201                         0, 0, frame->get_w(), frame->get_h(),
202                         0, 0, temp_frame->get_w(), temp_frame->get_h(),
203                         frame->get_color_model(), temp_frame->get_color_model(),
204                         0, frame->get_w(), temp_frame->get_w());
205         }
206         temp_frame->get_rows()[(temp_frame->get_h() / 2)][(temp_frame->get_w() / 2) * 4 + 3] = 0;
207         drag_pixmap = new BC_Pixmap(parent_window,
208                         temp_frame,
209                         PIXMAP_ALPHA);
210
211         return drag_pixmap;
212 }
213
214