Am 29.09.2009 13:30, schrieb Michael Schmid:
Hi!
Is there any way to have a Fl_Button with a image in it and a text on
this image? The text should be changeable, so I can't put the text onto
the picture directly... :-(
It seems to me, that FLTK 1.1.9 doesn't support this. Anybody ever done
this?
Yes, but it is not as simple as can be, because I used an existing class
for loading images. Files appended, think you will like the language. ;o)
Fl_Grafik is a simple wrapper for loading Fl_Images and Fl_Bn_Grafik
uses it.
HTH,
Ed
//----------------------------------------------------------------------------
// Projekt TANtec Studio
//
// Fl_Bn_Grafik.h
// - Schalter mit Icon und Beschriftung
//
// Copyright © 2007 by Edzard Egberts
//
// 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.
// Static linking of this library (or any modified version) to your software
// is explicitly allowed.
//
// 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; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//----------------------------------------------------------------------------
#include <string>
#include <FL/Fl.H>
#include <FL/Fl_Button.H>
#include <FL/fl_draw.H>
#include "Fl_Bn_Grafik.h"
//-------------------------------------------------------------------------------------
Fl_Bn_Grafik::Fl_Bn_Grafik(int X, int Y, int W, int H, const char* label):
Fl_Button(X, Y, W, H), m_Taste(0)
{ // Label wird in eigenen Speicher übernommen
if (label && *label)
{ // Übernehmen, wenn keine Nullzeiger und nicht leer
m_Label= label;
Fl_Button::label(m_Label.c_str());
}
}
//-------------------------------------------------------------------------------------
void Fl_Bn_Grafik::load(const char* Picname)
{
Fl_Grafik::load(Picname);
redraw();
}
//-------------------------------------------------------------------------------------
void Fl_Bn_Grafik::label(const char* Text)
{
m_Label= Text;
Fl_Button::label(m_Label.c_str());
}
//-------------------------------------------------------------------------------------
void Fl_Bn_Grafik::clear()
{
Fl_Grafik::clear();
redraw();
}
//-------------------------------------------------------------------------------------
void Fl_Bn_Grafik::draw()
{ // Die Bitmap wird beim Zeichnen so skaliert, dass sie den zur Verfügung
// stehenden Raum vollständig ausfüllt.
if (type() == FL_HIDDEN_BUTTON) return;
Fl_Color col = value() ? selection_color() : color();
draw_box(value() ? (down_box()?down_box():fl_down(box())) : box(), col);
if (Pic_w() && Pic_h())
{ // Bild ist erfolgreich geladen
int ox= 0; // Offset
int oy= 0;
int nw= w(); // Neue Breite
int nh= h(); // Neue Höhe
double f1= double(h())/double(w());
double f2= double(Pic_h())/ double(Pic_w());
if (f1 > f2)
{ // Bitmap ist zu breit und wird auf Breite eingepaßt,
so dass
// oben und unten eine Lücke entsteht
nh= int((Pic_h()*w())/Pic_w()); // Höhe ist
also von Fensterbreite abhängig
int Offset= (h() - nh)/2;
// Differenz zwischen Fenster und Maphöhe
if (Offset> 0) oy= Offset;
// Bitmap nach unten zentrieren
}
else
{ // Bitmap ist zu hoch und wird auf Höhe eingepaßt, so
dass rechts und links
// eine Lücke entsteht
nw= int((Pic_w() *h())/ Pic_h()); // Breite ist
also von Fensterhöhe abhängig
ox= int((w() -nw)/2); // zentrieren
}
const int d= 3; // 3 Pixel Abstand vom Rand einhalten
Fl_Image* Scale= mp_Pic->copy(nw - 2*d, nh - 2*d);
Scale->draw(x() +ox +d, y() + oy +d, w()- 2*d, h()- 2*d);
delete Scale;
}
// Label zeichnen:
if (!m_Label.empty())
{
fl_font(labelfont(), labelsize());
// Labelfont setzen
int aw= 0;
int ah= 0;
fl_color(FL_WHITE);
//
Texthintergrund löschen
if (align() & FL_ALIGN_WRAP) aw= w();
// Breite zum Umbrechen übergeben
else
aw= 0; //
Nicht umbrechen
std::string Txt= ' ' + m_Label + ' ';
fl_measure(Txt.c_str(), aw, ah);
// Ausgaberechteck berechnen
int ax= x();
//
Linksbündig anordnen
int ay= y();
// Top
anordnen
// Horizontal ausrichten
if (align() & FL_ALIGN_RIGHT) ax+=
w() - aw; // Rechtsbündig anordnen
else if (!(align() & FL_ALIGN_LEFT)) ax+= (w() - aw)/2;
// Horizontal zentrieren
if (align() & FL_ALIGN_BOTTOM) ay+= h() - ah;
// Nach unten ausrichten
else if (!(align() & FL_ALIGN_TOP)) ay+= (h() -
ah)/2; // Vertikal zentrieren
fl_rectf(ax, ay, aw, ah);
fl_color(FL_BLACK);
fl_rect(ax, ay, aw, ah);
if (value()) {
Fl_Color c = labelcolor();
labelcolor(fl_contrast(c, col));
fl_draw(Txt.c_str(), x(), y(), w(), h(), align());
labelcolor(c);
} else fl_draw(Txt.c_str(), x(), y(), w(), h(), align());
}
if (m_Taste)
{ // Kreis für Tasteneingaben ohne Touch anzeigen
int S= 15;
fl_color(FL_YELLOW);
fl_pie(x(), y(), 3*S, 3*S, 0, 360);
fl_color(FL_BLACK);
fl_arc(x(), y(), 3*S, 3*S, 0, 360);
fl_font(labelfont(), 2*S);
char str[2]; str[0]= m_Taste; str[1]= 0;
fl_draw(&str[0], x(), y(), 3*S, 3*S, FL_ALIGN_CENTER);
}
fl_color(Fl_Button::color());
if (Fl::focus() == this) draw_focus();
}
//----------------------------------------------------------------------------
// Projekt TANtec Studio
//
// Fl_Bn_Grafik.h
// - Schalter mit Icon und Beschriftung
//
// Copyright © 2007 by Edzard Egberts
//
// 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.
// Static linking of this library (or any modified version) to your software
// is explicitly allowed.
//
// 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; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//----------------------------------------------------------------------------
#ifndef FL_BN_GRAFIK_H
#define FL_BN_GRAFIK_H
#include <string>
#include <FL/Fl_Button.H>
#include <FL/fl_draw.H>
#include "Fl_Grafik.h"
class Fl_Bn_Grafik: public Fl_Button, public Fl_Grafik
{
public:
Fl_Bn_Grafik(int X, int Y, int W, int H, const char* label= "");
virtual ~Fl_Bn_Grafik() {}
inline std::string label() const {
return m_Label; }
inline void Taste(char Zeichen) {
m_Taste= Zeichen; }
inline char Taste() const
{ return m_Taste; }
void load(const char* Picname);
void label(const char* Text);
void clear();
protected:
std::string m_Label;
char m_Taste;
// Taste für Eingabe ohne Touchscreen anzeigen
virtual void draw();
friend class Fl_Pic_Browser;
};
#endif // FL_BN_GRAFIK_H
#include <locale> // für str_lower, str_upper
#include <fstream>
#include <FL/Fl_JPEG_Image.H>
#include <FL/Fl_PNG_Image.H>
#include <FL/Fl_GIF_Image.H>
#include <FL/Fl_BMP_Image.H>
#include "Fl_Grafik.h"
//------------------------------------------------------------------------------------
Fl_Grafik::Fl_Grafik(std::string Name):
m_Owner(true),
mp_Pic(0)
{
if (!Name.empty()) load(Name.c_str());
}
//------------------------------------------------------------------------------------
int Fl_Grafik::Pic_w() const
{
if (mp_Pic) return mp_Pic->w();
else return 0;
}
//------------------------------------------------------------------------------------
int Fl_Grafik::Pic_h() const
{
if (mp_Pic) return mp_Pic->h();
else return 0;
}
//------------------------------------------------------------------------------------
void Fl_Grafik::Show_Pic(Fl_Image* pPic)
{
if (m_Owner) delete mp_Pic;
m_Owner= false;
mp_Pic= pPic;
}
//------------------------------------------------------------------------------------
void Fl_Grafik::clear()
{
if (m_Owner) delete mp_Pic;
mp_Pic= 0;
}
//------------------------------------------------------------------------------------
bool Fl_Grafik::load(std::string Name)
{
if (m_Owner) delete mp_Pic;
mp_Pic= 0;
m_Owner= true;
if (!Name.empty() && std::ifstream(Name.c_str()))
{ // Ein Name muss angegeben sein und die Datei muss existieren.
// Beachte: Die Grafikkonstruktoren liefern immer ein Bild,
wenn die Datei
// nicht geöffnet werden konnte, wird das zuletzt geladene Bild
angezeigt,
// deshalb muss separat auf Vorhandensein der Datei geprüft
werden
unsigned Pos= Name.find_last_of('.') + 1;
if (Pos!= std::string::npos && Pos < Name.size())
{ // Dateiname mit Endung für Grafiktyp
std::string Endung= Name.substr(Pos, Name.size() -
Pos);
for (std::string::iterator iter = Endung.begin(); iter
!= Endung.end();
++iter) *iter = std::tolower(*iter, std::locale());
// Endung ausschneiden und to_lower
if (Endung== "jpg" || Endung== "jpeg")
mp_Pic = new Fl_JPEG_Image(Name.c_str());
else if (Endung== "png") mp_Pic = new
Fl_PNG_Image(Name.c_str());
else if (Endung== "gif") mp_Pic = new
Fl_GIF_Image(Name.c_str());
else if (Endung== "bmp") mp_Pic = new
Fl_BMP_Image(Name.c_str());
}
}
return mp_Pic && mp_Pic->w() && mp_Pic->h();
}
//----------------------------------------------------------------------------
// Projekt TANtec Studio
//
// Fl_Grafik.h
// - Wrapper für Fl_Image zum Laden verschiedener Grafiktypen von Festplatte
//
// Copyright © 2003 by Edzard Egberts
//
// 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.
// Static linking of this library (or any modified version) to your software
// is explicitly allowed.
//
// 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; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//----------------------------------------------------------------------------
#ifndef FL_BITMAP_H
#define FL_BITMAP_H
#include <string>
#include <FL/Fl_Image.H>
class Fl_Grafik
{
public:
Fl_Grafik(std::string Name= "");
inline Fl_Image* Pic()
{ return mp_Pic; }
inline const Fl_Image* Pic() const { return
mp_Pic; }
inline bool empty() const
{ return !Pic_w() || !Pic_h(); }
bool load(std::string Name);
// Owner = true
void clear();
void Show_Pic(Fl_Image* pPic); //
Owner = false
int Pic_w() const;
int Pic_h() const;
protected:
bool m_Owner; // mp_Pic
gehört Fl_Grafik oder wird nur angezeigt
Fl_Image* mp_Pic;
};
#endif // FL_BITMAP_H
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk