no longer need ffmpeg patch0 which was for Termux
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / bcwindowdraw.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2014 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 "bcbitmap.h"
23 #include "bcpixmap.h"
24 #include "bcpopup.h"
25 #include "bcresources.h"
26 #include "bcsignals.h"
27 #include "bcsynchronous.h"
28 #include "bctimer.h"
29 #include "bcwindowbase.h"
30 #include "clip.h"
31 #include "bccolors.h"
32 #include "bctrace.h"
33 #include "cursors.h"
34 #include "fonts.h"
35 #include "vframe.h"
36 #include <string.h>
37 #include <wchar.h>
38 #include <ft2build.h>
39 #include "workarounds.h"
40
41 void BC_WindowBase::copy_area(int x1, int y1, int x2, int y2, int w, int h, BC_Pixmap *pixmap)
42 { BT
43         XCopyArea(top_level->display,
44                 pixmap ? pixmap->opaque_pixmap : this->pixmap->opaque_pixmap,
45                 pixmap ? pixmap->opaque_pixmap : this->pixmap->opaque_pixmap,
46                 top_level->gc, x1, y1, w, h, x2, y2);
47 }
48
49
50 void BC_WindowBase::draw_box(int x, int y, int w, int h, BC_Pixmap *pixmap)
51 { BT
52 //if(x == 0) printf("BC_WindowBase::draw_box %d %d %d %d\n", x, y, w, h);
53         XFillRectangle(top_level->display,
54                 pixmap ? pixmap->opaque_pixmap : this->pixmap->opaque_pixmap,
55                 top_level->gc, x, y, w, h);
56 }
57
58
59 void BC_WindowBase::draw_circle(int x, int y, int w, int h, BC_Pixmap *pixmap)
60 { BT
61         XDrawArc(top_level->display,
62                 pixmap ? pixmap->opaque_pixmap : this->pixmap->opaque_pixmap,
63                 top_level->gc, x, y, (w - 1), (h - 2), 0 * 64, 360 * 64);
64 }
65
66 void BC_WindowBase::draw_arc(int x, int y, int w, int h,
67         int start_angle, int angle_length, BC_Pixmap *pixmap)
68 { BT
69         XDrawArc(top_level->display,
70                 pixmap ? pixmap->opaque_pixmap : this->pixmap->opaque_pixmap,
71                 top_level->gc, x, y, (w - 1), (h - 2), start_angle * 64,
72                 angle_length * 64);
73 }
74
75 void BC_WindowBase::draw_disc(int x, int y, int w, int h, BC_Pixmap *pixmap)
76 { BT
77         XFillArc(top_level->display,
78                 pixmap ? pixmap->opaque_pixmap : this->pixmap->opaque_pixmap,
79                 top_level->gc, x, y, (w - 1), (h - 2), 0 * 64, 360 * 64);
80 }
81
82 void BC_WindowBase::clear_box(int x, int y, int w, int h, BC_Pixmap *pixmap)
83 { BT
84         set_color(bg_color);
85         Pixmap xpixmap = pixmap ? pixmap->opaque_pixmap : this->pixmap->opaque_pixmap;
86         XFillRectangle(top_level->display, xpixmap, top_level->gc, x, y, w, h);
87 }
88
89 void BC_WindowBase::draw_text_line(int x, int y, const char *text, int len,
90         BC_Pixmap *pixmap)
91 {
92 #ifdef HAVE_XFT
93         if( get_resources()->use_xft ) {
94                 draw_xft_text(x, y, text, len, pixmap);
95                 return;
96         }
97 #endif
98  BT
99         Pixmap xpixmap = pixmap ? pixmap->opaque_pixmap : this->pixmap->opaque_pixmap;
100         if( get_resources()->use_fontset ) {
101                 XFontSet fontset = top_level->get_curr_fontset();
102                 if( fontset ) {
103                         XmbDrawString(top_level->display, xpixmap, fontset,
104                                 top_level->gc, x, y, text, len);
105                         return;
106                 }
107
108         }
109 //printf("BC_WindowBase::draw_text 3\n");
110         XDrawString(top_level->display, xpixmap, top_level->gc, x, y, text, len);
111 }
112
113 void BC_WindowBase::draw_text(int x, int y, const char *text, int length,
114         BC_Pixmap *pixmap)
115 {
116         if( length < 0 ) length = strlen(text);
117         //int boldface = top_level->current_font & BOLDFACE;
118         int font = top_level->current_font & 0xff;
119
120         switch(top_level->current_font) {
121         case MEDIUM_7SEGMENT:
122                 for(int i = 0; i < length; i++) {
123                         VFrame *image, **img7seg = get_resources()->medium_7segment;
124                         int ch = text[i];
125                         switch( ch ) {
126                         case '0': case '1': case '2': case '3': case '4':
127                         case '5': case '6': case '7': case '8': case '9':
128                                   image = img7seg[ch-'0'];  break;
129                         case ':': image = img7seg[10];      break;
130                         case '.': image = img7seg[11];      break;
131                         case 'a': case 'b': case 'c':
132                         case 'd': case 'e': case 'f':  ch -= 'a'-'A';
133                         case 'A': case 'B': case 'C':  /* fallthru */
134                         case 'D': case 'E': case 'F':
135                                 image = img7seg[12+ch-'A']; break;
136                                 break;
137                         case '-': image = img7seg[19];      break;
138                         default:
139                         case ' ': image = img7seg[18];      break;
140                         }
141
142                         draw_vframe(image, x, y - image->get_h());
143                         x += image->get_w();
144                 }
145                 break;
146
147         default: {
148                 if(top_level->get_xft_struct(top_level->current_font)) {
149                         draw_xft_text(x, y, text, length, pixmap);
150                         return;
151                 }
152  BT
153                 for(int i = 0, j = 0; i <= length; i++) {
154                         if(text[i] == '\n' || text[i] == 0) {
155                                 if(get_resources()->use_fontset && top_level->get_curr_fontset()) {
156                                         XmbDrawString(top_level->display,
157                                                 pixmap ? pixmap->opaque_pixmap : this->pixmap->opaque_pixmap,
158                                                 top_level->get_curr_fontset(),
159                                                 top_level->gc, x, y, &text[j], i-j);
160                                 }
161                                 else {
162                                         XDrawString(top_level->display,
163                                                 pixmap ? pixmap->opaque_pixmap : this->pixmap->opaque_pixmap,
164                                                 top_level->gc, x, y, &text[j], i-j);
165                                 }
166                                 j = i + 1;
167                                 y += get_text_height(font);
168                         }
169                 }
170                 break; }
171         }
172 }
173
174 void BC_WindowBase::draw_utf8_text(int x, int y,
175         const char *text, int length, BC_Pixmap *pixmap)
176 {
177         if(length < 0) length = strlen(text);
178
179         if(top_level->get_xft_struct(top_level->current_font))
180         {
181                 draw_xft_text(x, y, text, length, pixmap, 1);
182                 return;
183         }
184  BT
185         for(int i = 0, j = 0; i <= length; i++)
186         {
187                 if(text[i] == '\n' || text[i] == 0)
188                 {
189                         if(get_resources()->use_fontset && top_level->get_curr_fontset())
190                         {
191                                 XmbDrawString(top_level->display,
192                                         pixmap ? pixmap->opaque_pixmap : this->pixmap->opaque_pixmap,
193                                         top_level->get_curr_fontset(), top_level->gc,
194                                         x, y, &text[j], i - j);
195                         }
196                         else
197                         {
198                                 XDrawString(top_level->display,
199                                         pixmap ? pixmap->opaque_pixmap : this->pixmap->opaque_pixmap,
200                                         top_level->gc,
201                                         x, y, &text[j], i - j);
202                         }
203
204                         j = i + 1;
205                         y += get_text_height(MEDIUMFONT);
206                 }
207         }
208 }
209
210 void BC_WindowBase::draw_xft_text(int x, int y,
211         const char *text, int length, BC_Pixmap *pixmap, int is_utf8)
212 {
213         int l = length + 1;
214         wchar_t wide_text[l];
215         length = BC_Resources::encode(
216                 is_utf8 ? "UTF8" : BC_Resources::encoding, BC_Resources::wide_encoding,
217                 (char*)text, length, (char*)wide_text, l*sizeof(wchar_t)) / sizeof(wchar_t);
218         draw_xft_text(x, y, wide_text, length, pixmap);
219 }
220
221 void BC_WindowBase::draw_xft_text(int x, int y,
222         const wchar_t *text, int length, BC_Pixmap *pixmap)
223 {
224         int dy = -1;
225         const wchar_t *wsp = text, *wep = wsp + length;
226         int font = top_level->current_font;
227         while( wsp < wep ) {
228                 const wchar_t *wcp = wsp;
229                 while( wcp < wep && *wcp != '\n' ) ++wcp;
230                 int len = wcp - wsp;
231                 if( len > 0 )
232                         draw_single_text(1, font, x, y, wsp, len, pixmap);
233                 if( wcp >= wep ) break;
234                 if( dy < 0 )
235                         dy = get_text_height(font);
236                 y += dy;
237                 wsp = wcp + 1;
238         }
239 }
240
241 void BC_WindowBase::xft_draw_string(XftColor *xft_color, XftFont *xft_font,
242                 int x, int y, const FcChar32 *fc, int len, BC_Pixmap *pixmap)
243 { BT
244         Pixmap draw_pixmap = 0;
245         XftDraw *xft_draw = (XftDraw *)
246                 (pixmap ? pixmap->opaque_xft_draw : this->pixmap->opaque_xft_draw);
247         int src_x = x, src_y = y, src_w = 0, src_h = 0;
248         XGCValues values;
249         XGetGCValues(top_level->display, top_level->gc, GCFunction, &values);
250         if( values.function != GXcopy ) {
251                 XSetFunction(top_level->display, top_level->gc, GXcopy);
252                 XGlyphInfo info;
253                 xftTextExtents32(top_level->display, xft_font, fc, len, &info);
254                 src_w = info.width;  src_h = info.height;
255                 draw_pixmap = XCreatePixmap(top_level->display, top_level->win,
256                         src_w, src_h, top_level->default_depth);
257                 int color = get_color(); set_color(0);
258                 XFillRectangle(top_level->display, draw_pixmap, top_level->gc, 0, 0, src_w, src_h);
259                 set_color(color);
260                 xft_draw = xftDrawCreate(top_level->display, draw_pixmap,
261                            top_level->vis, top_level->cmap);
262                 src_x = info.x;  src_y = info.y;
263         }
264         xftDrawString32(xft_draw, xft_color, xft_font, src_x, src_y, fc, len);
265         if( values.function != GXcopy ) {
266                 XSetFunction(top_level->display, top_level->gc, values.function);
267                 Pixmap xpixmap = pixmap ? pixmap->opaque_pixmap : this->pixmap->opaque_pixmap;
268                 XCopyArea(top_level->display, draw_pixmap, xpixmap,
269                         top_level->gc, 0, 0, src_w, src_h, x, y);
270                 XFreePixmap(top_level->display, draw_pixmap);
271                 xftDrawDestroy(xft_draw);
272         }
273 }
274
275 int BC_WindowBase::get_single_text_width(int font, const wchar_t *text, int length)
276 {
277         return draw_single_text(0, font, 0,0, text, length);
278 }
279
280 int BC_WindowBase::draw_single_text(int draw, int font,
281         int x, int y, const wchar_t *text, int length, BC_Pixmap *pixmap)
282 {
283         if( length < 0 )
284                 length = wcslen(text);
285         if( !length ) return 0;
286
287         if( !get_resources()->use_xft ) {
288  BT
289                 if( !get_font_struct(font) ) return 0;
290                 XChar2b xtext[length], *xp = xtext;
291                 for( int i=0; i<length; ++i,++xp ) {
292                         xp->byte1 = (unsigned char) (text[i] >> 8);
293                         xp->byte2 = (unsigned char) (text[i] & 0xff);
294                 }
295                 if( draw ) {
296                         XDrawString16(top_level->display,
297                                 pixmap ? pixmap->opaque_pixmap : this->pixmap->opaque_pixmap,
298                                 top_level->gc, x, y, xtext, length);
299                 }
300                 return XTextWidth16(get_font_struct(font), xtext, length);
301         }
302
303 #ifdef HAVE_XFT
304         XftColor xft_color;
305         if( draw ) {
306                 XRenderColor color;
307                 color.red = (top_level->current_color & 0xff0000) >> 16;
308                 color.red |= color.red << 8;
309                 color.green = (top_level->current_color & 0xff00) >> 8;
310                 color.green |= color.green << 8;
311                 color.blue = (top_level->current_color & 0xff);
312                 color.blue |= color.blue << 8;
313                 color.alpha = 0xffff;
314
315                 xftColorAllocValue(top_level->display, top_level->vis, top_level->cmap,
316                         &color, &xft_color);
317         }
318
319         int x0 = x;
320         XftFont *basefont = top_level->get_xft_struct(font);
321         XftFont *curfont = 0, *altfont = 0;
322         const wchar_t *up = text, *ubp = up, *uep = ubp + length;
323
324         while( up < uep ) {
325                 XftFont *xft_font = 0;
326                 if( xftCharExists(top_level->display, basefont, *up) )
327                         xft_font = basefont;
328                 else if( altfont ) {
329                         if( xftCharExists(top_level->display, altfont, *up))
330                                 xft_font = altfont;
331                         else {
332                                 xftFontClose(top_level->display, altfont);
333                                 altfont = 0;
334                         }
335                 }
336                 if( !xft_font ) {
337                         FcPattern *pattern = BC_Resources::find_similar_font(*up, basefont->pattern);
338                         if( pattern != 0 ) {
339                                 double psize = 0;
340                                 fcPatternGetDouble(basefont->pattern, FC_PIXEL_SIZE, 0, &psize);
341                                 fcPatternAddDouble(pattern, FC_PIXEL_SIZE, psize);
342                                 fcPatternDel(pattern, FC_SCALABLE);
343                                 xft_font = altfont = xftFontOpenPattern(top_level->display, pattern);
344                         }
345                 }
346                 if( !xft_font )
347                         xft_font = basefont;
348                 if( xft_font != curfont ) {
349                         if( curfont && up > ubp ) {
350                                 if( draw ) {
351                                         xft_draw_string(&xft_color, curfont, x, y,
352                                                 (const FcChar32*)ubp, up-ubp, pixmap);
353                                 }
354                                 XGlyphInfo extents;
355                                 xftTextExtents32(top_level->display, curfont,
356                                         (const FcChar32*)ubp, up-ubp, &extents);
357                                 x += extents.xOff;
358                         }
359                         ubp = up;  curfont = xft_font;
360                 }
361                 ++up;
362         }
363
364         if( curfont && up > ubp ) {
365                 if( draw ) {
366                         xft_draw_string(&xft_color, curfont, x, y,
367                                 (const FcChar32*)ubp, up-ubp, pixmap);
368                 }
369                 XGlyphInfo extents;
370                 xftTextExtents32(top_level->display, curfont,
371                         (const FcChar32*)ubp, up-ubp, &extents);
372                 x += extents.xOff;
373         }
374
375         if( altfont )
376                 xftFontClose(top_level->display, altfont);
377
378         xftColorFree(top_level->display, top_level->vis, top_level->cmap, &xft_color);
379 #endif
380         return x - x0;
381 }
382
383 void BC_WindowBase::draw_center_text(int x, int y, const char *text, int length)
384 {
385         if(length < 0) length = strlen(text);
386         int w = get_text_width(current_font, text, length);
387         x -= w / 2;
388         draw_text(x, y, text, length);
389 }
390
391 void BC_WindowBase::draw_line(int x1, int y1, int x2, int y2, BC_Pixmap *pixmap)
392 { BT
393 // Some X drivers can't draw 0 length lines
394         if( x1 == x2 && y1 == y2 ) {
395                 draw_pixel(x1, y1, pixmap);
396         }
397         else {
398                 XDrawLine(top_level->display,
399                         pixmap ? pixmap->opaque_pixmap : this->pixmap->opaque_pixmap,
400                         top_level->gc, x1, y1, x2, y2);
401         }
402 }
403
404 void BC_WindowBase::draw_polygon(ArrayList<int> *x, ArrayList<int> *y, BC_Pixmap *pixmap)
405 { BT
406         int npoints = MIN(x->total, y->total);
407         XPoint *points = new XPoint[npoints];
408
409         for( int i=0; i<npoints; ++i ) {
410                 points[i].x = x->values[i];
411                 points[i].y = y->values[i];
412         }
413
414         XDrawLines(top_level->display,
415                 pixmap ? pixmap->opaque_pixmap : this->pixmap->opaque_pixmap,
416                 top_level->gc, points, npoints, CoordModeOrigin);
417
418         delete [] points;
419 }
420
421 void BC_WindowBase::fill_polygon(ArrayList<int> *x, ArrayList<int> *y, BC_Pixmap *pixmap)
422 { BT
423         int npoints = MIN(x->total, y->total);
424         XPoint *points = new XPoint[npoints];
425
426         for( int i=0; i<npoints; ++i ) {
427                 points[i].x = x->values[i];
428                 points[i].y = y->values[i];
429         }
430
431         XFillPolygon(top_level->display,
432                 pixmap ? pixmap->opaque_pixmap : this->pixmap->opaque_pixmap,
433                 top_level->gc, points, npoints, Nonconvex, CoordModeOrigin);
434
435         delete [] points;
436 }
437
438
439 void BC_WindowBase::draw_rectangle(int x, int y, int w, int h)
440 { BT
441         XDrawRectangle(top_level->display,
442                 pixmap->opaque_pixmap, top_level->gc,
443                 x, y, w - 1, h - 1);
444 }
445
446 void BC_WindowBase::draw_3d_border(int x, int y, int w, int h, int is_down)
447 {
448         draw_3d_border(x, y, w, h,
449                 top_level->get_resources()->border_shadow2,
450                 top_level->get_resources()->border_shadow1,
451                 top_level->get_resources()->border_light1,
452                 top_level->get_resources()->border_light2);
453 }
454
455
456 void BC_WindowBase::draw_3d_border(int x, int y, int w, int h,
457         int light1, int light2, int shadow1, int shadow2)
458 {
459         int lx, ly, ux, uy;
460
461         h--; w--;
462
463         lx = x+1;  ly = y+1;
464         ux = x+w-1;  uy = y+h-1;
465
466         set_color(light1);
467         draw_line(x, y, ux, y);
468         draw_line(x, y, x, uy);
469         set_color(light2);
470         draw_line(lx, ly, ux - 1, ly);
471         draw_line(lx, ly, lx, uy - 1);
472
473         set_color(shadow1);
474         draw_line(ux, ly, ux, uy);
475         draw_line(lx, uy, ux, uy);
476         set_color(shadow2);
477         draw_line(x + w, y, x + w, y + h);
478         draw_line(x, y + h, x + w, y + h);
479 }
480
481 void BC_WindowBase::draw_3d_box(int x, int y, int w, int h,
482         int light1, int light2, int middle, int shadow1, int shadow2,
483         BC_Pixmap *pixmap)
484 {
485         int lx, ly, ux, uy;
486
487         h--; w--;
488
489         lx = x+1;  ly = y+1;
490         ux = x+w-1;  uy = y+h-1;
491
492         set_color(middle);
493         draw_box(x, y, w, h, pixmap);
494
495         set_color(light1);
496         draw_line(x, y, ux, y, pixmap);
497         draw_line(x, y, x, uy, pixmap);
498         set_color(light2);
499         draw_line(lx, ly, ux - 1, ly, pixmap);
500         draw_line(lx, ly, lx, uy - 1, pixmap);
501
502         set_color(shadow1);
503         draw_line(ux, ly, ux, uy, pixmap);
504         draw_line(lx, uy, ux, uy, pixmap);
505         set_color(shadow2);
506         draw_line(x + w, y, x + w, y + h, pixmap);
507         draw_line(x, y + h, x + w, y + h, pixmap);
508 }
509
510 void BC_WindowBase::draw_colored_box(int x, int y, int w, int h, int down, int highlighted)
511 {
512         if(!down)
513         {
514                 if(highlighted)
515                         draw_3d_box(x, y, w, h,
516                                 top_level->get_resources()->button_light,
517                                 top_level->get_resources()->button_highlighted,
518                                 top_level->get_resources()->button_highlighted,
519                                 top_level->get_resources()->button_shadow,
520                                 BLACK);
521                 else
522                         draw_3d_box(x, y, w, h,
523                                 top_level->get_resources()->button_light,
524                                 top_level->get_resources()->button_up,
525                                 top_level->get_resources()->button_up,
526                                 top_level->get_resources()->button_shadow,
527                                 BLACK);
528         }
529         else
530         {
531 // need highlighting for toggles
532                 if(highlighted)
533                         draw_3d_box(x, y, w, h,
534                                 top_level->get_resources()->button_shadow,
535                                 BLACK,
536                                 top_level->get_resources()->button_up,
537                                 top_level->get_resources()->button_up,
538                                 top_level->get_resources()->button_light);
539                 else
540                         draw_3d_box(x, y, w, h,
541                                 top_level->get_resources()->button_shadow,
542                                 BLACK,
543                                 top_level->get_resources()->button_down,
544                                 top_level->get_resources()->button_down,
545                                 top_level->get_resources()->button_light);
546         }
547 }
548
549
550 void BC_WindowBase::draw_border(char *text, int x, int y, int w, int h)
551 {
552         int left_indent = xS(20);
553         int lx, ly, ux, uy;
554
555         h--; w--;
556         lx = x + 1;  ly = y + 1;
557         ux = x + w - 1;  uy = y + h - 1;
558
559         set_opaque();
560         if(text && text[0] != 0)
561         {
562                 set_color(BLACK);
563                 set_font(MEDIUMFONT);
564                 draw_text(x + left_indent, y + get_text_height(MEDIUMFONT) / 2, text);
565         }
566
567         set_color(top_level->get_resources()->button_shadow);
568         draw_line(x, y, x + left_indent - xS(5), y);
569         draw_line(x, y, x, uy);
570         draw_line(x + left_indent + xS(5) + get_text_width(MEDIUMFONT, text), y, ux, y);
571         draw_line(x, y, x, uy);
572         draw_line(ux, ly, ux, uy);
573         draw_line(lx, uy, ux, uy);
574         set_color(top_level->get_resources()->button_light);
575         draw_line(lx, ly, x + left_indent - xS(5) - 1, ly);
576         draw_line(lx, ly, lx, uy - 1);
577         draw_line(x + left_indent + xS(5) + get_text_width(MEDIUMFONT, text), ly, ux - 1, ly);
578         draw_line(lx, ly, lx, uy - 1);
579         draw_line(x + w, y, x + w, y + h);
580         draw_line(x, y + h, x + w, y + h);
581 }
582
583 void BC_WindowBase::draw_triangle_down_flat(int x, int y, int w, int h)
584 { BT
585         int x1, y1, x2, y2, x3;
586         XPoint point[3];
587
588         x1 = x+1; x2 = x + w/2; x3 = x+w-1;
589         y1 = y; y2 = y+h-1;
590
591         point[0].x = x2; point[0].y = y2;
592         point[1].x = x3; point[1].y = y1;
593         point[2].x = x1; point[2].y = y1;
594
595         XFillPolygon(top_level->display, pixmap->opaque_pixmap, top_level->gc,
596                 (XPoint *)point, 3, Nonconvex, CoordModeOrigin);
597         draw_line(x1,y1, x3,y1);
598 }
599
600 void BC_WindowBase::draw_triangle_up(int x, int y, int w, int h,
601         int light1, int light2, int middle, int shadow1, int shadow2)
602 { BT
603         int x1, y1, x2, y2, x3;
604         XPoint point[3];
605
606         x1 = x; y1 = y; x2 = x + w / 2;
607         y2 = y + h - 1; x3 = x + w - 1;
608
609 // middle
610         point[0].x = x2; point[0].y = y1; point[1].x = x3;
611         point[1].y = y2; point[2].x = x1; point[2].y = y2;
612
613         set_color(middle);
614         XFillPolygon(top_level->display, pixmap->opaque_pixmap, top_level->gc,
615                 (XPoint *)point, 3, Nonconvex, CoordModeOrigin);
616
617 // bottom and top right
618         set_color(shadow1);
619         draw_line(x3, y2-1, x1, y2-1);
620         draw_line(x2-1, y1, x3-1, y2);
621         set_color(shadow2);
622         draw_line(x3, y2, x1, y2);
623         draw_line(x2, y1, x3, y2);
624
625 // top left
626         set_color(light2);
627         draw_line(x2+1, y1, x1+1, y2);
628         set_color(light1);
629         draw_line(x2, y1, x1, y2);
630 }
631
632 void BC_WindowBase::draw_triangle_down(int x, int y, int w, int h,
633         int light1, int light2, int middle, int shadow1, int shadow2)
634 {
635         int x1, y1, x2, y2, x3;
636         XPoint point[3];
637
638         x1 = x; x2 = x + w / 2; x3 = x + w - 1;
639         y1 = y; y2 = y + h - 1;
640
641         point[0].x = x2; point[0].y = y2; point[1].x = x3;
642         point[1].y = y1; point[2].x = x1; point[2].y = y1;
643
644         set_color(middle);
645         XFillPolygon(top_level->display,
646                 pixmap->opaque_pixmap,
647                 top_level->gc,
648                 (XPoint *)point,
649                 3,
650                 Nonconvex,
651                 CoordModeOrigin);
652
653 // top and bottom left
654         set_color(light2);
655         draw_line(x3-1, y1+1, x1+1, y1+1);
656         draw_line(x1+1, y1, x2+1, y2);
657         set_color(light1);
658         draw_line(x3, y1, x1, y1);
659         draw_line(x1, y1, x2, y2);
660
661 // bottom right
662         set_color(shadow1);
663         draw_line(x3-1, y1, x2-1, y2);
664         set_color(shadow2);
665         draw_line(x3, y1, x2, y2);
666 }
667
668 void BC_WindowBase::draw_triangle_left(int x, int y, int w, int h,
669         int light1, int light2, int middle, int shadow1, int shadow2)
670 { BT
671         int x1, y1, x2, y2, y3;
672         XPoint point[3];
673
674         // draw back arrow
675         y1 = y; x1 = x; y2 = y + h / 2;
676         x2 = x + w - 1; y3 = y + h - 1;
677
678         point[0].x = x1; point[0].y = y2; point[1].x = x2;
679         point[1].y = y1; point[2].x = x2; point[2].y = y3;
680
681         set_color(middle);
682         XFillPolygon(top_level->display,
683                 pixmap->opaque_pixmap,
684                 top_level->gc,
685                 (XPoint *)point,
686                 3,
687                 Nonconvex,
688                 CoordModeOrigin);
689
690 // right and bottom right
691         set_color(shadow1);
692         draw_line(x2-1, y1, x2-1, y3-1);
693         draw_line(x2, y3-1, x1, y2-1);
694         set_color(shadow2);
695         draw_line(x2, y1, x2, y3);
696         draw_line(x2, y3, x1, y2);
697
698 // top left
699         set_color(light1);
700         draw_line(x1, y2, x2, y1);
701         set_color(light2);
702         draw_line(x1, y2+1, x2, y1+1);
703 }
704
705 void BC_WindowBase::draw_triangle_right(int x, int y, int w, int h,
706         int light1, int light2, int middle, int shadow1, int shadow2)
707 { BT
708         int x1, y1, x2, y2, y3;
709         XPoint point[3];
710
711         y1 = y; y2 = y + h / 2; y3 = y + h - 1;
712         x1 = x; x2 = x + w - 1;
713
714         point[0].x = x1; point[0].y = y1; point[1].x = x2;
715         point[1].y = y2; point[2].x = x1; point[2].y = y3;
716
717         set_color(middle);
718         XFillPolygon(top_level->display,
719                 pixmap->opaque_pixmap,
720                 top_level->gc,
721                 (XPoint *)point,
722                 3,
723                 Nonconvex,
724                 CoordModeOrigin);
725
726 // left and top right
727         set_color(light2);
728         draw_line(x1+1, y3, x1+1, y1);
729         draw_line(x1, y1+1, x2, y2+1);
730         set_color(light1);
731         draw_line(x1, y3, x1, y1);
732         draw_line(x1, y1, x2, y2);
733
734 // bottom right
735         set_color(shadow1);
736         draw_line(x2, y2-1, x1, y3-1);
737         set_color(shadow2);
738         draw_line(x2, y2, x1, y3);
739 }
740
741
742 void BC_WindowBase::draw_check(int x, int y)
743 {
744         int xs3 = xS(3), xs4 = xS(4), xs6 = xS(6);
745         int ys1 = yS(1), ys2 = yS(2), ys3 = yS(3), ys4 = yS(4);
746         const int w = xS(15), h = yS(15), yh2 = y + h/2;
747         draw_line(x + xs3, yh2 + 0,   x + xs6,     yh2 + ys2);
748         draw_line(x + xs3, yh2 + ys1, x + xs6,     yh2 + ys3);
749         draw_line(x + xs6, yh2 + ys2, x + w - xs4, yh2 - ys3);
750         draw_line(x + xs3, yh2 + ys2, x + xs6,     yh2 + ys4);
751         draw_line(x + xs6, yh2 + ys2, x + w - xs4, yh2 - ys3);
752         draw_line(x + xs6, yh2 + ys3, x + w - xs4, yh2 - ys2);
753         draw_line(x + xs6, yh2 + ys4, x + w - xs4, yh2 - ys1);
754 }
755
756 void BC_WindowBase::draw_tiles(BC_Pixmap *tile, int origin_x, int origin_y, int x, int y, int w, int h)
757 { BT
758         if( !tile ) {
759                 set_color(bg_color);
760                 draw_box(x, y, w, h);
761         }
762         else {
763                 XSetFillStyle(top_level->display, top_level->gc, FillTiled);
764 // Don't know how slow this is
765                 XSetTile(top_level->display, top_level->gc, tile->get_pixmap());
766                 XSetTSOrigin(top_level->display, top_level->gc, origin_x, origin_y);
767                 draw_box(x, y, w, h);
768                 XSetFillStyle(top_level->display, top_level->gc, FillSolid);
769         }
770 }
771
772 void BC_WindowBase::draw_top_tiles(BC_WindowBase *parent_window, int x, int y, int w, int h)
773 { BT
774         Window tempwin;
775         int origin_x, origin_y;
776         XTranslateCoordinates(top_level->display,
777                 parent_window->win, win,
778                 0, 0, &origin_x, &origin_y, &tempwin);
779         draw_tiles(parent_window->bg_pixmap,
780                 origin_x, origin_y,
781                 x, y, w, h);
782 }
783
784 void BC_WindowBase::draw_top_background(BC_WindowBase *parent_window,
785         int x, int y, int w, int h, BC_Pixmap *pixmap)
786 { BT
787         Window tempwin;
788         int top_x, top_y;
789         XLockDisplay(top_level->display);
790
791         XTranslateCoordinates(top_level->display,
792                 win, parent_window->win,
793                 x, y, &top_x, &top_y, &tempwin);
794
795         XCopyArea(top_level->display,
796                 parent_window->pixmap->opaque_pixmap,
797                 pixmap ? pixmap->opaque_pixmap : this->pixmap->opaque_pixmap,
798                 top_level->gc, top_x, top_y, w, h, x, y);
799
800         XUnlockDisplay(top_level->display);
801 }
802
803 void BC_WindowBase::draw_background(int x, int y, int w, int h)
804 {
805         if( bg_pixmap ) {
806                 draw_tiles(bg_pixmap, 0, 0, x, y, w, h);
807         }
808         else {
809                 clear_box(x, y, w, h);
810         }
811 }
812
813 void BC_WindowBase::draw_bitmap(BC_Bitmap *bitmap, int dont_wait,
814         int dest_x, int dest_y, int dest_w, int dest_h,
815         int src_x, int src_y, int src_w, int src_h,
816         BC_Pixmap *pixmap)
817 { BT
818 // Hide cursor if video enabled
819         update_video_cursor();
820
821 //printf("BC_WindowBase::draw_bitmap %d dest_y=%d\n", __LINE__, dest_y);
822         if( dest_w <= 0 || dest_h <= 0 ) {
823 // Use hardware scaling to canvas dimensions if proper color model.
824                 if( bitmap->get_color_model() == BC_YUV420P ) {
825                         dest_w = w;
826                         dest_h = h;
827                 }
828                 else {
829                         dest_w = bitmap->get_w();
830                         dest_h = bitmap->get_h();
831                 }
832         }
833
834         if( src_w <= 0 || src_h <= 0 ) {
835                 src_w = bitmap->get_w();
836                 src_h = bitmap->get_h();
837         }
838
839         if( video_on ) {
840                 bitmap->write_drawable(win,
841                         top_level->gc, src_x, src_y, src_w, src_h,
842                         dest_x, dest_y, dest_w, dest_h, dont_wait);
843                 top_level->flush();
844         }
845         else {
846                 bitmap->write_drawable(pixmap ? pixmap->opaque_pixmap : this->pixmap->opaque_pixmap,
847                         top_level->gc, dest_x, dest_y, src_x, src_y, dest_w, dest_h, dont_wait);
848         }
849 //printf("BC_WindowBase::draw_bitmap 2\n");
850 }
851
852
853 void BC_WindowBase::draw_pixel(int x, int y, BC_Pixmap *pixmap)
854 { BT
855         XDrawPoint(top_level->display,
856                 pixmap ? pixmap->opaque_pixmap : this->pixmap->opaque_pixmap,
857                 top_level->gc, x, y);
858 }
859
860
861 void BC_WindowBase::draw_pixmap(BC_Pixmap *pixmap,
862         int dest_x, int dest_y, int dest_w, int dest_h,
863         int src_x, int src_y, BC_Pixmap *dst)
864 { BT
865         pixmap->write_drawable(dst ? dst->opaque_pixmap : this->pixmap->opaque_pixmap,
866                         dest_x, dest_y, dest_w, dest_h, src_x, src_y);
867 }
868
869 void BC_WindowBase::draw_vframe(VFrame *frame,
870         int dest_x, int dest_y, int dest_w, int dest_h,
871         int src_x, int src_y, int src_w, int src_h,
872         BC_Pixmap *pixmap)
873 {
874         if(dest_w <= 0) dest_w = frame->get_w() - src_x;
875         if(dest_h <= 0) dest_h = frame->get_h() - src_y;
876         if(src_w <= 0) src_w = frame->get_w() - src_x;
877         if(src_h <= 0) src_h = frame->get_h() - src_y;
878         CLAMP(src_x, 0, frame->get_w() - 1);
879         CLAMP(src_y, 0, frame->get_h() - 1);
880         if(src_x + src_w > frame->get_w()) src_w = frame->get_w() - src_x;
881         if(src_y + src_h > frame->get_h()) src_h = frame->get_h() - src_y;
882
883         if( !temp_bitmap )
884                 temp_bitmap = new BC_Bitmap(this, dest_w, dest_h, get_color_model(), 0);
885         temp_bitmap->match_params(dest_w, dest_h, get_color_model(), 0);
886
887         temp_bitmap->read_frame(frame,
888                 src_x, src_y, src_w, src_h,
889                 0, 0, dest_w, dest_h, bg_color);
890
891         draw_bitmap(temp_bitmap, 0,
892                 dest_x, dest_y, dest_w, dest_h,
893                 0, 0, -1, -1, pixmap);
894 }
895
896 void BC_WindowBase::draw_tooltip(const char *text)
897 {
898         if( !text )
899                 text = tooltip_text;
900         if(tooltip_popup && text)
901         {
902                 int w = tooltip_popup->get_w(), h = tooltip_popup->get_h();
903                 tooltip_popup->set_color(get_resources()->tooltip_bg_color);
904                 tooltip_popup->draw_box(0, 0, w, h);
905                 tooltip_popup->set_color(BLACK);
906                 tooltip_popup->draw_rectangle(0, 0, w, h);
907                 tooltip_popup->set_font(MEDIUMFONT);
908                 tooltip_popup->draw_text(TOOLTIP_MARGIN,
909                         get_text_ascent(MEDIUMFONT) + TOOLTIP_MARGIN,
910                         text);
911         }
912 }
913
914 void BC_WindowBase::slide_left(int distance)
915 { BT
916         if(distance < w)
917         {
918                 XCopyArea(top_level->display,
919                         pixmap->opaque_pixmap,
920                         pixmap->opaque_pixmap,
921                         top_level->gc,
922                         distance,
923                         0,
924                         w - distance,
925                         h,
926                         0,
927                         0);
928         }
929 }
930
931 void BC_WindowBase::slide_right(int distance)
932 { BT
933         if(distance < w)
934         {
935                 XCopyArea(top_level->display,
936                         pixmap->opaque_pixmap, pixmap->opaque_pixmap, top_level->gc,
937                         0, 0, w - distance, h, distance, 0);
938         }
939 }
940
941 void BC_WindowBase::slide_up(int distance)
942 { BT
943         if(distance < h)
944         {
945                 XCopyArea(top_level->display,
946                         pixmap->opaque_pixmap, pixmap->opaque_pixmap, top_level->gc,
947                         0, distance, w, h - distance, 0, 0);
948                 set_color(bg_color);
949                 XFillRectangle(top_level->display, pixmap->opaque_pixmap, top_level->gc,
950                         0, h - distance, w, distance);
951         }
952 }
953
954 void BC_WindowBase::slide_down(int distance)
955 { BT
956         if(distance < h)
957         {
958                 XCopyArea(top_level->display,
959                         pixmap->opaque_pixmap, pixmap->opaque_pixmap, top_level->gc,
960                         0, 0, w, h - distance, 0, distance);
961                 set_color(bg_color);
962                 XFillRectangle(top_level->display,
963                         pixmap->opaque_pixmap, top_level->gc,
964                         0, 0, w, distance);
965         }
966 }
967
968 // 3 segments in separate pixmaps.  Obsolete.
969 void BC_WindowBase::draw_3segment(int x, int y, int w, int h,
970         BC_Pixmap *left_image, BC_Pixmap *mid_image,
971         BC_Pixmap *right_image, BC_Pixmap *pixmap)
972 {
973         if(w <= 0 || h <= 0) return;
974         int left_boundary = left_image->get_w_fixed();
975         int right_boundary = w - right_image->get_w_fixed();
976         for(int i = 0; i < w; )
977         {
978                 BC_Pixmap *image;
979
980                 if(i < left_boundary)
981                         image = left_image;
982                 else
983                 if(i < right_boundary)
984                         image = mid_image;
985                 else
986                         image = right_image;
987
988                 int output_w = image->get_w_fixed();
989
990                 if(i < left_boundary)
991                 {
992                         if(i + output_w > left_boundary) output_w = left_boundary - i;
993                 }
994                 else
995                 if(i < right_boundary)
996                 {
997                         if(i + output_w > right_boundary) output_w = right_boundary - i;
998                 }
999                 else
1000                         if(i + output_w > w) output_w = w - i;
1001
1002                 image->write_drawable(pixmap ? pixmap->opaque_pixmap : this->pixmap->opaque_pixmap,
1003                                 x + i, y, output_w, h, 0, 0);
1004                 i += output_w;
1005         }
1006 }
1007 // 3 segments in separate vframes.  Obsolete.
1008 void BC_WindowBase::draw_3segment(int x, int y, int w, int h,
1009         VFrame *left_image, VFrame *mid_image,
1010         VFrame *right_image, BC_Pixmap *pixmap)
1011 {
1012         if(w <= 0 || h <= 0) return;
1013         int left_boundary = left_image->get_w_fixed();
1014         int right_boundary = w - right_image->get_w_fixed();
1015
1016
1017         for(int i = 0; i < w; )
1018         {
1019                 VFrame *image;
1020
1021                 if(i < left_boundary)
1022                         image = left_image;
1023                 else
1024                 if(i < right_boundary)
1025                         image = mid_image;
1026                 else
1027                         image = right_image;
1028
1029                 int output_w = image->get_w_fixed();
1030
1031                 if(i < left_boundary)
1032                 {
1033                         if(i + output_w > left_boundary) output_w = left_boundary - i;
1034                 }
1035                 else
1036                 if(i < right_boundary)
1037                 {
1038                         if(i + output_w > right_boundary) output_w = right_boundary - i;
1039                 }
1040                 else
1041                         if(i + output_w > w) output_w = w - i;
1042
1043                 if(image)
1044                         draw_vframe(image, x + i, y, output_w, h,
1045                                         0, 0, 0, 0, pixmap);
1046
1047                 if(output_w == 0) break;
1048                 i += output_w;
1049         }
1050 }
1051
1052 // Draw all 3 segments in a single vframe for a changing level
1053
1054 // total_x
1055 // <------>
1056 // total_w
1057 //         <------------------------------------------------------------>
1058 // x
1059 // |
1060 // w
1061 // <-------------------------------------------------------------------->
1062 // output
1063 //         |-------------------|----------------------|------------------|
1064
1065
1066 void BC_WindowBase::draw_3segmenth(int x, int y, int w,
1067                 VFrame *image, BC_Pixmap *pixmap)
1068 {
1069         draw_3segmenth(x, y, w, x, w, image, pixmap);
1070 }
1071
1072 void BC_WindowBase::draw_3segmenth(int x, int y, int w,
1073                 int total_x, int total_w, VFrame *image,
1074                 BC_Pixmap *pixmap)
1075 {
1076         if(total_w <= 0 || w <= 0 || h <= 0) return;
1077         int third_image = image->get_w() / 3;
1078         int half_image = image->get_w() / 2;
1079         //int left_boundary = third_image;
1080         //int right_boundary = total_w - third_image;
1081         int left_in_x = 0;
1082         int left_in_w = third_image;
1083         int left_out_x = total_x;
1084         int left_out_w = third_image;
1085         int right_in_x = image->get_w() - third_image;
1086         int right_in_w = third_image;
1087         int right_out_x = total_x + total_w - third_image;
1088         int right_out_w = third_image;
1089         int center_out_x = total_x + third_image;
1090         int center_out_w = total_w - third_image * 2;
1091         //int image_x, image_w;
1092
1093 //printf("BC_WindowBase::draw_3segment 1 left_out_x=%d left_out_w=%d center_out_x=%d center_out_w=%d right_out_x=%d right_out_w=%d\n",
1094 //      left_out_x, left_out_w, center_out_x, center_out_w, right_out_x, right_out_w);
1095
1096         if(left_out_x < x)
1097         {
1098                 left_in_w -= x - left_out_x;
1099                 left_out_w -= x - left_out_x;
1100                 left_in_x += x - left_out_x;
1101                 left_out_x += x - left_out_x;
1102         }
1103
1104         if(left_out_x + left_out_w > x + w)
1105         {
1106                 left_in_w -= (left_out_x + left_out_w) - (x + w);
1107                 left_out_w -= (left_out_x + left_out_w) - (x + w);
1108         }
1109
1110         if(right_out_x < x)
1111         {
1112                 right_in_w -= x - right_out_x;
1113                 right_out_w -= x - right_out_x;
1114                 right_in_x += x - right_out_x;
1115                 right_out_x += x - right_out_x;
1116         }
1117
1118         if(right_out_x + right_out_w > x + w)
1119         {
1120                 right_in_w -= (right_out_x + right_out_w) - (x + w);
1121                 right_out_w -= (right_out_x + right_out_w) - (x + w);
1122         }
1123
1124         if(center_out_x < x)
1125         {
1126                 center_out_w -= x - center_out_x;
1127                 center_out_x += x - center_out_x;
1128         }
1129
1130         if(center_out_x + center_out_w > x + w)
1131         {
1132                 center_out_w -= (center_out_x + center_out_w) - (x + w);
1133         }
1134
1135         if(!temp_bitmap) temp_bitmap = new BC_Bitmap(top_level,
1136                 image->get_w(), image->get_h(),
1137                 get_color_model(), 0);
1138         temp_bitmap->match_params(image->get_w(), image->get_h(),
1139                 get_color_model(), 0);
1140         temp_bitmap->read_frame(image,
1141                 0, 0, image->get_w(), image->get_h(), bg_color);
1142 // src width and height are meaningless in video_off mode
1143 //printf("BC_WindowBase::draw_3segment 2 left_out_x=%d left_out_w=%d center_out_x=%d center_out_w=%d right_out_x=%d right_out_w=%d\n",
1144 //      left_out_x, left_out_w, center_out_x, center_out_w, right_out_x, right_out_w);
1145         if(left_out_w > 0) {
1146                 draw_bitmap(temp_bitmap, 0,
1147                         left_out_x, y, left_out_w, image->get_h(),
1148                         left_in_x, 0, -1, -1, pixmap);
1149         }
1150
1151         if(right_out_w > 0) {
1152                 draw_bitmap(temp_bitmap, 0,
1153                         right_out_x, y, right_out_w, image->get_h(),
1154                         right_in_x, 0, -1, -1, pixmap);
1155         }
1156
1157         for( int pixel = center_out_x;
1158                  pixel < center_out_x + center_out_w;
1159                  pixel += half_image ) {
1160                 int fragment_w = half_image;
1161                 if(fragment_w + pixel > center_out_x + center_out_w)
1162                         fragment_w = (center_out_x + center_out_w) - pixel;
1163
1164 //printf("BC_WindowBase::draw_3segment 2 pixel=%d fragment_w=%d\n", pixel, fragment_w);
1165                 draw_bitmap(temp_bitmap, 0,
1166                         pixel, y, fragment_w, image->get_h(),
1167                         third_image, 0, -1, -1, pixmap);
1168         }
1169
1170 }
1171
1172 void BC_WindowBase::draw_3segmenth(int x, int y, int w, int total_x, int total_w,
1173                 BC_Pixmap *src, BC_Pixmap *dst)
1174 {
1175         if(w <= 0 || total_w <= 0) return;
1176         if(!src) printf("BC_WindowBase::draw_3segmenth src=0\n");
1177         int quarter_src = src->get_w() / 4;
1178         int half_src = src->get_w() / 2;
1179         //int left_boundary = quarter_src;
1180         //int right_boundary = total_w - quarter_src;
1181         int left_in_x = 0;
1182         int left_in_w = quarter_src;
1183         int left_out_x = total_x;
1184         int left_out_w = quarter_src;
1185         int right_in_x = src->get_w() - quarter_src;
1186         int right_in_w = quarter_src;
1187         int right_out_x = total_x + total_w - quarter_src;
1188         int right_out_w = quarter_src;
1189         int center_out_x = total_x + quarter_src;
1190         int center_out_w = total_w - quarter_src * 2;
1191         //int src_x, src_w;
1192
1193 //printf("BC_WindowBase::draw_3segment 1 left_out_x=%d left_out_w=%d center_out_x=%d center_out_w=%d right_out_x=%d right_out_w=%d\n",
1194 //      left_out_x, left_out_w, center_out_x, center_out_w, right_out_x, right_out_w);
1195
1196         if(left_out_x < x)
1197         {
1198                 left_in_w -= x - left_out_x;
1199                 left_out_w -= x - left_out_x;
1200                 left_in_x += x - left_out_x;
1201                 left_out_x += x - left_out_x;
1202         }
1203
1204         if(left_out_x + left_out_w > x + w)
1205         {
1206                 left_in_w -= (left_out_x + left_out_w) - (x + w);
1207                 left_out_w -= (left_out_x + left_out_w) - (x + w);
1208         }
1209
1210         if(right_out_x < x)
1211         {
1212                 right_in_w -= x - right_out_x;
1213                 right_out_w -= x - right_out_x;
1214                 right_in_x += x - right_out_x;
1215                 right_out_x += x - right_out_x;
1216         }
1217
1218         if(right_out_x + right_out_w > x + w)
1219         {
1220                 right_in_w -= (right_out_x + right_out_w) - (x + w);
1221                 right_out_w -= (right_out_x + right_out_w) - (x + w);
1222         }
1223
1224         if(center_out_x < x)
1225         {
1226                 center_out_w -= x - center_out_x;
1227                 center_out_x += x - center_out_x;
1228         }
1229
1230         if(center_out_x + center_out_w > x + w)
1231         {
1232                 center_out_w -= (center_out_x + center_out_w) - (x + w);
1233         }
1234
1235
1236 //printf("BC_WindowBase::draw_3segment 2 left_out_x=%d left_out_w=%d center_out_x=%d center_out_w=%d right_out_x=%d right_out_w=%d\n",
1237 //      left_out_x, left_out_w, center_out_x, center_out_w, right_out_x, right_out_w);
1238         if(left_out_w > 0)
1239                 draw_pixmap(src, left_out_x, y, left_out_w, src->get_h(), left_in_x, 0, dst);
1240
1241         if(right_out_w > 0)
1242                 draw_pixmap(src, right_out_x, y, right_out_w, src->get_h(), right_in_x, 0, dst);
1243
1244         for( int pixel = center_out_x; pixel < center_out_x + center_out_w; pixel += half_src) {
1245                 int fragment_w = half_src;
1246                 if(fragment_w + pixel > center_out_x + center_out_w)
1247                         fragment_w = (center_out_x + center_out_w) - pixel;
1248 //printf("BC_WindowBase::draw_3segment 2 pixel=%d fragment_w=%d\n", pixel, fragment_w);
1249                 draw_pixmap(src, pixel, y, fragment_w, src->get_h(), quarter_src, 0, dst);
1250         }
1251
1252 }
1253
1254
1255 void BC_WindowBase::draw_3segmenth(int x, int y, int w, BC_Pixmap *src, BC_Pixmap *dst)
1256 {
1257         if(w <= 0) return;
1258         int third_image = src->get_w() / 3;
1259         int half_output = w / 2;
1260         //int left_boundary = third_image;
1261         //int right_boundary = w - third_image;
1262         int left_in_x = 0;
1263         int left_in_w = third_image;
1264         int left_out_x = x;
1265         int left_out_w = third_image;
1266         int right_in_x = src->get_w() - third_image;
1267         int right_in_w = third_image;
1268         int right_out_x = x + w - third_image;
1269         int right_out_w = third_image;
1270         //int image_x, image_w;
1271
1272 //printf("BC_WindowBase::draw_3segment 1 left_out_x=%d left_out_w=%d center_out_x=%d center_out_w=%d right_out_x=%d right_out_w=%d\n",
1273 //      left_out_x, left_out_w, center_out_x, center_out_w, right_out_x, right_out_w);
1274
1275         if(left_out_w > half_output)
1276         {
1277                 left_in_w -= left_out_w - half_output;
1278                 left_out_w -= left_out_w - half_output;
1279         }
1280
1281         if(right_out_x < x + half_output)
1282         {
1283                 right_in_w -= x + half_output - right_out_x;
1284                 right_out_w -= x + half_output - right_out_x;
1285                 right_in_x += x + half_output - right_out_x;
1286                 right_out_x += x + half_output - right_out_x;
1287         }
1288
1289 //printf("BC_WindowBase::draw_3segment 2 left_out_x=%d left_out_w=%d center_out_x=%d center_out_w=%d right_out_x=%d right_out_w=%d\n",
1290 //      left_out_x, left_out_w, center_out_x, center_out_w, right_out_x, right_out_w);
1291         if(left_out_w > 0)
1292                 draw_pixmap(src, left_out_x, y, left_out_w, src->get_h(), left_in_x, 0, dst);
1293
1294         if(right_out_w > 0)
1295                 draw_pixmap(src, right_out_x, y, right_out_w, src->get_h(), right_in_x, 0, dst);
1296
1297         for(int pixel = left_out_x + left_out_w; pixel < right_out_x; pixel += third_image) {
1298                 int fragment_w = third_image;
1299                 if(fragment_w + pixel > right_out_x)
1300                         fragment_w = right_out_x - pixel;
1301
1302 //printf("BC_WindowBase::draw_3segment 2 pixel=%d fragment_w=%d\n", pixel, fragment_w);
1303                 draw_pixmap(src, pixel, y, fragment_w, src->get_h(), third_image, 0, dst);
1304         }
1305
1306 }
1307
1308 void BC_WindowBase::draw_3segmentv(int x, int y, int h, VFrame *src, BC_Pixmap *dst)
1309 {
1310         if(h <= 0) return;
1311         int third_image = src->get_h() / 3;
1312         int half_output = h / 2;
1313         //int left_boundary = third_image;
1314         //int right_boundary = h - third_image;
1315         int left_in_y = 0;
1316         int left_in_h = third_image;
1317         int left_out_y = y;
1318         int left_out_h = third_image;
1319         int right_in_y = src->get_h() - third_image;
1320         int right_in_h = third_image;
1321         int right_out_y = y + h - third_image;
1322         int right_out_h = third_image;
1323         //int image_y, image_h;
1324
1325
1326         if(left_out_h > half_output)
1327         {
1328                 left_in_h -= left_out_h - half_output;
1329                 left_out_h -= left_out_h - half_output;
1330         }
1331
1332         if(right_out_y < y + half_output)
1333         {
1334                 right_in_h -= y + half_output - right_out_y;
1335                 right_out_h -= y + half_output - right_out_y;
1336                 right_in_y += y + half_output - right_out_y;
1337                 right_out_y += y + half_output - right_out_y;
1338         }
1339
1340
1341         if(!temp_bitmap) temp_bitmap = new BC_Bitmap(top_level,
1342                 src->get_w(),
1343                 src->get_h(),
1344                 get_color_model(),
1345                 0);
1346         temp_bitmap->match_params(src->get_w(),
1347                 src->get_h(),
1348                 get_color_model(),
1349                 0);
1350         temp_bitmap->read_frame(src,
1351                 0, 0, src->get_w(), src->get_h(), bg_color);
1352
1353
1354         if(left_out_h > 0)
1355         {
1356                 draw_bitmap(temp_bitmap, 0, x, left_out_y,
1357                         src->get_w(), left_out_h, 0, left_in_y,
1358                         -1, -1, dst);
1359         }
1360
1361         if(right_out_h > 0)
1362         {
1363                 draw_bitmap(temp_bitmap, 0, x, right_out_y,
1364                         src->get_w(), right_out_h, 0, right_in_y,
1365                         -1, -1, dst);
1366         }
1367
1368         for(int pixel = left_out_y + left_out_h;
1369                 pixel < right_out_y;
1370                 pixel += third_image)
1371         {
1372                 int fragment_h = third_image;
1373                 if(fragment_h + pixel > right_out_y)
1374                         fragment_h = right_out_y - pixel;
1375
1376 //printf("BC_WindowBase::draw_3segment 2 pixel=%d fragment_w=%d\n", pixel, fragment_w);
1377                 draw_bitmap(temp_bitmap, 0, x, pixel,
1378                         src->get_w(), fragment_h, 0, third_image,
1379                         -1, -1, dst);
1380         }
1381 }
1382
1383 void BC_WindowBase::draw_3segmentv(int x,
1384                 int y,
1385                 int h,
1386                 BC_Pixmap *src,
1387                 BC_Pixmap *dst)
1388 {
1389         if(h <= 0) return;
1390         int third_image = src->get_h() / 3;
1391         int half_output = h / 2;
1392         //int left_boundary = third_image;
1393         //int right_boundary = h - third_image;
1394         int left_in_y = 0;
1395         int left_in_h = third_image;
1396         int left_out_y = y;
1397         int left_out_h = third_image;
1398         int right_in_y = src->get_h() - third_image;
1399         int right_in_h = third_image;
1400         int right_out_y = y + h - third_image;
1401         int right_out_h = third_image;
1402         //int image_y, image_h;
1403
1404
1405         if(left_out_h > half_output)
1406         {
1407                 left_in_h -= left_out_h - half_output;
1408                 left_out_h -= left_out_h - half_output;
1409         }
1410
1411         if(right_out_y < y + half_output)
1412         {
1413                 right_in_h -= y + half_output - right_out_y;
1414                 right_out_h -= y + half_output - right_out_y;
1415                 right_in_y += y + half_output - right_out_y;
1416                 right_out_y += y + half_output - right_out_y;
1417         }
1418
1419         if(left_out_h > 0)
1420                 draw_pixmap(src, x, left_out_y, src->get_w(), left_out_h,
1421                         0, left_in_y, dst);
1422
1423         if(right_out_h > 0)
1424                 draw_pixmap(src, x, right_out_y, src->get_w(), right_out_h,
1425                         0, right_in_y, dst);
1426
1427         for(int pixel = left_out_y + left_out_h; pixel < right_out_y; pixel += third_image) {
1428                 int fragment_h = third_image;
1429                 if(fragment_h + pixel > right_out_y)
1430                         fragment_h = right_out_y - pixel;
1431
1432 //printf("BC_WindowBase::draw_3segment 2 pixel=%d fragment_w=%d\n", pixel, fragment_w);
1433                 draw_pixmap(src, x, pixel, src->get_w(), fragment_h,
1434                         0, third_image, dst);
1435         }
1436 }
1437
1438
1439 void BC_WindowBase::draw_9segment(int x, int y, int w, int h,
1440                 BC_Pixmap *src, BC_Pixmap *dst)
1441 {
1442         if(w <= 0 || h <= 0) return;
1443
1444         int in_x_third = src->get_w() / 3;
1445         int in_y_third = src->get_h() / 3;
1446         int out_x_half = w / 2;
1447         int out_y_half = h / 2;
1448
1449         int in_x1 = 0;
1450         int in_y1 = 0;
1451         int out_x1 = 0;
1452         int out_y1 = 0;
1453         int in_x2 = MIN(in_x_third, out_x_half);
1454         int in_y2 = MIN(in_y_third, out_y_half);
1455         int out_x2 = in_x2;
1456         int out_y2 = in_y2;
1457
1458         int out_x3 = MAX(w - out_x_half, w - in_x_third);
1459         int out_x4 = w;
1460         int in_x3 = src->get_w() - (out_x4 - out_x3);
1461         //int in_x4 = src->get_w();
1462
1463         int out_y3 = MAX(h - out_y_half, h - in_y_third);
1464         int out_y4 = h;
1465         int in_y3 = src->get_h() - (out_y4 - out_y3);
1466         //int in_y4 = src->get_h();
1467
1468 // Segment 1
1469         draw_pixmap(src, x + out_x1, y + out_y1, out_x2 - out_x1, out_y2 - out_y1,
1470                 in_x1, in_y1, dst);
1471
1472 // Segment 2 * n
1473         for(int i = out_x2; i < out_x3; i += in_x3 - in_x2) {
1474                 if(out_x3 - i > 0) {
1475                         int w = MIN(in_x3 - in_x2, out_x3 - i);
1476                         draw_pixmap(src, x + i, y + out_y1, w, out_y2 - out_y1,
1477                                 in_x2, in_y1, dst);
1478                 }
1479         }
1480
1481 // Segment 3
1482         draw_pixmap(src, x + out_x3, y + out_y1, out_x4 - out_x3, out_y2 - out_y1,
1483                 in_x3, in_y1, dst);
1484
1485 // Segment 4 * n
1486         for(int i = out_y2; i < out_y3; i += in_y3 - in_y2)
1487         {
1488                 if(out_y3 - i > 0)
1489                 {
1490                         int h = MIN(in_y3 - in_y2, out_y3 - i);
1491                         draw_pixmap(src, x + out_x1, y + i, out_x2 - out_x1, h,
1492                                 in_x1, in_y2, dst);
1493                 }
1494         }
1495
1496 // Segment 5 * n * n
1497         for(int i = out_y2; i < out_y3; i += in_y3 - in_y2 /* in_y_third */)
1498         {
1499                 if(out_y3 - i > 0)
1500                 {
1501                         int h = MIN(in_y3 - in_y2 /* in_y_third */, out_y3 - i);
1502
1503
1504                         for(int j = out_x2; j < out_x3; j += in_x3 - in_x2 /* in_x_third */)
1505                         {
1506                                 int w = MIN(in_x3 - in_x2 /* in_x_third */, out_x3 - j);
1507                                 if(out_x3 - j > 0)
1508                                         draw_pixmap(src, x + j, y + i, w, h,
1509                                                 in_x2, in_y2, dst);
1510                         }
1511                 }
1512         }
1513
1514 // Segment 6 * n
1515         for(int i = out_y2; i < out_y3; i += in_y3 - in_y2)
1516         {
1517                 if(out_y3 - i > 0)
1518                 {
1519                         int h = MIN(in_y3 - in_y2, out_y3 - i);
1520                         draw_pixmap(src, x + out_x3, y + i, out_x4 - out_x3, h,
1521                                 in_x3, in_y2, dst);
1522                 }
1523         }
1524
1525 // Segment 7
1526         draw_pixmap(src, x + out_x1, y + out_y3, out_x2 - out_x1, out_y4 - out_y3,
1527                 in_x1, in_y3, dst);
1528
1529 // Segment 8 * n
1530         for(int i = out_x2; i < out_x3; i += in_x3 - in_x2)
1531         {
1532                 if(out_x3 - i > 0)
1533                 {
1534                         int w = MIN(in_x3 - in_y2, out_x3 - i);
1535                         draw_pixmap(src, x + i, y + out_y3, w, out_y4 - out_y3,
1536                                 in_x2, in_y3, dst);
1537                 }
1538         }
1539
1540 // Segment 9
1541         draw_pixmap(src, x + out_x3, y + out_y3, out_x4 - out_x3, out_y4 - out_y3,
1542                 in_x3, in_y3, dst);
1543 }
1544
1545
1546 void BC_WindowBase::draw_9segment(int x, int y, int w, int h,
1547                 VFrame *src, BC_Pixmap *dst)
1548 {
1549         if(w <= 0 || h <= 0) return;
1550
1551         int in_x_third = src->get_w() / 3;
1552         int in_y_third = src->get_h() / 3;
1553         int out_x_half = w / 2;
1554         int out_y_half = h / 2;
1555
1556         int in_x1 = 0;
1557         int in_y1 = 0;
1558         int out_x1 = 0;
1559         int out_y1 = 0;
1560         int in_x2 = MIN(in_x_third, out_x_half);
1561         int in_y2 = MIN(in_y_third, out_y_half);
1562         int out_x2 = in_x2;
1563         int out_y2 = in_y2;
1564
1565         int out_x3 = MAX(w - out_x_half, w - in_x_third);
1566         int out_x4 = w;
1567         int in_x3 = src->get_w() - (out_x4 - out_x3);
1568         int in_x4 = src->get_w();
1569
1570         int out_y3 = MAX(h - out_y_half, h - in_y_third);
1571         int out_y4 = h;
1572         int in_y3 = src->get_h() - (out_y4 - out_y3);
1573         int in_y4 = src->get_h();
1574
1575 //printf("PFCFrame::draw_9segment 1 %d %d %d %d\n", out_x1, out_x2, out_x3, out_x4);
1576 //printf("PFCFrame::draw_9segment 2 %d %d %d %d\n", in_x1, in_x2, in_x3, in_x4);
1577 //printf("PFCFrame::draw_9segment 2 %d %d %d %d\n", in_y1, in_y2, in_y3, in_y4);
1578
1579         if(!temp_bitmap) temp_bitmap = new BC_Bitmap(top_level,
1580                 src->get_w(),
1581                 src->get_h(),
1582                 get_color_model(),
1583                 0);
1584         temp_bitmap->match_params(src->get_w(),
1585                 src->get_h(),
1586                 get_color_model(),
1587                 0);
1588         temp_bitmap->read_frame(src,
1589                 0, 0, src->get_w(), src->get_h(), bg_color);
1590
1591 // Segment 1
1592         draw_bitmap(temp_bitmap, 0,
1593                 x + out_x1, y + out_y1, out_x2 - out_x1, out_y2 - out_y1,
1594                 in_x1, in_y1, in_x2 - in_x1, in_y2 - in_y1,
1595                 dst);
1596
1597 // Segment 2 * n
1598         for( int i = out_x2; i < out_x3; i += in_x3 - in_x2 ) {
1599                 if( out_x3 - i > 0 ) {
1600                         int w = MIN(in_x3 - in_x2, out_x3 - i);
1601                         draw_bitmap(temp_bitmap, 0,
1602                                 x + i, y + out_y1, w, out_y2 - out_y1,
1603                                 in_x2, in_y1, w, in_y2 - in_y1,
1604                                 dst);
1605                 }
1606         }
1607
1608 // Segment 3
1609         draw_bitmap(temp_bitmap, 0,
1610                 x + out_x3, y + out_y1, out_x4 - out_x3, out_y2 - out_y1,
1611                 in_x3, in_y1, in_x4 - in_x3, in_y2 - in_y1,
1612                 dst);
1613
1614 // Segment 4 * n
1615         for( int i = out_y2; i < out_y3; i += in_y3 - in_y2 ) {
1616                 if( out_y3 - i > 0 ) {
1617                         int h = MIN(in_y3 - in_y2, out_y3 - i);
1618                         draw_bitmap(temp_bitmap, 0,
1619                                 x + out_x1, y + i, out_x2 - out_x1, h,
1620                                 in_x1, in_y2, in_x2 - in_x1, h,
1621                                 dst);
1622                 }
1623         }
1624
1625 // Segment 5 * n * n
1626         for( int i = out_y2; i < out_y3; i += in_y3 - in_y2 ) {
1627                 if( out_y3 - i > 0 ) {
1628                         int h = MIN(in_y3 - in_y2, out_y3 - i);
1629
1630                         for( int j = out_x2; j < out_x3; j += in_x3 - in_x2 ) {
1631                                 int w = MIN(in_x3 - in_x2, out_x3 - j);
1632                                 if(out_x3 - j > 0)
1633                                         draw_bitmap(temp_bitmap, 0,
1634                                                 x + j, y + i, w, h,
1635                                                 in_x2, in_y2, w, h,
1636                                                 dst);
1637                         }
1638                 }
1639         }
1640
1641 // Segment 6 * n
1642         for( int i = out_y2; i < out_y3; i += in_y_third ) {
1643                 if( out_y3 - i > 0 ) {
1644                         int h = MIN(in_y_third, out_y3 - i);
1645                         draw_bitmap(temp_bitmap, 0,
1646                                 x + out_x3, y + i, out_x4 - out_x3, h,
1647                                 in_x3, in_y2, in_x4 - in_x3, h,
1648                                 dst);
1649                 }
1650         }
1651
1652 // Segment 7
1653         draw_bitmap(temp_bitmap, 0,
1654                 x + out_x1, y + out_y3, out_x2 - out_x1, out_y4 - out_y3,
1655                 in_x1, in_y3, in_x2 - in_x1, in_y4 - in_y3,
1656                 dst);
1657
1658 // Segment 8 * n
1659         for( int i = out_x2; i < out_x3; i += in_x_third ) {
1660                 if( out_x3 - i > 0 ) {
1661                         int w = MIN(in_x_third, out_x3 - i);
1662                         draw_bitmap(temp_bitmap, 0,
1663                                 x + i, y + out_y3, w, out_y4 - out_y3,
1664                                 in_x2, in_y3, w, in_y4 - in_y3,
1665                                 dst);
1666                 }
1667         }
1668
1669 // Segment 9
1670         draw_bitmap(temp_bitmap, 0,
1671                 x + out_x3, y + out_y3, out_x4 - out_x3, out_y4 - out_y3,
1672                 in_x3, in_y3, in_x4 - in_x3, in_y4 - in_y3,
1673                 dst);
1674 }
1675