OSDN Git Service

Add facility for activation of a running application window.
authorKeith Marshall <keithmarshall@users.sourceforge.net>
Mon, 19 Aug 2013 12:40:03 +0000 (13:40 +0100)
committerKeith Marshall <keithmarshall@users.sourceforge.net>
Mon, 19 Aug 2013 12:40:03 +0000 (13:40 +0100)
ChangeLog
Makefile.in
wtkdefs.h [new file with mode: 0644]
wtklite.h
wtkraise.cpp [new file with mode: 0644]

index 81b71a3..46faf95 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,23 @@
 2013-08-19  Keith Marshall  <keithmarshall@users.sourceforge.net>
 
+       Add facility for activation of a running application window.
+
+       * wtkdefs.h: New file; it defines...
+       (EXTERN_C, BEGIN_NAMESPACE, END_NAMESPACE): ...these new macros.
+
+       * wtkraise.cpp: New file; it implements...
+       (RaiseAppWindow): ...this new extern "C" function.
+
+       * wtklite.h (RaiseAppWindow): Declare it; it is designated by...
+       (EXTERN_C): ...this new macro, within WTK namespace demarcated by...
+       (BEGIN_NAMESPACE, END_NAMESPACE): ...this new macro pair.
+
+       * Makefile.in (LIBWTK_OBJECTS): Add wtkraise.$OBJEXT
+       (SRCDIST_FILES): Add wtkdefs.h and wtkraise.cpp
+       (install-headers): Add wtkdefs.h
+
+2013-08-19  Keith Marshall  <keithmarshall@users.sourceforge.net>
+
        Do not track .orig files.
 
        * .hgignore (**.orig): Add pattern.
index 2c59fbb..c8724b5 100644 (file)
@@ -63,7 +63,8 @@ all: libwtklite.a
 #
 LIBWTK_OBJECTS = wtkbase.$(OBJEXT) wtkmain.$(OBJEXT) wndproc.$(OBJEXT) \
   dlgproc.$(OBJEXT) wtkchild.$(OBJEXT) wtkexcept.$(OBJEXT) errtext.$(OBJEXT) \
-  sashctrl.$(OBJEXT) hsashctl.$(OBJEXT) vsashctl.$(OBJEXT) strres.$(OBJEXT)
+  sashctrl.$(OBJEXT) hsashctl.$(OBJEXT) vsashctl.$(OBJEXT) strres.$(OBJEXT) \
+  wtkraise.$(OBJEXT)
 
 libwtklite.a: $(LIBWTK_OBJECTS)
        $(AR) $(ARFLAGS) $@ $^
@@ -103,7 +104,7 @@ install: install-dirs install-headers install-libs
 install-dirs:
        $(MKDIR_P) ${includedir} ${libdir}
 
-install-headers: wtklite.h wtkexcept.h
+install-headers: wtklite.h wtkdefs.h wtkexcept.h
        $(INSTALL_DATA) $^ ${includedir}
 
 install-libs: libwtklite.a
@@ -113,8 +114,9 @@ install-libs: libwtklite.a
 #
 TARNAME = $(PACKAGE)-$(VERSION)-mingw32
 SRCDIST_FILES = ChangeLog configure configure.ac Makefile.in install-sh \
-  wtklite.h wtkexcept.h wtkbase.cpp wtkmain.cpp wtkchild.cpp wndproc.cpp \
-  dlgproc.cpp sashctrl.cpp wtkexcept.cpp errtext.cpp strres.cpp
+  wtklite.h wtkdefs.h wtkexcept.h wtkbase.cpp wtkmain.cpp wtkchild.cpp \
+  wndproc.cpp dlgproc.cpp sashctrl.cpp wtkexcept.cpp errtext.cpp strres.cpp \
+  wtkraise.cpp
 
 dist: srcdist devdist
 
diff --git a/wtkdefs.h b/wtkdefs.h
new file mode 100644 (file)
index 0000000..cdbff58
--- /dev/null
+++ b/wtkdefs.h
@@ -0,0 +1,65 @@
+#ifndef WTKDEFS_H
+/*
+ * wtkdefs.h
+ *
+ * ---------------------------------------------------------------------------
+ *
+ * Implementation of a minimal C++ class framework for use with the
+ * Microsoft Windows Application Programming Interface.
+ *
+ * $Id$
+ *
+ * This header file provides a set of utility macro definitions, which
+ * may be required by other more user visible header files.
+ *
+ * Written by Keith Marshall <keithmarshall@users.sourceforge.net>
+ * Copyright (C) 2013, MinGW.org Project.
+ *
+ * ---------------------------------------------------------------------------
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice, this permission notice, and the following
+ * disclaimer shall be included in all copies or substantial portions of
+ * the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * ---------------------------------------------------------------------------
+ *
+ */
+#define WTKDEFS_H  1
+
+/* Some functions expose an extern "C" interface, (some may even be written
+ * in C), but within C++ clients we subsume them into the WTK namespace; we
+ * provide a set of macros to facilitate this...
+ */
+#ifdef __cplusplus
+/*
+ * ...for the (normal) C++ case...
+ */
+# define EXTERN_C               extern "C"
+# define BEGIN_NAMESPACE(NAME)  namespace NAME {
+# define END_NAMESPACE(NAME)    }
+
+#else
+/* ...while also providing an interface which exposes them globally,
+ * so that they may also be called from C code.
+ */
+# define EXTERN_C              extern
+# define BEGIN_NAMESPACE(NAME)
+# define END_NAMESPACE(NAME)
+#endif
+
+#endif /* WTKDEFS_H: $RCSfile$: end of file */
index 9662350..a7c4768 100644 (file)
--- a/wtklite.h
+++ b/wtklite.h
@@ -13,7 +13,7 @@
  * C++ class framework.
  *
  * Written by Keith Marshall <keithmarshall@users.sourceforge.net>
- * Copyright (C) 2012, MinGW.org Project.
+ * Copyright (C) 2012, 2013, MinGW.org Project.
  *
  * ---------------------------------------------------------------------------
  *
@@ -44,6 +44,7 @@
 #include <stdlib.h>
 #include <windows.h>
 #include "wtkexcept.h"
+#include "wtkdefs.h"
 
 /* This header file is primarily intended to be used only for C++.  However,
  * configure scripts may try to compile it as C, when checking availability;
@@ -304,6 +305,21 @@ namespace WTK
       void SetDisplacementFactor( unsigned long );
   };
 }
-
 #endif /* __cplusplus */
+
+/* We also provide a small collection of functions which, for C++
+ * applications, are considered to live within the WTK namespace,
+ * but may also be invoked from C; to facilitate this, we reopen
+ * the namespace in a C transparent manner...
+ */
+BEGIN_NAMESPACE( WTK )
+  /*
+   * ...which allows us to declare extern "C" function prototypes,
+   * each of which will be directly visible in C, but subsumed into
+   * the namespace when compiling C++.
+   */
+  EXTERN_C int RaiseAppWindow( HINSTANCE, unsigned int );
+
+END_NAMESPACE( WTK )
+
 #endif /* ! WTKLITE_H: $RCSfile$: end of file */
diff --git a/wtkraise.cpp b/wtkraise.cpp
new file mode 100644 (file)
index 0000000..9c37dbc
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * wtkraise.cpp
+ *
+ * ---------------------------------------------------------------------------
+ *
+ * Implementation of a minimal C++ class framework for use with the
+ * Microsoft Windows Application Programming Interface.
+ *
+ * $Id$
+ *
+ * This file provides the implementation for RaiseAppWindow(), a helper
+ * function which checks for any already running instance of the calling
+ * application, and promotes any such existing instance to foreground.
+ *
+ * Written by Keith Marshall <keithmarshall@users.sourceforge.net>
+ * Copyright (C) 2013, MinGW.org Project.
+ *
+ * ---------------------------------------------------------------------------
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice, this permission notice, and the following
+ * disclaimer shall be included in all copies or substantial portions of
+ * the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * ---------------------------------------------------------------------------
+ *
+ */
+#include <wtklite.h>
+
+namespace WTK
+{
+  EXTERN_C int RaiseAppWindow( HINSTANCE Instance, unsigned int ClassID )
+  {
+    /* Helper to search for any running instance of a specified window
+     * class; when one is found, activate it, and bring to foreground.
+     */
+    HWND AppWindow = FindWindow(
+       StringResource( Instance, ClassID ), NULL
+      );
+    if( (AppWindow != NULL) && IsWindow( AppWindow ) )
+    {
+      /* ...and when one is, we identify its active window...
+       */
+      HWND AppPopup = GetLastActivePopup( AppWindow );
+      if( IsWindow( AppPopup ) )
+       AppWindow = AppPopup;
+
+      /* ...bring it to the foreground...
+       */
+      SetForegroundWindow( AppWindow );
+      /*
+       * ...restore it from minimised state, if necessary...
+       */
+      if( IsIconic( AppWindow ) )
+       ShowWindow( AppWindow, SW_RESTORE );
+
+      /* ...and tell caller it may defer execution to this
+       * newly promoted foreground application.
+       */
+      return (int)(true);
+    }
+    return (int)(false);
+  }
+}
+
+/* $RCSfile$: end of file */