1 /************************************************************************
3 IMPORTANT NOTE : this file contains two clearly delimited sections :
4 the ARCHITECTURE section (in two parts) and the USER section. Each section
5 is governed by its own copyright and license. Please check individually
6 each section for license and copyright information.
7 *************************************************************************/
9 /*******************BEGIN ARCHITECTURE SECTION (part 1/2)****************/
11 /************************************************************************
12 FAUST Architecture File
13 Copyright (C) 2004-2011 GRAME, Centre National de Creation Musicale
14 ---------------------------------------------------------------------
15 This Architecture section is free software; you can redistribute it
16 and/or modify it under the terms of the GNU Lesser General Public
17 License as published by the Free Software Foundation; either version 3
18 of the License, or (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU Lesser General Public License for more details.
25 You should have received a copy of the GNU Lesser General Public License
26 along with this program; If not, see <http://www.gnu.org/licenses/>.
28 EXCEPTION : As a special exception, you may create a larger work
29 that contains this FAUST architecture section and distribute
30 that work under terms of your choice, so long as this FAUST
31 architecture section is not modified.
34 ************************************************************************
35 ************************************************************************/
37 #import <UIKit/UIKit.h>
38 #import "iPhoneViewController.h"
47 /******************************************************************************
48 *******************************************************************************
50 GRAPHIC USER INTERFACE (v2)
53 *******************************************************************************
54 *******************************************************************************/
56 //typedef void (*uiCallback)(float val, void* data);
64 @interface uiItem
: NSObject
71 - (id
)initWithValues
:(UI
*)ui
:(float*)zone
;
72 - (void)modifyZone
:(float)v
;
79 * Graphic User Interface : abstract definition
84 typedef list
<uiItem
*> clist
;
85 typedef map
<float*, clist
*> zmap
;
89 static list
<UI
*> fGuiList
;
95 UI() : fStopped(false)
97 fGuiList
.push_back(this);
102 // suppression de this dans fGuiList
105 // -- registerZone(z,c) : zone management
107 void registerZone(float* z
, uiItem
* c
)
109 if (fZoneMap
.find(z
) == fZoneMap
.end()) {
110 fZoneMap
[z
] = new clist();
112 fZoneMap
[z
]->push_back(c
);
115 // -- saveState(filename) : save the value of every zone to a file
117 void saveState(const char* filename
)
119 ofstream
f(filename
);
123 for (zmap::iterator i
= fZoneMap
.begin(); i
!= fZoneMap
.end(); i
++) {
124 f
<< *(i
->first
) << ' ';
132 // -- recallState(filename) : load the value of every zone from a file
134 void recallState(const char* filename
)
136 ifstream
f(filename
);
138 for (zmap::iterator i
= fZoneMap
.begin(); i
!= fZoneMap
.end(); i
++) {
146 void updateAllZones();
148 void updateZone(float* z
);
150 static void updateAllGuis()
152 list
<UI
*>::iterator g
;
153 for (g
= fGuiList
.begin(); g
!= fGuiList
.end(); g
++) {
154 (*g
)->updateAllZones();
160 virtual void addButton(const char* label
, float* zone
) = 0;
161 virtual void addToggleButton(const char* label
, float* zone
) = 0;
162 virtual void addCheckButton(const char* label
, float* zone
) = 0;
163 virtual void addVerticalSlider(const char* label
, float* zone
, float init
, float min
, float max
, float step
) = 0;
164 virtual void addHorizontalSlider(const char* label
, float* zone
, float init
, float min
, float max
, float step
) = 0;
165 virtual void addNumEntry(const char* label
, float* zone
, float init
, float min
, float max
, float step
) = 0;
167 // -- passive widgets
169 virtual void addNumDisplay(const char* label
, float* zone
, int precision
) = 0;
170 virtual void addTextDisplay(const char* label
, float* zone
, const char* names
[], float min
, float max
) = 0;
171 virtual void addHorizontalBargraph(const char* label
, float* zone
, float min
, float max
) = 0;
172 virtual void addVerticalBargraph(const char* label
, float* zone
, float min
, float max
) = 0;
174 //void addCallback(float* zone, uiCallback foo, void* data);
176 // -- widget's layouts
178 virtual void openFrameBox(const char* label
) = 0;
179 virtual void openTabBox(const char* label
) = 0;
180 virtual void openHorizontalBox(const char* label
) = 0;
181 virtual void openVerticalBox(const char* label
) = 0;
183 // -- extra widget's layouts
185 virtual void openDialogBox(const char* label
, float* zone
) = 0;
186 virtual void openEventBox(const char* label
) = 0;
187 virtual void openHandleBox(const char* label
) = 0;
188 virtual void openExpanderBox(const char* label
, float* zone
) = 0;
190 virtual void closeBox() = 0;
192 virtual void show() {};
193 virtual void run() {};
195 void stop() { fStopped
= true; }
196 bool stopped() { return fStopped
; }
198 virtual void declare(float* zone
, const char* key
, const char* value
) {}
202 * Update all user items reflecting zone z
205 void UI::updateZone(float* z
)
208 clist
* l
= fZoneMap
[z
];
210 for (clist::iterator c
= l
->begin(); c
!= l
->end(); c
++) {
211 if ([(*c
) cache
] != v
) [(*c
) reflectZone
];
216 * Update all user items not up to date
219 inline void UI::updateAllZones()
221 for (zmap::iterator m
= fZoneMap
.begin(); m
!= fZoneMap
.end(); m
++) {
223 clist
* l
= m
->second
;
225 for (clist::iterator c
= l
->begin(); c
!= l
->end(); c
++) {
226 if ([(*c
) cache
] != v
) [(*c
) reflectZone
];
232 inline void UI::addCallback(float* zone, uiCallback foo, void* data)
234 new uiCallbackItem(this, zone, foo, data);
238 #define WIDGET_SLICE 50.f
239 #define OFFSET_Y 80.f
241 #define SCREEN_WIDTH 320
242 #define SCREEN_HEIGHT 480
244 @implementation uiItem
246 - (id
)initWithValues
:(UI
*)ui
:(float*)zone
250 fCache
= -123456.654321;
251 ui
->registerZone(zone
, self
);
255 - (void)modifyZone
:(float)v
261 fGUI
->updateZone(fZone
);
270 // To implement in subclasses
277 // -------------------------- Slider -----------------------------------
279 @interface uiSlider
: uiItem
282 UITextField
* fTextField
;
285 - (void)changed
:(UISlider
*)sender
;
287 - (id
)initWithValues
:(int)index
:(UI
*)ui
:(iPhoneViewController
*)controler
:(const char*)label
:(float*)zone
:(float)init
:(float)min
:(float)max
:(float)step
;
291 @implementation uiSlider
293 - (id
)initWithValues
:(int)index
:(UI
*)ui
:(iPhoneViewController
*)controler
:(const char*)name
:(float*)zone
:(float)init
:(float)min
:(float)max
:(float)step
295 if (self
= [super initWithValues
:ui
:zone
]) {
297 CGRect labelFrame
= CGRectMake(0.0, OFFSET_Y
+ WIDGET_SLICE
* index
- 5.f
, 130.0, 30.0);
298 UILabel
*label
= [[UILabel alloc
] initWithFrame
:labelFrame
];
299 [label setFont
:[UIFont boldSystemFontOfSize
:12]];
300 label
.textAlignment
= UITextAlignmentCenter
;
301 [label setText
:[[NSString alloc
] initWithCString
:name encoding
:NSASCIIStringEncoding
]];
302 label
.textColor
= [UIColor blackColor
];
303 label
.backgroundColor
= [UIColor lightGrayColor
];
304 [controler
.view addSubview
:label
];
306 CGRect frame
= CGRectMake(130.0f
, OFFSET_Y
+ WIDGET_SLICE
* index
, 110.0f
, 7.0f
);
307 fSlider
= [[UISlider alloc
] initWithFrame
:frame
];
308 [fSlider addTarget
:self action
:@
selector(changed
:)forControlEvents
:UIControlEventValueChanged
];
309 fSlider
.minimumValue
= min
;
310 fSlider
.maximumValue
= max
;
311 fSlider
.continuous
= YES
;
312 fSlider
.value
= init
;
313 [controler
.view addSubview
:fSlider
];
315 CGRect textFieldFrame
= CGRectMake(250.0, OFFSET_Y
+ WIDGET_SLICE
* index
, 60.0, 20.0);
316 fTextField
= [[UITextField alloc
] initWithFrame
:textFieldFrame
];
317 [fTextField setBorderStyle
:UITextBorderStyleLine
];
318 fTextField
.textAlignment
= UITextAlignmentCenter
;
319 [fTextField setEnabled
:NO
];
320 [fTextField setTextColor
:[UIColor blackColor
]];
321 [fTextField setFont
:[UIFont systemFontOfSize
:14]];
322 [fTextField setPlaceholder
:[NSString stringWithFormat
:@
"%1.2f", init
]];
323 [fTextField setBackgroundColor
:[UIColor whiteColor
]];
324 [fTextField setAdjustsFontSizeToFitWidth
:YES
];
325 fTextField
.keyboardType
= UIKeyboardTypeNumbersAndPunctuation
;
326 [controler
.view addSubview
:fTextField
];
331 - (void)changed
:(UISlider
*)sender
333 [self modifyZone
:sender
.value
];
334 [fTextField setPlaceholder
:[NSString stringWithFormat
:@
"%1.2f", sender
.value
]];
342 [fTextField setPlaceholder
:[NSString stringWithFormat
:@
"%1.2f", v
]];
348 [fTextField release
];
354 // --------------------------- Press button ---------------------------
356 #define kStdButtonWidth 100.0
357 #define kStdButtonHeight 40.0
359 @interface uiButton
: uiItem
364 - (void)pressed
:(UIButton
*)sender
;
365 - (void)released
:(UIButton
*)sender
;
367 - (id
)initWithValues
:(int)index
:(UI
*)ui
:(iPhoneViewController
*)controler
:(const char*)label
:(float*)zone
;
371 @implementation uiButton
373 - (id
)initWithValues
:(int)index
:(UI
*)ui
:(iPhoneViewController
*)controler
:(const char*)name
:(float*)zone
375 if (self
= [super initWithValues
:ui
:zone
]) {
376 fButton
= [[UIButton buttonWithType
:UIButtonTypeRoundedRect
] retain
];
377 fButton
.frame
= CGRectMake(SCREEN_WIDTH
/2 - kStdButtonWidth
/2, OFFSET_Y
+ WIDGET_SLICE
* index
- 5.f
, kStdButtonWidth
, kStdButtonHeight
);
378 [fButton setTitle
:[[NSString alloc
] initWithCString
:name encoding
:NSASCIIStringEncoding
] forState
:UIControlStateNormal
];
379 fButton
.backgroundColor
= [UIColor clearColor
];
380 [fButton addTarget
:self action
:@
selector(pressed
:) forControlEvents
:UIControlEventTouchDown
];
381 [fButton addTarget
:self action
:@
selector(released
:) forControlEvents
:UIControlEventTouchUpInside
];
382 [controler
.view addSubview
:fButton
];
387 - (void)pressed
:(UIButton
*)sender
389 [self modifyZone
:1.0f
];
392 - (void)released
:(UIButton
*)sender
394 [self modifyZone
:0.0f
];
401 //if (v > 0.0) gtk_button_pressed(fButton); else gtk_button_released(fButton);
412 // ------------------------------ Num Entry -----------------------------------
414 @interface uiNumEntry
: uiItem
416 UITextField
* fTextField
;
419 - (id
)initWithValues
:(int)index
:(UI
*)ui
:(iPhoneViewController
*)controler
:(const char*)label
:(float*)zone
:(float)init
:(float)min
:(float)max
:(float)step
;
423 @implementation uiNumEntry
425 - (id
)initWithValues
:(int)index
:(UI
*)ui
:(iPhoneViewController
*)controler
:(const char*)label
:(float*)zone
:(float)init
:(float)min
:(float)max
:(float)step
427 if (self
= [super initWithValues
:ui
:zone
]) {
428 CGRect textFieldFrame
= CGRectMake(SCREEN_WIDTH
/2 - kStdButtonWidth
/2, OFFSET_Y
+ WIDGET_SLICE
* index
- 5.f
, kStdButtonWidth
, kStdButtonHeight
);
429 fTextField
= [[UITextField alloc
] initWithFrame
:textFieldFrame
];
430 [fTextField setTextColor
:[UIColor blackColor
]];
431 [fTextField setFont
:[UIFont systemFontOfSize
:14]];
432 [fTextField setPlaceholder
:@
"<enter text>"];
433 [fTextField setBackgroundColor
:[UIColor whiteColor
]];
434 fTextField
.keyboardType
= UIKeyboardTypeDefault
;
436 [controler
.view addSubview
:fTextField
];
443 [fTextField release
];
450 class CocoaUI
: public UI
456 iPhoneViewController
* fViewController
;
459 list
<uiItem
*> fWidgetList
;
461 void insert(const char* label
, uiItem
* widget
)
463 fWidgetList
.push_back(widget
);
464 [fViewController
.view setContentSize
:CGSizeMake(320, WIDGET_SLICE
* fWidgetList
.size() + 100.f
)];
469 // -- Labels and metadata
471 // virtual void declare (float* zone, const char* key, const char* value);
472 // virtual int checkLabelOptions (GtkWidget* widget, const string& fullLabel, string& simplifiedLabel);
473 // virtual void checkForTooltip (float* zone, GtkWidget* widget);
477 CocoaUI(UIWindow
* window
, iPhoneViewController
* viewController
, MY_Meta
* metadata
)
479 fViewController
= viewController
;
481 fMetadata
= metadata
;
483 CGRect titleFrame
= CGRectMake(0.0, 0.0f
, 320.0, 75.0);
484 UIView
*titleView
= [[UIView alloc
] initWithFrame
:titleFrame
];
485 titleView
.backgroundColor
= [UIColor brownColor
];
486 [fViewController
.view addSubview
:titleView
];
488 if (fMetadata
->find("name") != fMetadata
->end()) {
489 const char* name
= (*fMetadata
->find("name")).second
;
490 CGRect labelFrame
= CGRectMake(0.0, 20.0f
, 320.0, 30.0);
491 UILabel
*label
= [[UILabel alloc
] initWithFrame
:labelFrame
];
492 [label setFont
:[UIFont boldSystemFontOfSize
:18]];
493 label
.textAlignment
= UITextAlignmentCenter
;
494 [label setText
:[[NSString alloc
] initWithCString
:name encoding
:NSASCIIStringEncoding
]];
495 label
.textColor
= [UIColor blackColor
];
496 label
.backgroundColor
= [UIColor brownColor
];
497 [fViewController
.view addSubview
:label
];
500 if (fMetadata
->find("author") != fMetadata
->end()) {
501 const char* name
= (*fMetadata
->find("author")).second
;
502 CGRect labelFrame
= CGRectMake(0.0, 45.0f
, 320.0, 30.0);
503 UILabel
*label
= [[UILabel alloc
] initWithFrame
:labelFrame
];
504 [label setFont
:[UIFont boldSystemFontOfSize
:14]];
505 label
.textAlignment
= UITextAlignmentCenter
;
506 [label setText
:[[NSString alloc
] initWithCString
:name encoding
:NSASCIIStringEncoding
]];
507 label
.textColor
= [UIColor blackColor
];
508 label
.backgroundColor
= [UIColor brownColor
];
509 [fViewController
.view addSubview
:label
];
512 [window addSubview
:viewController
.view
];
513 [window makeKeyAndVisible
];
518 [fViewController release
];
522 virtual void openFrameBox(const char* label
)
524 virtual void openTabBox(const char* label
= "")
526 virtual void openHorizontalBox(const char* label
= "")
528 virtual void openVerticalBox(const char* label
= "")
531 // -- extra widget's layouts
533 virtual void openDialogBox(const char* label
, float* zone
)
535 virtual void openEventBox(const char* label
= "")
537 virtual void openHandleBox(const char* label
= "")
539 virtual void openExpanderBox(const char* label
, float* zone
)
542 virtual void closeBox()
545 //virtual void adjustStack(int n);
549 virtual void addButton(const char* label
, float* zone
)
551 uiItem
* item
= [[uiButton alloc
] initWithValues
:fWidgetList
.size():this:fViewController
:label
:zone
];
554 virtual void addToggleButton(const char* label
, float* zone
)
556 virtual void addCheckButton(const char* label
, float* zone
)
558 virtual void addVerticalSlider(const char* label
, float* zone
, float init
, float min
, float max
, float step
)
560 uiItem
* item
= [[uiSlider alloc
] initWithValues
:fWidgetList
.size():this:fViewController
:label
:zone
:init
:min
:max
:step
];
563 virtual void addHorizontalSlider(const char* label
, float* zone
, float init
, float min
, float max
, float step
)
565 uiItem
* item
= [[uiSlider alloc
] initWithValues
:fWidgetList
.size():this:fViewController
:label
:zone
:init
:min
:max
:step
];
568 virtual void addNumEntry(const char* label
, float* zone
, float init
, float min
, float max
, float step
)
570 uiItem
* item
= [[uiNumEntry alloc
] initWithValues
:fWidgetList
.size():this:fViewController
:label
:zone
:init
:min
:max
:step
];
574 // -- passive display widgets
576 virtual void addNumDisplay(const char* label
, float* zone
, int precision
)
578 virtual void addTextDisplay(const char* label
, float* zone
, const char* names
[], float min
, float max
)
580 virtual void addHorizontalBargraph(const char* label
, float* zone
, float min
, float max
)
582 virtual void addVerticalBargraph(const char* label
, float* zone
, float min
, float max
)
590 virtual void declare(float* zone
, const char* key
, const char* value
)
596 // global static fields
598 list
<UI
*> UI::fGuiList
;
601 bool GTKUI::fInitialized = false;
602 map<float*, float> GTKUI::fGuiSize;
603 map<float*, string> GTKUI::fTooltip;