fltk-flow 0.2.4

A flow widget wrapping Fl_Flow
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#ifndef FL_FL_FLOW_H
#define FL_FL_FLOW_H
#include <exception>
#include <string>

template <typename T>
struct Fl_Exception
{
  Fl_Exception(const char* _message) : m_message(_message) { }
  Fl_Exception(const std::string& _message) : m_message(_message) { }
  virtual ~Fl_Exception() throw() {}

  virtual const char* what() const throw()
  {
    return m_message.c_str();
  }

private:
  std::string m_message;

};

#define Fl_Exception Fl_Exception<char>

#include <FL/Fl.H>
#include <FL/Fl_Widget.H>
#include <FL/Fl_Group.H>

template <typename T>
struct Fl_Helper
{
  static void position(Fl_Widget_Tracker _w, int _x, int _y)
  {
    Fl_Widget_Tracker parent = _w.widget()->parent();
    int x = 0;
    int y = 0;

    if(parent.exists())
    {
      x = parent.widget()->x();
      y = parent.widget()->y();
    }

    _w.widget()->position(x + _x, y + _y);
  }
};

#define Fl_Helper Fl_Helper<char>

#include <FL/Fl.H>

template <typename T>
struct Fl_Transform
{
  Fl_Widget_Tracker m_widget;
  int m_padding;
  int m_x;
  int m_y;
  int m_w;
  int m_h;
  int m_cx;
  int m_cy;
  int m_cw;
  int m_ch;

  Fl_Transform(Fl_Widget_Tracker _widget, int _padding)
    : m_widget(_widget), m_padding(_padding), m_x(), m_y(), m_w(), m_h(),
    m_cx(), m_cy(), m_cw(), m_ch()
  {
    m_x = _widget.widget()->x() - m_padding;
    m_y = _widget.widget()->y() - m_padding;
    m_w = _widget.widget()->w() + m_padding * 2;
    m_h = _widget.widget()->h() + m_padding * 2;

    m_cx = m_x;
    m_cy = m_y;
    m_cw = m_w;
    m_ch = m_h;
  }

  bool contains(Fl_Transform& _other)
  {
    if(_other.m_x < m_x) return false;
    if(_other.m_y < m_y) return false;
    if(_other.m_x + _other.m_w > m_x + m_w) return false;
    if(_other.m_y + _other.m_h > m_y + m_h) return false;
    return true;
  }

  bool colliding(Fl_Transform& _other)
  {
    if(m_x < _other.m_x)
    {
      if(m_x + m_w < _other.m_x) return false;
    }
    else
    {
      if(_other.m_x + _other.m_w < m_x) return false;
    }

    if(m_y < _other.m_y)
    {
      if(m_y + m_h < _other.m_y) return false;
    }
    else
    {
      if(_other.m_y + _other.m_h < m_y) return false;
    }

    return true;
  }

  void apply()
  {
    m_widget.widget()->resize(m_cx + m_padding, m_cy + m_padding,
      m_cw - m_padding * 2, m_ch - m_padding * 2);
  }

  void debug_output()
  {
    printf("Committed: %i %i %i %i\n", m_cx, m_cy, m_cw, m_ch);
    printf("Staging: %i %i %i %i\n", m_x, m_y, m_w, m_h);
  }

  void commit()
  {
    m_cx = m_x;
    m_cy = m_y;
    m_cw = m_w;
    m_ch = m_h;
  }

  void rollback()
  {
    m_x = m_cx;
    m_y = m_cy;
    m_w = m_cw;
    m_h = m_ch;
  }

  void contract(int _w, int _h)
  {
    m_x += m_w / 2 - _w / 2;
    m_y += m_h / 2 - _h / 2;
    m_w = _w;
    m_h = _h;
  }

  void translate(int _x, int _y)
  {
    commit();
    m_x += _x;
    m_y += _y;
  }

  void scale(int _x, int _y)
  {
    commit();
    if(_x < 0)
    {
      m_x += _x;
      m_w -= _x;
    }
    else
    {
      m_w += _x;
    }

    if(_y < 0)
    {
      m_y += _y;
      m_h -= _y;
    }
    else
    {
      m_h += _y;
    }
  }
};

#define Fl_Transform Fl_Transform<char>

#include <FL/Fl.H>

template <typename T>
struct Fl_Instruction
{
  static const int NONE = 0;
  static const int EXPAND = 50;
  static const int CENTER = 60;

  static const int MOVE_LEFT = 1;
  static const int MOVE_RIGHT = 2;
  static const int MOVE_UP = 3;
  static const int MOVE_DOWN = 4;

  static const int EXPAND_LEFT = 6;
  static const int EXPAND_RIGHT = 7;
  static const int EXPAND_UP = 8;
  static const int EXPAND_DOWN = 9;

  static const int CENTER_LEFT = 11;
  static const int CENTER_RIGHT = 12;
  static const int CENTER_UP = 13;
  static const int CENTER_DOWN = 14;

  static int decode(char c, int _type)
  {
    if(_type == EXPAND)
    {
      if(c == '<') return EXPAND_LEFT;
      else if(c == '>') return EXPAND_RIGHT;
      else if(c == '^') return EXPAND_UP;
      else if(c == 'v') return EXPAND_DOWN;
    }
    else if(_type == CENTER)
    {
      if(c == '<') return CENTER_LEFT;
      else if(c == '>') return CENTER_RIGHT;
      else if(c == '^') return CENTER_UP;
      else if(c == 'v') return CENTER_DOWN;
    }
    else if(_type == NONE)
    {
      if(c == '<') return MOVE_LEFT;
      else if(c == '>') return MOVE_RIGHT;
      else if(c == '^') return MOVE_UP;
      else if(c == 'v') return MOVE_DOWN;
    }

    throw Fl_Exception("Invalid instruction");
  }

  Fl_Instruction() : m_widget(0), m_instruction() { }

  int x_direction()
  {
    if(m_instruction == MOVE_LEFT ||
      m_instruction == EXPAND_LEFT ||
      m_instruction == CENTER_LEFT) { return -1;}
    else if(m_instruction == MOVE_RIGHT ||
      m_instruction == EXPAND_RIGHT ||
      m_instruction == CENTER_RIGHT) { return 1; }

    return 0;
  }

  int y_direction()
  {
    if(m_instruction == MOVE_UP ||
      m_instruction == EXPAND_UP ||
      m_instruction == CENTER_UP) { return -1; }
    else if(m_instruction == MOVE_DOWN ||
      m_instruction == EXPAND_DOWN ||
      m_instruction == CENTER_DOWN) { return 1; }

    return 0;
  }

  Fl_Widget_Tracker m_widget;
  int m_instruction;
};

#define Fl_Instruction Fl_Instruction<char>

#include <FL/Fl.H>

template <typename T>
struct Fl_State
{
  Fl_State() : m_widget(0), m_w(), m_h(), m_placed() { }

  Fl_Widget_Tracker m_widget;
  int m_w;
  int m_h;
  bool m_placed;
};

#define Fl_State Fl_State<char>

#include <FL/Fl_Group.H>

#include <vector>
#include <string>

struct Fl_Flow : Fl_Group
{
  Fl_Flow(int _x = 0, int _y = 0, int _w = 128, int _h = 128, const char *_label = 0)
    : Fl_Group(_x, _y, _w, _h, _label), m_padding(5), m_drawn()
  {
    //box(FL_FLAT_BOX);
    //color(FL_RED);
    resizable(NULL);
  }

  void set_padding(int _padding)
  {
    m_padding = _padding;
    resize(x(), y(), w(), h());
  }

  void rule(Fl_Widget& _widget, const std::string& _instructions)
  {
    rule(&_widget, _instructions);
  }

  void rule(Fl_Widget_Tracker _widget, const std::string& _instructions)
  {
    int type = Fl_Instruction::NONE;

    add(_widget.widget());

    for(size_t ci = 0; ci < _instructions.length(); ++ci)
    {
      char c = _instructions.at(ci);

      if(c == '=')
      {
        type = Fl_Instruction::EXPAND;
        continue;
      }
      else if(c == '/')
      {
        type = Fl_Instruction::CENTER;
        continue;
      }

      Fl_Instruction instruction;
      instruction.m_widget = _widget;
      instruction.m_instruction = Fl_Instruction::decode(c, type);
      type = Fl_Instruction::NONE;
      m_instructions.push_back(instruction);
    }
  }

  /*
   * Ensure that widget layout has occurred at least once
   * before initial draw
   */
  virtual void draw()
  {
    if(!m_drawn)
    {
      m_drawn = true;
      //resize(x(), y(), w(), h());
      prepare();
      process();
    }

    Fl_Group::draw();
  }

  virtual void resize(int _x, int _y, int _w, int _h)
  {
    Fl_Group::resize(_x, _y, _w, _h);
    prepare();
    process();
  }

private:
  std::vector<Fl_Instruction> m_instructions;
  std::vector<Fl_State> m_states;
  int m_padding;
  bool m_drawn;

  void process()
  {
    Fl_Transform pt(this, 0);

    for(size_t ii = 0; ii < m_instructions.size(); ++ii)
    {
      Fl_Instruction& i = m_instructions.at(ii);

      if(i.m_instruction == Fl_Instruction::MOVE_LEFT ||
        i.m_instruction == Fl_Instruction::MOVE_RIGHT ||
        i.m_instruction == Fl_Instruction::MOVE_UP ||
        i.m_instruction == Fl_Instruction::MOVE_DOWN ||
        i.m_instruction == Fl_Instruction::EXPAND_LEFT ||
        i.m_instruction == Fl_Instruction::EXPAND_RIGHT ||
        i.m_instruction == Fl_Instruction::EXPAND_UP ||
        i.m_instruction == Fl_Instruction::EXPAND_DOWN ||
        i.m_instruction == Fl_Instruction::CENTER_LEFT ||
        i.m_instruction == Fl_Instruction::CENTER_RIGHT ||
        i.m_instruction == Fl_Instruction::CENTER_UP ||
        i.m_instruction == Fl_Instruction::CENTER_DOWN)
      {
        int xDir = i.x_direction();
        int yDir = i.y_direction();

        Fl_Transform wt(i.m_widget, m_padding);

        int origWidth = wt.m_w;
        int origHeight = wt.m_h;

        while(true)
        {
          if(i.m_instruction == Fl_Instruction::MOVE_LEFT ||
            i.m_instruction == Fl_Instruction::MOVE_RIGHT ||
            i.m_instruction == Fl_Instruction::MOVE_UP ||
            i.m_instruction == Fl_Instruction::MOVE_DOWN)
          {
            wt.translate(xDir, yDir);
          }
          else if(i.m_instruction == Fl_Instruction::EXPAND_LEFT ||
            i.m_instruction == Fl_Instruction::EXPAND_RIGHT ||
            i.m_instruction == Fl_Instruction::EXPAND_UP ||
            i.m_instruction == Fl_Instruction::EXPAND_DOWN ||
            i.m_instruction == Fl_Instruction::CENTER_LEFT ||
            i.m_instruction == Fl_Instruction::CENTER_RIGHT ||
            i.m_instruction == Fl_Instruction::CENTER_UP ||
            i.m_instruction == Fl_Instruction::CENTER_DOWN)
          {
            wt.scale(xDir, yDir);
          }
          else
          {
            throw Fl_Exception("Invalid instruction");
          }

          /*
           * Collide with parent bounds
           */
          if(!pt.contains(wt))
          {
            break;
          }

          bool colliding = false;

          /*
           * Collide with *positioned* siblings
           */
          for(size_t si = 0; si < m_states.size(); ++si)
          {
            Fl_State& s = m_states.at(si);
            if(!s.m_placed) continue;
            if(s.m_widget.widget() == i.m_widget.widget()) continue;

            Fl_Transform st(s.m_widget, 0);

            if(wt.colliding(st))
            {
              colliding = true;
              break;
            }
          }

          if(colliding) break;
        }

        /*
         * Transformed *just* too far, so rollback
         */
        wt.rollback();
        //wt.debug_output();

        if(i.m_instruction == Fl_Instruction::CENTER_LEFT ||
          i.m_instruction == Fl_Instruction::CENTER_RIGHT ||
          i.m_instruction == Fl_Instruction::CENTER_UP ||
          i.m_instruction == Fl_Instruction::CENTER_DOWN)
        {
          wt.contract(origWidth, origHeight);
          wt.commit();
        }

        wt.apply();
      }

      /*
       * Flag widget as placed.
       */
      for(size_t si = 0; si < m_states.size(); ++si)
      {
        Fl_State& s = m_states.at(si);
        if(s.m_widget.widget() != i.m_widget.widget()) continue;
        s.m_placed = true;
        break;
      }
    }
  }

  void prepare()
  {
    /*
     * Remove any states with invalid children
     */
    for(size_t si = 0; si < m_states.size(); ++si)
    {
      if(m_states.at(si).m_widget.deleted())
      {
        m_states.erase(m_states.begin() + si); --si; continue;
      }

      bool found = false;
      for(int ci = 0; ci < children(); ++ci)
      {
        if(child(ci) == m_states.at(si).m_widget.widget())
        {
          found = true; break;
        }
      }

      if(!found)
      {
        m_states.erase(m_states.begin() + si); --si; continue;
      }
    }

    /*
     * Remove any instructions with invalid children
     */
    for(size_t ii = 0; ii < m_instructions.size(); ++ii)
    {
      if(m_instructions.at(ii).m_widget.deleted())
      {
        m_instructions.erase(m_instructions.begin() + ii); --ii; continue;
      }

      bool found = false;
      for(int ci = 0; ci < children(); ++ci)
      {
        if(child(ci) == m_instructions.at(ii).m_widget.widget())
        {
          found = true; break;
        }
      }

      if(!found)
      {
        m_instructions.erase(m_instructions.begin() + ii); --ii; continue;
      }
    }

    /*
     * Add any missing children into the states
     */
    for(int ci = 0; ci < children(); ++ci)
    {
      bool found = false;
      for(size_t si = 0; si < m_states.size(); ++si)
      {
        if(child(ci) == m_states.at(si).m_widget.widget())
        {
          found = true; break;
        }
      }

      if(found == false)
      {
        Fl_State s;
        s.m_widget = child(ci);
        s.m_w = child(ci)->w();
        s.m_h = child(ci)->h();
        m_states.push_back(s);
      }
    }

    /*
     * Reset state for the children
     */
    for(size_t si = 0; si < m_states.size(); ++si)
    {
      m_states.at(si).m_placed = false;
      Fl_Widget_Tracker t = m_states.at(si).m_widget;
      t.widget()->size(m_states.at(si).m_w, m_states.at(si).m_h);
      Fl_Helper::position(t, w() - t.widget()->w() - m_padding, h() - t.widget()->h() - m_padding);
    }
  }
};

#endif