Debian Codegraph @

src/GUI_Motif.cc

raw symbols SCIP
1 /*
2 This file is part of the GVars3 Library.
3
4 Copyright (C) 2005 The Authors
5
6 This library is free software, see LICENSE file for details
7 */
8 #include "gvars3/instances.h"
9 #include "gvars3/GUI_Motif.h"
10 #include "gvars3/GStringUtil.h"
11 #include <Xm/PushB.h>
12 #include <Xm/RowColumn.h>
13 #include <Xm/FileSB.h>
14 #include <Xm/Label.h>
15 #include <Xm/ScrollBar.h>
16 #include <Xm/ToggleB.h>
17 #include <Xm/TextF.h>
18 #include <Xm/Protocols.h>
19 #include <Xm/AtomMgr.h>
20 #include <pthread.h>
21 #include <unistd.h>
22 #include <sstream>
23
24 using namespace std;
25 namespace GVars3
26 {
27
28 GUI_Motif::GUI_Motif(class GUI *pGUI, GVars2 *pGV2)
29 {
30 mpGUI=pGUI;
31 mpGV2=pGV2;
32 mpDisplay=NULL;
33 mpGUI->RegisterCommand("GUI.InitXInterface", InitXInterfaceCB, this);
34 mpGUI->RegisterCommand("GUI_Motif.InitXInterface", InitXInterfaceCB, this);
35 }
36
37
38 void GUI_Motif::InitXInterfaceCB(void* ptr, string , string sParams)
39 {
40 ((GUI_Motif*)(ptr))->InitXInterface(sParams);
41 }
42
43 void GUI_Motif::AddWindowCB(void* ptr, string , string sParams)
44 {
45 ((GUI_Motif*)(ptr))->AddWindow(sParams);
46 }
47
48
49
50 void GUI_Motif::AddWindow(string sParams)
51 {
52 if(!mpDisplay)
53 {
54 cout << "! GUI_Motif::AddWindow: No display. Call InitXInterface first." << endl;
55 return;
56 }
57
58 vector<string> vs = ChopAndUnquoteString(sParams);
59 if(((vs.size()<1) || (vs.size()>3)) )
60 {
61 cout << "! GUI_Motif::AddWindow: need 1 - 3 params: Name, Caption=Name, Width=200 " << endl;
62 return;
63 };
64
65 if(mmWindows.count(vs[0])>0)
66 {
67 cout << "? GUI_Motif::AddWindow: A window with id \"" << vs[0] << "\" already exists." << endl;
68 return;
69 }
70
71 string sCaption;
72 if(vs.size()>=2)
73 sCaption=vs[1];
74 else
75 sCaption=vs[0];
76
77
78 GUIWindowStruct gws;
79 gws.PushButtonMap.clear();
80 gws.MonitorMap.clear();
81 gws.ToggleButtonMap.clear();
82 gws.SliderMap.clear();
83 gws.nWidth = 200;
84 if(vs.size()>2)
85 {
86 int *pn = ParseAndAllocate<int>(vs[2]);
87 if(pn)
88 {
89 gws.nWidth=*pn;
90 delete pn;
91 };
92 }
93 gws.nHeight = 30;
94 gws.die=0;
95
96 pthread_mutex_lock((pthread_mutex_t*)mpMutex);
97
98 Arg al[10];
99 int ac = 0;
100 gws.wTopLevel=XtAppCreateShell(sCaption.c_str(), sCaption.c_str(), applicationShellWidgetClass, mpDisplay, al, ac);
101
102 ac=0;
103 XtSetArg(al[ac], XmNpacking, XmPACK_TIGHT);ac++;
104 XtSetArg(al[ac], XmNorientation, XmVERTICAL);ac++;
105 XtSetArg(al[ac], XmNadjustLast, False);ac++;
106 XtSetArg(al[ac], XmNresizeHeight, True); ac++;
107 XtSetArg(al[ac], XmNresizeWidth, False); ac++;
108
109 gws.wRowCol=XmCreateRowColumn(gws.wTopLevel,const_cast<char*>("GUI_Motif_rowcol"),al,ac);
110
111 XtManageChild(gws.wRowCol);
112 XtRealizeWidget(gws.wTopLevel);
113
114 XEvent xevent;
115 XtAppNextEvent(mxtac,&xevent);
116 XtDispatchEvent(&xevent);
117 DoMotifEvents();
118 XResizeWindow(mpDisplay, XtWindow(gws.wTopLevel), gws.nWidth, gws.nHeight);
119 XResizeWindow(mpDisplay, XtWindow(gws.wRowCol), gws.nWidth, gws.nHeight);
120 DoMotifEvents();
121
122
123 Atom delwinAtom1;
124 delwinAtom1 = XmInternAtom (mpDisplay,const_cast<char*>("WM_DELETE_WINDOW"), False);
125 XmAddWMProtocolCallback (gws.wTopLevel, delwinAtom1, RemoveWindowCB, this);
126 XtVaSetValues(gws.wTopLevel, XmNdeleteResponse, XmDO_NOTHING, NULL);
127
128 mmWindows[vs[0]]=gws;
129
130 mpGUI->RegisterCommand(vs[0] + ".AddPushButton", AddPushButtonCB, this);
131 mpGUI->RegisterCommand(vs[0] + ".AddToggleButton", AddToggleButtonCB, this);
132 mpGUI->RegisterCommand(vs[0] + ".AddMonitor", AddMonitorCB, this);
133 mpGUI->RegisterCommand(vs[0] + ".AddLabel", AddLabelCB, this);
134 mpGUI->RegisterCommand(vs[0] + ".AddSlider", AddSliderCB, this);
135 mpGUI->RegisterCommand(vs[0] + ".AddSpin", AddSpinCB, this);
136 mpGUI->RegisterCommand(vs[0] + ".Destroy", DestroyCB, this);
137
138 pthread_mutex_unlock((pthread_mutex_t*)mpMutex);
139
140
141 };
142
143
144 void GUI_Motif::InitXInterface(string sParams)
145 {
146 vector<string> vs = ChopAndUnquoteString(sParams);
147 string sDisplay="";
148 if(vs.size()>0)
149 sDisplay=vs[0];
150 if(vs.size()>1)
151 msName = vs[1];
152 else
153 msName = "GUI";
154
155
156 if(mpDisplay)
157 {
158 cout << "??GUI_Motif::InitXInterface: display already initialised.." << endl;
159 return;
160 }
161
162 if(sDisplay=="")
163 mpDisplay=XOpenDisplay(NULL);
164 else
165 mpDisplay=XOpenDisplay(sDisplay.c_str());
166
167 if(!mpDisplay)
168 {
169 cout << "??GUI_Motif::InitXInterface: Could not open display \""<<sDisplay<< "\"." << endl;
170 return;
171 }
172
173
174 // Ok, set up some widgets!
175 int argc=1;
176 char* argv[] = {0};
177
178 XtToolkitInitialize();
179 mxtac=XtCreateApplicationContext();
180 XtDisplayInitialize(mxtac, mpDisplay, "GUI_Motif", "GUI_Motif", NULL, 0, &argc, argv);
181
182
183 pthread_mutexattr_t attr;
184 pthread_mutexattr_init(&attr);
185 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
186 mpMutex = new(pthread_mutex_t);
187 pthread_mutex_init(mpMutex, &attr);
188 mpGUI->RegisterCommand(msName+".AddWindow", AddWindowCB, this);
189
190 }
191
192 void GUI_Motif::start_thread()
193 {
194 pthread_t t;
195 pthread_create(&t, NULL, GUI_Motif_Thread_CB, this);
196 }
197
198 void GUI_Motif::RemoveWindow(Widget w)
199 {
200 for(map<string,GUIWindowStruct>::iterator i = mmWindows.begin(); i!=mmWindows.end(); i++)
201 if (i->second.wTopLevel == w)
202 {
203 DestroyWindow(i->first + ".Destroy");
204 return;
205 };
206 }
207
208
209 void GUI_Motif::RemoveWindowCB(Widget w, void* client_data, void* )
210 {
211 ((GUI_Motif *) client_data)->RemoveWindow(w);
212 }
213
214 void GUI_Motif::DestroyCB(void* ptr, string sCommand, string)
215 {
216 ((GUI_Motif*)(ptr))->DestroyWindow(sCommand);
217 }
218
219 void GUI_Motif::DestroyWindow(string sCommand)
220 {
221 string sWindowName = sCommand;
222 string sCommandSuffix(".Destroy");
223 sWindowName.resize(sCommand.length() - sCommandSuffix.length());
224 mpGUI->UnRegisterCommand(sWindowName + ".AddPushButton");
225 mpGUI->UnRegisterCommand(sWindowName + ".AddToggleButton");
226 mpGUI->UnRegisterCommand(sWindowName + ".AddMonitor");
227 mpGUI->UnRegisterCommand(sWindowName + ".AddLabel");
228 mpGUI->UnRegisterCommand(sWindowName + ".Destroy");
229 XtUnrealizeWidget(mmWindows[sWindowName].wTopLevel);
230 mmWindows[sWindowName].die=1;
231 DoMotifEvents();
232
233 };
234
235 ////////////////////////////////////////////////////////////////////////////////
236 //
237 // Push buttons
238 //
239
240 void GUI_Motif::AddPushButtonCB(void* ptr, string sCommand, string sParams)
241 {
242 ((GUI_Motif*)(ptr))->AddPushButton(sCommand, sParams);
243 }
244
245
246 void GUI_Motif::AddPushButton(string sCommand, string sParams)
247 {
248
249 vector<string> vs = ChopAndUnquoteString(sParams);
250 if(vs.size()!=2)
251 {
252 cout << "! GUI_Motif::AddPushButton: Need 2 params (name, command)." << endl;
253 return;
254 };
255
256
257
258 string sWindowName = sCommand;
259 string sCommandSuffix(".AddPushButton");
260 sWindowName.resize(sCommand.length() - sCommandSuffix.length());
261 Widget w;
262
263 Arg al[10];
264 int ac = 0;
265 char szName[1000];
266 for(int i=0;i<1000;i++) szName[i]=0;
267 vs[0].copy(szName,999);
268
269 for(map<Widget,string>::iterator i = mmWindows[sWindowName].PushButtonMap.begin();
270 i!=mmWindows[sWindowName].PushButtonMap.end();
271 i++)
272 if(i->second == vs[1])
273 {
274 cout << "? GUI_Motif::AddPushButton: Button with command \"" << vs[1] << "\" already there.. skipping. " << endl;
275 return;
276 }
277
278 pthread_mutex_lock((pthread_mutex_t*)mpMutex);
279
280
281 w = XmCreatePushButton(mmWindows[sWindowName].wRowCol, szName,al,ac);
282 XtManageChild(w);
283 mmWindows[sWindowName].PushButtonMap[w] = vs[1];
284 XtAddCallback(w, XmNactivateCallback, ButtonHandlerCB, this);
285
286 XWindowAttributes window_attributes_return;
287 XGetWindowAttributes(mpDisplay, XtWindow(w), &window_attributes_return);
288 int nButtonHeight = window_attributes_return.height;
289 XGetWindowAttributes(mpDisplay, XtWindow(mmWindows[sWindowName].wTopLevel), &window_attributes_return);
290 mmWindows[sWindowName].nWidth = (mmWindows[sWindowName].nWidth > window_attributes_return.width) ? mmWindows[sWindowName].nWidth : window_attributes_return.width;
291 mmWindows[sWindowName].nHeight = (mmWindows[sWindowName].nHeight > window_attributes_return.height) ? mmWindows[sWindowName].nHeight : window_attributes_return.height;
292 mmWindows[sWindowName].nHeight += (nButtonHeight + 3);
293 XResizeWindow(mpDisplay, XtWindow(mmWindows[sWindowName].wTopLevel),
294 mmWindows[sWindowName].nWidth,
295 mmWindows[sWindowName].nHeight);
296 DoMotifEvents();
297 pthread_mutex_unlock((pthread_mutex_t*)mpMutex);
298
299 }
300
301 ////////////////////////////////////////////////////////////////////////////////
302 //
303 // Text input boxes
304 //
305
306 void GUI_Motif::AddSpinCB(void* ptr, string sCommand, string sParams)
307 {
308 ((GUI_Motif*)(ptr))->AddSpin(sCommand, sParams);
309 }
310
311
312 void GUI_Motif::AddSpin(string sCommand, string sParams)
313 {
314
315 vector<string> vs = ChopAndUnquoteString(sParams);
316 if(vs.size() < 3 || vs.size() > 4)
317 {
318 cout << "! GUI_Motif::AddSpin: Need 3--4 params (gvar, min, max [, title])." << endl;
319 return;
320 };
321
322
323 string sWindowName = sCommand;
324 string sCommandSuffix(".AddSpin");
325 sWindowName.resize(sCommand.length() - sCommandSuffix.length());
326 Widget w;
327
328 Arg al[10];
329 int ac = 0;
330 char szName[1000];
331 for(int i=0;i<1000;i++) szName[i]=0;
332 vs[0].copy(szName,999);
333
334 pthread_mutex_lock((pthread_mutex_t*)mpMutex);
335
336
337 w = XmCreateTextField(mmWindows[sWindowName].wRowCol, szName,al,ac);
338 mmWindows[sWindowName].SpinMap[w] = make_pair(vs[0], string(""));
339 XtManageChild(w);
340
341 XtAddCallback(w, XmNvalueChangedCallback, TextBoxCB, this);
342
343 XWindowAttributes window_attributes_return;
344 XGetWindowAttributes(mpDisplay, XtWindow(w), &window_attributes_return);
345 int nButtonHeight = window_attributes_return.height;
346 XGetWindowAttributes(mpDisplay, XtWindow(mmWindows[sWindowName].wTopLevel), &window_attributes_return);
347 mmWindows[sWindowName].nWidth = (mmWindows[sWindowName].nWidth > window_attributes_return.width) ? mmWindows[sWindowName].nWidth : window_attributes_return.width;
348 mmWindows[sWindowName].nHeight = (mmWindows[sWindowName].nHeight > window_attributes_return.height) ? mmWindows[sWindowName].nHeight : window_attributes_return.height;
349 mmWindows[sWindowName].nHeight += (nButtonHeight + 3);
350 XResizeWindow(mpDisplay, XtWindow(mmWindows[sWindowName].wTopLevel),
351 mmWindows[sWindowName].nWidth,
352 mmWindows[sWindowName].nHeight);
353 DoMotifEvents();
354 pthread_mutex_unlock((pthread_mutex_t*)mpMutex);
355
356 }
357
358 void GUI_Motif::TextBoxCB(Widget w, XtPointer ptrMe, XtPointer xtpCall)
359 {
360 ((GUI_Motif*)ptrMe) -> TextBox(w, xtpCall);
361 }
362
363
364
365 // Handles the actual callback work
366 void GUI_Motif::TextBox(Widget w, XtPointer )
367 {
368 for(map<string, GUIWindowStruct>::iterator i=mmWindows.begin(); i != mmWindows.end(); i++)
369 {
370
371 if(i->second.SpinMap.count(w))
372 {
373 char* c;
374 XmTextPosition p;
375 XtVaGetValues(w, XmNvalue, &c, XmNcursorPosition, &p, NULL);
376
377 string cs(c);
378 if(cs == "")
379 cs = "0";
380
381 p=1;
382
383 GV3::set_var(i->second.SpinMap[w].first, cs.c_str());
384 string s = GV3::get_var(i->second.SpinMap[w].first);
385
386 if(s != c)
387 {
388 i->second.SpinMap[w].second = s;
389 XtRemoveCallback(w, XmNvalueChangedCallback, TextBoxCB, this);
390 p = min((int)p, (int)s.size());
391 XtVaSetValues(w, XmNvalue, s.c_str(), XmNcursorPosition, p, NULL);
392 XtAddCallback(w, XmNvalueChangedCallback, TextBoxCB, this);
393 }
394 }
395 }
396 }
397
398 ////////////////////////////////////////////////////////////////////////////////
399 //
400 //
401 //
402
403 void GUI_Motif::AddToggleButtonCB(void* ptr, string sCommand, string sParams)
404 {
405 ((GUI_Motif*)(ptr))->AddToggleButton(sCommand, sParams);
406 }
407
408
409 void GUI_Motif::AddToggleButton(string sCommand, string sParams)
410 {
411 vector<string> vs = ChopAndUnquoteString(sParams);
412 if(!((vs.size() == 2) || (vs.size()==3)))
413 {
414 cout << "! GUI_Motif::AddToggleButton: Need 2 or 3 params (button name, gvar2_int name, {true,false}=false )." << endl;
415 return;
416 };
417 string sWindowName = sCommand;
418 string sCommandSuffix(".AddToggleButton");
419 sWindowName.resize(sCommand.length() - sCommandSuffix.length());
420
421 for(map<Widget,toggleMapStruct>::iterator i = mmWindows[sWindowName].ToggleButtonMap.begin();
422 i!=mmWindows[sWindowName].ToggleButtonMap.end();
423 i++)
424 if(i->second.sName == vs[0])
425 {
426 cout << "? GUI_Motif::AddToggleButton: Button with name \"" << vs[0] << "\" already there.. skipping. " << endl;
427 return;
428 }
429
430 toggleMapStruct tms;
431 tms.sName = vs[0];
432 int nDefault = 0;
433 if(vs.size()==3)
434 {
435 if(vs[2]=="true")
436 nDefault = 1;
437 else
438 nDefault = 0;
439 }
440
441 mpGV2->Register(tms.gvn,vs[1],nDefault,true);
442 tms.nCache = *tms.gvn;
443
444
445 pthread_mutex_lock((pthread_mutex_t*)mpMutex);
446
447 Widget w;
448 Arg al[10];
449 int ac = 0;
450 char szName[1000];
451 for(int i=0;i<1000;i++) szName[i]=0;
452 vs[0].copy(szName,999);
453
454 w = XmCreateToggleButton(mmWindows[sWindowName].wRowCol, szName,al,ac);
455 XtManageChild(w);
456 mmWindows[sWindowName].ToggleButtonMap[w] = tms;
457 XtAddCallback(w, XmNvalueChangedCallback, ButtonHandlerCB, this);
458 if(tms.nCache) XmToggleButtonSetState(w,true,true);
459
460 XWindowAttributes window_attributes_return;
461 XGetWindowAttributes(mpDisplay, XtWindow(w), &window_attributes_return);
462 int nButtonHeight = window_attributes_return.height;
463 XGetWindowAttributes(mpDisplay, XtWindow(mmWindows[sWindowName].wTopLevel), &window_attributes_return);
464 mmWindows[sWindowName].nWidth = (mmWindows[sWindowName].nWidth > window_attributes_return.width) ? mmWindows[sWindowName].nWidth : window_attributes_return.width;
465 mmWindows[sWindowName].nHeight = (mmWindows[sWindowName].nHeight > window_attributes_return.height) ? mmWindows[sWindowName].nHeight : window_attributes_return.height;
466 mmWindows[sWindowName].nHeight += (nButtonHeight + 3);
467 XResizeWindow(mpDisplay, XtWindow(mmWindows[sWindowName].wTopLevel),
468 mmWindows[sWindowName].nWidth,
469 mmWindows[sWindowName].nHeight);
470 DoMotifEvents();
471 pthread_mutex_unlock((pthread_mutex_t*)mpMutex);
472
473 }
474
475 ////////////////////////////////////////////////////////////////////////////////
476 //
477 // Monitor
478 //
479 void GUI_Motif::AddMonitorCB(void* ptr, string sCommand, string sParams)
480 {
481 ((GUI_Motif*)(ptr))->AddMonitor(sCommand, sParams);
482 }
483
484
485 void GUI_Motif::AddMonitor(string sCommand, string sParams)
486 {
487 if(!mpDisplay)
488 {
489 cout << "! GUI_Motif::AddMonitor: GUI_Motif not initialised. " << endl;
490 return;
491 }
492 vector<string> vs = ChopAndUnquoteString(sParams);
493 if((vs.size() <2) || (vs.size() > 3))
494 {
495 cout << "! GUI_Motif::AddMonitor: Need 2 or 3 params: label, gvar name, num 20ms cycles to skip = 0 " << endl;
496 return;
497 };
498 string sWindowName = sCommand;
499 string sCommandSuffix(".AddMonitor");
500 sWindowName.resize(sCommand.length() - sCommandSuffix.length());
501
502 for(map<Widget,monitorMapStruct>::iterator i = mmWindows[sWindowName].MonitorMap.begin();
503 i!=mmWindows[sWindowName].MonitorMap.end();
504 i++)
505 if(i->second.sLabel == vs[0])
506 {
507 cout << "? GUI_Motif::AddMontiro: Montor with Label \"" << vs[0] << "\" already there.. skipping. " << endl;
508 return;
509 }
510
511
512 pthread_mutex_lock((pthread_mutex_t*)mpMutex);
513
514 Widget w;
515 Arg al[10];
516 int ac = 0;
517 w = XmCreateLabel(mmWindows[sWindowName].wRowCol,const_cast<char*>("(wait)"),al,ac);
518 XtManageChild(w);
519 monitorMapStruct &mms = mmWindows[sWindowName].MonitorMap[w];
520 mms.sLabel = vs[0];
521 mms.sVarName = vs[1];
522 if(vs.size()==3)
523 {
524 int* pn = ParseAndAllocate<int>(vs[2]);
525 if (pn)
526 {
527 mms.nDelaySetting = *pn;
528 delete pn;
529 }
530 else
531 mms.nDelaySetting = 0;
532 }
533 else
534 mms.nDelaySetting = 0;
535 mms.nCurrentDelay=0;
536
537 XWindowAttributes window_attributes_return;
538 XGetWindowAttributes(mpDisplay, XtWindow(w), &window_attributes_return);
539 int nButtonHeight = window_attributes_return.height;
540 XGetWindowAttributes(mpDisplay, XtWindow(mmWindows[sWindowName].wTopLevel), &window_attributes_return);
541 mmWindows[sWindowName].nWidth = (mmWindows[sWindowName].nWidth > window_attributes_return.width) ? mmWindows[sWindowName].nWidth : window_attributes_return.width;
542 mmWindows[sWindowName].nHeight = (mmWindows[sWindowName].nHeight > window_attributes_return.height) ? mmWindows[sWindowName].nHeight : window_attributes_return.height;
543 mmWindows[sWindowName].nHeight += (nButtonHeight + 3);
544 XResizeWindow(mpDisplay, XtWindow(mmWindows[sWindowName].wTopLevel),
545 mmWindows[sWindowName].nWidth,
546 mmWindows[sWindowName].nHeight);
547 DoMotifEvents();
548 pthread_mutex_unlock((pthread_mutex_t*)mpMutex);
549
550 }
551
552 ////////////////////////////////////////////////////////////////////////////////
553 //
554 // Label
555 //
556 void GUI_Motif::AddLabelCB(void* ptr, string sCommand, string sParams)
557 {
558 ((GUI_Motif*)(ptr))->AddLabel(sCommand, sParams);
559 }
560
561
562 void GUI_Motif::AddLabel(string sCommand, string sParams)
563 {
564 if(!mpDisplay)
565 {
566 cout << "! GUI_Motif::AddMonitor: GUI_Motif not initialised. " << endl;
567 return;
568 }
569 vector<string> vs = ChopAndUnquoteString(sParams);
570 if(vs.size() != 1)
571 {
572 cout << "! GUI_Motif::AddMonitor: Need 1 params: label " << endl;
573 return;
574 };
575 string sWindowName = sCommand;
576 string sCommandSuffix(".AddLabel");
577 sWindowName.resize(sCommand.length() - sCommandSuffix.length());
578
579 pthread_mutex_lock((pthread_mutex_t*)mpMutex);
580
581 labels.push_back(vs[0]);
582
583 Widget w;
584 Arg al[10];
585 int ac = 0;
586 w = XmCreateLabel(mmWindows[sWindowName].wRowCol,const_cast<char*>(labels.back().c_str()),al,ac);
587 XtManageChild(w);
588
589 XWindowAttributes window_attributes_return;
590 XGetWindowAttributes(mpDisplay, XtWindow(w), &window_attributes_return);
591 int nButtonHeight = window_attributes_return.height;
592 XGetWindowAttributes(mpDisplay, XtWindow(mmWindows[sWindowName].wTopLevel), &window_attributes_return);
593 mmWindows[sWindowName].nWidth = (mmWindows[sWindowName].nWidth > window_attributes_return.width) ? mmWindows[sWindowName].nWidth : window_attributes_return.width;
594 mmWindows[sWindowName].nHeight = (mmWindows[sWindowName].nHeight > window_attributes_return.height) ? mmWindows[sWindowName].nHeight : window_attributes_return.height;
595 mmWindows[sWindowName].nHeight += (nButtonHeight + 3);
596 XResizeWindow(mpDisplay, XtWindow(mmWindows[sWindowName].wTopLevel),
597 mmWindows[sWindowName].nWidth,
598 mmWindows[sWindowName].nHeight);
599 DoMotifEvents();
600 pthread_mutex_unlock((pthread_mutex_t*)mpMutex);
601
602 }
603
604
605 void GUI_Motif::AddSliderCB(void* ptr, string sCommand, string sParams)
606 {
607 ((GUI_Motif*)(ptr))->AddSlider(sCommand, sParams);
608 }
609
610
611 void GUI_Motif::AddSlider(string sCommand, string sParams)
612 {
613 if(!mpDisplay)
614 {
615 cout << "! GUI_Motif::AddSlider: GUI_Motif not initialised. " << endl;
616 return;
617 }
618 vector<string> vs = ChopAndUnquoteString(sParams);
619 if((vs.size() != 3))
620 {
621 cout << "! GUI_Motif::AddSlider: 3 params: gvar name, min, max " << endl;
622 return;
623 };
624 string sWindowName = sCommand;
625 string sCommandSuffix(".AddSlider");
626 sWindowName.resize(sCommand.length() - sCommandSuffix.length());
627 for(map<Widget,sliderMapStruct>::iterator i = mmWindows[sWindowName].SliderMap.begin();
628 i!=mmWindows[sWindowName].SliderMap.end();
629 i++)
630 if(i->second.sVarName == vs[0])
631 {
632 cout << "? GUI_Motif::AddSlider: Slider with gvar \"" << vs[0] << "\" already there.. skipping. " << endl;
633 return;
634 }
635
636 sliderMapStruct sms;
637 sms.sVarName = vs[0];
638 double *pd;
639 pd = ParseAndAllocate<double>(vs[1]);
640 if(!pd)
641 {
642 cout << "? GUI_Motif::AddSlider: Could not parse min range parameter \"" << vs[1] << "\"."<< endl;
643 return;
644 }
645 sms.dMin = *pd;
646 delete pd;
647 pd=ParseAndAllocate<double>(vs[2]);
648 if(!pd)
649 {
650 cout << "? GUI_Motif::AddSlider: Could not parse max range parameter \"" << vs[2] << "\"."<< endl;
651 return;
652 }
653 sms.dMax=*pd;
654 delete pd;
655 if(sms.dMax == sms.dMin)
656 {
657 cout << "? GUI_Motif::AddSlider: Cannot have max = min! " << endl;
658 return;
659 }
660
661
662
663 pthread_mutex_lock((pthread_mutex_t*)mpMutex);
664
665 Widget w;
666 Arg al[10];
667 int ac = 0;
668
669 XtSetArg(al[ac], XmNorientation, XmHORIZONTAL); ac++;
670 // XtSetArg(al[ac], XmNsliderMark, XmETCHED_LINE); ac++;
671 XtSetArg(al[ac], XmNwidth, mmWindows[sWindowName].nWidth - 5); ac++;
672 XtSetArg(al[ac], XmNmaximum, 310); ac++;
673 XtSetArg(al[ac], XmNsliderSize, 10); ac++;
674
675 w = XmCreateScrollBar(mmWindows[sWindowName].wRowCol,const_cast<char*>("SliderName"),al,ac);
676 XtManageChild(w);
677 XtAddCallback(w, XmNdragCallback, ButtonHandlerCB, this);
678 XtAddCallback(w, XmNvalueChangedCallback, ButtonHandlerCB, this);
679
680 mmWindows[sWindowName].SliderMap[w] = sms;
681
682 // if(vs.size()==3)
683 // {
684 // int* pn = ParseAndAllocate<int>(vs[2]);
685 // if (pn)
686 // {
687 // mms.nDelaySetting = *pn;
688 // delete pn;
689 // }
690 // else
691 // mms.nDelaySetting = 0;
692 // }
693 // else
694 // mms.nDelaySetting = 0;
695 // mms.nCurrentDelay=0;
696
697 XWindowAttributes window_attributes_return;
698 XGetWindowAttributes(mpDisplay, XtWindow(w), &window_attributes_return);
699 int nButtonHeight = window_attributes_return.height;
700 XGetWindowAttributes(mpDisplay, XtWindow(mmWindows[sWindowName].wTopLevel), &window_attributes_return);
701 mmWindows[sWindowName].nWidth = (mmWindows[sWindowName].nWidth > window_attributes_return.width) ? mmWindows[sWindowName].nWidth : window_attributes_return.width;
702 mmWindows[sWindowName].nHeight = (mmWindows[sWindowName].nHeight > window_attributes_return.height) ? mmWindows[sWindowName].nHeight : window_attributes_return.height;
703 mmWindows[sWindowName].nHeight += (nButtonHeight + 3);
704 XResizeWindow(mpDisplay, XtWindow(mmWindows[sWindowName].wTopLevel),
705 mmWindows[sWindowName].nWidth,
706 mmWindows[sWindowName].nHeight);
707 DoMotifEvents();
708 pthread_mutex_unlock((pthread_mutex_t*)mpMutex);
709
710 }
711
712
713
714 // Callback function for MOTIF events, wrapper for button_F
715
716 void GUI_Motif::ButtonHandlerCB(Widget w, XtPointer ptrMe, XtPointer xtpCall)
717 {
718 ((GUI_Motif*)ptrMe) -> ButtonHandler(w, xtpCall);
719 }
720
721
722 // Handles the actual callback work
723 void GUI_Motif::ButtonHandler(Widget w, XtPointer xtpCall)
724 {
725
726 for(map<string, GUIWindowStruct>::iterator i=mmWindows.begin();i!=mmWindows.end();i++)
727 {
728 if(i->second.PushButtonMap.count(w)>0)
729 {
730 mpGUI->ParseLine(i->second.PushButtonMap[w]);
731 }
732 else if(i->second.ToggleButtonMap.count(w)>0)
733 {
734 *i->second.ToggleButtonMap[w].gvn = i->second.ToggleButtonMap[w].nCache = ((XmToggleButtonCallbackStruct*) xtpCall)->set;
735 }
736 else if(i->second.SliderMap.count(w)>0)
737 {
738 sliderMapStruct &sms = i->second.SliderMap[w];
739 double dRawValue = (double) (((XmScrollBarCallbackStruct*)xtpCall)->value);
740 ostringstream ost;
741 ost.str()="";
742 ost << (dRawValue / 300.0) * (sms.dMax-sms.dMin) + sms.dMin;
743 mpGV2->SetVar(sms.sVarName, ost.str(), 1);
744 sms.sCachedValue = GV3::get_var(sms.sVarName);
745
746 }
747 };
748
749 }
750
751 void GUI_Motif::GUI_Motif_Thread()
752 {
753 while(mpDisplay)
754 {
755 poll();
756 usleep(20000);
757 }
758 }
759
760 void GUI_Motif::poll()
761 {
762 if(!mpDisplay)
763 return;
764 static char szString[1000];
765
766 pthread_mutex_lock(mpMutex);
767 for(map<string,GUIWindowStruct>::iterator w=mmWindows.begin();w!=mmWindows.end();w++)
768 {
769 for(map< Widget, toggleMapStruct >::iterator i = w->second.ToggleButtonMap.begin(); i!=w->second.ToggleButtonMap.end(); i++)
770 {
771 if(*(i->second.gvn)!=(i->second.nCache))
772 {
773 i->second.nCache = *(i->second.gvn);
774 if(i->second.nCache)
775 XmToggleButtonSetState(i->first,true,false);
776 else
777 XmToggleButtonSetState(i->first,false,false);
778 };
779 }
780
781 for(map < Widget, monitorMapStruct>::iterator i = w->second.MonitorMap.begin(); i!=w->second.MonitorMap.end(); i++)
782 {
783 if( (i->second.nCurrentDelay--) >0)
784 continue;
785 i->second.nCurrentDelay=i->second.nDelaySetting;
786 ostringstream os;
787 os.str()="";
788 os << i->second.sLabel << ":" << mpGV2->StringValue(i->second.sVarName);
789 if(os.str() == i->second.sCache)
790 continue;
791 i->second.sCache=os.str();
792 strncpy(szString,os.str().c_str(),999);
793 Arg al;
794 XmString xmstr;
795 xmstr = XmStringCreate(szString, XmFONTLIST_DEFAULT_TAG);
796 XtSetArg (al, XmNlabelString, xmstr);
797 XtSetValues(i->first, &al, 1);
798 XmStringFree(xmstr);
799 };
800
801 for(map < Widget, sliderMapStruct>::iterator i = w->second.SliderMap.begin(); i!=w->second.SliderMap.end(); i++)
802 {
803 sliderMapStruct &sms = i->second;
804 string sNewValue = GV3::get_var(sms.sVarName);
805 if(sNewValue == sms.sCachedValue)
806 continue;
807 sms.sCachedValue = sNewValue;
808
809 double pdNewValue;
810 int err = GVars3::serialize::from_string(sNewValue, pdNewValue);
811
812 if(err)
813 continue;
814
815 sms.dMax = max(sms.dMax, pdNewValue);
816 sms.dMin = min(sms.dMin, pdNewValue);
817
818 double dFraction = ((pdNewValue - sms.dMin)/ (sms.dMax-sms.dMin));
819
820 XmScrollBarSetValues(i->first, (int)(300 * dFraction), 0, 0, 0, False);
821 };
822
823
824 for(map<Widget, pair<string, string> >::iterator i=w->second.SpinMap.begin(); i != w->second.SpinMap.end(); i++)
825 {
826 if(i->second.second != GV3::get_var(i->second.first))
827 {
828 XmTextPosition p;
829 XtVaGetValues(i->first, XmNcursorPosition, &p, NULL);
830
831 string s = GV3::get_var(i->second.first);
832
833 i->second.second = s;
834 XtRemoveCallback(i->first, XmNvalueChangedCallback, TextBoxCB, this);
835 p = min((int)p, (int)s.size());
836 XtVaSetValues(i->first, XmNvalue, s.c_str(), XmNcursorPosition, p, NULL);
837 XtAddCallback(i->first, XmNvalueChangedCallback, TextBoxCB, this);
838 }
839 }
840
841
842 };
843 DoMotifEvents();
844
845 //Now kill any windows in need of killing
846 for(map<string,GUIWindowStruct>::iterator tmp, w=mmWindows.begin();w!=mmWindows.end();)
847 {
848 tmp = w;
849 w++;
850 if(tmp->second.die)
851 mmWindows.erase(tmp);
852 }
853
854 DoMotifEvents();
855 }
856 void* GUI_Motif::GUI_Motif_Thread_CB(void* ptr)
857 {
858 ((GUI_Motif*) ptr)->GUI_Motif_Thread();
859 return 0;
860 }
861
862 void GUI_Motif::DoMotifEvents()
863 {
864 XEvent xevent;
865 int num_events=0;
866 while(XtAppPending(mxtac) && num_events < 100000){
867 XtAppNextEvent(mxtac,&xevent);
868 XtDispatchEvent(&xevent);
869 num_events++;
870 }
871 }
872
873 //Instantiations, link time virtual function definitions
874 class GUI_Motif GUI_Motif_instance(&GUI, &GV2);
875
876 void GUIWidgets::process_in_crnt_thread()
877 {
878 GUI_Motif_instance.poll();
879 }
880
881 void GUIWidgets::start_thread()
882 {
883 GUI_Motif_instance.start_thread();
884 }
885
886 }