Merge pull request #2 from RustamGamidov/resize-button

button: add uiButtonSetMinSize to avoid very small buttons
This commit is contained in:
Rustam 2020-03-25 12:28:52 +02:00 committed by GitHub
commit 7e671da5d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 97 additions and 3 deletions

9
common/general.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef H_COMMON_GENERAL
#define H_COMMON_GENERAL
int max(int first, int second)
{
return (first < second) ? second : first;
}
#endif

View File

@ -1,5 +1,36 @@
// 13 august 2015
#import "uipriv_darwin.h"
#import "../common/general.h"
@interface libui_NSButton : NSButton
@property int minWidth;
@property int minHeight;
- (id)initWithFrame:(NSRect)frameRect;
@end
@implementation libui_NSButton
- (id)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame: frameRect];
if (self) {
self.minHeight = -1;
self.minWidth = -1;
}
return self;
}
- (NSSize)intrinsicContentSize
{
NSSize s;
s = [super intrinsicContentSize];
s.width = max(self.minWidth, s.width);
s.height = max(self.minHeight, s.height);
return s;
}
@end
struct uiButton {
uiDarwinControl c;
@ -78,6 +109,20 @@ void uiButtonSetText(uiButton *b, const char *text)
[b->button setTitle:uiprivToNSString(text)];
}
void uiButtonSetMinSize(uiButton *b, int width, int height)
{
libui_NSButton *libui_btn = (libui_NSButton *)b->button;
libui_btn.minWidth = width;
libui_btn.minHeight = height;
}
void uiButtonPreferredSize(uiButton *b, int *width, int *height)
{
NSSize s = b->button.intrinsicContentSize;
*width = s.width;
*height = s.height;
}
void uiButtonOnClicked(uiButton *b, void (*f)(uiButton *, void *), void *data)
{
b->onClicked = f;
@ -95,7 +140,7 @@ uiButton *uiNewButton(const char *text)
uiDarwinNewControl(uiButton, b);
b->button = [[NSButton alloc] initWithFrame:NSZeroRect];
b->button = [[libui_NSButton alloc] initWithFrame:NSZeroRect];
[b->button setTitle:uiprivToNSString(text)];
[b->button setButtonType:NSMomentaryPushInButton];
[b->button setBordered:YES];

View File

@ -37,6 +37,11 @@ static uiControl *makeBasicControlsPage(void)
uiBoxAppend(hbox,
uiControl(uiNewCheckbox("Checkbox")),
0);
uiButton *btn = uiNewButton("Wide");
uiButtonSetMinSize(btn, 150, -1);
uiBoxAppend(hbox,
uiControl(btn),
0);
uiBoxAppend(vbox,
uiControl(uiNewLabel("This is a label. Right now, labels can only span one line.")),

2
ui.h
View File

@ -144,6 +144,8 @@ _UI_EXTERN char *uiButtonText(uiButton *b);
_UI_EXTERN void uiButtonSetText(uiButton *b, const char *text);
_UI_EXTERN void uiButtonOnClicked(uiButton *b, void (*f)(uiButton *b, void *data), void *data);
_UI_EXTERN uiButton *uiNewButton(const char *text);
_UI_EXTERN void uiButtonSetMinSize(uiButton *b, int width, int height);
_UI_EXTERN void uiButtonPreferredSize(uiButton *b, int *width, int *height);
typedef struct uiBox uiBox;
#define uiBox(this) ((uiBox *) (this))

View File

@ -33,6 +33,18 @@ void uiButtonSetText(uiButton *b, const char *text)
gtk_button_set_label(b->button, text);
}
void uiButtonSetMinSize(uiButton *b, int width, int height)
{
gtk_widget_set_size_request(b->widget, width, height);
}
void uiButtonPreferredSize(uiButton *b, int *width, int *height)
{
gtk_widget_show(b->widget);
gtk_widget_get_preferred_height(b->widget, NULL, height);
gtk_widget_get_preferred_width(b->widget, NULL, width);
}
void uiButtonOnClicked(uiButton *b, void (*f)(uiButton *, void *), void *data)
{
b->onClicked = f;

View File

@ -1,11 +1,14 @@
// 7 april 2015
#include "uipriv_windows.hpp"
#include "../common/general.h"
struct uiButton {
uiWindowsControl c;
HWND hwnd;
void (*onClicked)(uiButton *, void *);
void *onClickedData;
int minHeight;
int minWidth;
};
static BOOL onWM_COMMAND(uiControl *c, HWND hwnd, WORD code, LRESULT *lResult)
@ -44,8 +47,8 @@ static void uiButtonMinimumSize(uiWindowsControl *c, int *width, int *height)
size.cx = 0; // explicitly ask for ideal size
size.cy = 0;
if (SendMessageW(b->hwnd, BCM_GETIDEALSIZE, 0, (LPARAM) (&size)) != FALSE) {
*width = size.cx;
*height = size.cy;
*width = max(size.cx, (LONG)(b->minWidth));
*height = max(size.cy, (LONG)(b->minHeight));
return;
}
@ -57,6 +60,9 @@ static void uiButtonMinimumSize(uiWindowsControl *c, int *width, int *height)
uiWindowsGetSizing(b->hwnd, &sizing);
uiWindowsSizingDlgUnitsToPixels(&sizing, NULL, &y);
*height = y;
*width = max(*width, b->minWidth);
*height = max(y, b->minHeight);
}
static void defaultOnClicked(uiButton *b, void *data)
@ -76,6 +82,18 @@ void uiButtonSetText(uiButton *b, const char *text)
uiWindowsControlMinimumSizeChanged(uiWindowsControl(b));
}
void uiButtonSetMinSize(uiButton *b, int width, int height)
{
b->minHeight = height;
b->minWidth = width;
uiWindowsControlMinimumSizeChanged(uiWindowsControl(b));
}
void uiButtonPreferredSize(uiButton *b, int *width, int *height)
{
uiButtonMinimumSize(uiWindowsControl(b), width, height);
}
void uiButtonOnClicked(uiButton *b, void (*f)(uiButton *, void *), void *data)
{
b->onClicked = f;
@ -88,6 +106,8 @@ uiButton *uiNewButton(const char *text)
WCHAR *wtext;
uiWindowsNewControl(uiButton, b);
b->minHeight = -1;
b->minWidth = -1;
wtext = toUTF16(text);
b->hwnd = uiWindowsEnsureCreateControlHWND(0,

View File

@ -1,4 +1,5 @@
// 21 april 2016
#define NOMINMAX
#include "winapi.hpp"
#include "../ui.h"
#include "../ui_windows.h"