source: tags/ms_r16q3/WINDOW/AW_at.cxx

Last change on this file was 15176, checked in by westram, 8 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.8 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : AW_at.cxx                                         //
4//   Purpose   :                                                   //
5//                                                                 //
6//   Institute of Microbiology (Technical University Munich)       //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#include "aw_at.hxx"
12#include "aw_window.hxx"
13#include "aw_xfig.hxx"
14#include "aw_root.hxx"
15
16#include <arbdbt.h>
17
18AW_at::AW_at() {
19    memset((char*)this, 0, sizeof(AW_at));
20
21    length_of_buttons = 10;
22    height_of_buttons = 0;
23    shadow_thickness  = 2;
24    widget_mask       = AWM_ALL;
25}
26
27void AW_window::at(const char *at_id) {
28    char to_position[100];
29    memset(to_position, 0, sizeof(to_position));
30
31    _at->attach_y   = _at->attach_x = false;
32    _at->attach_ly  = _at->attach_lx = false;
33    _at->attach_any = false;
34
35    if (!xfig_data) GBK_terminatef("no xfig-data loaded, can't position at(\"%s\")", at_id);
36
37    AW_xfig     *xfig = (AW_xfig *)xfig_data;
38    AW_xfig_pos *pos;
39
40    pos = (AW_xfig_pos*)GBS_read_hash(xfig->at_pos_hash, at_id);
41
42    if (!pos) {
43        sprintf(to_position, "X:%s", at_id);
44        pos = (AW_xfig_pos*)GBS_read_hash(xfig->at_pos_hash, to_position);
45        if (pos) _at->attach_any = _at->attach_lx = true;
46    }
47    if (!pos) {
48        sprintf(to_position, "Y:%s", at_id);
49        pos = (AW_xfig_pos*)GBS_read_hash(xfig->at_pos_hash, to_position);
50        if (pos) _at->attach_any = _at->attach_ly = true;
51    }
52    if (!pos) {
53        sprintf(to_position, "XY:%s", at_id);
54        pos = (AW_xfig_pos*)GBS_read_hash(xfig->at_pos_hash, to_position);
55        if (pos) _at->attach_any = _at->attach_lx = _at->attach_ly = true;
56    }
57
58    if (!pos) GBK_terminatef("ID '%s' does not exist in xfig file", at_id);
59
60    at((pos->x - xfig->minx), (pos->y - xfig->miny - this->get_root()->font_height - 9));
61    _at->correct_for_at_center = pos->center;
62
63    sprintf(to_position, "to:%s", at_id);
64    pos = (AW_xfig_pos*)GBS_read_hash(xfig->at_pos_hash, to_position);
65
66    if (!pos) {
67        sprintf(to_position, "to:X:%s", at_id);
68        pos = (AW_xfig_pos*)GBS_read_hash(xfig->at_pos_hash, to_position);
69        if (pos) _at->attach_any = _at->attach_x = true;
70    }
71    if (!pos) {
72        sprintf(to_position, "to:Y:%s", at_id);
73        pos = (AW_xfig_pos*)GBS_read_hash(xfig->at_pos_hash, to_position);
74        if (pos) _at->attach_any = _at->attach_y = true;
75    }
76    if (!pos) {
77        sprintf(to_position, "to:XY:%s", at_id);
78        pos = (AW_xfig_pos*)GBS_read_hash(xfig->at_pos_hash, to_position);
79        if (pos) _at->attach_any = _at->attach_x = _at->attach_y = true;
80    }
81
82    if (pos) {
83        _at->to_position_exists = true;
84        _at->to_position_x = (pos->x - xfig->minx);
85        _at->to_position_y = (pos->y - xfig->miny);
86        _at->correct_for_at_center = 0; // always justify left when a to-position exists
87    }
88    else {
89        _at->to_position_exists = false;
90    }
91}
92
93void AW_window::at(int x, int y) {
94    at_x(x);
95    at_y(y);
96}
97
98void AW_window::at_x(int x) {
99    if (_at->x_for_next_button > _at->max_x_size) _at->max_x_size = _at->x_for_next_button;
100    _at->x_for_next_button = x;
101    if (_at->x_for_next_button > _at->max_x_size) _at->max_x_size = _at->x_for_next_button;
102}
103
104void AW_window::at_y(int y) {
105    if (_at->y_for_next_button + _at->biggest_height_of_buttons > _at->max_y_size)
106        _at->max_y_size = _at->y_for_next_button + _at->biggest_height_of_buttons;
107    _at->biggest_height_of_buttons = _at->biggest_height_of_buttons + _at->y_for_next_button - y;
108    if (_at->biggest_height_of_buttons<0) {
109        _at->biggest_height_of_buttons = 0;
110        if (_at->max_y_size < y)    _at->max_y_size = y;
111    }
112    _at->y_for_next_button = y;
113}
114
115void AW_window::at_shift(int x, int y) {
116    at(x+_at->x_for_next_button, y+_at->y_for_next_button);
117}
118
119void AW_window::at_newline() {
120    if (_at->do_auto_increment) {
121        at_y(_at->auto_increment_y + _at->y_for_next_button);
122    }
123    else {
124        if (_at->do_auto_space) {
125            at_y(_at->y_for_next_button + _at->auto_space_y + _at->biggest_height_of_buttons);
126        }
127        else {
128            GBK_terminate("neither auto_space nor auto_increment activated while using at_newline");
129        }
130    }
131    at_x(_at->x_for_newline);
132}
133
134bool AW_window::at_ifdef(const  char *at_id_) {
135    AW_xfig *xfig = (AW_xfig *)xfig_data;
136    if (!xfig) return false;
137
138    char buffer[100];
139
140#if defined(DEBUG)
141    int printed =
142#endif // DEBUG
143        sprintf(buffer, "XY:%s", at_id_);
144#if defined(DEBUG)
145    aw_assert(printed<100);
146#endif // DEBUG
147
148    if (GBS_read_hash(xfig->at_pos_hash, buffer+3)) return true; // "tag"
149    if (GBS_read_hash(xfig->at_pos_hash, buffer+1)) return true; // "Y:tag"
150    if (GBS_read_hash(xfig->at_pos_hash, buffer)) return true; // "XY:tag"
151    buffer[1] = 'X';
152    if (GBS_read_hash(xfig->at_pos_hash, buffer+1)) return true; // "X:tag"
153
154    return false;
155}
156
157void AW_window::at_set_to(bool attach_x, bool attach_y, int xoff, int yoff) {
158    // set "$to:XY:id" manually
159    // use negative offsets to set offset from right/lower border to to-position
160
161    _at->attach_any = attach_x || attach_y;
162    _at->attach_x   = attach_x;
163    _at->attach_y   = attach_y;
164
165    _at->to_position_exists = true;
166    _at->to_position_x      = xoff >= 0 ? _at->x_for_next_button + xoff : _at->max_x_size+xoff;
167    _at->to_position_y      = yoff >= 0 ? _at->y_for_next_button + yoff : _at->max_y_size+yoff;
168
169    if (_at->to_position_x > _at->max_x_size) _at->max_x_size = _at->to_position_x;
170    if (_at->to_position_y > _at->max_y_size) _at->max_y_size = _at->to_position_y;
171
172    _at->correct_for_at_center = 0;
173}
174
175void AW_window::unset_at_commands() {
176    _callback   = NULL;
177    _d_callback = NULL;
178
179    _at->correct_for_at_center = 0;
180    _at->to_position_exists    = false;
181    _at->highlight             = false;
182
183    freenull(_at->helptext_for_next_button);
184    freenull(_at->label_for_inputfield);
185
186    _at->background_color = 0;
187}
188
189void AW_window::increment_at_commands(int width, int height) {
190
191    at_shift(width, 0);
192    at_shift(-width, 0);        // set bounding box
193
194    if (_at->do_auto_increment) {
195        at_shift(_at->auto_increment_x, 0);
196    }
197    if (_at->do_auto_space) {
198        at_shift(_at->auto_space_x + width, 0);
199    }
200
201    if (_at->biggest_height_of_buttons < height) {
202        _at->biggest_height_of_buttons = height;
203    }
204
205    if (_at->max_y_size < (_at->y_for_next_button + _at->biggest_height_of_buttons + 3.0)) {
206        _at->max_y_size = _at->y_for_next_button + _at->biggest_height_of_buttons + 3;
207    }
208
209    if (_at->max_x_size < (_at->x_for_next_button + this->get_root()->font_width)) {
210        _at->max_x_size = _at->x_for_next_button + this->get_root()->font_width;
211    }
212}
213
214void AW_window::at_unset_to() {
215    _at->attach_x   = _at->attach_y = _at->to_position_exists = false;
216    _at->attach_any = _at->attach_lx || _at->attach_ly;
217}
218
219void AW_window::auto_space(int x, int y) {
220    _at->do_auto_space = true;
221    _at->auto_space_x  = x;
222    _at->auto_space_y  = y;
223
224    _at->do_auto_increment = false;
225
226    _at->x_for_newline             = _at->x_for_next_button;
227    _at->biggest_height_of_buttons = 0;
228}
229
230// -------------------------------------------------------------------------
231//      force-diff-sync 9126478246 (remove after merging back to trunk)
232
233void AW_window::auto_increment(int x, int y) {
234    _at->do_auto_increment         = true;
235    _at->auto_increment_x          = x;
236    _at->auto_increment_y          = y;
237    _at->x_for_newline             = _at->x_for_next_button;
238    _at->do_auto_space             = false;
239    _at->biggest_height_of_buttons = 0;
240}
241
242void AW_window::label_length(int length) {
243    _at->length_of_label_for_inputfield = length;
244}
245
246void AW_window::button_length(int length) {
247    _at->length_of_buttons = length;
248}
249
250int  AW_window::get_button_length() const {
251    return _at->length_of_buttons;
252}
253
254void AW_window::get_at_position(int *x, int *y) const {
255    *x = _at->x_for_next_button;
256    *y = _at->y_for_next_button;
257}
258
259int AW_window::get_at_xposition() const {
260    return _at->x_for_next_button;
261}
262
263int AW_window::get_at_yposition() const {
264    return _at->y_for_next_button;
265}
266
267// -------------------
268//      AW_at_size
269
270class AW_at_size : public AW_at_storage {
271    int  to_offset_x;                               // here we use offsets (not positions like in AW_at)
272    int  to_offset_y;
273    bool to_position_exists;
274
275    bool attach_x;           // attach right side to right form
276    bool attach_y;
277    bool attach_lx;          // attach left side to right form
278    bool attach_ly;
279    bool attach_any;
280
281public:
282    AW_at_size()
283        : to_offset_x(0),
284          to_offset_y(0),
285          to_position_exists(false),
286          attach_x(false),
287          attach_y(false),
288          attach_lx(false),
289          attach_ly(false),
290          attach_any(false)
291    {}
292
293    void store(const AW_at& at) OVERRIDE;
294    void restore(AW_at& at) const OVERRIDE;
295};
296
297void AW_at_size::store(const AW_at& at) {
298    to_position_exists = at.to_position_exists;
299    if (to_position_exists) {
300        to_offset_x = at.to_position_x - at.x_for_next_button;
301        to_offset_y = at.to_position_y - at.y_for_next_button;
302    }
303    attach_x   = at.attach_x;
304    attach_y   = at.attach_y;
305    attach_lx  = at.attach_lx;
306    attach_ly  = at.attach_ly;
307    attach_any = at.attach_any;
308}
309
310void AW_at_size::restore(AW_at& at) const {
311    at.to_position_exists = to_position_exists;
312    if (to_position_exists) {
313        at.to_position_x = at.x_for_next_button + to_offset_x;
314        at.to_position_y = at.y_for_next_button + to_offset_y;
315    }
316    at.attach_x   = attach_x;
317    at.attach_y   = attach_y;
318    at.attach_lx  = attach_lx;
319    at.attach_ly  = attach_ly;
320    at.attach_any = attach_any;
321}
322
323// -------------------
324//      AW_at_maxsize
325
326class AW_at_maxsize : public AW_at_storage {
327    int maxx;
328    int maxy;
329
330public:
331    AW_at_maxsize()
332        : maxx(0),
333          maxy(0)
334    {}
335
336    void store(const AW_at &at) OVERRIDE;
337    void restore(AW_at &at) const OVERRIDE;
338};
339
340void AW_at_maxsize::store(const AW_at &at) {
341    maxx = at.max_x_size;
342    maxy = at.max_y_size;
343}
344
345void AW_at_maxsize::restore(AW_at &at) const {
346    at.max_x_size = maxx;
347    at.max_y_size = maxy;
348}
349
350// -------------------
351//      AW_at_auto
352
353class AW_at_auto : public AW_at_storage {
354    enum { INC, SPACE, OFF } type;
355    int x, y;
356    int xfn, xfnb, yfnb, bhob;
357public:
358    AW_at_auto() : type(OFF), x(0), y(0), xfn(0), xfnb(0), yfnb(0), bhob(0) {}
359
360    void store(const AW_at &at) OVERRIDE;
361    void restore(AW_at &at) const OVERRIDE;
362};
363
364void AW_at_auto::store(const AW_at &at) {
365    if (at.do_auto_increment) {
366        type = INC;
367        x    = at.auto_increment_x;
368        y    = at.auto_increment_y;
369    }
370    else if (at.do_auto_space)  {
371        type = SPACE;
372        x    = at.auto_space_x;
373        y    = at.auto_space_y;
374    }
375    else {
376        type = OFF;
377    }
378
379    xfn  = at.x_for_newline;
380    xfnb = at.x_for_next_button;
381    yfnb = at.y_for_next_button;
382    bhob = at.biggest_height_of_buttons;
383}
384
385void AW_at_auto::restore(AW_at &at) const {
386    at.do_auto_space     = (type == SPACE);
387    at.do_auto_increment = (type == INC);
388
389    if (at.do_auto_space) {
390        at.auto_space_x = x;
391        at.auto_space_y = y;
392    }
393    else if (at.do_auto_increment) {
394        at.auto_increment_x = x;
395        at.auto_increment_y = y;
396    }
397
398    at.x_for_newline             = xfn;
399    at.x_for_next_button         = xfnb;
400    at.y_for_next_button         = yfnb;
401    at.biggest_height_of_buttons = bhob;
402}
403
404// -------------------------------
405//      AW_at_storage factory
406
407AW_at_storage *AW_at_storage::make(AW_window *aww, AW_at_storage_type type) {
408    AW_at_storage *s = NULL;
409    switch (type) {
410        case AW_AT_SIZE_AND_ATTACH: s = new AW_at_size(); break;
411        case AW_AT_AUTO:            s = new AW_at_auto(); break;
412        case AW_AT_MAXSIZE:         s = new AW_at_maxsize(); break;
413    }
414    aww->store_at_to(*s);
415    return s;
416}
417
418// -------------------------------------------------------------------------
419//      force-diff-sync 6423462367 (remove after merging back to trunk)
420
421int AW_window::calculate_string_width(int columns) const {
422    if (xfig_data) {
423        AW_xfig *xfig = (AW_xfig *)xfig_data;
424        return (int)(columns * xfig->font_scale * XFIG_DEFAULT_FONT_WIDTH);   // stdfont 8x13
425    }
426    else {
427        return columns * XFIG_DEFAULT_FONT_WIDTH; // stdfont 8x13
428    }
429}
430
431int AW_window::calculate_string_height(int rows, int offset) const {
432    if (xfig_data) {
433        AW_xfig *xfig = (AW_xfig *)xfig_data;
434        return (int)((rows * XFIG_DEFAULT_FONT_HEIGHT + offset) * xfig->font_scale); // stdfont 8x13
435    }
436    else {
437        return (rows * XFIG_DEFAULT_FONT_HEIGHT + offset);    // stdfont 8x13
438    }
439}
440
441char *AW_window::align_string(const char *label_text, int columns) {
442    // shortens or expands 'label_text' to 'columns' columns
443    // if label_text contains '\n', each "line" is handled separately
444
445    const char *lf     = strchr(label_text, '\n');
446    char       *result = 0;
447
448    if (!lf) {
449        ARB_alloc(result, columns+1);
450
451        int len              = strlen(label_text);
452        if (len>columns) len = columns;
453
454        memcpy(result, label_text, len);
455        if (len<columns) memset(result+len, ' ', columns-len);
456        result[columns] = 0;
457    }
458    else {
459        char *part1    = ARB_strpartdup(label_text, lf-1);
460        char *aligned1 = align_string(part1, columns);
461        char *aligned2 = align_string(lf+1, columns);
462
463        result = GBS_global_string_copy("%s\n%s", aligned1, aligned2);
464
465        free(aligned2);
466        free(aligned1);
467        free(part1);
468    }
469    return result;
470}
471
Note: See TracBrowser for help on using the repository browser.