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

Annotation of /trunk/Src/wptW32API.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 105 - (hide annotations)
Wed Nov 30 10:22:00 2005 UTC (19 years, 3 months ago) by twoaday
File size: 11521 byte(s)
Several cleanups and improved translation.


1 werner 36 /* wptW32API.cpp - Common W32 API functions
2     * Copyright (C) 2001, 2002, 2003 Timo Schulz
3     *
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     #ifdef HAVE_CONFIG_H
21     #include <config.h>
22     #endif
23    
24     #include <windows.h>
25     #include <stdio.h>
26     #include <sys/types.h>
27     #include <sys/stat.h>
28     #include <shellapi.h>
29     #include <shlobj.h>
30     #include <commctrl.h>
31    
32     #include "wptNLS.h"
33     #include "wptW32API.h"
34     #include "wptErrors.h"
35     #include "wptVersion.h"
36     #include "wptTypes.h"
37    
38    
39     extern "C" void _SHFree (void *p);
40    
41    
42     static void
43     set_menu_text_ext (HMENU menu, int by_pos, int m_uid, const char *text)
44     {
45     MENUITEMINFO mii;
46    
47     memset (&mii, 0, sizeof mii);
48     mii.cbSize = sizeof mii;
49     mii.fMask = MIIM_TYPE;
50     mii.fType = MFT_STRING;
51     mii.dwTypeData = (char *) text;
52     SetMenuItemInfo (menu, m_uid, by_pos? TRUE : FALSE, &mii);
53     }
54    
55    
56     /* Set the text of a menu item @m_uid to @text. */
57     void
58     set_menu_text (HMENU menu, int m_uid, const char *text)
59     {
60     set_menu_text_ext (menu, 0, m_uid, text);
61     }
62    
63    
64     /* Set the text of a menu item with the position @pos to @text. */
65     void
66     set_menu_text_bypos (HMENU menu, int pos, const char *text)
67     {
68     set_menu_text_ext (menu, 1, pos, text);
69     }
70    
71    
72     /* Set the state of a menu item @m_uid to @state. */
73     void
74     set_menu_state (HMENU menu, int m_uid, int state)
75     {
76     MENUITEMINFO mii;
77    
78     memset( &mii, 0, sizeof (mii) );
79     mii.cbSize = sizeof (mii);
80     mii.fMask = MIIM_STATE;
81     mii.fState = state;
82     SetMenuItemInfo (menu, m_uid, FALSE, &mii);
83     }
84    
85    
86 twoaday 77 enum {
87     CDLG_FILE_OPEN = 0,
88     CDLG_FILE_SAVE = 1
89     };
90 werner 36
91     /* Use the common dialog to request a file from the user.
92     id can be either FILE_OPEN or FILE_SAVE.
93     The return value is the file name or NULL if cancel was chosen. */
94     const char *
95     get_filename_dlg (HWND hwnd, int id, const char * title,
96     const char * filter, const char * name)
97     {
98     static char file[512] = "";
99     OPENFILENAME open;
100    
101     if (name && strlen (name) < (sizeof (file)-1))
102     strcpy (file, name);
103     else
104     memset (file, 0, sizeof (file));
105     if (!filter)
106     filter = _("All Files (*.*)\0*.*\0\0");
107     /* XXX: problem with gettext because of the 'artificial'
108     double string termination!. */
109     memset (&open, 0, sizeof (open));
110     open.lStructSize = sizeof (OPENFILENAME);
111     open.hInstance = glob_hinst;
112     open.lpstrTitle = title;
113     open.lpstrFilter = filter;
114     open.hwndOwner = hwnd;
115     open.lpstrFile = file;
116     open.nMaxFile = sizeof (file) - 1;
117 twoaday 77 if (id == CDLG_FILE_OPEN)
118 werner 36 open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
119     else
120     open.Flags = OFN_OVERWRITEPROMPT;
121    
122 twoaday 77 if (id == CDLG_FILE_OPEN && GetOpenFileName (&open))
123 werner 36 return open.lpstrFile;
124 twoaday 77 else if (id == CDLG_FILE_SAVE && GetSaveFileName (&open))
125 werner 36 return open.lpstrFile;
126    
127     return NULL;
128     }
129    
130     const char*
131     get_filesave_dlg (HWND hwnd, const char *title,
132     const char *filter, const char *name)
133     {
134 twoaday 77 return get_filename_dlg (hwnd, CDLG_FILE_SAVE, title, filter, name);
135 werner 36 }
136    
137     const char *
138     get_fileopen_dlg (HWND hwnd, const char *title, const char *filter,
139     const char *name)
140     {
141 twoaday 77 return get_filename_dlg (hwnd, CDLG_FILE_OPEN, title, filter, name);
142 werner 36 }
143    
144    
145     /* Use the common dialog to allow the user to select a folder.
146     The return value is either the folder path or NULL if cancel was chosen. */
147     const char*
148     get_folder_dlg (HWND hwnd, const char * title, const char * name)
149     {
150     static char folder[MAX_PATH+1] = "";
151     BROWSEINFO bi;
152     ITEMIDLIST * il;
153    
154     memset (&bi, 0, sizeof (bi));
155     bi.hwndOwner = hwnd;
156     if (title)
157     bi.lpszTitle = title;
158     if (name && strlen (name) < MAX_PATH-1)
159     strcpy (folder, name);
160     else
161     memset (folder, 0, sizeof (folder));
162     il = SHBrowseForFolder (&bi);
163     if (il) {
164     SHGetPathFromIDList (il, folder);
165     _SHFree (il);
166     return folder;
167     }
168     return NULL;
169     }
170    
171    
172     /* Return the clipboard contents as a string or NULL
173     if the clipboard does not contain text. */
174     char*
175     get_clip_text (HWND hwnd)
176     {
177     HANDLE clipmem;
178     char *cliptxt, *p;
179     int len;
180    
181     if (OpenClipboard (hwnd) == FALSE)
182     return NULL;
183     clipmem = GetClipboardData (CF_TEXT);
184     if (clipmem == NULL) {
185     p = NULL;
186     goto leave;
187     }
188     cliptxt = (char *) GlobalLock (clipmem);
189     if (cliptxt == NULL) {
190     p = NULL;
191     goto leave;
192     }
193    
194     len = strlen (cliptxt);
195     p = new char[len + 1];
196     if (!p)
197     BUG (NULL);
198     memcpy (p, cliptxt, len);
199     p[len] = '\0';
200     GlobalUnlock (clipmem);
201    
202     leave:
203     CloseClipboard ();
204     return p;
205     }
206    
207    
208     /* Set @text as the new clipboard content. */
209     int
210     set_clip_text (HWND hwnd, const char *text, int nbytes)
211     {
212     HANDLE clipmem;
213     int rc = 0;
214     char *p;
215    
216     if (OpenClipboard (hwnd) == FALSE)
217     return WPTERR_CLIP_OPEN;
218     EmptyClipboard ();
219    
220     clipmem = GlobalAlloc (GHND, nbytes + 1);
221     if (clipmem == NULL)
222     BUG (NULL);
223     p = (char *) GlobalLock (clipmem);
224     if (p == NULL) {
225     rc = WPTERR_GENERAL;;
226     goto leave;
227     }
228     memcpy (p, text, nbytes);
229     p[nbytes] = '\0';
230    
231     GlobalUnlock (clipmem);
232     SetClipboardData (CF_TEXT, clipmem);
233    
234     leave:
235     CloseClipboard ();
236     return rc;
237     } /* set_clip_text */
238    
239    
240     /* Append or prepend some text to the clipboard contents.
241     If as_footer = 1, append the text otherwise prepend. */
242     int
243     set_clip_text2 (HWND hwnd, const char *text, int nbytes, int as_footer)
244     {
245     char *p, *new_text;
246    
247     p = get_clip_text (hwnd);
248     if (!p)
249     return WPTERR_CLIP_GET;
250     new_text = new char [strlen (p)+strlen (text)+8];
251     if (!new_text)
252     BUG (0);
253     if (as_footer == 0)
254     sprintf (new_text, "%s\r\n%s\r\n\r\n", text, p);
255     else
256     sprintf (new_text, "%s\n%s\n\n", p, text);
257     set_clip_text (hwnd, new_text, strlen (new_text)+1);
258     free_if_alloc (p);
259     free_if_alloc (new_text);
260     return 0;
261     }
262    
263    
264     /* Make a file name out of the path, the file and an extension. */
265     char*
266     make_filename (const char *path, const char *file, const char *ext)
267     {
268     char *p;
269     size_t size = 0;
270    
271     if( path && *path )
272     size += strlen( path );
273     if( file && *file )
274     size += strlen( file );
275     if( ext && *ext )
276     size += strlen( ext );
277     p = new char[size + 4];
278     memset( p, 0, size );
279     if( path ) {
280     strcat( p, path );
281     if( path[strlen( path ) -1] != '\\' )
282     strcat( p, "\\" );
283     }
284     if( file )
285     strcat( p, file );
286     if( ext ) {
287     strcat( p, "." );
288     strcat( p, ext );
289     }
290     return p;
291     } /* make_filename */
292    
293    
294     /* return 0 if it exists, otherwise >0. */
295     int
296     file_exist_check (const char * fname)
297     {
298     struct stat st;
299     if (stat (fname, &st) == -1)
300     return WPTERR_FILE_EXIST;
301     return 0;
302     }
303    
304    
305     /* Check if the current folder exists.
306     Return 0 for success. */
307     int
308     dir_exist_check (const char *dir)
309     {
310     struct stat statbuf;
311    
312 twoaday 105 if (stat (dir, &statbuf) == -1)
313 werner 36 return WPTERR_GENERAL;
314 twoaday 105 if (statbuf.st_mode & _S_IFDIR)
315 werner 36 return 0;
316     return WPTERR_GENERAL;
317     }
318    
319    
320 twoaday 77 /* Return the file size of the given file @fname. */
321     DWORD
322 werner 36 get_file_size (const char *fname)
323     {
324 twoaday 77 DWORD fsize;
325 werner 36 HANDLE fh;
326    
327 twoaday 77 fh = CreateFile (fname, GENERIC_READ, FILE_SHARE_READ,
328     NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
329     if (fh == INVALID_HANDLE_VALUE)
330 werner 36 return 0;
331 twoaday 77 fsize = GetFileSize (fh, NULL);
332     if (fsize == 0xFFFFFFFF)
333 werner 36 fsize = 0;
334 twoaday 77 CloseHandle (fh);
335 werner 36 return fsize;
336 twoaday 77 }
337 werner 36
338    
339     int
340     init_file_lock( LOCK *ctx, const char *file )
341     {
342    
343     ctx->size = get_file_size( file );
344     ctx->file = m_strdup( file );
345     ctx->fh = CreateFile( file, GENERIC_READ, FILE_SHARE_READ, NULL,
346     OPEN_ALWAYS, 0, NULL );
347     if( ctx->fh == INVALID_HANDLE_VALUE )
348     return WPTERR_GENERAL;
349     if( LockFile( ctx->fh, 0, 0, ctx->size, 0 ) == FALSE ) {
350     CloseHandle( ctx->fh );
351     ctx->fh = INVALID_HANDLE_VALUE;
352     ctx->size = 0;
353     free( ctx->file );
354     return WPTERR_GENERAL;
355     }
356     return 0;
357     } /* init_file_lock */
358    
359    
360     void
361     release_file_lock( LOCK *ctx )
362     {
363     free_if_alloc( ctx->file );
364     ctx->file = NULL;
365     ctx->size = 0;
366     CloseHandle( ctx->fh );
367     } /* release_file_lock */
368    
369    
370     /* Start a dialog with the exception that before it is checked that the
371     dialog is not already openened. */
372     int
373 twoaday 105 dialog_box_param (HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,
374     LPARAM param, LPCTSTR title, int title_id)
375 werner 36 {
376 twoaday 105 if (FindWindowEx (GetDesktopWindow (), NULL, NULL, title))
377 werner 36 return -1;
378 twoaday 105 return DialogBoxParam (hinst, name, parent, fnc, param);
379     }
380 werner 36
381    
382     /* Wrapper for message box which forces the message box into the
383     foreground and it is displayed always on top. */
384     int
385     msg_box (HWND hwnd, const char *text, const char *title, int mode)
386     {
387     mode |= MB_SETFOREGROUND;
388     mode |= MB_TASKMODAL;
389     mode |= MB_TOPMOST;
390     return MessageBox(hwnd, text, title, mode);
391     }
392    
393    
394     void
395     set_active_window( HWND dlg)
396     {
397     activ_hwnd = dlg;
398     } /* set_active_window */
399    
400     void
401     reset_active_window( void )
402     {
403     activ_hwnd = NULL;
404     } /* reset_active_window */
405    
406    
407     static DWORD CALLBACK
408     reminder_thread (void *ctx)
409     {
410     reminder_ctx_s *c = (reminder_ctx_s *)ctx;
411    
412     Sleep( c->msecs );
413     SetForegroundWindow( activ_hwnd );
414    
415     return 0;
416     } /* reminder_thread */
417    
418    
419     HANDLE
420     window_reminder( struct reminder_ctx_s *ctx )
421     {
422     DWORD tid = 0;
423    
424     return CreateThread( NULL, 0, reminder_thread, ctx, 0, &tid );
425     } /* window_reminder */
426    
427    
428     char*
429     m_strdup (const char *str)
430     {
431     char * p = new char[strlen (str) + 1];
432     if (p)
433     strcpy (p, str);
434     return p;
435     } /* m_strdup */
436    
437    
438     /* Center the hwndChild relative to parent.
439     The style param allows to specificy additional styles (like topmost). */
440     void
441     center_window2 (HWND hwndChild, HWND parent, HWND style)
442     {
443     HWND hwndParent;
444     RECT rChild, rParent;
445     HDC hdc;
446     int wChild, hChild, wParent, hParent;
447     int wScreen, hScreen, xNew, yNew;
448     int flags = SWP_NOSIZE | SWP_NOZORDER;
449    
450     hwndParent = parent;
451     if (hwndParent == NULL)
452     hwndParent = GetDesktopWindow ();
453     GetWindowRect (hwndChild, &rChild);
454     wChild = rChild.right - rChild.left;
455     hChild = rChild.bottom - rChild.top;
456    
457     GetWindowRect (hwndParent, &rParent);
458     wParent = rParent.right - rParent.left;
459     hParent = rParent.bottom - rParent.top;
460    
461     hdc = GetDC (hwndChild);
462     wScreen = GetDeviceCaps (hdc, HORZRES);
463     hScreen = GetDeviceCaps (hdc, VERTRES);
464     ReleaseDC (hwndChild, hdc);
465     xNew = rParent.left + ((wParent - wChild) /2);
466     if (xNew < 0)
467     xNew = 0;
468     else if ((xNew+wChild) > wScreen)
469     xNew = wScreen - wChild;
470     yNew = rParent.top + ((hParent - hChild) /2);
471     if (yNew < 0)
472     yNew = 0;
473     else if ((yNew+hChild) > hScreen)
474     yNew = hScreen - hChild;
475     if (style == HWND_TOPMOST || style == HWND_NOTOPMOST)
476     flags = SWP_NOMOVE | SWP_NOSIZE;
477     SetWindowPos (hwndChild, style? style : NULL, xNew, yNew, 0, 0, flags);
478     }
479    
480    
481     /* Center the given hwndChild window with no special style. */
482     void
483     center_window (HWND hwndChild, HWND hwndParent)
484     {
485     center_window2 (hwndChild, hwndParent, NULL);
486     }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26