├── etc
├── README.md
└── 1049.zip
├── odbc-wlx.png
├── README.md
├── LICENSE
└── main.c
/etc/README.md:
--------------------------------------------------------------------------------
1 | These files is a part of Microsoft database engine.
2 |
--------------------------------------------------------------------------------
/etc/1049.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/little-brother/odbc-wlx/HEAD/etc/1049.zip
--------------------------------------------------------------------------------
/odbc-wlx.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/little-brother/odbc-wlx/HEAD/odbc-wlx.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | **odbc-wlx** is a [Total Commander](https://www.ghisler.com/) plugin to view Access (\*.mdb, \*.accdb), Excel (\*.xls, \*.xlsx, \*.xlsb) and Data Source (\*.dsn) files through ODBC interface.
2 | Check [Wiki](https://github.com/little-brother/odbc-wlx/wiki) if you have trouble viewing the files.
3 |
4 | |[**Download the latest version**](https://github.com/little-brother/odbc-wlx/releases/latest/download/odbc-wlx.zip)|
5 | |-------------------------------------------------------------------------------------------|
6 |
7 | 
8 |
9 | ### Features
10 | * Column filters
11 | * Sort data by column click
12 |
13 | If you have any problems, comments or suggestions, check [Wiki](https://github.com/little-brother/odbc-wlx/wiki), create [issue](https://github.com/little-brother/odbc-wlx/issues) or just let me know lb.im@ya.ru.
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 2, June 1991
3 |
4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | Preamble
10 |
11 | The licenses for most software are designed to take away your
12 | freedom to share and change it. By contrast, the GNU General Public
13 | License is intended to guarantee your freedom to share and change free
14 | software--to make sure the software is free for all its users. This
15 | General Public License applies to most of the Free Software
16 | Foundation's software and to any other program whose authors commit to
17 | using it. (Some other Free Software Foundation software is covered by
18 | the GNU Lesser General Public License instead.) You can apply it to
19 | your programs, too.
20 |
21 | When we speak of free software, we are referring to freedom, not
22 | price. Our General Public Licenses are designed to make sure that you
23 | have the freedom to distribute copies of free software (and charge for
24 | this service if you wish), that you receive source code or can get it
25 | if you want it, that you can change the software or use pieces of it
26 | in new free programs; and that you know you can do these things.
27 |
28 | To protect your rights, we need to make restrictions that forbid
29 | anyone to deny you these rights or to ask you to surrender the rights.
30 | These restrictions translate to certain responsibilities for you if you
31 | distribute copies of the software, or if you modify it.
32 |
33 | For example, if you distribute copies of such a program, whether
34 | gratis or for a fee, you must give the recipients all the rights that
35 | you have. You must make sure that they, too, receive or can get the
36 | source code. And you must show them these terms so they know their
37 | rights.
38 |
39 | We protect your rights with two steps: (1) copyright the software, and
40 | (2) offer you this license which gives you legal permission to copy,
41 | distribute and/or modify the software.
42 |
43 | Also, for each author's protection and ours, we want to make certain
44 | that everyone understands that there is no warranty for this free
45 | software. If the software is modified by someone else and passed on, we
46 | want its recipients to know that what they have is not the original, so
47 | that any problems introduced by others will not reflect on the original
48 | authors' reputations.
49 |
50 | Finally, any free program is threatened constantly by software
51 | patents. We wish to avoid the danger that redistributors of a free
52 | program will individually obtain patent licenses, in effect making the
53 | program proprietary. To prevent this, we have made it clear that any
54 | patent must be licensed for everyone's free use or not licensed at all.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | GNU GENERAL PUBLIC LICENSE
60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61 |
62 | 0. This License applies to any program or other work which contains
63 | a notice placed by the copyright holder saying it may be distributed
64 | under the terms of this General Public License. The "Program", below,
65 | refers to any such program or work, and a "work based on the Program"
66 | means either the Program or any derivative work under copyright law:
67 | that is to say, a work containing the Program or a portion of it,
68 | either verbatim or with modifications and/or translated into another
69 | language. (Hereinafter, translation is included without limitation in
70 | the term "modification".) Each licensee is addressed as "you".
71 |
72 | Activities other than copying, distribution and modification are not
73 | covered by this License; they are outside its scope. The act of
74 | running the Program is not restricted, and the output from the Program
75 | is covered only if its contents constitute a work based on the
76 | Program (independent of having been made by running the Program).
77 | Whether that is true depends on what the Program does.
78 |
79 | 1. You may copy and distribute verbatim copies of the Program's
80 | source code as you receive it, in any medium, provided that you
81 | conspicuously and appropriately publish on each copy an appropriate
82 | copyright notice and disclaimer of warranty; keep intact all the
83 | notices that refer to this License and to the absence of any warranty;
84 | and give any other recipients of the Program a copy of this License
85 | along with the Program.
86 |
87 | You may charge a fee for the physical act of transferring a copy, and
88 | you may at your option offer warranty protection in exchange for a fee.
89 |
90 | 2. You may modify your copy or copies of the Program or any portion
91 | of it, thus forming a work based on the Program, and copy and
92 | distribute such modifications or work under the terms of Section 1
93 | above, provided that you also meet all of these conditions:
94 |
95 | a) You must cause the modified files to carry prominent notices
96 | stating that you changed the files and the date of any change.
97 |
98 | b) You must cause any work that you distribute or publish, that in
99 | whole or in part contains or is derived from the Program or any
100 | part thereof, to be licensed as a whole at no charge to all third
101 | parties under the terms of this License.
102 |
103 | c) If the modified program normally reads commands interactively
104 | when run, you must cause it, when started running for such
105 | interactive use in the most ordinary way, to print or display an
106 | announcement including an appropriate copyright notice and a
107 | notice that there is no warranty (or else, saying that you provide
108 | a warranty) and that users may redistribute the program under
109 | these conditions, and telling the user how to view a copy of this
110 | License. (Exception: if the Program itself is interactive but
111 | does not normally print such an announcement, your work based on
112 | the Program is not required to print an announcement.)
113 |
114 | These requirements apply to the modified work as a whole. If
115 | identifiable sections of that work are not derived from the Program,
116 | and can be reasonably considered independent and separate works in
117 | themselves, then this License, and its terms, do not apply to those
118 | sections when you distribute them as separate works. But when you
119 | distribute the same sections as part of a whole which is a work based
120 | on the Program, the distribution of the whole must be on the terms of
121 | this License, whose permissions for other licensees extend to the
122 | entire whole, and thus to each and every part regardless of who wrote it.
123 |
124 | Thus, it is not the intent of this section to claim rights or contest
125 | your rights to work written entirely by you; rather, the intent is to
126 | exercise the right to control the distribution of derivative or
127 | collective works based on the Program.
128 |
129 | In addition, mere aggregation of another work not based on the Program
130 | with the Program (or with a work based on the Program) on a volume of
131 | a storage or distribution medium does not bring the other work under
132 | the scope of this License.
133 |
134 | 3. You may copy and distribute the Program (or a work based on it,
135 | under Section 2) in object code or executable form under the terms of
136 | Sections 1 and 2 above provided that you also do one of the following:
137 |
138 | a) Accompany it with the complete corresponding machine-readable
139 | source code, which must be distributed under the terms of Sections
140 | 1 and 2 above on a medium customarily used for software interchange; or,
141 |
142 | b) Accompany it with a written offer, valid for at least three
143 | years, to give any third party, for a charge no more than your
144 | cost of physically performing source distribution, a complete
145 | machine-readable copy of the corresponding source code, to be
146 | distributed under the terms of Sections 1 and 2 above on a medium
147 | customarily used for software interchange; or,
148 |
149 | c) Accompany it with the information you received as to the offer
150 | to distribute corresponding source code. (This alternative is
151 | allowed only for noncommercial distribution and only if you
152 | received the program in object code or executable form with such
153 | an offer, in accord with Subsection b above.)
154 |
155 | The source code for a work means the preferred form of the work for
156 | making modifications to it. For an executable work, complete source
157 | code means all the source code for all modules it contains, plus any
158 | associated interface definition files, plus the scripts used to
159 | control compilation and installation of the executable. However, as a
160 | special exception, the source code distributed need not include
161 | anything that is normally distributed (in either source or binary
162 | form) with the major components (compiler, kernel, and so on) of the
163 | operating system on which the executable runs, unless that component
164 | itself accompanies the executable.
165 |
166 | If distribution of executable or object code is made by offering
167 | access to copy from a designated place, then offering equivalent
168 | access to copy the source code from the same place counts as
169 | distribution of the source code, even though third parties are not
170 | compelled to copy the source along with the object code.
171 |
172 | 4. You may not copy, modify, sublicense, or distribute the Program
173 | except as expressly provided under this License. Any attempt
174 | otherwise to copy, modify, sublicense or distribute the Program is
175 | void, and will automatically terminate your rights under this License.
176 | However, parties who have received copies, or rights, from you under
177 | this License will not have their licenses terminated so long as such
178 | parties remain in full compliance.
179 |
180 | 5. You are not required to accept this License, since you have not
181 | signed it. However, nothing else grants you permission to modify or
182 | distribute the Program or its derivative works. These actions are
183 | prohibited by law if you do not accept this License. Therefore, by
184 | modifying or distributing the Program (or any work based on the
185 | Program), you indicate your acceptance of this License to do so, and
186 | all its terms and conditions for copying, distributing or modifying
187 | the Program or works based on it.
188 |
189 | 6. Each time you redistribute the Program (or any work based on the
190 | Program), the recipient automatically receives a license from the
191 | original licensor to copy, distribute or modify the Program subject to
192 | these terms and conditions. You may not impose any further
193 | restrictions on the recipients' exercise of the rights granted herein.
194 | You are not responsible for enforcing compliance by third parties to
195 | this License.
196 |
197 | 7. If, as a consequence of a court judgment or allegation of patent
198 | infringement or for any other reason (not limited to patent issues),
199 | conditions are imposed on you (whether by court order, agreement or
200 | otherwise) that contradict the conditions of this License, they do not
201 | excuse you from the conditions of this License. If you cannot
202 | distribute so as to satisfy simultaneously your obligations under this
203 | License and any other pertinent obligations, then as a consequence you
204 | may not distribute the Program at all. For example, if a patent
205 | license would not permit royalty-free redistribution of the Program by
206 | all those who receive copies directly or indirectly through you, then
207 | the only way you could satisfy both it and this License would be to
208 | refrain entirely from distribution of the Program.
209 |
210 | If any portion of this section is held invalid or unenforceable under
211 | any particular circumstance, the balance of the section is intended to
212 | apply and the section as a whole is intended to apply in other
213 | circumstances.
214 |
215 | It is not the purpose of this section to induce you to infringe any
216 | patents or other property right claims or to contest validity of any
217 | such claims; this section has the sole purpose of protecting the
218 | integrity of the free software distribution system, which is
219 | implemented by public license practices. Many people have made
220 | generous contributions to the wide range of software distributed
221 | through that system in reliance on consistent application of that
222 | system; it is up to the author/donor to decide if he or she is willing
223 | to distribute software through any other system and a licensee cannot
224 | impose that choice.
225 |
226 | This section is intended to make thoroughly clear what is believed to
227 | be a consequence of the rest of this License.
228 |
229 | 8. If the distribution and/or use of the Program is restricted in
230 | certain countries either by patents or by copyrighted interfaces, the
231 | original copyright holder who places the Program under this License
232 | may add an explicit geographical distribution limitation excluding
233 | those countries, so that distribution is permitted only in or among
234 | countries not thus excluded. In such case, this License incorporates
235 | the limitation as if written in the body of this License.
236 |
237 | 9. The Free Software Foundation may publish revised and/or new versions
238 | of the General Public License from time to time. Such new versions will
239 | be similar in spirit to the present version, but may differ in detail to
240 | address new problems or concerns.
241 |
242 | Each version is given a distinguishing version number. If the Program
243 | specifies a version number of this License which applies to it and "any
244 | later version", you have the option of following the terms and conditions
245 | either of that version or of any later version published by the Free
246 | Software Foundation. If the Program does not specify a version number of
247 | this License, you may choose any version ever published by the Free Software
248 | Foundation.
249 |
250 | 10. If you wish to incorporate parts of the Program into other free
251 | programs whose distribution conditions are different, write to the author
252 | to ask for permission. For software which is copyrighted by the Free
253 | Software Foundation, write to the Free Software Foundation; we sometimes
254 | make exceptions for this. Our decision will be guided by the two goals
255 | of preserving the free status of all derivatives of our free software and
256 | of promoting the sharing and reuse of software generally.
257 |
258 | NO WARRANTY
259 |
260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268 | REPAIR OR CORRECTION.
269 |
270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278 | POSSIBILITY OF SUCH DAMAGES.
279 |
280 | END OF TERMS AND CONDITIONS
281 |
282 | How to Apply These Terms to Your New Programs
283 |
284 | If you develop a new program, and you want it to be of the greatest
285 | possible use to the public, the best way to achieve this is to make it
286 | free software which everyone can redistribute and change under these terms.
287 |
288 | To do so, attach the following notices to the program. It is safest
289 | to attach them to the start of each source file to most effectively
290 | convey the exclusion of warranty; and each file should have at least
291 | the "copyright" line and a pointer to where the full notice is found.
292 |
293 |
294 | Copyright (C)
295 |
296 | This program is free software; you can redistribute it and/or modify
297 | it under the terms of the GNU General Public License as published by
298 | the Free Software Foundation; either version 2 of the License, or
299 | (at your option) any later version.
300 |
301 | This program is distributed in the hope that it will be useful,
302 | but WITHOUT ANY WARRANTY; without even the implied warranty of
303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304 | GNU General Public License for more details.
305 |
306 | You should have received a copy of the GNU General Public License along
307 | with this program; if not, write to the Free Software Foundation, Inc.,
308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309 |
310 | Also add information on how to contact you by electronic and paper mail.
311 |
312 | If the program is interactive, make it output a short notice like this
313 | when it starts in an interactive mode:
314 |
315 | Gnomovision version 69, Copyright (C) year name of author
316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317 | This is free software, and you are welcome to redistribute it
318 | under certain conditions; type `show c' for details.
319 |
320 | The hypothetical commands `show w' and `show c' should show the appropriate
321 | parts of the General Public License. Of course, the commands you use may
322 | be called something other than `show w' and `show c'; they could even be
323 | mouse-clicks or menu items--whatever suits your program.
324 |
325 | You should also get your employer (if you work as a programmer) or your
326 | school, if any, to sign a "copyright disclaimer" for the program, if
327 | necessary. Here is a sample; alter the names:
328 |
329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330 | `Gnomovision' (which makes passes at compilers) written by James Hacker.
331 |
332 | , 1 April 1989
333 | Ty Coon, President of Vice
334 |
335 | This General Public License does not permit incorporating your program into
336 | proprietary programs. If your program is a subroutine library, you may
337 | consider it more useful to permit linking proprietary applications with the
338 | library. If this is what you want to do, use the GNU Lesser General
339 | Public License instead of this License.
--------------------------------------------------------------------------------
/main.c:
--------------------------------------------------------------------------------
1 | #define UNICODE
2 | #define _UNICODE
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 |
15 | #include
16 | #include
17 | #include
18 |
19 | #define LVS_EX_AUTOSIZECOLUMNS 0x10000000
20 |
21 | #define WMU_UPDATE_GRID WM_USER + 1
22 | #define WMU_UPDATE_CACHE WM_USER + 2
23 | #define WMU_UPDATE_FILTER_SIZE WM_USER + 3
24 | #define WMU_SET_HEADER_FILTERS WM_USER + 4
25 | #define WMU_AUTO_COLUMN_SIZE WM_USER + 5
26 | #define WMU_SET_CURRENT_CELL WM_USER + 6
27 | #define WMU_RESET_CACHE WM_USER + 7
28 | #define WMU_SET_FONT WM_USER + 8
29 | #define WMU_SET_THEME WM_USER + 9
30 | #define WMU_HIDE_COLUMN WM_USER + 10
31 | #define WMU_SHOW_COLUMNS WM_USER + 11
32 | #define WMU_SORT_COLUMN WM_USER + 12
33 | #define WMU_HOT_KEYS WM_USER + 13
34 | #define WMU_HOT_CHARS WM_USER + 14
35 |
36 |
37 | #define IDC_MAIN 100
38 | #define IDC_TABLELIST 101
39 | #define IDC_GRID 102
40 | #define IDC_STATUSBAR 103
41 | #define IDC_HEADER_EDIT 1000
42 |
43 | #define IDM_COPY_CELL 5000
44 | #define IDM_COPY_ROWS 5001
45 | #define IDM_COPY_COLUMN 5002
46 | #define IDM_FILTER_ROW 5003
47 | #define IDM_HEADER_ROW 5004
48 | #define IDM_DARK_THEME 5005
49 | #define IDM_HIDE_COLUMN 5020
50 |
51 | #define SB_VERSION 0
52 | #define SB_TABLE_COUNT 1
53 | #define SB_VIEW_COUNT 2
54 | #define SB_TYPE 3
55 | #define SB_ROW_COUNT 4
56 | #define SB_CURRENT_CELL 5
57 | #define SB_AUXILIARY 6
58 |
59 | #define SPLITTER_WIDTH 5
60 | #define MAX_TEXT_LENGTH 32000
61 | #define MAX_DATA_LENGTH 32000
62 | #define MAX_TABLE_LENGTH 2000
63 | #define MAX_COLUMN_LENGTH 2000
64 | #define MAX_ERROR_LENGTH 2000
65 |
66 | #define ODBC_UNKNOWN 0
67 | #define ODBC_ACCESS 1
68 | #define ODBC_EXCEL 2
69 | #define ODBC_EXCELX 3
70 |
71 | #define APP_NAME TEXT("odbc-wlx")
72 | #define APP_VERSION TEXT("1.0.6")
73 |
74 | #define LCS_FINDFIRST 1
75 | #define LCS_MATCHCASE 2
76 | #define LCS_WHOLEWORDS 4
77 | #define LCS_BACKWARDS 8
78 |
79 | typedef struct {
80 | int size;
81 | DWORD PluginInterfaceVersionLow;
82 | DWORD PluginInterfaceVersionHi;
83 | char DefaultIniName[MAX_PATH];
84 | } ListDefaultParamStruct;
85 |
86 | static TCHAR iniPath[MAX_PATH] = {0};
87 |
88 | LRESULT CALLBACK cbNewMain (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
89 | LRESULT CALLBACK cbHotKey(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
90 | LRESULT CALLBACK cbNewHeader(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
91 | LRESULT CALLBACK cbNewFilterEdit (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
92 |
93 | HWND getMainWindow(HWND hWnd);
94 | void setStoredValue(TCHAR* name, int value);
95 | int getStoredValue(TCHAR* name, int defValue);
96 | TCHAR* getStoredString(TCHAR* name, TCHAR* defValue);
97 | int CALLBACK cbEnumTabStopChildren (HWND hWnd, LPARAM lParam);
98 | TCHAR* utf8to16(const char* in);
99 | char* utf16to8(const TCHAR* in);
100 | int findString(TCHAR* text, TCHAR* word, BOOL isMatchCase, BOOL isWholeWords);
101 | TCHAR* extractUrl(TCHAR* data);
102 | void setClipboardText(const TCHAR* text);
103 | BOOL isNumber(TCHAR* val);
104 | int ListView_AddColumn(HWND hListWnd, TCHAR* colName, int fmt);
105 | int Header_GetItemText(HWND hWnd, int i, TCHAR* pszText, int cchTextMax);
106 | void Menu_SetItemState(HMENU hMenu, UINT wID, UINT fState);
107 |
108 | BOOL APIENTRY DllMain (HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
109 | if (ul_reason_for_call == DLL_PROCESS_ATTACH && iniPath[0] == 0) {
110 | TCHAR path[MAX_PATH + 1] = {0};
111 | GetModuleFileName(hModule, path, MAX_PATH);
112 | TCHAR* dot = _tcsrchr(path, TEXT('.'));
113 | _tcsncpy(dot, TEXT(".ini"), 5);
114 | if (_taccess(path, 0) == 0)
115 | _tcscpy(iniPath, path);
116 | }
117 | return TRUE;
118 | }
119 |
120 | void __stdcall ListGetDetectString(char* DetectString, int maxlen) {
121 | snprintf(DetectString, maxlen, "MULTIMEDIA & (ext=\"MDB\" | ext=\"XLS\" | ext=\"XLSX\" | ext=\"XLSB\" | ext=\"XLSM\" | ext=\"DSN\")");
122 | }
123 |
124 | void __stdcall ListSetDefaultParams(ListDefaultParamStruct* dps) {
125 | if (iniPath[0] == 0) {
126 | DWORD size = MultiByteToWideChar(CP_ACP, 0, dps->DefaultIniName, -1, NULL, 0);
127 | MultiByteToWideChar(CP_ACP, 0, dps->DefaultIniName, -1, iniPath, size);
128 | }
129 | }
130 |
131 | int __stdcall ListSearchTextW(HWND hWnd, TCHAR* searchString, int searchParameter) {
132 | HWND hGridWnd = GetDlgItem(hWnd, IDC_GRID);
133 | HWND hStatusWnd = GetDlgItem(hWnd, IDC_STATUSBAR);
134 |
135 | TCHAR*** cache = (TCHAR***)GetProp(hWnd, TEXT("CACHE"));
136 | int rowCount = *(int*)GetProp(hWnd, TEXT("ROWCOUNT"));
137 | int colCount = Header_GetItemCount(ListView_GetHeader(hGridWnd));
138 |
139 | BOOL isFindFirst = searchParameter & LCS_FINDFIRST;
140 | BOOL isBackward = searchParameter & LCS_BACKWARDS;
141 | BOOL isMatchCase = searchParameter & LCS_MATCHCASE;
142 | BOOL isWholeWords = searchParameter & LCS_WHOLEWORDS;
143 |
144 | if (isFindFirst) {
145 | *(int*)GetProp(hWnd, TEXT("CURRENTCOLNO")) = 0;
146 | *(int*)GetProp(hWnd, TEXT("SEARCHCELLPOS")) = 0;
147 | *(int*)GetProp(hWnd, TEXT("CURRENTROWNO")) = isBackward ? rowCount - 1 : 0;
148 | }
149 |
150 | int rowNo = *(int*)GetProp(hWnd, TEXT("CURRENTROWNO"));
151 | int colNo = *(int*)GetProp(hWnd, TEXT("CURRENTCOLNO"));
152 | int *pStartPos = (int*)GetProp(hWnd, TEXT("SEARCHCELLPOS"));
153 | rowNo = rowNo == -1 || rowNo >= rowCount ? 0 : rowNo;
154 | colNo = colNo == -1 || colNo >= colCount ? 0 : colNo;
155 |
156 | int pos = -1;
157 | do {
158 | for (; (pos == -1) && colNo < colCount; colNo++) {
159 | pos = findString(cache[rowNo][colNo] + *pStartPos, searchString, isMatchCase, isWholeWords);
160 | if (pos != -1)
161 | pos += *pStartPos;
162 | *pStartPos = pos == -1 ? 0 : pos + *pStartPos + _tcslen(searchString);
163 | }
164 | colNo = pos != -1 ? colNo - 1 : 0;
165 | rowNo += pos != -1 ? 0 : isBackward ? -1 : 1;
166 | } while ((pos == -1) && (isBackward ? rowNo > 0 : rowNo < rowCount));
167 | ListView_SetItemState(hGridWnd, -1, 0, LVIS_SELECTED | LVIS_FOCUSED);
168 |
169 | TCHAR buf[256] = {0};
170 | if (pos != -1) {
171 | ListView_EnsureVisible(hGridWnd, rowNo, FALSE);
172 | ListView_SetItemState(hGridWnd, rowNo, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
173 |
174 | TCHAR* val = cache[rowNo][colNo];
175 | int len = _tcslen(searchString);
176 | _sntprintf(buf, 255, TEXT("%ls%.*ls%ls"),
177 | pos > 0 ? TEXT("...") : TEXT(""),
178 | len + pos + 10, val + pos,
179 | _tcslen(val + pos + len) > 10 ? TEXT("...") : TEXT(""));
180 | SendMessage(hWnd, WMU_SET_CURRENT_CELL, rowNo, colNo);
181 | } else {
182 | MessageBox(hWnd, searchString, TEXT("Not found:"), MB_OK);
183 | }
184 | SendMessage(hStatusWnd, SB_SETTEXT, SB_AUXILIARY, (LPARAM)buf);
185 | SetFocus(hGridWnd);
186 |
187 | return 0;
188 | }
189 |
190 | int __stdcall ListSearchText(HWND hWnd, char* searchString, int searchParameter) {
191 | DWORD len = MultiByteToWideChar(CP_ACP, 0, searchString, -1, NULL, 0);
192 | TCHAR* searchString16 = (TCHAR*)calloc (len, sizeof (TCHAR));
193 | MultiByteToWideChar(CP_ACP, 0, searchString, -1, searchString16, len);
194 | int rc = ListSearchTextW(hWnd, searchString16, searchParameter);
195 | free(searchString16);
196 | return rc;
197 | }
198 |
199 | HWND APIENTRY ListLoadW (HWND hListerWnd, TCHAR* fileToLoad, int showFlags) {
200 | TCHAR* fileext = _tcsrchr(fileToLoad, TEXT('.'));
201 | _tcslwr(fileext);
202 |
203 | TCHAR* dbpath = calloc(MAX_PATH, sizeof(TCHAR));
204 | _tcsncpy(dbpath, fileToLoad, MAX_PATH);
205 |
206 | int odbcType = ODBC_UNKNOWN;
207 | TCHAR connectionString[MAX_TEXT_LENGTH] = {0};
208 | TCHAR connectionString2[MAX_TEXT_LENGTH] = {0};
209 | if (_tcscmp(fileext, TEXT(".mdb")) == 0 || _tcscmp(fileext, TEXT(".accdb")) == 0) {
210 | _sntprintf(connectionString, MAX_TEXT_LENGTH, TEXT("Driver={Microsoft Access Driver (*.mdb)};Dbq=%ls;Uid=Admin;Pwd=;ReadOnly=1;"), fileToLoad);
211 | _sntprintf(connectionString2, MAX_TEXT_LENGTH, TEXT("Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=%ls;Uid=Admin;Pwd=;ReadOnly=1;"), fileToLoad);
212 | odbcType = ODBC_ACCESS;
213 | } else if (_tcscmp(fileext, TEXT(".xls")) == 0) {
214 | _sntprintf(connectionString, MAX_TEXT_LENGTH, TEXT("Driver={Microsoft Excel Driver (*.xls)};Dbq=%ls;ReadOnly=1;"), fileToLoad);
215 | _sntprintf(connectionString2, MAX_TEXT_LENGTH, TEXT("Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};Dbq=%ls;ReadOnly=1;"), fileToLoad);
216 | odbcType = ODBC_EXCEL;
217 | } else if (_tcscmp(fileext, TEXT(".xlsx")) == 0 || _tcscmp(fileext, TEXT(".xlsb")) == 0 || _tcscmp(fileext, TEXT(".xlsm")) == 0) {
218 | _sntprintf(connectionString, MAX_TEXT_LENGTH, TEXT("Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};Dbq=%ls;ReadOnly=1;"), fileToLoad);
219 | _sntprintf(connectionString2, MAX_TEXT_LENGTH, TEXT("Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};Dbq=%ls;ReadOnly=1;"), fileToLoad);
220 | odbcType = ODBC_EXCELX;
221 | } else if (_tcscmp(fileext, TEXT(".dsn")) == 0) {
222 | TCHAR buf[32000];
223 |
224 | int len = GetPrivateProfileString(TEXT("ODBC"), NULL, NULL, buf, 32000, fileToLoad);
225 | int start = 0;
226 | for (int i = 0; i < len; i++) {
227 | if (buf[i] != 0)
228 | continue;
229 |
230 | TCHAR key[i - start + 1];
231 | _tcsncpy(key, buf + start, i - start + 1);
232 | TCHAR value[1024];
233 | GetPrivateProfileString(TEXT("ODBC"), key, NULL, value, 1024, fileToLoad);
234 | TCHAR pair[2000];
235 | BOOL isQ = _tcschr(value, TEXT(' ')) != 0;
236 | _sntprintf(pair, 2000, TEXT("%ls=%ls%ls%ls;"), key, isQ ? TEXT("{") : TEXT(""), value, isQ ? TEXT("}") : TEXT(""));
237 | _tcscat(connectionString, pair);
238 |
239 | start = i + 1;
240 |
241 | _tcslwr(key);
242 | _tcslwr(value);
243 | if (_tcscmp(key, TEXT("driver")) == 0)
244 | odbcType = _tcsstr(value, TEXT("*.mdb")) ? ODBC_ACCESS :
245 | _tcsstr(value, TEXT("*.xls")) ? ODBC_EXCEL :
246 | _tcsstr(value, TEXT("*.xlsx")) ? ODBC_EXCELX :
247 | ODBC_UNKNOWN;
248 | if (_tcscmp(key, TEXT("dbq")) == 0)
249 | _tcsncpy(dbpath, value, MAX_PATH);
250 | }
251 | }
252 | SQLHANDLE hEnv = 0;
253 | SQLHANDLE hConn = 0;
254 | SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv);
255 | SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);
256 | SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hConn);
257 |
258 | if (!hEnv || !hConn)
259 | return 0;
260 |
261 | if (SQL_ERROR == SQLDriverConnect(hConn, NULL, connectionString, _tcslen(connectionString), 0, 0, NULL, SQL_DRIVER_NOPROMPT) &&
262 | SQL_ERROR == SQLDriverConnect(hConn, NULL, connectionString2, _tcslen(connectionString2), 0, 0, NULL, SQL_DRIVER_NOPROMPT)) {
263 | MessageBox(hListerWnd, TEXT("Can't connect to database"), NULL, MB_OK);
264 | SQLFreeHandle(SQL_HANDLE_DBC, hConn);
265 | SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
266 | free(dbpath);
267 | return 0;
268 | }
269 |
270 | INITCOMMONCONTROLSEX icex;
271 | icex.dwSize = sizeof(icex);
272 | icex.dwICC = ICC_LISTVIEW_CLASSES;
273 | InitCommonControlsEx(&icex);
274 |
275 | setlocale(LC_CTYPE, "");
276 |
277 | BOOL isStandalone = GetParent(hListerWnd) == HWND_DESKTOP;
278 | HWND hMainWnd = CreateWindow(WC_STATIC, APP_NAME, WS_CHILD | (isStandalone ? SS_SUNKEN : 0),
279 | 0, 0, 100, 100, hListerWnd, (HMENU)IDC_MAIN, GetModuleHandle(0), NULL);
280 |
281 | SetProp(hMainWnd, TEXT("WNDPROC"), (HANDLE)SetWindowLongPtr(hMainWnd, GWLP_WNDPROC, (LONG_PTR)&cbNewMain));
282 | SetProp(hMainWnd, TEXT("FILTERROW"), calloc(1, sizeof(int)));
283 | SetProp(hMainWnd, TEXT("HEADERROW"), calloc(1, sizeof(int)));
284 | SetProp(hMainWnd, TEXT("CACHE"), 0);
285 | SetProp(hMainWnd, TEXT("ORDERBY"), calloc(1, sizeof(int)));
286 | SetProp(hMainWnd, TEXT("DBPATH"), dbpath);
287 | SetProp(hMainWnd, TEXT("TABLENAME"), calloc(MAX_TABLE_LENGTH, sizeof(TCHAR)));
288 | SetProp(hMainWnd, TEXT("WHERE"), calloc(MAX_TEXT_LENGTH, sizeof(TCHAR)));
289 | SetProp(hMainWnd, TEXT("ROWCOUNT"), calloc(1, sizeof(int)));
290 | SetProp(hMainWnd, TEXT("TOTALROWCOUNT"), calloc(1, sizeof(int)));
291 | SetProp(hMainWnd, TEXT("DBENV"), hEnv);
292 | SetProp(hMainWnd, TEXT("DB"), hConn);
293 | SetProp(hMainWnd, TEXT("CURRENTROWNO"), calloc(1, sizeof(int)));
294 | SetProp(hMainWnd, TEXT("CURRENTCOLNO"), calloc(1, sizeof(int)));
295 | SetProp(hMainWnd, TEXT("SEARCHCELLPOS"), calloc(1, sizeof(int)));
296 | SetProp(hMainWnd, TEXT("ODBCTYPE"), calloc(1, sizeof(int)));
297 | SetProp(hMainWnd, TEXT("SPLITTERPOSITION"), calloc(1, sizeof(int)));
298 | SetProp(hMainWnd, TEXT("FONT"), 0);
299 | SetProp(hMainWnd, TEXT("FONTFAMILY"), getStoredString(TEXT("font"), TEXT("Arial")));
300 | SetProp(hMainWnd, TEXT("FONTSIZE"), calloc(1, sizeof(int)));
301 | SetProp(hMainWnd, TEXT("FILTERALIGN"), calloc(1, sizeof(int)));
302 |
303 | SetProp(hMainWnd, TEXT("DARKTHEME"), calloc(1, sizeof(int)));
304 | SetProp(hMainWnd, TEXT("TEXTCOLOR"), calloc(1, sizeof(int)));
305 | SetProp(hMainWnd, TEXT("BACKCOLOR"), calloc(1, sizeof(int)));
306 | SetProp(hMainWnd, TEXT("BACKCOLOR2"), calloc(1, sizeof(int)));
307 | SetProp(hMainWnd, TEXT("FILTERTEXTCOLOR"), calloc(1, sizeof(int)));
308 | SetProp(hMainWnd, TEXT("FILTERBACKCOLOR"), calloc(1, sizeof(int)));
309 | SetProp(hMainWnd, TEXT("CURRENTCELLCOLOR"), calloc(1, sizeof(int)));
310 | SetProp(hMainWnd, TEXT("SELECTIONTEXTCOLOR"), calloc(1, sizeof(int)));
311 | SetProp(hMainWnd, TEXT("SELECTIONBACKCOLOR"), calloc(1, sizeof(int)));
312 | SetProp(hMainWnd, TEXT("SPLITTERCOLOR"), calloc(1, sizeof(int)));
313 |
314 | *(int*)GetProp(hMainWnd, TEXT("HEADERROW")) = getStoredValue(TEXT("header-row"), 1);
315 | *(int*)GetProp(hMainWnd, TEXT("SPLITTERPOSITION")) = getStoredValue(TEXT("splitter-position"), 200);
316 | *(int*)GetProp(hMainWnd, TEXT("FONTSIZE")) = getStoredValue(TEXT("font-size"), 16);
317 | *(int*)GetProp(hMainWnd, TEXT("FILTERROW")) = getStoredValue(TEXT("filter-row"), 1);
318 | *(int*)GetProp(hMainWnd, TEXT("ODBCTYPE")) = odbcType;
319 | *(int*)GetProp(hMainWnd, TEXT("DARKTHEME")) = getStoredValue(TEXT("dark-theme"), 0);
320 | *(int*)GetProp(hMainWnd, TEXT("FILTERALIGN")) = getStoredValue(TEXT("filter-align"), 0);
321 |
322 | HWND hStatusWnd = CreateStatusWindow(WS_CHILD | WS_VISIBLE | (isStandalone ? SBARS_SIZEGRIP : 0), NULL, hMainWnd, IDC_STATUSBAR);
323 | HDC hDC = GetDC(hMainWnd);
324 | float z = GetDeviceCaps(hDC, LOGPIXELSX) / 96.0; // 96 = 100%, 120 = 125%, 144 = 150%
325 | ReleaseDC(hMainWnd, hDC);
326 | int sizes[7] = {35 * z, 110 * z, 180 * z, 225 * z, 420 * z, 500 * z, -1};
327 | SendMessage(hStatusWnd, SB_SETPARTS, 7, (LPARAM)&sizes);
328 |
329 | HWND hListWnd = CreateWindow(TEXT("LISTBOX"), NULL, WS_CHILD | WS_VISIBLE | LBS_NOTIFY | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP | WS_HSCROLL | LBS_SORT,
330 | 0, 0, 100, 100, hMainWnd, (HMENU)IDC_TABLELIST, GetModuleHandle(0), NULL);
331 | SetProp(hListWnd, TEXT("WNDPROC"), (HANDLE)SetWindowLongPtr(hListWnd, GWLP_WNDPROC, (LONG_PTR)cbHotKey));
332 |
333 | HWND hGridWnd = CreateWindow(WC_LISTVIEW, NULL, WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SHOWSELALWAYS | LVS_OWNERDATA | WS_TABSTOP,
334 | 205, 0, 100, 100, hMainWnd, (HMENU)IDC_GRID, GetModuleHandle(0), NULL);
335 |
336 | int noLines = getStoredValue(TEXT("disable-grid-lines"), 0);
337 | ListView_SetExtendedListViewStyle(hGridWnd, LVS_EX_FULLROWSELECT | LVS_EX_DOUBLEBUFFER | (noLines ? 0 : LVS_EX_GRIDLINES) | LVS_EX_LABELTIP | LVS_EX_HEADERDRAGDROP);
338 | SetProp(hGridWnd, TEXT("WNDPROC"), (HANDLE)SetWindowLongPtr(hGridWnd, GWLP_WNDPROC, (LONG_PTR)cbHotKey));
339 |
340 | HWND hHeader = ListView_GetHeader(hGridWnd);
341 | LONG_PTR styles = GetWindowLongPtr(hHeader, GWL_STYLE);
342 | SetWindowLongPtr(hHeader, GWL_STYLE, styles | HDS_FILTERBAR);
343 | SetWindowTheme(hHeader, TEXT(" "), TEXT(" "));
344 | SetProp(hHeader, TEXT("WNDPROC"), (HANDLE)SetWindowLongPtr(hHeader, GWLP_WNDPROC, (LONG_PTR)cbNewHeader));
345 |
346 | HMENU hGridMenu = CreatePopupMenu();
347 | AppendMenu(hGridMenu, MF_STRING, IDM_COPY_CELL, TEXT("Copy cell"));
348 | AppendMenu(hGridMenu, MF_STRING, IDM_COPY_ROWS, TEXT("Copy row(s)"));
349 | AppendMenu(hGridMenu, MF_STRING, IDM_COPY_COLUMN, TEXT("Copy column"));
350 | AppendMenu(hGridMenu, MF_STRING, 0, NULL);
351 | AppendMenu(hGridMenu, MF_STRING, IDM_HIDE_COLUMN, TEXT("Hide column"));
352 | AppendMenu(hGridMenu, MF_STRING, 0, NULL);
353 | AppendMenu(hGridMenu, (*(int*)GetProp(hMainWnd, TEXT("FILTERROW")) != 0 ? MF_CHECKED : 0) | MF_STRING, IDM_FILTER_ROW, TEXT("Filters"));
354 | if (odbcType == ODBC_EXCEL || odbcType == ODBC_EXCELX)
355 | AppendMenu(hGridMenu, (*(int*)GetProp(hMainWnd, TEXT("HEADERROW")) != 0 ? MF_CHECKED : 0) | MF_STRING, IDM_HEADER_ROW, TEXT("Header row"));
356 | AppendMenu(hGridMenu, (*(int*)GetProp(hMainWnd, TEXT("DARKTHEME")) != 0 ? MF_CHECKED : 0) | MF_STRING, IDM_DARK_THEME, TEXT("Dark theme"));
357 | SetProp(hMainWnd, TEXT("GRIDMENU"), hGridMenu);
358 |
359 | int tCount = 0, vCount = 0;
360 | SQLHANDLE hStmt = 0;
361 | SQLAllocHandle(SQL_HANDLE_STMT, hConn, &hStmt);
362 | SQLTables(hStmt, NULL, 0, NULL, 0, NULL, 0, NULL, 0);
363 |
364 | SQLLEN res = 0;
365 | while (SQLFetch(hStmt) == SQL_SUCCESS) {
366 | SQLWCHAR tblName[MAX_DATA_LENGTH + 1];
367 | SQLGetData(hStmt, 3, SQL_WCHAR, tblName, MAX_DATA_LENGTH * sizeof(TCHAR), &res);
368 |
369 | SQLWCHAR tblType[MAX_DATA_LENGTH + 1];
370 | SQLGetData(hStmt, 4, SQL_WCHAR, tblType, MAX_DATA_LENGTH * sizeof(TCHAR), &res);
371 | _tcslwr(tblType);
372 |
373 | BOOL isSystem = _tcscmp(tblType, TEXT("system table")) == 0;
374 | if (odbcType == ODBC_ACCESS && isSystem)
375 | continue;
376 |
377 | if (odbcType == ODBC_EXCEL || odbcType == ODBC_EXCELX) {
378 | TCHAR* tail = _tcsstr(tblName, TEXT("$'"));
379 | int len = tail ? _tcslen(tail) : 0;
380 | if (len > 2 || len < 2 && !isSystem)
381 | continue;
382 |
383 | // Remove $-tail or quotes e.g. 'tblName$'
384 | TCHAR tmpName[MAX_DATA_LENGTH] = {0};
385 | for (int i = 0; i < _tcslen(tblName) && tblName[i] != TEXT('$'); i++)
386 | tmpName[i] = tblName[i];
387 | _sntprintf(tblName, MAX_DATA_LENGTH, TEXT("%ls"), tmpName + (tmpName[0] == TEXT('\'')));
388 | }
389 |
390 | int pos = ListBox_AddString(hListWnd, tblName);
391 | BOOL isTable = _tcsstr(tblType, TEXT("view")) == 0;
392 | SendMessage(hListWnd, LB_SETITEMDATA, pos, isTable);
393 |
394 | tCount += isTable;
395 | vCount += !isTable;
396 | }
397 | SQLCloseCursor(hStmt);
398 | SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
399 |
400 | TCHAR buf[255];
401 | _sntprintf(buf, 32, TEXT(" %ls"), APP_VERSION);
402 | SendMessage(hStatusWnd, SB_SETTEXT, SB_VERSION, (LPARAM)buf);
403 | _sntprintf(buf, 255, TEXT(" Tables: %i"), tCount);
404 | SendMessage(hStatusWnd, SB_SETTEXT, SB_TABLE_COUNT, (LPARAM)buf);
405 | _sntprintf(buf, 255, TEXT(" Views: %i"), vCount);
406 | SendMessage(hStatusWnd, SB_SETTEXT, SB_VIEW_COUNT, (LPARAM)buf);
407 |
408 | SendMessage(hMainWnd, WMU_SET_FONT, 0, 0);
409 | SendMessage(hMainWnd, WMU_SET_THEME, 0, 0);
410 | ListBox_SetCurSel(hListWnd, 0);
411 | SendMessage(hMainWnd, WMU_UPDATE_GRID, 0, 0);
412 | ShowWindow(hMainWnd, SW_SHOW);
413 | SetFocus(hListWnd);
414 |
415 | return hMainWnd;
416 | }
417 |
418 | HWND APIENTRY ListLoad (HWND hListerWnd, char* fileToLoad, int showFlags) {
419 | DWORD size = MultiByteToWideChar(CP_ACP, 0, fileToLoad, -1, NULL, 0);
420 | TCHAR* fileToLoadW = (TCHAR*)calloc (size, sizeof (TCHAR));
421 | MultiByteToWideChar(CP_ACP, 0, fileToLoad, -1, fileToLoadW, size);
422 | HWND hWnd = ListLoadW(hListerWnd, fileToLoadW, showFlags);
423 | free(fileToLoadW);
424 | return hWnd;
425 | }
426 |
427 | void __stdcall ListCloseWindow(HWND hWnd) {
428 | setStoredValue(TEXT("splitter-position"), *(int*)GetProp(hWnd, TEXT("SPLITTERPOSITION")));
429 | setStoredValue(TEXT("font-size"), *(int*)GetProp(hWnd, TEXT("FONTSIZE")));
430 | setStoredValue(TEXT("filter-row"), *(int*)GetProp(hWnd, TEXT("FILTERROW")));
431 | setStoredValue(TEXT("header-row"), *(int*)GetProp(hWnd, TEXT("HEADERROW")));
432 | setStoredValue(TEXT("dark-theme"), *(int*)GetProp(hWnd, TEXT("DARKTHEME")));
433 |
434 | SQLHANDLE hEnv = (SQLHANDLE)GetProp(hWnd, TEXT("DBENV"));
435 | SQLHANDLE hConn = (SQLHANDLE)GetProp(hWnd, TEXT("DB"));
436 | SQLDisconnect(hConn);
437 | SQLFreeHandle(SQL_HANDLE_DBC, hConn);
438 | SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
439 |
440 | SendMessage(hWnd, WMU_RESET_CACHE, 0, 0);
441 | free((int*)GetProp(hWnd, TEXT("FILTERROW")));
442 | free((int*)GetProp(hWnd, TEXT("HEADERROW")));
443 | free((int*)GetProp(hWnd, TEXT("DARKTHEME")));
444 | free((int*)GetProp(hWnd, TEXT("ORDERBY")));
445 | free((TCHAR*)GetProp(hWnd, TEXT("DBPATH")));
446 | free((TCHAR*)GetProp(hWnd, TEXT("TABLENAME")));
447 | free((TCHAR*)GetProp(hWnd, TEXT("WHERE")));
448 | free((int*)GetProp(hWnd, TEXT("ROWCOUNT")));
449 | free((int*)GetProp(hWnd, TEXT("TOTALROWCOUNT")));
450 | free((int*)GetProp(hWnd, TEXT("SPLITTERPOSITION")));
451 | free((int*)GetProp(hWnd, TEXT("FONTSIZE")));
452 | free((int*)GetProp(hWnd, TEXT("FILTERCOLOR")));
453 | free((int*)GetProp(hWnd, TEXT("CURRENTROWNO")));
454 | free((int*)GetProp(hWnd, TEXT("CURRENTCOLNO")));
455 | free((int*)GetProp(hWnd, TEXT("SEARCHCELLPOS")));
456 | free((int*)GetProp(hWnd, TEXT("ODBCTYPE")));
457 | free((TCHAR*)GetProp(hWnd, TEXT("FONTFAMILY")));
458 | free((int*)GetProp(hWnd, TEXT("FILTERALIGN")));
459 |
460 | free((int*)GetProp(hWnd, TEXT("TEXTCOLOR")));
461 | free((int*)GetProp(hWnd, TEXT("BACKCOLOR")));
462 | free((int*)GetProp(hWnd, TEXT("BACKCOLOR2")));
463 | free((int*)GetProp(hWnd, TEXT("FILTERTEXTCOLOR")));
464 | free((int*)GetProp(hWnd, TEXT("FILTERBACKCOLOR")));
465 | free((int*)GetProp(hWnd, TEXT("CURRENTCELLCOLOR")));
466 | free((int*)GetProp(hWnd, TEXT("SELECTIONTEXTCOLOR")));
467 | free((int*)GetProp(hWnd, TEXT("SELECTIONBACKCOLOR")));
468 | free((int*)GetProp(hWnd, TEXT("SPLITTERCOLOR")));
469 |
470 | DeleteFont(GetProp(hWnd, TEXT("FONT")));
471 | DeleteObject(GetProp(hWnd, TEXT("BACKBRUSH")));
472 | DeleteObject(GetProp(hWnd, TEXT("FILTERBACKBRUSH")));
473 | DeleteObject(GetProp(hWnd, TEXT("SPLITTERBRUSH")));
474 | DestroyMenu(GetProp(hWnd, TEXT("GRIDMENU")));
475 |
476 | RemoveProp(hWnd, TEXT("WNDPROC"));
477 | RemoveProp(hWnd, TEXT("FILTERROW"));
478 | RemoveProp(hWnd, TEXT("HEADERROW"));
479 | RemoveProp(hWnd, TEXT("DARKTHEME"));
480 | RemoveProp(hWnd, TEXT("CACHE"));
481 | RemoveProp(hWnd, TEXT("DBENV"));
482 | RemoveProp(hWnd, TEXT("DB"));
483 | RemoveProp(hWnd, TEXT("ORDERBY"));
484 | RemoveProp(hWnd, TEXT("DBPATH"));
485 | RemoveProp(hWnd, TEXT("TABLENAME"));
486 | RemoveProp(hWnd, TEXT("WHERE"));
487 | RemoveProp(hWnd, TEXT("ROWCOUNT"));
488 | RemoveProp(hWnd, TEXT("TOTALROWCOUNT"));
489 | RemoveProp(hWnd, TEXT("SPLITTERPOSITION"));
490 | RemoveProp(hWnd, TEXT("CURRENTROWNO"));
491 | RemoveProp(hWnd, TEXT("CURRENTCOLNO"));
492 | RemoveProp(hWnd, TEXT("SEARCHCELLPOS"));
493 | RemoveProp(hWnd, TEXT("ODBCTYPE"));
494 | RemoveProp(hWnd, TEXT("FILTERALIGN"));
495 | RemoveProp(hWnd, TEXT("LASTFOCUS"));
496 |
497 | RemoveProp(hWnd, TEXT("FONT"));
498 | RemoveProp(hWnd, TEXT("FONTFAMILY"));
499 | RemoveProp(hWnd, TEXT("FONTSIZE"));
500 | RemoveProp(hWnd, TEXT("TEXTCOLOR"));
501 | RemoveProp(hWnd, TEXT("BACKCOLOR"));
502 | RemoveProp(hWnd, TEXT("BACKCOLOR2"));
503 | RemoveProp(hWnd, TEXT("FILTERTEXTCOLOR"));
504 | RemoveProp(hWnd, TEXT("FILTERBACKCOLOR"));
505 | RemoveProp(hWnd, TEXT("CURRENTCELLCOLOR"));
506 | RemoveProp(hWnd, TEXT("SELECTIONTEXTCOLOR"));
507 | RemoveProp(hWnd, TEXT("SELECTIONBACKCOLOR"));
508 | RemoveProp(hWnd, TEXT("SPLITTERCOLOR"));
509 | RemoveProp(hWnd, TEXT("BACKBRUSH"));
510 | RemoveProp(hWnd, TEXT("FILTERBACKBRUSH"));
511 | RemoveProp(hWnd, TEXT("SPLITTERBRUSH"));
512 | RemoveProp(hWnd, TEXT("GRIDMENU"));
513 |
514 | DestroyWindow(hWnd);
515 | }
516 |
517 | LRESULT CALLBACK cbNewMain(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
518 | switch (msg) {
519 | case WM_SIZE: {
520 | HWND hStatusWnd = GetDlgItem(hWnd, IDC_STATUSBAR);
521 | SendMessage(hStatusWnd, WM_SIZE, 0, 0);
522 | RECT rc;
523 | GetClientRect(hStatusWnd, &rc);
524 | int statusH = rc.bottom;
525 |
526 | int splitterW = *(int*)GetProp(hWnd, TEXT("SPLITTERPOSITION"));
527 | GetClientRect(hWnd, &rc);
528 | HWND hListWnd = GetDlgItem(hWnd, IDC_TABLELIST);
529 | HWND hGridWnd = GetDlgItem(hWnd, IDC_GRID);
530 | SetWindowPos(hListWnd, 0, 0, 0, splitterW, rc.bottom - statusH, SWP_NOMOVE | SWP_NOZORDER);
531 | SetWindowPos(hGridWnd, 0, splitterW + SPLITTER_WIDTH, 0, rc.right - splitterW - SPLITTER_WIDTH, rc.bottom - statusH, SWP_NOZORDER);
532 | }
533 | break;
534 |
535 | case WM_PAINT: {
536 | PAINTSTRUCT ps = {0};
537 | HDC hDC = BeginPaint(hWnd, &ps);
538 |
539 | RECT rc;
540 | GetClientRect(hWnd, &rc);
541 | rc.left = *(int*)GetProp(hWnd, TEXT("SPLITTERPOSITION"));
542 | rc.right = rc.left + SPLITTER_WIDTH;
543 | FillRect(hDC, &rc, (HBRUSH)GetProp(hWnd, TEXT("SPLITTERBRUSH")));
544 | EndPaint(hWnd, &ps);
545 |
546 | return 0;
547 | }
548 | break;
549 |
550 | // https://groups.google.com/g/comp.os.ms-windows.programmer.win32/c/1XhCKATRXws
551 | case WM_NCHITTEST: {
552 | return 1;
553 | }
554 | break;
555 |
556 | case WM_SETCURSOR: {
557 | SetCursor(LoadCursor(0, GetProp(hWnd, TEXT("ISMOUSEHOVER")) ? IDC_SIZEWE : IDC_ARROW));
558 | return TRUE;
559 | }
560 | break;
561 |
562 | case WM_SETFOCUS: {
563 | SetFocus(GetProp(hWnd, TEXT("LASTFOCUS")));
564 | }
565 | break;
566 |
567 | case WM_LBUTTONDOWN: {
568 | int x = GET_X_LPARAM(lParam);
569 | int pos = *(int*)GetProp(hWnd, TEXT("SPLITTERPOSITION"));
570 | if (x >= pos && x <= pos + SPLITTER_WIDTH) {
571 | SetProp(hWnd, TEXT("ISMOUSEDOWN"), (HANDLE)1);
572 | SetCapture(hWnd);
573 | }
574 | return 0;
575 | }
576 | break;
577 |
578 | case WM_LBUTTONUP: {
579 | ReleaseCapture();
580 | RemoveProp(hWnd, TEXT("ISMOUSEDOWN"));
581 | }
582 | break;
583 |
584 | case WM_MOUSEMOVE: {
585 | DWORD x = GET_X_LPARAM(lParam);
586 | int* pPos = (int*)GetProp(hWnd, TEXT("SPLITTERPOSITION"));
587 |
588 | if (!GetProp(hWnd, TEXT("ISMOUSEHOVER")) && *pPos <= x && x <= *pPos + SPLITTER_WIDTH) {
589 | TRACKMOUSEEVENT tme = {sizeof(TRACKMOUSEEVENT), TME_LEAVE, hWnd, 0};
590 | TrackMouseEvent(&tme);
591 | SetProp(hWnd, TEXT("ISMOUSEHOVER"), (HANDLE)1);
592 | }
593 |
594 | if (GetProp(hWnd, TEXT("ISMOUSEDOWN")) && x > 0 && x < 32000) {
595 | *pPos = x;
596 | SendMessage(hWnd, WM_SIZE, 0, 0);
597 | }
598 | }
599 | break;
600 |
601 | case WM_MOUSELEAVE: {
602 | SetProp(hWnd, TEXT("ISMOUSEHOVER"), 0);
603 | }
604 | break;
605 |
606 | case WM_MOUSEWHEEL: {
607 | if (LOWORD(wParam) == MK_CONTROL) {
608 | SendMessage(hWnd, WMU_SET_FONT, GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? 1: -1, 0);
609 | return 1;
610 | }
611 | }
612 | break;
613 |
614 | case WM_KEYDOWN: {
615 | if (SendMessage(hWnd, WMU_HOT_KEYS, wParam, lParam))
616 | return 0;
617 | }
618 | break;
619 |
620 | case WM_CTLCOLORLISTBOX: {
621 | SetBkColor((HDC)wParam, *(int*)GetProp(hWnd, TEXT("BACKCOLOR")));
622 | SetTextColor((HDC)wParam, *(int*)GetProp(hWnd, TEXT("TEXTCOLOR")));
623 | return (INT_PTR)(HBRUSH)GetProp(hWnd, TEXT("BACKBRUSH"));
624 | }
625 | break;
626 |
627 | case WM_COMMAND: {
628 | WORD cmd = LOWORD(wParam);
629 | if (cmd == IDC_TABLELIST && HIWORD(wParam) == LBN_SELCHANGE)
630 | SendMessage(hWnd, WMU_UPDATE_GRID, 0, 0);
631 |
632 | if (cmd == IDC_TABLELIST && HIWORD(wParam) == LBN_SETFOCUS)
633 | SetProp(hWnd, TEXT("LASTFOCUS"), (HWND)lParam);
634 |
635 | if (cmd == IDM_COPY_CELL || cmd == IDM_COPY_ROWS || cmd == IDM_COPY_COLUMN) {
636 | HWND hGridWnd = GetDlgItem(hWnd, IDC_GRID);
637 | HWND hHeader = ListView_GetHeader(hGridWnd);
638 | int rowNo = *(int*)GetProp(hWnd, TEXT("CURRENTROWNO"));
639 | int colNo = *(int*)GetProp(hWnd, TEXT("CURRENTCOLNO"));
640 |
641 | int colCount = Header_GetItemCount(hHeader);
642 | int rowCount = *(int*)GetProp(hWnd, TEXT("ROWCOUNT"));
643 | int selCount = ListView_GetSelectedCount(hGridWnd);
644 |
645 | if (rowNo == -1 ||
646 | rowNo >= rowCount ||
647 | colCount == 0 ||
648 | cmd == IDM_COPY_CELL && colNo == -1 ||
649 | cmd == IDM_COPY_CELL && colNo >= colCount ||
650 | cmd == IDM_COPY_COLUMN && colNo == -1 ||
651 | cmd == IDM_COPY_COLUMN && colNo >= colCount ||
652 | cmd == IDM_COPY_ROWS && selCount == 0) {
653 | setClipboardText(TEXT(""));
654 | return 0;
655 | }
656 |
657 | TCHAR*** cache = (TCHAR***)GetProp(hWnd, TEXT("CACHE"));
658 | TCHAR* delimiter = getStoredString(TEXT("column-delimiter"), TEXT("\t"));
659 |
660 | int len = 0;
661 | if (cmd == IDM_COPY_CELL)
662 | len = _tcslen(cache[rowNo][colNo]);
663 |
664 | if (cmd == IDM_COPY_ROWS) {
665 | int rowNo = ListView_GetNextItem(hGridWnd, -1, LVNI_SELECTED);
666 | while (rowNo != -1) {
667 | for (int colNo = 0; colNo < colCount; colNo++) {
668 | if (ListView_GetColumnWidth(hGridWnd, colNo))
669 | len += _tcslen(cache[rowNo][colNo]) + 1; /* column delimiter */
670 | }
671 |
672 | len++; /* \n */
673 | rowNo = ListView_GetNextItem(hGridWnd, rowNo, LVNI_SELECTED);
674 | }
675 | }
676 |
677 | if (cmd == IDM_COPY_COLUMN) {
678 | int rowNo = selCount < 2 ? 0 : ListView_GetNextItem(hGridWnd, -1, LVNI_SELECTED);
679 | while (rowNo != -1 && rowNo < rowCount) {
680 | len += _tcslen(cache[rowNo][colNo]) + 1 /* \n */;
681 | rowNo = selCount < 2 ? rowNo + 1 : ListView_GetNextItem(hGridWnd, rowNo, LVNI_SELECTED);
682 | }
683 | }
684 |
685 | TCHAR* buf = calloc(len + 1, sizeof(TCHAR));
686 | if (cmd == IDM_COPY_CELL)
687 | _tcscat(buf, cache[rowNo][colNo]);
688 |
689 | if (cmd == IDM_COPY_ROWS) {
690 | int pos = 0;
691 | int rowNo = ListView_GetNextItem(hGridWnd, -1, LVNI_SELECTED);
692 |
693 | int* colOrder = calloc(colCount, sizeof(int));
694 | Header_GetOrderArray(hHeader, colCount, colOrder);
695 |
696 | while (rowNo != -1) {
697 | for (int idx = 0; idx < colCount; idx++) {
698 | int colNo = colOrder[idx];
699 | if (ListView_GetColumnWidth(hGridWnd, colNo)) {
700 | int len = _tcslen(cache[rowNo][colNo]);
701 | _tcsncpy(buf + pos, cache[rowNo][colNo], len);
702 | buf[pos + len] = delimiter[0];
703 | pos += len + 1;
704 | }
705 | }
706 |
707 | buf[pos - (pos > 0)] = TEXT('\n');
708 | rowNo = ListView_GetNextItem(hGridWnd, rowNo, LVNI_SELECTED);
709 | }
710 | buf[pos - 1] = 0; // remove last \n
711 |
712 | free(colOrder);
713 | }
714 |
715 | if (cmd == IDM_COPY_COLUMN) {
716 | int pos = 0;
717 | int rowNo = selCount < 2 ? 0 : ListView_GetNextItem(hGridWnd, -1, LVNI_SELECTED);
718 | while (rowNo != -1 && rowNo < rowCount) {
719 | int len = _tcslen(cache[rowNo][colNo]);
720 | _tcsncpy(buf + pos, cache[rowNo][colNo], len);
721 | rowNo = selCount < 2 ? rowNo + 1 : ListView_GetNextItem(hGridWnd, rowNo, LVNI_SELECTED);
722 | if (rowNo != -1 && rowNo < rowCount)
723 | buf[pos + len] = TEXT('\n');
724 | pos += len + 1;
725 | }
726 | }
727 |
728 | setClipboardText(buf);
729 | free(buf);
730 | free(delimiter);
731 | }
732 |
733 | if (cmd == IDM_HIDE_COLUMN) {
734 | int colNo = *(int*)GetProp(hWnd, TEXT("CURRENTCOLNO"));
735 | SendMessage(hWnd, WMU_HIDE_COLUMN, colNo, 0);
736 | }
737 |
738 | if (cmd == IDM_FILTER_ROW || cmd == IDM_HEADER_ROW || cmd == IDM_DARK_THEME) {
739 | HMENU hMenu = (HMENU)GetProp(hWnd, TEXT("GRIDMENU"));
740 | int* pOpt = (int*)GetProp(hWnd, cmd == IDM_FILTER_ROW ? TEXT("FILTERROW") : cmd == IDM_HEADER_ROW ? TEXT("HEADERROW") : TEXT("DARKTHEME"));
741 | *pOpt = (*pOpt + 1) % 2;
742 | Menu_SetItemState(hMenu, cmd, *pOpt ? MFS_CHECKED : 0);
743 |
744 | UINT msg = cmd == IDM_FILTER_ROW ? WMU_SET_HEADER_FILTERS : cmd == IDM_HEADER_ROW ? WMU_UPDATE_GRID : WMU_SET_THEME;
745 | SendMessage(hWnd, msg, 0, 0);
746 | }
747 | }
748 | break;
749 |
750 | case WM_NOTIFY : {
751 | NMHDR* pHdr = (LPNMHDR)lParam;
752 | if (pHdr->idFrom == IDC_GRID && pHdr->code == LVN_GETDISPINFO) {
753 | LV_DISPINFO* pDispInfo = (LV_DISPINFO*)lParam;
754 | LV_ITEM* pItem= &(pDispInfo)->item;
755 | TCHAR*** cache = (TCHAR***)GetProp(hWnd, TEXT("CACHE"));
756 |
757 | if(pItem->mask & LVIF_TEXT)
758 | pItem->pszText = cache[pItem->iItem][pItem->iSubItem];
759 | }
760 |
761 | if (pHdr->idFrom == IDC_GRID && pHdr->code == LVN_COLUMNCLICK) {
762 | NMLISTVIEW* lv = (NMLISTVIEW*)lParam;
763 | return SendMessage(hWnd, HIWORD(GetKeyState(VK_CONTROL)) ? WMU_HIDE_COLUMN : WMU_SORT_COLUMN, lv->iSubItem, 0);
764 | }
765 |
766 | if (pHdr->idFrom == IDC_GRID && (pHdr->code == (DWORD)NM_CLICK || pHdr->code == (DWORD)NM_RCLICK)) {
767 | NMITEMACTIVATE* ia = (LPNMITEMACTIVATE) lParam;
768 | SendMessage(hWnd, WMU_SET_CURRENT_CELL, ia->iItem, ia->iSubItem);
769 | }
770 |
771 | if (pHdr->idFrom == IDC_GRID && pHdr->code == (DWORD)NM_CLICK && HIWORD(GetKeyState(VK_MENU))) {
772 | NMITEMACTIVATE* ia = (LPNMITEMACTIVATE) lParam;
773 | TCHAR*** cache = (TCHAR***)GetProp(hWnd, TEXT("CACHE"));
774 | int* resultset = (int*)GetProp(hWnd, TEXT("RESULTSET"));
775 |
776 | TCHAR* url = extractUrl(cache[ia->iItem][ia->iSubItem]);
777 | ShellExecute(0, TEXT("open"), url, 0, 0 , SW_SHOW);
778 | free(url);
779 | }
780 |
781 | if (pHdr->idFrom == IDC_GRID && pHdr->code == (DWORD)NM_RCLICK) {
782 | POINT p;
783 | GetCursorPos(&p);
784 | TrackPopupMenu(GetProp(hWnd, TEXT("GRIDMENU")), TPM_RIGHTBUTTON | TPM_TOPALIGN | TPM_LEFTALIGN, p.x, p.y, 0, hWnd, NULL);
785 | }
786 |
787 | if (pHdr->idFrom == IDC_GRID && pHdr->code == (DWORD)LVN_ITEMCHANGED) {
788 | NMLISTVIEW* lv = (NMLISTVIEW*)lParam;
789 | if (lv->uOldState != lv->uNewState && (lv->uNewState & LVIS_SELECTED))
790 | SendMessage(hWnd, WMU_SET_CURRENT_CELL, lv->iItem, *(int*)GetProp(hWnd, TEXT("CURRENTCOLNO")));
791 | }
792 |
793 | if (pHdr->idFrom == IDC_GRID && pHdr->code == (DWORD)LVN_KEYDOWN) {
794 | NMLVKEYDOWN* kd = (LPNMLVKEYDOWN) lParam;
795 | if (kd->wVKey == 0x43) { // C
796 | BOOL isCtrl = HIWORD(GetKeyState(VK_CONTROL));
797 | BOOL isShift = HIWORD(GetKeyState(VK_SHIFT));
798 | BOOL isCopyColumn = getStoredValue(TEXT("copy-column"), 0) && ListView_GetSelectedCount(pHdr->hwndFrom) > 1;
799 | if (!isCtrl && !isShift)
800 | return FALSE;
801 |
802 | int action = !isShift && !isCopyColumn ? IDM_COPY_CELL : isCtrl || isCopyColumn ? IDM_COPY_COLUMN : IDM_COPY_ROWS;
803 | SendMessage(hWnd, WM_COMMAND, action, 0);
804 |
805 | SendMessage(hWnd, WM_COMMAND, action, 0);
806 | return TRUE;
807 | }
808 |
809 | if (kd->wVKey == 0x41 && HIWORD(GetKeyState(VK_CONTROL))) { // Ctrl + A
810 | HWND hGridWnd = pHdr->hwndFrom;
811 | SendMessage(hGridWnd, WM_SETREDRAW, FALSE, 0);
812 | int rowNo = *(int*)GetProp(hWnd, TEXT("CURRENTROWNO"));
813 | int colNo = *(int*)GetProp(hWnd, TEXT("CURRENTCOLNO"));
814 | ListView_SetItemState(hGridWnd, -1, LVIS_SELECTED, LVIS_SELECTED | LVIS_FOCUSED);
815 | SendMessage(hWnd, WMU_SET_CURRENT_CELL, rowNo, colNo);
816 | SendMessage(hGridWnd, WM_SETREDRAW, TRUE, 0);
817 | InvalidateRect(hGridWnd, NULL, TRUE);
818 | }
819 |
820 | if (kd->wVKey == 0x20 && HIWORD(GetKeyState(VK_CONTROL))) { // Ctrl + Space
821 | SendMessage(hWnd, WMU_SHOW_COLUMNS, 0, 0);
822 | return TRUE;
823 | }
824 |
825 | if (kd->wVKey == VK_LEFT || kd->wVKey == VK_RIGHT) {
826 | HWND hGridWnd = GetDlgItem(hWnd, IDC_GRID);
827 | HWND hHeader = ListView_GetHeader(hGridWnd);
828 |
829 | int colCount = Header_GetItemCount(ListView_GetHeader(pHdr->hwndFrom));
830 | int colNo = *(int*)GetProp(hWnd, TEXT("CURRENTCOLNO"));
831 |
832 | int* colOrder = calloc(colCount, sizeof(int));
833 | Header_GetOrderArray(hHeader, colCount, colOrder);
834 |
835 | int dir = kd->wVKey == VK_RIGHT ? 1 : -1;
836 | int idx = 0;
837 | for (idx; colOrder[idx] != colNo; idx++);
838 | do {
839 | idx = (colCount + idx + dir) % colCount;
840 | } while (ListView_GetColumnWidth(hGridWnd, colOrder[idx]) == 0);
841 |
842 | colNo = colOrder[idx];
843 | free(colOrder);
844 |
845 | SendMessage(hWnd, WMU_SET_CURRENT_CELL, *(int*)GetProp(hWnd, TEXT("CURRENTROWNO")), colNo);
846 | return TRUE;
847 | }
848 | }
849 |
850 | if ((pHdr->code == HDN_ITEMCHANGED || pHdr->code == HDN_ENDDRAG) && pHdr->hwndFrom == ListView_GetHeader(GetDlgItem(hWnd, IDC_GRID)))
851 | PostMessage(hWnd, WMU_UPDATE_FILTER_SIZE, 0, 0);
852 |
853 | if (pHdr->code == (UINT)NM_SETFOCUS)
854 | SetProp(hWnd, TEXT("LASTFOCUS"), pHdr->hwndFrom);
855 |
856 | if (pHdr->idFrom == IDC_GRID && pHdr->code == (UINT)NM_CUSTOMDRAW) {
857 | int result = CDRF_DODEFAULT;
858 |
859 | NMLVCUSTOMDRAW* pCustomDraw = (LPNMLVCUSTOMDRAW)lParam;
860 | if (pCustomDraw->nmcd.dwDrawStage == CDDS_PREPAINT)
861 | result = CDRF_NOTIFYITEMDRAW;
862 |
863 | if (pCustomDraw->nmcd.dwDrawStage == CDDS_ITEMPREPAINT) {
864 | if (ListView_GetItemState(pHdr->hwndFrom, pCustomDraw->nmcd.dwItemSpec, LVIS_SELECTED)) {
865 | pCustomDraw->nmcd.uItemState &= ~CDIS_SELECTED;
866 | result = CDRF_NOTIFYSUBITEMDRAW;
867 | } else {
868 | pCustomDraw->clrTextBk = *(int*)GetProp(hWnd, pCustomDraw->nmcd.dwItemSpec % 2 == 0 ? TEXT("BACKCOLOR") : TEXT("BACKCOLOR2"));
869 | }
870 | }
871 |
872 | if (pCustomDraw->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM)) {
873 | int rowNo = *(int*)GetProp(hWnd, TEXT("CURRENTROWNO"));
874 | int colNo = *(int*)GetProp(hWnd, TEXT("CURRENTCOLNO"));
875 | BOOL isCurrCell = (pCustomDraw->nmcd.dwItemSpec == (DWORD)rowNo) && (pCustomDraw->iSubItem == colNo);
876 | pCustomDraw->clrText = *(int*)GetProp(hWnd, TEXT("SELECTIONTEXTCOLOR"));
877 | pCustomDraw->clrTextBk = *(int*)GetProp(hWnd, isCurrCell ? TEXT("CURRENTCELLCOLOR") : TEXT("SELECTIONBACKCOLOR"));
878 | }
879 |
880 | return result;
881 | }
882 | }
883 | break;
884 |
885 | // wParam = colNo
886 | case WMU_HIDE_COLUMN: {
887 | HWND hGridWnd = GetDlgItem(hWnd, IDC_GRID);
888 | HWND hHeader = ListView_GetHeader(hGridWnd);
889 | int colNo = (int)wParam;
890 |
891 | HWND hEdit = GetDlgItem(hHeader, IDC_HEADER_EDIT + colNo);
892 | SetWindowLongPtr(hEdit, GWLP_USERDATA, (LONG_PTR)ListView_GetColumnWidth(hGridWnd, colNo));
893 | ListView_SetColumnWidth(hGridWnd, colNo, 0);
894 | InvalidateRect(hHeader, NULL, TRUE);
895 | }
896 | break;
897 |
898 | case WMU_SHOW_COLUMNS: {
899 | HWND hGridWnd = GetDlgItem(hWnd, IDC_GRID);
900 | HWND hHeader = ListView_GetHeader(hGridWnd);
901 | int colCount = Header_GetItemCount(ListView_GetHeader(hGridWnd));
902 | for (int colNo = 0; colNo < colCount; colNo++) {
903 | if (ListView_GetColumnWidth(hGridWnd, colNo) == 0) {
904 | HWND hEdit = GetDlgItem(hHeader, IDC_HEADER_EDIT + colNo);
905 | ListView_SetColumnWidth(hGridWnd, colNo, (int)GetWindowLongPtr(hEdit, GWLP_USERDATA));
906 | }
907 | }
908 |
909 | InvalidateRect(hGridWnd, NULL, TRUE);
910 | }
911 | break;
912 |
913 | // wParam = colNo
914 | case WMU_SORT_COLUMN: {
915 | int colNo = (int)wParam + 1;
916 | if (colNo <= 0)
917 | return FALSE;
918 |
919 | int* pOrderBy = (int*)GetProp(hWnd, TEXT("ORDERBY"));
920 | int orderBy = *pOrderBy;
921 | *pOrderBy = colNo == orderBy || colNo == -orderBy ? -orderBy : colNo;
922 | SendMessage(hWnd, WMU_UPDATE_CACHE, 0, 0);
923 | }
924 | break;
925 |
926 | case WMU_UPDATE_GRID: {
927 | HWND hListWnd = GetDlgItem(hWnd, IDC_TABLELIST);
928 | HWND hGridWnd = GetDlgItem(hWnd, IDC_GRID);
929 | HWND hStatusWnd = GetDlgItem(hWnd, IDC_STATUSBAR);
930 | SQLHANDLE hConn = (SQLHANDLE)GetProp(hWnd, TEXT("DB"));
931 | int odbcType = *(int*)GetProp(hWnd, TEXT("ODBCTYPE"));
932 | BOOL isHeaderRow = *(int*)GetProp(hWnd, TEXT("HEADERROW"));
933 | int filterAlign = *(int*)GetProp(hWnd, TEXT("FILTERALIGN"));
934 |
935 | SendMessage(hGridWnd, WM_SETREDRAW, FALSE, 0);
936 | HWND hHeader = ListView_GetHeader(hGridWnd);
937 |
938 | SendMessage(hWnd, WMU_RESET_CACHE, 0, 0);
939 | ListView_SetItemCount(hGridWnd, 0);
940 | SendMessage(hWnd, WMU_SET_CURRENT_CELL, 0, 0);
941 |
942 | int colCount = Header_GetItemCount(hHeader);
943 | for (int colNo = 0; colNo < colCount; colNo++)
944 | DestroyWindow(GetDlgItem(hHeader, IDC_HEADER_EDIT + colNo));
945 |
946 | for (int colNo = 0; colNo < colCount; colNo++)
947 | ListView_DeleteColumn(hGridWnd, colCount - colNo - 1);
948 |
949 | TCHAR* tablename = (TCHAR*)GetProp(hWnd, TEXT("TABLENAME"));
950 | int pos = ListBox_GetCurSel(hListWnd);
951 | ListBox_GetText(hListWnd, pos, tablename);
952 |
953 |
954 | TCHAR buf[255];
955 | int type = SendMessage(hListWnd, LB_GETITEMDATA, pos, 0);
956 | _sntprintf(buf, 255, type ? TEXT(" TABLE"): TEXT(" VIEW"));
957 | SendMessage(hStatusWnd, SB_SETTEXT, SB_TYPE, (LPARAM)buf);
958 |
959 | SQLHANDLE hStmt = 0;
960 | SQLAllocHandle(SQL_HANDLE_STMT, hConn, &hStmt);
961 | int len = 1024 + MAX_PATH + _tcslen(tablename);
962 | TCHAR query[len + 1];
963 | if (odbcType == ODBC_EXCEL || odbcType == ODBC_EXCELX) {
964 | TCHAR* dbpath = (TCHAR*)GetProp(hWnd, TEXT("DBPATH"));
965 | _sntprintf(query, len, TEXT("select * from \"Excel 8.0;HDR=%ls;IMEX=1;Database=%ls;\".\"%ls$\" where 1 = 2"),
966 | isHeaderRow ? TEXT("YES") : TEXT("NO"), dbpath, tablename);
967 | } else {
968 | _sntprintf(query, len, TEXT("select * from \"%ls\" where 1 = 2"), tablename);
969 | }
970 |
971 | if (SQL_SUCCESS == SQLExecDirect(hStmt, query, SQL_NTS)) {
972 | SQLSMALLINT colCount = 0;
973 | SQLNumResultCols(hStmt, &colCount);
974 |
975 | for (int colNo = 0; colNo < colCount; colNo++) {
976 | SQLWCHAR colName[MAX_COLUMN_LENGTH];
977 | SQLSMALLINT colType = 0;
978 | SQLDescribeCol(hStmt, colNo + 1, colName, MAX_COLUMN_LENGTH, 0, &colType, 0, 0, 0);
979 | if (!isHeaderRow && (odbcType == ODBC_EXCEL || odbcType == ODBC_EXCELX))
980 | _sntprintf(colName, 64, TEXT("Column #%i"), colNo + 1);
981 |
982 | int fmt = colType == SQL_DECIMAL || colType == SQL_NUMERIC || colType == SQL_REAL || colType == SQL_FLOAT || colType == SQL_DOUBLE ||
983 | colType == SQL_SMALLINT || colType == SQL_INTEGER || colType == SQL_BIT || colType == SQL_TINYINT || colType == SQL_BIGINT ?
984 | LVCFMT_RIGHT :
985 | LVCFMT_LEFT;
986 |
987 | ListView_AddColumn(hGridWnd, colName, fmt);
988 | }
989 |
990 | int align = filterAlign == -1 ? ES_LEFT : filterAlign == 1 ? ES_RIGHT : ES_CENTER;
991 | for (int colNo = 0; colNo < colCount; colNo++) {
992 | RECT rc;
993 | Header_GetItemRect(hHeader, colNo, &rc);
994 | HWND hEdit = CreateWindowEx(WS_EX_TOPMOST, WC_EDIT, NULL, align | ES_AUTOHSCROLL | WS_CHILD | WS_BORDER | WS_TABSTOP, 0, 0, 0, 0, hHeader, (HMENU)(INT_PTR)(IDC_HEADER_EDIT + colNo), GetModuleHandle(0), NULL);
995 | SendMessage(hEdit, WM_SETFONT, (LPARAM)GetProp(hWnd, TEXT("FONT")), TRUE);
996 | SetProp(hEdit, TEXT("WNDPROC"), (HANDLE)SetWindowLongPtr(hEdit, GWLP_WNDPROC, (LONG_PTR)cbNewFilterEdit));
997 | }
998 | } else {
999 | SQLWCHAR err[MAX_ERROR_LENGTH + 1];
1000 | SQLWCHAR code[6];
1001 | SQLGetDiagRec(SQL_HANDLE_STMT, hStmt, 1, code, NULL, err, MAX_ERROR_LENGTH, NULL);
1002 | TCHAR msg[MAX_ERROR_LENGTH + 100];
1003 | _sntprintf(msg, MAX_ERROR_LENGTH + 100, TEXT("Error (%ls): %ls"), code, err);
1004 | MessageBox(hWnd, msg, NULL, MB_OK);
1005 | }
1006 | SQLCloseCursor(hStmt);
1007 | SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
1008 |
1009 | *(int*)GetProp(hWnd, TEXT("ORDERBY")) = 0;
1010 | SendMessage(hWnd, WMU_UPDATE_CACHE, 0, 0);
1011 | SendMessage(hGridWnd, WM_SETREDRAW, TRUE, 0);
1012 |
1013 | SendMessage(hWnd, WMU_SET_HEADER_FILTERS, 0, 0);
1014 | PostMessage(hWnd, WMU_AUTO_COLUMN_SIZE, 0, 0);
1015 | }
1016 | break;
1017 |
1018 | case WMU_UPDATE_CACHE: {
1019 | HWND hGridWnd = GetDlgItem(hWnd, IDC_GRID);
1020 | HWND hStatusWnd = GetDlgItem(hWnd, IDC_STATUSBAR);
1021 | HWND hHeader = ListView_GetHeader(hGridWnd);
1022 | int colCount = Header_GetItemCount(hHeader);
1023 | SQLHANDLE hConn = (SQLHANDLE)GetProp(hWnd, TEXT("DB"));
1024 | int odbcType = *(int*)GetProp(hWnd, TEXT("ODBCTYPE"));
1025 | TCHAR* tablename = (TCHAR*)GetProp(hWnd, TEXT("TABLENAME"));
1026 | TCHAR* where = (TCHAR*)GetProp(hWnd, TEXT("WHERE"));
1027 | int* pRowCount = (int*)GetProp(hWnd, TEXT("ROWCOUNT"));
1028 | int* pTotalRowCount = (int*)GetProp(hWnd, TEXT("TOTALROWCOUNT"));
1029 | int orderBy = *(int*)GetProp(hWnd, TEXT("ORDERBY"));
1030 | BOOL isHeaderRow = *(int*)GetProp(hWnd, TEXT("HEADERROW"));
1031 | BOOL isExcel = odbcType == ODBC_EXCEL || odbcType == ODBC_EXCELX;
1032 |
1033 | SendMessage(hWnd, WMU_RESET_CACHE, 0, 0);
1034 | ListView_SetItemCount(hGridWnd, 0);
1035 |
1036 | _sntprintf(where, MAX_TEXT_LENGTH, TEXT("where (1 = 1)"));
1037 | for (int colNo = 0; colNo < colCount; colNo++) {
1038 | HWND hEdit = GetDlgItem(hHeader, IDC_HEADER_EDIT + colNo);
1039 | int len = GetWindowTextLength(hEdit);
1040 | if (len > 0) {
1041 | TCHAR colName[256] = {0};
1042 | if (!isHeaderRow && isExcel)
1043 | _sntprintf(colName, 255, TEXT("F%i"), colNo + 1);
1044 | else
1045 | Header_GetItemText(hHeader, colNo, colName, 255);
1046 |
1047 | TCHAR val[len + 1];
1048 | GetWindowText(hEdit, val, len + 1);
1049 | BOOL hasPrefix = len > 1 && (val[0] == TEXT('=') || val[0] == TEXT('!') || val[0] == TEXT('<') || val[0] == TEXT('>'));
1050 |
1051 | TCHAR qval[2 * len + 1];
1052 | qval[0] = TEXT('\'');
1053 | int pos = 1;
1054 | for (int i = hasPrefix; i < len; i++) {
1055 | qval[pos] = val[i];
1056 | if (val[i] == TEXT('\'')) {
1057 | pos++;
1058 | qval[pos] = val[i];
1059 | }
1060 | pos++;
1061 | }
1062 | qval[pos] = TEXT('\'');
1063 | qval[pos + 1] = 0;
1064 |
1065 | _tcscat(where, TEXT(" and \""));
1066 | _tcscat(where, colName);
1067 |
1068 | TCHAR cond[MAX_TEXT_LENGTH];
1069 | _sntprintf(cond, MAX_TEXT_LENGTH, len == 1 ? TEXT("\" like '%%' & %ls & '%%'") :
1070 | val[0] == TEXT('=') ? TEXT("\" = %ls") :
1071 | val[0] == TEXT('!') ? TEXT("\" not like '%%' & %ls & '%%'") :
1072 | val[0] == TEXT('>') ? TEXT("\" > %ls") :
1073 | val[0] == TEXT('<') ? TEXT("\" < %ls") :
1074 | TEXT("\" like '%%' & %ls & '%%'"), isNumber(val + hasPrefix) ? val + hasPrefix : qval);
1075 |
1076 | _tcscat(where, cond);
1077 | }
1078 | }
1079 |
1080 | int cacheSize = 1000;
1081 | TCHAR*** cache = calloc(cacheSize, sizeof(TCHAR*));
1082 | int rowLimit = getStoredValue(TEXT("max-row-count"), 0);
1083 |
1084 | SQLHANDLE hStmt = 0;
1085 | SQLAllocHandle(SQL_HANDLE_STMT, hConn, &hStmt);
1086 | int len = 1024 + MAX_PATH + _tcslen(tablename) + _tcslen(where);
1087 | TCHAR* query = calloc(len + 1, sizeof(TCHAR));
1088 |
1089 | TCHAR orderBy16[32] = {0};
1090 | if (orderBy > 0)
1091 | _sntprintf(orderBy16, 32, TEXT("order by %i"), orderBy);
1092 | if (orderBy < 0)
1093 | _sntprintf(orderBy16, 32, TEXT("order by %i desc"), -orderBy);
1094 |
1095 | if (isExcel) {
1096 | TCHAR* dbpath = (TCHAR*)GetProp(hWnd, TEXT("DBPATH"));
1097 | _sntprintf(query, len, TEXT("select * from \"Excel 8.0;HDR=%ls;IMEX=1;Database=%ls;\".\"%ls$\" %ls %ls"),
1098 | isHeaderRow ? TEXT("YES") : TEXT("NO"), dbpath, tablename, where, orderBy16);
1099 | } else {
1100 | _sntprintf(query, len, TEXT("select * from \"%ls\" %ls %ls"), tablename, where, orderBy16);
1101 | }
1102 |
1103 | int rowNo = -1;
1104 | if(SQL_SUCCESS == SQLExecDirect(hStmt, query, SQL_NTS)) {
1105 | rowNo = 0;
1106 | while (SQLFetch(hStmt) == SQL_SUCCESS && (rowNo < rowLimit || rowLimit == 0)) {
1107 | if (rowNo >= cacheSize) {
1108 | cacheSize += 100;
1109 | cache = realloc(cache, cacheSize * sizeof(TCHAR**));
1110 | }
1111 | cache[rowNo] = (TCHAR**)calloc (colCount, sizeof (TCHAR*));
1112 |
1113 | for (int colNo = 0; colNo < colCount; colNo++) {
1114 | SQLLEN bytes = 0;
1115 | SQLWCHAR val[MAX_DATA_LENGTH];
1116 | SQLGetData(hStmt, colNo + 1, SQL_C_TCHAR, val, MAX_DATA_LENGTH * sizeof(TCHAR), &bytes);
1117 |
1118 | int len = bytes == -1 /* NULL */ ? 0 : bytes / 2;
1119 | cache[rowNo][colNo] = calloc(len + 1, sizeof(TCHAR));
1120 |
1121 | if (len > 0) {
1122 | // Excel: fix trailing zero .0
1123 | if (isExcel && len > 2 && (val[len - 2] == TEXT('.')) && (val[len - 1] == TEXT('0'))) {
1124 | BOOL isNum = TRUE;
1125 | for (int i = 0; isNum && i < len - 2; i++)
1126 | isNum = _istdigit(val[i]);
1127 | len -= isNum ? 2 : 0;
1128 | }
1129 |
1130 | _tcsncpy(cache[rowNo][colNo], val, len);
1131 | }
1132 | }
1133 |
1134 | rowNo++;
1135 | }
1136 |
1137 | if (rowNo > 0)
1138 | cache = realloc(cache, rowNo * sizeof(TCHAR**));
1139 | }
1140 | SQLCloseCursor(hStmt);
1141 | SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
1142 | free(query);
1143 |
1144 | if (rowNo > 0) {
1145 | SetProp(hWnd, TEXT("CACHE"), cache);
1146 | } else {
1147 | free(cache);
1148 | }
1149 |
1150 | if (_tcscmp(where, TEXT("where (1 = 1)")) == 0)
1151 | *pTotalRowCount = rowNo != -1 ? rowNo : 0;
1152 | *pRowCount = rowNo != -1 ? rowNo : 0;
1153 | ListView_SetItemCount(hGridWnd, *pRowCount);
1154 |
1155 | TCHAR buf[1024];
1156 | if (rowNo != -1)
1157 | _sntprintf(buf, 255, TEXT(" Rows: %i/%i"), *pRowCount, *pTotalRowCount);
1158 | else
1159 | _sntprintf(buf, 255, TEXT(" Rows: N/A"));
1160 | SendMessage(hStatusWnd, SB_SETTEXT, SB_ROW_COUNT, (LPARAM)buf);
1161 | SendMessage(hStatusWnd, SB_SETTEXT, SB_AUXILIARY, 0);
1162 | }
1163 | break;
1164 |
1165 | case WMU_UPDATE_FILTER_SIZE: {
1166 | HWND hGridWnd = GetDlgItem(hWnd, IDC_GRID);
1167 | HWND hHeader = ListView_GetHeader(hGridWnd);
1168 | int colCount = Header_GetItemCount(hHeader);
1169 | SendMessage(hHeader, WM_SIZE, 0, 0);
1170 |
1171 | int* colOrder = calloc(colCount, sizeof(int));
1172 | Header_GetOrderArray(hHeader, colCount, colOrder);
1173 |
1174 | for (int idx = 0; idx < colCount; idx++) {
1175 | int colNo = colOrder[idx];
1176 | RECT rc;
1177 | Header_GetItemRect(hHeader, colNo, &rc);
1178 | int h2 = round((rc.bottom - rc.top) / 2);
1179 | SetWindowPos(GetDlgItem(hHeader, IDC_HEADER_EDIT + colNo), 0, rc.left, h2, rc.right - rc.left, h2 + 1, SWP_NOZORDER);
1180 | }
1181 |
1182 | free(colOrder);
1183 | }
1184 | break;
1185 |
1186 | case WMU_SET_HEADER_FILTERS: {
1187 | HWND hGridWnd = GetDlgItem(hWnd, IDC_GRID);
1188 | HWND hHeader = ListView_GetHeader(hGridWnd);
1189 | int isFilterRow = *(int*)GetProp(hWnd, TEXT("FILTERROW"));
1190 | int colCount = Header_GetItemCount(hHeader);
1191 |
1192 | SendMessage(hWnd, WM_SETREDRAW, FALSE, 0);
1193 | LONG_PTR styles = GetWindowLongPtr(hHeader, GWL_STYLE);
1194 | styles = isFilterRow ? styles | HDS_FILTERBAR : styles & (~HDS_FILTERBAR);
1195 | SetWindowLongPtr(hHeader, GWL_STYLE, styles);
1196 |
1197 | for (int colNo = 0; colNo < colCount; colNo++)
1198 | ShowWindow(GetDlgItem(hHeader, IDC_HEADER_EDIT + colNo), isFilterRow ? SW_SHOW : SW_HIDE);
1199 |
1200 | // Bug fix: force Windows to redraw header
1201 | SetWindowPos(hGridWnd, 0, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOMOVE);
1202 | SendMessage(getMainWindow(hWnd), WM_SIZE, 0, 0);
1203 |
1204 | if (isFilterRow)
1205 | SendMessage(hWnd, WMU_UPDATE_FILTER_SIZE, 0, 0);
1206 |
1207 | SendMessage(hWnd, WM_SETREDRAW, TRUE, 0);
1208 | InvalidateRect(hWnd, NULL, TRUE);
1209 | }
1210 | break;
1211 |
1212 | case WMU_AUTO_COLUMN_SIZE: {
1213 | HWND hGridWnd = GetDlgItem(hWnd, IDC_GRID);
1214 | SendMessage(hGridWnd, WM_SETREDRAW, FALSE, 0);
1215 | HWND hHeader = ListView_GetHeader(hGridWnd);
1216 | int colCount = Header_GetItemCount(hHeader);
1217 |
1218 | for (int colNo = 0; colNo < colCount - 1; colNo++)
1219 | ListView_SetColumnWidth(hGridWnd, colNo, colNo < colCount - 1 ? LVSCW_AUTOSIZE_USEHEADER : LVSCW_AUTOSIZE);
1220 |
1221 | if (colCount == 1 && ListView_GetColumnWidth(hGridWnd, 0) < 100)
1222 | ListView_SetColumnWidth(hGridWnd, 0, 100);
1223 |
1224 | int maxWidth = getStoredValue(TEXT("max-column-width"), 300);
1225 | if (colCount > 1) {
1226 | for (int colNo = 0; colNo < colCount; colNo++) {
1227 | if (ListView_GetColumnWidth(hGridWnd, colNo) > maxWidth)
1228 | ListView_SetColumnWidth(hGridWnd, colNo, maxWidth);
1229 | }
1230 | }
1231 |
1232 | // Fix last column
1233 | if (colCount > 1) {
1234 | int colNo = colCount - 1;
1235 | ListView_SetColumnWidth(hGridWnd, colNo, LVSCW_AUTOSIZE);
1236 | TCHAR name16[MAX_COLUMN_LENGTH + 1];
1237 | Header_GetItemText(hHeader, colNo, name16, MAX_COLUMN_LENGTH);
1238 |
1239 | SIZE s = {0};
1240 | HDC hDC = GetDC(hHeader);
1241 | HFONT hOldFont = (HFONT)SelectObject(hDC, (HFONT)GetProp(hWnd, TEXT("FONT")));
1242 | GetTextExtentPoint32(hDC, name16, _tcslen(name16), &s);
1243 | SelectObject(hDC, hOldFont);
1244 | ReleaseDC(hHeader, hDC);
1245 |
1246 | int w = s.cx + 12;
1247 | if (ListView_GetColumnWidth(hGridWnd, colNo) < w)
1248 | ListView_SetColumnWidth(hGridWnd, colNo, w);
1249 |
1250 | if (ListView_GetColumnWidth(hGridWnd, colNo) > maxWidth)
1251 | ListView_SetColumnWidth(hGridWnd, colNo, maxWidth);
1252 | }
1253 |
1254 | SendMessage(hGridWnd, WM_SETREDRAW, TRUE, 0);
1255 | InvalidateRect(hGridWnd, NULL, TRUE);
1256 |
1257 | PostMessage(hWnd, WMU_UPDATE_FILTER_SIZE, 0, 0);
1258 | }
1259 | break;
1260 |
1261 | // wParam = rowNo, lParam = colNo
1262 | case WMU_SET_CURRENT_CELL: {
1263 | HWND hGridWnd = GetDlgItem(hWnd, IDC_GRID);
1264 | HWND hHeader = ListView_GetHeader(hGridWnd);
1265 | HWND hStatusWnd = GetDlgItem(hWnd, IDC_STATUSBAR);
1266 | SendMessage(hStatusWnd, SB_SETTEXT, SB_AUXILIARY, (LPARAM)0);
1267 |
1268 | int *pRowNo = (int*)GetProp(hWnd, TEXT("CURRENTROWNO"));
1269 | int *pColNo = (int*)GetProp(hWnd, TEXT("CURRENTCOLNO"));
1270 | if (*pRowNo == wParam && *pColNo == lParam)
1271 | return 0;
1272 |
1273 | RECT rc, rc2;
1274 | ListView_GetSubItemRect(hGridWnd, *pRowNo, *pColNo, LVIR_BOUNDS, &rc);
1275 | if (*pColNo == 0)
1276 | rc.right = ListView_GetColumnWidth(hGridWnd, *pColNo);
1277 | InvalidateRect(hGridWnd, &rc, TRUE);
1278 |
1279 | *pRowNo = wParam;
1280 | *pColNo = lParam;
1281 | ListView_GetSubItemRect(hGridWnd, *pRowNo, *pColNo, LVIR_BOUNDS, &rc);
1282 | if (*pColNo == 0)
1283 | rc.right = ListView_GetColumnWidth(hGridWnd, *pColNo);
1284 | InvalidateRect(hGridWnd, &rc, FALSE);
1285 |
1286 | GetClientRect(hGridWnd, &rc2);
1287 | int w = rc.right - rc.left;
1288 | int dx = rc2.right < rc.right ? rc.left - rc2.right + w : rc.left < 0 ? rc.left : 0;
1289 |
1290 | ListView_Scroll(hGridWnd, dx, 0);
1291 |
1292 | TCHAR buf[32] = {0};
1293 | if (*pColNo != - 1 && *pRowNo != -1)
1294 | _sntprintf(buf, 32, TEXT(" %i:%i"), *pRowNo + 1, *pColNo + 1);
1295 | SendMessage(hStatusWnd, SB_SETTEXT, SB_CURRENT_CELL, (LPARAM)buf);
1296 | }
1297 | break;
1298 |
1299 | case WMU_RESET_CACHE: {
1300 | HWND hGridWnd = GetDlgItem(hWnd, IDC_GRID);
1301 | TCHAR*** cache = (TCHAR***)GetProp(hWnd, TEXT("CACHE"));
1302 | int* pRowCount = (int*)GetProp(hWnd, TEXT("ROWCOUNT"));
1303 |
1304 | int colCount = Header_GetItemCount(ListView_GetHeader(hGridWnd));
1305 | if (colCount > 0 && cache != 0) {
1306 | for (int rowNo = 0; rowNo < *pRowCount; rowNo++) {
1307 | if (cache[rowNo]) {
1308 | for (int colNo = 0; colNo < colCount; colNo++)
1309 | if (cache[rowNo][colNo])
1310 | free(cache[rowNo][colNo]);
1311 |
1312 | free(cache[rowNo]);
1313 | }
1314 | cache[rowNo] = 0;
1315 | }
1316 | free(cache);
1317 | }
1318 |
1319 | SetProp(hWnd, TEXT("CACHE"), 0);
1320 | *pRowCount = 0;
1321 | }
1322 | break;
1323 |
1324 | // wParam - size delta
1325 | case WMU_SET_FONT: {
1326 | int* pFontSize = (int*)GetProp(hWnd, TEXT("FONTSIZE"));
1327 | if (*pFontSize + wParam < 10 || *pFontSize + wParam > 48)
1328 | return 0;
1329 | *pFontSize += wParam;
1330 | DeleteFont(GetProp(hWnd, TEXT("FONT")));
1331 |
1332 | HFONT hFont = CreateFont (*pFontSize, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, (TCHAR*)GetProp(hWnd, TEXT("FONTFAMILY")));
1333 | HWND hListWnd = GetDlgItem(hWnd, IDC_TABLELIST);
1334 | HWND hGridWnd = GetDlgItem(hWnd, IDC_GRID);
1335 | SendMessage(hListWnd, WM_SETFONT, (LPARAM)hFont, TRUE);
1336 | SendMessage(hGridWnd, WM_SETFONT, (LPARAM)hFont, TRUE);
1337 |
1338 | HWND hHeader = ListView_GetHeader(hGridWnd);
1339 | for (int colNo = 0; colNo < Header_GetItemCount(hHeader); colNo++)
1340 | SendMessage(GetDlgItem(hHeader, IDC_HEADER_EDIT + colNo), WM_SETFONT, (LPARAM)hFont, TRUE);
1341 |
1342 | int w = 0;
1343 | HDC hDC = GetDC(hListWnd);
1344 | HFONT hOldFont = (HFONT)SelectObject(hDC, hFont);
1345 | TCHAR buf[MAX_TABLE_LENGTH];
1346 | for (int i = 0; i < ListBox_GetCount(hListWnd); i++) {
1347 | SIZE s = {0};
1348 | ListBox_GetText(hListWnd, i, buf);
1349 | GetTextExtentPoint32(hDC, buf, _tcslen(buf), &s);
1350 | if (w < s.cx)
1351 | w = s.cx;
1352 | }
1353 | SelectObject(hDC, hOldFont);
1354 | ReleaseDC(hHeader, hDC);
1355 | SendMessage(hListWnd, LB_SETHORIZONTALEXTENT, w, 0);
1356 |
1357 | SetProp(hWnd, TEXT("FONT"), hFont);
1358 | PostMessage(hWnd, WMU_AUTO_COLUMN_SIZE, 0, 0);
1359 | }
1360 | break;
1361 |
1362 | case WMU_SET_THEME: {
1363 | HWND hGridWnd = GetDlgItem(hWnd, IDC_GRID);
1364 | BOOL isDark = *(int*)GetProp(hWnd, TEXT("DARKTHEME"));
1365 |
1366 | int textColor = !isDark ? getStoredValue(TEXT("text-color"), RGB(0, 0, 0)) : getStoredValue(TEXT("text-color-dark"), RGB(220, 220, 220));
1367 | int backColor = !isDark ? getStoredValue(TEXT("back-color"), RGB(255, 255, 255)) : getStoredValue(TEXT("back-color-dark"), RGB(32, 32, 32));
1368 | int backColor2 = !isDark ? getStoredValue(TEXT("back-color2"), RGB(240, 240, 240)) : getStoredValue(TEXT("back-color2-dark"), RGB(52, 52, 52));
1369 | int filterTextColor = !isDark ? getStoredValue(TEXT("filter-text-color"), RGB(0, 0, 0)) : getStoredValue(TEXT("filter-text-color-dark"), RGB(255, 255, 255));
1370 | int filterBackColor = !isDark ? getStoredValue(TEXT("filter-back-color"), RGB(240, 240, 240)) : getStoredValue(TEXT("filter-back-color-dark"), RGB(60, 60, 60));
1371 | int currCellColor = !isDark ? getStoredValue(TEXT("current-cell-back-color"), RGB(70, 96, 166)) : getStoredValue(TEXT("current-cell-back-color-dark"), RGB(32, 62, 62));
1372 | int selectionTextColor = !isDark ? getStoredValue(TEXT("selection-text-color"), RGB(255, 255, 255)) : getStoredValue(TEXT("selection-text-color-dark"), RGB(220, 220, 220));
1373 | int selectionBackColor = !isDark ? getStoredValue(TEXT("selection-back-color"), RGB(10, 36, 106)) : getStoredValue(TEXT("selection-back-color-dark"), RGB(72, 102, 102));
1374 | int splitterColor = !isDark ? getStoredValue(TEXT("splitter-color"), GetSysColor(COLOR_BTNFACE)) : getStoredValue(TEXT("splitter-color-dark"), GetSysColor(COLOR_BTNFACE));
1375 |
1376 | *(int*)GetProp(hWnd, TEXT("TEXTCOLOR")) = textColor;
1377 | *(int*)GetProp(hWnd, TEXT("BACKCOLOR")) = backColor;
1378 | *(int*)GetProp(hWnd, TEXT("BACKCOLOR2")) = backColor2;
1379 | *(int*)GetProp(hWnd, TEXT("FILTERTEXTCOLOR")) = filterTextColor;
1380 | *(int*)GetProp(hWnd, TEXT("FILTERBACKCOLOR")) = filterBackColor;
1381 | *(int*)GetProp(hWnd, TEXT("CURRENTCELLCOLOR")) = currCellColor;
1382 | *(int*)GetProp(hWnd, TEXT("SELECTIONTEXTCOLOR")) = selectionTextColor;
1383 | *(int*)GetProp(hWnd, TEXT("SELECTIONBACKCOLOR")) = selectionBackColor;
1384 | *(int*)GetProp(hWnd, TEXT("SPLITTERCOLOR")) = splitterColor;
1385 |
1386 | DeleteObject(GetProp(hWnd, TEXT("BACKBRUSH")));
1387 | DeleteObject(GetProp(hWnd, TEXT("FILTERBACKBRUSH")));
1388 | DeleteObject(GetProp(hWnd, TEXT("SPLITTERBRUSH")));
1389 | SetProp(hWnd, TEXT("BACKBRUSH"), CreateSolidBrush(backColor));
1390 | SetProp(hWnd, TEXT("FILTERBACKBRUSH"), CreateSolidBrush(filterBackColor));
1391 | SetProp(hWnd, TEXT("SPLITTERBRUSH"), CreateSolidBrush(splitterColor));
1392 |
1393 | ListView_SetTextColor(hGridWnd, textColor);
1394 | ListView_SetBkColor(hGridWnd, backColor);
1395 | ListView_SetTextBkColor(hGridWnd, backColor);
1396 | InvalidateRect(hWnd, NULL, TRUE);
1397 | }
1398 | break;
1399 |
1400 | case WMU_HOT_KEYS: {
1401 | BOOL isCtrl = HIWORD(GetKeyState(VK_CONTROL));
1402 | if (wParam == VK_TAB) {
1403 | HWND hFocus = GetFocus();
1404 | HWND wnds[1000] = {0};
1405 | EnumChildWindows(hWnd, (WNDENUMPROC)cbEnumTabStopChildren, (LPARAM)wnds);
1406 |
1407 | int no = 0;
1408 | while(wnds[no] && wnds[no] != hFocus)
1409 | no++;
1410 |
1411 | int cnt = no;
1412 | while(wnds[cnt])
1413 | cnt++;
1414 |
1415 | no += isCtrl ? -1 : 1;
1416 | SetFocus(wnds[no] && no >= 0 ? wnds[no] : (isCtrl ? wnds[cnt - 1] : wnds[0]));
1417 | }
1418 |
1419 | if (wParam == VK_F1) {
1420 | ShellExecute(0, 0, TEXT("https://github.com/little-brother/odbc-wlx/wiki"), 0, 0 , SW_SHOW);
1421 | return TRUE;
1422 | }
1423 |
1424 | if (wParam == 0x20 && isCtrl) { // Ctrl + Space
1425 | SendMessage(hWnd, WMU_SHOW_COLUMNS, 0, 0);
1426 | return TRUE;
1427 | }
1428 |
1429 | if (wParam == VK_ESCAPE || wParam == VK_F11 ||
1430 | wParam == VK_F3 || wParam == VK_F5 || wParam == VK_F7 || (isCtrl && wParam == 0x46) || // Ctrl + F
1431 | ((wParam >= 0x31 && wParam <= 0x38) && !getStoredValue(TEXT("disable-num-keys"), 0) && !isCtrl || // 1 - 8
1432 | (wParam == 0x4E || wParam == 0x50) && !getStoredValue(TEXT("disable-np-keys"), 0) || // N, P
1433 | wParam == 0x51 && getStoredValue(TEXT("exit-by-q"), 0)) && // Q
1434 | GetDlgCtrlID(GetFocus()) / 100 * 100 != IDC_HEADER_EDIT) {
1435 | SetFocus(GetParent(hWnd));
1436 | keybd_event(wParam, wParam, KEYEVENTF_EXTENDEDKEY, 0);
1437 |
1438 | return TRUE;
1439 | }
1440 |
1441 | if (isCtrl && wParam >= 0x30 && wParam <= 0x39 && !getStoredValue(TEXT("disable-num-keys"), 0)) {// 0-9
1442 | HWND hGridWnd = GetDlgItem(hWnd, IDC_GRID);
1443 | HWND hHeader = ListView_GetHeader(hGridWnd);
1444 | int colCount = Header_GetItemCount(ListView_GetHeader(hGridWnd));
1445 |
1446 | BOOL isCurrent = wParam == 0x30;
1447 | int colNo = isCurrent ? *(int*)GetProp(hWnd, TEXT("CURRENTCOLNO")) : wParam - 0x30 - 1;
1448 | if (colNo < 0 || colNo > colCount - 1 || isCurrent && ListView_GetColumnWidth(hGridWnd, colNo) == 0)
1449 | return FALSE;
1450 |
1451 | if (!isCurrent) {
1452 | int* colOrder = calloc(colCount, sizeof(int));
1453 | Header_GetOrderArray(hHeader, colCount, colOrder);
1454 |
1455 | int hiddenColCount = 0;
1456 | for (int idx = 0; (idx < colCount) && (idx - hiddenColCount <= colNo); idx++)
1457 | hiddenColCount += ListView_GetColumnWidth(hGridWnd, colOrder[idx]) == 0;
1458 |
1459 | colNo = colOrder[colNo + hiddenColCount];
1460 | free(colOrder);
1461 | }
1462 |
1463 | SendMessage(hWnd, WMU_SORT_COLUMN, colNo, 0);
1464 | return TRUE;
1465 | }
1466 |
1467 | return FALSE;
1468 | }
1469 | break;
1470 |
1471 | case WMU_HOT_CHARS: {
1472 | BOOL isCtrl = HIWORD(GetKeyState(VK_CONTROL));
1473 |
1474 | unsigned char scancode = ((unsigned char*)&lParam)[2];
1475 | UINT key = MapVirtualKey(scancode, MAPVK_VSC_TO_VK);
1476 |
1477 | return !_istprint(wParam) && (
1478 | wParam == VK_ESCAPE || wParam == VK_F11 || wParam == VK_F1 ||
1479 | wParam == VK_F3 || wParam == VK_F5 || wParam == VK_F7) ||
1480 | wParam == VK_TAB || wParam == VK_RETURN ||
1481 | isCtrl && key == 0x46 || // Ctrl + F
1482 | getStoredValue(TEXT("exit-by-q"), 0) && key == 0x51 && GetDlgCtrlID(GetFocus()) / 100 * 100 != IDC_HEADER_EDIT; // Q
1483 | }
1484 | break;
1485 | }
1486 |
1487 | return CallWindowProc((WNDPROC)GetProp(hWnd, TEXT("WNDPROC")), hWnd, msg, wParam, lParam);
1488 | }
1489 |
1490 | LRESULT CALLBACK cbHotKey(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
1491 | if (msg == WM_KEYDOWN && SendMessage(getMainWindow(hWnd), WMU_HOT_KEYS, wParam, lParam))
1492 | return 0;
1493 |
1494 | // Prevent beep
1495 | if (msg == WM_CHAR && SendMessage(getMainWindow(hWnd), WMU_HOT_CHARS, wParam, lParam))
1496 | return 0;
1497 |
1498 | return CallWindowProc((WNDPROC)GetProp(hWnd, TEXT("WNDPROC")), hWnd, msg, wParam, lParam);
1499 | }
1500 |
1501 | LRESULT CALLBACK cbNewHeader(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
1502 | if (msg == WM_CTLCOLOREDIT) {
1503 | HWND hMainWnd = getMainWindow(hWnd);
1504 | SetBkColor((HDC)wParam, *(int*)GetProp(hMainWnd, TEXT("FILTERBACKCOLOR")));
1505 | SetTextColor((HDC)wParam, *(int*)GetProp(hMainWnd, TEXT("FILTERTEXTCOLOR")));
1506 | return (INT_PTR)(HBRUSH)GetProp(hMainWnd, TEXT("FILTERBACKBRUSH"));
1507 | }
1508 |
1509 | return CallWindowProc((WNDPROC)GetProp(hWnd, TEXT("WNDPROC")), hWnd, msg, wParam, lParam);
1510 | }
1511 |
1512 | LRESULT CALLBACK cbNewFilterEdit(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
1513 | WNDPROC cbDefault = (WNDPROC)GetProp(hWnd, TEXT("WNDPROC"));
1514 |
1515 | switch(msg){
1516 | case WM_PAINT: {
1517 | cbDefault(hWnd, msg, wParam, lParam);
1518 |
1519 | RECT rc;
1520 | GetClientRect(hWnd, &rc);
1521 | HWND hMainWnd = getMainWindow(hWnd);
1522 | BOOL isDark = *(int*)GetProp(hMainWnd, TEXT("DARKTHEME"));
1523 |
1524 | HDC hDC = GetWindowDC(hWnd);
1525 | HPEN hPen = CreatePen(PS_SOLID, 1, *(int*)GetProp(hMainWnd, TEXT("FILTERBACKCOLOR")));
1526 | HPEN oldPen = SelectObject(hDC, hPen);
1527 | MoveToEx(hDC, 1, 0, 0);
1528 | LineTo(hDC, rc.right - 1, 0);
1529 | LineTo(hDC, rc.right - 1, rc.bottom - 1);
1530 |
1531 | if (isDark) {
1532 | DeleteObject(hPen);
1533 | hPen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_BTNFACE));
1534 | SelectObject(hDC, hPen);
1535 |
1536 | MoveToEx(hDC, 0, 0, 0);
1537 | LineTo(hDC, 0, rc.bottom);
1538 | MoveToEx(hDC, 0, rc.bottom - 1, 0);
1539 | LineTo(hDC, rc.right, rc.bottom - 1);
1540 | MoveToEx(hDC, 0, rc.bottom - 2, 0);
1541 | LineTo(hDC, rc.right, rc.bottom - 2);
1542 | }
1543 |
1544 | SelectObject(hDC, oldPen);
1545 | DeleteObject(hPen);
1546 | ReleaseDC(hWnd, hDC);
1547 |
1548 | return 0;
1549 | }
1550 | break;
1551 |
1552 | case WM_SETFOCUS: {
1553 | SetProp(getMainWindow(hWnd), TEXT("LASTFOCUS"), hWnd);
1554 | }
1555 | break;
1556 |
1557 | case WM_KEYDOWN: {
1558 | HWND hMainWnd = getMainWindow(hWnd);
1559 | if (wParam == VK_RETURN) {
1560 | SendMessage(hMainWnd, WMU_UPDATE_CACHE, 0, 0);
1561 | return 0;
1562 | }
1563 |
1564 | if (SendMessage(hMainWnd, WMU_HOT_KEYS, wParam, lParam))
1565 | return 0;
1566 | }
1567 | break;
1568 |
1569 | // Prevent beep
1570 | case WM_CHAR: {
1571 | if (SendMessage(getMainWindow(hWnd), WMU_HOT_CHARS, wParam, lParam))
1572 | return 0;
1573 | }
1574 | break;
1575 |
1576 | case WM_DESTROY: {
1577 | RemoveProp(hWnd, TEXT("WNDPROC"));
1578 | }
1579 | break;
1580 | }
1581 |
1582 | return CallWindowProc(cbDefault, hWnd, msg, wParam, lParam);
1583 | }
1584 |
1585 | HWND getMainWindow(HWND hWnd) {
1586 | HWND hMainWnd = hWnd;
1587 | while (hMainWnd && GetDlgCtrlID(hMainWnd) != IDC_MAIN)
1588 | hMainWnd = GetParent(hMainWnd);
1589 | return hMainWnd;
1590 | }
1591 |
1592 | void setStoredValue(TCHAR* name, int value) {
1593 | TCHAR buf[128];
1594 | _sntprintf(buf, 128, TEXT("%i"), value);
1595 | WritePrivateProfileString(APP_NAME, name, buf, iniPath);
1596 | }
1597 |
1598 | int getStoredValue(TCHAR* name, int defValue) {
1599 | TCHAR buf[128];
1600 | return GetPrivateProfileString(APP_NAME, name, NULL, buf, 128, iniPath) ? _ttoi(buf) : defValue;
1601 | }
1602 |
1603 | TCHAR* getStoredString(TCHAR* name, TCHAR* defValue) {
1604 | TCHAR* buf = calloc(256, sizeof(TCHAR));
1605 | if (0 == GetPrivateProfileString(APP_NAME, name, NULL, buf, 128, iniPath) && defValue)
1606 | _tcsncpy(buf, defValue, 255);
1607 | return buf;
1608 | }
1609 |
1610 | int CALLBACK cbEnumTabStopChildren (HWND hWnd, LPARAM lParam) {
1611 | if (GetWindowLong(hWnd, GWL_STYLE) & WS_TABSTOP && IsWindowVisible(hWnd)) {
1612 | int no = 0;
1613 | HWND* wnds = (HWND*)lParam;
1614 | while (wnds[no])
1615 | no++;
1616 | wnds[no] = hWnd;
1617 | }
1618 |
1619 | return TRUE;
1620 | }
1621 |
1622 | TCHAR* utf8to16(const char* in) {
1623 | TCHAR *out;
1624 | if (!in || strlen(in) == 0) {
1625 | out = (TCHAR*)calloc (1, sizeof (TCHAR));
1626 | } else {
1627 | DWORD size = MultiByteToWideChar(CP_UTF8, 0, in, -1, NULL, 0);
1628 | out = (TCHAR*)calloc (size, sizeof (TCHAR));
1629 | MultiByteToWideChar(CP_UTF8, 0, in, -1, out, size);
1630 | }
1631 | return out;
1632 | }
1633 |
1634 | char* utf16to8(const TCHAR* in) {
1635 | char* out;
1636 | if (!in || _tcslen(in) == 0) {
1637 | out = (char*)calloc (1, sizeof(char));
1638 | } else {
1639 | int len = WideCharToMultiByte(CP_UTF8, 0, in, -1, NULL, 0, 0, 0);
1640 | out = (char*)calloc (len, sizeof(char));
1641 | WideCharToMultiByte(CP_UTF8, 0, in, -1, out, len, 0, 0);
1642 | }
1643 | return out;
1644 | }
1645 |
1646 | int findString(TCHAR* text, TCHAR* word, BOOL isMatchCase, BOOL isWholeWords) {
1647 | if (!text || !word)
1648 | return -1;
1649 |
1650 | int res = -1;
1651 | int tlen = _tcslen(text);
1652 | int wlen = _tcslen(word);
1653 | if (!tlen || !wlen)
1654 | return res;
1655 |
1656 | if (!isMatchCase) {
1657 | TCHAR* ltext = calloc(tlen + 1, sizeof(TCHAR));
1658 | _tcsncpy(ltext, text, tlen);
1659 | text = _tcslwr(ltext);
1660 |
1661 | TCHAR* lword = calloc(wlen + 1, sizeof(TCHAR));
1662 | _tcsncpy(lword, word, wlen);
1663 | word = _tcslwr(lword);
1664 | }
1665 |
1666 | if (isWholeWords) {
1667 | for (int pos = 0; (res == -1) && (pos <= tlen - wlen); pos++)
1668 | res = (pos == 0 || pos > 0 && !_istalnum(text[pos - 1])) &&
1669 | !_istalnum(text[pos + wlen]) &&
1670 | _tcsncmp(text + pos, word, wlen) == 0 ? pos : -1;
1671 | } else {
1672 | TCHAR* s = _tcsstr(text, word);
1673 | res = s != NULL ? s - text : -1;
1674 | }
1675 |
1676 | if (!isMatchCase) {
1677 | free(text);
1678 | free(word);
1679 | }
1680 |
1681 | return res;
1682 | }
1683 |
1684 | TCHAR* extractUrl(TCHAR* data) {
1685 | int len = data ? _tcslen(data) : 0;
1686 | int start = len;
1687 | int end = len;
1688 |
1689 | TCHAR* url = calloc(len + 10, sizeof(TCHAR));
1690 |
1691 | TCHAR* slashes = _tcsstr(data, TEXT("://"));
1692 | if (slashes) {
1693 | start = len - _tcslen(slashes);
1694 | end = start + 3;
1695 | for (; start > 0 && _istalpha(data[start - 1]); start--);
1696 | for (; end < len && data[end] != TEXT(' ') && data[end] != TEXT('"') && data[end] != TEXT('\''); end++);
1697 | _tcsncpy(url, data + start, end - start);
1698 |
1699 | } else if (_tcschr(data, TEXT('.'))) {
1700 | _sntprintf(url, len + 10, TEXT("https://%ls"), data);
1701 | }
1702 |
1703 | return url;
1704 | }
1705 |
1706 | void setClipboardText(const TCHAR* text) {
1707 | int len = (_tcslen(text) + 1) * sizeof(TCHAR);
1708 | HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, len);
1709 | memcpy(GlobalLock(hMem), text, len);
1710 | GlobalUnlock(hMem);
1711 | OpenClipboard(0);
1712 | EmptyClipboard();
1713 | SetClipboardData(CF_UNICODETEXT, hMem);
1714 | CloseClipboard();
1715 | }
1716 |
1717 | BOOL isNumber(TCHAR* val) {
1718 | int len = _tcslen(val);
1719 | BOOL res = TRUE;
1720 | int pCount = 0;
1721 | for (int i = 0; res && i < len; i++) {
1722 | pCount += val[i] == TEXT('.');
1723 | res = _istdigit(val[i]) || val[i] == TEXT('.');
1724 | }
1725 | return res && pCount < 2;
1726 | }
1727 |
1728 | int ListView_AddColumn(HWND hListWnd, TCHAR* colName, int fmt) {
1729 | int colNo = Header_GetItemCount(ListView_GetHeader(hListWnd));
1730 | LVCOLUMN lvc = {0};
1731 | lvc.mask = LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM | LVCF_FMT;
1732 | lvc.iSubItem = colNo;
1733 | lvc.pszText = colName;
1734 | lvc.cchTextMax = _tcslen(colName) + 1;
1735 | lvc.cx = 100;
1736 | lvc.fmt = fmt;
1737 | return ListView_InsertColumn(hListWnd, colNo, &lvc);
1738 | }
1739 |
1740 | int Header_GetItemText(HWND hWnd, int i, TCHAR* pszText, int cchTextMax) {
1741 | if (i < 0)
1742 | return FALSE;
1743 |
1744 | TCHAR buf[cchTextMax];
1745 |
1746 | HDITEM hdi = {0};
1747 | hdi.mask = HDI_TEXT;
1748 | hdi.pszText = buf;
1749 | hdi.cchTextMax = cchTextMax;
1750 | int rc = Header_GetItem(hWnd, i, &hdi);
1751 |
1752 | _tcsncpy(pszText, buf, cchTextMax);
1753 | return rc;
1754 | }
1755 |
1756 | void Menu_SetItemState(HMENU hMenu, UINT wID, UINT fState) {
1757 | MENUITEMINFO mii = {0};
1758 | mii.cbSize = sizeof(MENUITEMINFO);
1759 | mii.fMask = MIIM_STATE;
1760 | mii.fState = fState;
1761 | SetMenuItemInfo(hMenu, wID, FALSE, &mii);
1762 | }
--------------------------------------------------------------------------------