/[winpt]/trunk/Src/wptW32API.cpp
ViewVC logotype

Annotation of /trunk/Src/wptW32API.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 196 - (hide annotations)
Fri Apr 7 10:47:37 2006 UTC (18 years, 10 months ago) by twoaday
File size: 14836 byte(s)
2006-04-07  Timo Schulz  <ts@g10code.de>

        * wptW32API.cpp (set_clip_text): Change code so it also
        works again with non-XP systems.


1 werner 36 /* wptW32API.cpp - Common W32 API functions
2 twoaday 121 * Copyright (C) 2001, 2002, 2003, 2005 Timo Schulz
3 werner 36 *
4     * This file is part of WinPT.
5     *
6     * WinPT is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * WinPT is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with WinPT; if not, write to the Free Software Foundation,
18     * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19     */
20 twoaday 121
21 werner 36 #ifdef HAVE_CONFIG_H
22     #include <config.h>
23     #endif
24    
25     #include <windows.h>
26     #include <stdio.h>
27     #include <sys/types.h>
28     #include <sys/stat.h>
29     #include <shellapi.h>
30     #include <shlobj.h>
31     #include <commctrl.h>
32 twoaday 129 #include <time.h>
33 werner 36
34     #include "wptNLS.h"
35     #include "wptW32API.h"
36     #include "wptErrors.h"
37     #include "wptVersion.h"
38     #include "wptTypes.h"
39    
40    
41     extern "C" void _SHFree (void *p);
42    
43    
44 twoaday 193
45     /* Insert a new item into the menu @hm at position @pos. */
46     void
47     insert_menu_item (HMENU hm, int pos, UINT m_id, const char *text)
48     {
49     MENUITEMINFO mi;
50    
51     memset (&mi, 0, sizeof mi);
52     mi.cbSize = sizeof mi;
53     mi.fType = MF_STRING;
54     mi.dwTypeData = (char *)text;
55     mi.cch = strlen (text);
56     mi.wID = m_id;
57     mi.fMask = MIIM_ID|MIIM_DATA| MIIM_TYPE;
58     InsertMenuItem (hm, pos, FALSE, &mi);
59     }
60    
61    
62 werner 36 static void
63     set_menu_text_ext (HMENU menu, int by_pos, int m_uid, const char *text)
64     {
65     MENUITEMINFO mii;
66    
67     memset (&mii, 0, sizeof mii);
68     mii.cbSize = sizeof mii;
69     mii.fMask = MIIM_TYPE;
70     mii.fType = MFT_STRING;
71     mii.dwTypeData = (char *) text;
72     SetMenuItemInfo (menu, m_uid, by_pos? TRUE : FALSE, &mii);
73     }
74    
75    
76     /* Set the text of a menu item @m_uid to @text. */
77     void
78 twoaday 129 set_menu_text (HMENU menu, UINT m_uid, const char *text)
79 werner 36 {
80     set_menu_text_ext (menu, 0, m_uid, text);
81     }
82    
83    
84     /* Set the text of a menu item with the position @pos to @text. */
85     void
86 twoaday 129 set_menu_text_bypos (HMENU menu, UINT pos, const char *text)
87 werner 36 {
88     set_menu_text_ext (menu, 1, pos, text);
89     }
90    
91    
92     /* Set the state of a menu item @m_uid to @state. */
93     void
94 twoaday 129 set_menu_state (HMENU menu, UINT m_uid, UINT state)
95 werner 36 {
96     MENUITEMINFO mii;
97    
98 twoaday 121 memset (&mii, 0, sizeof (mii));
99 werner 36 mii.cbSize = sizeof (mii);
100     mii.fMask = MIIM_STATE;
101     mii.fState = state;
102     SetMenuItemInfo (menu, m_uid, FALSE, &mii);
103     }
104    
105    
106 twoaday 129 /* Retrieve the state of the menu item @m_uid and return it. */
107     UINT
108     get_menu_state (HMENU menu, UINT m_uid)
109     {
110     MENUITEMINFO mii;
111    
112     memset (&mii, 0, sizeof (mii));
113     mii.cbSize = sizeof (mii);
114     mii.fMask = MIIM_STATE;
115     GetMenuItemInfo (menu, m_uid, FALSE, &mii);
116     return mii.fState;
117     }
118    
119    
120 twoaday 77 enum {
121     CDLG_FILE_OPEN = 0,
122     CDLG_FILE_SAVE = 1
123     };
124 werner 36
125     /* Use the common dialog to request a file from the user.
126     id can be either FILE_OPEN or FILE_SAVE.
127     The return value is the file name or NULL if cancel was chosen. */
128     const char *
129 twoaday 193 get_filename_dlg (HWND hwnd, int id, const char *title,
130     const char *filter, const char *name)
131 werner 36 {
132     static char file[512] = "";
133     OPENFILENAME open;
134    
135     if (name && strlen (name) < (sizeof (file)-1))
136     strcpy (file, name);
137     else
138     memset (file, 0, sizeof (file));
139     if (!filter)
140 twoaday 167 filter = "All Files (*.*)\0*.*\0\0";
141 werner 36 /* XXX: problem with gettext because of the 'artificial'
142     double string termination!. */
143     memset (&open, 0, sizeof (open));
144     open.lStructSize = sizeof (OPENFILENAME);
145     open.hInstance = glob_hinst;
146     open.lpstrTitle = title;
147     open.lpstrFilter = filter;
148     open.hwndOwner = hwnd;
149     open.lpstrFile = file;
150     open.nMaxFile = sizeof (file) - 1;
151 twoaday 77 if (id == CDLG_FILE_OPEN)
152 werner 36 open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
153     else
154     open.Flags = OFN_OVERWRITEPROMPT;
155    
156 twoaday 77 if (id == CDLG_FILE_OPEN && GetOpenFileName (&open))
157 werner 36 return open.lpstrFile;
158 twoaday 77 else if (id == CDLG_FILE_SAVE && GetSaveFileName (&open))
159 werner 36 return open.lpstrFile;
160    
161     return NULL;
162     }
163    
164     const char*
165     get_filesave_dlg (HWND hwnd, const char *title,
166     const char *filter, const char *name)
167     {
168 twoaday 77 return get_filename_dlg (hwnd, CDLG_FILE_SAVE, title, filter, name);
169 werner 36 }
170    
171 twoaday 121 const char*
172 werner 36 get_fileopen_dlg (HWND hwnd, const char *title, const char *filter,
173     const char *name)
174     {
175 twoaday 77 return get_filename_dlg (hwnd, CDLG_FILE_OPEN, title, filter, name);
176 werner 36 }
177    
178    
179     /* Use the common dialog to allow the user to select a folder.
180     The return value is either the folder path or NULL if cancel was chosen. */
181     const char*
182 twoaday 121 get_folder_dlg (HWND hwnd, const char *title, const char *name)
183 werner 36 {
184     static char folder[MAX_PATH+1] = "";
185     BROWSEINFO bi;
186 twoaday 121 ITEMIDLIST *il;
187 werner 36
188     memset (&bi, 0, sizeof (bi));
189     bi.hwndOwner = hwnd;
190     if (title)
191     bi.lpszTitle = title;
192     if (name && strlen (name) < MAX_PATH-1)
193     strcpy (folder, name);
194     else
195     memset (folder, 0, sizeof (folder));
196     il = SHBrowseForFolder (&bi);
197     if (il) {
198     SHGetPathFromIDList (il, folder);
199     _SHFree (il);
200     return folder;
201     }
202     return NULL;
203     }
204    
205    
206     /* Return the clipboard contents as a string or NULL
207     if the clipboard does not contain text. */
208     char*
209     get_clip_text (HWND hwnd)
210     {
211     HANDLE clipmem;
212     char *cliptxt, *p;
213     int len;
214    
215     if (OpenClipboard (hwnd) == FALSE)
216     return NULL;
217     clipmem = GetClipboardData (CF_TEXT);
218     if (clipmem == NULL) {
219     p = NULL;
220     goto leave;
221     }
222     cliptxt = (char *) GlobalLock (clipmem);
223     if (cliptxt == NULL) {
224     p = NULL;
225     goto leave;
226     }
227    
228     len = strlen (cliptxt);
229     p = new char[len + 1];
230     if (!p)
231     BUG (NULL);
232     memcpy (p, cliptxt, len);
233     p[len] = '\0';
234     GlobalUnlock (clipmem);
235    
236     leave:
237     CloseClipboard ();
238     return p;
239     }
240    
241    
242     /* Set @text as the new clipboard content. */
243     int
244     set_clip_text (HWND hwnd, const char *text, int nbytes)
245     {
246     HANDLE clipmem;
247     int rc = 0;
248     char *p;
249    
250     if (OpenClipboard (hwnd) == FALSE)
251     return WPTERR_CLIP_OPEN;
252     EmptyClipboard ();
253    
254     clipmem = GlobalAlloc (GHND, nbytes + 1);
255     if (clipmem == NULL)
256     BUG (NULL);
257     p = (char *) GlobalLock (clipmem);
258     if (p == NULL) {
259 twoaday 196 CloseClipboard ();
260     GlobalFree (clipmem);
261     return WPTERR_GENERAL;
262 werner 36 }
263     memcpy (p, text, nbytes);
264     p[nbytes] = '\0';
265    
266 twoaday 196 SetClipboardData (CF_TEXT, clipmem);
267 werner 36 GlobalUnlock (clipmem);
268 twoaday 196 CloseClipboard ();
269 twoaday 181 GlobalFree (clipmem);
270 werner 36
271     return rc;
272 twoaday 181 }
273 werner 36
274    
275     /* Append or prepend some text to the clipboard contents.
276     If as_footer = 1, append the text otherwise prepend. */
277     int
278     set_clip_text2 (HWND hwnd, const char *text, int nbytes, int as_footer)
279     {
280     char *p, *new_text;
281    
282     p = get_clip_text (hwnd);
283     if (!p)
284     return WPTERR_CLIP_GET;
285     new_text = new char [strlen (p)+strlen (text)+8];
286     if (!new_text)
287     BUG (0);
288     if (as_footer == 0)
289     sprintf (new_text, "%s\r\n%s\r\n\r\n", text, p);
290     else
291     sprintf (new_text, "%s\n%s\n\n", p, text);
292     set_clip_text (hwnd, new_text, strlen (new_text)+1);
293     free_if_alloc (p);
294     free_if_alloc (new_text);
295     return 0;
296     }
297    
298    
299 twoaday 121 /* Make a file name out of the path @path, the file @file and
300     an extension. @ext.
301     Return value: the full file name on success. */
302 werner 36 char*
303     make_filename (const char *path, const char *file, const char *ext)
304     {
305     char *p;
306     size_t size = 0;
307    
308     if( path && *path )
309     size += strlen( path );
310     if( file && *file )
311     size += strlen( file );
312     if( ext && *ext )
313     size += strlen( ext );
314     p = new char[size + 4];
315     memset( p, 0, size );
316     if( path ) {
317     strcat( p, path );
318     if( path[strlen( path ) -1] != '\\' )
319     strcat( p, "\\" );
320     }
321     if( file )
322     strcat( p, file );
323     if( ext ) {
324     strcat( p, "." );
325     strcat( p, ext );
326     }
327     return p;
328 twoaday 121 }
329 werner 36
330    
331 twoaday 150 /* Generate a file name from a special dirctory. */
332     char*
333     make_special_filename (int folder, const char *file, const char *ext)
334     {
335     BOOL ec;
336     char path[MAX_PATH], *p;
337     size_t n=0;
338    
339     /* MSDN: buf must be at least MAX_PATH=256 bytes */
340     ec = SHGetSpecialFolderPath (HWND_DESKTOP, path, folder, TRUE);
341     if (ec != 1) {
342     log_debug ("SHGetSpecialFolderPath() failed\r\n", (int)GetLastError ());
343     return NULL;
344     }
345    
346     n = strlen (path)+1;
347     if (file)
348     n += strlen (file)+1;
349     if (ext)
350     n += strlen (ext)+1;
351     p = new char[n+2];
352     if (!p)
353     BUG (0);
354     memset (p, 0, n+2);
355     strcpy (p, path);
356     if (file) {
357     strcat (p, "\\");
358     strcat (p, file);
359     }
360     if (ext)
361     strcat (p, ext);
362     return p;
363     }
364    
365    
366 twoaday 121 /* return 0 if the file @fname exists, otherwise >0. */
367 werner 36 int
368 twoaday 121 file_exist_check (const char *fname)
369 werner 36 {
370     struct stat st;
371     if (stat (fname, &st) == -1)
372     return WPTERR_FILE_EXIST;
373     return 0;
374     }
375    
376    
377     /* Check if the current folder exists.
378     Return 0 for success. */
379     int
380     dir_exist_check (const char *dir)
381     {
382     struct stat statbuf;
383    
384 twoaday 105 if (stat (dir, &statbuf) == -1)
385 werner 36 return WPTERR_GENERAL;
386 twoaday 105 if (statbuf.st_mode & _S_IFDIR)
387 werner 36 return 0;
388     return WPTERR_GENERAL;
389     }
390    
391    
392 twoaday 77 /* Return the file size of the given file @fname. */
393     DWORD
394 werner 36 get_file_size (const char *fname)
395     {
396 twoaday 77 DWORD fsize;
397 werner 36 HANDLE fh;
398    
399 twoaday 77 fh = CreateFile (fname, GENERIC_READ, FILE_SHARE_READ,
400     NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
401     if (fh == INVALID_HANDLE_VALUE)
402 werner 36 return 0;
403 twoaday 77 fsize = GetFileSize (fh, NULL);
404     if (fsize == 0xFFFFFFFF)
405 werner 36 fsize = 0;
406 twoaday 77 CloseHandle (fh);
407 werner 36 return fsize;
408 twoaday 77 }
409 werner 36
410    
411     /* Start a dialog with the exception that before it is checked that the
412     dialog is not already openened. */
413     int
414 twoaday 105 dialog_box_param (HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,
415     LPARAM param, LPCTSTR title, int title_id)
416 werner 36 {
417 twoaday 105 if (FindWindowEx (GetDesktopWindow (), NULL, NULL, title))
418 werner 36 return -1;
419 twoaday 105 return DialogBoxParam (hinst, name, parent, fnc, param);
420     }
421 werner 36
422    
423     /* Wrapper for message box which forces the message box into the
424     foreground and it is displayed always on top. */
425     int
426     msg_box (HWND hwnd, const char *text, const char *title, int mode)
427     {
428     mode |= MB_SETFOREGROUND;
429     mode |= MB_TASKMODAL;
430     mode |= MB_TOPMOST;
431     return MessageBox(hwnd, text, title, mode);
432     }
433    
434    
435 twoaday 121 /* Safe strdup version (C++ version). */
436 werner 36 char*
437     m_strdup (const char *str)
438     {
439 twoaday 121 char *p = new char[strlen (str) + 1];
440     if (!p)
441     BUG (NULL);
442     strcpy (p, str);
443 werner 36 return p;
444 twoaday 121 }
445 werner 36
446    
447     /* Center the hwndChild relative to parent.
448     The style param allows to specificy additional styles (like topmost). */
449     void
450     center_window2 (HWND hwndChild, HWND parent, HWND style)
451     {
452     HWND hwndParent;
453     RECT rChild, rParent;
454     HDC hdc;
455     int wChild, hChild, wParent, hParent;
456     int wScreen, hScreen, xNew, yNew;
457     int flags = SWP_NOSIZE | SWP_NOZORDER;
458    
459     hwndParent = parent;
460     if (hwndParent == NULL)
461     hwndParent = GetDesktopWindow ();
462     GetWindowRect (hwndChild, &rChild);
463     wChild = rChild.right - rChild.left;
464     hChild = rChild.bottom - rChild.top;
465    
466     GetWindowRect (hwndParent, &rParent);
467     wParent = rParent.right - rParent.left;
468     hParent = rParent.bottom - rParent.top;
469    
470     hdc = GetDC (hwndChild);
471     wScreen = GetDeviceCaps (hdc, HORZRES);
472     hScreen = GetDeviceCaps (hdc, VERTRES);
473     ReleaseDC (hwndChild, hdc);
474     xNew = rParent.left + ((wParent - wChild) /2);
475     if (xNew < 0)
476     xNew = 0;
477     else if ((xNew+wChild) > wScreen)
478     xNew = wScreen - wChild;
479     yNew = rParent.top + ((hParent - hChild) /2);
480     if (yNew < 0)
481     yNew = 0;
482     else if ((yNew+hChild) > hScreen)
483     yNew = hScreen - hChild;
484     if (style == HWND_TOPMOST || style == HWND_NOTOPMOST)
485     flags = SWP_NOMOVE | SWP_NOSIZE;
486     SetWindowPos (hwndChild, style? style : NULL, xNew, yNew, 0, 0, flags);
487     }
488    
489    
490     /* Center the given hwndChild window with no special style. */
491     void
492     center_window (HWND hwndChild, HWND hwndParent)
493     {
494     center_window2 (hwndChild, hwndParent, NULL);
495     }
496 twoaday 121
497    
498     /* Retrieve the product verion of the given file @fname.
499     Format: MAJOR.MINOR.PATCH1.PATCH2
500     Return value: 0 on success. */
501     int
502     get_file_version (const char *fname, WORD *major, WORD *minor,
503     WORD *patch1, WORD *patch2)
504     {
505     VS_FIXEDFILEINFO *inf = {0};
506     char file[MAX_PATH+1] = {0};
507     LPVOID buf, data;
508     DWORD arg;
509     DWORD size;
510     UINT qlen;
511    
512     strncpy (file, fname, MAX_PATH);
513     size = GetFileVersionInfoSize (file, &arg);
514     if (!size)
515     return -1;
516     buf = (LPVOID)new CHAR[size];
517     if (!buf)
518     BUG (NULL);
519     GetFileVersionInfo (file, 0, size, buf);
520    
521     qlen=0;
522     VerQueryValue (buf, "\\", &data, &qlen);
523     if (!qlen) {
524     delete [] (char*)buf;
525     return -1;
526     }
527     inf = (VS_FIXEDFILEINFO*)data;
528    
529     if (major)
530     *major = HIWORD (inf->dwProductVersionMS);
531     if (minor)
532     *minor = LOWORD (inf->dwProductVersionMS);
533     if (patch1)
534     *patch1 = HIWORD (inf->dwProductVersionLS);
535     if (patch2)
536     *patch2 = LOWORD (inf->dwProductVersionLS);
537    
538     delete [] (char*)buf;
539     return 0;
540     }
541    
542    
543 twoaday 129 /* Return date in a format which complies with the
544     system locale settings. */
545     const char*
546     get_locale_date (long tm_t, char *buf, DWORD buflen)
547     {
548     SYSTEMTIME st;
549     struct tm *ptm;
550 twoaday 121
551 twoaday 129 ptm = localtime (&tm_t);
552     st.wYear = (WORD)ptm->tm_year;
553     st.wMonth = (WORD)ptm->tm_mon;
554     st.wDay = (WORD)ptm->tm_mday;
555     st.wYear += 1900;
556     st.wMonth += 1;
557     if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st,
558     NULL, buf, buflen))
559     return NULL;
560     return buf;
561     }
562 twoaday 121
563    
564 twoaday 175 /* Generate a temporary file name by using the users
565     temp path and optionally a name @name provided by the caller.
566     Return value: 0 on success. */
567     int
568     get_temp_name (char *buf, DWORD buflen, const char *name)
569     {
570     char tmp[32];
571    
572     if (!name) {
573     sprintf (tmp, "%08lX", GetTickCount ());
574     name = tmp;
575     }
576 twoaday 190
577     /* in the mobile mode we use a local temp folder
578     with the fixed name 'temp'. */
579     if (mobile_mode_active) {
580     _snprintf (buf, buflen-1, "temp\\%s", name);
581     return 0;
582     }
583    
584 twoaday 175 if (!GetTempPath (buflen - strlen (name) -2, buf)) {
585     log_debug ("GetTempPath() failed ec=%d\n", (int)GetLastError ());
586     return -1;
587     }
588     strcat (buf, name);
589     return 0;
590     }
591    
592    
593 twoaday 129 struct reminder_hd_s {
594     int msecs;
595     HWND dlg;
596     HANDLE hd;
597     };
598 twoaday 121
599    
600 twoaday 129 static DWORD CALLBACK
601     foreground_reminder_thread (void *c)
602     {
603     struct reminder_hd_s *ctx = (struct reminder_hd_s *)c;
604     Sleep (ctx->msecs);
605     SetForegroundWindow (ctx->dlg);
606     CloseHandle (ctx->hd);
607     delete ctx;
608     ExitThread (0);
609 twoaday 121 return 0;
610 twoaday 129 }
611 twoaday 121
612 twoaday 129 /* Try to force the window @dlg to the foreground.
613     On NT5 or later this will not work if the user
614     is working in another window (console for example). */
615     void
616     force_foreground_window (HWND dlg, int msecs)
617     {
618     struct reminder_hd_s *hd;
619     DWORD tid;
620 twoaday 121
621 twoaday 129 hd = new reminder_hd_s;
622     hd->dlg = dlg;
623     hd->msecs = msecs;
624     hd->hd = CreateThread (NULL, 0, foreground_reminder_thread,
625     hd, NULL, &tid);
626     }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26