ntutil.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #include "ntdef.h"
  2. static struct NT_window *window_list=NULL;
  3. void
  4. freeMemory(void *p) {
  5. if (p!=NULL) free(p);
  6. }
  7. void *
  8. allocateMemory(int s) {
  9. void *p=NULL;
  10. if (s)
  11. {
  12. p=(void *)malloc(s);
  13. if (p) memset(p,0,s);
  14. }
  15. return p;
  16. }
  17. /*---------------------------------------------------*\
  18. | Function: NT_new_window |
  19. | Purpose: Add a new window id to the Window table |
  20. | Return: Pointer to the new Window structure. |
  21. \*---------------------------------------------------*/
  22. struct NT_window *
  23. NT_new_window()
  24. {
  25. struct NT_window *new;
  26. xtrace("NT_new_window\n");
  27. new = (struct NT_window *) allocateMemory (sizeof(struct NT_window));
  28. new->next = window_list;
  29. new->child=NULL;
  30. new->min = 1;
  31. new->minx =0;
  32. new->miny =0;
  33. new->w = INVALID_HANDLE;
  34. new->parent= NULL;
  35. new->hBitmap = INVALID_HANDLE;
  36. new->hDC = INVALID_HANDLE;
  37. window_list = new;
  38. cjh_printf("NEW window %x\n",window_list);
  39. return(window_list);
  40. }
  41. /*---------------------------------------------------*\
  42. | Function: NT_delete_window |
  43. | Purpose: Remove a window from the window list |
  44. | Input: w - pointer to window data |
  45. | Return: TRUE if deleted |
  46. \*---------------------------------------------------*/
  47. int
  48. NT_delete_window(struct NT_window *w)
  49. {
  50. NT_window *f;
  51. xtrace("NT_delete_window\n");
  52. if (w->w != INVALID_HANDLE)
  53. {
  54. /* ShowWindow(w->w,SW_HIDE);*/
  55. DestroyWindow(w->w);
  56. w->w=INVALID_HANDLE;
  57. }
  58. if (w->hBitmap != INVALID_HANDLE)
  59. {
  60. DeleteObject(w->hBitmap);
  61. w->hBitmap = INVALID_HANDLE;
  62. }
  63. if (w->hDC != INVALID_HANDLE)
  64. {
  65. DeleteDC(w->hDC);
  66. w->hDC=INVALID_HANDLE;
  67. }
  68. if (window_list == w)
  69. window_list=w->next;
  70. else
  71. {
  72. for (f=window_list; f!=NULL && f->next!=w; f=f->next);
  73. if (f!=NULL)
  74. f->next = w->next;
  75. }
  76. freeMemory(w);
  77. return TRUE;
  78. }
  79. /*------------------------------------------------*\
  80. | Function: NT_find_window_from_id |
  81. | Purpose: Find the window in the window list |
  82. | from the HWND id of the window. |
  83. | Input: w - Window id (Windows HWND) |
  84. | Return: pointer to NT_window structure for w. |
  85. \*------------------------------------------------*/
  86. struct NT_window *
  87. NT_find_window_from_id(HWND w)
  88. {
  89. struct NT_window *current = window_list;
  90. /* xtrace("NT_find_window_from_id\n"); */
  91. while ( current != NULL &&
  92. current->w != w )
  93. current = current->next;
  94. if(current)
  95. return(current);
  96. current=window_list;
  97. return NULL;
  98. }
  99. /*****************************************************************\
  100. Function: NT_add_child
  101. Inputs: parent and child window IDs.
  102. Returned: 1
  103. Comments: When a child window is created (eg. client canvas) we
  104. update our internal list of windows and their children.
  105. New children are added to the front of the list.
  106. \*****************************************************************/
  107. int
  108. NT_add_child(parent,child)
  109. NT_window *parent,*child;
  110. {
  111. struct NT_child *new;
  112. new=(struct NT_child *) allocateMemory (sizeof(struct NT_child));
  113. new->w=child;
  114. new->next = parent->child;
  115. parent->child=new;
  116. return(1);
  117. }
  118. struct NT_window *
  119. NT_find_child(NT_window *w,unsigned long mask,
  120. unsigned long val)
  121. {
  122. struct NT_window *ret = NULL;
  123. struct NT_child *child = NULL;
  124. if (w)
  125. {
  126. if ((w->mask&mask)==val) ret=w;
  127. child = w->child;
  128. while(!ret && child) {
  129. ret = NT_find_child(child->w, mask, val);
  130. child=child->next;
  131. }
  132. }
  133. return ret;
  134. }
  135. /*****************************************************************\
  136. Function: NT_del_child
  137. Inputs: parent and child window IDs.
  138. Returned: TRUE if window is removed, FALSE otherwise.
  139. Comments: Finds child window if it exits, and it so removes it from
  140. the window list.
  141. \*****************************************************************/
  142. int
  143. NT_del_child(parent,child)
  144. struct NT_window *parent;
  145. struct NT_window *child;
  146. {
  147. struct NT_child *current,*last;
  148. int status=FALSE;
  149. if (parent->child==NULL)
  150. {
  151. }
  152. else if (parent->child->w==child)
  153. {
  154. current = parent->child;
  155. parent->child=parent->child->next;
  156. freeMemory(current);
  157. status=TRUE;
  158. }
  159. else
  160. {
  161. last=parent->child;
  162. current=parent->child->next;
  163. while (current->w!=child && current!=NULL)
  164. {
  165. last=current;
  166. current=current->next;
  167. }
  168. if (current!=NULL)
  169. {
  170. last->next=current->next;
  171. freeMemory(current);
  172. status=TRUE;
  173. }
  174. }
  175. return(status);
  176. }
  177. /*****************************************************************\
  178. Function: WinMain
  179. Inputs: instance, previous instance, command line arguments,
  180. default start up.
  181. Comments: Called instead of main() as the execution entry point.
  182. \*****************************************************************/
  183. #ifdef NOTCYGWIN
  184. #define MAX_COMMAND_ARGS 20
  185. static HANDLE hInstance,hPrevInstance;
  186. int APIENTRY
  187. WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nCmdShow)
  188. {
  189. static char *command_args[MAX_COMMAND_ARGS];
  190. static int num_command_args;
  191. static char proEng[] = "proe";
  192. char *wordPtr,*tempPtr;
  193. int i,quote;
  194. hInstance=hInst;
  195. hPrevInstance=hPrevInst;
  196. for (i=0;i<MAX_COMMAND_ARGS;i++)
  197. command_args[i] = NULL;
  198. wordPtr = lpCmdLine;
  199. quote = 0;
  200. num_command_args = 1;
  201. command_args[0] = proEng;
  202. while (*wordPtr && (*wordPtr == ' ' || *wordPtr == '\t'))
  203. wordPtr++;
  204. if (*wordPtr == '\"')
  205. {
  206. quote = 1;
  207. wordPtr++;
  208. }
  209. if (!*wordPtr)
  210. main(0,NULL);
  211. else
  212. {
  213. while (*wordPtr && num_command_args < MAX_COMMAND_ARGS)
  214. {
  215. tempPtr = wordPtr;
  216. if (quote)
  217. {
  218. while (*tempPtr && *tempPtr != '\"')
  219. tempPtr++;
  220. quote = 0;
  221. }
  222. else
  223. while (*tempPtr && *tempPtr != ' ')
  224. tempPtr++;
  225. if (*tempPtr)
  226. *(tempPtr++) = '\0';
  227. command_args[num_command_args++] = wordPtr;
  228. wordPtr = tempPtr;
  229. while (*wordPtr && (*wordPtr == ' ' || *wordPtr == '\t'))
  230. wordPtr++;
  231. if (*wordPtr == '\"')
  232. {
  233. quote = 1;
  234. wordPtr++;
  235. }
  236. }
  237. main(num_command_args,command_args);
  238. }
  239. }
  240. #endif
  241. static ATOM atom=0;
  242. void
  243. NT_SetAtom(ATOM class)
  244. {
  245. atom = class;
  246. }
  247. HWND
  248. NT_create_window(char *title,DWORD style,int x,int y,int w, int h,HWND parent)
  249. {
  250. HMODULE hInst = NULL; /* GetModuleHandleA(NULL); */
  251. return CreateWindow((LPCTSTR)MAKELONG(atom,0),title,style,x,y,w,h,
  252. parent,NULL,hInst,NULL);
  253. }
  254. HBITMAP
  255. NT_getWallpaper()
  256. {
  257. HBITMAP hb = NULL;
  258. do {
  259. HKEY hKey;
  260. TCHAR wallpaper[MAX_PATH];
  261. DWORD dwBufLen = MAX_PATH;
  262. LONG lRet;
  263. lRet = RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Control Panel\\Desktop"), 0, KEY_QUERY_VALUE, &hKey);
  264. if (lRet != ERROR_SUCCESS) break;
  265. lRet = RegQueryValueEx(hKey, TEXT("Wallpaper"), NULL, NULL, (LPBYTE)wallpaper, &dwBufLen);
  266. if (lRet != ERROR_SUCCESS) break;
  267. RegCloseKey(hKey);
  268. hb = LoadImage(NULL, wallpaper, IMAGE_BITMAP, 0, 0,
  269. LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE );
  270. } while(0);
  271. return hb;
  272. }
  273. void
  274. NT_moveWindow(NT_window *ntw, BOOL repaint)
  275. {
  276. MoveWindow(ntw->w, ntw->x,ntw->y,ntw->wdth,ntw->hght,repaint);
  277. NT_configureNotify(ntw,ntw->x,ntw->y);
  278. }
  279. NT_set_origin(NT_window *ntw,int x, int y)
  280. {
  281. HDC hdc;
  282. hdc = GetDC(ntw->w);
  283. SetBrushOrgEx(hdc,-x,-y,NULL);
  284. ReleaseDC(ntw->w,hdc);
  285. }
  286. NT_confChild(NT_window *ntw,int x, int y)
  287. {
  288. HDC hdc;
  289. struct NT_child *child = NULL;
  290. if (ntw)
  291. {
  292. if (ntw->parentRelative) NT_set_origin(ntw,x,y);
  293. child = ntw->child;
  294. while(child && child->w) {
  295. NT_confChild(child->w, child->w->x+x, child->w->y+y);
  296. child=child->next;
  297. }
  298. }
  299. }
  300. NT_configureNotify(NT_window *ntw,int x, int y)
  301. {
  302. while(ntw && ntw->parent) {
  303. ntw=ntw->parent;
  304. }
  305. NT_confChild(ntw,ntw->x,ntw->y);
  306. }
  307. /*
  308. HBITMAP
  309. NT_getWallpaper()
  310. {
  311. WCHAR wszWallpaper [MAX_PATH];
  312. CHAR szWallpaper[MAX_PATH];
  313. HRESULT hr;
  314. HBITMAP hb = NULL;
  315. IActiveDesktop* pIAD = NULL;
  316. do {
  317. CoInitialize ( NULL );
  318. hr = CoCreateInstance ( CLSID_ActiveDesktop,
  319. NULL,
  320. CLSCTX_INPROC_SERVER,
  321. IID_IActiveDesktop,
  322. (void**) &pIAD );
  323. if (! SUCCEEDED(hr) ) break;
  324. hr = pIAD->GetWallpaper ( wszWallpaper, MAX_PATH, 0 );
  325. if (! SUCCEEDED(hr) ) break;
  326. wsprintf(szWallpaper,"%ls",wszWallpaper);
  327. hb = LoadImage(NULL, szWallpaper, IMAGE_BITMAP, 0, 0,
  328. LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE );
  329. } while(0);
  330. if (pIAD) pIAD->Release();
  331. CoUninitialize();
  332. return hb;
  333. }
  334. */