Fl_Osc_Pane.cpp (4355B)
1 /* 2 ZynAddSubFX - a software synthesizer 3 4 Fl_Osc_Pane.cpp - OSC Subwindow 5 Copyright (C) 2016 Mark McCurry 6 7 This program is free software; you can redistribute it and/or 8 modify it under the terms of the GNU General Public License 9 as published by the Free Software Foundation; either version 2 10 of the License, or (at your option) any later version. 11 */ 12 #include "Fl_Osc_Pane.H" 13 #include "Fl_Osc_Widget.H" 14 #include <cassert> 15 #include <cstdio> 16 17 Fl_Osc_Pane::Fl_Osc_Pane(void) 18 :osc(NULL), base() 19 {} 20 21 22 Fl_Osc_Window::Fl_Osc_Window(int w, int h, const char *L) 23 :Fl_Double_Window(w,h,L), title_ext(NULL) 24 {} 25 26 void Fl_Osc_Window::init(Fl_Osc_Interface *osc_, std::string loc_) 27 { 28 title_ext = new Osc_DataModel(osc_); 29 title_ext->doUpdate("/ui/title"); 30 #if 0 31 title_ext->callback = [this](string next) { 32 rewrite_rule = next; 33 //printf("old: %s\n", title_orig.c_str()); 34 const char *orig = title_orig.c_str(); 35 // 12345678901 36 const char *sub = strstr(orig, "zynaddsubfx"); 37 if(!sub) 38 sub = strstr(orig, "ZynAddSubFX"); 39 title_new = ""; 40 while(*orig) { 41 if(orig == sub) { 42 title_new += next; 43 orig += 11; 44 } else 45 title_new += *orig++; 46 } 47 //title_new = title_orig + next; 48 this->label(title_new.c_str()); 49 }; 50 #else 51 title_ext->callback = [](string ) {}; 52 #endif 53 title_orig = label(); 54 55 osc = osc_; 56 base = loc_; 57 } 58 59 Fl_Osc_Window::~Fl_Osc_Window(void) 60 { 61 delete title_ext; 62 } 63 64 std::string Fl_Osc_Window::loc(void) const 65 { 66 return base; 67 } 68 69 static void nested_update(Fl_Group *g) 70 { 71 unsigned nchildren = g->children(); 72 for(unsigned i=0; i < nchildren; ++i) { 73 Fl_Widget *widget = g->child(i); 74 if(Fl_Osc_Widget *o = dynamic_cast<Fl_Osc_Widget*>(widget)) { 75 o->update(); 76 } else if(Fl_Group *o = dynamic_cast<Fl_Group*>(widget)) { 77 nested_update(o); 78 } 79 80 } 81 } 82 83 void Fl_Osc_Window::update(void) 84 { 85 unsigned nchildren = this->children(); 86 for(unsigned i=0; i < nchildren; ++i) { 87 Fl_Widget *widget = this->child(i); 88 if(Fl_Osc_Widget *o = dynamic_cast<Fl_Osc_Widget*>(widget)) { 89 o->update(); 90 } 91 if(dynamic_cast<Fl_Group*>(widget)) 92 nested_update(dynamic_cast<Fl_Group*>(widget)); 93 } 94 } 95 96 void Fl_Osc_Window::update_title(void) 97 { 98 title_orig = label(); 99 title_ext->callback(rewrite_rule); 100 } 101 102 static void nested_rebase(Fl_Group *g, std::string new_base) 103 { 104 unsigned nchildren = g->children(); 105 for(unsigned i=0; i < nchildren; ++i) { 106 Fl_Widget *widget = g->child(i); 107 if(Fl_Osc_Widget *o = dynamic_cast<Fl_Osc_Widget*>(widget)) { 108 o->rebase(new_base); 109 } else if(Fl_Osc_Group *o = dynamic_cast<Fl_Osc_Group*>(widget)) { 110 o->rebase(new_base); 111 } else if(Fl_Group *o = dynamic_cast<Fl_Group*>(widget)) { 112 nested_rebase(o, new_base); 113 } 114 115 } 116 } 117 118 void Fl_Osc_Window::rebase(std::string new_base) 119 { 120 unsigned nchildren = this->children(); 121 for(unsigned i=0; i < nchildren; ++i) { 122 Fl_Widget *widget = this->child(i); 123 if(Fl_Osc_Widget *o = dynamic_cast<Fl_Osc_Widget*>(widget)) { 124 o->rebase(new_base); 125 } 126 if(Fl_Osc_Group *o = dynamic_cast<Fl_Osc_Group*>(widget)) 127 o->rebase(new_base); 128 else if(dynamic_cast<Fl_Group*>(widget)) 129 nested_rebase(dynamic_cast<Fl_Group*>(widget), new_base); 130 } 131 base = new_base; 132 } 133 134 static Fl_Osc_Pane *find_osc_pane(Fl_Widget *root) 135 { 136 if(!root) 137 return NULL; 138 Fl_Group *next = root->parent(); 139 140 if(auto *p = dynamic_cast<Fl_Osc_Pane*>(next)) 141 return p; 142 else 143 return find_osc_pane(next); 144 } 145 146 Fl_Osc_Group::Fl_Osc_Group(int x, int y, int w, int h, const char *L) 147 :Fl_Group(x,y,w,h,L) 148 { 149 if(auto *p = find_osc_pane(this)) { 150 osc = p->osc; 151 base = p->loc(); 152 assert(osc); 153 } 154 }; 155 156 157 std::string Fl_Osc_Group::loc(void) const 158 { 159 return base + ext; 160 } 161 162 163 void Fl_Osc_Group::rebase(std::string new_base) 164 { 165 nested_rebase(this, new_base+ext); 166 base = new_base; 167 } 168 169 void Fl_Osc_Group::reext(std::string new_ext) 170 { 171 nested_rebase(this, base+new_ext); 172 ext = new_ext; 173 }