Skip to content

Commit

Permalink
on #179 - mucho updates to check for optional font/stroke in attr
Browse files Browse the repository at this point in the history
  • Loading branch information
Cecil committed Jan 31, 2018
1 parent 1767e5b commit 05e9fcd
Show file tree
Hide file tree
Showing 13 changed files with 220 additions and 144 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Expand Up @@ -15,14 +15,10 @@ Makefile
VERSION.txt
*-custom.yaml
Shoes3.2.geany
crosscompile
bin/main.c
shoes/version.h
deps
dist
pkg
notes
/i686-linux
/x86_64-linux
/xarmv6hf
/xwin7
Expand All @@ -40,3 +36,4 @@ req/json
/qtifw
*.orig
cshoes
Gemfile.lock
40 changes: 40 additions & 0 deletions Tests/plot/manual.rb
@@ -0,0 +1,40 @@
# two graphs - line and bar
Shoes.app width: 800, height: 500 do
@values1 = [24, 22, 10, 13, 20, 8, 22]
@x_axis1 = ['a','b','c','d','e','f', 'g']
@values2 = [200, 150, 75, 125, 75, 225, 125]
@x_axis2 = ['a','b','c','d','e','f', 'g']
stack do
para "Plot Demo Line and Column"
flow do
button "quit" do Shoes.quit end
end
widget_width = 400
widget_height = 300
stack do
flow do
@grf = plot widget_width, widget_height, title: "My Graph", caption:
"Look at that! Booyah!!" , font: "Helvetica", auto_grid: true,
default: "skip", background: honeydew
@grf2 = plot widget_width, widget_height+100, title: "Column Graph", caption:
"Amazing!!" , font: "Mono", auto_grid: false,
default: "skip", background: cornsilk, chart: "column", boundary_box: false
end
end
@grf.add values: @values1, labels: @x_axis1,
name: "foobar", min: 6, max: 26 , desc: "foobar Yy", color: dodgerblue,
points: true
@grf.add values: @values2, labels: @x_axis2,
name: "Tab", min: @values2.min, max: @values2.max, desc: "BarTab", color: coral,
points: true, strokewidth: 2

@grf2.add values: @values1, labels: @x_axis1,
name: "Bar", min: 0, max: 30, desc: "foobar Yy", color: rgb(220, 20, 60),
points: true, strokewidth: 12
cs2 = chart_series values: @values2, labels: @x_axis2,
name: "Tab", min: 50, max: 230, desc: "BarTab", color: green,
points: true, strokewidth: 6
@grf2.add cs2
end
end

2 changes: 1 addition & 1 deletion shoes/native/cocoa.h
Expand Up @@ -51,7 +51,7 @@ void create_window_menu(NSMenu *main);
void create_help_menu(NSMenu *main);
void shoes_native_view_supplant(NSView *from, NSView *to);
void gettimeofday(void *ts, void *extra);

NSMutableDictionary *shoes_attr_dict(VALUE attr);

#define VK_ESCAPE 53
#define VK_DELETE 117
Expand Down
68 changes: 68 additions & 0 deletions shoes/native/cocoa.m
Expand Up @@ -1373,3 +1373,71 @@ void shoes_native_app_window_move(shoes_app *app, int x, int y) {
//NSLog(@"real pos %i,%i return y: %i", (int)frame.origin.x, (int)frame.origin.y, app->y);

}

/*
* Helper function to return a NSDict for use in AttributedString creation
* input is the Shoes attrs (font and stroke)
*/
NSMutableDictionary *shoes_attr_dict(VALUE attr) {
char *fntstr = 0;
VALUE fgclr = Qnil; // Could be hex color string or Shoes color object
NSInteger fsize = 0;
NSArray *fontsettings;
NSMutableDictionary *dict = NULL;
NSMutableString *fontname = [[NSMutableString alloc] initWithCapacity: 40];


// get the Shoes attributes
if (!NIL_P(shoes_hash_get(attr, rb_intern("font")))) {
fntstr = RSTRING_PTR(shoes_hash_get(attr, rb_intern("font")));
NSString *fstr = [NSString stringWithUTF8String: fntstr];
fontsettings = [fstr componentsSeparatedByString:@" "];
// in OSX there is font name - may include Bold etc, and size
int cnt = fontsettings.count;
fsize = [fontsettings[cnt-1] integerValue];
if (fsize > 0 && fsize < 24) {
//we probably have a size spec - everything before that is fontname
int i;
for (i = 0; i < cnt-1; i++) {
[fontname appendString: fontsettings[i]];
if (i < cnt-2) {
[fontname appendString:@" "];
}
}
} else {
// have to assume they didn't give a point size so
[fontname appendString: fstr];
fsize = 10;
}
}
if (!NIL_P(shoes_hash_get(attr, rb_intern("stroke")))) {
fgclr = shoes_hash_get(attr, rb_intern("stroke"));
}
if (fntstr || !NIL_P(fgclr)) {
dict = [[NSMutableDictionary alloc] initWithCapacity: 5];
//NSString *title = [NSString stringWithUTF8String: msg];
if (fntstr) {
NSFont *font = [NSFont fontWithName: fontname size: fsize];
if (font == nil)
// Don't do this : rb_raise(rb_eArgError, "Font \"%s\" not found", fntstr);
font = [NSFont fontWithName: @"arial" size: 12];
[dict setObject: font forKey: NSFontAttributeName];
}
if (! NIL_P(fgclr)) {
// convert Shoes color to NSColor
if (TYPE(fgclr) == T_STRING)
fgclr = shoes_color_parse(cColor, fgclr); // convert string to cColor
if (rb_obj_is_kind_of(fgclr, cColor))
{
shoes_color *color;
Data_Get_Struct(fgclr, shoes_color, color);
CGFloat rg = (CGFloat)color->r / 255;
CGFloat gb = (CGFloat)color->g / 255;
CGFloat bb = (CGFloat)color->b / 255;
NSColor *clr = [NSColor colorWithCalibratedRed: rg green: gb blue: bb alpha: 1.0];
[dict setObject: clr forKey: NSForegroundColorAttributeName];
}
}
}
return dict;
}
2 changes: 2 additions & 0 deletions shoes/native/cocoa/listbox.h
Expand Up @@ -2,5 +2,7 @@
@interface ShoesPopUpButton : NSPopUpButton
{
VALUE object;
@public
NSMutableDictionary *attrs;
}
@end
38 changes: 32 additions & 6 deletions shoes/native/cocoa/listbox.m
Expand Up @@ -28,6 +28,7 @@ - (id)initWithFrame: (NSRect)frame andObject: (VALUE)o
if ((self = [super initWithFrame: frame pullsDown: NO]))
{
object = o;
attrs = NULL;
[self setTarget: self];
[self setAction: @selector(handleChange:)];
}
Expand All @@ -49,6 +50,9 @@ -(IBAction)handleChange: (id)sender
NSMakeRect(place->ix + place->dx, place->iy + place->dy,
place->ix + place->dx + place->iw, place->iy + place->dy + place->ih)
andObject: self];

pop->attrs = shoes_attr_dict(attr);

// Tooltip
VALUE vtip = shoes_hash_get(attr, rb_intern("tooltip"));
if (! NIL_P(vtip)) {
Expand All @@ -66,16 +70,38 @@ -(IBAction)handleChange: (id)sender
{
long i;
ShoesPopUpButton *pop = (ShoesPopUpButton *)ref;
COCOA_DO({
INIT;
if (pop->attrs) {
// Need to use menu_items to set AttributedStrings
// TODO: probably too complicated - but it works
[pop removeAllItems];
for (i = 0; i < RARRAY_LEN(ary); i++)
{
NSString *emptystr = @"";
int icnt = RARRAY_LEN(ary);
NSArray *itemAry = [pop itemArray];
for (i = 0; i < icnt; i++) {
VALUE msg_s = shoes_native_to_s(rb_ary_entry(ary, i));
char *msg = RSTRING_PTR(msg_s);
[[pop menu] insertItemWithTitle: [NSString stringWithUTF8String: msg] action: nil
keyEquivalent: @"" atIndex: i];
NSString *str = [NSString stringWithUTF8String: msg];
NSAttributedString *astr = [[NSAttributedString alloc] initWithString: str
attributes: pop->attrs];
//printf(stderr,"Colorize %s\n", msg); // C string
[[pop menu] insertItemWithTitle: str action: nil
keyEquivalent: @"" atIndex: i];
itemAry = [pop itemArray];
NSMenuItem *mitem = itemAry[i];
[mitem setAttributedTitle: astr];
}
});
} else {
[pop removeAllItems];
for (i = 0; i < RARRAY_LEN(ary); i++) {
VALUE msg_s = shoes_native_to_s(rb_ary_entry(ary, i));
char *msg = RSTRING_PTR(msg_s);
NSString *str = [NSString stringWithUTF8String: msg];
[[pop menu] insertItemWithTitle: str action: nil
keyEquivalent: @"" atIndex: i];
}
}
RELEASE;
}

VALUE
Expand Down
2 changes: 2 additions & 0 deletions shoes/native/cocoa/switch.h
Expand Up @@ -2,5 +2,7 @@
@interface ShoesSwitch : NSButton
{
VALUE object;
@public
NSMutableDictionary *attrs;
}
@end
24 changes: 21 additions & 3 deletions shoes/native/cocoa/switch.m
Expand Up @@ -36,6 +36,7 @@ - (id)initWithType: (NSButtonType)t andObject: (VALUE)o
if ((self = [super init]))
{
object = o;
attrs = NULL;
[self setButtonType: t];
[self setBezelStyle: NSRoundedBezelStyle];
[self setTarget: self];
Expand All @@ -61,14 +62,31 @@ -(IBAction)handleClick: (id)sender
INIT;
ShoesSwitch *button = [[ShoesSwitch alloc] initWithType: NSToggleButton
andObject: self];
[button setTitle: @"Off"];
[button setAlternateTitle: @"On"];
button->attrs = shoes_attr_dict(attr);
if (button->attrs) {
[button setAttributedTitle: [[NSAttributedString alloc] initWithString: @"Off"
attributes: button->attrs]];
[button setAttributedAlternateTitle: [[NSAttributedString alloc] initWithString: @"On"
attributes: button->attrs]];
}
else {
[button setTitle: @"Off"];
[button setAlternateTitle: @"On"];
}
if (!NIL_P(shoes_hash_get(attr, rb_intern("active")))) {
VALUE bstv = shoes_hash_get(attr, rb_intern("active"));
button.state = !NIL_P(bstv) ? NSOnState : NSOffState;
//fprintf(stderr, "have a initial active %li\n",button.state);
}
//button->sw_state = button.state; //property -> instance_var

// Tooltip
VALUE vtip = shoes_hash_get(attr, rb_intern("tooltip"));
if (! NIL_P(vtip)) {
char *cstr = RSTRING_PTR(vtip);
NSString *tip = [NSString stringWithUTF8String: cstr];
[button setToolTip:tip];
}

RELEASE;
return (SHOES_CONTROL_REF) button;
}
Expand Down
44 changes: 44 additions & 0 deletions shoes/native/gtk.c
Expand Up @@ -1476,3 +1476,47 @@ void shoes_css_parse_error (GtkCssProvider *provider,
{
fprintf(stderr,"css parse error\n");
}

void shoes_css_apply(GtkWidget *widget, VALUE attr, char *css_template)
{
// default css values
char *font = "Arial 12";
char color[40] = "black";
int do_sub = 0;
int have_color = 0;
VALUE vclr = Qnil;
VALUE vfont = ATTR(attr, font);
if (! NIL_P(vfont)) {
font = RSTRING_PTR(vfont);
do_sub = 1;
}
if (RTEST(ATTR(attr, stroke))) {
vclr = (ATTR(attr, stroke));
// That's a Shoes color turn it into a css rgba

shoes_color *scolor;
Data_Get_Struct(vclr, shoes_color, scolor);
sprintf(color, "rgba(%d,%d,%d,%d)", scolor->r, scolor->g, scolor->b,
scolor->a);
do_sub = 1;
}
if (do_sub) {
/* Change default font and color through widget css */
GtkCssProvider *provider;
GtkStyleContext *context;
char new_css[100];
sprintf(new_css, css_template, font, color);
//printf("css: %s", new_css);
provider = gtk_css_provider_new ();
g_signal_connect(G_OBJECT(provider), "parsing-error",
G_CALLBACK(shoes_css_parse_error),
(gpointer)NULL);
gtk_css_provider_load_from_data(provider, new_css, -1, NULL);
context = gtk_widget_get_style_context (widget);
gtk_style_context_add_provider (context,
GTK_STYLE_PROVIDER (provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
// check what's really in provider ?
//printf("provider has: %s\n", gtk_css_provider_to_string(provider));
}
}
1 change: 1 addition & 0 deletions shoes/native/gtk.h
Expand Up @@ -6,3 +6,4 @@ void shoes_css_parse_error (GtkCssProvider *provider,
GtkCssSection *section,
GError *error,
gpointer user_data);
void shoes_css_apply(GtkWidget *widget, VALUE attr, char *css_template);

0 comments on commit 05e9fcd

Please sign in to comment.