Skip to content

Commit

Permalink
Running FLTK in static initializers (cont'd):
Browse files Browse the repository at this point in the history
1) Changed the way fluid attaches images to widgets and menu items so
it is compatible with running fluid-generated code containing such images
in a static initializer. Images are now attached calling a function:
  widget->image( image_function_name() );
and this function is defined before in fluid-generated code as:
   static Fl_Image *image_function_name() {
     static Fl_Image *image = new image_type(......);
     return image;
   }

2) Changed src/Fl_File_Chooser.fl so the source code generate by fluid
from it is compatible with running in a static initializer.

3) Changed src/Fl_File_Chooser.cxx and FL/Fl_File_Chooser.H
to the result of running fluid on src/Fl_File_Chooser.fl 

git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@10972 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
  • Loading branch information
Manolo Gouy authored and Manolo Gouy committed Dec 18, 2015
1 parent 5c7ef30 commit 43bfe74
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 42 deletions.
2 changes: 1 addition & 1 deletion FL/Fl_File_Chooser.H
Expand Up @@ -49,7 +49,7 @@ class FL_EXPORT Fl_File_Chooser {
public:
enum { SINGLE = 0, MULTI = 1, CREATE = 2, DIRECTORY = 4 };
private:
static Fl_Preferences *prefs_;
static Fl_Preferences *prefs_;
void (*callback_)(Fl_File_Chooser*, void *);
void *data_;
char directory_[FL_PATH_MAX];
Expand Down
2 changes: 2 additions & 0 deletions fluid/Fl_Type.h
Expand Up @@ -30,6 +30,7 @@
#include <FL/Fl_Plugin.H>
#include "Fluid_Image.h"
#include <FL/fl_draw.H>
#include <stdarg.h>

void set_modflag(int mf);

Expand Down Expand Up @@ -813,6 +814,7 @@ int write_declare(const char *, ...) __fl_attr((__format__ (__printf__, 1, 2)));
int is_id(char);
const char* unique_id(void* o, const char*, const char*, const char*);
void write_c(const char*, ...) __fl_attr((__format__ (__printf__, 1, 2)));
void vwrite_c(const char* format, va_list args);
void write_h(const char*, ...) __fl_attr((__format__ (__printf__, 1, 2)));
void write_cstring(const char *);
void write_cstring(const char *,int length);
Expand Down
65 changes: 34 additions & 31 deletions fluid/Fluid_Image.cxx
@@ -1,9 +1,9 @@
//
// "$Id$"
//
// Pixmap label support for the Fast Light Tool Kit (FLTK).
// Pixmap (and other images) label support for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2010 by Bill Spitzak and others.
// Copyright 1998-2015 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
Expand All @@ -24,10 +24,11 @@
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <stdarg.h>
#include <FL/filename.H>

extern void goto_source_dir(); // in fluid.C
extern void leave_source_dir(); // in fluid.C
extern void goto_source_dir(); // in fluid.cxx
extern void leave_source_dir(); // in fluid.cxx

void Fluid_Image::image(Fl_Widget *o) {
if (o->window() != o) o->image(img);
Expand All @@ -44,15 +45,16 @@ static int jpeg_header_written = 0;

void Fluid_Image::write_static() {
if (!img) return;
const char *idata_name = unique_id(this, "idata", fl_filename_name(name()), 0);
function_name_ = unique_id(this, "image", fl_filename_name(name()), 0);
if (img->count() > 1) {
// Write Pixmap data...
write_c("\n");
if (pixmap_header_written != write_number) {
write_c("#include <FL/Fl_Pixmap.H>\n");
pixmap_header_written = write_number;
}
write_c("static const char *%s[] = {\n",
unique_id(this, "idata", fl_filename_name(name()), 0));
write_c("static const char *%s[] = {\n", idata_name);
write_cstring(img->data()[0], strlen(img->data()[0]));

int i;
Expand All @@ -74,33 +76,26 @@ void Fluid_Image::write_static() {
write_cstring(img->data()[i], img->w() * chars_per_color);
}
write_c("\n};\n");
write_c("static Fl_Pixmap %s(%s);\n",
unique_id(this, "image", fl_filename_name(name()), 0),
unique_id(this, "idata", fl_filename_name(name()), 0));
write_initializer("Fl_Pixmap", "%s", idata_name);
} else if (img->d() == 0) {
// Write Bitmap data...
write_c("\n");
if (bitmap_header_written != write_number) {
write_c("#include <FL/Fl_Bitmap.H>\n");
bitmap_header_written = write_number;
}
write_c("static const unsigned char %s[] =\n",
unique_id(this, "idata", fl_filename_name(name()), 0));
write_c("static const unsigned char %s[] =\n", idata_name);
write_cdata(img->data()[0], ((img->w() + 7) / 8) * img->h());
write_c(";\n");
write_c("static Fl_Bitmap %s(%s, %d, %d);\n",
unique_id(this, "image", fl_filename_name(name()), 0),
unique_id(this, "idata", fl_filename_name(name()), 0),
img->w(), img->h());
write_initializer( "Fl_Bitmap", "%s, %d, %d", idata_name, img->w(), img->h());
} else if (strcmp(fl_filename_ext(name()), ".jpg")==0) {
// Write jpeg image data...
write_c("\n");
if (jpeg_header_written != write_number) {
write_c("#include <FL/Fl_JPEG_Image.H>\n");
jpeg_header_written = write_number;
}
write_c("static const unsigned char %s[] =\n",
unique_id(this, "idata", fl_filename_name(name()), 0));
write_c("static const unsigned char %s[] =\n", idata_name);

FILE *f = fl_fopen(name(), "rb");
if (!f) {
Expand All @@ -119,32 +114,39 @@ void Fluid_Image::write_static() {
}

write_c(";\n");
write_c("static Fl_JPEG_Image %s(\"%s\", %s);\n",
unique_id(this, "image", fl_filename_name(name()), 0),
fl_filename_name(name()),
unique_id(this, "idata", fl_filename_name(name()), 0));
write_initializer("Fl_JPEG_Image", "\"%s\", %s", fl_filename_name(name()), idata_name);
} else {
// Write image data...
write_c("\n");
if (image_header_written != write_number) {
write_c("#include <FL/Fl_Image.H>\n");
image_header_written = write_number;
}
write_c("static const unsigned char %s[] =\n",
unique_id(this, "idata", fl_filename_name(name()), 0));
}
write_c("static const unsigned char %s[] =\n", idata_name);
write_cdata(img->data()[0], (img->w() * img->d() + img->ld()) * img->h());
write_c(";\n");
write_c("static Fl_RGB_Image %s(%s, %d, %d, %d, %d);\n",
unique_id(this, "image", fl_filename_name(name()), 0),
unique_id(this, "idata", fl_filename_name(name()), 0),
img->w(), img->h(), img->d(), img->ld());
write_initializer("Fl_RGB_Image", "%s, %d, %d, %d, %d", idata_name, img->w(), img->h(), img->d(), img->ld());
}
}

void Fluid_Image::write_initializer(const char *type_name, const char *format, ...) {
/* Outputs code that returns (and initializes if needed) an Fl_Image as follows:
static Fl_Image *'function_name_'() {
static Fl_Image *image = new 'type_name'('product of format and remaining args');
return image;
} */
va_list ap;
va_start(ap, format);
write_c("static Fl_Image *%s() {\n static Fl_Image *image = new %s(", function_name_, type_name);
vwrite_c(format, ap);
write_c(");\n return image;\n}\n");
va_end(ap);
}

void Fluid_Image::write_code(const char *var, int inactive) {
if (!img) return;
write_c("%s%s->%s(%s);\n", indent(), var, inactive ? "deimage" : "image",
unique_id(this, "image", fl_filename_name(name()), 0));
/* Outputs code that attaches an image to an Fl_Widget or Fl_Menu_Item.
This code calls a function output before by Fluid_Image::write_initializer() */
if (img) write_c("%s%s->%s( %s() );\n", indent(), var, inactive ? "deimage" : "image", function_name_);
}


Expand Down Expand Up @@ -207,6 +209,7 @@ Fluid_Image::Fluid_Image(const char *iname) {
written = 0;
refcount = 0;
img = Fl_Shared_Image::get(iname);
function_name_ = NULL;
}

void Fluid_Image::increment() {
Expand Down
2 changes: 2 additions & 0 deletions fluid/Fluid_Image.h
Expand Up @@ -30,6 +30,7 @@ class Fluid_Image {
const char *name_;
int refcount;
Fl_Shared_Image *img;
const char *function_name_;
protected:
Fluid_Image(const char *name); // no public constructor
~Fluid_Image(); // no public destructor
Expand All @@ -41,6 +42,7 @@ class Fluid_Image {
void image(Fl_Widget *); // set the image of this widget
void deimage(Fl_Widget *); // set the deimage of this widget
void write_static();
void write_initializer(const char *type_name, const char *format, ...);
void write_code(const char *var, int inactive = 0);
const char *name() const {return name_;}
};
Expand Down
8 changes: 6 additions & 2 deletions fluid/code.cxx
Expand Up @@ -254,14 +254,18 @@ void write_cdata(const char *s, int length) {
putc('}', code_file);
}

void write_c(const char* format,...) {
void vwrite_c(const char* format, va_list args) {
if (varused_test) {
varused = 1;
return;
}
vfprintf(code_file, format, args);
}

void write_c(const char* format,...) {
va_list args;
va_start(args, format);
vfprintf(code_file, format, args);
vwrite_c(format, args);
va_end(args);
}

Expand Down
12 changes: 7 additions & 5 deletions src/Fl_File_Chooser.cxx
Expand Up @@ -27,7 +27,6 @@

#include "../FL/Fl_File_Chooser.H"
#include <FL/fl_draw.H>
#include <FL/Fl_Bitmap.H>

void Fl_File_Chooser::cb_window_i(Fl_Double_Window*, void*) {
fileName->value("");
Expand Down Expand Up @@ -60,9 +59,14 @@ void Fl_File_Chooser::cb_newButton(Fl_Button* o, void* v) {
((Fl_File_Chooser*)(o->parent()->parent()->user_data()))->cb_newButton_i(o,v);
}

#include <FL/Fl_Bitmap.H>
static const unsigned char idata_new[] =
{0,0,120,0,132,0,2,1,1,254,1,128,49,128,49,128,253,128,253,128,49,128,49,
128,1,128,1,128,255,255,0,0};
static Fl_Image *image_new() {
static Fl_Image *image = new Fl_Bitmap(idata_new, 16, 16);
return image;
}

void Fl_File_Chooser::cb__i(Fl_Tile*, void*) {
update_preview();
Expand Down Expand Up @@ -163,10 +167,8 @@ void Fl_File_Chooser::cb_favOkButton(Fl_Return_Button* o, void* v) {
}

Fl_File_Chooser::Fl_File_Chooser(const char *d, const char *p, int t, const char *title) {
static Fl_Bitmap *image_new = NULL;
if (!image_new) {
if (!prefs_) {
prefs_ = new Fl_Preferences(Fl_Preferences::USER, "fltk.org", "filechooser");
image_new = new Fl_Bitmap(idata_new, 16, 16);
}
Fl_Group *prev_current = Fl_Group::current();
{ window = new Fl_Double_Window(490, 380, "Choose File");
Expand All @@ -186,7 +188,7 @@ Fl_File_Chooser::Fl_File_Chooser(const char *d, const char *p, int t, const char
favoritesButton->label(favorites_label);
} // Fl_Menu_Button* favoritesButton
{ Fl_Button* o = newButton = new Fl_Button(455, 10, 25, 25);
newButton->image(image_new);
newButton->image( image_new() );
newButton->labelsize(8);
newButton->callback((Fl_Callback*)cb_newButton);
o->tooltip(new_directory_tooltip);
Expand Down
9 changes: 6 additions & 3 deletions src/Fl_File_Chooser.fl
Expand Up @@ -36,7 +36,7 @@ class FL_EXPORT Fl_File_Chooser {open
} {
decl {enum { SINGLE = 0, MULTI = 1, CREATE = 2, DIRECTORY = 4 };} {public local
}
decl {static Fl_Preferences prefs_;} {private local
decl {static Fl_Preferences *prefs_;} {private local
}
decl {void (*callback_)(Fl_File_Chooser*, void *);} {private local
}
Expand Down Expand Up @@ -69,6 +69,9 @@ class FL_EXPORT Fl_File_Chooser {open
decl {void update_preview();} {private local
}
Function {Fl_File_Chooser(const char *d, const char *p, int t, const char *title)} {} {
code {if (!prefs_) {
prefs_ = new Fl_Preferences(Fl_Preferences::USER, "fltk.org", "filechooser");
}} {}
code {Fl_Group *prev_current = Fl_Group::current();} {}
Fl_Window window {
label {Choose File}
Expand Down Expand Up @@ -238,7 +241,7 @@ update_favorites();
value(d);
type(t);
int e;
prefs_.get("preview", e, 1);
prefs_->get("preview", e, 1);
preview(e);
Fl_Group::current(prev_current);} {}
code {ext_group=(Fl_Widget*)0;} {}
Expand Down Expand Up @@ -307,7 +310,7 @@ showChoiceCB();} {}
}
Function {ok_label(const char *l)} {return_type void
} {
code {okButton->label(l);
code {if (l) okButton->label(l);
int w=0, h=0;
okButton->measure_label(w, h);
okButton->resize(cancelButton->x() - 50 - w, cancelButton->y(),
Expand Down

0 comments on commit 43bfe74

Please sign in to comment.