Browse Source

Running FLTK in static initializers (cont'd):

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
pull/49/head
Manolo Gouy 10 years ago
parent
commit
43bfe74b25
  1. 2
      fluid/Fl_Type.h
  2. 63
      fluid/Fluid_Image.cxx
  3. 2
      fluid/Fluid_Image.h
  4. 8
      fluid/code.cxx
  5. 12
      src/Fl_File_Chooser.cxx
  6. 9
      src/Fl_File_Chooser.fl

2
fluid/Fl_Type.h

@ -30,6 +30,7 @@
#include <FL/Fl_Plugin.H> #include <FL/Fl_Plugin.H>
#include "Fluid_Image.h" #include "Fluid_Image.h"
#include <FL/fl_draw.H> #include <FL/fl_draw.H>
#include <stdarg.h>
void set_modflag(int mf); void set_modflag(int mf);
@ -813,6 +814,7 @@ int write_declare(const char *, ...) __fl_attr((__format__ (__printf__, 1, 2)));
int is_id(char); int is_id(char);
const char* unique_id(void* o, const char*, const char*, const 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 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_h(const char*, ...) __fl_attr((__format__ (__printf__, 1, 2)));
void write_cstring(const char *); void write_cstring(const char *);
void write_cstring(const char *,int length); void write_cstring(const char *,int length);

63
fluid/Fluid_Image.cxx

@ -1,9 +1,9 @@
// //
// "$Id$" // "$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 // 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 // the file "COPYING" which should have been included with this file. If this
@ -24,10 +24,11 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h>
#include <FL/filename.H> #include <FL/filename.H>
extern void goto_source_dir(); // in fluid.C extern void goto_source_dir(); // in fluid.cxx
extern void leave_source_dir(); // in fluid.C extern void leave_source_dir(); // in fluid.cxx
void Fluid_Image::image(Fl_Widget *o) { void Fluid_Image::image(Fl_Widget *o) {
if (o->window() != o) o->image(img); if (o->window() != o) o->image(img);
@ -44,6 +45,8 @@ static int jpeg_header_written = 0;
void Fluid_Image::write_static() { void Fluid_Image::write_static() {
if (!img) return; 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) { if (img->count() > 1) {
// Write Pixmap data... // Write Pixmap data...
write_c("\n"); write_c("\n");
@ -51,8 +54,7 @@ void Fluid_Image::write_static() {
write_c("#include <FL/Fl_Pixmap.H>\n"); write_c("#include <FL/Fl_Pixmap.H>\n");
pixmap_header_written = write_number; pixmap_header_written = write_number;
} }
write_c("static const char *%s[] = {\n", write_c("static const char *%s[] = {\n", idata_name);
unique_id(this, "idata", fl_filename_name(name()), 0));
write_cstring(img->data()[0], strlen(img->data()[0])); write_cstring(img->data()[0], strlen(img->data()[0]));
int i; int i;
@ -74,9 +76,7 @@ void Fluid_Image::write_static() {
write_cstring(img->data()[i], img->w() * chars_per_color); write_cstring(img->data()[i], img->w() * chars_per_color);
} }
write_c("\n};\n"); write_c("\n};\n");
write_c("static Fl_Pixmap %s(%s);\n", write_initializer("Fl_Pixmap", "%s", idata_name);
unique_id(this, "image", fl_filename_name(name()), 0),
unique_id(this, "idata", fl_filename_name(name()), 0));
} else if (img->d() == 0) { } else if (img->d() == 0) {
// Write Bitmap data... // Write Bitmap data...
write_c("\n"); write_c("\n");
@ -84,14 +84,10 @@ void Fluid_Image::write_static() {
write_c("#include <FL/Fl_Bitmap.H>\n"); write_c("#include <FL/Fl_Bitmap.H>\n");
bitmap_header_written = write_number; bitmap_header_written = write_number;
} }
write_c("static const unsigned char %s[] =\n", write_c("static const unsigned char %s[] =\n", idata_name);
unique_id(this, "idata", fl_filename_name(name()), 0));
write_cdata(img->data()[0], ((img->w() + 7) / 8) * img->h()); write_cdata(img->data()[0], ((img->w() + 7) / 8) * img->h());
write_c(";\n"); write_c(";\n");
write_c("static Fl_Bitmap %s(%s, %d, %d);\n", write_initializer( "Fl_Bitmap", "%s, %d, %d", idata_name, img->w(), img->h());
unique_id(this, "image", fl_filename_name(name()), 0),
unique_id(this, "idata", fl_filename_name(name()), 0),
img->w(), img->h());
} else if (strcmp(fl_filename_ext(name()), ".jpg")==0) { } else if (strcmp(fl_filename_ext(name()), ".jpg")==0) {
// Write jpeg image data... // Write jpeg image data...
write_c("\n"); write_c("\n");
@ -99,8 +95,7 @@ void Fluid_Image::write_static() {
write_c("#include <FL/Fl_JPEG_Image.H>\n"); write_c("#include <FL/Fl_JPEG_Image.H>\n");
jpeg_header_written = write_number; jpeg_header_written = write_number;
} }
write_c("static const unsigned char %s[] =\n", write_c("static const unsigned char %s[] =\n", idata_name);
unique_id(this, "idata", fl_filename_name(name()), 0));
FILE *f = fl_fopen(name(), "rb"); FILE *f = fl_fopen(name(), "rb");
if (!f) { if (!f) {
@ -119,10 +114,7 @@ void Fluid_Image::write_static() {
} }
write_c(";\n"); write_c(";\n");
write_c("static Fl_JPEG_Image %s(\"%s\", %s);\n", write_initializer("Fl_JPEG_Image", "\"%s\", %s", fl_filename_name(name()), idata_name);
unique_id(this, "image", fl_filename_name(name()), 0),
fl_filename_name(name()),
unique_id(this, "idata", fl_filename_name(name()), 0));
} else { } else {
// Write image data... // Write image data...
write_c("\n"); write_c("\n");
@ -130,21 +122,31 @@ void Fluid_Image::write_static() {
write_c("#include <FL/Fl_Image.H>\n"); write_c("#include <FL/Fl_Image.H>\n");
image_header_written = write_number; image_header_written = write_number;
} }
write_c("static const unsigned char %s[] =\n", write_c("static const unsigned char %s[] =\n", idata_name);
unique_id(this, "idata", fl_filename_name(name()), 0));
write_cdata(img->data()[0], (img->w() * img->d() + img->ld()) * img->h()); write_cdata(img->data()[0], (img->w() * img->d() + img->ld()) * img->h());
write_c(";\n"); write_c(";\n");
write_c("static Fl_RGB_Image %s(%s, %d, %d, %d, %d);\n", write_initializer("Fl_RGB_Image", "%s, %d, %d, %d, %d", idata_name, img->w(), img->h(), img->d(), img->ld());
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());
} }
} }
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) { void Fluid_Image::write_code(const char *var, int inactive) {
if (!img) return; /* Outputs code that attaches an image to an Fl_Widget or Fl_Menu_Item.
write_c("%s%s->%s(%s);\n", indent(), var, inactive ? "deimage" : "image", This code calls a function output before by Fluid_Image::write_initializer() */
unique_id(this, "image", fl_filename_name(name()), 0)); if (img) write_c("%s%s->%s( %s() );\n", indent(), var, inactive ? "deimage" : "image", function_name_);
} }
@ -207,6 +209,7 @@ Fluid_Image::Fluid_Image(const char *iname) {
written = 0; written = 0;
refcount = 0; refcount = 0;
img = Fl_Shared_Image::get(iname); img = Fl_Shared_Image::get(iname);
function_name_ = NULL;
} }
void Fluid_Image::increment() { void Fluid_Image::increment() {

2
fluid/Fluid_Image.h

@ -30,6 +30,7 @@ class Fluid_Image {
const char *name_; const char *name_;
int refcount; int refcount;
Fl_Shared_Image *img; Fl_Shared_Image *img;
const char *function_name_;
protected: protected:
Fluid_Image(const char *name); // no public constructor Fluid_Image(const char *name); // no public constructor
~Fluid_Image(); // no public destructor ~Fluid_Image(); // no public destructor
@ -41,6 +42,7 @@ public:
void image(Fl_Widget *); // set the image of this widget void image(Fl_Widget *); // set the image of this widget
void deimage(Fl_Widget *); // set the deimage of this widget void deimage(Fl_Widget *); // set the deimage of this widget
void write_static(); void write_static();
void write_initializer(const char *type_name, const char *format, ...);
void write_code(const char *var, int inactive = 0); void write_code(const char *var, int inactive = 0);
const char *name() const {return name_;} const char *name() const {return name_;}
}; };

8
fluid/code.cxx

@ -254,14 +254,18 @@ void write_cdata(const char *s, int length) {
putc('}', code_file); putc('}', code_file);
} }
void write_c(const char* format,...) { void vwrite_c(const char* format, va_list args) {
if (varused_test) { if (varused_test) {
varused = 1; varused = 1;
return; return;
} }
vfprintf(code_file, format, args);
}
void write_c(const char* format,...) {
va_list args; va_list args;
va_start(args, format); va_start(args, format);
vfprintf(code_file, format, args); vwrite_c(format, args);
va_end(args); va_end(args);
} }

12
src/Fl_File_Chooser.cxx

@ -27,7 +27,6 @@
#include "../FL/Fl_File_Chooser.H" #include "../FL/Fl_File_Chooser.H"
#include <FL/fl_draw.H> #include <FL/fl_draw.H>
#include <FL/Fl_Bitmap.H>
void Fl_File_Chooser::cb_window_i(Fl_Double_Window*, void*) { void Fl_File_Chooser::cb_window_i(Fl_Double_Window*, void*) {
fileName->value(""); fileName->value("");
@ -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); ((Fl_File_Chooser*)(o->parent()->parent()->user_data()))->cb_newButton_i(o,v);
} }
#include <FL/Fl_Bitmap.H>
static const unsigned char idata_new[] = 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, {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}; 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*) { void Fl_File_Chooser::cb__i(Fl_Tile*, void*) {
update_preview(); update_preview();
@ -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) { Fl_File_Chooser::Fl_File_Chooser(const char *d, const char *p, int t, const char *title) {
static Fl_Bitmap *image_new = NULL; if (!prefs_) {
if (!image_new) {
prefs_ = new Fl_Preferences(Fl_Preferences::USER, "fltk.org", "filechooser"); 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(); Fl_Group *prev_current = Fl_Group::current();
{ window = new Fl_Double_Window(490, 380, "Choose File"); { window = new Fl_Double_Window(490, 380, "Choose File");
@ -186,7 +188,7 @@ Fl_File_Chooser::Fl_File_Chooser(const char *d, const char *p, int t, const char
favoritesButton->label(favorites_label); favoritesButton->label(favorites_label);
} // Fl_Menu_Button* favoritesButton } // Fl_Menu_Button* favoritesButton
{ Fl_Button* o = newButton = new Fl_Button(455, 10, 25, 25); { Fl_Button* o = newButton = new Fl_Button(455, 10, 25, 25);
newButton->image(image_new); newButton->image( image_new() );
newButton->labelsize(8); newButton->labelsize(8);
newButton->callback((Fl_Callback*)cb_newButton); newButton->callback((Fl_Callback*)cb_newButton);
o->tooltip(new_directory_tooltip); o->tooltip(new_directory_tooltip);

9
src/Fl_File_Chooser.fl

@ -36,7 +36,7 @@ class FL_EXPORT Fl_File_Chooser {open
} { } {
decl {enum { SINGLE = 0, MULTI = 1, CREATE = 2, DIRECTORY = 4 };} {public local 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 decl {void (*callback_)(Fl_File_Chooser*, void *);} {private local
} }
@ -69,6 +69,9 @@ class FL_EXPORT Fl_File_Chooser {open
decl {void update_preview();} {private local decl {void update_preview();} {private local
} }
Function {Fl_File_Chooser(const char *d, const char *p, int t, const char *title)} {} { 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();} {} code {Fl_Group *prev_current = Fl_Group::current();} {}
Fl_Window window { Fl_Window window {
label {Choose File} label {Choose File}
@ -238,7 +241,7 @@ update_favorites();
value(d); value(d);
type(t); type(t);
int e; int e;
prefs_.get("preview", e, 1); prefs_->get("preview", e, 1);
preview(e); preview(e);
Fl_Group::current(prev_current);} {} Fl_Group::current(prev_current);} {}
code {ext_group=(Fl_Widget*)0;} {} code {ext_group=(Fl_Widget*)0;} {}
@ -307,7 +310,7 @@ showChoiceCB();} {}
} }
Function {ok_label(const char *l)} {return_type void Function {ok_label(const char *l)} {return_type void
} { } {
code {okButton->label(l); code {if (l) okButton->label(l);
int w=0, h=0; int w=0, h=0;
okButton->measure_label(w, h); okButton->measure_label(w, h);
okButton->resize(cancelButton->x() - 50 - w, cancelButton->y(), okButton->resize(cancelButton->x() - 50 - w, cancelButton->y(),

Loading…
Cancel
Save