Root/src/link.cpp

1/***************************************************************************
2 * Copyright (C) 2006 by Massimiliano Torromeo *
3 * massimiliano.torromeo@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20#include <fstream>
21#include <sstream>
22#include "link.h"
23#include "menu.h"
24#include "selector.h"
25
26using namespace std;
27using namespace fastdelegate;
28
29Link::Link(GMenu2X *gmenu2x_)
30    : Button(gmenu2x_->ts, true)
31    , gmenu2x(gmenu2x_)
32{
33    action = MakeDelegate(this, &Link::run);
34    edited = false;
35    iconPath = gmenu2x->sc.getSkinFilePath("icons/generic.png");
36    iconX = 0;
37    padding = 0;
38
39    updateSurfaces();
40}
41
42void Link::run() {}
43
44void Link::paint() {
45    iconSurface->blit(gmenu2x->s, iconX, rect.y+padding, 32,32);
46    gmenu2x->s->write( gmenu2x->font, getTitle(), iconX+16, rect.y+gmenu2x->skinConfInt["linkHeight"]-padding, ASFont::HAlignCenter, ASFont::VAlignBottom );
47}
48
49bool Link::paintHover() {
50    if (gmenu2x->useSelectionPng)
51        gmenu2x->sc["imgs/selection.png"]->blit(gmenu2x->s,rect,ASFont::HAlignCenter,ASFont::VAlignMiddle);
52    else
53        gmenu2x->s->box(rect.x, rect.y, rect.w, rect.h, gmenu2x->skinConfColors[COLOR_SELECTION_BG]);
54    return true;
55}
56
57void Link::updateSurfaces()
58{
59    iconSurface = gmenu2x->sc[getIconPath()];
60}
61
62const string &Link::getTitle() {
63    return title;
64}
65
66void Link::setTitle(const string &title) {
67    this->title = title;
68    edited = true;
69}
70
71const string &Link::getDescription() {
72    return description;
73}
74
75void Link::setDescription(const string &description) {
76    this->description = description;
77    edited = true;
78}
79
80const string &Link::getIcon() {
81    return icon;
82}
83
84void Link::setIcon(const string &icon) {
85    string skinpath = gmenu2x->getExePath()+"skins/"+gmenu2x->confStr["skin"];
86
87    if (icon.substr(0,skinpath.length()) == skinpath) {
88        string tempIcon = icon.substr(skinpath.length(), icon.length());
89        string::size_type pos = tempIcon.find("/");
90        if (pos != string::npos)
91            this->icon = "skin:"+tempIcon.substr(pos+1,icon.length());
92        else
93            this->icon = icon;
94    } else {
95        this->icon = icon;
96    }
97
98    iconPath = strreplace(this->icon,"skin:",skinpath+"/");
99    if (iconPath.empty() || !fileExists(iconPath)) {
100        iconPath = strreplace(this->icon,"skin:",gmenu2x->getExePath()+"skins/Default/");
101        if (!fileExists(iconPath)) searchIcon();
102    }
103
104    edited = true;
105    updateSurfaces();
106}
107
108const string &Link::searchIcon() {
109    iconPath = gmenu2x->sc.getSkinFilePath("icons/generic.png");
110    return iconPath;
111}
112
113const string &Link::getIconPath() {
114    if (iconPath.empty()) searchIcon();
115    return iconPath;
116}
117
118void Link::setIconPath(const string &icon) {
119    if (fileExists(icon))
120        iconPath = icon;
121    else
122        iconPath = gmenu2x->sc.getSkinFilePath("icons/generic.png");
123    updateSurfaces();
124}
125
126void Link::setSize(int w, int h) {
127    Button::setSize(w,h);
128    recalcCoordinates();
129}
130
131void Link::setPosition(int x, int y) {
132    Button::setPosition(x,y);
133    recalcCoordinates();
134}
135
136void Link::recalcCoordinates() {
137    iconX = rect.x+(rect.w-32)/2;
138    padding = (gmenu2x->skinConfInt["linkHeight"] - 32 - gmenu2x->font->getLineHeight()) / 3;
139}
140

Archive Download this file



interactive