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
|
#include "stdafx.h"
#include "Button.h"
#include "Container.h"
#include "GtkSignal.h"
namespace gui {
Button::Button(Str *title) : isDefault(false) {
text(title);
onClick = null;
}
Button::Button(Str *title, Fn<void> *click) : isDefault(false) {
text(title);
onClick = click;
}
void Button::clicked() {
if (onClick)
onClick->call();
}
#ifdef GUI_WIN32
void Button::text(Str *text) {
Window::text(MnemonicStr(text).win32Mnemonic());
}
bool Button::create(ContainerBase *parent, nat id) {
DWORD flags = buttonFlags;
if (isDefault)
flags |= BS_DEFPUSHBUTTON;
return Window::createEx(WC_BUTTON, flags, 0, parent->handle().hwnd(), id);
}
bool Button::onCommand(nat id) {
if (id == BN_CLICKED) {
clicked();
return true;
}
return false;
}
Size Button::minSize() {
Size sz = font()->stringSize(text());
sz.w += sz.h * 2;
sz.h *= 1.53f;
return sz;
}
void Button::setDefault(Bool def) {
isDefault = def;
if (created()) {
LONG flags = GetWindowLong(handle().hwnd(), GWL_STYLE);
if (def)
flags |= BS_DEFPUSHBUTTON;
else
flags &= ~(BS_DEFPUSHBUTTON);
SetWindowLong(handle().hwnd(), GWL_STYLE, flags);
}
}
#endif
#ifdef GUI_GTK
bool Button::create(ContainerBase *parent, nat id) {
GtkWidget *button = gtk_button_new_with_label(text()->utf8_str());
initWidget(parent, button);
Signal<void, Button>::Connect<&Button::clicked>::to(button, "clicked", engine());
return true;
}
void Button::text(Str *text) {
if (created()) {
GtkWidget *widget = gtk_bin_get_child(GTK_BIN(handle().widget()));
gtk_label_set_text(GTK_LABEL(widget), text->utf8_str());
}
Window::text(text);
}
GtkWidget *Button::fontWidget() {
return gtk_bin_get_child(GTK_BIN(handle().widget()));
}
Size Button::minSize() {
gint w = 0, h = 0;
if (created()) {
gtk_widget_get_preferred_width(handle().widget(), &w, NULL);
gtk_widget_get_preferred_height(handle().widget(), &h, NULL);
}
return Size(Float(w), Float(h));
}
void Button::setDefault(Bool def) {
isDefault = def;
// We don't need anything more here for Gtk+.
}
#endif
}
|