OSDN Git Service

Show Ignore Sub Menu
[tortoisegit/TortoiseGitJp.git] / Utils / MiscUI / Cursor.h
1 // TortoiseSVN - a Windows shell extension for easy version control\r
2 \r
3 // Copyright (C) 2003-2006 - Stefan Kueng\r
4 \r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 \r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 \r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software Foundation,\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 //\r
19 #pragma once\r
20 \r
21 /**\r
22  * \ingroup Utils\r
23  * Helper class for setting mouse cursors.\n\r
24  * There are two ways of using this class:\r
25  * -# Just declare a CCursor object with the\r
26  *    required cursor. As soon as the object\r
27  *    goes out of scope the previous cursor\r
28  *    is restored.\r
29  *    \code\r
30  *    someMethod()\r
31  *    {\r
32  *      CCursor(IDC_WAIT);\r
33  *      //do something here\r
34  *    }\r
35  *    //now CCursor is out of scope and the default cursor is restored\r
36  *    \endcode\r
37  * -# use the object the usual way. Declare a CCursor object\r
38  *    and use the methods to set the cursors.\r
39  *\r
40  * \remark the class can be used on Win95 and NT4 too, but the\r
41  * hand cursor won't be available.\r
42  */\r
43 class CCursor\r
44 {\r
45 public:\r
46         /**\r
47          * Constructs a CCursor object.\r
48          */\r
49         CCursor(LPCTSTR CursorName)\r
50         {\r
51                 ASSERT(this);\r
52                 m_bInitialized = FALSE;\r
53                 SetCursor(CursorName);\r
54         }\r
55 \r
56         CCursor()\r
57         {\r
58                 ASSERT(this);\r
59                 m_bInitialized = FALSE;\r
60         }\r
61         ~CCursor(void)\r
62         {\r
63                 ASSERT(this);\r
64                 Restore();\r
65         }\r
66         /**\r
67          * Sets a new cursor. If you previously set a new cursor then\r
68          * before setting a second cursor the old one is restored and\r
69          * then the new one is set.\r
70          */\r
71         HCURSOR SetCursor(LPCTSTR CursorName)\r
72         {\r
73                 //first restore possible old cursor before setting new one\r
74                 Restore();\r
75                 //try to load system cursor\r
76                 HCURSOR NewCursor = ::LoadCursor(NULL, CursorName);\r
77                 if(!NewCursor)\r
78                         //try to load application cursor\r
79                         NewCursor = ::LoadCursor(AfxGetResourceHandle(), CursorName);\r
80                 if(NewCursor)\r
81                 {\r
82                         m_hOldCursor = ::SetCursor(NewCursor);\r
83                         m_bInitialized = TRUE;\r
84                 }\r
85                 else\r
86                 {\r
87                         m_bInitialized = FALSE;\r
88                         TRACE("cursor not found!\n");\r
89                 }\r
90                 return m_hOldCursor;\r
91         }\r
92         /**\r
93          * Restores the cursor.\r
94          */\r
95         void Restore()\r
96         {\r
97                 ASSERT(this);\r
98                 if(m_bInitialized)\r
99                 {\r
100                         ::SetCursor(m_hOldCursor);\r
101                         m_hOldCursor = NULL;\r
102                 }\r
103                 m_bInitialized = FALSE;\r
104         }\r
105 \r
106 private:\r
107         HCURSOR m_hOldCursor;\r
108         BOOL m_bInitialized;\r
109 };\r