summaryrefslogtreecommitdiff
path: root/libtemplate/templatepixmap.c
blob: 52ae63f4810ac17383fd58552edd86376e0daca9 (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
#include "templatepixmap.h"

using namespace std;

// --- cTemplatePixmap -------------------------------------------------------------

cTemplatePixmap::cTemplatePixmap(void) {
    parameters = NULL;
    containerX = 0;
    containerY = 0;
    containerWidth = 0;
    containerHeight = 0;
    globals = NULL;
    scrolling = false;
}

cTemplatePixmap::~cTemplatePixmap() {
    for (vector<cTemplateFunction*>::iterator it = functions.begin(); it != functions.end(); it++) {
        delete (*it);
    }
    if (parameters)
        delete parameters;
}

void cTemplatePixmap::SetParameters(vector<pair<string, string> > &params) {
    parameters = new cTemplateFunction(ftPixmap);
    parameters->SetGlobals(globals);
    parameters->SetParameters(params);
}

void cTemplatePixmap::SetContainer(int x, int y, int w, int h) { 
    containerX = x;
    containerY = y;
    containerWidth = w; 
    containerHeight = h;
}

void cTemplatePixmap::SetWidth(int width) {
    cString pWidth = cString::sprintf("%d", width);
    parameters->SetWidthManually(*pWidth);
}

void cTemplatePixmap::SetHeight(int height) {
    cString pHeight = cString::sprintf("%d", height);
    parameters->SetHeightManually(*pHeight);
}

void cTemplatePixmap::SetX(int x) {
    parameters->SetXManually(x);
}

void cTemplatePixmap::SetY(int y) {
    parameters->SetYManually(y);
}

void cTemplatePixmap::SetWidthPercent(double width) {
    int absWidth = containerWidth * width;
    cString pWidth = cString::sprintf("%d", absWidth);
    parameters->SetWidthManually(*pWidth);    
}

void cTemplatePixmap::SetHeightPercent(double height) {
    int absHeight = containerHeight * height;
    cString pHeight = cString::sprintf("%d", absHeight);
    parameters->SetHeightManually(*pHeight);    
}

void cTemplatePixmap::SetXPercent(double x) {
    int absX = containerX + containerWidth * x;
    parameters->SetXManually(absX);
}

void cTemplatePixmap::SetYPercent(double y) {
    int absY = containerY + containerHeight * y;
    parameters->SetYManually(absY);
}

void cTemplatePixmap::ClearDynamicParameters(void) {
    parameters->ClearDynamicParameters();
}

void cTemplatePixmap::ParseDynamicParameters(map <string,int> *intTokens, bool initFuncs) {
    parameters->ClearDynamicParameters();
    parameters->SetIntTokens(intTokens); 
    parameters->ParseParameters();
    parameters->UnsetIntTokens();
    
    if (!DoExecute()) {
        parameters->ClearDynamicParameters();
        return;
    }

    if (!initFuncs || !Ready())
        return;

    int x = parameters->GetNumericParameter(ptX);
    int y = parameters->GetNumericParameter(ptY);
    int width = parameters->GetNumericParameter(ptWidth);
    int height = parameters->GetNumericParameter(ptHeight);

    for (vector<cTemplateFunction*>::iterator func = functions.begin(); func != functions.end(); func++) {
        (*func)->SetContainer(x, y, width, height);
        (*func)->ReCalculateParameters();
        (*func)->CompleteParameters();
        if ((*func)->GetType() == ftLoop) {
            cTemplateLoopFunction *loopFunc = dynamic_cast<cTemplateLoopFunction*>(*func);
            if (!loopFunc->Ready()) {
                loopFunc->CalculateParameters();
                loopFunc->SetIntTokens(intTokens);
                loopFunc->ParseParameters();
                loopFunc->UnsetIntTokens();
            }
            loopFunc->CalculateLoopFuncParameters();
        }
    }
}

void cTemplatePixmap::AddFunction(string name, vector<pair<string, string> > &params) {
    eFuncType type = ftNone;

    if (!name.compare("fill")) {
        type = ftFill;
    } else if (!name.compare("drawtext")) {
        type = ftDrawText;
    } else if (!name.compare("drawtextbox")) {
        type = ftDrawTextBox;
    } else if (!name.compare("drawtextvertical")) {
        type = ftDrawTextVertical;
    } else if (!name.compare("drawimage")) {
        type = ftDrawImage;
    } else if (!name.compare("drawrectangle")) {
        type = ftDrawRectangle;
    } else if (!name.compare("drawellipse")) {
        type = ftDrawEllipse;
    } else if (!name.compare("drawslope")) {
        type = ftDrawSlope;
    }

    if (type == ftNone) {
        return;
    }

    cTemplateFunction *f = new cTemplateFunction(type);
    f->SetParameters(params);
    functions.push_back(f);
}

void cTemplatePixmap::AddLoopFunction(cTemplateLoopFunction *lf) {
    functions.push_back(lf);
}


bool cTemplatePixmap::CalculateParameters(void) {
    bool paramsValid = true;
    //Calculate Pixmap Size
    parameters->SetContainer(containerX, containerY, containerWidth, containerHeight);
    parameters->SetGlobals(globals);
    paramsValid = parameters->CalculateParameters();

    int pixWidth = parameters->GetNumericParameter(ptWidth);
    int pixHeight = parameters->GetNumericParameter(ptHeight);

    for (vector<cTemplateFunction*>::iterator func = functions.begin(); func != functions.end(); func++) {
        (*func)->SetGlobals(globals);
        (*func)->SetContainer(0, 0, pixWidth, pixHeight);
        paramsValid = (*func)->CalculateParameters();
        (*func)->CompleteParameters();
        if ((*func)->GetType() == ftLoop) {
            cTemplateLoopFunction *loopFunc = dynamic_cast<cTemplateLoopFunction*>(*func);
            loopFunc->CalculateLoopFuncParameters();
        }
    }

    return paramsValid;
}

void cTemplatePixmap::ClearDynamicFunctionParameters(void) {
    InitIterator();
    cTemplateFunction *func = NULL;
    while(func = GetNextFunction()) {
        func->ClearDynamicParameters();
    }
}

void cTemplatePixmap::ParseDynamicFunctionParameters(map <string,string> *stringTokens, map <string,int> *intTokens) {
    InitIterator();
    cTemplateFunction *func = NULL;
    bool completelyParsed = true;
    bool updated = false;
    while(func = GetNextFunction()) {
        func->SetStringTokens(stringTokens);
        func->SetIntTokens(intTokens);
        bool funcCompletelyParsed = func->ParseParameters();
        if (!funcCompletelyParsed)
            completelyParsed = false;
        if (func->Updated())
            func->CompleteParameters();
        func->UnsetIntTokens();
        func->UnsetStringTokens();
    }

    if (completelyParsed) {
        return;
    }

    ReplaceWidthFunctions();
    ReplaceHeightFunctions();
    ReplacePosXFunctions();
    ReplacePosYFunctions();
}

bool cTemplatePixmap::CalculateDrawPortSize(cSize &size, map < string, vector< map< string, string > > > *loopTokens) {
    int pixWidth = parameters->GetNumericParameter(ptWidth);
    int pixHeight = parameters->GetNumericParameter(ptHeight);
    int orientation = parameters->GetNumericParameter(ptOrientation);
    if (orientation < 0)
        orientation = orVertical;
    if (orientation == orHorizontal) {
        //get function which determinates drawport width 
        cTemplateFunction *scrollFunc = GetScrollFunction();
        if (!scrollFunc)
            return false;
        int drawportWidth = scrollFunc->GetWidth(false) + scrollFunc->GetNumericParameter(ptX) + 10;
        if (drawportWidth > pixWidth) {
            size.SetWidth(drawportWidth);
            size.SetHeight(pixHeight);
            return true;
        }
    } else if (orientation == orVertical) {
        //check "last" element height
        InitIterator();
        cTemplateFunction *f = NULL;
        int drawportHeight = 1;
        while (f = GetNextFunction()) {
            if (f->GetType() == ftLoop) {
                cTemplateLoopFunction *loopFunc = dynamic_cast<cTemplateLoopFunction*>(f);
                //get number of loop tokens
                string loopTokenName = loopFunc->GetParameter(ptName);
                int numLoopTokens = 0;
                map < string, vector< map< string, string > > >::iterator hit = loopTokens->find(loopTokenName);
                if (hit != loopTokens->end()) {
                    vector< map<string,string> > loopToken = hit->second;
                    numLoopTokens = loopToken.size();
                    //parse first loop token element to get correct height
                    vector< map<string,string> >::iterator firstLoopToken = loopToken.begin();
                    loopFunc->ClearDynamicParameters();
                    loopFunc->ParseDynamicParameters(&(*firstLoopToken));
                }
                int orientation = loopFunc->GetNumericParameter(ptOrientation);
                int yFunc = loopFunc->GetNumericParameter(ptY);
                int heightFunc = loopFunc->GetLoopElementsHeight();
                if (loopTokens && orientation == orVertical) {
                    //height is height of loop elements times num loop elements
                    heightFunc = heightFunc * numLoopTokens;
                } else if (loopTokens && orientation == orHorizontal) {
                    int overflow = loopFunc->GetNumericParameter(ptOverflow);
                    if (overflow == otCut) {
                        //do nothing, height is only height of one line
                    } else if (overflow == otWrap) {
                        int widthFunc = loopFunc->GetLoopElementsWidth();
                        if (widthFunc <= 0)
                            continue;
                        int loopWidth = loopFunc->GetNumericParameter(ptWidth);
                        if (loopWidth <= 0)
                            loopWidth = loopFunc->GetContainerWidth();
                        int elementsPerRow = loopWidth / widthFunc;
                        int rest = loopWidth % widthFunc;
                        if (rest > 0)
                            elementsPerRow++;
                        if (elementsPerRow <= 0)
                            continue;
                        int lines = numLoopTokens / elementsPerRow;
                        rest = numLoopTokens % elementsPerRow;
                        if (rest > 0)
                            lines++;
                        heightFunc = heightFunc * lines;  
                    }
                }
                int neededHeight = heightFunc + yFunc;
                if (neededHeight > drawportHeight)
                    drawportHeight = neededHeight;
            } else {
                int yFunc = f->GetNumericParameter(ptY);
                int heightFunc = f->GetHeight();
                int neededHeight = heightFunc + yFunc;
                if (neededHeight > drawportHeight)
                    drawportHeight = neededHeight;
            }
        }
        if (drawportHeight > pixHeight) {
            size.SetWidth(pixWidth);
            size.SetHeight(drawportHeight);
            return true;
        }
    }
    size.SetWidth(0);
    size.SetHeight(0);
    return false;
}

void cTemplatePixmap::SetScrollingTextWidth(void) {
    int orientation = parameters->GetNumericParameter(ptOrientation);
    if (orientation != orHorizontal)
        return;
    int pixWidth = parameters->GetNumericParameter(ptWidth);
    InitIterator();
    cTemplateFunction *func = NULL;
    while(func = GetNextFunction()) {
        if (func->GetType() == ftDrawText) {
            int offset = func->GetNumericParameter(ptX);
            func->SetMaxTextWidth(pixWidth - offset);
        }
    }        
}


cTemplateFunction *cTemplatePixmap::GetScrollFunction(void) {
    string scrollElement = parameters->GetParameter(ptScrollElement);
    if (scrollElement.size() == 0)
        return NULL;
    InitIterator();
    cTemplateFunction *f = NULL;
    bool foundElement = false;
    while (f = GetNextFunction()) {
        string funcName = f->GetParameter(ptName);
        if (!funcName.compare(scrollElement)) {
            return f;
        }
    }
    return NULL;
}

cRect cTemplatePixmap::GetPixmapSize(void) {
    cRect size;
    size.SetX(GetNumericParameter(ptX));
    size.SetY(GetNumericParameter(ptY));
    size.SetWidth(GetNumericParameter(ptWidth));
    size.SetHeight(GetNumericParameter(ptHeight));
    return size;
}

int cTemplatePixmap::GetNumericParameter(eParamType type) {
    if (!parameters)
        return -1;
    return parameters->GetNumericParameter(type);
}

void cTemplatePixmap::InitIterator(void) {
    funcIt = functions.begin();
}

cTemplateFunction *cTemplatePixmap::GetNextFunction(void) {
    if (funcIt == functions.end())
        return NULL;
    cTemplateFunction *func = *funcIt;
    funcIt++;
    return func;
}

bool cTemplatePixmap::Ready(void) {
    int myX = parameters->GetNumericParameter(ptX);
    if (myX < 0)
        return false;
    int myY = parameters->GetNumericParameter(ptY);
    if (myY < 0)
        return false;
    int myWidth = parameters->GetNumericParameter(ptWidth);
    if (myWidth < 1)
        return false;
    int myHeight = parameters->GetNumericParameter(ptHeight);
    if (myHeight < 1)
        return false;
    return true;
}

void cTemplatePixmap::ReplaceWidthFunctions(void) {
    InitIterator();
    cTemplateFunction *func = NULL;
    while(func = GetNextFunction()) {
        if (func->ParsedCompletely()) {
            continue;
        }
        multimap<eParamType,string> widths;
        func->GetNeededWidths(&widths);
        for (map<eParamType, string>::iterator names = widths.begin(); names !=widths.end(); names++) {
            eParamType type = names->first;
            string label = names->second;
            int funcWidth = 0;
            for (vector<cTemplateFunction*>::iterator it = functions.begin(); it != functions.end(); it++) {
                cTemplateFunction *myFunc = *it;
                string myFuncName = myFunc->GetParameter(ptName); 
                if (!myFuncName.compare(label)) {
                    funcWidth = myFunc->GetWidth();
                    func->SetWidth(type, label, funcWidth);
                    if (func->Updated()) {
                        func->CompleteParameters();
                    }
                }
            }
        }
    }
}

void cTemplatePixmap::ReplaceHeightFunctions(void) {
    InitIterator();
    cTemplateFunction *func = NULL;
    while(func = GetNextFunction()) {
        if (func->ParsedCompletely()) {
            continue;
        }
        multimap<eParamType,string> heights;
        func->GetNeededHeights(&heights);
        for (map<eParamType, string>::iterator names = heights.begin(); names !=heights.end(); names++) {
            eParamType type = names->first;
            string label = names->second;
            int funcHeight = 0;
            for (vector<cTemplateFunction*>::iterator it = functions.begin(); it != functions.end(); it++) {
                cTemplateFunction *myFunc = *it;
                string myFuncName = myFunc->GetParameter(ptName); 
                if (!myFuncName.compare(label)) {
                    funcHeight = myFunc->GetHeight();
                    func->SetHeight(type, label, funcHeight);
                    if (func->Updated()) {
                        func->CompleteParameters();
                    }
                }
            }
        }
    }
}

void cTemplatePixmap::ReplacePosXFunctions(void) {
    InitIterator();
    cTemplateFunction *func = NULL;
    while(func = GetNextFunction()) {
        if (func->ParsedCompletely()) {
            continue;
        }
        multimap<eParamType,string> posXs;
        func->GetNeededPosX(&posXs);
        for (map<eParamType, string>::iterator names = posXs.begin(); names !=posXs.end(); names++) {
            eParamType type = names->first;
            string label = names->second;
            int funcX = 0;
            for (vector<cTemplateFunction*>::iterator it = functions.begin(); it != functions.end(); it++) {
                cTemplateFunction *myFunc = *it;
                string myFuncName = myFunc->GetParameter(ptName); 
                if (!myFuncName.compare(label)) {
                    funcX = myFunc->GetNumericParameter(ptX);
                    if (funcX > -1) {
                        func->SetX(type, label, funcX);
                        if (func->Updated()) {
                            func->CompleteParameters();
                        }
                    }
                }
            }
        }
    }
}

void cTemplatePixmap::ReplacePosYFunctions(void) {
    InitIterator();
    cTemplateFunction *func = NULL;
    while(func = GetNextFunction()) {
        if (func->ParsedCompletely()) {
            continue;
        }
        multimap<eParamType,string> posYs;
        func->GetNeededPosY(&posYs);
        for (map<eParamType, string>::iterator names = posYs.begin(); names !=posYs.end(); names++) {
            eParamType type = names->first;
            string label = names->second;
            int funcY = 0;
            for (vector<cTemplateFunction*>::iterator it = functions.begin(); it != functions.end(); it++) {
                cTemplateFunction *myFunc = *it;
                string myFuncName = myFunc->GetParameter(ptName);
                if (!myFuncName.compare(label)) {
                    funcY = myFunc->GetNumericParameter(ptY);
                    if (funcY > -1) {
                        func->SetY(type, label, funcY);
                        if (func->Updated()) {
                            func->CompleteParameters();
                        }
                    }
                }
            }
        }
    }
}

void cTemplatePixmap::Debug(void) {
    esyslog("skindesigner: pixmap container size x: %d, y: %d, width: %d, height %d", containerX, containerY, containerWidth, containerHeight);
    parameters->Debug();
    for (vector<cTemplateFunction*>::iterator it = functions.begin(); it != functions.end(); it++) {
        (*it)->Debug();
    }
}