summaryrefslogtreecommitdiffstats
path: root/kjsembed/bindings/painter_imp.cpp
blob: 184529dfee0ab0949beadf0d3fd172cbeecffd91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
// -*- c++ -*-

/*
 *  Copyright (C) 2003, Richard J. Moore <rich@kde.org>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 */

#include <tqpainter.h>
#include <tqpixmap.h>
#include <tqimage.h>
#include <kjsembed/jsopaqueproxy.h>
#include <kjsembed/jsobjectproxy.h>
#include <kjsembed/jsvalueproxy.h>
#include <kjsembed/jsbinding.h>
#include <tqfont.h>
#include <kjsembed/global.h>

#include "painter_imp.h"

namespace KJSEmbed {
namespace Bindings {

    KJS::Object PainterLoader::createBinding(KJSEmbedPart *jspart, KJS::ExecState *exec, const KJS::List &args) const
    {
        PainterRef * p = new PainterRef();
        /*
        TTQWidget *w = extractTQWidget(exec, args, 0);
        TTQPixmap pix = extractTQPixmap(exec, args, 0);
        if( w )
        {
            p->setDevice(w);
            p->setCanDelete(false);
        }
        else if( !pix.isNull() )
            p->setDevice( new TTQPixmap(pix) );
        */
        JSOpaqueProxy *prx = new JSOpaqueProxy( p, "Painter" );
        prx->setOwner( JSProxy::JavaScript );
        KJS::Object proxyObj( prx );
	Painter::addBindings( exec, proxyObj );
        return proxyObj;
    }

    void PainterLoader::addBindings( KJSEmbedPart *jspart, KJS::ExecState *exec, KJS::Object &proxy ) const
    {
        Painter::addBindings( exec, proxy );
    }
        
PainterRef::PainterRef()
{
	m_device = 0L;
	m_painter = new TTQPainter;
  m_canDelete = true;
}
PainterRef::~PainterRef()
{
	kdDebug() << "Painter ref going away..." << endl;
  if( m_device && m_canDelete)
		delete m_device;
	delete m_painter;
}

TTQPainter *PainterRef::painter()
{
	return m_painter;
}

TTQPaintDevice *PainterRef::device()
{
	return m_device;
}

void PainterRef::setDevice(TTQPaintDevice *d)
{
  if( m_device && m_canDelete)
	{
		delete m_device;
		m_device = 0L;
	}
	m_device = d;

}

void Painter::addBindings( KJS::ExecState *exec, KJS::Object &object ) {
 
    if( !JSProxy::checkType(object, JSProxy::OpaqueProxy, "Painter") ) return;
    
    JSProxy::MethodTable methods[] = {
    { Methodbegin, "begin" },
    { Methodend, "end" },
    { Methodpixmap, "pixmap" },
    { MethodsetPixmap, "setPixmap" },
    { MethodsetPen, "setPen" },
    { Methodpen, "pen" },
    { MethodtextBox, "textBox" },
    { MethodmoveTo, "moveTo" },
    { MethodlineTo, "lineTo" },
    { MethoddrawPoint, "drawPoint" },
    { MethoddrawLine, "drawLine" },
    { MethoddrawRect, "drawRect" },
    { MethoddrawRoundRect, "drawRoundRect" },
    { MethoddrawEllipse, "drawEllipse" },
    { MethoddrawText, "drawText" },
    { MethoddrawArc, "drawArc" },
    { MethoddrawPie, "drawPie" },
    { MethoddrawPixmap, "drawPixmap" },
    { MethoddrawImage, "drawImage" },
    { MethoddrawTiledPixmap, "drawTiledPixmap" },
    { Methodscale, "scale" },
    { Methodshear, "shear" },
    { Methodrotate, "rotate" },
    { Methodtranslate, "translate" },
    { MethodsetFont, "setFont" },
    { Methodfont, "font" },
    { MethodsetBrush, "setBrush" },
    { Methodbrush, "brush" },
    { MethodbackgroundColor, "backgroundColor" },
    { MethodsetBackgroundColor, "setBackgroundColor" },
    { 0, 0 }
    };

    JSProxy::addMethods<Painter>(exec, methods, object );

}

KJS::Value Painter::call( KJS::ExecState *exec, KJS::Object &self, const KJS::List &args ) {
    if( !JSProxy::checkType(self, JSProxy::OpaqueProxy, "Painter") ) return KJS::Boolean(false);
    
    KJS::Value retValue = KJS::Boolean(false);
    JSOpaqueProxy *op = JSProxy::toOpaqueProxy( self.imp() );

    pr = op->toNative<PainterRef>();

    switch ( mid ) {
    case Methodbegin:
    {
        if ( args.size() == 1 ) {
            JSObjectProxy *objp = JSProxy::toObjectProxy(args[0].imp() );
            JSValueProxy *vp = JSProxy::toValueProxy(args[0].imp() );
            JSOpaqueProxy *op = JSProxy::toOpaqueProxy(args[0].imp() );
            if ( objp )
            {
                if( objp->widget() )
                {
                    pr->setDevice( objp->widget() );
                    pr->setCanDelete(false);
                    retValue = KJS::Boolean( begin() );
                }
                else
                {
                    TTQString msg = i18n( "Paintdevice was not a valid widget." );
                    throwError(exec,msg);
                    retValue =  KJS::Boolean(false);
                }
            }
            else if ( vp )
            {
                TTQPixmap *p = new TTQPixmap(vp->toVariant().toPixmap());
                if( p->isNull() )
                {
                    TTQString msg = i18n( "Cannto paint to a null pixmap." ).arg( op->typeName()  );
                    throwError(exec,msg);
                    retValue =  KJS::Boolean(false);
                }
                else
                {
                    pr->setDevice( p );
                    pr->setCanDelete(true);
                    retValue = KJS::Boolean( begin() );
                }
            }
            else if ( op && op->typeName() == "TTQPaintDevice" )
            {
                pr->setDevice( op->toNative<TTQPaintDevice>() );
                pr->setCanDelete(false);
                retValue = KJS::Boolean( begin() );
            }
            else
            {
                TTQString msg = i18n( "Paintdevice of type '%1' is not supported." ).arg( op->typeName()  );
                throwError(exec,msg);
                retValue =  KJS::Boolean(false);
            }
        }
        else
        {
            TTQString msg = i18n( "Incorrect number of arguements '%1'." ).arg( args.size() );
            throwError(exec,msg);
            retValue =  KJS::Boolean(false);
        }
        break;
    }
    case Methodend:
        retValue = KJS::Boolean( end() );
        break;
    case Methodpixmap:
    {
	retValue = convertToValue(exec, pixmap());
        break;
    }
    case MethodsetPixmap:
        break;
    case MethodsetPen:
    {
        TTQPen pn = extractTQPen(exec, args, 0);
	setPen(pn);
        break;
    }
    case Methodpen:
    {
        retValue = convertToValue(exec, pen());
        break;
    }
    case MethodtextBox:
    {
        retValue = convertToValue(exec, textBox(extractTQString(exec, args, 0)));
	break;
    }
    case MethodmoveTo:
    {
        moveTo(extractInt(exec, args, 0), extractInt(exec, args, 1));
	break;
    }
    case MethodlineTo:
    {
        lineTo(extractInt(exec, args, 0), extractInt(exec, args, 1));
	break;
    }
    case MethoddrawPoint:
    {
        drawPoint(extractInt(exec, args, 0), extractInt(exec, args, 1));
	break;
    }
    case MethoddrawLine:
    {
        drawLine(extractInt(exec, args, 0), extractInt(exec, args, 1),extractInt(exec, args, 2), extractInt(exec, args, 3));
        break;
    }
    case MethoddrawRect:
    {
        drawRect(extractInt(exec, args, 0), extractInt(exec, args, 1),extractInt(exec, args, 2), extractInt(exec, args, 3));
	break;
    }
    case MethoddrawRoundRect:
    {
        drawRoundRect(extractInt(exec, args, 0), extractInt(exec, args, 1),extractInt(exec, args, 2), extractInt(exec, args, 3),extractInt(exec, args, 4), extractInt(exec, args, 5));
	break;
    }
    case MethoddrawEllipse:
    {
        drawEllipse(extractInt(exec, args, 0), extractInt(exec, args, 1),extractInt(exec, args, 2), extractInt(exec, args, 3));
	break;
    }
    case MethoddrawText:
    {
        drawText(extractInt(exec, args, 0), extractInt(exec, args, 1), extractTQString(exec, args, 2));
	break;
    }
    case MethoddrawArc:
    {
        drawArc(extractInt(exec, args, 0), extractInt(exec, args, 1),extractInt(exec, args, 2), extractInt(exec, args, 3),extractInt(exec, args, 4), extractInt(exec, args, 5));
	break;
    }
    case MethoddrawPie:
    {
        drawPie(extractInt(exec, args, 0), extractInt(exec, args, 1),extractInt(exec, args, 2), extractInt(exec, args, 3),extractInt(exec, args, 4), extractInt(exec, args, 5));
	break;
    }
    case MethoddrawPixmap:
    {
        drawPixmap(extractInt(exec, args, 0), extractInt(exec, args, 1), extractTQPixmap(exec, args, 2), extractInt(exec, args, 3), extractInt(exec, args, 4), extractInt(exec, args, 5), extractInt(exec, args, 6));
	break;
    }
    case MethoddrawImage:
    {
        drawImage( extractInt(exec, args, 0), extractInt(exec, args, 1), extractTQImage(exec, args, 2), extractInt(exec, args, 3), extractInt(exec, args, 4), extractInt(exec, args, 5), extractInt(exec, args, 6), extractInt(exec, args, 7) );
	break;
    }
    case MethoddrawTiledPixmap:
    {
        drawTiledPixmap(extractInt(exec, args, 0), extractInt(exec, args, 1), extractInt(exec, args, 2), extractInt(exec, args, 3), extractTQPixmap(exec, args, 4), extractInt(exec, args, 5), extractInt(exec, args, 6));
	break;
    }
    case Methodscale:
    {
        scale(extractDouble(exec, args, 0), extractDouble(exec, args, 1));
	break;
    }
    case Methodshear:
    {
        shear(extractDouble(exec, args, 0), extractDouble(exec, args, 1));
	break;
    }
    case Methodrotate:
    {
        rotate(extractDouble(exec, args, 0));
	break;
    }
    case Methodtranslate:
    {
        translate(extractDouble(exec, args, 0), extractDouble(exec, args, 1));
        break;
    }
    case MethodsetFont:
    {
        setFont(extractTQFont(exec, args, 0));
        break;
    }
    case Methodfont:
    {
        retValue = convertToValue(exec, font());
	break;
    }
    case MethodsetBrush:
    {
    	pr->painter()->setBrush(extractTQBrush(exec, args, 0));
        break;
    }
    case Methodbrush:
    {
        retValue = convertToValue(exec, brush());
	break;
    }
    case MethodbackgroundColor:
    {
        retValue = convertToValue(exec, backgroundColor());
	break;
    }
    case MethodsetBackgroundColor:
    {
        setBackgroundColor(extractTQColor(exec, args, 0));
	break;
    }
    default:
        kdWarning() << "Painter has no method " << mid << endl;
        break;
    }

    //op->setValue(pr, "Painter");
    return retValue;
}

Painter::Painter( KJS::ExecState *exec, int id )
	: JSProxyImp(exec), mid(id),  pr(0)
{
}

Painter::~Painter()
{
}

bool Painter::begin()
{
    return pr->painter()->begin(pr->device());
}
bool Painter::begin(TTQPaintDevice *dev)
{
    return pr->painter()->begin(dev);
}

bool Painter::end()
{
    return pr->painter()->end();
}

TTQPixmap Painter::pixmap() const
{
   TTQPixmap *pix = dynamic_cast<TTQPixmap *>(pr->device());
   if( pix )
   {
	return *pix;
   }
   return TTQPixmap();
}

void Painter::setPen( const TTQPen& pn )
{
    pr->painter()->setPen(pn);
}

TTQPen Painter::pen() const
{
    return pr->painter()->pen();
}

TTQRect Painter::textBox(const TTQString &text)
{
	TTQFontMetrics fnt = pr->painter()->fontMetrics();
	return fnt.boundingRect(text);
}

void Painter::moveTo( int x, int y )
{
    pr->painter()->moveTo( x, y );
}

void Painter::lineTo( int x, int y )
{
    pr->painter()->lineTo( x, y );
}

void Painter::drawPoint( int x, int y )
{
    pr->painter()->drawPoint( x, y );
}

void Painter::drawLine( int x1, int y1, int x2, int y2 )
{
    pr->painter()->drawLine( x1, y1, x2, y2 );
}

void Painter::drawRect( int x, int y, int w, int h )
{
    pr->painter()->drawRect( x, y, w, h );
}

void Painter::drawRoundRect( int x, int y, int w, int h, int xrnd, int yrnd )
{
     pr->painter()->drawRoundRect( x, y, w, h, xrnd, yrnd );
}

void Painter::drawEllipse( int x, int y, int w, int h )
{
    pr->painter()->drawEllipse( x, y, w, h );
}

void Painter::drawText( int x, int y, const TTQString &txt )
{
    pr->painter()->drawText( x, y, txt );
}

void Painter::drawArc ( int x, int y, int w, int h, int a, int alen )
{
    pr->painter()->drawArc( x, y, w, h, a, alen );
}
void Painter::drawPie ( int x, int y, int w, int h, int a, int alen )
{
    pr->painter()->drawPie( x, y, w, h, a, alen);
}
void Painter::drawPixmap ( int x, int y, const TTQPixmap & pixmap, int sx, int sy, int sw, int sh )
{
    pr->painter()->drawPixmap(  x, y, pixmap, sx, sy, sw, sh );
}
void Painter::drawImage ( int x, int y, const TTQImage &image, int sx , int sy, int sw , int sh, int conversionFlags)
{
    pr->painter()->drawImage( x, y, image, sx, sy, sw, sh, conversionFlags );
}
void Painter::drawTiledPixmap ( int x, int y, int w, int h, const TTQPixmap & pixmap, int sx, int sy )
{
    pr->painter()->drawTiledPixmap( x, y, w, h, pixmap, sx, sy );
}

void Painter::scale ( double sx, double sy )
{
    pr->painter()->scale( sx,sy );
}
void Painter::shear ( double sh, double sv )
{
    pr->painter()->shear( sh, sv );
}
void Painter::rotate ( double a )
{
    pr->painter()->rotate( a );
}
void Painter::translate ( double dx, double dy )
{
    pr->painter()->translate( dx, dy );
}

void KJSEmbed::Bindings::Painter::setFont( const TTQFont & font )
{
	pr->painter()->setFont(font);
}

TTQFont KJSEmbed::Bindings::Painter::font( ) const
{
	return pr->painter()->font();
}

TTQColor KJSEmbed::Bindings::Painter::backgroundColor( ) const
{
	return pr->painter()->backgroundColor();
}

void KJSEmbed::Bindings::Painter::setBackgroundColor( const TTQColor & color )
{
	pr->painter()->setBackgroundColor(color);
}

} // namespace Bindings
} // namespace KJSEmbed


void KJSEmbed::Bindings::Painter::setBrush( const TTQColor & brush )
{
	pr->painter()->setBrush(brush);
}

TTQColor KJSEmbed::Bindings::Painter::brush( ) const
{
	return pr->painter()->brush().color();
}
// Local Variables:
// c-basic-offset: 4
// End: