fl_widget_type.cxx
来自「SRI international 发布的OAA框架软件」· CXX 代码 · 共 1,977 行 · 第 1/5 页
CXX
1,977 行
//
// "$Id: Fl_Widget_Type.cxx,v 1.1.1.1 2003/06/03 22:25:40 agno Exp $"
//
// Widget type code for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2002 by Bill Spitzak and others.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//
// Please report all bugs and problems to "fltk-bugs@fltk.org".
//
#include <FL/Fl.H>
#include <FL/Fl_Group.H>
#include <FL/Fl_Input.H>
#include "Fl_Widget_Type.h"
#include "alignment_panel.h"
#include <FL/fl_message.H>
#include <FL/Fl_Slider.H>
#include <FL/Fl_Window.H>
#include "../src/flstring.h"
#include <stdio.h>
#include <stdlib.h>
// Make an Fl_Widget_Type subclass instance.
// It figures out the automatic size and parent of the new widget,
// creates the Fl_Widget (by calling the virtual function _make),
// adds it to the Fl_Widget hierarchy, creates a new Fl_Type
// instance, sets the widget pointers, and makes all the display
// update correctly...
extern int reading_file;
int force_parent;
extern int gridx;
extern int gridy;
extern int i18n_type;
extern const char* i18n_include;
extern const char* i18n_function;
extern const char* i18n_file;
extern const char* i18n_set;
int Fl_Widget_Type::is_widget() const {return 1;}
int Fl_Widget_Type::is_public() const {return public_;}
const char* subclassname(Fl_Type* l) {
if (l->is_widget()) {
Fl_Widget_Type* p = (Fl_Widget_Type*)l;
const char* c = p->subclass();
if (c) return c;
if (p->o->type() == FL_WINDOW+1) return "Fl_Double_Window";
}
return l->type_name();
}
Fl_Type *Fl_Widget_Type::make() {
// Find the current widget, or widget to copy:
Fl_Type *qq = Fl_Type::current;
while (qq && (!qq->is_widget() || qq->is_menu_item())) qq = qq->parent;
if (!qq) {
fl_message("Please select a widget");
return 0;
}
Fl_Widget_Type* q = (Fl_Widget_Type*)qq;
// find the parent widget:
Fl_Widget_Type* p = q;
if ((force_parent || !p->is_group()) && p->parent->is_widget())
p = (Fl_Widget_Type*)(p->parent);
force_parent = 0;
// Figure out a border between widget and window:
int B = p->o->w()/2; if (p->o->h()/2 < B) B = p->o->h()/2; if (B>25) B = 25;
int ULX,ULY; // parent's origin in window
if (!p->is_window()) { // if it is a group, add corner
ULX = p->o->x(); ULY = p->o->y();
} else {
ULX = ULY = 0;
}
// Figure out a position and size for the widget
int X,Y,W,H;
if (is_group()) { // fill the parent with the widget
X = ULX+B;
W = p->o->w()-B;
Y = ULY+B;
H = p->o->h()-B;
} else if (q != p) { // copy position and size of current widget
W = q->o->w();
H = q->o->h();
X = q->o->x()+W;
Y = q->o->y();
if (X+W > ULX+p->o->w()) {
X = q->o->x();
Y = q->o->y()+H;
if (Y+H > ULY+p->o->h()) Y = ULY+B;
}
} else { // just make it small and square...
X = ULX+B;
Y = ULY+B;
W = H = B;
}
// satisfy the grid requirements (otherwise it edits really strangely):
if (gridx>1) {
X = (X/gridx)*gridx;
W = ((W-1)/gridx+1)*gridx;
}
if (gridy>1) {
Y = (Y/gridy)*gridy;
H = ((H-1)/gridy+1)*gridy;
}
// Construct the Fl_Type:
Fl_Widget_Type *t = _make();
if (!o) o = widget(0,0,100,100); // create template widget
t->factory = this;
// Construct the Fl_Widget:
t->o = widget(X,Y,W,H);
if (reading_file) t->o->label(0);
else if (t->o->label()) t->label(t->o->label()); // allow editing
t->o->user_data((void*)t);
// Put it in the parent:
// ((Fl_Group *)(p->o))->add(t->o); (done by Fl_Type::add())
// add to browser:
t->add(p);
t->redraw();
return t;
}
#include "Fluid_Image.h"
void Fl_Widget_Type::setimage(Fluid_Image *i) {
if (i == image) return;
if (image) image->decrement();
if (i) i->increment();
image = i;
if (i) i->image(o);
else o->image(0);
redraw();
}
void Fl_Widget_Type::setinactive(Fluid_Image *i) {
if (i == inactive) return;
if (inactive) inactive->decrement();
if (i) i->increment();
inactive = i;
if (i) i->deimage(o);
else o->deimage(0);
redraw();
}
void Fl_Widget_Type::setlabel(const char *n) {
o->label(n);
redraw();
}
Fl_Widget_Type::Fl_Widget_Type() {
for (int n=0; n<NUM_EXTRA_CODE; n++) {extra_code_[n] = 0; subclass_ = 0;}
hotspot_ = 0;
tooltip_ = 0;
image_name_ = 0;
inactive_name_ = 0;
image = 0;
inactive = 0;
xclass = 0;
o = 0;
public_ = 1;
}
Fl_Widget_Type::~Fl_Widget_Type() {
if (o) {
o->hide();
if (o->parent()) ((Fl_Group*)o->parent())->remove(*o);
delete o;
}
}
void Fl_Widget_Type::extra_code(int m,const char *n) {
storestring(n,extra_code_[m]);
}
extern void redraw_browser();
void Fl_Widget_Type::subclass(const char *n) {
if (storestring(n,subclass_) && visible)
redraw_browser();
}
void Fl_Widget_Type::tooltip(const char *n) {
storestring(n,tooltip_);
o->tooltip(n);
}
void Fl_Widget_Type::image_name(const char *n) {
setimage(Fluid_Image::find(n));
storestring(n,image_name_);
}
void Fl_Widget_Type::inactive_name(const char *n) {
setinactive(Fluid_Image::find(n));
storestring(n,inactive_name_);
}
void Fl_Widget_Type::redraw() {
Fl_Type *t = this;
if (is_menu_item()) {
// find the menu button that parents this menu:
do t = t->parent; while (t && t->is_menu_item());
// kludge to cause build_menu to be called again:
t->add_child(0,0);
} else {
while (t->parent && t->parent->is_widget()) t = t->parent;
((Fl_Widget_Type*)t)->o->redraw();
}
}
// the recursive part sorts all children, returns pointer to next:
Fl_Type *sort(Fl_Type *parent) {
Fl_Type *f,*n=0;
for (f = parent ? parent->next : Fl_Type::first; ; f = n) {
if (!f || parent && f->level <= parent->level) return f;
n = sort(f);
if (!f->selected || (!f->is_widget() || f->is_menu_item())) continue;
Fl_Widget* fw = ((Fl_Widget_Type*)f)->o;
Fl_Type *g; // we will insert before this
for (g = parent->next; g != f; g = g->next) {
if (!g->selected || g->level > f->level) continue;
Fl_Widget* gw = ((Fl_Widget_Type*)g)->o;
if (gw->y() > fw->y()) break;
if (gw->y() == fw->y() && gw->x() > fw->x()) break;
}
if (g != f) f->move_before(g);
}
}
////////////////////////////////////////////////////////////////
// The control panels!
#include "widget_panel.h"
#include <FL/fl_show_colormap.H>
static Fl_Window *the_panel;
// All the callbacks use the argument to indicate whether to load or store.
// This avoids the need for pointers to all the widgets, and keeps the
// code localized in the callbacks.
// A value of LOAD means to load. The hope is that this will not collide
// with any actual useful values for the argument. I also use this to
// initialized parts of the widget that are nyi by fluid.
Fl_Widget_Type *current_widget; // one of the selected ones
static int numselected; // number selected
static int haderror;
void name_cb(Fl_Input* o, void *v) {
if (v == LOAD) {
if (numselected != 1) {
static char buf[16];
sprintf(buf,"(%d widgets)",numselected);
the_panel->label(buf);
o->hide();
} else {
o->static_value(current_widget->name());
o->show();
the_panel->label(current_widget->title());
}
} else {
if (numselected == 1) {
current_widget->name(o->value());
// I don't update window title, as it probably is being closed
// and wm2 (a window manager) barfs if you retitle and then
// hide a window:
// ((Fl_Window*)(o->parent()->parent()->parent()))->label(current_widget->title());
}
}
}
void name_public_cb(Fl_Light_Button* i, void* v) {
if (v == LOAD) {
i->value(current_widget->public_);
} else {
for (Fl_Type *o = Fl_Type::first; o; o = o->next)
if (o->selected && o->is_widget())
((Fl_Widget_Type*)o)->public_ = i->value();
}
}
static char* oldlabel;
static unsigned oldlabellen;
void label_cb(Fl_Input* i, void *v) {
if (v == LOAD) {
i->static_value(current_widget->label());
if (strlen(i->value()) >= oldlabellen) {
oldlabellen = strlen(i->value())+128;
oldlabel = (char*)realloc(oldlabel,oldlabellen);
}
strcpy(oldlabel,i->value());
} else {
for (Fl_Type *o = Fl_Type::first; o; o = o->next)
if (o->selected && o->is_widget()) o->label(i->value());
}
}
static Fl_Input *image_input;
void image_cb(Fl_Input* i, void *v) {
if (v == LOAD) {
image_input = i;
if (current_widget->is_widget()) {
i->activate();
i->static_value(((Fl_Widget_Type*)current_widget)->image_name());
} else i->deactivate();
} else {
for (Fl_Type *o = Fl_Type::first; o; o = o->next)
if (o->selected && o->is_widget()) ((Fl_Widget_Type*)o)->image_name(i->value());
}
}
void image_browse_cb(Fl_Button* b, void *v) {
if (v == LOAD) {
if (current_widget->is_widget()) b->activate();
else b->deactivate();
} else {
if (ui_find_image(image_input->value())) {
image_input->value(ui_find_image_name);
for (Fl_Type *o = Fl_Type::first; o; o = o->next)
if (o->selected && o->is_widget()) ((Fl_Widget_Type*)o)->image_name(ui_find_image_name);
}
}
}
static Fl_Input *inactive_input;
void inactive_cb(Fl_Input* i, void *v) {
if (v == LOAD) {
inactive_input = i;
if (current_widget->is_widget()) {
i->activate();
i->static_value(((Fl_Widget_Type*)current_widget)->inactive_name());
} else i->deactivate();
} else {
for (Fl_Type *o = Fl_Type::first; o; o = o->next)
if (o->selected && o->is_widget()) ((Fl_Widget_Type*)o)->inactive_name(i->value());
}
}
void inactive_browse_cb(Fl_Button* b, void *v) {
if (v == LOAD) {
if (current_widget->is_widget()) b->activate();
else b->deactivate();
} else {
if (ui_find_image(inactive_input->value())) {
inactive_input->value(ui_find_image_name);
for (Fl_Type *o = Fl_Type::first; o; o = o->next)
if (o->selected && o->is_widget()) ((Fl_Widget_Type*)o)->inactive_name(ui_find_image_name);
}
}
}
void tooltip_cb(Fl_Input* i, void *v) {
if (v == LOAD) {
if (current_widget->is_widget()) {
i->activate();
i->static_value(((Fl_Widget_Type*)current_widget)->tooltip());
} else i->deactivate();
} else {
for (Fl_Type *o = Fl_Type::first; o; o = o->next)
if (o->selected && o->is_widget()) ((Fl_Widget_Type*)o)->tooltip(i->value());
}
}
Fl_Value_Input *x_input, *y_input, *w_input, *h_input;
void x_cb(Fl_Value_Input *i, void *v) {
if (v == LOAD) {
x_input = i;
if (current_widget->is_widget()) {
i->value(((Fl_Widget_Type *)current_widget)->o->x());
x_input->activate();
} else x_input->deactivate();
} else {
for (Fl_Type *o = Fl_Type::first; o; o = o->next)
if (o->selected && o->is_widget()) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?