📄 button.cpp
字号:
// Button.cpp: implementation of the CButton class.
//
//////////////////////////////////////////////////////////////////////
#include <gtk/gtk.h>
#include "Button.h"
#include "Pixmap.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CButton::CButton(gchar* label, CWidget* parent) : CWidget(parent)
{
m_Label = NULL;
m_pix1 = m_pix2 = (CPixmap*)NULL;
m_bState = false;
m_hHandle = Create(label);
SetData("Wellgain Objects", this);
gtk_signal_connect (GTK_OBJECT (m_hHandle), "destroy",
GTK_SIGNAL_FUNC (on_widget_destroy),
this);
Show();
}
CButton::~CButton()
{
if(m_pix1)
delete m_pix1;
m_pix1 = NULL;
if(m_pix2)
delete m_pix2;
m_pix2 = NULL;
if(m_Label)
delete m_Label;
m_Label = NULL;
}
GtkWidget* CButton::Create(gchar* label)
{
GtkWidget* but;
but = gtk_button_new();
if(label){
m_Label = new CLabel(label, this);
gtk_container_add (GTK_CONTAINER (but), m_Label->GetHandle());
}
return but;
}
const char* CButton::GetLabel() const
{
if(m_Label)
return m_Label->GetText();
return NULL;
}
void CButton::SetLabel(const char* szText)
{
if(m_Label)
m_Label->SetText(szText);
}
void CButton::SetPixLabel(const gchar *xpm_filename,
const gchar *label_text )
{
if(!m_hHandle)
return ;
GtkWidget *box1;
GtkWidget *pixmapwid;
if(GetChild()){
gtk_container_remove(GTK_CONTAINER(m_hHandle), GetChild());
if(m_Label){
delete m_Label;
m_Label = NULL;
}
}
if(!label_text){
CPixmap* pix = new CPixmap(xpm_filename);
pixmapwid = gtk_pixmap_new (pix->GetPixmap(), pix->GetMask());
gtk_widget_show(pixmapwid);
gtk_container_add (GTK_CONTAINER (m_hHandle), pixmapwid);
SetSize(pix->GetWidth(), pix->GetHeight());
delete pix;
}
else{
/* Create box for xpm and label */
box1 = gtk_hbox_new (FALSE, 0);
gtk_widget_show(box1);
gtk_container_add (GTK_CONTAINER (m_hHandle), box1);
CPixmap* pix = new CPixmap(xpm_filename);
pixmapwid = gtk_pixmap_new (pix->GetPixmap(), pix->GetMask());
delete pix;
/* Pack the pixmap and label into the box */
gtk_box_pack_start (GTK_BOX (box1),
pixmapwid, FALSE, FALSE, 1);
gtk_widget_show(pixmapwid);
/* Create a label for the button */
if(label_text){
m_Label = new CLabel(label_text, this);
gtk_box_pack_start (GTK_BOX (box1), m_Label->GetHandle(), FALSE, FALSE, 0);
}
}
}
void CButton::SetPixmap(const gchar *file_normal, const gchar *file_active, bool isToggle)
{
if(!m_hHandle)
return ;
if(m_pix1)
delete m_pix1;
m_pix1 = new CPixmap(file_normal);
if(m_pix2)
delete m_pix2;
m_pix2 = new CPixmap(file_active);
CPixmap* pix;
if(GetState())
pix = m_pix2;
else
pix = m_pix1;
GtkWidget* child = GetChild();
if(child){
if(GTK_IS_PIXMAP(child)){
gtk_pixmap_set(GTK_PIXMAP(child), pix->GetPixmap(), pix->GetMask());
return;
}
else{
gtk_container_remove(GTK_CONTAINER(m_hHandle), child);
if(m_Label){
delete m_Label;
m_Label = NULL;
}
}
}
GtkWidget* p = gtk_pixmap_new(pix->GetPixmap(), pix->GetMask());
gtk_container_add(GTK_CONTAINER(m_hHandle), p);
gtk_widget_show(p);
SetSize(pix->GetWidth(), pix->GetHeight());
gtk_signal_connect (GTK_OBJECT (GetHandle()), "pressed",
GTK_SIGNAL_FUNC (on_button_pressed),
this);
if(isToggle)
gtk_signal_connect (GTK_OBJECT (GetHandle()), "released",
GTK_SIGNAL_FUNC (on_button_released),
this);
}
void CButton::SetState(bool bActive)
{
if(!m_hHandle)
return ;
if(m_bState == bActive)
return;
m_bState = bActive;
CPixmap* pix;
if(GetState())
pix = m_pix2;
else
pix = m_pix1;
if(!pix)
return;
GtkWidget* child = GetChild();
if(child){
if(GTK_IS_PIXMAP(child)){
gtk_pixmap_set(GTK_PIXMAP(child), pix->GetPixmap(), pix->GetMask());
return;
}
else{
gtk_container_remove(GTK_CONTAINER(m_hHandle), child);
if(m_Label){
delete m_Label;
m_Label = NULL;
}
}
}
GtkWidget* p = gtk_pixmap_new(pix->GetPixmap(), pix->GetMask());
gtk_container_add(GTK_CONTAINER(m_hHandle), p);
gtk_widget_show(p);
}
void CButton::on_button_pressed(GtkButton* button, gpointer user_data)
{
CButton* b = (CButton*)user_data;
if(b)
b->SetState(true);
}
void CButton::on_button_released(GtkButton* button, gpointer user_data)
{
CButton* b = (CButton*)user_data;
if(b)
b->SetState(false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -