Some more wxMotif stuff: menus
[wxWidgets.git] / src / motif / utils.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utils.cpp
3 // Purpose: Various utilities
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 // Note: this is done in utilscmn.cpp now.
14 // #pragma implementation
15 // #pragma implementation "utils.h"
16 #endif
17
18 #include "wx/setup.h"
19 #include "wx/utils.h"
20 #include "wx/app.h"
21
22 #include <ctype.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28
29 #include <Xm/Xm.h>
30
31 #include "wx/motif/private.h"
32
33 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
34 bool wxGetHostName(char *buf, int maxSize)
35 {
36 // TODO
37 return FALSE;
38 }
39
40 // Get user ID e.g. jacs
41 bool wxGetUserId(char *buf, int maxSize)
42 {
43 // TODO
44 return FALSE;
45 }
46
47 // Get user name e.g. Julian Smart
48 bool wxGetUserName(char *buf, int maxSize)
49 {
50 // TODO
51 return FALSE;
52 }
53
54 int wxKill(long pid, int sig)
55 {
56 // TODO
57 return 0;
58 }
59
60 //
61 // Execute a program in an Interactive Shell
62 //
63 bool wxShell(const wxString& command)
64 {
65 // TODO
66 return FALSE;
67 }
68
69 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
70 long wxGetFreeMemory()
71 {
72 // TODO
73 return 0;
74 }
75
76 void wxSleep(int nSecs)
77 {
78 // TODO
79 }
80
81 // Consume all events until no more left
82 void wxFlushEvents()
83 {
84 }
85
86 // Output a debug message, in a system dependent fashion.
87 void wxDebugMsg(const char *fmt ...)
88 {
89 va_list ap;
90 static char buffer[512];
91
92 if (!wxTheApp->GetWantDebugOutput())
93 return ;
94
95 va_start(ap, fmt);
96
97 // wvsprintf(buffer,fmt,ap) ;
98 // TODO: output buffer
99
100 va_end(ap);
101 }
102
103 // Non-fatal error: pop up message box and (possibly) continue
104 void wxError(const wxString& msg, const wxString& title)
105 {
106 // TODO
107 wxExit();
108 }
109
110 // Fatal error: pop up message box and abort
111 void wxFatalError(const wxString& msg, const wxString& title)
112 {
113 // TODO
114 }
115
116 // Emit a beeeeeep
117 void wxBell()
118 {
119 // TODO
120 }
121
122 int wxGetOsVersion(int *majorVsn, int *minorVsn)
123 {
124 // TODO
125 return 0;
126 }
127
128 // Reading and writing resources (eg WIN.INI, .Xdefaults)
129 #if USE_RESOURCES
130 bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
131 {
132 // TODO
133 return FALSE;
134 }
135
136 bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
137 {
138 char buf[50];
139 sprintf(buf, "%.4f", value);
140 return wxWriteResource(section, entry, buf, file);
141 }
142
143 bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
144 {
145 char buf[50];
146 sprintf(buf, "%ld", value);
147 return wxWriteResource(section, entry, buf, file);
148 }
149
150 bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
151 {
152 char buf[50];
153 sprintf(buf, "%d", value);
154 return wxWriteResource(section, entry, buf, file);
155 }
156
157 bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
158 {
159 // TODO
160 return FALSE;
161 }
162
163 bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
164 {
165 char *s = NULL;
166 bool succ = wxGetResource(section, entry, (char **)&s, file);
167 if (succ)
168 {
169 *value = (float)strtod(s, NULL);
170 delete[] s;
171 return TRUE;
172 }
173 else return FALSE;
174 }
175
176 bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
177 {
178 char *s = NULL;
179 bool succ = wxGetResource(section, entry, (char **)&s, file);
180 if (succ)
181 {
182 *value = strtol(s, NULL, 10);
183 delete[] s;
184 return TRUE;
185 }
186 else return FALSE;
187 }
188
189 bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
190 {
191 char *s = NULL;
192 bool succ = wxGetResource(section, entry, (char **)&s, file);
193 if (succ)
194 {
195 *value = (int)strtol(s, NULL, 10);
196 delete[] s;
197 return TRUE;
198 }
199 else return FALSE;
200 }
201 #endif // USE_RESOURCES
202
203 static int wxBusyCursorCount = 0;
204
205 // Set the cursor to the busy cursor for all windows
206 void wxBeginBusyCursor(wxCursor *cursor)
207 {
208 wxBusyCursorCount ++;
209 if (wxBusyCursorCount == 1)
210 {
211 // TODO
212 }
213 else
214 {
215 // TODO
216 }
217 }
218
219 // Restore cursor to normal
220 void wxEndBusyCursor()
221 {
222 if (wxBusyCursorCount == 0)
223 return;
224
225 wxBusyCursorCount --;
226 if (wxBusyCursorCount == 0)
227 {
228 // TODO
229 }
230 }
231
232 // TRUE if we're between the above two calls
233 bool wxIsBusy()
234 {
235 return (wxBusyCursorCount > 0);
236 }
237
238 char *wxGetUserHome (const wxString& user)
239 {
240 // TODO
241 return NULL;
242 }
243
244 // Check whether this window wants to process messages, e.g. Stop button
245 // in long calculations.
246 bool wxCheckForInterrupt(wxWindow *wnd)
247 {
248 // TODO
249 return FALSE;
250 }
251
252 void wxGetMousePosition( int* x, int* y )
253 {
254 // TODO
255 };
256
257 // Return TRUE if we have a colour display
258 bool wxColourDisplay()
259 {
260 // TODO
261 return TRUE;
262 }
263
264 // Returns depth of screen
265 int wxDisplayDepth()
266 {
267 // TODO
268 return 0;
269 }
270
271 // Get size of display
272 void wxDisplaySize(int *width, int *height)
273 {
274 // TODO
275 }
276
277 /* Configurable display in Motif */
278 static WXDisplay *gs_currentDisplay = NULL;
279 static wxString gs_displayName;
280
281 WXDisplay *wxGetDisplay()
282 {
283 if (gs_currentDisplay)
284 return gs_currentDisplay;
285
286 return XtDisplay ((Widget) wxTheApp->GetTopLevelWidget());
287 }
288
289 bool wxSetDisplay(const wxString& display_name)
290 {
291 gs_displayName = display_name;
292
293 if (display_name.IsNull() || display_name.IsEmpty())
294 {
295 gs_currentDisplay = NULL;
296 return TRUE;
297 }
298 else
299 {
300 Cardinal argc = 0;
301
302 Display *display = XtOpenDisplay((XtAppContext) wxTheApp->GetAppContext(),
303 (const char*) display_name,
304 (const char*) wxTheApp->GetAppName(),
305 (const char*) wxTheApp->GetClassName(),
306 NULL,
307 # if XtSpecificationRelease < 5
308 0, &argc, NULL);
309 # else
310 0, (int *)&argc, NULL);
311 # endif
312
313 if (display)
314 {
315 gs_currentDisplay = (WXDisplay*) display;
316 return TRUE;
317 } else
318 return FALSE;
319 }
320 return FALSE;
321 }
322
323 wxString wxGetDisplayName()
324 {
325 return gs_displayName;
326 }
327
328 // Find the letter corresponding to the mnemonic, for Motif
329 char wxFindMnemonic (const char *s)
330 {
331 char mnem = 0;
332 int len = strlen (s);
333 int i;
334 for (i = 0; i < len; i++)
335 {
336 if (s[i] == '&')
337 {
338 // Carefully handle &&
339 if ((i + 1) <= len && s[i + 1] == '&')
340 i++;
341 else
342 {
343 mnem = s[i + 1];
344 break;
345 }
346 }
347 }
348 return mnem;
349 }
350
351 char * wxFindAccelerator (char *s)
352 {
353 // The accelerator text is after the \t char.
354 while (*s && *s != '\t')
355 s++;
356 if (*s == '\0')
357 return (NULL);
358 s++;
359 /*
360 Now we need to format it as X standard:
361
362 input output
363
364 F7 --> <Key>F7
365 Ctrl+N --> Ctrl<Key>N
366 Alt+k --> Meta<Key>k
367 Ctrl+Shift+A --> Ctrl Shift<Key>A
368
369 */
370
371 wxBuffer[0] = '\0';
372 char *tmp = copystring (s);
373 s = tmp;
374 char *p = s;
375
376 while (1)
377 {
378 while (*p && *p != '+')
379 p++;
380 if (*p)
381 {
382 *p = '\0';
383 if (wxBuffer[0])
384 strcat (wxBuffer, " ");
385 if (strcmp (s, "Alt"))
386 strcat (wxBuffer, s);
387 else
388 strcat (wxBuffer, "Meta");
389 s = p + 1;
390 p = s;
391 }
392 else
393 {
394 strcat (wxBuffer, "<Key>");
395 strcat (wxBuffer, s);
396 break;
397 }
398 }
399 delete[]tmp;
400 return wxBuffer;
401 }
402
403 XmString wxFindAcceleratorText (char *s)
404 {
405 // The accelerator text is after the \t char.
406 while (*s && *s != '\t')
407 s++;
408 if (*s == '\0')
409 return (NULL);
410 s++;
411 XmString text = XmStringCreateSimple (s);
412 return text;
413 }
414
415 #include <X11/keysym.h>
416
417 int wxCharCodeXToWX(KeySym keySym)
418 {
419 int id;
420 switch (keySym) {
421 case XK_Shift_L:
422 case XK_Shift_R:
423 id = WXK_SHIFT; break;
424 case XK_Control_L:
425 case XK_Control_R:
426 id = WXK_CONTROL; break;
427 case XK_BackSpace:
428 id = WXK_BACK; break;
429 case XK_Delete:
430 id = WXK_DELETE; break;
431 case XK_Clear:
432 id = WXK_CLEAR; break;
433 case XK_Tab:
434 id = WXK_TAB; break;
435 case XK_numbersign:
436 id = '#'; break;
437 case XK_Return:
438 id = WXK_RETURN; break;
439 case XK_Escape:
440 id = WXK_ESCAPE; break;
441 case XK_Pause:
442 case XK_Break:
443 id = WXK_PAUSE; break;
444 case XK_Num_Lock:
445 id = WXK_NUMLOCK; break;
446 case XK_Scroll_Lock:
447 id = WXK_SCROLL; break;
448
449 case XK_Home:
450 id = WXK_HOME; break;
451 case XK_End:
452 id = WXK_END; break;
453 case XK_Left:
454 id = WXK_LEFT; break;
455 case XK_Right:
456 id = WXK_RIGHT; break;
457 case XK_Up:
458 id = WXK_UP; break;
459 case XK_Down:
460 id = WXK_DOWN; break;
461 case XK_Next:
462 id = WXK_NEXT; break;
463 case XK_Prior:
464 id = WXK_PRIOR; break;
465 case XK_Menu:
466 id = WXK_MENU; break;
467 case XK_Select:
468 id = WXK_SELECT; break;
469 case XK_Cancel:
470 id = WXK_CANCEL; break;
471 case XK_Print:
472 id = WXK_PRINT; break;
473 case XK_Execute:
474 id = WXK_EXECUTE; break;
475 case XK_Insert:
476 id = WXK_INSERT; break;
477 case XK_Help:
478 id = WXK_HELP; break;
479
480 case XK_KP_Multiply:
481 id = WXK_MULTIPLY; break;
482 case XK_KP_Add:
483 id = WXK_ADD; break;
484 case XK_KP_Subtract:
485 id = WXK_SUBTRACT; break;
486 case XK_KP_Divide:
487 id = WXK_DIVIDE; break;
488 case XK_KP_Decimal:
489 id = WXK_DECIMAL; break;
490 case XK_KP_Equal:
491 id = '='; break;
492 case XK_KP_Space:
493 id = ' '; break;
494 case XK_KP_Tab:
495 id = WXK_TAB; break;
496 case XK_KP_Enter:
497 id = WXK_RETURN; break;
498 case XK_KP_0:
499 id = WXK_NUMPAD0; break;
500 case XK_KP_1:
501 id = WXK_NUMPAD1; break;
502 case XK_KP_2:
503 id = WXK_NUMPAD2; break;
504 case XK_KP_3:
505 id = WXK_NUMPAD3; break;
506 case XK_KP_4:
507 id = WXK_NUMPAD4; break;
508 case XK_KP_5:
509 id = WXK_NUMPAD5; break;
510 case XK_KP_6:
511 id = WXK_NUMPAD6; break;
512 case XK_KP_7:
513 id = WXK_NUMPAD7; break;
514 case XK_KP_8:
515 id = WXK_NUMPAD8; break;
516 case XK_KP_9:
517 id = WXK_NUMPAD9; break;
518 case XK_F1:
519 id = WXK_F1; break;
520 case XK_F2:
521 id = WXK_F2; break;
522 case XK_F3:
523 id = WXK_F3; break;
524 case XK_F4:
525 id = WXK_F4; break;
526 case XK_F5:
527 id = WXK_F5; break;
528 case XK_F6:
529 id = WXK_F6; break;
530 case XK_F7:
531 id = WXK_F7; break;
532 case XK_F8:
533 id = WXK_F8; break;
534 case XK_F9:
535 id = WXK_F9; break;
536 case XK_F10:
537 id = WXK_F10; break;
538 case XK_F11:
539 id = WXK_F11; break;
540 case XK_F12:
541 id = WXK_F12; break;
542 case XK_F13:
543 id = WXK_F13; break;
544 case XK_F14:
545 id = WXK_F14; break;
546 case XK_F15:
547 id = WXK_F15; break;
548 case XK_F16:
549 id = WXK_F16; break;
550 case XK_F17:
551 id = WXK_F17; break;
552 case XK_F18:
553 id = WXK_F18; break;
554 case XK_F19:
555 id = WXK_F19; break;
556 case XK_F20:
557 id = WXK_F20; break;
558 case XK_F21:
559 id = WXK_F21; break;
560 case XK_F22:
561 id = WXK_F22; break;
562 case XK_F23:
563 id = WXK_F23; break;
564 case XK_F24:
565 id = WXK_F24; break;
566 default:
567 id = (keySym <= 255) ? (int)keySym : -1;
568 } // switch
569 return id;
570 }
571
572 KeySym wxCharCodeWXToX(int id)
573 {
574 KeySym keySym;
575
576 switch (id) {
577 case WXK_CANCEL: keySym = XK_Cancel; break;
578 case WXK_BACK: keySym = XK_BackSpace; break;
579 case WXK_TAB: keySym = XK_Tab; break;
580 case WXK_CLEAR: keySym = XK_Clear; break;
581 case WXK_RETURN: keySym = XK_Return; break;
582 case WXK_SHIFT: keySym = XK_Shift_L; break;
583 case WXK_CONTROL: keySym = XK_Control_L; break;
584 case WXK_MENU : keySym = XK_Menu; break;
585 case WXK_PAUSE: keySym = XK_Pause; break;
586 case WXK_ESCAPE: keySym = XK_Escape; break;
587 case WXK_SPACE: keySym = ' '; break;
588 case WXK_PRIOR: keySym = XK_Prior; break;
589 case WXK_NEXT : keySym = XK_Next; break;
590 case WXK_END: keySym = XK_End; break;
591 case WXK_HOME : keySym = XK_Home; break;
592 case WXK_LEFT : keySym = XK_Left; break;
593 case WXK_UP: keySym = XK_Up; break;
594 case WXK_RIGHT: keySym = XK_Right; break;
595 case WXK_DOWN : keySym = XK_Down; break;
596 case WXK_SELECT: keySym = XK_Select; break;
597 case WXK_PRINT: keySym = XK_Print; break;
598 case WXK_EXECUTE: keySym = XK_Execute; break;
599 case WXK_INSERT: keySym = XK_Insert; break;
600 case WXK_DELETE: keySym = XK_Delete; break;
601 case WXK_HELP : keySym = XK_Help; break;
602 case WXK_NUMPAD0: keySym = XK_KP_0; break;
603 case WXK_NUMPAD1: keySym = XK_KP_1; break;
604 case WXK_NUMPAD2: keySym = XK_KP_2; break;
605 case WXK_NUMPAD3: keySym = XK_KP_3; break;
606 case WXK_NUMPAD4: keySym = XK_KP_4; break;
607 case WXK_NUMPAD5: keySym = XK_KP_5; break;
608 case WXK_NUMPAD6: keySym = XK_KP_6; break;
609 case WXK_NUMPAD7: keySym = XK_KP_7; break;
610 case WXK_NUMPAD8: keySym = XK_KP_8; break;
611 case WXK_NUMPAD9: keySym = XK_KP_9; break;
612 case WXK_MULTIPLY: keySym = XK_KP_Multiply; break;
613 case WXK_ADD: keySym = XK_KP_Add; break;
614 case WXK_SUBTRACT: keySym = XK_KP_Subtract; break;
615 case WXK_DECIMAL: keySym = XK_KP_Decimal; break;
616 case WXK_DIVIDE: keySym = XK_KP_Divide; break;
617 case WXK_F1: keySym = XK_F1; break;
618 case WXK_F2: keySym = XK_F2; break;
619 case WXK_F3: keySym = XK_F3; break;
620 case WXK_F4: keySym = XK_F4; break;
621 case WXK_F5: keySym = XK_F5; break;
622 case WXK_F6: keySym = XK_F6; break;
623 case WXK_F7: keySym = XK_F7; break;
624 case WXK_F8: keySym = XK_F8; break;
625 case WXK_F9: keySym = XK_F9; break;
626 case WXK_F10: keySym = XK_F10; break;
627 case WXK_F11: keySym = XK_F11; break;
628 case WXK_F12: keySym = XK_F12; break;
629 case WXK_F13: keySym = XK_F13; break;
630 case WXK_F14: keySym = XK_F14; break;
631 case WXK_F15: keySym = XK_F15; break;
632 case WXK_F16: keySym = XK_F16; break;
633 case WXK_F17: keySym = XK_F17; break;
634 case WXK_F18: keySym = XK_F18; break;
635 case WXK_F19: keySym = XK_F19; break;
636 case WXK_F20: keySym = XK_F20; break;
637 case WXK_F21: keySym = XK_F21; break;
638 case WXK_F22: keySym = XK_F22; break;
639 case WXK_F23: keySym = XK_F23; break;
640 case WXK_F24: keySym = XK_F24; break;
641 case WXK_NUMLOCK: keySym = XK_Num_Lock; break;
642 case WXK_SCROLL: keySym = XK_Scroll_Lock; break;
643 default: keySym = id <= 255 ? (KeySym)id : 0;
644 } // switch
645 return keySym;
646 }