New cisco icons, and a fix in element.h
[dia.git] / app / winmain.c
blobc207cd7d66c049a716f8e77a7af98552d2707252
1 #include <config.h>
3 #include <stdlib.h>
4 #include <glib.h>
5 #include <gtk/gtk.h> /* just for version */
7 #ifdef G_OS_WIN32
8 #define WIN32_LEAN_AND_MEAN
9 #include <windows.h> /* native file api */
11 extern int main (int argc, char **argv);
13 /* In case we build this as a windowed application */
15 #ifdef __GNUC__
16 # ifndef _stdcall
17 # define _stdcall __attribute__((stdcall))
18 # endif
19 #endif
21 int _stdcall
22 WinMain (struct HINSTANCE__ *hInstance,
23 struct HINSTANCE__ *hPrevInstance,
24 char *lpszCmdLine,
25 int nCmdShow)
27 return main (__argc, __argv);
30 void
31 dia_log_func (const gchar *log_domain,
32 GLogLevelFlags flags,
33 const gchar *message,
34 gpointer data)
36 HANDLE file = (HANDLE)data;
37 const char* level;
38 static char* last_message = NULL;
39 guint32 dwWritten; /* looks like being optional in MSDN, but isn't */
41 /* some small optimization especially for the ugly tweaked font message */
42 if (last_message && (0 == strcmp (last_message, message)))
43 return;
44 /* not using GLib functions! */
45 if (last_message)
46 free (last_message);
47 last_message = strdup (message); /* finally leaked */
49 WriteFile (file, log_domain ? log_domain : "?", log_domain ? strlen(log_domain) : 1, &dwWritten, 0);
50 WriteFile (file, "-", 1, &dwWritten, 0);
51 level = flags & G_LOG_LEVEL_ERROR ? "Error" :
52 flags & G_LOG_LEVEL_CRITICAL ? "Critical" :
53 flags & G_LOG_LEVEL_WARNING ? "Warning" :
54 flags & G_LOG_LEVEL_INFO ? "Info" :
55 flags & G_LOG_LEVEL_DEBUG ? "Debug" :
56 flags & G_LOG_LEVEL_MESSAGE ? "Message" : "?";
57 WriteFile (file, level, strlen(level), &dwWritten, 0);
58 WriteFile (file, ": ", 2, &dwWritten, 0);
59 WriteFile (file, message, strlen(message), &dwWritten, 0);
60 WriteFile (file, "\r\n", 2, &dwWritten, 0);
61 FlushFileBuffers (file);
63 if (flags & G_LOG_FATAL_MASK)
65 /* should we also exit here or at least do MessageBox ?*/
69 void
70 dia_print_func (const char* string)
75 void
76 dia_redirect_console (void)
78 const gchar *log_domains[] =
80 "GLib", "GModule", "GThread", "GLib-GObject",
81 "Pango", "PangoFT2", "PangoWin32", /* noone sets them ?*/
82 "Gtk", "Gdk", "GdkPixbuf",
83 "Dia", "DiaLib", "DiaObject", "DiaPlugin", "DiaPython",
84 NULL,
86 static int MAX_TRIES = 16;
87 int i = 0;
88 HANDLE file = INVALID_HANDLE_VALUE;
89 char logname[] = "dia--" VERSION ".log";
90 char* redirected;
91 gboolean verbose = TRUE;
93 if ( ((file = GetStdHandle (STD_OUTPUT_HANDLE)) != INVALID_HANDLE_VALUE)
94 || ((file = GetStdHandle (STD_ERROR_HANDLE)) != INVALID_HANDLE_VALUE))
95 verbose = FALSE; /* use it but not too much ;-) */
96 else do
98 /* overwrite at startup */
99 redirected = g_build_filename (g_get_tmp_dir (), logname, NULL);
100 /* not using standard c runtime functions to
101 * deal with possibly multiple instances
103 file = CreateFile (redirected, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH, 0);
104 if (file == INVALID_HANDLE_VALUE)
106 i++;
107 g_free (redirected);
108 logname[3] = '0' + i;
110 } while (file == INVALID_HANDLE_VALUE && i < MAX_TRIES);
112 if (file != INVALID_HANDLE_VALUE)
114 char* log2 = g_strjoinv("\r\n", _environ);
115 char* log = g_strdup_printf ("Dia (%s) instance %d started "
116 "using Gtk %d.%d.%d (%d)\r\n%s\r\n",
117 VERSION, i + 1,
118 gtk_major_version, gtk_minor_version, gtk_micro_version, gtk_binary_age,
119 log2);
120 guint32 dwWritten; /* looks like being optional in msdn, but isn't */
121 g_free (log2);
123 if (!verbose || WriteFile (file, log, strlen(log), &dwWritten, 0))
125 for (i = 0; i < G_N_ELEMENTS (log_domains); i++)
126 g_log_set_handler (log_domains[i],
127 G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR,
128 dia_log_func,
129 file);
130 /* don't redirect g_print () yet, it's upcoming API */
131 //g_set_print_handler (dia_print_func);
133 else
135 char* emsg = g_win32_error_message (GetLastError ());
136 g_printerr ("Logfile %s writing failed: %s", redirected, emsg);
137 g_free (emsg);
139 g_free (log);
143 #endif