00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "CocoaMainWindow.h"
00016 #include "Interfaces/MainWindowEventInterface.h"
00017
00018 #include "Image/ByteImage.h"
00019 #include "Image/ImageProcessor.h"
00020
00021 #ifdef USE_OPENGL
00022
00023 #endif
00024
00025 enum CocoaWidgetType
00026 {
00027 eImage = 0,
00028 eButton,
00029 eLabel,
00030 eCheckBox,
00031 eTextEdit,
00032 eSlider,
00033 eComboBox,
00034 eGLWidget
00035 };
00036
00037 struct CCocoaMainWindowWidget
00038 {
00039 CocoaWidgetType type;
00040 void *cocoa_ptr;
00041 int width;
00042 int height;
00043 };
00044
00045
00046
00047
00048 extern "C"
00049 {
00050 void* CocoaCreateMainWindow(int x, int y, int width, int height, const char *title, void* main_window_ptr);
00051 void CocoaDestroyMainWindow(void* window);
00052 void* CocoaCreateImage(void* window, int x, int y, int width, int height, void* ptr);
00053 void* CocoaCreateButton(void* window, int x, int y, int width, int height, const char *text, void* ptr);
00054 void* CocoaCreateLabel(void* window, int x, int y, int width, int height, const char *text, void* ptr);
00055 void* CocoaCreateCheckBox(void* window, int x, int y, int width, int height, const char *text, bool checked, void* ptr);
00056 void* CocoaCreateTextEdit(void* window, int x, int y, int width, int height, const char *text, void* ptr);
00057 void* CocoaCreateSlider(void* window, int x, int y, int width, int height, int min_value, int max_value, int step, int value, void* ptr);
00058 void* CocoaCreateComboBox(void* window, int x, int y, int width, int height, int num_entries, const char **entries, int current_entry, void* ptr);
00059 void* CocoaCreateOpenGLWidget(void* window, int x, int y, int width, int height, void* ptr);
00060
00061 int CocoaGetInt(void* ptr);
00062 void CocoaGetText(void* ptr, char *str, int len);
00063 void CocoaGetTitle(void* ptr, char *str, int len);
00064 int CocoaGetComboBoxSelection(void *ptr);
00065
00066 void CocoaSetInt(void* ptr, int value);
00067 void CocoaSetText(void* ptr, const char *str);
00068 void CocoaSetTitle(void* ptr, const char *str);
00069 void CocoaSetComboBoxSelection(void *ptr, int value);
00070 void CocoaSetImage(void* ptr, int width, int height, unsigned char *pixels);
00071
00072 void CocoaShow(void* ptr);
00073 void CocoaHide(void* ptr);
00074 void CocoaShowWindow(void* ptr);
00075 void CocoaHideWindow(void* ptr);
00076
00077 int CocoaGetModifierKeyState();
00078
00079 void CocoaSwapBuffers(void* ptr);
00080 void CocoaMakeCurrent(void* ptr);
00081
00082 void CocoaGetCurrentWorkingDirectory(char* str, int len);
00083 }
00084
00085 class SetTheCWDHack
00086 {
00087 public:
00088 SetTheCWDHack()
00089 {
00090 char buf[1024];
00091 CocoaGetCurrentWorkingDirectory(buf, 1024);
00092 chdir(buf);
00093 }
00094 };
00095
00096 SetTheCWDHack hack;
00097
00098
00099 extern "C" void EventCallback(void* window, void* widget, int type, int *params)
00100 {
00101 CCocoaMainWindow *main_window = (CCocoaMainWindow*)window;
00102 main_window->EventCallback(widget, type, params);
00103 }
00104
00105 void CCocoaMainWindow::EventCallback(void* widget, int type, int *params)
00106 {
00107 if (m_event_callback)
00108 {
00109 int s = m_widgets.size();
00110
00111 for (int i = 0; i < s; i++)
00112 {
00113 CCocoaMainWindowWidget *w = m_widgets[i];
00114
00115 if (w->cocoa_ptr == widget)
00116 {
00117 switch (w->type)
00118 {
00119 case eImage:
00120 {
00121 if (type == 1)
00122 {
00123 m_event_callback->PointClicked(w, params[0], params[1]);
00124 }
00125 else if (type == 2)
00126 {
00127 m_event_callback->RectSelected(w, params[0], params[1], params[2], params[3]);
00128 }
00129 else if (type == 3)
00130 {
00131 m_event_callback->MouseDown(w, params[0], params[1], params[2]);
00132 }
00133 else if (type == 4)
00134 {
00135 m_event_callback->MouseUp(w, params[0], params[1], params[2]);
00136 }
00137 else if (type == 5)
00138 {
00139 m_event_callback->MouseMove(w, params[0], params[1]);
00140 }
00141 else if (type == 6)
00142 {
00143 m_event_callback->KeyDown(w, params[0]);
00144 }
00145 else if (type == 7)
00146 {
00147 m_event_callback->KeyUp(w, params[0]);
00148 }
00149 }
00150 break;
00151 case eButton: m_event_callback->ButtonPushed(w); break;
00152 case eCheckBox:
00153 {
00154 int v = CocoaGetInt(w->cocoa_ptr);
00155 m_event_callback->ValueChanged(w, v);
00156 }
00157 break;
00158 case eTextEdit: m_event_callback->ValueChanged(w, -1); break;
00159 case eSlider:
00160 {
00161 int v = CocoaGetInt(w->cocoa_ptr);
00162 m_event_callback->ValueChanged(w, v);
00163 }
00164 break;
00165 case eComboBox:
00166 {
00167 int v = CocoaGetComboBoxSelection(w->cocoa_ptr);
00168 m_event_callback->ValueChanged(w, v);
00169 }
00170 break;
00171 case eGLWidget:
00172 {
00173 if (type == 3)
00174 {
00175 m_event_callback->MouseDown(w, params[0], params[1], params[2]);
00176 }
00177 else if (type == 4)
00178 {
00179 m_event_callback->MouseUp(w, params[0], params[1], params[2]);
00180 }
00181 else if (type == 5)
00182 {
00183 m_event_callback->MouseMove(w, params[0], params[1]);
00184 }
00185 else if (type == 6)
00186 {
00187 m_event_callback->KeyDown(w, params[0]);
00188 }
00189 else if (type == 7)
00190 {
00191 m_event_callback->KeyUp(w, params[0]);
00192 }
00193 }
00194 break;
00195 default: break;
00196 }
00197
00198 break;
00199 }
00200 }
00201 }
00202 }
00203
00204
00205
00206
00207
00208 CCocoaMainWindow::CCocoaMainWindow(int x, int y, int width, int height, const char *title)
00209 {
00210 m_cocoa_main_window = CocoaCreateMainWindow(x, y, width, height, title, this);
00211
00212 m_width = width;
00213 m_height = height;
00214
00215 if (!m_cocoa_main_window)
00216 printf("error: couldn't create Cocoa main window\n");
00217
00218 m_event_callback = NULL;
00219 }
00220
00221 CCocoaMainWindow::~CCocoaMainWindow()
00222 {
00223 int s = m_widgets.size();
00224
00225 for (int i = 0; i < s; i++)
00226 {
00227 CCocoaMainWindowWidget *widget = m_widgets[i];
00228 delete widget;
00229 }
00230 m_widgets.clear();
00231
00232 CocoaDestroyMainWindow(m_cocoa_main_window);
00233 m_cocoa_main_window = NULL;
00234 }
00235
00236
00237 WIDGET_HANDLE CCocoaMainWindow::AddImage(int x, int y, int width, int height, WIDGET_HANDLE parent)
00238 {
00239 CCocoaMainWindowWidget *w = (CCocoaMainWindowWidget*)parent;
00240
00241 FixPosition(x, y, width, height);
00242
00243 void* ptr = CocoaCreateImage(m_cocoa_main_window, x, y, width, height, (w != NULL ? w->cocoa_ptr : NULL));
00244 if (ptr != NULL)
00245 {
00246 CCocoaMainWindowWidget *widget = new CCocoaMainWindowWidget;
00247
00248 widget->type = eImage;
00249 widget->cocoa_ptr = ptr;
00250 widget->width = width;
00251 widget->height = height;
00252
00253 m_widgets.push_back(widget);
00254
00255 return (WIDGET_HANDLE)widget;
00256 }
00257
00258
00259 return (WIDGET_HANDLE)NULL;
00260 }
00261
00262 WIDGET_HANDLE CCocoaMainWindow::AddButton(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent)
00263 {
00264 CCocoaMainWindowWidget *w = (CCocoaMainWindowWidget*)parent;
00265
00266 FixPosition(x, y, width, height);
00267
00268 void* ptr = CocoaCreateButton(m_cocoa_main_window, x, y, width, height, text, (w != NULL ? w->cocoa_ptr : NULL));
00269 if (ptr != NULL)
00270 {
00271 CCocoaMainWindowWidget *widget = new CCocoaMainWindowWidget;
00272
00273 widget->type = eButton;
00274 widget->cocoa_ptr = ptr;
00275
00276 m_widgets.push_back(widget);
00277
00278 return (WIDGET_HANDLE)widget;
00279 }
00280
00281 return (WIDGET_HANDLE)NULL;
00282 }
00283
00284 WIDGET_HANDLE CCocoaMainWindow::AddLabel(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent)
00285 {
00286 CCocoaMainWindowWidget *w = (CCocoaMainWindowWidget*)parent;
00287
00288 FixPosition(x, y, width, height);
00289
00290 void* ptr = CocoaCreateLabel(m_cocoa_main_window, x, y, width, height, text, (w != NULL ? w->cocoa_ptr : NULL));
00291 if (ptr != NULL)
00292 {
00293 CCocoaMainWindowWidget *widget = new CCocoaMainWindowWidget;
00294
00295 widget->type = eLabel;
00296 widget->cocoa_ptr = ptr;
00297
00298 m_widgets.push_back(widget);
00299
00300 return (WIDGET_HANDLE)widget;
00301 }
00302
00303 return (WIDGET_HANDLE)NULL;
00304 }
00305
00306 WIDGET_HANDLE CCocoaMainWindow::AddCheckBox(int x, int y, int width, int height, const char *text, bool checked, WIDGET_HANDLE parent)
00307 {
00308 CCocoaMainWindowWidget *w = (CCocoaMainWindowWidget*)parent;
00309
00310 FixPosition(x, y, width, height);
00311
00312 void* ptr = CocoaCreateCheckBox(m_cocoa_main_window, x, y, width, height, text, checked, (w != NULL ? w->cocoa_ptr : NULL));
00313 if (ptr != NULL)
00314 {
00315 CCocoaMainWindowWidget *widget = new CCocoaMainWindowWidget;
00316
00317 widget->type = eCheckBox;
00318 widget->cocoa_ptr = ptr;
00319
00320 m_widgets.push_back(widget);
00321
00322 return (WIDGET_HANDLE)widget;
00323 }
00324
00325 return (WIDGET_HANDLE)NULL;
00326 }
00327
00328 WIDGET_HANDLE CCocoaMainWindow::AddTextEdit(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent)
00329 {
00330 CCocoaMainWindowWidget *w = (CCocoaMainWindowWidget*)parent;
00331
00332 FixPosition(x, y, width, height);
00333
00334 void* ptr = CocoaCreateTextEdit(m_cocoa_main_window, x, y, width, height, text, (w != NULL ? w->cocoa_ptr : NULL));
00335 if (ptr != NULL)
00336 {
00337 CCocoaMainWindowWidget *widget = new CCocoaMainWindowWidget;
00338
00339 widget->type = eTextEdit;
00340 widget->cocoa_ptr = ptr;
00341
00342 m_widgets.push_back(widget);
00343
00344 return (WIDGET_HANDLE)widget;
00345 }
00346
00347 return (WIDGET_HANDLE)NULL;
00348 }
00349
00350 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)
00351 {
00352 CCocoaMainWindowWidget *w = (CCocoaMainWindowWidget*)parent;
00353
00354 FixPosition(x, y, width, height);
00355
00356 void* ptr = CocoaCreateSlider(m_cocoa_main_window, x, y, width, height, min_value, max_value, step, value, (w != NULL ? w->cocoa_ptr : NULL));
00357 if (ptr != NULL)
00358 {
00359 CCocoaMainWindowWidget *widget = new CCocoaMainWindowWidget;
00360
00361 widget->type = eSlider;
00362 widget->cocoa_ptr = ptr;
00363
00364 m_widgets.push_back(widget);
00365
00366 return (WIDGET_HANDLE)widget;
00367 }
00368
00369 return (WIDGET_HANDLE)NULL;
00370 }
00371
00372 WIDGET_HANDLE CCocoaMainWindow::AddComboBox(int x, int y, int width, int height, int num_entries, const char **entries, int current_entry, WIDGET_HANDLE parent)
00373 {
00374 CCocoaMainWindowWidget *w = (CCocoaMainWindowWidget*)parent;
00375
00376 FixPosition(x, y, width, height);
00377
00378 void* ptr = CocoaCreateComboBox(m_cocoa_main_window, x, y, width, height, num_entries, entries, current_entry, (w != NULL ? w->cocoa_ptr : NULL));
00379 if (ptr != NULL)
00380 {
00381 CCocoaMainWindowWidget *widget = new CCocoaMainWindowWidget;
00382
00383 widget->type = eComboBox;
00384 widget->cocoa_ptr = ptr;
00385
00386 m_widgets.push_back(widget);
00387
00388 return (WIDGET_HANDLE)widget;
00389 }
00390
00391 return (WIDGET_HANDLE)NULL;
00392 }
00393
00394 WIDGET_HANDLE CCocoaMainWindow::AddGLWidget(int x, int y, int width, int height, WIDGET_HANDLE parent)
00395 {
00396 #ifdef USE_OPENGL
00397 CCocoaMainWindowWidget *w = (CCocoaMainWindowWidget*)parent;
00398
00399 FixPosition(x, y, width, height);
00400
00401 void* ptr = CocoaCreateOpenGLWidget(m_cocoa_main_window, x, y, width, height, (w != NULL ? w->cocoa_ptr : NULL));
00402 if (ptr != NULL)
00403 {
00404 CCocoaMainWindowWidget *widget = new CCocoaMainWindowWidget;
00405
00406 widget->type = eGLWidget;
00407 widget->cocoa_ptr = ptr;
00408
00409 m_widgets.push_back(widget);
00410
00411 return (WIDGET_HANDLE)widget;
00412 }
00413
00414 #endif
00415
00416 return (WIDGET_HANDLE)NULL;
00417 }
00418
00419
00420
00421 bool CCocoaMainWindow::GetText(WIDGET_HANDLE widget, char *text, int len)
00422 {
00423 CCocoaMainWindowWidget *w = (CCocoaMainWindowWidget*)widget;
00424
00425 if (w->type != eImage && w->type != eGLWidget && w->type != eSlider && w->type != eComboBox)
00426 {
00427 if (w->type == eButton || w->type == eCheckBox)
00428 CocoaGetTitle(w->cocoa_ptr, text, len);
00429 else
00430 CocoaGetText(w->cocoa_ptr, text, len);
00431
00432 return true;
00433 }
00434
00435 return false;
00436 }
00437 bool CCocoaMainWindow::SetText(WIDGET_HANDLE widget, const char *text)
00438 {
00439 CCocoaMainWindowWidget *w = (CCocoaMainWindowWidget*)widget;
00440
00441 if (w->type != eImage && w->type != eGLWidget && w->type != eSlider && w->type != eComboBox)
00442 {
00443 if (w->type == eButton || w->type == eCheckBox)
00444 CocoaSetTitle(w->cocoa_ptr, text);
00445 else
00446 CocoaSetText(w->cocoa_ptr, text);
00447
00448 return true;
00449 }
00450
00451 return false;
00452 }
00453
00454 bool CCocoaMainWindow::SetImage(WIDGET_HANDLE widget, const CByteImage *pImage)
00455 {
00456 CCocoaMainWindowWidget *w = (CCocoaMainWindowWidget*)widget;
00457
00458 if (w->type == eImage)
00459 {
00460 if (w->width != pImage->width || w->height != pImage->height)
00461 {
00462 printf("error: CCocoaMainWindow::SetImage: image dimensions do not match dimensions of image widget!\n");
00463 return false;
00464 }
00465
00466 if (pImage->type == CByteImage::eRGB24)
00467 {
00468 CocoaSetImage(w->cocoa_ptr, pImage->width, pImage->height, pImage->pixels);
00469 }
00470 else
00471 {
00472 CByteImage temp(pImage->width, pImage->height, CByteImage::eRGB24);
00473
00474 ImageProcessor::ConvertImage(pImage, &temp);
00475
00476 CocoaSetImage(w->cocoa_ptr, pImage->width, pImage->height, temp.pixels);
00477 }
00478
00479 return true;
00480 }
00481
00482 return false;
00483 }
00484
00485 bool CCocoaMainWindow::GetValue(WIDGET_HANDLE widget, int &value)
00486 {
00487 CCocoaMainWindowWidget *w = (CCocoaMainWindowWidget*)widget;
00488
00489 if (w->type == eComboBox)
00490 {
00491 value = CocoaGetComboBoxSelection(w->cocoa_ptr);
00492
00493 return true;
00494 }
00495 else if (w->type == eCheckBox || w->type == eSlider)
00496 {
00497 value = CocoaGetInt(w->cocoa_ptr);
00498
00499 return true;
00500 }
00501
00502 return false;
00503 }
00504
00505 bool CCocoaMainWindow::SetValue(WIDGET_HANDLE widget, int value)
00506 {
00507 CCocoaMainWindowWidget *w = (CCocoaMainWindowWidget*)widget;
00508
00509 if (w->type == eComboBox)
00510 {
00511 CocoaSetComboBoxSelection(w->cocoa_ptr, value);
00512
00513 return true;
00514 }
00515 else if (w->type == eCheckBox || w->type == eSlider)
00516 {
00517 CocoaSetInt(w->cocoa_ptr, value);
00518
00519 return true;
00520 }
00521
00522 return false;
00523 }
00524
00525 bool CCocoaMainWindow::SwapBuffersGLWidget(WIDGET_HANDLE widget)
00526 {
00527 #ifdef USE_OPENGL
00528 CCocoaMainWindowWidget *w = (CCocoaMainWindowWidget*)widget;
00529
00530 if (w->type == eGLWidget)
00531 {
00532 CocoaSwapBuffers(w->cocoa_ptr);
00533
00534 return true;
00535 }
00536
00537 #endif
00538
00539 return false;
00540 }
00541
00542 bool CCocoaMainWindow::MakeCurrentGLWidget(WIDGET_HANDLE widget)
00543 {
00544 #ifdef USE_OPENGL
00545 CCocoaMainWindowWidget *w = (CCocoaMainWindowWidget*)widget;
00546
00547 if (w->type == eGLWidget)
00548 {
00549 CocoaMakeCurrent(w->cocoa_ptr);
00550
00551 return true;
00552 }
00553
00554 #endif
00555
00556 return false;
00557 }
00558
00559
00560
00561
00562 void CCocoaMainWindow::Show(WIDGET_HANDLE widget)
00563 {
00564 CCocoaMainWindowWidget *w = (CCocoaMainWindowWidget*)widget;
00565
00566 if (w != NULL)
00567 {
00568 CocoaShow(w->cocoa_ptr);
00569 }
00570 else
00571 {
00572 CocoaShowWindow(m_cocoa_main_window);
00573 }
00574 }
00575 void CCocoaMainWindow::Hide(WIDGET_HANDLE widget)
00576 {
00577 CCocoaMainWindowWidget *w = (CCocoaMainWindowWidget*)widget;
00578
00579 if (w != NULL)
00580 {
00581 CocoaHide(w->cocoa_ptr);
00582 }
00583 else
00584 {
00585 CocoaHideWindow(m_cocoa_main_window);
00586 }
00587 }
00588
00589 int CCocoaMainWindow::GetModifierKeyState()
00590 {
00591 return CocoaGetModifierKeyState();
00592 }
00593
00594 void CCocoaMainWindow::FixPosition(int &x, int &y, int &width, int &height)
00595 {
00596 y = m_height - y - height;
00597 }