FLTK sandbox

Check-in [94d917ba9c]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:added new method Style::replace() to change default value for given property name added an initialization of some classes via the replace() fixed calls to realloc() every time on change class_name_ changed type of property name to const char* (it keeps only pointer to outer string)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | styles
Files: files | file ages | folders
SHA1:94d917ba9c3b949e71aea6e63c359c407a80ead1
User & Date: Nikita 2016-01-31 21:34:55
Context
2016-01-31
21:34
added new method Style::replace() to change default value for given property name added an initialization of some classes via the replace() fixed calls to realloc() every time on change class_name_ changed type of property name to const char* (it keeps only pointer to outer string) Leaf check-in: 94d917ba9c user: Nikita tags: styles
2016-01-30
20:44
started adding of styles to fltk check-in: 5b4d03fe0b user: Nikita tags: styles
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to FL/Fl_Widget.H.

111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129




130
131
132
133
134
135
136
...
139
140
141
142
143
144
145
146
147
148
149
150
151

152
153
154
155
156
157
158
  int color2_id_;
  uchar type_;
  uchar damage_;
  int box_id_;
  uchar when_;
   
  struct Style {    
    Style() : properties_(0), count_(0), class_name_(0) {        
    }
    ~Style();
    /** creates a full class name eg "widget.button.check" and
        re-initializes properties of base class if already exists
        Note! call it only once in constructor of your class  */
    int register_class(const char* cn);

    /** adds new name and returns its index, 
        saves defval in order to use in case if such property is not exist.
        the returned index is used to access the property */
    int add(const char* name, int defval=0);





    /** value of property */
    int get(int prop_id) const;

    /** saves the local property value
        since the moment we will ignore the global table of styles*/
    void set(int prop_id, int value);
................................................................................
    void reset(int prop_id);
            
    /**  an array to requested properties */
    struct properties {
        int value_; // unique property value for every object (not class!)
        char use_local_; // flag to use local value instead of the global table
        int table_id_; // ID of the property, used to access global table
        char* sname_; // string to keep the property name
    } *properties_;
    /* size of the array */
    int count_; 
    /* full class name including names of all base classes */
    char *class_name_;  

  } style_;
 
  const char *tooltip_;

  /** unimplemented copy ctor */
  Fl_Widget(const Fl_Widget &);
  /** unimplemented assignment operator */







|











>
>
>
>







 







|




|
>







111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
...
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  int color2_id_;
  uchar type_;
  uchar damage_;
  int box_id_;
  uchar when_;
   
  struct Style {    
    Style() : properties_(0), count_(0), class_name_(0), class_name_size_(32) {        
    }
    ~Style();
    /** creates a full class name eg "widget.button.check" and
        re-initializes properties of base class if already exists
        Note! call it only once in constructor of your class  */
    int register_class(const char* cn);

    /** adds new name and returns its index, 
        saves defval in order to use in case if such property is not exist.
        the returned index is used to access the property */
    int add(const char* name, int defval=0);

    /** replaces the default value or adds new item if not exists 
        use it if you suppose the name was initialized before */
    int replace(const char* name, int defval);

    /** value of property */
    int get(int prop_id) const;

    /** saves the local property value
        since the moment we will ignore the global table of styles*/
    void set(int prop_id, int value);
................................................................................
    void reset(int prop_id);
            
    /**  an array to requested properties */
    struct properties {
        int value_; // unique property value for every object (not class!)
        char use_local_; // flag to use local value instead of the global table
        int table_id_; // ID of the property, used to access global table
        const char* sname_; // string to keep the property name
    } *properties_;
    /* size of the array */
    int count_; 
    /* full class name including names of all base classes */
    char *class_name_;
    int class_name_size_;
  } style_;
 
  const char *tooltip_;

  /** unimplemented copy ctor */
  Fl_Widget(const Fl_Widget &);
  /** unimplemented assignment operator */

Changes to src/Fl_Button.cxx.

209
210
211
212
213
214
215
216


217
218
219
220
221
222
223

  \param[in] X, Y, W, H position and size of the widget
  \param[in] L widget label, default is no label
 */
Fl_Button::Fl_Button(int X, int Y, int W, int H, const char *L)
: Fl_Widget(X,Y,W,H,L) {
  style()->register_class("button");
  //box(FL_UP_BOX);


  down_box_id_ = style()->add("down-box", FL_NO_BOX);
  value_ = oldval = 0;
  shortcut_ = 0;
  set_flag(SHORTCUT_LABEL);
}

/**







|
>
>







209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225

  \param[in] X, Y, W, H position and size of the widget
  \param[in] L widget label, default is no label
 */
Fl_Button::Fl_Button(int X, int Y, int W, int H, const char *L)
: Fl_Widget(X,Y,W,H,L) {
  style()->register_class("button");
  /* note: we override default value of widget property "box",
     therefore we use replace() instead of add() */
  down_box_id_ = style()->replace("box", FL_UP_BOX);
  down_box_id_ = style()->add("down-box", FL_NO_BOX);
  value_ = oldval = 0;
  shortcut_ = 0;
  set_flag(SHORTCUT_LABEL);
}

/**

Changes to src/Fl_Check_Button.cxx.

47
48
49
50
51
52
53
54
55
56
57

  \param[in] X, Y, W, H position and size of the widget
  \param[in] L widget label, default is no label
 */
Fl_Check_Button::Fl_Check_Button(int X, int Y, int W, int H, const char *L)
: Fl_Light_Button(X, Y, W, H, L) {
  style()->register_class("check");
  //box(FL_NO_BOX);
  //down_box(FL_DOWN_BOX);
  //selection_color(FL_FOREGROUND_COLOR);
}







|
|
|

47
48
49
50
51
52
53
54
55
56
57

  \param[in] X, Y, W, H position and size of the widget
  \param[in] L widget label, default is no label
 */
Fl_Check_Button::Fl_Check_Button(int X, int Y, int W, int H, const char *L)
: Fl_Light_Button(X, Y, W, H, L) {
  style()->register_class("check");
  style()->replace("box", FL_NO_BOX);
  style()->replace("down-box", FL_DOWN_BOX);
  style()->replace("color2", FL_FOREGROUND_COLOR);
}

Changes to src/Fl_Light_Button.cxx.

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  <P>The destructor deletes the check button.
*/
Fl_Light_Button::Fl_Light_Button(int X, int Y, int W, int H, const char* l)
: Fl_Button(X, Y, W, H, l) {
  style()->register_class("light");
  pushed_box_id_ = style()->add("pushed-box", FL_DOWN_BOX);
  type(FL_TOGGLE_BUTTON);
  //selection_color(FL_YELLOW);
  align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
}


Fl_Radio_Light_Button::Fl_Radio_Light_Button(int X,int Y,int W,int H,const char *l)
: Fl_Light_Button(X,Y,W,H,l) 
{







|







154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  <P>The destructor deletes the check button.
*/
Fl_Light_Button::Fl_Light_Button(int X, int Y, int W, int H, const char* l)
: Fl_Button(X, Y, W, H, l) {
  style()->register_class("light");
  pushed_box_id_ = style()->add("pushed-box", FL_DOWN_BOX);
  type(FL_TOGGLE_BUTTON);
  style()->replace("color2", FL_YELLOW);
  align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
}


Fl_Radio_Light_Button::Fl_Radio_Light_Button(int X,int Y,int W,int H,const char *l)
: Fl_Light_Button(X,Y,W,H,l) 
{

Changes to src/Fl_Round_Button.cxx.

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

  \param[in] X, Y, W, H position and size of the widget
  \param[in] L widget label, default is no label
*/
Fl_Round_Button::Fl_Round_Button(int X,int Y,int W,int H, const char *L)
: Fl_Light_Button(X,Y,W,H,L) {
  style()->register_class("round");
  //box(FL_NO_BOX);
  //down_box(FL_ROUND_DOWN_BOX);
  //selection_color(FL_FOREGROUND_COLOR);
}

/**
  Creates a new Fl_Radio_Button widget using the given position, size, and label string.

  The button type() is set to FL_RADIO_BUTTON.








|
|
|







44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

  \param[in] X, Y, W, H position and size of the widget
  \param[in] L widget label, default is no label
*/
Fl_Round_Button::Fl_Round_Button(int X,int Y,int W,int H, const char *L)
: Fl_Light_Button(X,Y,W,H,L) {
  style()->register_class("round");
  style()->replace("box", FL_NO_BOX);
  style()->replace("down-box", FL_ROUND_DOWN_BOX);
  style()->replace("color2", FL_FOREGROUND_COLOR);
}

/**
  Creates a new Fl_Radio_Button widget using the given position, size, and label string.

  The button type() is set to FL_RADIO_BUTTON.

Changes to src/Fl_Widget.cxx.

205
206
207
208
209
210
211

212
213
214
215
216
217
218
...
291
292
293
294
295
296
297
298
299

300
301
302
303
304
305
306











307
308
309
310
311
312
313



314

315
316
317
318
319
320
321

322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
}

static const int MAX_DEEP = 32;

/** searches the nearest class name with given property name
    returns ID of found property or -1 if none */
static int table_id(const char* class_name, const char* prop_name) {

    if (!style_table)
        return -1;
    /* list of indices */
    int ids[MAX_DEEP];
    /* getting list of suitable records */
    int cnt = class_id(class_name, ids);
    /* testing every item in reverse order, because the last - the more suitable */
................................................................................
        prop_name[0] = '\0'; ++prop_name;
        put_style(class_name, prop_name, table[i].value);
        free(class_name);
    }
    return i;
}

int Fl_Widget::Style::add(const char* name, int defval) {
    int last = count_++;

    properties_ = (properties*)realloc(properties_, sizeof(properties_[0]) * count_);
    properties_[last].value_ = defval;
    properties_[last].table_id_ = table_id(class_name_, name);
    properties_[last].use_local_ = (properties_[last].table_id_ < 0) ? 1 : 0;
    properties_[last].sname_ = strdup(name);
    return last;
}    












int Fl_Widget::Style::register_class(const char* name) {
    /** create styles before first using */
    static int temp = simple_styles_table(); 

    if (class_name_) {
        /** if full class name already is exist, we append the current name to it */



        class_name_ = (char*)realloc(class_name_, strlen(class_name_) + 1 + strlen(name) + 1);

        strcat(class_name_, ".");
        strcat(class_name_, name);
        /* in this case we need update properties, because name of the class is changed ! */
        for (int i = 0; i < count_; i++) {
            properties_[i].table_id_ = table_id(class_name_, properties_[i].sname_);
        }
    } else /** otherwise make a copy */

        class_name_ = strdup(name);

    return 0;
}

Fl_Widget::Style::~Style() {
    for (int i=0;i<count_;i++) {
        free(properties_[i].sname_);
    }
    free(properties_);
    free(class_name_);
}

int Fl_Widget::Style::get(int prop_id) const {
    properties* v = properties_ + prop_id;
    return v->use_local_ ? v->value_ : ::style(v->table_id_);
}

void Fl_Widget::Style::set(int prop_id, int value) {
    properties* v = properties_ + prop_id;
    v->value_=value;
    v->use_local_=1;
}

void Fl_Widget::Style::reset(int prop_id) {
    properties* v = properties_ + prop_id;







>







 







|

>
|



|

|
>
>
>
>
>
>
>
>
>
>
>







>
>
>
|
>






|
>
|
|



|
<
<
<









|







205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
...
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
}

static const int MAX_DEEP = 32;

/** searches the nearest class name with given property name
    returns ID of found property or -1 if none */
static int table_id(const char* class_name, const char* prop_name) {
    //return -1;
    if (!style_table)
        return -1;
    /* list of indices */
    int ids[MAX_DEEP];
    /* getting list of suitable records */
    int cnt = class_id(class_name, ids);
    /* testing every item in reverse order, because the last - the more suitable */
................................................................................
        prop_name[0] = '\0'; ++prop_name;
        put_style(class_name, prop_name, table[i].value);
        free(class_name);
    }
    return i;
}

int Fl_Widget::Style::add(const char* name, int defval) {    
    int last = count_++;
    if (!properties_)
        properties_ = (properties*)realloc(properties_, sizeof(properties_[0]) * 10);
    properties_[last].value_ = defval;
    properties_[last].table_id_ = table_id(class_name_, name);
    properties_[last].use_local_ = (properties_[last].table_id_ < 0) ? 1 : 0;
    properties_[last].sname_ = name;
    return last;
}

int Fl_Widget::Style::replace(const char* name, int defval) {
    /* search the name among the properties */
    for (int i = 0; i < count_; i++) {
        if (strcmp(properties_[i].sname_, name) == 0) {
            properties_[i].value_ = defval;
            return i;
        }
    }    
    return add(name, defval);
}

int Fl_Widget::Style::register_class(const char* name) {
    /** create styles before first using */
    static int temp = simple_styles_table(); 

    if (class_name_) {
        /** if full class name already is exist, we append the current name to it */
        int newsize = strlen(class_name_) + 1 + strlen(name) + 1;
        if (newsize >= class_name_size_) {
            class_name_size_ *= 2;
            class_name_ = (char*)realloc(class_name_, class_name_size_);
        }
        strcat(class_name_, ".");
        strcat(class_name_, name);
        /* in this case we need update properties, because name of the class is changed ! */
        for (int i = 0; i < count_; i++) {
            properties_[i].table_id_ = table_id(class_name_, properties_[i].sname_);
        }
    } else { /** otherwise just make a copy of name */
        class_name_ = (char*)malloc(class_name_size_);
        strcpy(class_name_, name);        
    }
    return 0;
}

Fl_Widget::Style::~Style() {    



    free(properties_);
    free(class_name_);
}

int Fl_Widget::Style::get(int prop_id) const {
    properties* v = properties_ + prop_id;
    return v->use_local_ ? v->value_ : ::style(v->table_id_);
}

void Fl_Widget::Style::set(int prop_id, int value) {   
    properties* v = properties_ + prop_id;
    v->value_=value;
    v->use_local_=1;
}

void Fl_Widget::Style::reset(int prop_id) {
    properties* v = properties_ + prop_id;