Add ability to use PATH from the specified environment
[git-cheetah/kirill.git] / menu.c
blobb8098d29739e354f6be21f40df22f8781380a96c
1 #include <shlobj.h>
2 #include <tchar.h>
3 #include <stdio.h>
4 #include <process.h>
5 #include "menu.h"
6 #include "ext.h"
7 #include "debug.h"
8 #include "systeminfo.h"
9 #include "exec.h"
11 #define LONGEST_MENU_ITEM 40
14 * These are the functions for handling the context menu.
17 static STDMETHODIMP query_context_menu(void *p, HMENU menu,
18 UINT index, UINT first_command,
19 UINT last_command, UINT flags)
21 struct git_menu *this_menu = p;
22 struct git_data *this_ = this_menu->git_data;
23 char *wd;
24 BOOL bDirSelected = TRUE;
26 UINT original_first = first_command;
27 char menu_item[LONGEST_MENU_ITEM];
29 int status;
31 if (flags & CMF_DEFAULTONLY)
32 return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 0);
34 /* figure out the directory */
35 wd = strdup(this_->name);
36 if (!(FILE_ATTRIBUTE_DIRECTORY & GetFileAttributes(wd))) {
37 char *c = strrchr(wd, '\\');
38 if (c) {
39 *c = 0;
40 bDirSelected = FALSE;
44 status = exec_git("rev-parse --show-cdup", wd, P_WAIT);
45 free (wd);
47 if (-1 == status)
48 return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 0);
51 * TODO: the following big, ugly code needs to be something like
52 * build_menu_items()
53 * It's left as is to signify the preview nature of the patch
55 if (0 != status) { /* this is not a repository */
56 if (bDirSelected)
57 strcpy(menu_item, "&Git Clone Here");
58 else
59 strcpy(menu_item, "&Git Init Here");
60 } else
61 strcpy(menu_item, "&Git");
63 InsertMenu(menu, index, MF_SEPARATOR | MF_BYPOSITION,
64 first_command++, "");
65 InsertMenu(menu, index+1, MF_STRING | MF_BYPOSITION,
66 first_command++, menu_item);
69 * TODO: when the above block is fixed, we'll just have
70 * return MAKE_RESULT(..., build_menu_items());
72 return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL,
73 first_command - original_first);
77 * Perform a couple of transformations, such that a directory
78 * C:\Program Files\Bunch of stuff\in\A dir
79 * becomes
80 * /C/Program\ Files/Bunch\ of\ stuff/in/A\ dir
82 * Assumes path is initially a correctly formed Windows-style path.
83 * Returns a new string.
85 static char *convert_directory_format(const char *path)
87 int i;
88 int size_incr = 0;
89 char *converted;
90 char *dst;
92 /* Figure out how much extra space we need to escape spaces */
93 for (i = 0; i < MAX_PATH && path[i] != '\0'; ++i)
94 if (path[i] == ' ')
95 size_incr++;
97 converted = (char *)calloc(size_incr + i + 1, sizeof(char));
98 dst = converted;
100 /* Transform:
101 * " " -> "\ "
102 * "\" -> "/"
104 for (i = 0; i < MAX_PATH && path[i] != '\0'; ++i)
106 switch (path[i])
108 case ' ':
109 *(dst++) = '\\';
110 *(dst++) = ' ';
111 break;
112 case '\\':
113 *(dst++) = '/';
114 break;
115 default:
116 *(dst++) = path[i];
117 break;
120 *dst = '\0';
122 /* X: -> /X */
123 converted[1] = converted[0];
124 converted[0] = '/';
126 return converted;
129 static STDMETHODIMP invoke_command(void *p,
130 LPCMINVOKECOMMANDINFO info)
132 struct git_menu *this_menu = p;
133 struct git_data *this_ = this_menu->git_data;
134 int command = LOWORD(info->lpVerb);
136 if (HIWORD(info->lpVerb) != 0)
137 return E_INVALIDARG;
139 if (command == 1)
141 const char *wd;
142 DWORD dwAttr, fa;
144 wd = this_->name;
145 if (wd == NULL || strlen(wd) == 0)
146 wd = info->lpDirectory;
148 dwAttr = FILE_ATTRIBUTE_DIRECTORY;
149 fa = GetFileAttributes(wd);
150 if (!(fa & dwAttr))
151 wd = info->lpDirectory;
153 exec_git("gui", wd, P_NOWAIT);
155 return S_OK;
158 return E_INVALIDARG;
161 static STDMETHODIMP get_command_string(void *p, UINT id,
162 UINT flags, UINT *reserved,
163 LPSTR name, UINT size)
166 struct git_menu *this_menu = p;
167 struct git_data *this_ = this_menu->git_data;
169 if (id != 1)
170 return E_INVALIDARG;
173 if (flags & GCS_HELPTEXT) {
174 LPCTSTR text = _T("Launch the GIT Gui in the local or chosen directory.");
175 size_t len = strlen(text) + 1;
176 LPWSTR tw = malloc(len * sizeof(wchar_t));
177 /* need to convert terminating NULL as well */
178 mbstowcs(tw, text, len);
179 /* use Win32 lstrcpyn to [automatically] avoid buffer overflow */
180 if (flags & GCS_UNICODE)
181 lstrcpynW((LPWSTR)name, tw, size);
182 else
183 lstrcpynA(name, text, size);
184 free(tw);
185 return S_OK;
189 return E_INVALIDARG;
192 DEFINE_STANDARD_METHODS(git_menu)
194 struct git_menu_virtual_table git_menu_virtual_table = {
195 query_interface_git_menu,
196 add_ref_git_menu,
197 release_git_menu,
198 query_context_menu,
199 invoke_command,
200 get_command_string