NOEXPORT void save_log() { TCHAR file_name[MAX_PATH]; OPENFILENAME ofn; LPTSTR txt; LPSTR str; ZeroMemory(&ofn, sizeof ofn); file_name[0]='\0'; ofn.lStructSize=sizeof ofn; ofn.hwndOwner=hwnd; ofn.lpstrFilter=TEXT("Log Files (*.log)\0*.log\0All Files (*.*)\0*.*\0\0"); ofn.lpstrFile=file_name; ofn.nMaxFile=MAX_PATH; ofn.lpstrDefExt=TEXT("LOG"); ofn.lpstrInitialDir=TEXT("."); ofn.lpstrTitle=TEXT("Save Log"); ofn.Flags=OFN_EXPLORER|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY| OFN_OVERWRITEPROMPT; if(!GetSaveFileName(&ofn)) return; txt=log_txt(); /* need to convert the result to UTF-8 */ str=tstr2str(txt); str_free(txt); save_text_file(file_name, str); str_free(str); }
void win_log(char *line) { /* Also used in log.c */ struct LIST *curr; int len; static int log_len=0; char *txt; len=strlen(line); curr=malloc(sizeof(struct LIST)+len); curr->len=len; strcpy(curr->txt, line); curr->next=NULL; enter_critical_section(CRIT_WIN_LOG); if(tail) tail->next=curr; tail=curr; if(!head) head=tail; log_len++; while(log_len>LOG_LINES) { curr=head; head=head->next; free(curr); log_len--; } leave_critical_section(CRIT_WIN_LOG); if(visible) { txt=log_txt(); SetWindowText(EditControl, txt); free(txt); } }
NOEXPORT void update_logs(void) { LPTSTR txt; if(!InterlockedExchange(&new_logs, 0)) return; txt=log_txt(); SetWindowText(edit_handle, txt); str_free(txt); SendMessage(edit_handle, WM_VSCROLL, (WPARAM)SB_BOTTOM, (LPARAM)0); }
static void set_visible(int i) { char *txt; visible=i; /* setup global variable */ CheckMenuItem(hpopup, GetMenuItemID(hpopup, 1), visible?MF_CHECKED:MF_UNCHECKED); /* check or uncheck menu item */ if(visible) { txt=log_txt(); SetWindowText(EditControl, txt); /* setup window content */ free(txt); ShowWindow(hwnd, SW_SHOWNORMAL); /* show window */ SetForegroundWindow(hwnd); /* bring on top */ } else ShowWindow(hwnd, SW_HIDE); /* hide window */ }
static void save_file(HWND hwnd) { TCHAR szFileName[MAX_PATH]; OPENFILENAME ofn; HANDLE hFile; BOOL bResult; LPTSTR txt; LPSTR str; DWORD nWritten; ZeroMemory(&ofn, sizeof(ofn)); szFileName[0]='\0'; ofn.lStructSize=sizeof(ofn); ofn.hwndOwner=hwnd; ofn.lpstrFilter=TEXT("Log Files (*.log)\0*.log\0All Files (*.*)\0*.*\0\0"); ofn.lpstrFile=szFileName; ofn.nMaxFile=MAX_PATH; ofn.lpstrDefExt=TEXT("LOG"); ofn.lpstrInitialDir=TEXT("."); ofn.lpstrTitle=TEXT("Save Log"); ofn.Flags=OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT; if(!GetSaveFileName(&ofn)) return; if((hFile=CreateFile(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL))==INVALID_HANDLE_VALUE) { error_box(TEXT("CreateFile")); return; } txt=log_txt(); str=tstr2str(txt); free(txt); bResult=WriteFile(hFile, str, strlen(str), &nWritten, NULL); free(str); if(!bResult) error_box(TEXT("WriteFile")); CloseHandle(hFile); }
static void save_file(HWND hwnd) { char szFileName[MAX_PATH]; OPENFILENAME ofn; HANDLE hFile; BOOL bResult; char *txt; DWORD nToWrite, nWritten; ZeroMemory(&ofn, sizeof(ofn)); szFileName[0]='\0'; ofn.lStructSize=sizeof(ofn); ofn.hwndOwner=hwnd; ofn.lpstrFilter="Log Files (*.log)\0*.log\0All Files (*.*)\0*.*\0\0"; ofn.lpstrFile=szFileName; ofn.nMaxFile=MAX_PATH; ofn.lpstrDefExt="LOG"; ofn.lpstrInitialDir="."; ofn.lpstrTitle="Save Log"; ofn.Flags=OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT; if(!GetSaveFileName(&ofn)) return; if((hFile=CreateFile((LPCSTR)szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL))==INVALID_HANDLE_VALUE) { MessageBox(hwnd, "File open failed", options.win32_name, MB_ICONERROR); return; } txt=log_txt(); nToWrite=strlen(txt); bResult=WriteFile(hFile, txt, nToWrite, &nWritten, NULL); free(txt); if(!bResult) MessageBox(hwnd, "File write failed", options.win32_name, MB_ICONERROR); CloseHandle(hFile); }
static LRESULT CALLBACK wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { POINT pt; NOTIFYICONDATA nid; RECT rect; char *txt; #if 0 if(message!=WM_CTLCOLORSTATIC && message!=WM_TIMER) log(LOG_DEBUG, "Window message: %d", message); #endif switch (message) { case WM_CREATE: if (options.option.taskbar) /* taskbar update enabled? */ SetTimer(hwnd, 0x29a, 1000, NULL); /* 1-second timer */ return TRUE; case WM_SIZE: GetClientRect(hwnd, &rect); MoveWindow(EditControl, 0, 0, rect.right, rect.bottom, TRUE); UpdateWindow(EditControl); return TRUE; case WM_SETFOCUS: txt=log_txt(); SetWindowText(EditControl, txt); free(txt); SetFocus(EditControl); return TRUE; case WM_TIMER: update_taskbar(); return TRUE; case WM_CLOSE: set_visible(0); return TRUE; case WM_DESTROY: DestroyMenu(hmainmenu); DestroyMenu(htraymenu); ZeroMemory(&nid, sizeof(nid)); nid.cbSize=sizeof(NOTIFYICONDATA); nid.hWnd=hwnd; nid.uID=1; nid.uFlags=NIF_TIP; /* not really sure what to put here, but it works */ Shell_NotifyIcon(NIM_DELETE, &nid); /* this removes the icon */ PostQuitMessage(0); KillTimer(hwnd, 0x29a); return TRUE; case WM_COMMAND: switch(wParam) { case IDM_ABOUT: DialogBox(ghInst, "AboutBox", hwnd, (DLGPROC)about_proc); break; case IDM_LOG: set_visible(!visible); break; case IDM_CLOSE: set_visible(0); break; case IDM_EXIT: DestroyWindow(hwnd); break; case IDM_SAVEAS: save_file(hwnd); break; case IDM_SETUP: MessageBox(hwnd, "Function not implemented", options.win32_name, MB_ICONERROR); break; }; return TRUE; case UWM_SYSTRAY: switch (lParam) { case WM_RBUTTONUP: /* track a popup menu */ /* http://support.microsoft.com/support/kb/articles/Q135/7/88.asp */ GetCursorPos(&pt); SetForegroundWindow(hwnd); TrackPopupMenu(hpopup, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hwnd, NULL); PostMessage(hwnd, WM_NULL, 0, 0); /* see above */ break; case WM_LBUTTONDBLCLK: /* switch log window visibility */ set_visible(!visible); break; } return TRUE; } return DefWindowProc(hwnd, message, wParam, lParam); }
static LRESULT CALLBACK wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { NOTIFYICONDATA nid; POINT pt; RECT rect; LPTSTR txt; #if 0 if(message!=WM_CTLCOLORSTATIC && message!=WM_TIMER) s_log(LOG_DEBUG, "Window message: %d", message); #endif switch (message) { case WM_CREATE: if (options.option.taskbar) /* taskbar update enabled? */ SetTimer(hwnd, 0x29a, 1000, NULL); /* 1-second timer */ #ifdef _WIN32_WCE /* create command bar */ hwndCB=CommandBar_Create(ghInst, hwnd, 1); if(!hwndCB) error_box(TEXT("CommandBar_Create")); if(!CommandBar_InsertMenubar(hwndCB, ghInst, IDM_MAINMENU, 0)) error_box(TEXT("CommandBar_InsertMenubar")); if(!CommandBar_AddAdornments(hwndCB, 0, 0)) error_box(TEXT("CommandBar_AddAdornments")); #endif /* create child edit window */ EditControl=CreateWindow(TEXT("EDIT"), NULL, WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL|ES_MULTILINE|ES_READONLY, 0, 0, 0, 0, hwnd, (HMENU)IDE_EDIT, ghInst, NULL); #ifndef _WIN32_WCE SendMessage(EditControl, WM_SETFONT, (WPARAM)GetStockObject(OEM_FIXED_FONT), MAKELPARAM(FALSE, 0)); /* no need to redraw right, now */ #endif /* NOTE: there's no return statement here -> proceeding with resize */ case WM_SIZE: GetClientRect(hwnd, &rect); #ifdef _WIN32_WCE MoveWindow(EditControl, 0, CommandBar_Height(hwndCB), rect.right, rect.bottom-CommandBar_Height(hwndCB), TRUE); #else MoveWindow(EditControl, 0, 0, rect.right, rect.bottom, TRUE); #endif UpdateWindow(EditControl); /* CommandBar_Show(hwndCB, TRUE); */ return TRUE; case WM_SETFOCUS: txt=log_txt(); SetWindowText(EditControl, txt); free(txt); SetFocus(EditControl); return TRUE; case WM_TIMER: update_taskbar(); return TRUE; case WM_CLOSE: set_visible(0); return TRUE; case WM_DESTROY: #ifdef _WIN32_WCE CommandBar_Destroy(hwndCB); #else if(hmainmenu) DestroyMenu(hmainmenu); #endif if(htraymenu) DestroyMenu(htraymenu); ZeroMemory(&nid, sizeof(nid)); nid.cbSize=sizeof(NOTIFYICONDATA); nid.hWnd=hwnd; nid.uID=1; nid.uFlags=NIF_TIP; /* not really sure what to put here, but it works */ Shell_NotifyIcon(NIM_DELETE, &nid); /* this removes the icon */ PostQuitMessage(0); KillTimer(hwnd, 0x29a); return TRUE; case WM_COMMAND: switch(wParam) { case IDM_ABOUT: DialogBox(ghInst, TEXT("AboutBox"), hwnd, (DLGPROC)about_proc); break; case IDM_LOG: set_visible(!visible); break; case IDM_CLOSE: set_visible(0); break; case IDM_EXIT: DestroyWindow(hwnd); break; case IDM_SAVEAS: save_file(hwnd); break; case IDM_SETUP: MessageBox(hwnd, TEXT("Function not implemented"), win32_name, MB_ICONERROR); break; } return TRUE; case UWM_SYSTRAY: /* a taskbar event */ switch (lParam) { #ifdef _WIN32_WCE case WM_LBUTTONDOWN: /* no right mouse button on Windows CE */ GetWindowRect(GetDesktopWindow(), &rect); /* no cursor position */ pt.x=rect.right; pt.y=rect.bottom-25; #else case WM_RBUTTONDOWN: GetCursorPos(&pt); #endif SetForegroundWindow(hwnd); TrackPopupMenuEx(hpopup, TPM_BOTTOMALIGN, pt.x, pt.y, hwnd, NULL); PostMessage(hwnd, WM_NULL, 0, 0); break; #ifndef _WIN32_WCE case WM_LBUTTONDBLCLK: /* switch log window visibility */ set_visible(!visible); break; #endif } return TRUE; } return DefWindowProc(hwnd, message, wParam, lParam); }