More Motif stuff
[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 Display *dpy = (Display*) wxGetDisplay();
261
262 if (DefaultDepth (dpy, DefaultScreen (dpy)) < 2)
263 return FALSE;
264 else
265 return TRUE;
266 }
267
268 // Returns depth of screen
269 int wxDisplayDepth()
270 {
271 Display *dpy = (Display*) wxGetDisplay();
272 return DefaultDepth (dpy, DefaultScreen (dpy));
273 }
274
275 // Get size of display
276 void wxDisplaySize(int *width, int *height)
277 {
278 Display *dpy = (Display*) wxGetDisplay();
279
280 *width = DisplayWidth (dpy, DefaultScreen (dpy));
281 *height = DisplayHeight (dpy, DefaultScreen (dpy));
282 }
283
284 /* Configurable display in Motif */
285 static WXDisplay *gs_currentDisplay = NULL;
286 static wxString gs_displayName;
287
288 WXDisplay *wxGetDisplay()
289 {
290 if (gs_currentDisplay)
291 return gs_currentDisplay;
292
293 return XtDisplay ((Widget) wxTheApp->GetTopLevelWidget());
294 }
295
296 bool wxSetDisplay(const wxString& display_name)
297 {
298 gs_displayName = display_name;
299
300 if (display_name.IsNull() || display_name.IsEmpty())
301 {
302 gs_currentDisplay = NULL;
303 return TRUE;
304 }
305 else
306 {
307 Cardinal argc = 0;
308
309 Display *display = XtOpenDisplay((XtAppContext) wxTheApp->GetAppContext(),
310 (const char*) display_name,
311 (const char*) wxTheApp->GetAppName(),
312 (const char*) wxTheApp->GetClassName(),
313 NULL,
314 # if XtSpecificationRelease < 5
315 0, &argc, NULL);
316 # else
317 0, (int *)&argc, NULL);
318 # endif
319
320 if (display)
321 {
322 gs_currentDisplay = (WXDisplay*) display;
323 return TRUE;
324 } else
325 return FALSE;
326 }
327 return FALSE;
328 }
329
330 wxString wxGetDisplayName()
331 {
332 return gs_displayName;
333 }
334
335 // Find the letter corresponding to the mnemonic, for Motif
336 char wxFindMnemonic (const char *s)
337 {
338 char mnem = 0;
339 int len = strlen (s);
340 int i;
341 for (i = 0; i < len; i++)
342 {
343 if (s[i] == '&')
344 {
345 // Carefully handle &&
346 if ((i + 1) <= len && s[i + 1] == '&')
347 i++;
348 else
349 {
350 mnem = s[i + 1];
351 break;
352 }
353 }
354 }
355 return mnem;
356 }
357
358 char * wxFindAccelerator (char *s)
359 {
360 // The accelerator text is after the \t char.
361 while (*s && *s != '\t')
362 s++;
363 if (*s == '\0')
364 return (NULL);
365 s++;
366 /*
367 Now we need to format it as X standard:
368
369 input output
370
371 F7 --> <Key>F7
372 Ctrl+N --> Ctrl<Key>N
373 Alt+k --> Meta<Key>k
374 Ctrl+Shift+A --> Ctrl Shift<Key>A
375
376 */
377
378 wxBuffer[0] = '\0';
379 char *tmp = copystring (s);
380 s = tmp;
381 char *p = s;
382
383 while (1)
384 {
385 while (*p && *p != '+')
386 p++;
387 if (*p)
388 {
389 *p = '\0';
390 if (wxBuffer[0])
391 strcat (wxBuffer, " ");
392 if (strcmp (s, "Alt"))
393 strcat (wxBuffer, s);
394 else
395 strcat (wxBuffer, "Meta");
396 s = p + 1;
397 p = s;
398 }
399 else
400 {
401 strcat (wxBuffer, "<Key>");
402 strcat (wxBuffer, s);
403 break;
404 }
405 }
406 delete[]tmp;
407 return wxBuffer;
408 }
409
410 XmString wxFindAcceleratorText (char *s)
411 {
412 // The accelerator text is after the \t char.
413 while (*s && *s != '\t')
414 s++;
415 if (*s == '\0')
416 return (NULL);
417 s++;
418 XmString text = XmStringCreateSimple (s);
419 return text;
420 }
421
422 #include <X11/keysym.h>
423
424 int wxCharCodeXToWX(KeySym keySym)
425 {
426 int id;
427 switch (keySym) {
428 case XK_Shift_L:
429 case XK_Shift_R:
430 id = WXK_SHIFT; break;
431 case XK_Control_L:
432 case XK_Control_R:
433 id = WXK_CONTROL; break;
434 case XK_BackSpace:
435 id = WXK_BACK; break;
436 case XK_Delete:
437 id = WXK_DELETE; break;
438 case XK_Clear:
439 id = WXK_CLEAR; break;
440 case XK_Tab:
441 id = WXK_TAB; break;
442 case XK_numbersign:
443 id = '#'; break;
444 case XK_Return:
445 id = WXK_RETURN; break;
446 case XK_Escape:
447 id = WXK_ESCAPE; break;
448 case XK_Pause:
449 case XK_Break:
450 id = WXK_PAUSE; break;
451 case XK_Num_Lock:
452 id = WXK_NUMLOCK; break;
453 case XK_Scroll_Lock:
454 id = WXK_SCROLL; break;
455
456 case XK_Home:
457 id = WXK_HOME; break;
458 case XK_End:
459 id = WXK_END; break;
460 case XK_Left:
461 id = WXK_LEFT; break;
462 case XK_Right:
463 id = WXK_RIGHT; break;
464 case XK_Up:
465 id = WXK_UP; break;
466 case XK_Down:
467 id = WXK_DOWN; break;
468 case XK_Next:
469 id = WXK_NEXT; break;
470 case XK_Prior:
471 id = WXK_PRIOR; break;
472 case XK_Menu:
473 id = WXK_MENU; break;
474 case XK_Select:
475 id = WXK_SELECT; break;
476 case XK_Cancel:
477 id = WXK_CANCEL; break;
478 case XK_Print:
479 id = WXK_PRINT; break;
480 case XK_Execute:
481 id = WXK_EXECUTE; break;
482 case XK_Insert:
483 id = WXK_INSERT; break;
484 case XK_Help:
485 id = WXK_HELP; break;
486
487 case XK_KP_Multiply:
488 id = WXK_MULTIPLY; break;
489 case XK_KP_Add:
490 id = WXK_ADD; break;
491 case XK_KP_Subtract:
492 id = WXK_SUBTRACT; break;
493 case XK_KP_Divide:
494 id = WXK_DIVIDE; break;
495 case XK_KP_Decimal:
496 id = WXK_DECIMAL; break;
497 case XK_KP_Equal:
498 id = '='; break;
499 case XK_KP_Space:
500 id = ' '; break;
501 case XK_KP_Tab:
502 id = WXK_TAB; break;
503 case XK_KP_Enter:
504 id = WXK_RETURN; break;
505 case XK_KP_0:
506 id = WXK_NUMPAD0; break;
507 case XK_KP_1:
508 id = WXK_NUMPAD1; break;
509 case XK_KP_2:
510 id = WXK_NUMPAD2; break;
511 case XK_KP_3:
512 id = WXK_NUMPAD3; break;
513 case XK_KP_4:
514 id = WXK_NUMPAD4; break;
515 case XK_KP_5:
516 id = WXK_NUMPAD5; break;
517 case XK_KP_6:
518 id = WXK_NUMPAD6; break;
519 case XK_KP_7:
520 id = WXK_NUMPAD7; break;
521 case XK_KP_8:
522 id = WXK_NUMPAD8; break;
523 case XK_KP_9:
524 id = WXK_NUMPAD9; break;
525 case XK_F1:
526 id = WXK_F1; break;
527 case XK_F2:
528 id = WXK_F2; break;
529 case XK_F3:
530 id = WXK_F3; break;
531 case XK_F4:
532 id = WXK_F4; break;
533 case XK_F5:
534 id = WXK_F5; break;
535 case XK_F6:
536 id = WXK_F6; break;
537 case XK_F7:
538 id = WXK_F7; break;
539 case XK_F8:
540 id = WXK_F8; break;
541 case XK_F9:
542 id = WXK_F9; break;
543 case XK_F10:
544 id = WXK_F10; break;
545 case XK_F11:
546 id = WXK_F11; break;
547 case XK_F12:
548 id = WXK_F12; break;
549 case XK_F13:
550 id = WXK_F13; break;
551 case XK_F14:
552 id = WXK_F14; break;
553 case XK_F15:
554 id = WXK_F15; break;
555 case XK_F16:
556 id = WXK_F16; break;
557 case XK_F17:
558 id = WXK_F17; break;
559 case XK_F18:
560 id = WXK_F18; break;
561 case XK_F19:
562 id = WXK_F19; break;
563 case XK_F20:
564 id = WXK_F20; break;
565 case XK_F21:
566 id = WXK_F21; break;
567 case XK_F22:
568 id = WXK_F22; break;
569 case XK_F23:
570 id = WXK_F23; break;
571 case XK_F24:
572 id = WXK_F24; break;
573 default:
574 id = (keySym <= 255) ? (int)keySym : -1;
575 } // switch
576 return id;
577 }
578
579 KeySym wxCharCodeWXToX(int id)
580 {
581 KeySym keySym;
582
583 switch (id) {
584 case WXK_CANCEL: keySym = XK_Cancel; break;
585 case WXK_BACK: keySym = XK_BackSpace; break;
586 case WXK_TAB: keySym = XK_Tab; break;
587 case WXK_CLEAR: keySym = XK_Clear; break;
588 case WXK_RETURN: keySym = XK_Return; break;
589 case WXK_SHIFT: keySym = XK_Shift_L; break;
590 case WXK_CONTROL: keySym = XK_Control_L; break;
591 case WXK_MENU : keySym = XK_Menu; break;
592 case WXK_PAUSE: keySym = XK_Pause; break;
593 case WXK_ESCAPE: keySym = XK_Escape; break;
594 case WXK_SPACE: keySym = ' '; break;
595 case WXK_PRIOR: keySym = XK_Prior; break;
596 case WXK_NEXT : keySym = XK_Next; break;
597 case WXK_END: keySym = XK_End; break;
598 case WXK_HOME : keySym = XK_Home; break;
599 case WXK_LEFT : keySym = XK_Left; break;
600 case WXK_UP: keySym = XK_Up; break;
601 case WXK_RIGHT: keySym = XK_Right; break;
602 case WXK_DOWN : keySym = XK_Down; break;
603 case WXK_SELECT: keySym = XK_Select; break;
604 case WXK_PRINT: keySym = XK_Print; break;
605 case WXK_EXECUTE: keySym = XK_Execute; break;
606 case WXK_INSERT: keySym = XK_Insert; break;
607 case WXK_DELETE: keySym = XK_Delete; break;
608 case WXK_HELP : keySym = XK_Help; break;
609 case WXK_NUMPAD0: keySym = XK_KP_0; break;
610 case WXK_NUMPAD1: keySym = XK_KP_1; break;
611 case WXK_NUMPAD2: keySym = XK_KP_2; break;
612 case WXK_NUMPAD3: keySym = XK_KP_3; break;
613 case WXK_NUMPAD4: keySym = XK_KP_4; break;
614 case WXK_NUMPAD5: keySym = XK_KP_5; break;
615 case WXK_NUMPAD6: keySym = XK_KP_6; break;
616 case WXK_NUMPAD7: keySym = XK_KP_7; break;
617 case WXK_NUMPAD8: keySym = XK_KP_8; break;
618 case WXK_NUMPAD9: keySym = XK_KP_9; break;
619 case WXK_MULTIPLY: keySym = XK_KP_Multiply; break;
620 case WXK_ADD: keySym = XK_KP_Add; break;
621 case WXK_SUBTRACT: keySym = XK_KP_Subtract; break;
622 case WXK_DECIMAL: keySym = XK_KP_Decimal; break;
623 case WXK_DIVIDE: keySym = XK_KP_Divide; break;
624 case WXK_F1: keySym = XK_F1; break;
625 case WXK_F2: keySym = XK_F2; break;
626 case WXK_F3: keySym = XK_F3; break;
627 case WXK_F4: keySym = XK_F4; break;
628 case WXK_F5: keySym = XK_F5; break;
629 case WXK_F6: keySym = XK_F6; break;
630 case WXK_F7: keySym = XK_F7; break;
631 case WXK_F8: keySym = XK_F8; break;
632 case WXK_F9: keySym = XK_F9; break;
633 case WXK_F10: keySym = XK_F10; break;
634 case WXK_F11: keySym = XK_F11; break;
635 case WXK_F12: keySym = XK_F12; break;
636 case WXK_F13: keySym = XK_F13; break;
637 case WXK_F14: keySym = XK_F14; break;
638 case WXK_F15: keySym = XK_F15; break;
639 case WXK_F16: keySym = XK_F16; break;
640 case WXK_F17: keySym = XK_F17; break;
641 case WXK_F18: keySym = XK_F18; break;
642 case WXK_F19: keySym = XK_F19; break;
643 case WXK_F20: keySym = XK_F20; break;
644 case WXK_F21: keySym = XK_F21; break;
645 case WXK_F22: keySym = XK_F22; break;
646 case WXK_F23: keySym = XK_F23; break;
647 case WXK_F24: keySym = XK_F24; break;
648 case WXK_NUMLOCK: keySym = XK_Num_Lock; break;
649 case WXK_SCROLL: keySym = XK_Scroll_Lock; break;
650 default: keySym = id <= 255 ? (KeySym)id : 0;
651 } // switch
652 return keySym;
653 }