]> git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/gtkmisc.cpp
use q3map2 from NetRadiant (VOLUNTEERS: split this commit into functional parts!)
[xonotic/netradiant.git] / radiant / gtkmisc.cpp
1 /*
2 Copyright (c) 2001, Loki software, inc.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this list
9 of conditions and the following disclaimer.
10
11 Redistributions in binary form must reproduce the above copyright notice, this
12 list of conditions and the following disclaimer in the documentation and/or
13 other materials provided with the distribution.
14
15 Neither the name of Loki software nor the names of its contributors may be used
16 to endorse or promote products derived from this software without specific prior
17 written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
23 DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 //
32 // Small functions to help with GTK
33 //
34
35 #include <gdk/gdkkeysyms.h>
36 #include <glib/gi18n.h>
37
38 #if defined (__linux__) || defined (__APPLE__)
39 #include <unistd.h>
40 #endif
41
42 #include <gtk/gtk.h>
43
44 #ifdef _WIN32
45 #include <gdk/gdkwin32.h>
46 #define WIN32_LEAN_AND_MEAN
47 #include <windows.h>
48 #endif
49
50
51
52 #ifdef _WIN32
53 #include <io.h>
54 #include <direct.h>
55 #define R_OK 04
56 #endif
57 #include "stdafx.h"
58
59 // =============================================================================
60 // Misc stuff
61
62 // NOTE TTimo window position saving has always been tricky
63 //   it doesn't work the same between win32 and linux .. see below that code is fairly different
64 //   it's also very poorly done, the save calls are a bit randomly disctributed in the OnDestroy
65
66 void save_window_pos (GtkWidget *wnd, window_position_t& pos)
67 {
68   if ((wnd == NULL) || (wnd->window == NULL))
69     return;
70
71   get_window_pos(wnd, &pos.x, &pos.y);
72
73   pos.w = wnd->allocation.width;
74   pos.h = wnd->allocation.height;
75
76 #ifdef DBG_WINDOWPOS
77   //Sys_Printf("save_window_pos 'Window %s'\n",buf);
78 #endif
79 }
80
81 #ifdef _WIN32
82 void win32_get_window_pos(GtkWidget *widget, gint *x, gint *y)
83 {
84   if ( g_PrefsDlg.m_bStartOnPrimMon ) {
85     RECT rc;
86     POINT point;
87     HWND xwnd = (HWND)GDK_WINDOW_HWND (widget->window);
88     const GdkRectangle primaryMonitorRect = g_pParentWnd->GetPrimaryMonitorRect();
89
90     GetClientRect(xwnd,&rc);
91     point.x=rc.left;
92     point.y=rc.top;
93     ClientToScreen(xwnd,&point);
94
95     *x=point.x;
96     *y=point.y;
97
98     *x=max(*x,-widget->allocation.width+10);
99     *x=min(*x,primaryMonitorRect.width-10);
100     *y=max(*y,-widget->allocation.height+10);
101     *y=min(*y,primaryMonitorRect.height-10);
102   } else {
103     // this is the same as the unix version of get_window_pos
104     gdk_window_get_root_origin (widget->window, x, y);
105   }
106 #ifdef DBG_WINDOWPOS
107   Sys_Printf("win32_get_window_pos %p %d,%d\n",widget,*x,*y);
108 #endif
109 }
110 #endif
111
112 void load_window_pos (GtkWidget *wnd, window_position_t& pos)
113 {
114 #ifdef _WIN32
115   const GdkRectangle primaryMonitorRect = g_pParentWnd->GetPrimaryMonitorRect();
116
117   if(pos.x < primaryMonitorRect.x
118     || pos.y < primaryMonitorRect.y
119     || pos.x > primaryMonitorRect.x + primaryMonitorRect.width
120     || pos.y > primaryMonitorRect.y + primaryMonitorRect.height)
121     gtk_window_set_position(GTK_WINDOW(wnd), GTK_WIN_POS_CENTER_ON_PARENT);
122 #else
123   // FIXME: not multihead safe
124   if(pos.x < 0
125     || pos.y < 0
126     || pos.x > gdk_screen_width ()
127     || pos.y > gdk_screen_height ())
128     gtk_window_set_position(GTK_WINDOW(wnd), GTK_WIN_POS_CENTER_ON_PARENT);
129 #endif
130   else
131     gtk_window_move(GTK_WINDOW(wnd), pos.x, pos.y);
132
133   gtk_window_set_default_size (GTK_WINDOW (wnd), pos.w, pos.h);
134 #ifdef DBG_WINDOWPOS
135   Sys_Printf("load_window_pos %p 'Window,%s'\n",wnd,windowData);
136 #endif
137 }
138
139 gint widget_delete_hide (GtkWidget *widget)
140 {
141   gtk_widget_hide (widget);
142
143   return TRUE;
144 }
145
146
147 // Thanks to Mercury, Fingolfin - ETG
148 int readLongLE(FILE *file, unsigned long *m_bytesRead, int *value)
149 {
150   byte buf[4];
151   int len = fread(buf, 4, 1, file);
152   *m_bytesRead += 4;
153   if (len != 1)
154     return -1;
155
156   *value = buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24;
157   return 0;
158 }
159
160 short readShortLE(FILE *file, unsigned long *m_bytesRead, short unsigned *value)
161 {
162   byte buf[2];
163   int len = fread(buf, 2, 1, file);
164   *m_bytesRead += 2;
165   if (len != 1)
166     return -1;
167
168   *value = buf[0] | buf[1] << 8;
169   return 0;
170 }
171
172 unsigned char *load_bitmap_file (const char* filename, guint16 *width, guint16 *height)
173 {
174   int bmWidth, bmHeight;
175   short unsigned bmPlanes, bmBitsPixel;
176   typedef struct {
177     unsigned char rgbBlue;
178     unsigned char rgbGreen;
179     unsigned char rgbRed;
180     unsigned char rgbReserved;
181   } RGBQUAD;
182   unsigned char m1,m2;
183   int sizeimage;
184   short unsigned res1,res2;
185   int filesize, pixoff;
186   int bmisize, compression;
187   int xscale, yscale;
188   int colors, impcol;
189   unsigned long m_bytesRead = 0;
190   unsigned char *imagebits = NULL;
191   FILE *fp;
192
193   *width = *height = 0;
194
195   fp = fopen(filename,"rb");
196   if (fp == NULL)
197   {
198         return NULL;
199   }
200
201   size_t rc;
202   rc = fread(&m1, 1, 1, fp);
203   m_bytesRead++;
204   if (rc == -1)
205   {
206       fclose(fp);
207       return NULL;
208   }
209
210   rc = fread(&m2, 1, 1, fp);
211   m_bytesRead++;
212   if ((m1!='B') || (m2!='M'))
213   {
214       fclose(fp);
215       return NULL;
216   }
217
218   if (readLongLE(fp,&m_bytesRead,&filesize)) {
219         fclose(fp);
220         return NULL;
221   }
222
223   if (readShortLE(fp,&m_bytesRead,&res1)) {
224         fclose(fp);
225         return NULL;
226   }
227
228   if (readShortLE(fp,&m_bytesRead,&res2)) {
229         fclose(fp);
230         return NULL;
231   }
232
233   if (readLongLE(fp,&m_bytesRead,&pixoff)) {
234         fclose(fp);
235         return NULL;
236   }
237
238   if (readLongLE(fp,&m_bytesRead,&bmisize)) {
239         fclose(fp);
240         return NULL;
241   }
242
243   if (readLongLE(fp,&m_bytesRead,&bmWidth)) {
244         fclose(fp);
245         return NULL;
246   }
247
248   if (readLongLE(fp,&m_bytesRead,&bmHeight)) {
249         fclose(fp);
250         return NULL;
251   }
252
253   if (readShortLE(fp,&m_bytesRead,&bmPlanes)) {
254         fclose(fp);
255         return NULL;
256   }
257
258   if (readShortLE(fp,&m_bytesRead,&bmBitsPixel)) {
259         fclose(fp);
260         return NULL;
261   }
262
263   if (readLongLE(fp,&m_bytesRead,&compression)) {
264         fclose(fp);
265         return NULL;
266   }
267
268   if (readLongLE(fp,&m_bytesRead,&sizeimage)) {
269         fclose(fp);
270         return NULL;
271   }
272
273   if (readLongLE(fp,&m_bytesRead,&xscale)) {
274         fclose(fp);
275         return NULL;
276   }
277
278   if (readLongLE(fp,&m_bytesRead,&yscale)) {
279         fclose(fp);
280         return NULL;
281   }
282
283   if (readLongLE(fp,&m_bytesRead,&colors)) {
284         fclose(fp);
285         return NULL;
286   }
287
288   if (readLongLE(fp,&m_bytesRead,&impcol)) {
289         fclose(fp);
290         return NULL;
291   }
292
293   if (colors == 0)
294     colors = 1 << bmBitsPixel;
295
296   RGBQUAD *colormap = NULL;
297   if (bmBitsPixel != 24)
298   {
299         colormap = new RGBQUAD[colors];
300         if (colormap == NULL)
301         {
302           fclose(fp);
303           return NULL;
304         }
305
306         int i;
307         for (i = 0; i < colors; i++)
308         {
309           unsigned char r ,g, b, dummy;
310
311           rc = fread(&b, 1, 1, fp);
312           m_bytesRead++;
313           if (rc!=1)
314           {
315                 delete [] colormap;
316                 fclose(fp);
317                 return NULL;
318           }
319
320           rc = fread(&g, 1, 1, fp);
321           m_bytesRead++;
322           if (rc!=1)
323           {
324                 delete [] colormap;
325                 fclose(fp);
326                 return NULL;
327           }
328
329           rc = fread(&r, 1, 1, fp);
330           m_bytesRead++;
331           if (rc != 1)
332           {
333                 delete [] colormap;
334                 fclose(fp);
335                 return NULL;
336           }
337
338           rc = fread(&dummy, 1, 1, fp);
339           m_bytesRead++;
340           if (rc != 1)
341           {
342                 delete [] colormap;
343                 fclose(fp);
344                 return NULL;
345           }
346
347           colormap[i].rgbRed=r;
348           colormap[i].rgbGreen=g;
349           colormap[i].rgbBlue=b;
350     }
351   }
352
353   if ((long)m_bytesRead > pixoff)
354   {
355         delete [] colormap;
356         fclose(fp);
357         return NULL;
358   }
359
360   while ((long)m_bytesRead < pixoff)
361   {
362         char dummy;
363         fread(&dummy,1,1,fp);
364         m_bytesRead++;
365   }
366
367   int w = bmWidth;
368   int h = bmHeight;
369
370   // set the output params
371   imagebits = (unsigned char *)malloc(w * h * 3);
372   long row_size = w * 3;
373
374   if (imagebits != NULL)
375   {
376           *width = w;
377           *height = h;
378           unsigned char *outbuf = imagebits;
379           long row = 0;
380           long rowOffset = 0;
381
382           if (compression == 0) // BI_RGB
383     {
384             // read rows in reverse order
385             for (row = bmHeight - 1; row >= 0; row--)
386             {
387                     // which row are we working on?
388                     rowOffset = (long unsigned)row * row_size;
389
390                     if (bmBitsPixel == 24)
391                     {
392                       for (int col=0;col<w;col++)
393                       {
394                             long offset = col * 3;
395                             char pixel[3];
396
397                             if (fread((void *)(pixel),1,3,fp) == 3)
398                             {
399                               // we swap red and blue here
400                               *(outbuf + rowOffset + offset + 0) = pixel[2];  // r
401                               *(outbuf + rowOffset + offset + 1) = pixel[1];  // g
402                               *(outbuf + rowOffset + offset + 2) = pixel[0];  // b
403                             }
404               }
405                       m_bytesRead += row_size;
406
407                       // read DWORD padding
408                       while ((m_bytesRead - pixoff) & 3)
409                       {
410                               char dummy;
411                               if (fread(&dummy,1,1,fp) != 1)
412             {
413                           free(imagebits);
414                           fclose(fp);
415                           return NULL;
416             }
417                               m_bytesRead++;
418           }
419         }
420         else
421         {
422                       // pixels are packed as 1 , 4 or 8 bit vals. need to unpack them
423                       int bit_count = 0;
424                       unsigned long mask = (1 << bmBitsPixel) - 1;
425                       unsigned char inbyte = 0;
426
427                       for (int col = 0; col < w; col++)
428           {
429                         int pix = 0;
430
431                         // if we need another byte
432                         if (bit_count <= 0)
433                               {
434                                 bit_count = 8;
435                                 if (fread(&inbyte,1,1,fp) != 1)
436                                 {
437                                         free(imagebits);
438                                         delete [] colormap;
439                                         fclose(fp);
440                                         return NULL;
441                                 }
442                                 m_bytesRead++;
443                               }
444
445                               // keep track of where we are in the bytes
446                               bit_count -= bmBitsPixel;
447                               pix = ( inbyte >> bit_count) & mask;
448
449                               // lookup the color from the colormap - stuff it in our buffer
450                               // swap red and blue
451                               *(outbuf + rowOffset + col * 3 + 2) = colormap[pix].rgbBlue;
452                               *(outbuf + rowOffset + col * 3 + 1) = colormap[pix].rgbGreen;
453                               *(outbuf + rowOffset + col * 3 + 0) = colormap[pix].rgbRed;
454           }
455
456                       // read DWORD padding
457                       while ((m_bytesRead - pixoff) & 3)
458           {
459             char dummy;
460             if (fread(&dummy,1,1,fp)!=1)
461             {
462               free(imagebits);
463               if (colormap)
464                 delete [] colormap;
465               fclose(fp);
466               return NULL;
467             }
468             m_bytesRead++;
469           }
470         }
471       }
472     }
473           else
474           {
475             int i, x = 0;
476             unsigned char c, c1 = 0, *pp;
477             row = 0;
478             pp = outbuf + (bmHeight - 1) * bmWidth * 3;
479
480             if (bmBitsPixel == 8)
481             {
482                     while (row < bmHeight)
483                     {
484                       c = getc(fp);
485
486                       if (c)
487                       {
488                               // encoded mode
489                               c1 = getc(fp);
490                               for (i = 0; i < c; x++, i++)
491                               {
492                                 *pp = colormap[c1].rgbRed; pp++;
493                                 *pp = colormap[c1].rgbGreen; pp++;
494                                 *pp = colormap[c1].rgbBlue; pp++;
495                               }
496                       }
497                       else
498                       {
499                               // c==0x00,  escape codes
500                               c = getc(fp);
501                               if (c == 0x00) // end of line
502                               {
503                                 row++;
504                                 x = 0;
505                                 pp = outbuf + (bmHeight - row - 1) * bmWidth * 3;
506                               }
507                               else if (c == 0x01)
508                                 break; // end of pic
509                               else if (c == 0x02) // delta
510                               {
511                                 c = getc(fp);
512                                 x += c;
513                                 c = getc(fp);
514                                 row += c;
515                                 pp = outbuf + x*3 + (bmHeight - row - 1) * bmWidth * 3;
516                               }
517                               else // absolute mode
518                               {
519                                 for (i = 0; i < c; x++, i++)
520                                 {
521                                         c1 = getc(fp);
522                                         *pp = colormap[c1].rgbRed; pp++;
523                                         *pp = colormap[c1].rgbGreen; pp++;
524                                         *pp = colormap[c1].rgbBlue; pp++;
525                                 }
526
527                                 if (c & 1)
528                                       getc(fp); // odd length run: read an extra pad byte
529                               }
530                       }
531                     }
532             }
533             else if (bmBitsPixel == 4)
534             {
535                     while (row < bmHeight)
536                     {
537                       c = getc(fp);
538
539                       if (c)
540                       {
541                               // encoded mode
542                               c1 = getc(fp);
543                               for (i = 0; i < c; x++, i++)
544                               {
545                                 *pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbRed; pp++;
546                                 *pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbGreen; pp++;
547                                 *pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbBlue; pp++;
548                               }
549                       }
550                       else
551                       {
552                               // c==0x00,  escape codes
553                               c = getc(fp);
554
555                               if (c == 0x00) // end of line
556                               {
557                                 row++;
558                                 x = 0;
559                                 pp = outbuf + (bmHeight - row - 1) * bmWidth * 3;
560                               }
561                               else if (c == 0x01)
562                                 break; // end of pic
563                               else if (c == 0x02) // delta
564                               {
565                                 c = getc(fp);
566                                 x += c;
567                                 c = getc(fp);
568                                 row += c;
569                                 pp = outbuf + x * 3 + (bmHeight - row - 1) * bmWidth * 3;
570                               }
571                               else // absolute mode
572                               {
573                                 for (i = 0; i < c; x++, i++)
574                                 {
575                                         if ((i&1) == 0)
576                                           c1 = getc(fp);
577                                         *pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbRed; pp++;
578                                         *pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbGreen; pp++;
579                                         *pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbBlue; pp++;
580                                 }
581
582                                 if (((c & 3) == 1) || ((c & 3) == 2))
583                                       getc(fp); // odd length run: read an extra pad byte
584             }
585           }
586                     }
587             }
588           }
589           if (colormap)
590             delete [] colormap;
591
592           fclose(fp);
593   }
594   return imagebits;
595 }
596
597 void bmp_to_pixmap (const char* filename, GdkPixmap **pixmap, GdkBitmap **mask)
598 {
599   guint16 width, height;
600   unsigned char *buf;
601   GdkWindow *window = gdk_get_default_root_window();
602   GdkColormap *colormap;
603   GdkGC* gc = gdk_gc_new (window);
604   int i, j;
605   bool hasMask = false;
606
607   *pixmap = *mask = NULL;
608   buf = load_bitmap_file (filename, &width, &height);
609   if (!buf)
610     return;
611
612   colormap = gdk_drawable_get_colormap (window);
613   *pixmap = gdk_pixmap_new (window, width, height, -1);
614
615   typedef struct
616   {
617     GdkColor c;
618     unsigned char *p;
619   } PAL;
620
621   for (i = 0; i < height; i++)
622   {
623     for (j = 0; j < width; j++)
624     {
625       unsigned char *p = &buf[(i * width + j) * 3];
626       PAL pe;
627
628       pe.c.red = (gushort)(p[0] * 0xFF);
629       pe.c.green = (gushort)(p[1] * 0xFF);
630       pe.c.blue = (gushort)(p[2] * 0xFF);
631       gdk_colormap_alloc_color(colormap, &pe.c, FALSE, TRUE);
632       gdk_gc_set_foreground(gc, &pe.c);
633       gdk_draw_point(*pixmap, gc, j, i);
634
635       if (p[0] == 0xFF && p[1] == 0x00 && p[2] == 0xFF)
636         hasMask = true;
637     }
638   }
639
640   gdk_gc_unref (gc);
641   *mask = gdk_pixmap_new (window, width, height, 1);
642   gc = gdk_gc_new (*mask);
643   if (hasMask)
644   {
645     for (i = 0; i < height; i++)
646     {
647       for (j = 0; j < width; j++)
648       {
649               GdkColor mask_pattern;
650
651               // pink is transparent
652               if ((buf[(i*width+j)*3] == 0xff) &&
653                       (buf[(i*width+j)*3+1] == 0x00) &&
654                       (buf[(i*width+j)*3+2] == 0xff))
655                       mask_pattern.pixel = 0;
656         else
657                       mask_pattern.pixel = 1;
658
659         gdk_gc_set_foreground (gc, &mask_pattern);
660         // possible Win32 Gtk bug here
661         //gdk_draw_point (*mask, gc, j, i);
662         gdk_draw_line (*mask, gc, j, i, j + 1, i);
663       }
664     }
665   }
666   else
667   {
668     GdkColor mask_pattern;
669     mask_pattern.pixel = 1;
670     gdk_gc_set_foreground (gc, &mask_pattern);
671     gdk_draw_rectangle (*mask, gc, 1, 0, 0, width, height);
672   }
673   gdk_gc_unref(gc);
674   free (buf);
675 }
676
677 void load_pixmap (const char* filename, GtkWidget* widget, GdkPixmap **gdkpixmap, GdkBitmap **mask)
678 {
679   CString str;
680
681   str = g_strBitmapsPath;
682   str += filename;
683
684   bmp_to_pixmap (str.GetBuffer (), gdkpixmap, mask);
685   if (*gdkpixmap == NULL)
686   {
687     printf("gdkpixmap was null\n");
688     gchar *dummy[] = { "1 1 1 1", "  c None", " " };
689     printf("calling gdk_pixmap_create_from_xpm_d\n");
690     *gdkpixmap = gdk_pixmap_create_from_xpm_d (gdk_get_default_root_window(), mask, NULL, dummy);
691   }
692 }
693
694 // this is the same as above but used by the plugins
695 // GdkPixmap **gdkpixmap, GdkBitmap **mask
696 bool WINAPI load_plugin_bitmap (const char* filename, void **gdkpixmap, void **mask)
697 {
698   CString str;
699
700   str = g_strGameToolsPath;
701   str += g_strPluginsDir;
702   str += "bitmaps/";
703   str += filename;
704   bmp_to_pixmap (str.GetBuffer (), (GdkPixmap **)gdkpixmap, (GdkBitmap **)mask);
705
706   if (*gdkpixmap == NULL)
707   {
708     // look in the core plugins
709     str = g_strAppPath;
710     str += g_strPluginsDir;
711     str += "bitmaps/";
712     str += filename;
713     bmp_to_pixmap (str.GetBuffer (), (GdkPixmap **)gdkpixmap, (GdkBitmap **)mask);
714
715     if (*gdkpixmap == NULL)
716     {
717
718       // look in core modules
719       str = g_strAppPath;
720       str += g_strModulesDir;
721       str += "bitmaps/";
722       str += filename;
723       bmp_to_pixmap (str.GetBuffer (), (GdkPixmap **)gdkpixmap, (GdkBitmap **)mask);
724
725       if (*gdkpixmap == NULL)
726       {
727         gchar *dummy[] = { "1 1 1 1", "  c None", " " };
728         *gdkpixmap = gdk_pixmap_create_from_xpm_d (gdk_get_default_root_window(), (GdkBitmap **)mask, NULL, dummy);
729         return false;
730       }
731     }
732   }
733   return true;
734 }
735
736 // Load a xpm file and return a pixmap widget.
737 GtkWidget* new_pixmap (GtkWidget* widget, const char* filename)
738 {
739   GdkPixmap *gdkpixmap;
740   GdkBitmap *mask;
741   GtkWidget *pixmap;
742
743   load_pixmap (filename, widget, &gdkpixmap, &mask);
744   pixmap = gtk_pixmap_new (gdkpixmap, mask);
745
746   gdk_drawable_unref (gdkpixmap);
747   gdk_drawable_unref (mask);
748
749   return pixmap;
750 }
751
752 // =============================================================================
753 // Menu stuff
754
755 GtkWidget* menu_separator (GtkWidget *menu)
756 {
757   GtkWidget *menu_item = gtk_menu_item_new ();
758   gtk_menu_append (GTK_MENU (menu), menu_item);
759   gtk_widget_set_sensitive (menu_item, FALSE);
760   gtk_widget_show (menu_item);
761   return menu_item;
762 }
763
764 GtkWidget* menu_tearoff (GtkWidget *menu)
765 {
766   GtkWidget *menu_item = gtk_tearoff_menu_item_new ();
767   gtk_menu_append (GTK_MENU (menu), menu_item);
768 // gtk_widget_set_sensitive (menu_item, FALSE); -- controls whether menu is detachable
769   gtk_widget_show (menu_item);
770   return menu_item;
771 }
772
773 GtkWidget* create_sub_menu_with_mnemonic (GtkWidget *bar, const gchar *mnemonic)
774 {
775   GtkWidget *item, *sub_menu;
776
777   item = gtk_menu_item_new_with_mnemonic (mnemonic);
778   gtk_widget_show (item);
779   gtk_container_add (GTK_CONTAINER (bar), item);
780
781   sub_menu = gtk_menu_new ();
782   gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), sub_menu);
783
784   return sub_menu;
785 }
786
787 extern void AddMenuItem (GtkWidget* menu, unsigned int id);
788
789 GtkWidget* create_menu_item_with_mnemonic (GtkWidget *menu, const gchar *mnemonic, GtkSignalFunc func, int id)
790 {
791   GtkWidget *item;
792
793   item = gtk_menu_item_new_with_mnemonic (mnemonic);
794
795   gtk_widget_show (item);
796   gtk_container_add (GTK_CONTAINER (menu), item);
797   gtk_signal_connect (GTK_OBJECT (item), "activate", GTK_SIGNAL_FUNC (func), GINT_TO_POINTER (id));
798
799   AddMenuItem (item, id);
800   return item;
801 }
802
803 GtkWidget* create_check_menu_item_with_mnemonic (GtkWidget *menu, const gchar *mnemonic, GtkSignalFunc func, int id, gboolean active)
804 {
805   GtkWidget *item;
806
807   item = gtk_check_menu_item_new_with_mnemonic(mnemonic);
808
809   gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), active);
810   gtk_widget_show (item);
811   gtk_container_add (GTK_CONTAINER (menu), item);
812   gtk_signal_connect (GTK_OBJECT (item), "activate", GTK_SIGNAL_FUNC (func), GINT_TO_POINTER (id));
813
814   AddMenuItem (item, id);
815   return item;
816 }
817
818 GtkWidget* create_radio_menu_item_with_mnemonic (GtkWidget *menu, GtkWidget *last, const gchar *mnemonic, GtkSignalFunc func, int id, gboolean state)
819 {
820   GtkWidget *item;
821   GSList *group = (GSList*)NULL;
822
823   if (last != NULL)
824     group = gtk_radio_menu_item_group (GTK_RADIO_MENU_ITEM (last));
825   item = gtk_radio_menu_item_new_with_mnemonic (group, mnemonic);
826   gtk_check_menu_item_set_state (GTK_CHECK_MENU_ITEM (item), state);
827
828   gtk_widget_show (item);
829   gtk_container_add (GTK_CONTAINER (menu), item);
830   gtk_signal_connect (GTK_OBJECT (item), "activate", GTK_SIGNAL_FUNC (func), GINT_TO_POINTER (id));
831
832   AddMenuItem (item, id);
833   return item;
834 }
835
836 GtkWidget* create_menu_in_menu_with_mnemonic (GtkWidget *menu, const gchar *mnemonic)
837 {
838   GtkWidget *item, *submenu;
839
840   item = gtk_menu_item_new_with_mnemonic(mnemonic);
841   gtk_widget_show (item);
842   gtk_container_add (GTK_CONTAINER (menu), item);
843
844   submenu = gtk_menu_new ();
845   gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
846
847   return submenu;
848 }
849
850 // =============================================================================
851 // Message Boxes
852
853 void dialog_button_callback( GtkWidget *widget, gpointer data ) {
854   GtkWidget *parent;
855   int *loop, *ret;
856
857   parent = gtk_widget_get_toplevel( widget );
858   loop = (int*)g_object_get_data( G_OBJECT( parent ), "loop" );
859   ret = (int*)g_object_get_data( G_OBJECT( parent ), "ret" );
860
861   *loop = 0;
862   *ret = GPOINTER_TO_INT (data);
863 }
864
865 gint dialog_delete_callback (GtkWidget *widget, GdkEvent* event, gpointer data)
866 {
867   int *loop;
868
869   gtk_widget_hide (widget);
870   loop = (int*)g_object_get_data (G_OBJECT (widget), "loop");
871   *loop = 0;
872
873   return TRUE;
874 }
875
876 gint dialog_url_callback (GtkWidget *widget, GdkEvent* event, gpointer data)
877 {
878   OpenURL((const char *)g_object_get_data (G_OBJECT (widget), "URL"));
879
880   return TRUE;
881 }
882
883 int WINAPI gtk_MessageBox (void *parent, const char* lpText, const char* lpCaption, guint32 uType, const char* URL)
884 {
885   GtkWidget *window, *w, *vbox, *hbox;
886   GtkAccelGroup *accel;
887   int mode = (uType & MB_TYPEMASK), ret, loop = 1;
888
889   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
890   gtk_signal_connect (GTK_OBJECT (window), "delete_event",
891                       GTK_SIGNAL_FUNC (dialog_delete_callback), NULL);
892   gtk_signal_connect (GTK_OBJECT (window), "destroy",
893                       GTK_SIGNAL_FUNC (gtk_widget_destroy), NULL);
894   gtk_window_set_title (GTK_WINDOW (window), lpCaption);
895   gtk_container_border_width (GTK_CONTAINER (window), 10);
896   g_object_set_data (G_OBJECT (window), "loop", &loop);
897   g_object_set_data (G_OBJECT (window), "ret", &ret);
898   gtk_widget_realize (window);
899
900   gtk_window_set_policy(GTK_WINDOW (window),FALSE,FALSE,TRUE);
901
902   if (parent != NULL)
903     gtk_window_set_transient_for (GTK_WINDOW (window), GTK_WINDOW (parent));
904
905   accel = gtk_accel_group_new ();
906   gtk_window_add_accel_group (GTK_WINDOW (window), accel);
907
908   vbox = gtk_vbox_new (FALSE, 10);
909   gtk_container_add (GTK_CONTAINER (window), vbox);
910   gtk_widget_show (vbox);
911
912   w = gtk_label_new (lpText);
913   gtk_box_pack_start (GTK_BOX (vbox), w, FALSE, FALSE, 2);
914   gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_LEFT);
915   gtk_widget_show (w);
916
917   w = gtk_hseparator_new ();
918   gtk_box_pack_start (GTK_BOX (vbox), w, FALSE, FALSE, 2);
919   gtk_widget_show (w);
920
921   hbox = gtk_hbox_new (FALSE, 10);
922   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 2);
923   gtk_widget_show (hbox);
924
925   if (mode == MB_OK)
926   {
927     w = gtk_button_new_with_label (_("Ok"));
928     gtk_box_pack_start (GTK_BOX (hbox), w, TRUE, TRUE, 0);
929     gtk_signal_connect (GTK_OBJECT (w), "clicked",
930                         GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (IDOK));
931     gtk_widget_add_accelerator (w, "clicked", accel, GDK_Escape, (GdkModifierType)0, (GtkAccelFlags)0);
932     gtk_widget_add_accelerator (w, "clicked", accel, GDK_Return, (GdkModifierType)0, (GtkAccelFlags)0);
933     GTK_WIDGET_SET_FLAGS (w, GTK_CAN_DEFAULT);
934     gtk_widget_grab_default (w);
935     gtk_widget_show (w);
936     ret = IDOK;
937   }
938   else if (mode ==  MB_OKCANCEL)
939   {
940     w = gtk_button_new_with_label (_("Ok"));
941     gtk_box_pack_start (GTK_BOX (hbox), w, TRUE, TRUE, 0);
942     gtk_signal_connect (GTK_OBJECT (w), "clicked",
943                         GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (IDOK));
944     gtk_widget_add_accelerator (w, "clicked", accel, GDK_Return, (GdkModifierType)0, (GtkAccelFlags)0);
945     GTK_WIDGET_SET_FLAGS (w, GTK_CAN_DEFAULT);
946     gtk_widget_grab_default (w);
947     gtk_widget_show (w);
948
949     w = gtk_button_new_with_label (_("Cancel"));
950     gtk_box_pack_start (GTK_BOX (hbox), w, TRUE, TRUE, 0);
951     gtk_signal_connect (GTK_OBJECT (w), "clicked",
952                         GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (IDCANCEL));
953     gtk_widget_add_accelerator (w, "clicked", accel, GDK_Escape, (GdkModifierType)0, (GtkAccelFlags)0);
954     gtk_widget_show (w);
955     ret = IDCANCEL;
956   }
957   else if (mode == MB_YESNOCANCEL)
958   {
959     w = gtk_button_new_with_label (_("Yes"));
960     gtk_box_pack_start (GTK_BOX (hbox), w, TRUE, TRUE, 0);
961     gtk_signal_connect (GTK_OBJECT (w), "clicked",
962                         GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (IDYES));
963     GTK_WIDGET_SET_FLAGS (w, GTK_CAN_DEFAULT);
964     gtk_widget_grab_default (w);
965     gtk_widget_show (w);
966
967     w = gtk_button_new_with_label (_("No"));
968     gtk_box_pack_start (GTK_BOX (hbox), w, TRUE, TRUE, 0);
969     gtk_signal_connect (GTK_OBJECT (w), "clicked",
970                         GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (IDNO));
971     gtk_widget_show (w);
972
973     w = gtk_button_new_with_label (_("Cancel"));
974     gtk_box_pack_start (GTK_BOX (hbox), w, TRUE, TRUE, 0);
975     gtk_signal_connect (GTK_OBJECT (w), "clicked",
976                         GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (IDCANCEL));
977     gtk_widget_show (w);
978     ret = IDCANCEL;
979   }
980   else /* if (mode == MB_YESNO) */
981   {
982     w = gtk_button_new_with_label (_("Yes"));
983     gtk_box_pack_start (GTK_BOX (hbox), w, TRUE, TRUE, 0);
984     gtk_signal_connect (GTK_OBJECT (w), "clicked",
985                         GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (IDYES));
986     GTK_WIDGET_SET_FLAGS (w, GTK_CAN_DEFAULT);
987     gtk_widget_grab_default (w);
988     gtk_widget_show (w);
989
990     w = gtk_button_new_with_label (_("No"));
991     gtk_box_pack_start (GTK_BOX (hbox), w, TRUE, TRUE, 0);
992     gtk_signal_connect (GTK_OBJECT (w), "clicked",
993                         GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (IDNO));
994     gtk_widget_show (w);
995     ret = IDNO;
996   }
997
998   if (URL)
999   {
1000     w = gtk_button_new_with_label (_("Go to URL"));
1001     gtk_box_pack_start (GTK_BOX (hbox), w, TRUE, TRUE, 0);
1002     gtk_signal_connect (GTK_OBJECT (w), "clicked",
1003                         GTK_SIGNAL_FUNC (dialog_url_callback), NULL);
1004     g_object_set_data (G_OBJECT (w), "URL", (void *)URL);
1005     GTK_WIDGET_SET_FLAGS (w, GTK_CAN_DEFAULT);
1006     gtk_widget_grab_default (w);
1007     gtk_widget_show (w);
1008   }
1009
1010
1011   gtk_widget_show (window);
1012   gtk_grab_add (window);
1013
1014   while (loop)
1015     gtk_main_iteration ();
1016
1017   gtk_grab_remove (window);
1018   gtk_widget_destroy (window);
1019
1020   return ret;
1021 }
1022
1023 // =============================================================================
1024 // File dialog
1025
1026 // fenris #3078 WHENHELLISFROZENOVER
1027
1028 static void file_sel_callback (GtkWidget *widget, gpointer data)
1029 {
1030   GtkWidget *parent;
1031   int *loop;
1032   bool *success;
1033
1034   parent = gtk_widget_get_toplevel (widget);
1035   loop = (int*)g_object_get_data (G_OBJECT (parent), "loop");
1036   success = (bool*)g_object_get_data (G_OBJECT (parent), "success");
1037
1038   if (GPOINTER_TO_INT (data) == IDOK)
1039     *success = true;
1040
1041   *loop = 0;
1042 }
1043
1044 #ifdef _WIN32
1045 #include <commdlg.h>
1046 static OPENFILENAME ofn;       /* common dialog box structure   */
1047 static char szDirName[MAX_PATH];    /* directory string              */
1048 static char szFile[MAX_PATH];       /* filename string               */
1049 static char szFileTitle[MAX_PATH];  /* file title string             */
1050 static int i, cbString;        /* integer count variables       */
1051 static HANDLE hf;              /* file handle                   */
1052 #else
1053 static char szFile[QER_MAX_NAMELEN];
1054 #endif
1055
1056 #define FILEDLG_CUSTOM_FILTER_LENGTH 64
1057 // to be used with the advanced file selector
1058
1059 class CFileType : public IFileTypeList
1060 {
1061   struct filetype_copy_t
1062   {
1063     void operator=(const filetype_t& other)
1064     {
1065       m_name = other.name;
1066       m_pattern = other.pattern;
1067     }
1068     string_t m_name;
1069     string_t m_pattern;
1070   };
1071 public:
1072   CFileType()
1073   {
1074     m_nTypes = 0;
1075     m_pTypes = NULL;
1076     m_strWin32Filters = NULL;
1077     m_pstrGTKMasks = NULL;
1078   }
1079
1080   virtual ~CFileType()
1081   {
1082     delete[] m_pTypes;
1083     DestroyWin32Filters();
1084     DestroyGTKMasks();
1085   }
1086
1087   void addType(filetype_t type)
1088   {
1089     filetype_copy_t* newTypes = new filetype_copy_t [m_nTypes+1];
1090     if(m_nTypes > 0)
1091     {
1092       for(int i=0; i<m_nTypes; i++)
1093         newTypes[i] = m_pTypes[i];
1094       delete[] m_pTypes;
1095     }
1096     m_pTypes = newTypes;
1097     m_pTypes[m_nTypes] = type;
1098     m_nTypes++;
1099     ConstructGTKMasks();
1100     ConstructWin32Filters();
1101   }
1102
1103   filetype_t GetTypeForWin32Filter(const char *filter) const
1104   {
1105     for(int i=0; i<m_nTypes; i++)
1106       if(strcmp(m_pTypes[i].m_pattern.c_str(), filter)==0)
1107         return filetype_t(m_pTypes[i].m_name.c_str(), m_pTypes[i].m_pattern.c_str());
1108     return filetype_t();
1109   }
1110
1111   filetype_t GetTypeForGTKMask(const char *mask) const
1112   {
1113     for(int i=0; i<m_nTypes; i++)
1114       if(strcmp(m_pstrGTKMasks[i],mask)==0)
1115         return filetype_t(m_pTypes[i].m_name.c_str(), m_pTypes[i].m_pattern.c_str());
1116     return filetype_t();
1117   }
1118
1119   int GetNumTypes()
1120   {
1121     return m_nTypes;
1122   }
1123
1124   filetype_t GetTypeForIndex(int index) const // Zero-based index.
1125   {
1126     if (index >= 0 && index < m_nTypes)
1127       return filetype_t(m_pTypes[index].m_name.c_str(), m_pTypes[index].m_pattern.c_str());
1128     return filetype_t();
1129   }
1130
1131   char *m_strWin32Filters;
1132   char **m_pstrGTKMasks;
1133 private:
1134   int m_nTypes;
1135   filetype_copy_t *m_pTypes;
1136
1137   void DestroyWin32Filters()
1138   {
1139     delete[] m_strWin32Filters;
1140   }
1141
1142   void ConstructWin32Filters()
1143   {
1144     const char *r;
1145     char *w;
1146     int i;
1147     int len = 0;
1148     DestroyWin32Filters();
1149     for(i=0; i<m_nTypes; i++)
1150       len = len + strlen(m_pTypes[i].m_name.c_str()) + strlen(m_pTypes[i].m_pattern.c_str())*2 + 5;
1151     m_strWin32Filters = new char[len+1]; // length + null char
1152     for(i=0, w = m_strWin32Filters; i<m_nTypes; i++)
1153     {
1154       for(r = m_pTypes[i].m_name.c_str(); *r!='\0'; r++, w++)
1155         *w = *r;
1156       *w++ = ' ';
1157       *w++ = '(';
1158       for(r = m_pTypes[i].m_pattern.c_str(); *r!='\0'; r++, w++)
1159         *w = *r;
1160       *w++ = ')';
1161       *w++ = '\0';
1162       for(r = m_pTypes[i].m_pattern.c_str(); *r!='\0'; r++, w++)
1163         *w = (*r == ',') ? ';' : *r;
1164       *w++ = '\0';
1165     }
1166     m_strWin32Filters[len] = '\0';
1167   }
1168
1169   void DestroyGTKMasks()
1170   {
1171     if(m_pstrGTKMasks != NULL)
1172       for(char **p = m_pstrGTKMasks; *p != NULL; p++)
1173         delete[] *p;
1174     delete[] m_pstrGTKMasks;
1175   }
1176
1177   void ConstructGTKMasks()
1178   {
1179     const char *r;
1180     char *w;
1181     int i;
1182     int len = 0;
1183     DestroyGTKMasks();
1184     m_pstrGTKMasks = new char*[m_nTypes+1];
1185     for(i=0; i<m_nTypes; i++)
1186     {
1187       len = strlen(m_pTypes[i].m_name.c_str()) + strlen(m_pTypes[i].m_pattern.c_str()) + 3;
1188       m_pstrGTKMasks[i] = new char[len+1]; // length + null char
1189       w = m_pstrGTKMasks[i];
1190       for(r = m_pTypes[i].m_name.c_str(); *r!='\0'; r++, w++)
1191         *w = *r;
1192       *w++ = ' ';
1193       *w++ = '(';
1194       for(r = m_pTypes[i].m_pattern.c_str(); *r!='\0'; r++, w++)
1195         *w = *r;
1196       *w++ = ')';
1197       *w++ = '\0';
1198     }
1199     m_pstrGTKMasks[m_nTypes] = NULL;
1200   }
1201
1202 };
1203
1204 #ifdef _WIN32
1205
1206 typedef struct {
1207   gboolean open;
1208   OPENFILENAME *ofn;
1209   BOOL dlgRtnVal;
1210   bool done;
1211 } win32_native_file_dialog_comms_t;
1212
1213 DWORD WINAPI win32_native_file_dialog_thread_func(LPVOID lpParam)
1214 {
1215   win32_native_file_dialog_comms_t *fileDialogComms;
1216   fileDialogComms = (win32_native_file_dialog_comms_t *) lpParam;
1217   if (fileDialogComms->open) {
1218     fileDialogComms->dlgRtnVal = GetOpenFileName(fileDialogComms->ofn);
1219   }
1220   else {
1221     fileDialogComms->dlgRtnVal = GetSaveFileName(fileDialogComms->ofn);
1222   }
1223   fileDialogComms->done = true; // No need to synchronize around lock, one-way gate.
1224   return 0;
1225 }
1226
1227 #endif
1228
1229 /**
1230  * @param[in] baseSubDir should have a trailing slash if not @c NULL
1231  */
1232 const char* file_dialog (void *parent, gboolean open, const char* title, const char* path, const char* pattern, const char *baseSubDir)
1233 {
1234
1235 #ifdef _WIN32
1236   static bool in_file_dialog = false;
1237   HANDLE fileDialogThreadHandle;
1238   win32_native_file_dialog_comms_t fileDialogComms;
1239   bool dialogDone;
1240 #endif
1241
1242   // Gtk dialog
1243   GtkWidget* file_sel;
1244   char *new_path = NULL;
1245
1246   const char* r;
1247   char *v, *w;
1248   filetype_t type;
1249   CFileType typelist;
1250   if(pattern != NULL)
1251     GetFileTypeRegistry()->getTypeList(pattern, &typelist);
1252
1253 #ifdef _WIN32
1254   if (g_PrefsDlg.m_bNativeGUI)
1255   {
1256     // do that the native way
1257
1258     if (in_file_dialog) return NULL; // Avoid recursive entry.
1259     in_file_dialog = true;
1260     /* Set the members of the OPENFILENAME structure. */
1261     // See http://msdn.microsoft.com/en-us/library/ms646839%28v=vs.85%29.aspx .
1262     memset(&ofn, 0, sizeof(ofn));
1263     ofn.lStructSize = sizeof(ofn);
1264     ofn.hwndOwner = (HWND)GDK_WINDOW_HWND (g_pParentWnd->m_pWidget->window);
1265     ofn.nFilterIndex = 1; // The index is 1-based, not 0-based.  This basically says,
1266                           // "select the first filter as default".
1267     if (pattern)
1268     {
1269       ofn.lpstrFilter = typelist.m_strWin32Filters;
1270     }
1271     else
1272     {
1273       // TODO: Would be a bit cleaner if we could extract this string from
1274       // GetFileTypeRegistry() instead of hardcoding it here.
1275       ofn.lpstrFilter = "all files\0*.*\0"; // Second '\0' will be added to end of string.
1276     }
1277     szFile[0] = '\0';
1278     ofn.lpstrFile = szFile;
1279     ofn.nMaxFile = sizeof(szFile);
1280     if(path)
1281     {
1282       // szDirName: Radiant uses unix convention for paths internally
1283       //   Win32 (of course) and Gtk (who would have thought) expect the '\\' convention
1284       // copy path, replacing dir separators as appropriate
1285       for(r=path, w=szDirName; *r!='\0'; r++)
1286         *w++ = (*r=='/') ? '\\' : *r;
1287       // terminate string
1288       *w = '\0';
1289       ofn.lpstrInitialDir = szDirName;
1290     }
1291     ofn.lpstrTitle = title;
1292     ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
1293
1294     memset(&fileDialogComms, 0, sizeof(fileDialogComms));
1295     fileDialogComms.open = open;
1296     fileDialogComms.ofn = &ofn;
1297
1298     fileDialogThreadHandle =
1299       CreateThread(NULL, // lpThreadAttributes
1300                    0, // dwStackSize, default stack size
1301                    win32_native_file_dialog_thread_func, // lpStartAddress, funcion to call
1302                    &fileDialogComms, // lpParameter, argument to pass to function
1303                    0, // dwCreationFlags
1304                    NULL); // lpThreadId
1305
1306     dialogDone = false;
1307     while (1) {
1308       // Avoid blocking indefinitely.  Another thread will set fileDialogComms->done to true;
1309       // we don't want to be in an indefinite blocked state when this happens.  We want to break
1310       // out of here eventually.
1311       while (gtk_events_pending()) {
1312         gtk_main_iteration();
1313       }
1314       if (dialogDone) break;
1315       if (fileDialogComms.done) dialogDone = true; // One more loop of gtk_main_iteration() to get things in sync.
1316       // Avoid tight infinte loop, add a small amount of sleep.
1317       Sleep(10);
1318     }
1319     // Make absolutely sure that the thread is finished before we call CloseHandle().
1320     WaitForSingleObject(fileDialogThreadHandle, INFINITE);
1321     CloseHandle(fileDialogThreadHandle);
1322
1323     in_file_dialog = false;
1324     
1325     if (!fileDialogComms.dlgRtnVal) {
1326       return NULL; // Cancelled.
1327     }
1328
1329     if(pattern != NULL)
1330       type = typelist.GetTypeForIndex(ofn.nFilterIndex - 1);
1331
1332   }
1333   else
1334   {
1335 #endif
1336         char buf[PATH_MAX];
1337     // do that the Gtk way
1338     if (title == NULL)
1339       title = open ? _("Open File") : _("Save File");
1340
1341     // we expect an actual path below, if the path is NULL we might crash
1342     if (!path || path[0] == '\0')
1343     {
1344                 strcpy(buf, g_pGameDescription->mEnginePath.GetBuffer());
1345                 strcat(buf, g_pGameDescription->mBaseGame.GetBuffer());
1346                 strcat(buf, "/");
1347                 if (baseSubDir)
1348                         strcat(buf, baseSubDir);
1349                 path = buf;
1350         }
1351
1352     // alloc new path with extra char for dir separator
1353     new_path = new char[strlen(path)+1+1];
1354     // copy path, replacing dir separators as appropriate
1355     for(r=path, w=new_path; *r!='\0'; r++)
1356       *w++ = (*r=='/') ? G_DIR_SEPARATOR : *r;
1357     // add dir separator to end of path if required
1358     if(*(w-1) != G_DIR_SEPARATOR) *w++ = G_DIR_SEPARATOR;
1359     // terminate string
1360     *w = '\0';
1361
1362     file_sel = gtk_file_chooser_dialog_new(title,
1363                                            GTK_WINDOW(parent),
1364                                            open ? GTK_FILE_CHOOSER_ACTION_OPEN : GTK_FILE_CHOOSER_ACTION_SAVE,
1365                                            GTK_STOCK_CANCEL,
1366                                            GTK_RESPONSE_CANCEL,
1367                                            open ? GTK_STOCK_OPEN : GTK_STOCK_SAVE,
1368                                            GTK_RESPONSE_ACCEPT,
1369                                            NULL);
1370     gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(file_sel), new_path);
1371     delete[] new_path;
1372
1373     // Setting the file chooser dialog to modal and centering it on the parent is done automatically.
1374
1375     if (pattern != NULL) {
1376       for (int i = 0; i < typelist.GetNumTypes(); i++) {
1377         GtkFileFilter *filter = gtk_file_filter_new();
1378         type = typelist.GetTypeForIndex(i);
1379         // We can use type.name here, or m_pstrGTKMasks[i], which includes the actual pattern.
1380         gtk_file_filter_set_name(filter, typelist.m_pstrGTKMasks[i]);
1381         gtk_file_filter_add_pattern(filter, type.pattern);
1382         // "Note that the chooser takes ownership of the filter, so
1383         // you have to ref and sink it if you want to keep a reference."
1384         // So I guess we won't need to garbage collect this.
1385         gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_sel), filter);
1386       }
1387     }
1388
1389     if (gtk_dialog_run(GTK_DIALOG(file_sel)) == GTK_RESPONSE_ACCEPT) {
1390       strcpy(szFile, gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(file_sel)));
1391     }
1392     else {
1393       szFile[0] = '\0';
1394     }
1395
1396     if (pattern != NULL) {
1397       GtkFileFilter *filter = gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(file_sel));
1398       if (filter == NULL) {
1399         type = filetype_t();
1400       }
1401       else {
1402         type = typelist.GetTypeForGTKMask(gtk_file_filter_get_name(filter));
1403       }
1404     }
1405     gtk_widget_destroy(file_sel);
1406
1407 #ifdef _WIN32
1408   }
1409 #endif
1410
1411   // don't return an empty filename
1412   if(szFile[0] == '\0') return NULL;
1413
1414   // convert back to unix format
1415   for(w=szFile; *w!='\0'; w++)
1416     if(*w=='\\')
1417       *w = '/';
1418
1419   /* \todo SPoG - file_dialog should return filetype information separately.. not force file extension.. */
1420   if(!open && pattern != NULL) {
1421     v = strrchr(szFile, '/');
1422     w = strrchr(szFile, '.');
1423     if ((v && w && w < v) || // Last '.' is before the file.
1424         w == NULL) { // Extension missing.
1425       if (type.pattern[0]) {
1426         w = szFile + strlen(szFile);
1427         strcpy(w, type.pattern + 1); // Add extension of selected filter type.
1428       }
1429       else {
1430         // type will be empty if for example there were no filters for pattern,
1431         // or if some other UI inconsistencies happen.
1432         if (gtk_MessageBox(parent, "No file extension specified in file to be saved.\nAttempt to save anyways?",
1433                            "GtkRadiant", MB_YESNO) == IDNO) {
1434           return NULL;
1435         }
1436       }
1437     }
1438     else { // An extension was explicitly in the filename.
1439       bool knownExtension = false;
1440       for (int i = typelist.GetNumTypes() - 1; i >= 0; i--) {
1441         type = typelist.GetTypeForIndex(i);
1442         if (type.pattern[0] && strcmp(w, type.pattern + 1) == 0) {
1443           knownExtension = true;
1444           break;
1445         }
1446       }
1447       if (!knownExtension) {
1448         if (gtk_MessageBox(parent, "Unknown file extension for this save operation.\nAttempt to save anyways?",
1449                            "GtkRadiant", MB_YESNO) == IDNO) {
1450           return NULL;
1451         }
1452       }
1453     }
1454   }
1455
1456   // prompt to overwrite existing files
1457   if (!open)
1458     if (access (szFile, R_OK) == 0)
1459       if (gtk_MessageBox (parent, "File already exists.\nOverwrite?", "GtkRadiant", MB_YESNO) == IDNO)
1460         return NULL;
1461
1462   return szFile;
1463 }
1464
1465 char* WINAPI dir_dialog (void *parent, const char* title, const char* path)
1466 {
1467   GtkWidget* file_sel;
1468   char* filename = (char*)NULL;
1469   int loop = 1;
1470   bool success = false;
1471
1472   file_sel = gtk_file_selection_new (title);
1473   gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (file_sel)->ok_button), "clicked",
1474                       GTK_SIGNAL_FUNC (file_sel_callback), GINT_TO_POINTER (IDOK));
1475   gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (file_sel)->cancel_button), "clicked",
1476                       GTK_SIGNAL_FUNC (file_sel_callback), GINT_TO_POINTER (IDCANCEL));
1477   gtk_signal_connect (GTK_OBJECT (file_sel), "delete_event",
1478                       GTK_SIGNAL_FUNC (dialog_delete_callback), NULL);
1479   gtk_file_selection_hide_fileop_buttons (GTK_FILE_SELECTION (file_sel));
1480
1481         if ( parent != NULL ) {
1482                 gtk_window_set_transient_for( GTK_WINDOW( file_sel ), GTK_WINDOW( parent ) );
1483         }
1484
1485   gtk_widget_hide (GTK_FILE_SELECTION (file_sel)->file_list->parent);
1486
1487   g_object_set_data (G_OBJECT (file_sel), "loop", &loop);
1488   g_object_set_data (G_OBJECT (file_sel), "success", &success);
1489
1490   if (path != NULL)
1491     gtk_file_selection_set_filename (GTK_FILE_SELECTION (file_sel), path);
1492
1493   gtk_grab_add (file_sel);
1494   gtk_widget_show (file_sel);
1495
1496   while (loop)
1497     gtk_main_iteration ();
1498
1499   filename = g_strdup( gtk_file_selection_get_filename( GTK_FILE_SELECTION( file_sel ) ) );
1500
1501   gtk_grab_remove( file_sel );
1502   gtk_widget_destroy( file_sel );
1503
1504   return filename;
1505 }
1506
1507 bool WINAPI color_dialog (void *parent, float *color, const char* title)
1508 {
1509   GtkWidget* dlg;
1510   double clr[3];
1511   int loop = 1, ret = IDCANCEL;
1512
1513   clr[0] = color[0];
1514   clr[1] = color[1];
1515   clr[2] = color[2];
1516
1517   dlg = gtk_color_selection_dialog_new (title);
1518   gtk_color_selection_set_color (GTK_COLOR_SELECTION (GTK_COLOR_SELECTION_DIALOG (dlg)->colorsel), clr);
1519   gtk_signal_connect (GTK_OBJECT (dlg), "delete_event",
1520                       GTK_SIGNAL_FUNC (dialog_delete_callback), NULL);
1521   gtk_signal_connect (GTK_OBJECT (dlg), "destroy",
1522                       GTK_SIGNAL_FUNC (gtk_widget_destroy), NULL);
1523   gtk_signal_connect (GTK_OBJECT (GTK_COLOR_SELECTION_DIALOG (dlg)->ok_button), "clicked",
1524                       GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (IDOK));
1525   gtk_signal_connect (GTK_OBJECT (GTK_COLOR_SELECTION_DIALOG (dlg)->cancel_button), "clicked",
1526                       GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (IDCANCEL));
1527   g_object_set_data (G_OBJECT (dlg), "loop", &loop);
1528   g_object_set_data (G_OBJECT (dlg), "ret", &ret);
1529
1530   if (parent != NULL)
1531     gtk_window_set_transient_for (GTK_WINDOW (dlg), GTK_WINDOW (parent));
1532
1533   gtk_widget_show (dlg);
1534   gtk_grab_add (dlg);
1535
1536   while (loop)
1537     gtk_main_iteration ();
1538
1539   GdkColor gdkcolor;
1540   gtk_color_selection_get_current_color (GTK_COLOR_SELECTION (GTK_COLOR_SELECTION_DIALOG (dlg)->colorsel), &gdkcolor);
1541   clr[0] = gdkcolor.red / 65535.0;
1542   clr[1] = gdkcolor.green / 65535.0;
1543   clr[2] = gdkcolor.blue / 65535.0;
1544
1545   gtk_grab_remove (dlg);
1546   gtk_widget_destroy (dlg);
1547
1548   if (ret == IDOK)
1549   {
1550     color[0] = (float)clr[0];
1551     color[1] = (float)clr[1];
1552     color[2] = (float)clr[2];
1553
1554     return true;
1555   }
1556
1557   return false;
1558 }
1559
1560 void OpenURL(const char *url)
1561 {
1562   // let's put a little comment
1563   Sys_Printf("OpenURL: %s\n", url);
1564 #ifdef __linux__
1565   // \todo FIXME: the way we open URLs on *nix should be improved. A script is good (see how I do on RTCW)
1566   char command[2*PATH_MAX];
1567   snprintf( command, sizeof(command), "%s/openurl.sh \"%s\" &", g_strAppPath.GetBuffer(), url );
1568   if (system (command) != 0)
1569     gtk_MessageBox (g_pParentWnd->m_pWidget, "Failed to launch Netscape!");
1570 #endif
1571 #ifdef __APPLE__
1572   char command[2*PATH_MAX];
1573   snprintf (command, sizeof(command),
1574             "open \"%s\" &", url, url);
1575   if (system (command) != 0)
1576     gtk_MessageBox (g_pParentWnd->m_pWidget, "Unable to launch browser!");
1577 #endif
1578 #ifdef _WIN32
1579   ShellExecute( (HWND)GDK_WINDOW_HWND (g_pParentWnd->m_pWidget->window), "open", url, NULL, NULL, SW_SHOW );
1580 #endif
1581 }
1582
1583 void CheckMenuSplitting (GtkWidget *&menu)
1584 {
1585   GtkWidget *item,*menu2;
1586
1587   GtkRequisition requisition;
1588   gint screen_height;
1589
1590   gtk_widget_size_request (GTK_WIDGET (menu), &requisition);
1591   screen_height = gdk_screen_height ();
1592
1593   if ((screen_height - requisition.height) < 20)
1594   {
1595     menu2 = gtk_menu_new ();
1596
1597     // move the last 2 items to a submenu (3 because of win32)
1598     for (int i = 0; i < 3; i++)
1599     {
1600       item = GTK_WIDGET (g_list_last (gtk_container_children (GTK_CONTAINER (menu)))->data);
1601       gtk_widget_ref (item);
1602       gtk_container_remove (GTK_CONTAINER (menu), item);
1603       gtk_menu_append (GTK_MENU (menu2), item);
1604       gtk_widget_unref (item);
1605     }
1606
1607     item = gtk_menu_item_new_with_label ("--------");
1608     gtk_widget_show (item);
1609     gtk_container_add (GTK_CONTAINER (menu), item);
1610     gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu2);
1611     menu = menu2;
1612   }
1613 }