2 Copyright (c) 2001, Loki software, inc.
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
8 Redistributions of source code must retain the above copyright notice, this list
9 of conditions and the following disclaimer.
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.
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
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.
32 // Small functions to help with GTK
35 #include <gdk/gdkkeysyms.h>
37 #if defined (__linux__) || defined (__APPLE__)
44 #include <gdk/gdkwin32.h>
45 #define WIN32_LEAN_AND_MEAN
58 // =============================================================================
61 // NOTE TTimo window position saving has always been tricky
62 // it doesn't work the same between win32 and linux .. see below that code is fairly different
63 // it's also very poorly done, the save calls are a bit randomly disctributed in the OnDestroy
65 void save_window_pos (GtkWidget *wnd, window_position_t& pos)
67 if ((wnd == NULL) || (wnd->window == NULL))
70 get_window_pos(wnd, &pos.x, &pos.y);
72 pos.w = wnd->allocation.width;
73 pos.h = wnd->allocation.height;
76 //Sys_Printf("save_window_pos 'Window %s'\n",buf);
81 void win32_get_window_pos(GtkWidget *widget, gint *x, gint *y)
83 // http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=913
84 if ( g_PrefsDlg.m_bStartOnPrimMon ) {
87 HWND xwnd = (HWND)GDK_WINDOW_HWND (widget->window);
88 const GdkRectangle primaryMonitorRect = g_pParentWnd->GetPrimaryMonitorRect();
90 GetClientRect(xwnd,&rc);
93 ClientToScreen(xwnd,&point);
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);
103 // this is the same as the unix version of get_window_pos
104 gdk_window_get_root_origin (widget->window, x, y);
107 Sys_Printf("win32_get_window_pos %p %d,%d\n",widget,*x,*y);
112 void load_window_pos (GtkWidget *wnd, window_position_t& pos)
115 const GdkRectangle primaryMonitorRect = g_pParentWnd->GetPrimaryMonitorRect();
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);
123 // FIXME: not multihead safe
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);
131 gtk_window_move(GTK_WINDOW(wnd), pos.x, pos.y);
133 gtk_window_set_default_size (GTK_WINDOW (wnd), pos.w, pos.h);
135 Sys_Printf("load_window_pos %p 'Window,%s'\n",wnd,windowData);
139 gint widget_delete_hide (GtkWidget *widget)
141 gtk_widget_hide (widget);
147 // Thanks to Mercury, Fingolfin - ETG
148 int readLongLE(FILE *file, unsigned long *m_bytesRead, int *value)
151 int len = fread(buf, 4, 1, file);
156 *value = buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24;
160 short readShortLE(FILE *file, unsigned long *m_bytesRead, short unsigned *value)
163 int len = fread(buf, 2, 1, file);
168 *value = buf[0] | buf[1] << 8;
172 unsigned char *load_bitmap_file (const char* filename, guint16 *width, guint16 *height)
174 int bmWidth, bmHeight;
175 short unsigned bmPlanes, bmBitsPixel;
177 unsigned char rgbBlue;
178 unsigned char rgbGreen;
179 unsigned char rgbRed;
180 unsigned char rgbReserved;
184 short unsigned res1,res2;
185 int filesize, pixoff;
186 int bmisize, compression;
189 unsigned long m_bytesRead = 0;
190 unsigned char *imagebits = NULL;
193 *width = *height = 0;
195 fp = fopen(filename,"rb");
202 rc = fread(&m1, 1, 1, fp);
210 rc = fread(&m2, 1, 1, fp);
212 if ((m1!='B') || (m2!='M'))
218 if (readLongLE(fp,&m_bytesRead,&filesize)) {
223 if (readShortLE(fp,&m_bytesRead,&res1)) {
228 if (readShortLE(fp,&m_bytesRead,&res2)) {
233 if (readLongLE(fp,&m_bytesRead,&pixoff)) {
238 if (readLongLE(fp,&m_bytesRead,&bmisize)) {
243 if (readLongLE(fp,&m_bytesRead,&bmWidth)) {
248 if (readLongLE(fp,&m_bytesRead,&bmHeight)) {
253 if (readShortLE(fp,&m_bytesRead,&bmPlanes)) {
258 if (readShortLE(fp,&m_bytesRead,&bmBitsPixel)) {
263 if (readLongLE(fp,&m_bytesRead,&compression)) {
268 if (readLongLE(fp,&m_bytesRead,&sizeimage)) {
273 if (readLongLE(fp,&m_bytesRead,&xscale)) {
278 if (readLongLE(fp,&m_bytesRead,&yscale)) {
283 if (readLongLE(fp,&m_bytesRead,&colors)) {
288 if (readLongLE(fp,&m_bytesRead,&impcol)) {
294 colors = 1 << bmBitsPixel;
296 RGBQUAD *colormap = NULL;
297 if (bmBitsPixel != 24)
299 colormap = new RGBQUAD[colors];
300 if (colormap == NULL)
307 for (i = 0; i < colors; i++)
309 unsigned char r ,g, b, dummy;
311 rc = fread(&b, 1, 1, fp);
320 rc = fread(&g, 1, 1, fp);
329 rc = fread(&r, 1, 1, fp);
338 rc = fread(&dummy, 1, 1, fp);
347 colormap[i].rgbRed=r;
348 colormap[i].rgbGreen=g;
349 colormap[i].rgbBlue=b;
353 if ((long)m_bytesRead > pixoff)
360 while ((long)m_bytesRead < pixoff)
363 fread(&dummy,1,1,fp);
370 // set the output params
371 imagebits = (unsigned char *)malloc(w * h * 3);
372 long row_size = w * 3;
374 if (imagebits != NULL)
378 unsigned char *outbuf = imagebits;
382 if (compression == 0) // BI_RGB
384 // read rows in reverse order
385 for (row = bmHeight - 1; row >= 0; row--)
387 // which row are we working on?
388 rowOffset = (long unsigned)row * row_size;
390 if (bmBitsPixel == 24)
392 for (int col=0;col<w;col++)
394 long offset = col * 3;
397 if (fread((void *)(pixel),1,3,fp) == 3)
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
405 m_bytesRead += row_size;
407 // read DWORD padding
408 while ((m_bytesRead - pixoff) & 3)
411 if (fread(&dummy,1,1,fp) != 1)
422 // pixels are packed as 1 , 4 or 8 bit vals. need to unpack them
424 unsigned long mask = (1 << bmBitsPixel) - 1;
425 unsigned char inbyte = 0;
427 for (int col = 0; col < w; col++)
431 // if we need another byte
435 if (fread(&inbyte,1,1,fp) != 1)
445 // keep track of where we are in the bytes
446 bit_count -= bmBitsPixel;
447 pix = ( inbyte >> bit_count) & mask;
449 // lookup the color from the colormap - stuff it in our buffer
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;
456 // read DWORD padding
457 while ((m_bytesRead - pixoff) & 3)
460 if (fread(&dummy,1,1,fp)!=1)
476 unsigned char c, c1 = 0, *pp;
478 pp = outbuf + (bmHeight - 1) * bmWidth * 3;
480 if (bmBitsPixel == 8)
482 while (row < bmHeight)
490 for (i = 0; i < c; x++, i++)
492 *pp = colormap[c1].rgbRed; pp++;
493 *pp = colormap[c1].rgbGreen; pp++;
494 *pp = colormap[c1].rgbBlue; pp++;
499 // c==0x00, escape codes
501 if (c == 0x00) // end of line
505 pp = outbuf + (bmHeight - row - 1) * bmWidth * 3;
509 else if (c == 0x02) // delta
515 pp = outbuf + x*3 + (bmHeight - row - 1) * bmWidth * 3;
517 else // absolute mode
519 for (i = 0; i < c; x++, i++)
522 *pp = colormap[c1].rgbRed; pp++;
523 *pp = colormap[c1].rgbGreen; pp++;
524 *pp = colormap[c1].rgbBlue; pp++;
528 getc(fp); // odd length run: read an extra pad byte
533 else if (bmBitsPixel == 4)
535 while (row < bmHeight)
543 for (i = 0; i < c; x++, i++)
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++;
552 // c==0x00, escape codes
555 if (c == 0x00) // end of line
559 pp = outbuf + (bmHeight - row - 1) * bmWidth * 3;
563 else if (c == 0x02) // delta
569 pp = outbuf + x * 3 + (bmHeight - row - 1) * bmWidth * 3;
571 else // absolute mode
573 for (i = 0; i < c; x++, i++)
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++;
582 if (((c & 3) == 1) || ((c & 3) == 2))
583 getc(fp); // odd length run: read an extra pad byte
597 void bmp_to_pixmap (const char* filename, GdkPixmap **pixmap, GdkBitmap **mask)
599 guint16 width, height;
601 GdkWindow *window = gdk_get_default_root_window();
602 GdkColormap *colormap;
603 GdkGC* gc = gdk_gc_new (window);
605 bool hasMask = false;
607 *pixmap = *mask = NULL;
608 buf = load_bitmap_file (filename, &width, &height);
612 colormap = gdk_drawable_get_colormap (window);
613 *pixmap = gdk_pixmap_new (window, width, height, -1);
621 for (i = 0; i < height; i++)
623 for (j = 0; j < width; j++)
625 unsigned char *p = &buf[(i * width + j) * 3];
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);
635 if (p[0] == 0xFF && p[1] == 0x00 && p[2] == 0xFF)
641 *mask = gdk_pixmap_new (window, width, height, 1);
642 gc = gdk_gc_new (*mask);
645 for (i = 0; i < height; i++)
647 for (j = 0; j < width; j++)
649 GdkColor mask_pattern;
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;
657 mask_pattern.pixel = 1;
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);
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);
677 void load_pixmap (const char* filename, GtkWidget* widget, GdkPixmap **gdkpixmap, GdkBitmap **mask)
681 str = g_strBitmapsPath;
684 bmp_to_pixmap (str.GetBuffer (), gdkpixmap, mask);
685 if (*gdkpixmap == NULL)
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);
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)
700 str = g_strGameToolsPath;
701 str += g_strPluginsDir;
704 bmp_to_pixmap (str.GetBuffer (), (GdkPixmap **)gdkpixmap, (GdkBitmap **)mask);
706 if (*gdkpixmap == NULL)
708 // look in the core plugins
710 str += g_strPluginsDir;
713 bmp_to_pixmap (str.GetBuffer (), (GdkPixmap **)gdkpixmap, (GdkBitmap **)mask);
715 if (*gdkpixmap == NULL)
718 // look in core modules
720 str += g_strModulesDir;
723 bmp_to_pixmap (str.GetBuffer (), (GdkPixmap **)gdkpixmap, (GdkBitmap **)mask);
725 if (*gdkpixmap == NULL)
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);
736 // Load a xpm file and return a pixmap widget.
737 GtkWidget* new_pixmap (GtkWidget* widget, const char* filename)
739 GdkPixmap *gdkpixmap;
743 load_pixmap (filename, widget, &gdkpixmap, &mask);
744 pixmap = gtk_pixmap_new (gdkpixmap, mask);
746 gdk_drawable_unref (gdkpixmap);
747 gdk_drawable_unref (mask);
752 // =============================================================================
755 GtkWidget* menu_separator (GtkWidget *menu)
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);
764 GtkWidget* menu_tearoff (GtkWidget *menu)
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);
773 GtkWidget* create_sub_menu_with_mnemonic (GtkWidget *bar, const gchar *mnemonic)
775 GtkWidget *item, *sub_menu;
777 item = gtk_menu_item_new_with_mnemonic (mnemonic);
778 gtk_widget_show (item);
779 gtk_container_add (GTK_CONTAINER (bar), item);
781 sub_menu = gtk_menu_new ();
782 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), sub_menu);
787 extern void AddMenuItem (GtkWidget* menu, unsigned int id);
789 GtkWidget* create_menu_item_with_mnemonic (GtkWidget *menu, const gchar *mnemonic, GtkSignalFunc func, int id)
793 item = gtk_menu_item_new_with_mnemonic (mnemonic);
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));
799 AddMenuItem (item, id);
803 GtkWidget* create_check_menu_item_with_mnemonic (GtkWidget *menu, const gchar *mnemonic, GtkSignalFunc func, int id, gboolean active)
807 item = gtk_check_menu_item_new_with_mnemonic(mnemonic);
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));
814 AddMenuItem (item, id);
818 GtkWidget* create_radio_menu_item_with_mnemonic (GtkWidget *menu, GtkWidget *last, const gchar *mnemonic, GtkSignalFunc func, int id, gboolean state)
821 GSList *group = (GSList*)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);
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));
832 AddMenuItem (item, id);
836 GtkWidget* create_menu_in_menu_with_mnemonic (GtkWidget *menu, const gchar *mnemonic)
838 GtkWidget *item, *submenu;
840 item = gtk_menu_item_new_with_mnemonic(mnemonic);
841 gtk_widget_show (item);
842 gtk_container_add (GTK_CONTAINER (menu), item);
844 submenu = gtk_menu_new ();
845 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
850 // =============================================================================
853 void dialog_button_callback( GtkWidget *widget, gpointer data ) {
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" );
865 gint dialog_delete_callback (GtkWidget *widget, GdkEvent* event, gpointer data)
869 gtk_widget_hide (widget);
870 loop = (int*)g_object_get_data (G_OBJECT (widget), "loop");
876 gint dialog_url_callback (GtkWidget *widget, GdkEvent* event, gpointer data)
878 OpenURL((const char *)g_object_get_data (G_OBJECT (widget), "URL"));
883 int WINAPI gtk_MessageBox (void *parent, const char* lpText, const char* lpCaption, guint32 uType, const char* URL)
885 GtkWidget *window, *w, *vbox, *hbox;
886 GtkAccelGroup *accel;
887 int mode = (uType & MB_TYPEMASK), ret, loop = 1;
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);
900 gtk_window_set_policy(GTK_WINDOW (window),FALSE,FALSE,TRUE);
903 gtk_window_set_transient_for (GTK_WINDOW (window), GTK_WINDOW (parent));
905 accel = gtk_accel_group_new ();
906 gtk_window_add_accel_group (GTK_WINDOW (window), accel);
908 vbox = gtk_vbox_new (FALSE, 10);
909 gtk_container_add (GTK_CONTAINER (window), vbox);
910 gtk_widget_show (vbox);
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);
917 w = gtk_hseparator_new ();
918 gtk_box_pack_start (GTK_BOX (vbox), w, FALSE, FALSE, 2);
921 hbox = gtk_hbox_new (FALSE, 10);
922 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 2);
923 gtk_widget_show (hbox);
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);
938 else if (mode == MB_OKCANCEL)
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);
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);
957 else if (mode == MB_YESNOCANCEL)
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);
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));
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));
980 else /* if (mode == MB_YESNO) */
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);
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));
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);
1011 gtk_widget_show (window);
1012 gtk_grab_add (window);
1015 gtk_main_iteration ();
1017 gtk_grab_remove (window);
1018 gtk_widget_destroy (window);
1023 // =============================================================================
1026 // fenris #3078 WHENHELLISFROZENOVER
1028 //#define FILEDLG_DBG
1030 static void file_sel_callback (GtkWidget *widget, gpointer data)
1036 parent = gtk_widget_get_toplevel (widget);
1037 loop = (int*)g_object_get_data (G_OBJECT (parent), "loop");
1038 success = (bool*)g_object_get_data (G_OBJECT (parent), "success");
1040 if ((int)data == IDOK)
1045 Sys_Printf("file_sel_callback != IDOK\n");
1052 #include <commdlg.h>
1053 static OPENFILENAME ofn; /* common dialog box structure */
1054 static char szDirName[MAX_PATH]; /* directory string */
1055 static char szFile[MAX_PATH]; /* filename string */
1056 static char szFileTitle[MAX_PATH]; /* file title string */
1057 static int i, cbString; /* integer count variables */
1058 static HANDLE hf; /* file handle */
1060 static char szFile[QER_MAX_NAMELEN];
1063 #define FILEDLG_CUSTOM_FILTER_LENGTH 64
1064 // to be used with the advanced file selector
1066 class CFileType : public IFileTypeList
1068 struct filetype_copy_t
1070 void operator=(const filetype_t& other)
1072 m_name = other.name;
1073 m_pattern = other.pattern;
1083 m_strWin32Filters = NULL;
1084 m_pstrGTKMasks = NULL;
1087 virtual ~CFileType()
1090 DestroyWin32Filters();
1094 void addType(filetype_t type)
1096 filetype_copy_t* newTypes = new filetype_copy_t [m_nTypes+1];
1099 for(int i=0; i<m_nTypes; i++)
1100 newTypes[i] = m_pTypes[i];
1103 m_pTypes = newTypes;
1104 m_pTypes[m_nTypes] = type;
1106 ConstructGTKMasks();
1107 ConstructWin32Filters();
1110 filetype_t GetTypeForWin32Filter(const char *filter) const
1112 for(int i=0; i<m_nTypes; i++)
1113 if(strcmp(m_pTypes[i].m_pattern.c_str(), filter)==0)
1114 return filetype_t(m_pTypes[i].m_name.c_str(), m_pTypes[i].m_pattern.c_str());
1115 return filetype_t();
1118 filetype_t GetTypeForGTKMask(const char *mask) const
1120 for(int i=0; i<m_nTypes; i++)
1121 if(strcmp(m_pstrGTKMasks[i],mask)==0)
1122 return filetype_t(m_pTypes[i].m_name.c_str(), m_pTypes[i].m_pattern.c_str());
1123 return filetype_t();
1126 char *m_strWin32Filters;
1127 char **m_pstrGTKMasks;
1130 filetype_copy_t *m_pTypes;
1132 void DestroyWin32Filters()
1134 delete[] m_strWin32Filters;
1137 void ConstructWin32Filters()
1143 DestroyWin32Filters();
1144 for(i=0; i<m_nTypes; i++)
1145 len = len + strlen(m_pTypes[i].m_name.c_str()) + strlen(m_pTypes[i].m_pattern.c_str())*2 + 5;
1146 m_strWin32Filters = new char[len+1]; // length + null char
1147 for(i=0, w = m_strWin32Filters; i<m_nTypes; i++)
1149 for(r = m_pTypes[i].m_name.c_str(); *r!='\0'; r++, w++)
1153 for(r = m_pTypes[i].m_pattern.c_str(); *r!='\0'; r++, w++)
1157 for(r = m_pTypes[i].m_pattern.c_str(); *r!='\0'; r++, w++)
1158 *w = (*r == ',') ? ';' : *r;
1161 m_strWin32Filters[len] = '\0';
1164 void DestroyGTKMasks()
1166 if(m_pstrGTKMasks != NULL)
1167 for(char **p = m_pstrGTKMasks; *p != NULL; p++)
1169 delete[] m_pstrGTKMasks;
1172 void ConstructGTKMasks()
1179 m_pstrGTKMasks = new char*[m_nTypes+1];
1180 for(i=0; i<m_nTypes; i++)
1182 len = strlen(m_pTypes[i].m_name.c_str()) + strlen(m_pTypes[i].m_pattern.c_str()) + 3;
1183 m_pstrGTKMasks[i] = new char[len+1]; // length + null char
1184 w = m_pstrGTKMasks[i];
1185 for(r = m_pTypes[i].m_name.c_str(); *r!='\0'; r++, w++)
1189 for(r = m_pTypes[i].m_pattern.c_str(); *r!='\0'; r++, w++)
1194 m_pstrGTKMasks[m_nTypes] = NULL;
1199 const char* file_dialog (void *parent, gboolean open, const char* title, const char* path, const char* pattern)
1202 GtkWidget* file_sel;
1204 char *new_path = NULL;
1211 GetFileTypeRegistry()->getTypeList(pattern, &typelist);
1214 Sys_Printf("file_dialog: open = %d title = %s path = %s\n", open, title, path);
1217 Sys_Printf("Patterns:\n");
1218 char** p = typelist.m_pstrGTKMasks;
1220 Sys_Printf("%s\n", *p++);
1223 Sys_Printf("no patterns\n");
1227 // win32 dialog stores the selected "save as type" extension in the second null-terminated string
1228 char customfilter[FILEDLG_CUSTOM_FILTER_LENGTH];
1230 if (g_PrefsDlg.m_bNativeGUI)
1233 Sys_Printf("Doing win32 file dialog...");
1235 // do that the native way
1236 /* Place the terminating null character in the szFile. */
1238 customfilter[0] = customfilter[1] = customfilter[2] = '\0';
1240 /* Set the members of the OPENFILENAME structure. */
1241 ofn.lStructSize = sizeof(OPENFILENAME);
1242 ofn.hwndOwner = (HWND)GDK_WINDOW_HWND (g_pParentWnd->m_pWidget->window);
1245 ofn.nFilterIndex = 0;
1246 ofn.lpstrFilter = typelist.m_strWin32Filters;
1248 else ofn.nFilterIndex = 1;
1249 ofn.lpstrCustomFilter = customfilter;
1250 ofn.nMaxCustFilter = sizeof(customfilter);
1251 ofn.lpstrFile = szFile;
1252 ofn.nMaxFile = sizeof(szFile);
1253 ofn.lpstrFileTitle = NULL; // we don't need to get the name of the file
1256 // szDirName: Radiant uses unix convention for paths internally
1257 // Win32 (of course) and Gtk (who would have thought) expect the '\\' convention
1258 // copy path, replacing dir separators as appropriate
1259 for(r=path, w=szDirName; *r!='\0'; r++)
1260 *w++ = (*r=='/') ? '\\' : *r;
1263 ofn.lpstrInitialDir = szDirName;
1265 else ofn.lpstrInitialDir = NULL;
1266 ofn.lpstrTitle = title;
1267 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
1269 /* Display the Open dialog box. */
1270 // it's open or close depending on 'open' parameter
1273 if (!GetOpenFileName(&ofn))
1274 return NULL; // canceled
1278 if (!GetSaveFileName(&ofn))
1279 return NULL; // canceled
1283 type = typelist.GetTypeForWin32Filter(customfilter+1);
1286 Sys_Printf("Done.\n");
1292 // do that the Gtk way
1294 title = open ? "Open File" : "Save File";
1297 Sys_Printf("Doing Gtk file dialog:\nBuilding new_path..");
1299 // we expect an actual path below, if the path is NULL we might crash
1300 if (!path || path[0] == '\0')
1304 #elif defined (__linux__) || defined (__APPLE__)
1311 // alloc new path with extra char for dir separator
1312 new_path = new char[strlen(path)+1+1];
1313 // copy path, replacing dir separators as appropriate
1314 for(r=path, w=new_path; *r!='\0'; r++)
1315 *w++ = (*r=='/') ? G_DIR_SEPARATOR : *r;
1316 // add dir separator to end of path if required
1317 if(*(w-1) != G_DIR_SEPARATOR) *w++ = G_DIR_SEPARATOR;
1322 Sys_Printf("Done.\n");
1323 Sys_Printf("Calling gtk_file_selection_new with title: %s...", title);
1326 file_sel = gtk_file_selection_new (title);
1329 Sys_Printf("Done.\n");
1330 Sys_Printf("Set the masks...");
1333 #if 0 //!\todo Add masks to GtkFileSelection in gtk-2.0
1337 gtk_file_selection_clear_masks (GTK_FILE_SELECTION (file_sel));
1338 gtk_file_selection_set_masks (GTK_FILE_SELECTION (file_sel), const_cast<const char**>(typelist.m_pstrGTKMasks));
1343 Sys_Printf("Done.\n");
1346 gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (file_sel)->ok_button), "clicked",
1347 GTK_SIGNAL_FUNC (file_sel_callback), GINT_TO_POINTER (IDOK));
1348 gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (file_sel)->cancel_button), "clicked",
1349 GTK_SIGNAL_FUNC (file_sel_callback), GINT_TO_POINTER (IDCANCEL));
1350 gtk_signal_connect (GTK_OBJECT (file_sel), "delete_event",
1351 GTK_SIGNAL_FUNC (dialog_delete_callback), NULL);
1352 gtk_file_selection_hide_fileop_buttons (GTK_FILE_SELECTION (file_sel));
1355 gtk_window_set_transient_for (GTK_WINDOW (file_sel), GTK_WINDOW (parent));
1358 Sys_Printf("set_data...");
1360 bool success = false;
1361 g_object_set_data (G_OBJECT (file_sel), "loop", &loop);
1362 g_object_set_data (G_OBJECT (file_sel), "success", &success);
1364 Sys_Printf("Done.\n");
1370 Sys_Printf("set_data \"overwrite\" ...");
1372 g_object_set_data (G_OBJECT (file_sel), "overwrite", GINT_TO_POINTER (1));
1374 Sys_Printf("Done.\n");
1378 if (new_path != NULL)
1381 Sys_Printf("gtk_file_selection_set_filename... %p", file_sel);
1383 gtk_file_selection_set_filename (GTK_FILE_SELECTION (file_sel), new_path);
1386 Sys_Printf("Done.\n");
1390 gtk_grab_add (file_sel);
1392 Sys_Printf("gtk_widget_show... %p", file_sel);
1394 gtk_widget_show (file_sel);
1396 Sys_Printf("Done.\n");
1400 Sys_Printf("gtk_main_iteration...");
1403 gtk_main_iteration ();
1406 #if 0 //!\todo Add masks to GtkFileSelection in gtk2
1408 type = typelist.GetTypeForGTKMask(GTK_FILE_SELECTION (file_sel)->mask);
1410 strcpy(szFile, gtk_file_selection_get_filename (GTK_FILE_SELECTION (file_sel)));
1413 Sys_Printf("Done.\n");
1416 gtk_grab_remove (file_sel);
1417 gtk_widget_destroy (file_sel);
1422 // don't return an empty filename
1423 if(szFile[0] == '\0') return NULL;
1425 // convert back to unix format
1426 for(w=szFile; *w!='\0'; w++)
1431 if (g_PrefsDlg.m_bNativeGUI) // filetype mask not supported in gtk dialog yet
1433 // when saving, force an extension depending on filetype
1434 /* \todo SPoG - file_dialog should return filetype information separately.. not force file extension.. */
1435 if(!open && pattern != NULL)
1437 // last ext separator
1438 w = strrchr(szFile, '.');
1440 w = (w!=NULL) ? w : szFile+strlen(szFile);
1441 strcpy(w, type.pattern+1);
1446 // prompt to overwrite existing files
1448 if (access (szFile, R_OK) == 0)
1449 if (gtk_MessageBox (parent, "File already exists.\nOverwrite?", "GtkRadiant", MB_YESNO) == IDNO)
1453 // ... let's use a static filename
1454 Sys_Printf("filename: %p\n", szFile);
1460 char* WINAPI dir_dialog (void *parent, const char* title, const char* path)
1462 GtkWidget* file_sel;
1463 char* filename = (char*)NULL;
1465 bool success = false;
1467 file_sel = gtk_file_selection_new (title);
1468 gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (file_sel)->ok_button), "clicked",
1469 GTK_SIGNAL_FUNC (file_sel_callback), GINT_TO_POINTER (IDOK));
1470 gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (file_sel)->cancel_button), "clicked",
1471 GTK_SIGNAL_FUNC (file_sel_callback), GINT_TO_POINTER (IDCANCEL));
1472 gtk_signal_connect (GTK_OBJECT (file_sel), "delete_event",
1473 GTK_SIGNAL_FUNC (dialog_delete_callback), NULL);
1474 gtk_file_selection_hide_fileop_buttons (GTK_FILE_SELECTION (file_sel));
1477 gtk_window_set_transient_for (GTK_WINDOW (file_sel), GTK_WINDOW (parent));
1479 gtk_widget_hide (GTK_FILE_SELECTION (file_sel)->file_list->parent);
1481 g_object_set_data (G_OBJECT (file_sel), "loop", &loop);
1482 g_object_set_data (G_OBJECT (file_sel), "success", &success);
1485 gtk_file_selection_set_filename (GTK_FILE_SELECTION (file_sel), path);
1487 gtk_grab_add (file_sel);
1488 gtk_widget_show (file_sel);
1491 gtk_main_iteration ();
1493 filename = g_strdup(gtk_file_selection_get_filename (GTK_FILE_SELECTION (file_sel)));
1495 gtk_grab_remove (file_sel);
1496 gtk_widget_destroy (file_sel);
1501 bool WINAPI color_dialog (void *parent, float *color, const char* title)
1505 int loop = 1, ret = IDCANCEL;
1511 dlg = gtk_color_selection_dialog_new (title);
1512 gtk_color_selection_set_color (GTK_COLOR_SELECTION (GTK_COLOR_SELECTION_DIALOG (dlg)->colorsel), clr);
1513 gtk_signal_connect (GTK_OBJECT (dlg), "delete_event",
1514 GTK_SIGNAL_FUNC (dialog_delete_callback), NULL);
1515 gtk_signal_connect (GTK_OBJECT (dlg), "destroy",
1516 GTK_SIGNAL_FUNC (gtk_widget_destroy), NULL);
1517 gtk_signal_connect (GTK_OBJECT (GTK_COLOR_SELECTION_DIALOG (dlg)->ok_button), "clicked",
1518 GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (IDOK));
1519 gtk_signal_connect (GTK_OBJECT (GTK_COLOR_SELECTION_DIALOG (dlg)->cancel_button), "clicked",
1520 GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (IDCANCEL));
1521 g_object_set_data (G_OBJECT (dlg), "loop", &loop);
1522 g_object_set_data (G_OBJECT (dlg), "ret", &ret);
1525 gtk_window_set_transient_for (GTK_WINDOW (dlg), GTK_WINDOW (parent));
1527 gtk_widget_show (dlg);
1531 gtk_main_iteration ();
1534 gtk_color_selection_get_current_color (GTK_COLOR_SELECTION (GTK_COLOR_SELECTION_DIALOG (dlg)->colorsel), &gdkcolor);
1535 clr[0] = gdkcolor.red / 65535.0;
1536 clr[1] = gdkcolor.green / 65535.0;
1537 clr[2] = gdkcolor.blue / 65535.0;
1539 gtk_grab_remove (dlg);
1540 gtk_widget_destroy (dlg);
1544 color[0] = (float)clr[0];
1545 color[1] = (float)clr[1];
1546 color[2] = (float)clr[2];
1554 void OpenURL(const char *url)
1556 // let's put a little comment
1557 Sys_Printf("OpenURL: %s\n", url);
1559 // \todo FIXME: the way we open URLs on *nix should be improved. A script is good (see how I do on RTCW)
1560 char command[2*PATH_MAX];
1561 snprintf( command, sizeof(command), "%s/openurl.sh \"%s\" &", g_strAppPath.GetBuffer(), url );
1562 if (system (command) != 0)
1563 gtk_MessageBox (g_pParentWnd->m_pWidget, "Failed to launch Netscape!");
1566 char command[2*PATH_MAX];
1567 snprintf (command, sizeof(command),
1568 "open \"%s\" &", url, url);
1569 if (system (command) != 0)
1570 gtk_MessageBox (g_pParentWnd->m_pWidget, "Unable to launch browser!");
1573 ShellExecute( (HWND)GDK_WINDOW_HWND (g_pParentWnd->m_pWidget->window), "open", url, NULL, NULL, SW_SHOW );
1577 void CheckMenuSplitting (GtkWidget *&menu)
1579 GtkWidget *item,*menu2;
1581 GtkRequisition requisition;
1584 gtk_widget_size_request (GTK_WIDGET (menu), &requisition);
1585 screen_height = gdk_screen_height ();
1587 if ((screen_height - requisition.height) < 20)
1589 menu2 = gtk_menu_new ();
1591 // move the last 2 items to a submenu (3 because of win32)
1592 for (int i = 0; i < 3; i++)
1594 item = GTK_WIDGET (g_list_last (gtk_container_children (GTK_CONTAINER (menu)))->data);
1595 gtk_widget_ref (item);
1596 gtk_container_remove (GTK_CONTAINER (menu), item);
1597 gtk_menu_append (GTK_MENU (menu2), item);
1598 gtk_widget_unref (item);
1601 item = gtk_menu_item_new_with_label ("--------");
1602 gtk_widget_show (item);
1603 gtk_container_add (GTK_CONTAINER (menu), item);
1604 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu2);