gwenhywfar  4.3.3
cocoa_gui.m
Go to the documentation of this file.
1 /***************************************************************************
2  begin : August 03 2010
3  copyright : (C) 2010 by Samuel Strupp
4 
5  ***************************************************************************
6  * Please see toplevel file COPYING for license details *
7  ***************************************************************************/
8 
9 
10 #ifdef HAVE_CONFIG_H
11 # include <config.h>
12 #endif
13 
14 
15 #include "cocoa_gui_dialog_l.h"
16 #include "cocoa_gui_p.h"
17 
18 #include <assert.h>
19 
20 #include <gwenhywfar/inherit.h>
21 #include <gwenhywfar/debug.h>
22 #include <gwenhywfar/gui_be.h>
23 #include <gwenhywfar/i18n.h>
24 
25 #include <gwenhywfar/text.h>
26 #include <gwenhywfar/mdigest.h>
27 #include <gwenhywfar/debug.h>
28 
29 
30 GWEN_INHERIT(GWEN_GUI, COCOA_GUI)
31 
32 
33 #define I18N(msg) GWEN_I18N_Translate(PACKAGE, msg)
34 
35 
36 
38  GWEN_GUI *gui;
39  COCOA_GUI *xgui;
40 
41  gui=GWEN_Gui_new();
42  GWEN_NEW_OBJECT(COCOA_GUI, xgui);
43  GWEN_INHERIT_SETDATA(GWEN_GUI, COCOA_GUI, gui, xgui, Cocoa_Gui_FreeData);
44 
47  xgui->execDialogFn=GWEN_Gui_SetExecDialogFn(gui, COCOA_Gui_ExecDialog);
48  xgui->openDialogFn=GWEN_Gui_SetOpenDialogFn(gui, COCOA_Gui_OpenDialog);
49  xgui->closeDialogFn=GWEN_Gui_SetCloseDialogFn(gui, COCOA_Gui_CloseDialog);
50  xgui->runDialogFn=GWEN_Gui_SetRunDialogFn(gui, COCOA_Gui_RunDialog);
51  xgui->getFileNameDialogFn=GWEN_Gui_SetGetFileNameFn(gui, COCOA_Gui_GetFileName);
52 
53  return gui;
54 }
55 
56 
57 
58 void Cocoa_Gui_FreeData(void *bp, void *p) {
59  COCOA_GUI *xgui;
60 
61  xgui=(COCOA_GUI*) p;
62 
63  GWEN_FREE_OBJECT(xgui);
64 }
65 
66 
67 
68 
69 int COCOA_Gui_ExecDialog(GWEN_GUI *gui, GWEN_DIALOG *dlg, uint32_t guiid) {
70  NSLog(@"ExecDialog");
71 
72  int rv;
73 
74  rv = COCOA_Gui_OpenDialog(gui, dlg, guiid);
75  if (rv<0) {
76  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
77  return rv;
78  }
79 
80  rv = COCOA_Gui_RunDialog(gui, dlg, 1);
81  COCOA_Gui_CloseDialog(gui, dlg);
82  if (rv<0) {
83  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
84  return rv;
85  }
86 
87  return rv;
88 }
89 
90 
91 
92 int COCOA_Gui_OpenDialog(GWEN_GUI *gui, GWEN_DIALOG *dlg, uint32_t guiid) {
93  int rv;
94  NSWindow *g;
95 
96  NSLog(@"OpenDialog");
97 
99  rv=CocoaGui_Dialog_Setup(dlg, NULL);
100  if (rv<0) {
101  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
103  return rv;
104  }
105 
106  NSLog(@"OpenDialog Setup");
107 
109  if (g==NULL) {
110  DBG_ERROR(GWEN_LOGDOMAIN, "No main widget");
112  return GWEN_ERROR_INVALID;
113  }
114 
115  NSLog(@"OpenDialog GetMainWidget");
116 
118  if (rv<0) {
119  DBG_INFO(GWEN_LOGDOMAIN, "Error initializing dialog: %d", rv);
121  return rv;
122  }
123 
124  NSLog(@"OpenDialog EmitSignalToAll");
125 
126  /* show dialog */
127  [g center];
128  [g makeKeyAndOrderFront:nil];
129 
130 
131  return 0;
132 }
133 
134 
135 
137  NSWindow *g;
138  int rv;
139 
141  if (g==NULL) {
142  DBG_ERROR(GWEN_LOGDOMAIN, "No main widget");
144  return GWEN_ERROR_INVALID;
145  }
146 
147  /* hide dialog */
148  [g close];
149 
150 
151  /* send fini signal to dialog */
153  if (rv<0) {
154  DBG_INFO(GWEN_LOGDOMAIN, "Error deinitializing dialog: %d", rv);
156  return rv;
157  }
158 
160  return 0;
161 }
162 
163 
164 
165 int COCOA_Gui_RunDialog(GWEN_GUI *gui, GWEN_DIALOG *dlg, int untilEnd) {
166  int rv;
167 
168  rv=Cocoa_Gui_Dialog_Run(dlg, untilEnd);
169  if (rv<0) {
170  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
171  return rv;
172  }
173  return rv;
174 }
175 
176 
177 
179  const char *caption,
181  uint32_t flags,
182  const char *patterns,
183  GWEN_BUFFER *pathBuffer,
184  uint32_t guiid) {
185  char *folder=NULL;
186  char *fileName=NULL;
187 
188  if (GWEN_Buffer_GetUsedBytes(pathBuffer)) {
189  folder=strdup(GWEN_Buffer_GetStart(pathBuffer));
190  fileName=strchr(folder, GWEN_DIR_SEPARATOR);
191  if (fileName) {
192  *fileName=0;
193  fileName++;
194  if (*fileName==0)
195  fileName=NULL;
196  }
197  }
198 
199  switch(fnt) {
202 
203  NSString *title = nil;
204  if (caption && *caption)
205  title = [[[NSString alloc] initWithCString:caption encoding:NSUTF8StringEncoding] autorelease];
206 
207  NSOpenPanel *openPanel = [NSOpenPanel openPanel];
208  if (openPanel) {
209  if (title) [openPanel setTitle:title];
210  [openPanel setAllowsMultipleSelection:NO];
211  [openPanel setCanChooseFiles:(fnt == GWEN_Gui_FileNameType_OpenFileName)];
212  [openPanel setCanChooseDirectories:(fnt == GWEN_Gui_FileNameType_OpenDirectory)];
213 
214  NSString *pathToFolder = nil;
215 
216  if (fileName && *fileName)
217  pathToFolder = [[[NSString alloc] initWithCString:fileName encoding:NSUTF8StringEncoding] autorelease];
218 
219  if (pathToFolder)
220  [openPanel setDirectory:pathToFolder];
221 
222  NSInteger result = [openPanel runModal];
223  if (result == NSFileHandlingPanelOKButton) {
224  NSArray *urls = [openPanel URLs];
225  if (urls && [urls count] > 0) {
226  NSString *path = [[(NSURL*)[urls objectAtIndex:0] path] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
227  if (path && [path length]>0) {
228  const char *filename = [path cStringUsingEncoding:NSUTF8StringEncoding];
229  GWEN_Buffer_Reset(pathBuffer);
230  GWEN_Buffer_AppendString(pathBuffer, filename);
231  return 0;
232  }
233  }
234  }
235  }
236 
238  }
239 
241 
242  NSString *title = nil;
243  if (caption && *caption)
244  title = [[[NSString alloc] initWithCString:caption encoding:NSUTF8StringEncoding] autorelease];
245 
246  NSSavePanel *savePanel = [NSSavePanel savePanel];
247  if (savePanel) {
248  if (title) [savePanel setTitle:title];
249  [savePanel setCanCreateDirectories:YES];
250 
251  NSString *pathToFolder = nil;
252 
253  if (fileName && *fileName)
254  pathToFolder = [[[NSString alloc] initWithCString:fileName encoding:NSUTF8StringEncoding] autorelease];
255 
256  if (pathToFolder)
257  [savePanel setDirectory:pathToFolder];
258 
259  NSInteger result = [savePanel runModal];
260  if (result == NSFileHandlingPanelOKButton) {
261  NSURL *url = [savePanel URL];
262  if (url) {
263  NSString *path = [[url path] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
264  if (path && [path length]>0) {
265  const char *filename = [path cStringUsingEncoding:NSUTF8StringEncoding];
266  GWEN_Buffer_Reset(pathBuffer);
267  GWEN_Buffer_AppendString(pathBuffer, filename);
268  return 0;
269  }
270  }
271  }
272  }
273 
275  }
276 
277  default:
278  break;
279  }
280  free(folder);
281 
283 }
284 
285 
286