CocoaMainWindow.cpp
Go to the documentation of this file.
1 // *****************************************************************
2 // Filename: CocoaMainWindow.cpp
3 // Copyright: Pedram Azad, Chair Prof. Dillmann (IAIM),
4 // Institute for Computer Science and Engineering (CSE),
5 // University of Karlsruhe. All rights reserved.
6 // Author: Florian Hecht
7 // Date: 2008
8 // *****************************************************************
9 
10 
11 // *****************************************************************
12 // necessary includes
13 // *****************************************************************
14 
15 #include "CocoaMainWindow.h"
17 
18 #include "Image/ByteImage.h"
19 #include "Image/ImageProcessor.h"
20 
21 #ifdef USE_OPENGL
22 
23 #endif
24 
26 {
27  eImage = 0,
35 };
36 
38 {
40  void *cocoa_ptr;
41  int width;
42  int height;
43 };
44 
45 
46 
47 // these functions are defined in CocoaImplementation.m
48 extern "C"
49 {
50  void* CocoaCreateMainWindow(int x, int y, int width, int height, const char *title, void* main_window_ptr);
51  void CocoaDestroyMainWindow(void* window);
52  void* CocoaCreateImage(void* window, int x, int y, int width, int height, void* ptr);
53  void* CocoaCreateButton(void* window, int x, int y, int width, int height, const char *text, void* ptr);
54  void* CocoaCreateLabel(void* window, int x, int y, int width, int height, const char *text, void* ptr);
55  void* CocoaCreateCheckBox(void* window, int x, int y, int width, int height, const char *text, bool checked, void* ptr);
56  void* CocoaCreateTextEdit(void* window, int x, int y, int width, int height, const char *text, void* ptr);
57  void* CocoaCreateSlider(void* window, int x, int y, int width, int height, int min_value, int max_value, int step, int value, void* ptr);
58  void* CocoaCreateComboBox(void* window, int x, int y, int width, int height, int num_entries, const char **entries, int current_entry, void* ptr);
59  void* CocoaCreateOpenGLWidget(void* window, int x, int y, int width, int height, void* ptr);
60 
61  int CocoaGetInt(void* ptr);
62  void CocoaGetText(void* ptr, char *str, int len);
63  void CocoaGetTitle(void* ptr, char *str, int len);
64  int CocoaGetComboBoxSelection(void *ptr);
65 
66  void CocoaSetInt(void* ptr, int value);
67  void CocoaSetText(void* ptr, const char *str);
68  void CocoaSetTitle(void* ptr, const char *str);
69  void CocoaSetComboBoxSelection(void *ptr, int value);
70  void CocoaSetImage(void* ptr, int width, int height, unsigned char *pixels);
71 
72  void CocoaShow(void* ptr);
73  void CocoaHide(void* ptr);
74  void CocoaShowWindow(void* ptr);
75  void CocoaHideWindow(void* ptr);
76 
78 
79  void CocoaSwapBuffers(void* ptr);
80  void CocoaMakeCurrent(void* ptr);
81 
82  void CocoaGetCurrentWorkingDirectory(char* str, int len);
83 }
84 
86 {
87 public:
89  {
90  char buf[1024];
92  chdir(buf);
93  }
94 };
95 
97 
98 // this is for callbacks from the ObjC implementation
99 extern "C" void EventCallback(void* window, void* widget, int type, int *params)
100 {
101  CCocoaMainWindow *main_window = (CCocoaMainWindow*)window;
102  main_window->EventCallback(widget, type, params);
103 }
104 
105 void CCocoaMainWindow::EventCallback(void* widget, int type, int *params)
106 {
107  if (m_event_callback)
108  {
109  int s = m_widgets.size();
110 
111  for (int i = 0; i < s; i++)
112  {
113  CCocoaMainWindowWidget *w = m_widgets[i];
114 
115  if (w->cocoa_ptr == widget)
116  {
117  switch (w->type)
118  {
119  case eImage:
120  {
121  if (type == 1)
122  {
123  m_event_callback->PointClicked(w, params[0], params[1]);
124  }
125  else if (type == 2)
126  {
127  m_event_callback->RectSelected(w, params[0], params[1], params[2], params[3]);
128  }
129  else if (type == 3)
130  {
131  m_event_callback->MouseDown(w, params[0], params[1], params[2]);
132  }
133  else if (type == 4)
134  {
135  m_event_callback->MouseUp(w, params[0], params[1], params[2]);
136  }
137  else if (type == 5)
138  {
139  m_event_callback->MouseMove(w, params[0], params[1]);
140  }
141  else if (type == 6)
142  {
143  m_event_callback->KeyDown(w, params[0]);
144  }
145  else if (type == 7)
146  {
147  m_event_callback->KeyUp(w, params[0]);
148  }
149  }
150  break;
151  case eButton: m_event_callback->ButtonPushed(w); break;
152  case eCheckBox:
153  {
154  int v = CocoaGetInt(w->cocoa_ptr);
155  m_event_callback->ValueChanged(w, v);
156  }
157  break;
158  case eTextEdit: m_event_callback->ValueChanged(w, -1); break;
159  case eSlider:
160  {
161  int v = CocoaGetInt(w->cocoa_ptr);
162  m_event_callback->ValueChanged(w, v);
163  }
164  break;
165  case eComboBox:
166  {
168  m_event_callback->ValueChanged(w, v);
169  }
170  break;
171  case eGLWidget:
172  {
173  if (type == 3)
174  {
175  m_event_callback->MouseDown(w, params[0], params[1], params[2]);
176  }
177  else if (type == 4)
178  {
179  m_event_callback->MouseUp(w, params[0], params[1], params[2]);
180  }
181  else if (type == 5)
182  {
183  m_event_callback->MouseMove(w, params[0], params[1]);
184  }
185  else if (type == 6)
186  {
187  m_event_callback->KeyDown(w, params[0]);
188  }
189  else if (type == 7)
190  {
191  m_event_callback->KeyUp(w, params[0]);
192  }
193  }
194  break;
195  default: break;
196  }
197 
198  break;
199  }
200  }
201  }
202 }
203 
204 
205 
206 
207 
208 CCocoaMainWindow::CCocoaMainWindow(int x, int y, int width, int height, const char *title)
209 {
210  m_cocoa_main_window = CocoaCreateMainWindow(x, y, width, height, title, this);
211 
212  m_width = width;
213  m_height = height;
214 
215  if (!m_cocoa_main_window)
216  printf("error: couldn't create Cocoa main window\n");
217 
218  m_event_callback = NULL;
219 }
220 
222 {
223  int s = m_widgets.size();
224 
225  for (int i = 0; i < s; i++)
226  {
227  CCocoaMainWindowWidget *widget = m_widgets[i];
228  delete widget;
229  }
230  m_widgets.clear();
231 
232  CocoaDestroyMainWindow(m_cocoa_main_window);
233  m_cocoa_main_window = NULL;
234 }
235 
236 // create widgets
238 {
240 
241  FixPosition(x, y, width, height);
242 
243  void* ptr = CocoaCreateImage(m_cocoa_main_window, x, y, width, height, (w != NULL ? w->cocoa_ptr : NULL));
244  if (ptr != NULL)
245  {
247 
248  widget->type = eImage;
249  widget->cocoa_ptr = ptr;
250  widget->width = width;
251  widget->height = height;
252 
253  m_widgets.push_back(widget);
254 
255  return (WIDGET_HANDLE)widget;
256  }
257 
258 
259  return (WIDGET_HANDLE)NULL;
260 }
261 
262 WIDGET_HANDLE CCocoaMainWindow::AddButton(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent)
263 {
265 
266  FixPosition(x, y, width, height);
267 
268  void* ptr = CocoaCreateButton(m_cocoa_main_window, x, y, width, height, text, (w != NULL ? w->cocoa_ptr : NULL));
269  if (ptr != NULL)
270  {
272 
273  widget->type = eButton;
274  widget->cocoa_ptr = ptr;
275 
276  m_widgets.push_back(widget);
277 
278  return (WIDGET_HANDLE)widget;
279  }
280 
281  return (WIDGET_HANDLE)NULL;
282 }
283 
284 WIDGET_HANDLE CCocoaMainWindow::AddLabel(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent)
285 {
287 
288  FixPosition(x, y, width, height);
289 
290  void* ptr = CocoaCreateLabel(m_cocoa_main_window, x, y, width, height, text, (w != NULL ? w->cocoa_ptr : NULL));
291  if (ptr != NULL)
292  {
294 
295  widget->type = eLabel;
296  widget->cocoa_ptr = ptr;
297 
298  m_widgets.push_back(widget);
299 
300  return (WIDGET_HANDLE)widget;
301  }
302 
303  return (WIDGET_HANDLE)NULL;
304 }
305 
306 WIDGET_HANDLE CCocoaMainWindow::AddCheckBox(int x, int y, int width, int height, const char *text, bool checked, WIDGET_HANDLE parent)
307 {
309 
310  FixPosition(x, y, width, height);
311 
312  void* ptr = CocoaCreateCheckBox(m_cocoa_main_window, x, y, width, height, text, checked, (w != NULL ? w->cocoa_ptr : NULL));
313  if (ptr != NULL)
314  {
316 
317  widget->type = eCheckBox;
318  widget->cocoa_ptr = ptr;
319 
320  m_widgets.push_back(widget);
321 
322  return (WIDGET_HANDLE)widget;
323  }
324 
325  return (WIDGET_HANDLE)NULL;
326 }
327 
328 WIDGET_HANDLE CCocoaMainWindow::AddTextEdit(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent)
329 {
331 
332  FixPosition(x, y, width, height);
333 
334  void* ptr = CocoaCreateTextEdit(m_cocoa_main_window, x, y, width, height, text, (w != NULL ? w->cocoa_ptr : NULL));
335  if (ptr != NULL)
336  {
338 
339  widget->type = eTextEdit;
340  widget->cocoa_ptr = ptr;
341 
342  m_widgets.push_back(widget);
343 
344  return (WIDGET_HANDLE)widget;
345  }
346 
347  return (WIDGET_HANDLE)NULL;
348 }
349 
350 WIDGET_HANDLE CCocoaMainWindow::AddSlider(int x, int y, int width, int height, int min_value, int max_value, int step, int value, WIDGET_HANDLE parent)
351 {
353 
354  FixPosition(x, y, width, height);
355 
356  void* ptr = CocoaCreateSlider(m_cocoa_main_window, x, y, width, height, min_value, max_value, step, value, (w != NULL ? w->cocoa_ptr : NULL));
357  if (ptr != NULL)
358  {
360 
361  widget->type = eSlider;
362  widget->cocoa_ptr = ptr;
363 
364  m_widgets.push_back(widget);
365 
366  return (WIDGET_HANDLE)widget;
367  }
368 
369  return (WIDGET_HANDLE)NULL;
370 }
371 
372 WIDGET_HANDLE CCocoaMainWindow::AddComboBox(int x, int y, int width, int height, int num_entries, const char **entries, int current_entry, WIDGET_HANDLE parent)
373 {
375 
376  FixPosition(x, y, width, height);
377 
378  void* ptr = CocoaCreateComboBox(m_cocoa_main_window, x, y, width, height, num_entries, entries, current_entry, (w != NULL ? w->cocoa_ptr : NULL));
379  if (ptr != NULL)
380  {
382 
383  widget->type = eComboBox;
384  widget->cocoa_ptr = ptr;
385 
386  m_widgets.push_back(widget);
387 
388  return (WIDGET_HANDLE)widget;
389  }
390 
391  return (WIDGET_HANDLE)NULL;
392 }
393 
395 {
396 #ifdef USE_OPENGL
398 
399  FixPosition(x, y, width, height);
400 
401  void* ptr = CocoaCreateOpenGLWidget(m_cocoa_main_window, x, y, width, height, (w != NULL ? w->cocoa_ptr : NULL));
402  if (ptr != NULL)
403  {
405 
406  widget->type = eGLWidget;
407  widget->cocoa_ptr = ptr;
408 
409  m_widgets.push_back(widget);
410 
411  return (WIDGET_HANDLE)widget;
412  }
413 
414 #endif
415 
416  return (WIDGET_HANDLE)NULL;
417 }
418 
419 
420 // access to widget attributes
421 bool CCocoaMainWindow::GetText(WIDGET_HANDLE widget, char *text, int len)
422 {
424 
425  if (w->type != eImage && w->type != eGLWidget && w->type != eSlider && w->type != eComboBox)
426  {
427  if (w->type == eButton || w->type == eCheckBox)
428  CocoaGetTitle(w->cocoa_ptr, text, len);
429  else
430  CocoaGetText(w->cocoa_ptr, text, len);
431 
432  return true;
433  }
434 
435  return false;
436 }
437 bool CCocoaMainWindow::SetText(WIDGET_HANDLE widget, const char *text)
438 {
440 
441  if (w->type != eImage && w->type != eGLWidget && w->type != eSlider && w->type != eComboBox)
442  {
443  if (w->type == eButton || w->type == eCheckBox)
444  CocoaSetTitle(w->cocoa_ptr, text);
445  else
446  CocoaSetText(w->cocoa_ptr, text);
447 
448  return true;
449  }
450 
451  return false;
452 }
453 
455 {
457 
458  if (w->type == eImage)
459  {
460  if (w->width != pImage->width || w->height != pImage->height)
461  {
462  printf("error: CCocoaMainWindow::SetImage: image dimensions do not match dimensions of image widget!\n");
463  return false;
464  }
465 
466  if (pImage->type == CByteImage::eRGB24)
467  {
468  CocoaSetImage(w->cocoa_ptr, pImage->width, pImage->height, pImage->pixels);
469  }
470  else
471  {
472  CByteImage temp(pImage->width, pImage->height, CByteImage::eRGB24);
473 
474  ImageProcessor::ConvertImage(pImage, &temp);
475 
476  CocoaSetImage(w->cocoa_ptr, pImage->width, pImage->height, temp.pixels);
477  }
478 
479  return true;
480  }
481 
482  return false;
483 }
484 
486 {
488 
489  if (w->type == eComboBox)
490  {
492 
493  return true;
494  }
495  else if (w->type == eCheckBox || w->type == eSlider)
496  {
497  value = CocoaGetInt(w->cocoa_ptr);
498 
499  return true;
500  }
501 
502  return false;
503 }
504 
506 {
508 
509  if (w->type == eComboBox)
510  {
512 
513  return true;
514  }
515  else if (w->type == eCheckBox || w->type == eSlider)
516  {
517  CocoaSetInt(w->cocoa_ptr, value);
518 
519  return true;
520  }
521 
522  return false;
523 }
524 
526 {
527 #ifdef USE_OPENGL
529 
530  if (w->type == eGLWidget)
531  {
533 
534  return true;
535  }
536 
537 #endif
538 
539  return false;
540 }
541 
543 {
544 #ifdef USE_OPENGL
546 
547  if (w->type == eGLWidget)
548  {
550 
551  return true;
552  }
553 
554 #endif
555 
556  return false;
557 }
558 
559 
560 
561 // window control
563 {
565 
566  if (w != NULL)
567  {
568  CocoaShow(w->cocoa_ptr);
569  }
570  else
571  {
572  CocoaShowWindow(m_cocoa_main_window);
573  }
574 }
576 {
578 
579  if (w != NULL)
580  {
581  CocoaHide(w->cocoa_ptr);
582  }
583  else
584  {
585  CocoaHideWindow(m_cocoa_main_window);
586  }
587 }
588 
590 {
591  return CocoaGetModifierKeyState();
592 }
593 
594 void CCocoaMainWindow::FixPosition(int &x, int &y, int &width, int &height)
595 {
596  y = m_height - y - height;
597 }
bool MakeCurrentGLWidget(WIDGET_HANDLE widget)
void CocoaGetTitle(void *ptr, char *str, int len)
void CocoaSetTitle(void *ptr, const char *str)
bool SetText(WIDGET_HANDLE widget, const char *text)
GLenum const GLfloat * params
Definition: glext.h:3123
bool SetValue(WIDGET_HANDLE widget, int value)
WIDGET_HANDLE AddComboBox(int x, int y, int width, int height, int num_entries, const char **entries, int current_entry, WIDGET_HANDLE parent=0)
void EventCallback(void *window, void *widget, int type, int *params)
bool SwapBuffersGLWidget(WIDGET_HANDLE widget)
CocoaWidgetType
void CocoaSetImage(void *ptr, int width, int height, unsigned char *pixels)
WIDGET_HANDLE AddGLWidget(int x, int y, int width, int height, WIDGET_HANDLE parent=0)
WIDGET_HANDLE AddButton(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent=0)
int width
The width of the image in pixels.
Definition: ByteImage.h:257
void * CocoaCreateImage(void *window, int x, int y, int width, int height, void *ptr)
SetTheCWDHack hack
WIDGET_HANDLE AddSlider(int x, int y, int width, int height, int min_value, int max_value, int step, int value, WIDGET_HANDLE parent=0)
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
Definition: ByteImage.h:80
GLdouble s
Definition: glext.h:3211
GLenum GLsizei len
Definition: glext.h:3940
void CocoaDestroyMainWindow(void *window)
void CocoaShow(void *ptr)
GLuint GLuint GLsizei GLenum type
Definition: glext.h:3121
unsigned char * pixels
The pointer to the the pixels.
Definition: ByteImage.h:283
bool ConvertImage(const CByteImage *pInputImage, CByteImage *pOutputImage, bool bFast=false, const MyRegion *pROI=0)
Converts a grayscale CByteImage to an RGB CByteImage image and vice versa.
void * CocoaCreateMainWindow(int x, int y, int width, int height, const char *title, void *main_window_ptr)
void CocoaHide(void *ptr)
int CocoaGetComboBoxSelection(void *ptr)
void EventCallback(void *widget, int type, int *params)
GLenum GLint x
Definition: glext.h:3125
void CocoaSetInt(void *ptr, int value)
void * CocoaCreateTextEdit(void *window, int x, int y, int width, int height, const char *text, void *ptr)
void CocoaSwapBuffers(void *ptr)
WIDGET_HANDLE AddCheckBox(int x, int y, int width, int height, const char *text, bool checked, WIDGET_HANDLE parent=0)
void CocoaShowWindow(void *ptr)
void * WIDGET_HANDLE
WIDGET_HANDLE AddImage(int x, int y, int width, int height, WIDGET_HANDLE parent=0)
void CocoaSetComboBoxSelection(void *ptr, int value)
GLsizei const GLfloat * value
Definition: glext.h:3538
void * CocoaCreateOpenGLWidget(void *window, int x, int y, int width, int height, void *ptr)
void * CocoaCreateComboBox(void *window, int x, int y, int width, int height, int num_entries, const char **entries, int current_entry, void *ptr)
bool GetValue(WIDGET_HANDLE widget, int &value)
void CocoaGetText(void *ptr, char *str, int len)
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: glext.h:3154
void Hide(WIDGET_HANDLE widget=0)
int height
The height of the image in pixels.
Definition: ByteImage.h:264
void * CocoaCreateSlider(void *window, int x, int y, int width, int height, int min_value, int max_value, int step, int value, void *ptr)
CCocoaMainWindow(int x, int y, int width, int height, const char *title)
GLenum GLsizei width
Definition: glext.h:3122
GLenum GLsizei GLsizei height
Definition: glext.h:3132
int CocoaGetModifierKeyState()
ImageType type
The type of the image.
Definition: ByteImage.h:292
GLenum GLint GLint y
Definition: glext.h:3125
const GLdouble * v
Definition: glext.h:3212
WIDGET_HANDLE AddLabel(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent=0)
void * CocoaCreateCheckBox(void *window, int x, int y, int width, int height, const char *text, bool checked, void *ptr)
void CocoaGetCurrentWorkingDirectory(char *str, int len)
int CocoaGetInt(void *ptr)
bool GetText(WIDGET_HANDLE widget, char *text, int len)
void FixPosition(int &x, int &y, int &width, int &height)
void CocoaSetText(void *ptr, const char *str)
void CocoaMakeCurrent(void *ptr)
void Show(WIDGET_HANDLE widget=0)
void * CocoaCreateLabel(void *window, int x, int y, int width, int height, const char *text, void *ptr)
bool SetImage(WIDGET_HANDLE widget, const CByteImage *pImage)
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:3571
void CocoaHideWindow(void *ptr)
void * CocoaCreateButton(void *window, int x, int y, int width, int height, const char *text, void *ptr)
WIDGET_HANDLE AddTextEdit(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent=0)


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:27