OSDN Git Service

first
[psychlops/cpp.git] / psychlops / platform / osx / extension / psychlops_widgets_osx.mm
1 /*
2  *  psychlops_widgets_osx.cpp
3  *  Psychlops Standard Library (OSX)
4  *
5  *  Last Modified 2009/12/14 by Kenchi HOSOKAWA
6  *  (C) 2009 Kenchi HOSOKAWA, Kazushi MARUYA and Takao SATO
7  */
8
9
10 #include <AppKit/AppKit.h>
11 #include "../../../core/ApplicationInterfaces/psychlops_code_exception.h"
12 #include "../../../extension/standard/widgets/psychlops_widget.h"
13 #include "../psychlops_app_tweak_osx.h"
14
15 namespace Psychlops {
16         namespace Widgets {
17                 
18                 
19                 namespace Dialog {
20                         
21                         void alert(std::string message)
22                         {
23                                 boolean_t mouse_visible = CGCursorIsVisible();
24                                 if(!mouse_visible) CGDisplayShowCursor( kCGDirectMainDisplay );
25                                 NSString *str = [[ NSString alloc ] autorelease];
26                                 [str initWithCString: message.c_str() ];
27                                 NSAlert *alert = [[ NSAlert alertWithMessageText: @"Alert"
28                                                                                                    defaultButton: @"OK"
29                                                                                                  alternateButton: nil
30                                                                                                          otherButton: nil
31                                                                            informativeTextWithFormat: @"Notification" ] autorelease];
32                                 if(!mouse_visible) CGDisplayHideCursor( kCGDirectMainDisplay );
33                                 [ alert runModal ];
34                         }
35                         void alert(std::wstring message)
36                         {
37                                 boolean_t mouse_visible = CGCursorIsVisible();
38                                 if(!mouse_visible) CGDisplayShowCursor( kCGDirectMainDisplay );
39                                 NSString *str = [[NSString alloc] autorelease];
40                                 [ str initWithBytes: message.c_str()
41                                                          length: message.length()
42                                                    encoding: NSUnicodeStringEncoding ];
43                                 NSAlert *alert = [[ NSAlert alertWithMessageText: @"Alert"
44                                                                                                    defaultButton: @"OK"
45                                                                                                  alternateButton: nil
46                                                                                                          otherButton: nil
47                                                                            informativeTextWithFormat: @"Notification" ] autorelease];
48                                 [ alert runModal ];
49                                 if(!mouse_visible) CGDisplayHideCursor( kCGDirectMainDisplay );
50                         }
51                         
52                         std::string getOpenFileName()
53                         {
54                                 boolean_t mouse_visible = CGCursorIsVisible();
55                                 if(!mouse_visible) CGDisplayShowCursor( kCGDirectMainDisplay );
56                                 NSOpenPanel *openBox = [ NSOpenPanel openPanel ];
57                                 //[ openBox setTitle: @"Select File" ];
58                                 [ openBox setAllowsMultipleSelection: NO ];
59                                 [ openBox runModal ];
60                                 if(!mouse_visible) CGDisplayHideCursor( kCGDirectMainDisplay );
61                                 const char *url = [ [ [ openBox URL ] path ] UTF8String ];
62                                 std::string result;
63                                 try {
64                                         if(url!=0) result = url;
65                                         else result.clear();
66                                 }
67                                 catch (std::exception e) {
68                                         result.clear();
69                                 }
70                                 return result;
71                         }
72                         
73                 }       /*      <- namespace Dialog     */
74                 
75                 
76                 
77                 
78                 
79                 CloseButton::CloseButton()
80                 : Button(L"X", 20)
81                 {
82                         theme_type = Widgets::Theme::ALERT;
83                         Button::onClick = &exitApp;
84                         label.font.weight = Font::bold;
85                         label.fill = Color::white;
86                 }
87                 void CloseButton::exitApp()
88                 {
89                         if(AppState::shouldBeClose) exit(0);
90                         AppState::shouldBeClose = true;
91                 }
92                 
93                 MinimizeButton::MinimizeButton(Canvas &target)
94                 : Button(L"-", 20)
95                 {
96                         window = &target;
97                         arg = this;
98                         Button::onClickP = &minimizeApp;
99                         label.font.weight = Font::bold;
100                         label.fill = Color::white;
101                 }
102                 void MinimizeButton::minimizeApp(void *hwnd)
103                 {
104                         MinimizeButton *button = (MinimizeButton*)(hwnd);
105                         button->pushed_ = false;
106                         //              HWND window = Psychlops::DebugTweakTool::getRawWindowHandle(*(button->window));
107                         //              ShowWindow( window, SW_MINIMIZE );
108                 }
109                 
110                 
111                 TitleBar::TitleBar(Canvas &target)
112                 : mleft(Mouse::left), minimize(target)
113                 {
114                         Theme::initialize();
115                         area.set(target.getWidth()-85, 20);
116                         area.fill = Color::white;
117                         close.area.set(50, 20).setDatum(Point(target.getWidth()-55, 0));
118                         minimize.area.set(30, 20).setDatum(Point(target.getWidth()-85, 0));
119                         dragged_ = false;
120                 }
121                 TitleBar& TitleBar::draw(Drawable &target)
122                 {
123                         /*
124                          Canvas *cnvs = dynamic_cast<Canvas*>(&target);
125                          ::RECT rect;
126                          TRACKMOUSEEVENT eventTrack;
127                          
128                          if(cnvs!=0) {
129                          HWND window = Psychlops::DebugTweakTool::getRawWindowHandle(*cnvs);
130                          ::POINT mousept;
131                          if(mleft.pushed()) {
132                          if(area.include(Mouse::position)) {
133                          GetWindowRect(window, &rect);
134                          begin_w.set(rect.left, rect.top);
135                          begin_w_size.set(rect.right - rect.left, rect.bottom - rect.top);
136                          begin_m = Mouse::uniPosition;
137                          GetCursorPos(&mousept);
138                          eventTrack.cbSize = sizeof(TRACKMOUSEEVENT);
139                          eventTrack.dwFlags = TME_LEAVE;
140                          eventTrack.hwndTrack = window;
141                          eventTrack.dwHoverTime = 100;
142                          TrackMouseEvent(&eventTrack);
143                          DragDetect( window, mousept );
144                          //ClipCursor( rect );
145                          dragged_=true;
146                          }
147                          } else if(mleft.pressed() && dragged_) {
148                          GetWindowRect(window, &rect);
149                          Point p = (Point)Mouse::uniPosition - begin_m, np = p + begin_w;
150                          MoveWindow(window, np.x, np.y, begin_w_size.x, begin_w_size.y, false);
151                          eventTrack.cbSize = sizeof(TRACKMOUSEEVENT);
152                          eventTrack.dwFlags = TME_LEAVE;
153                          eventTrack.hwndTrack = window;
154                          eventTrack.dwHoverTime = 100;
155                          TrackMouseEvent(&eventTrack);
156                          //GetCursorPos(&mousept);
157                          //DragDetect( window, mousept );
158                          //ClipCursor( rect );
159                          } else if(mleft.released()) {
160                          //ClipCursor( NULL );
161                          dragged_ = false;
162                          }
163                          cnvs->drawImage(*(Theme::current.button_back), area);
164                          cnvs->drawImage(*(Theme::current.horiz_grad), area);
165                          }
166                          */
167                         minimize.draw();
168                         close.draw();
169                         return *this;
170                 }
171                 
172                 
173                 
174                 //std::string SequentialScreenshot::bin_path = "C:\\Library\\Frameworks\\Psychlops.framework\\bin\\fmpeg.exe ";
175                 const SequentialScreenshot& SequentialScreenshot::save(std::string filename) {
176 /*
177                         std::stringstream command;
178                         char buf[256];
179                         for(int i=0; i<movie.size(); i++) {
180                                 memset(buf, 0, 256);
181                                 sprintf(buf, "snap%04d.png\0", i+1);
182                                 movie[i]->save(directory+"/ScreenShots/"+buf);
183                         }
184                         command << bin_path << " -r 60 -y -i cap\\cap%04d.png -vcodec msmpeg4 " << filename;
185                         const char *lpArg = command.str().c_str();
186                         PROCESS_INFORMATION pi;
187                         STARTUPINFO si;
188                         ZeroMemory(&si,sizeof(si));
189                         si.cb=sizeof(si);
190                         CreateProcess(NULL,(LPTSTR)lpArg,NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&si, &pi);
191                         CloseHandle(pi.hThread);
192                         WaitForSingleObject(pi.hProcess,INFINITE);
193                         CloseHandle(pi.hProcess);
194                         clear();
195                         saved = true;
196  */
197                         return *this;
198                 }
199                 
200                 
201         }       /*      <- namespace Widgets    */
202         namespace Figures {
203                 std::string Movie::bin_path = "ffmpeg ";
204                 const Movie& Movie::save(std::string filename)
205                 {
206                         //Process Manager Reference(Carbon) LaunchApplication
207                         //NSWorkspace(Cocoa) launchApplication
208                         std::string app_ = File::decodePath("%APP%");
209                         std::stringstream command;
210                         char buf[256];
211                         for(int i=0; i<frame.size(); i++) {
212                                 memset(buf, 0, 256);
213                                 sprintf(buf, "cap/snap%04d.png\0", i+1);
214                                 frame[i]->save(buf);
215                         }
216                         command.str("");
217                         command << bin_path << " -i \"cap/snap%04d.png\" -loop_output 0 -pix_fmt rgb24 \"" << filename << "\"";
218                         NSString *str = [[NSString alloc] initWithUTF8String:command.str().c_str()];                    
219                         [[NSWorkspace sharedWorkspace] launchApplication:str];
220                         [str release];
221                         return *this;
222                 }
223         }
224 }       /*      <- namespace Psycholops         */