diff options
author | Ramana | 2018-06-06 14:48:14 +0530 |
---|---|---|
committer | GitHub | 2018-06-06 14:48:14 +0530 |
commit | 2307d7aa409430506eeaed03633c8c31dd7f4ac1 (patch) | |
tree | 411f4316499406d225c50fb64da5bf48240983ab /ldmicro/lib | |
parent | 8deab843fa6d616086955702c77751f631badc0d (diff) | |
parent | 0a7ef8991842872aa1cbc828619fc71a1216f748 (diff) | |
download | LDMicroGtk-2307d7aa409430506eeaed03633c8c31dd7f4ac1.tar.gz LDMicroGtk-2307d7aa409430506eeaed03633c8c31dd7f4ac1.tar.bz2 LDMicroGtk-2307d7aa409430506eeaed03633c8c31dd7f4ac1.zip |
Merge pull request #4 from Rr42/GUI_port
First merge
Diffstat (limited to 'ldmicro/lib')
-rw-r--r-- | ldmicro/lib/freezeLD/CMakeLists.txt | 4 | ||||
-rw-r--r-- | ldmicro/lib/freezeLD/freezeLD.cpp | 265 | ||||
-rw-r--r-- | ldmicro/lib/freezeLD/freezeLD.h | 52 | ||||
-rw-r--r-- | ldmicro/lib/linuxUI/CMakeLists.txt | 3 | ||||
-rw-r--r-- | ldmicro/lib/linuxUI/linuxLD.cpp | 114 | ||||
-rw-r--r-- | ldmicro/lib/linuxUI/linuxLD.h | 157 | ||||
-rw-r--r-- | ldmicro/lib/linuxUI/linuxUI.cpp | 175 | ||||
-rw-r--r-- | ldmicro/lib/linuxUI/linuxUI.h | 68 |
8 files changed, 838 insertions, 0 deletions
diff --git a/ldmicro/lib/freezeLD/CMakeLists.txt b/ldmicro/lib/freezeLD/CMakeLists.txt new file mode 100644 index 0000000..761f348 --- /dev/null +++ b/ldmicro/lib/freezeLD/CMakeLists.txt @@ -0,0 +1,4 @@ +project( FreezeLD ) + +add_library(FreezeLD freezeLD.cpp) +target_link_libraries(FreezeLD LinuxUI) diff --git a/ldmicro/lib/freezeLD/freezeLD.cpp b/ldmicro/lib/freezeLD/freezeLD.cpp new file mode 100644 index 0000000..0bbd33f --- /dev/null +++ b/ldmicro/lib/freezeLD/freezeLD.cpp @@ -0,0 +1,265 @@ +/* + * A library for storing parameters in a key file + * + * This library is an analog to the windows freeze library + * developed by Jonathan Westhues. + * + * R Ramana, 2018 + */ +#include "linuxUI.h" +#include "freezeLD.h" +#include <cstdlib> +#include <fstream> +#include <stdlib.h> +#include <stdio.h> + +/* + * store a window's position in the registry, or fail silently if the registry calls don't work + */ +void FreezeWindowPosF(HWID hwid, char *subKey, char *name) +{ + //g_print("freezing"); + char* moveToKeyLocatin = (char *)malloc(strlen(subKey) + 35); + if(!moveToKeyLocatin) + return; + sprintf(moveToKeyLocatin, "mkdir -p %s/%s", LDMICRO_REGISTER, subKey); + system(moveToKeyLocatin); + sprintf(moveToKeyLocatin, "cd %s/%s", LDMICRO_REGISTER, subKey); + if (-1 == system(moveToKeyLocatin)) + return; + free(moveToKeyLocatin); + + char *keyName = (char *)malloc(strlen(name) + 30); + if(!keyName) + return; + + Key newKey; + + int val; + //g_print("get width"); + sprintf(keyName, "%s_width", name); + std::ofstream Register(keyName, std::ios::binary | std::ios::trunc); + if (!Register.is_open()) + return; + gtk_window_get_size(GTK_WINDOW(hwid), &val, NULL); + newKey.type = 'i'; + newKey.val.i = val; + Register.write((char*) &newKey, sizeof(newKey)); + Register.close(); + + //g_print("get height"); + sprintf(keyName, "%s_height", name); + Register.open(keyName, std::ios::binary | std::ios::trunc); + if (!Register.is_open()) + return; + gtk_window_get_size(GTK_WINDOW(hwid), NULL, &val); + newKey.type = 'i'; + newKey.val.i = val; + Register.write((char*) &newKey, sizeof(newKey)); + Register.close(); + + //g_print("get posX"); + sprintf(keyName, "%s_posX", name); + Register.open(keyName, std::ios::binary | std::ios::trunc); + if (!Register.is_open()) + return; + gtk_window_get_position(GTK_WINDOW(hwid), &val, NULL); + newKey.type = 'i'; + newKey.val.i = val; + Register.write((char*) &newKey, sizeof(newKey)); + Register.close(); + + //g_print("get posY"); + sprintf(keyName, "%s_posY", name); + Register.open(keyName, std::ios::binary | std::ios::trunc); + if (!Register.is_open()) + return; + gtk_window_get_position(GTK_WINDOW(hwid), NULL, &val); + newKey.type = 'i'; + newKey.val.i = val; + Register.write((char*) &newKey, sizeof(newKey)); + Register.close(); + + //g_print("get max"); + sprintf(keyName, "%s_maximized", name); + Register.open(keyName, std::ios::binary | std::ios::trunc); + if (!Register.is_open()) + return; + newKey.type = 'b'; + newKey.val.b = gtk_window_is_maximized(GTK_WINDOW(hwid)); + Register.write((char*) &newKey, sizeof(newKey)); + Register.close(); + + free(keyName); + //g_print("freezed"); +} + +static void Clamp(LONG *v, LONG min, LONG max) +{ + if(*v < min) *v = min; + if(*v > max) *v = max; +} + +/* + * retrieve a window's position from the registry, or do nothing if there is no info saved + */ +void ThawWindowPosF(HWID hwid, char *subKey, char *name) +{ + char* moveToKeyLocatin = (char *)malloc(strlen(name) + 30); + if(!moveToKeyLocatin) + return; + sprintf(moveToKeyLocatin, "cd %s/%s", LDMICRO_REGISTER, subKey); + if (-1 == system(moveToKeyLocatin)) + return; + free(moveToKeyLocatin); + + char *keyName = (char *)malloc(strlen(name) + 30); + if(!keyName) + return; + + Key newKey1, newKey2; + + /// set size + sprintf(keyName, "%s_width", name); + std::ifstream Register(keyName, std::ios::binary); + if (!Register.is_open()) + return; + Register.read((char*) &newKey1, sizeof(newKey1)); + Register.close(); + + sprintf(keyName, "%s_height", name); + Register.open(keyName, std::ios::binary); + if (!Register.is_open()) + return; + Register.read((char*) &newKey2, sizeof(newKey2)); + Register.close(); + if (newKey1.type == 'i' && newKey2.type == 'i') + gtk_window_resize(GTK_WINDOW(hwid), newKey1.val.i, newKey2.val.i); + + + /// set position + sprintf(keyName, "%s_posX", name); + Register.open(keyName, std::ios::binary); + if (!Register.is_open()) + return; + Register.read((char*) &newKey1, sizeof(newKey1)); + Register.close(); + + sprintf(keyName, "%s_posY", name); + Register.open(keyName, std::ios::binary); + if (!Register.is_open()) + return; + Register.read((char*) &newKey2, sizeof(newKey2)); + Register.close(); + if (newKey1.type == 'i' && newKey2.type == 'i') + gtk_window_move(GTK_WINDOW(hwid), newKey1.val.i, newKey2.val.i); + + + sprintf(keyName, "%s_maximized", name); + Register.open(keyName, std::ios::binary); + if (!Register.is_open()) + return; + Register.read((char*) &newKey1, sizeof(newKey1)); + Register.close(); + if (newKey1.type == 'b') + if (newKey1.val.b) + gtk_window_maximize(GTK_WINDOW(hwid)); + + + /// gtk_window_move handles off-screen window placement + + free(keyName); +} + +/* + * store a DWORD setting in the registry + */ +void FreezeDWORDF(DWORD val, char *subKey, char *name) +{ + char* moveToKeyLocatin = (char *)malloc(strlen(name) + 30); + if(!moveToKeyLocatin) + return; + sprintf(moveToKeyLocatin, "mkdir -p %s/%s", LDMICRO_REGISTER, subKey); + system(moveToKeyLocatin); + sprintf(moveToKeyLocatin, "cd %s/%s", LDMICRO_REGISTER, subKey); + if (-1 == system(moveToKeyLocatin)) + return; + free(moveToKeyLocatin); + + Key newKey; + newKey.type = 'D'; + newKey.val.D = val; + std::ofstream Register(name, std::ios::binary | std::ios::trunc); + Register.write((char*) &newKey, sizeof(newKey)); + Register.close(); +} + +/* + * retrieve a DWORD setting, or return the default if that setting is unavailable + */ +DWORD ThawDWORDF(DWORD val, char *subKey, char *name) +{ + char* moveToKeyLocatin = (char *)malloc(strlen(name) + 30); + if(!moveToKeyLocatin) + return val; + sprintf(moveToKeyLocatin, "cd %s/%s", LDMICRO_REGISTER, subKey); + if (-1 == system(moveToKeyLocatin)) + return val; + free(moveToKeyLocatin); + + Key newKey; + + std::ifstream Register(name, std::ios::binary); + Register.read((char*) &newKey, sizeof(newKey)); + Register.close(); + if(Register.bad()) + return val; + + if(newKey.type == 'D') + return newKey.val.D; + else + return val; +} + +/* + * store a string setting in the registry + */ +void FreezeStringF(char *val, char *subKey, char *name) +{ + char* moveToKeyLocatin = (char *)malloc(strlen(name) + 30); + if(!moveToKeyLocatin) + return; + sprintf(moveToKeyLocatin, "mkdir -p %s/%s", LDMICRO_REGISTER, subKey); + system(moveToKeyLocatin); + sprintf(moveToKeyLocatin, "cd %s/%s", LDMICRO_REGISTER, subKey); + if (-1 == system(moveToKeyLocatin)) + return; + free(moveToKeyLocatin); + + std::ofstream Register(name, std::ios::trunc); + Register << strlen(val)+1 << "\n"; + Register << val; + Register.close(); +} + +/* + * retrieve a string setting, or return the default if that setting is unavailable + */ +void ThawStringF(char *val, int max, char *subKey, char *name) +{ + char* moveToKeyLocatin = (char *)malloc(strlen(name) + 30); + if(!moveToKeyLocatin) + return; + sprintf(moveToKeyLocatin, "cd %s/%s", LDMICRO_REGISTER, subKey); + if (-1 == system(moveToKeyLocatin)) + return; + free(moveToKeyLocatin); + + std::ifstream Register(name); + int l; + Register >> l; + if (l >= max) + return; + Register >> val; +} + diff --git a/ldmicro/lib/freezeLD/freezeLD.h b/ldmicro/lib/freezeLD/freezeLD.h new file mode 100644 index 0000000..f14ef07 --- /dev/null +++ b/ldmicro/lib/freezeLD/freezeLD.h @@ -0,0 +1,52 @@ +/* + * A library for storing parameters in a key file + * + * This library is an analog to the windows freeze library + * developed by Jonathan Westhues. + * + * R Ramana, 2018 + */ + +#ifndef __FREEZE_H +#define __FREEZE_H + +#define LDMICRO_REGISTER "/usr/share/ldmicro" + +#define FREEZE_SUBKEY "LDMicro" + +// #ifndef FREEZE_SUBKEY +// #error must define FREEZE_SUBKEY to a string uniquely identifying the app +// #endif + +#define FreezeWindowPos(hwnd) FreezeWindowPosF(hwnd, FREEZE_SUBKEY, #hwnd) +void FreezeWindowPosF(HWID hWid, char *subKey, char *name); + +#define ThawWindowPos(hwnd) ThawWindowPosF(hwnd, FREEZE_SUBKEY, #hwnd) +void ThawWindowPosF(HWID hWid, char *subKey, char *name); + +#define FreezeDWORD(val) FreezeDWORDF(val, FREEZE_SUBKEY, #val) +void FreezeDWORDF(DWORD val, char *subKey, char *name); + +#define ThawDWORD(val) val = ThawDWORDF(val, FREEZE_SUBKEY, #val) +DWORD ThawDWORDF(DWORD val, char *subKey, char *name); + +#define FreezeString(val) FreezeStringF(val, FREEZE_SUBKEY, #val) +void FreezeStringF(char *val, char *subKey, char *name); + +#define ThawString(val, max) ThawStringF(val, max, FREEZE_SUBKEY, #val) +void ThawStringF(char *val, int max, char *subKey, char *name); + +typedef union regKeyVal{ + int i; + float f; + bool b; + DWORD D; +} KeyVal; + + +typedef struct regKeys{ + char type; + KeyVal val; +} Key; + +#endif diff --git a/ldmicro/lib/linuxUI/CMakeLists.txt b/ldmicro/lib/linuxUI/CMakeLists.txt new file mode 100644 index 0000000..25ba6b2 --- /dev/null +++ b/ldmicro/lib/linuxUI/CMakeLists.txt @@ -0,0 +1,3 @@ +project(LinuxUI) + +add_library(LinuxUI linuxUI.cpp linuxLD.cpp) diff --git a/ldmicro/lib/linuxUI/linuxLD.cpp b/ldmicro/lib/linuxUI/linuxLD.cpp new file mode 100644 index 0000000..e4fd018 --- /dev/null +++ b/ldmicro/lib/linuxUI/linuxLD.cpp @@ -0,0 +1,114 @@ +#include "linuxUI.h" + +std::vector<HEAPRECORD> HeapRecords; + +HANDLE HeapCreate(DWORD flOptions, SIZE_T dwInitialSize, SIZE_T dwMaximumSize) +{ + HANDLE hHeap = NULL; + HEAPRECORD hHeapRecord; + hHeapRecord.dwMaximumSize = dwMaximumSize; + hHeap = malloc(dwInitialSize); + + if (hHeap == NULL) + return NULL; + + hHeapRecord.dwSize = dwInitialSize; + hHeapRecord.hHeap = hHeap; + hHeapRecord.dwAllocatedSizeOffset = 0; + hHeapRecord.HeapID = HeapRecords.size()+1; + HeapRecords.push_back(hHeapRecord); + + return hHeap; +} + +LPVOID HeapAlloc(HANDLE hHeap, DWORD dwFlags, SIZE_T dwBytes) +{ + if (hHeap == NULL) + { + printf("Alloc**********NULL HEAP***************\n"); + LPVOID p = malloc(dwBytes); + return p; + } + + auto it = std::find_if(HeapRecords.begin(), HeapRecords.end(), [&hHeap](HEAPRECORD &Record) { return Record.hHeap == hHeap; }); + + if (it == HeapRecords.end()) + return NULL; + + if ((*it).dwAllocatedSizeOffset + dwBytes > (*it).dwSize) + { + if ((*it).dwMaximumSize != 0) + if((*it).dwAllocatedSizeOffset + dwBytes > (*it).dwMaximumSize) + return NULL; + + (*it).hHeap = realloc((*it).hHeap, (*it).dwAllocatedSizeOffset + dwBytes); + hHeap = (*it).hHeap; + } + + /// HEAP_ZERO_MEMORY is set by default + DWORD flags = MAP_ANONYMOUS; + + // if ( !((dwFlags & HEAP_ZERO_MEMORY) == HEAP_ZERO_MEMORY) ) + // flags = MAP_ANONYMOUS | MAP_UNINITIALIZED; + + /* Use for setting a meamory chunck with some value + * void * memset ( void * ptr, int value, size_t num ); + */ + LPVOID p = mmap(hHeap + (*it).dwAllocatedSizeOffset, dwBytes, PROT_EXEC, flags, -1, 0); + + if (p == NULL) + return NULL; + + (*it).dwAllocatedSizeOffset += dwBytes; + HEAPCHUNCK chunck; + chunck.Chunck = p; + chunck.dwSize = dwBytes; + (*it).Element.push_back(chunck); + + return p; +} + +BOOL HeapFree(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem) +{ + /// if NULL free() + if (hHeap == NULL) + { + printf("free*********NULL HEAP***************\n"); + free(lpMem); + return TRUE; + } + auto heap_it = std::find_if(HeapRecords.begin(), HeapRecords.end(), [&hHeap](HEAPRECORD &Record) { return Record.hHeap == hHeap; }); + + if (heap_it == HeapRecords.end()) + return FALSE; + + auto chunck_it = std::find_if((*heap_it).Element.begin(), (*heap_it).Element.end(), [&lpMem](HEAPCHUNCK &Chunck) { return Chunck.Chunck == lpMem; }); + + if (chunck_it == (*heap_it).Element.end()) + return FALSE; + + int result = munmap((*chunck_it).Chunck, (*chunck_it).dwSize); + + if (result == 0) + { + (*heap_it).Element.erase(chunck_it); + return TRUE; + } + else + return FALSE; + +} + +void OutputDebugString(char* str) +{ + +} + +double GetTickCount(void) +{ +// timespec now; +// clock_gettime() +// if (clock_gettime(CLOCK_MONOTONIC, &now)) +// return 0; + return 10.2;//now.tv_sec * 1000.0 + now.tv_nsec / 1000000.0; +}
\ No newline at end of file diff --git a/ldmicro/lib/linuxUI/linuxLD.h b/ldmicro/lib/linuxUI/linuxLD.h new file mode 100644 index 0000000..e204c63 --- /dev/null +++ b/ldmicro/lib/linuxUI/linuxLD.h @@ -0,0 +1,157 @@ +#ifndef __LINUX_LD__ +#define __LINUX_LD__ + +#include "linuxUI.h" +#include <ctype.h> +#include <vector> +#include <algorithm> +#include <sys/mman.h> + +/// common windows referances for linux + +/// definitions +#define MAX_PATH PATH_MAX +/// CALLBACK or __stdcall os defined empty +#define CALLBACK +#define CONST const + +#define HEAP_ZERO_MEMORY 0x00000008 + +/// Typedefs +//typedef int64_t __int64; +typedef bool BOOL; +typedef unsigned char BYTE; +typedef unsigned short WORD; +typedef unsigned int DWORD; +typedef unsigned int UINT; +typedef size_t SIZE_T; +typedef long LONG; +typedef wchar_t WCHAR; +typedef char CHAR; + +typedef CONST WCHAR *LPCWSTR; +typedef CONST CHAR *LPCSTR; /// should be __nullterminated + +#ifdef UNICODE + typedef LPCWSTR LPCTSTR; +#else + typedef LPCSTR LPCTSTR; +#endif + +typedef WCHAR *LPWSTR; +typedef CHAR *LPSTR; + +#ifdef UNICODE + typedef LPWSTR LPTSTR; +#else + typedef LPSTR LPTSTR; +#endif + +typedef void *PVOID; +typedef void *LPVOID; +typedef PVOID HFONT; +typedef PVOID HMODULE; +typedef PVOID HHOOK; +typedef PVOID HBRUSH; +typedef PVOID HFONT; +typedef PVOID HANDLE; +typedef HANDLE HINSTANCE; +typedef HANDLE HDC; + +typedef GtkWidget *HWID; +typedef GtkWidget *HMENU; +typedef GtkWindow *HWND; +typedef GtkApplication *HAPP; + +/// Check if system is x64 or x86 +#if defined(__UNIX64) +typedef uint64_t UINT_PTR; +#else +typedef unsigned int UINT_PTR; +#endif + +typedef UINT_PTR WPARAM; + +#if defined(__UNIX64) + typedef __int64_t LONG_PTR; +#else + typedef long LONG_PTR; +#endif + +typedef LONG_PTR LPARAM; +typedef LONG_PTR LRESULT; + +/// Custom classes +class COLORREF : public GdkRGBA{ + public: + COLORREF() + { + this->red = 0.0; + this->green = 0.0; + this->blue = 0.0; + this->alpha = 1.0; + } + COLORREF(int r, int g, int b) + { + this->red = r/255.0; + this->green = g/255.0; + this->blue = b/255.0; + this->alpha = 1.0; + } +}; + +/// Custom structures +typedef struct HeapRecordChunckTag{ + PVOID Chunck; + SIZE_T dwSize; +} HEAPCHUNCK; + +typedef struct HeapRecordTag{ + PVOID hHeap; + DWORD HeapID; + std::vector<HEAPCHUNCK> Element; + SIZE_T dwMaximumSize; + SIZE_T dwSize; + SIZE_T dwAllocatedSizeOffset; +} HEAPRECORD; + +typedef struct tagSCROLLINFO { + UINT cbSize; + UINT fMask; + int nMin; + int nMax; + UINT nPage; + int nPos; + int nTrackPos; +} SCROLLINFO, *LPCSCROLLINFO; + +typedef struct tagNMHDR { + HWND hwndFrom; + UINT_PTR idFrom; + UINT code; +} NMHDR; + +/// Variables +extern std::vector<HEAPRECORD> HeapRecord; + +/// Functions +HANDLE HeapCreate( + DWORD flOptions, + SIZE_T dwInitialSize, + SIZE_T dwMaximumSize); + +LPVOID HeapAlloc( + HANDLE hHeap, + DWORD dwFlags, + SIZE_T dwBytes); + +BOOL HeapFree( + HANDLE hHeap, + DWORD dwFlags, + LPVOID lpMem); + +/// functions to be ported +void OutputDebugString(char*); +double GetTickCount(void); + +#endif
\ No newline at end of file diff --git a/ldmicro/lib/linuxUI/linuxUI.cpp b/ldmicro/lib/linuxUI/linuxUI.cpp new file mode 100644 index 0000000..3bebf6d --- /dev/null +++ b/ldmicro/lib/linuxUI/linuxUI.cpp @@ -0,0 +1,175 @@ +#include "linuxUI.h" + +/// EnableMenuItem Variables +const UINT MF_ENABLED = 0; +const UINT MF_GRAYED = 1; +const UINT MF_CHECKED = 2; +const UINT MF_UNCHECKED = 3; + +/// ListStore +GtkWidget* view; +GtkTreeViewColumn* column; + +/// Wraper function for gtk_window_has_toplevel_focus +BOOL isFocus(HWID window) +{ + return (BOOL) gtk_window_has_toplevel_focus(GTK_WINDOW(window)); +} + +COLORREF RGB(int red, int green, int blue) +{ + COLORREF col; + col.red = red/255.0; + col.green = green/255.0; + col.blue = blue/255.0; + col.alpha = 1.0; + + return col; +} + +int MessageBox(HWID pWindow, char* message, char* title, UINT mFlags) + { + GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT; + GtkMessageType mType; + + if ((mFlags & MB_ICONERROR) == MB_ICONERROR) + mType = GTK_MESSAGE_ERROR; + else if ((mFlags & MB_ICONQUESTION) == MB_ICONQUESTION) + mType = GTK_MESSAGE_QUESTION; + else if ((mFlags & MB_ICONWARNING) == MB_ICONWARNING) + mType = GTK_MESSAGE_WARNING; + else if ((mFlags & MB_ICONINFORMATION) == MB_ICONINFORMATION) + mType = GTK_MESSAGE_INFO; + else + mType = GTK_MESSAGE_OTHER; + + mType = GTK_MESSAGE_ERROR; + HWID dialog = gtk_message_dialog_new (GTK_WINDOW(pWindow), + flags, + mType, + GTK_BUTTONS_NONE, + message); + + if ((mFlags & MB_OKCANCEL) == MB_OKCANCEL) + { + gtk_dialog_add_button(GTK_DIALOG(dialog), "_OK", IDOK); + gtk_dialog_add_button(GTK_DIALOG(dialog), "_CANCEL", IDCANCEL); + } + else if ((mFlags & MB_YESNO) == MB_YESNO) + { + gtk_dialog_add_button(GTK_DIALOG(dialog), "_YES", IDYES); + gtk_dialog_add_button(GTK_DIALOG(dialog), "_NO", IDNO); + } + else if ((mFlags & MB_YESNOCANCEL) == MB_YESNOCANCEL) + { + gtk_dialog_add_button(GTK_DIALOG(dialog), "_YES", IDYES); + gtk_dialog_add_button(GTK_DIALOG(dialog), "_NO", IDNO); + gtk_dialog_add_button(GTK_DIALOG(dialog), "_CANCEL", IDCANCEL); + } + else + gtk_dialog_add_button(GTK_DIALOG(dialog), "OK", IDOK); + + gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG(dialog), + title); + gtk_message_dialog_format_secondary_markup(GTK_MESSAGE_DIALOG(dialog), message); + int result = gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + + return result; + } + +BOOL GetSaveFileName(OPENFILENAME *ofn) +{ + GtkWidget *dialog; + GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN; + + dialog = gtk_file_chooser_dialog_new (ofn->lpstrTitle, + GTK_WINDOW(ofn->parentWindow), + action, + "_Cancel", + GTK_RESPONSE_CANCEL, + "_Open", + GTK_RESPONSE_ACCEPT, + NULL); + //g_print("filter created\n"); + + if (ofn->Flags & OFN_OVERWRITEPROMPT == OFN_OVERWRITEPROMPT) + gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE); + + GtkFileFilter *filter = gtk_file_filter_new (); + char* strFilter = new char[strlen(ofn->lpstrFilter)]; + DWORD strFilterLen = 0; + BOOL filterResetFlag = FALSE; + + for (int i = 0; !(ofn->lpstrFilter[i] == '\0' && ofn->lpstrFilter[i-1] == '\0'); ++i) + { + memcpy (strFilter + strFilterLen, &ofn->lpstrFilter[i], 1 ); + ++strFilterLen; + if (ofn->lpstrFilter[i] == '\0') + if (filterResetFlag) + { + gtk_file_filter_add_pattern (GTK_FILE_FILTER(filter), strFilter); + gtk_file_chooser_add_filter (GTK_FILE_CHOOSER(dialog), filter); + filter = gtk_file_filter_new (); + strFilterLen = 0; + //g_print("filter pat: %s\n", strFilter); + //g_print("filter reset\n"); + filterResetFlag = FALSE; + } + else + { + gtk_file_filter_set_name (GTK_FILE_FILTER(filter), strFilter); + //g_print("filter name: %s\n", strFilter); + strFilterLen = 0; + filterResetFlag = TRUE; + } + } + //g_print("filter rules added\n"); + + sprintf(strFilter, "*.%s", ofn->lpstrDefExt); + gtk_file_filter_add_pattern (GTK_FILE_FILTER(filter), strFilter); + //gtk_file_filter_set_name (filter, "int files"); + gtk_file_chooser_set_filter (GTK_FILE_CHOOSER(dialog), filter); + + delete strFilter; + + //g_print("default filter set\n"); + + BOOL exitStatus = gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT; + if (exitStatus) + { + char* str; + str = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(dialog)); + strcpy(ofn->lpstrFile, str); + g_free(str); + } + //g_print("file path saved: %s\n", ofn->lpstrFile); + + gtk_widget_destroy (dialog); + + //g_print("exit\n"); + + return exitStatus; +} + +void EnableMenuItem(HMENU MenuName, HMENU MenuItem, UINT CheckEnabledItem) { + switch (CheckEnabledItem){ + case MF_ENABLED : + gtk_widget_set_sensitive (MenuItem, true); + break; + case MF_GRAYED : + gtk_widget_set_sensitive (MenuItem, false); + break; + } +} + +void CheckMenuItem(HMENU MenuName, HMENU MenuItem, UINT Check){ + switch (Check){ + case MF_CHECKED : + gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(MenuItem), true); + break; + case MF_UNCHECKED : + gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(MenuItem), false); + break; + } +}
\ No newline at end of file diff --git a/ldmicro/lib/linuxUI/linuxUI.h b/ldmicro/lib/linuxUI/linuxUI.h new file mode 100644 index 0000000..c6bb1ac --- /dev/null +++ b/ldmicro/lib/linuxUI/linuxUI.h @@ -0,0 +1,68 @@ +#ifndef __LINUX_UI__ +#define __LINUX_UI__ + +/// includes +#include <gtk/gtk.h> +#include <linux/limits.h> +#include <stdio.h> +#include <inttypes.h> +#include "linuxLD.h" +//#include "windows.h" + +/// version control +#define LDMicro_VERSION_MAJOR 1 +#define LDMicro_VERSION_MINOR 0 + +/// Flags +/// message box +#define MB_OK 0x00000001L +#define MB_OKCANCEL 0x00000002L +#define MB_YESNO 0x00000004L +#define MB_YESNOCANCEL 0x00000008L + +#define IDOK 1 +#define IDCANCEL 2 +#define IDYES 3 +#define IDNO 4 + +#define MB_ICONERROR 0x00000010L +#define MB_ICONQUESTION 0x00000020L +#define MB_ICONWARNING 0x00000040L +#define MB_ICONINFORMATION 0x00000080L + +/// open/save file +#define OFN_PATHMUSTEXIST 0x00000100L +#define OFN_HIDEREADONLY 0x00000200L +#define OFN_OVERWRITEPROMPT 0x00000400L + +/// EnableMenuItem variables +extern const UINT MF_ENABLED; +extern const UINT MF_GRAYED; +extern const UINT MF_CHECKED; +extern const UINT MF_UNCHECKED; + +/// ListStore +extern GtkWidget* view; +extern GtkTreeViewColumn* column; + +/// data types +typedef struct OpenFileInfoData { + DWORD lStructSize; + HWID parentWindow; + LPTSTR lpstrFile; + LPCTSTR lpstrFilter; + DWORD nMaxFile; + LPCTSTR lpstrTitle; + DWORD Flags; + LPCTSTR lpstrDefExt; +} OPENFILENAME; + +/// functions +BOOL isFocus(HWID); +COLORREF RGB(int, int, int); +int MessageBox(HWID, char*, char*, UINT); +BOOL GetSaveFileName(OPENFILENAME* ); +void EnableMenuItem(HMENU, HMENU, UINT); +void CheckMenuItem(HMENU, HMENU, UINT); + +#endif
\ No newline at end of file |