diff options
author | NatsuDrag9 | 2018-06-26 10:39:51 +0530 |
---|---|---|
committer | NatsuDrag9 | 2018-06-26 10:39:51 +0530 |
commit | d3f238d01fd368ce36fe3b59df63406b27c018f7 (patch) | |
tree | dc80af072e1699caf155730efd60305af62ee2b4 | |
parent | cc789def969c9b1ddc5338a934c2c63717177351 (diff) | |
parent | 0dad08dd60d4de845db5a7f0d0948b3503c46d4a (diff) | |
download | LDMicroGtk-d3f238d01fd368ce36fe3b59df63406b27c018f7.tar.gz LDMicroGtk-d3f238d01fd368ce36fe3b59df63406b27c018f7.tar.bz2 LDMicroGtk-d3f238d01fd368ce36fe3b59df63406b27c018f7.zip |
Merged files for testing dialogs.
-rw-r--r-- | help/sample-gtk.cpp | 175 | ||||
-rw-r--r-- | ldmicro/compilecommon.cpp | 2 | ||||
-rw-r--r-- | ldmicro/draw_outputdev.cpp | 18 | ||||
-rw-r--r-- | ldmicro/helpdialog.cpp | 2 | ||||
-rw-r--r-- | ldmicro/includes/ldmicro.h | 12 | ||||
-rw-r--r-- | ldmicro/iolist.cpp | 510 | ||||
-rw-r--r-- | ldmicro/ldmicro.cpp | 1204 | ||||
-rw-r--r-- | ldmicro/lib/freezeLD/freezeLD.cpp | 8 | ||||
-rw-r--r-- | ldmicro/lib/linuxUI/linuxLD.h | 51 | ||||
-rw-r--r-- | ldmicro/lib/linuxUI/linuxUI.cpp | 146 | ||||
-rw-r--r-- | ldmicro/lib/linuxUI/linuxUI.h | 54 | ||||
-rw-r--r-- | ldmicro/maincontrols.cpp | 301 | ||||
-rw-r--r-- | ldmicro/miscutil.cpp | 50 | ||||
-rw-r--r-- | ldmicro/pic16.cpp | 23 | ||||
-rw-r--r-- | ldmicro/simulate.cpp | 1198 |
15 files changed, 1840 insertions, 1914 deletions
diff --git a/help/sample-gtk.cpp b/help/sample-gtk.cpp deleted file mode 100644 index 02238ed..0000000 --- a/help/sample-gtk.cpp +++ /dev/null @@ -1,175 +0,0 @@ -#include "GTK3Test.h" - -/// GTK Widgets -GtkWidget *window; -GtkWidget *windowGrid; -GtkWidget *radioButton1, *radioButton2; -GtkWidget *textLabel; -GtkWidget *textBox; -GtkWidget *buttonSubmit; -GtkWidget *buttonCancel; -GtkWidget *textViewWindow; -GtkWidget *scrolledwindow; - -/// GDK properties -GdkRGBA color; - -void textViewPrint(const char* format, ...) -{ - va_list arglist; - /// initialize valist for format arguments - va_start(arglist, format); - char* str; - // g_print("--in-print--\n"); - // vprintf(format, arglist); - vsprintf(str, format, arglist); - // g_print("text: "); - // g_print(str); - // g_print("\n"); - va_end(arglist); - // g_print("--text-gen-done--\n"); - - GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textViewWindow)); - // g_print("--buffer-created--\n"); - gtk_text_buffer_insert_at_cursor(GTK_TEXT_BUFFER(buffer), str, strlen(str)); - // g_print("--text-buffered--\n"); - gtk_text_view_set_buffer(GTK_TEXT_VIEW(textViewWindow), buffer); - // g_print("--text-printed--\n"); -} - - void setRGB(GdkRGBA* color, int r, int g, int b) -{ - color->red = r; - color->green = g; - color->blue = b; - color->alpha = 1; -} - -static void button1(GtkWidget *widget, gpointer data) -{ - textViewPrint ("Button1\n"); -} - -static void submitClicked(GtkWidget *widget, gpointer data) -{ - g_print("Submitted\n"); - textViewPrint("Submitted\n"); - - if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radioButton1))) - { - g_print("Radio 1 selected\n"); - textViewPrint("Radio 1 selected\n"); - } - else - { - g_print("Radio 2 selected\n"); - textViewPrint("Radio 2 selected\n"); - } - - GtkEntryBuffer *entryBuffer; - entryBuffer = gtk_entry_get_buffer(GTK_ENTRY(textBox)); - - g_print("Text in textbox: %s\n", gtk_entry_buffer_get_text(GTK_ENTRY_BUFFER(entryBuffer))); - textViewPrint("Text in textbox: %s\n", gtk_entry_buffer_get_text(GTK_ENTRY_BUFFER(entryBuffer))); - - int w,h; - gtk_window_get_size(GTK_WINDOW(window), &w, &h); - - g_print("Width: %d, Height: %d\n", w, h); - textViewPrint("Width: %i, Height: %i\n", w, h); -} - -static void activate(GtkApplication* app, gpointer user_data) -{ - /// Make window - window = gtk_application_window_new(app); - gtk_window_set_title(GTK_WINDOW(window), "Window"); - gtk_window_set_default_size(GTK_WINDOW(window), 200, 200); - - /// grid to hold multiple witgets - windowGrid = gtk_grid_new(); - /// Attach grid to window - gtk_container_add(GTK_CONTAINER(window), windowGrid); - - /// Make button - GtkWidget *button; - button = gtk_button_new_with_label ("Button1"); - g_signal_connect (button, "clicked", G_CALLBACK (button1), NULL); - - /** Place the first button in the grid cell (0, 0), and make it fill - * just 1 cell horizontally and vertically (ie no spanning) - */ - gtk_grid_attach(GTK_GRID(windowGrid), button, 0, 0, 1, 1); - - /// Make box for 2 radio buttons - // GtkWidget *radioBox; - // radioBox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2); // (orientation, number of rows) - //gtk_box_set_homogeneous (GTK_BOX(radioBox), TRUE); - - /// Make radio buttons - radioButton1 = gtk_radio_button_new_with_label(NULL, "radio1"); - radioButton2 = gtk_radio_button_new_with_label(NULL, "radio2"); - gtk_radio_button_set_group(GTK_RADIO_BUTTON(radioButton2), gtk_radio_button_get_group(GTK_RADIO_BUTTON(radioButton1))); - gtk_grid_attach(GTK_GRID(windowGrid), radioButton1, 0, 1, 1, 1); - gtk_grid_attach(GTK_GRID(windowGrid), radioButton2, 1, 1, 1, 1); - - /// Pack radio buttons in radio box - // gtk_box_pack_start(GTK_BOX (radioBox), radioButton1, FALSE, FALSE, 0); - // gtk_box_pack_start(GTK_BOX (radioBox), radioButton2, FALSE, FALSE, 0); - // gtk_box_pack_start (GTK_BOX(windowGrid), radioBox, FALSE, FALSE, 0); - - /// Make lable for text box - textLabel = gtk_label_new ("Enter something: "); - gtk_grid_attach(GTK_GRID(windowGrid), textLabel, 0, 2, 1, 1); - /// Spacing for widget - gtk_widget_set_margin_top(GTK_WIDGET(textLabel), 10); - gtk_widget_set_margin_bottom(GTK_WIDGET(textLabel), 10); - gtk_widget_set_margin_right(GTK_WIDGET(textLabel), 10); - gtk_widget_set_margin_left(GTK_WIDGET(textLabel), 10); - - /// Make text box - textBox = gtk_entry_new(); - gtk_entry_set_max_length(GTK_ENTRY(textBox), 0); - gtk_grid_attach(GTK_GRID(windowGrid), textBox, 1, 2, 1, 1); - - /// Make submit button - buttonSubmit = gtk_button_new_with_label ("Submit"); - g_signal_connect (buttonSubmit, "clicked", G_CALLBACK (submitClicked), NULL); - gtk_grid_attach(GTK_GRID(windowGrid), buttonSubmit, 0, 5, 1, 1); - setRGB(&color, 0, 1, 0); - gtk_widget_override_color(GTK_WIDGET(buttonSubmit), GTK_STATE_FLAG_NORMAL, &color); - - /// Make cancel button - buttonCancel = gtk_button_new_with_label ("Cancel"); - g_signal_connect_swapped (buttonCancel, "clicked", G_CALLBACK (gtk_widget_destroy), window); - gtk_grid_attach(GTK_GRID(windowGrid), buttonCancel, 1, 5, 1, 1); - setRGB(&color, 1, 0, 0); - gtk_widget_override_color(GTK_WIDGET(buttonCancel), GTK_STATE_FLAG_NORMAL, &color); - - /// Make text view widget - scrolledwindow = gtk_scrolled_window_new(NULL, NULL); - textViewWindow = gtk_text_view_new(); - gtk_container_add(GTK_CONTAINER(scrolledwindow), textViewWindow); - gtk_text_view_set_editable(GTK_TEXT_VIEW(textViewWindow), FALSE); - gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textViewWindow), TRUE); - gtk_text_view_set_overwrite(GTK_TEXT_VIEW(textViewWindow), FALSE); - //gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textViewWindow), GTK_WRAP_WORD); - gtk_widget_set_size_request(GTK_WIDGET(scrolledwindow), 2, 80); - gtk_grid_attach(GTK_GRID(windowGrid), scrolledwindow, 0, 6, 5, 5); - - gtk_widget_show_all (window); -} - -int main (int argc, char **argv) -{ - /// Make app - GtkApplication *app; - int status; - - app = gtk_application_new ("org.git.GTK", G_APPLICATION_FLAGS_NONE); - g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); - status = g_application_run (G_APPLICATION (app), argc, argv); - g_object_unref (app); - - return status; -} diff --git a/ldmicro/compilecommon.cpp b/ldmicro/compilecommon.cpp index a25d742..f66e7f9 100644 --- a/ldmicro/compilecommon.cpp +++ b/ldmicro/compilecommon.cpp @@ -118,8 +118,8 @@ static void MemForPin(char *name, DWORD *addr, int *bit, BOOL asInput) if(strcmp(Prog.io.assignment[i].name, name)==0) break; } - if(i >= Prog.io.count) oops(); + if(i >= Prog.io.count) oops(); if(asInput && Prog.io.assignment[i].type == IO_TYPE_DIG_OUTPUT) oops(); if(!asInput && Prog.io.assignment[i].type == IO_TYPE_DIG_INPUT) oops(); diff --git a/ldmicro/draw_outputdev.cpp b/ldmicro/draw_outputdev.cpp index 69f08ca..eab7324 100644 --- a/ldmicro/draw_outputdev.cpp +++ b/ldmicro/draw_outputdev.cpp @@ -81,9 +81,10 @@ SyntaxHighlightingColours HighlightColours; // bottom, left, right) but we don't care; just go from the coordinates // computed when we drew the schematic in the paint procedure. //----------------------------------------------------------------------------- -gboolean BlinkCursor(GtkWidget * window) //(HWND hwnd, UINT msg, UINT_PTR id, DWORD time) +BOOL BlinkCursor(BOOL kill = FALSE) { // if(GetFocus(MainWindow) != !CursorDrawn) return TRUE; + if(Cursor.left == 0) return TRUE; PlcCursor c; @@ -126,11 +127,10 @@ gboolean BlinkCursor(GtkWidget * window) //(HWND hwnd, UINT msg, UINT_PTR id, DW else PatBlt(Hcr, c.left, c.top, c.width, c.height, PATINVERT, (HBRUSH)GetStockObject(BLACK_BRUSH)); InvalidateRect(DrawWindow, NULL, FALSE); - // g_print("BLINK\n"); cairo_destroy(Hcr); CursorDrawn = !CursorDrawn; - return TRUE; + return !kill; } //----------------------------------------------------------------------------- @@ -342,13 +342,11 @@ void PaintWindow(HCRDC Hcr) // BitBlt(paintDc, 0, 0, bw, bh, BackDc, ScrollXOffset, 0, SRCCOPY); - // if(InSimulationMode) { - // KillTimer(MainWindow, TIMER_BLINK_CURSOR); - // } else { - // KillTimer(MainWindow, TIMER_BLINK_CURSOR); - // BlinkCursor(NULL, 0, NULL, 0); - // SetTimer(MainWindow, TIMER_BLINK_CURSOR, 800, BlinkCursor); - // } + if(InSimulationMode) { + KillTimer(DrawWindow, TIMER_BLINK_CURSOR); + } else { + SetTimer(DrawWindow, TIMER_BLINK_CURSOR, 200, BlinkCursor); + } ok(); } diff --git a/ldmicro/helpdialog.cpp b/ldmicro/helpdialog.cpp index fb1b6c3..71cbd1a 100644 --- a/ldmicro/helpdialog.cpp +++ b/ldmicro/helpdialog.cpp @@ -225,7 +225,7 @@ static void MakeControls(int a) } } gtk_widget_override_background_color (TextView, GTK_STATE_FLAG_NORMAL, - ((HBRUSH)GetStockObject(BLACK_BRUSH))->getThis()); + ((HBRUSH)GetStockObject(BLACK_BRUSH))); gtk_container_add (GTK_CONTAINER(RichEdit[a]), TextView); } diff --git a/ldmicro/includes/ldmicro.h b/ldmicro/includes/ldmicro.h index bd38e1a..0822179 100644 --- a/ldmicro/includes/ldmicro.h +++ b/ldmicro/includes/ldmicro.h @@ -589,7 +589,6 @@ void RefreshScrollbars(void); extern HINSTANCE Instance; extern HWID MainWindow; extern HWID DrawWindow; -// extern HCRDC Hcr; extern PlcProgram Prog; extern char CurrentSaveFile[MAX_PATH]; extern char CurrentCompileFile[MAX_PATH]; @@ -632,7 +631,7 @@ extern BOOL ThisHighlighted; // draw_outputdev.cpp extern void (*DrawChars)(HCRDC Hcr, int, int, const char *); -gboolean BlinkCursor(GtkWidget * window); +BOOL BlinkCursor(BOOL kill); void PaintWindow(HCRDC Hcr); void ExportDrawingAsText(char *file); void InitForDrawing(void); @@ -794,10 +793,17 @@ void CompileSuccessfulMessage(char *str); extern BOOL RunningInBatchMode; extern HFONT MyNiceFont; extern HFONT MyFixedFont; +<<<<<<< HEAD extern HWID OkButton; extern HWID CancelButton; extern BOOL DialogDone; extern BOOL DialogCancel; +======= +// extern HWND OkButton; +// extern HWND CancelButton; +// extern BOOL DialogDone; +// extern BOOL DialogCancel; +>>>>>>> akshay-c-GUI_port // lang.cpp char *_(char *in); @@ -805,7 +811,7 @@ char *_(char *in); // simulate.cpp void SimulateOneCycle(BOOL forceRefresh); -void CALLBACK PlcCycleTimer(HWND hwnd, UINT msg, UINT_PTR id, DWORD time); +BOOL PlcCycleTimer(BOOL kill); void StartSimulationTimer(void); void ClearSimulationData(void); void DescribeForIoList(char *name, char *out); diff --git a/ldmicro/iolist.cpp b/ldmicro/iolist.cpp index e2fc032..33ec2d9 100644 --- a/ldmicro/iolist.cpp +++ b/ldmicro/iolist.cpp @@ -43,20 +43,20 @@ static struct { static int IoSeenPreviouslyCount; // // stuff for the dialog box that lets you choose pin assignments -// static BOOL DialogDone; -// static BOOL DialogCancel; +static BOOL DialogDone; +static BOOL DialogCancel; -// static HWND IoDialog; +static HWID IoDialog; -// static HWND PinList; -// static HWND OkButton; -// static HWND CancelButton; +static HWID PinList; +static HWID OkButton; +static HWID CancelButton; // // stuff for the popup that lets you set the simulated value of an analog in -// static HWND AnalogSliderMain; -// static HWND AnalogSliderTrackbar; -// static BOOL AnalogSliderDone; -// static BOOL AnalogSliderCancel; +static HWID AnalogSliderMain; +static HWID AnalogSliderTrackbar; +static BOOL AnalogSliderDone; +static BOOL AnalogSliderCancel; //----------------------------------------------------------------------------- @@ -394,126 +394,150 @@ void SaveIoListToFile(FILE *f) // Dialog proc for the popup that lets you set the value of an analog input for // simulation. //----------------------------------------------------------------------------- -// static LRESULT CALLBACK AnalogSliderDialogProc(HWND hwnd, UINT msg, -// WPARAM wParam, LPARAM lParam) -// { -// switch (msg) { -// case WM_CLOSE: -// case WM_DESTROY: -// AnalogSliderDone = TRUE; -// AnalogSliderCancel = TRUE; -// return 1; - -// default: -// return DefWindowProc(hwnd, msg, wParam, lParam); -// } -// } - -//----------------------------------------------------------------------------- -// A little toolbar-style window that pops up to allow the user to set the -// simulated value of an ADC pin. -//----------------------------------------------------------------------------- -// void ShowAnalogSliderPopup(char *name) -// { -// WNDCLASSEX wc; -// memset(&wc, 0, sizeof(wc)); -// wc.cbSize = sizeof(wc); +static gboolean AnalogSliderDialogKeyboardProc(GtkWidget* widget, GdkEventKey* event, gpointer name) +{ + // g_print("key click!\n"); + + SWORD v = (SWORD)gtk_range_get_value(GTK_RANGE(AnalogSliderTrackbar)); + SetAdcShadow((char*)name, v); + if (AnalogSliderDone == TRUE || AnalogSliderCancel == TRUE) + { + gtk_widget_destroy (AnalogSliderTrackbar); + gtk_widget_destroy (AnalogSliderMain); + return FALSE; + } -// wc.style = CS_BYTEALIGNCLIENT | CS_BYTEALIGNWINDOW | CS_OWNDC | -// CS_DBLCLKS; -// wc.lpfnWndProc = (WNDPROC)AnalogSliderDialogProc; -// wc.hInstance = Instance; -// wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW; -// wc.lpszClassName = "LDmicroAnalogSlider"; -// wc.lpszMenuName = NULL; -// wc.hCursor = LoadCursor(NULL, IDC_ARROW); + if (event->keyval == GDK_KEY_Return){ + gtk_widget_destroy (AnalogSliderTrackbar); + gtk_widget_destroy (AnalogSliderMain); + AnalogSliderDone = TRUE; + } + else if (event->keyval == GDK_KEY_Escape){ + gtk_widget_destroy (AnalogSliderTrackbar); + gtk_widget_destroy (AnalogSliderMain); + AnalogSliderDone = TRUE; + AnalogSliderCancel = TRUE; + } -// RegisterClassEx(&wc); + return FALSE; +} -// POINT pt; -// GetCursorPos(&pt); +static gboolean AnalogSliderDialogMouseProc(GtkWidget *widget, GdkEventButton *event, gpointer name) +{ + // g_print("mouse click! %i, %i\n", event->button, event->type == GDK_BUTTON_PRESS); + SWORD v = (SWORD)gtk_range_get_value(GTK_RANGE(AnalogSliderTrackbar)); + SetAdcShadow((char*)name, v); + if (event->button == 1 && event->type == GDK_BUTTON_RELEASE){ + gtk_widget_destroy (AnalogSliderTrackbar); + gtk_widget_destroy (AnalogSliderMain); + AnalogSliderDone = TRUE; + } -// SWORD currentVal = GetAdcShadow(name); + return FALSE; +} -// SWORD maxVal; -// if(Prog.mcu) { -// maxVal = Prog.mcu->adcMax; -// } else { -// maxVal = 1023; -// } -// if(maxVal == 0) { -// Error(_("No ADC or ADC not supported for selected micro.")); -// return; -// } +void AnalogSliderUpdateProc (GtkRange *range, GtkScrollType step, gpointer name) +{ + // g_print("dlide bar adj\n"); + SWORD v = (SWORD)gtk_range_get_value(GTK_RANGE(AnalogSliderTrackbar)); + SetAdcShadow((char*)name, v); +} -// int left = pt.x - 10; -// // try to put the slider directly under the cursor (though later we might -// // realize that that would put the popup off the screen) -// int top = pt.y - (15 + (73*currentVal)/maxVal); +//----------------------------------------------------------------------------- +// A little toolbar-style window that pops up to allow the user to set the +// simulated value of an ADC pin. +//----------------------------------------------------------------------------- +void ShowAnalogSliderPopup(char *name) +{ + // POINT pt; + // GetCursorPos(&pt); -// RECT r; -// SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0); + SWORD currentVal = GetAdcShadow(name); -// if(top + 110 >= r.bottom) { -// top = r.bottom - 110; -// } -// if(top < 0) top = 0; + SWORD maxVal; + if(Prog.mcu) { + maxVal = Prog.mcu->adcMax; + } else { + maxVal = 1023; + } + if(maxVal == 0) { + Error(_("No ADC or ADC not supported for selected micro.")); + return; + } -// AnalogSliderMain = CreateWindowClient(0, "LDmicroAnalogSlider", "I/O Pin", -// WS_VISIBLE | WS_POPUP | WS_DLGFRAME, -// left, top, 30, 100, NULL, NULL, Instance, NULL); - -// AnalogSliderTrackbar = CreateWindowEx(0, TRACKBAR_CLASS, "", WS_CHILD | -// TBS_AUTOTICKS | TBS_VERT | TBS_TOOLTIPS | WS_CLIPSIBLINGS | WS_VISIBLE, -// 0, 0, 30, 100, AnalogSliderMain, NULL, Instance, NULL); -// SendMessage(AnalogSliderTrackbar, TBM_SETRANGE, FALSE, -// MAKELONG(0, maxVal)); -// SendMessage(AnalogSliderTrackbar, TBM_SETTICFREQ, (maxVal + 1)/8, 0); -// SendMessage(AnalogSliderTrackbar, TBM_SETPOS, TRUE, currentVal); - -// EnableWindow(MainWindow, FALSE); -// ShowWindow(AnalogSliderMain, TRUE); -// SetFocus(AnalogSliderTrackbar); - -// DWORD ret; -// MSG msg; -// AnalogSliderDone = FALSE; -// AnalogSliderCancel = FALSE; - -// SWORD orig = GetAdcShadow(name); - -// while(!AnalogSliderDone && (ret = GetMessage(&msg, NULL, 0, 0))) { -// SWORD v = (SWORD)SendMessage(AnalogSliderTrackbar, TBM_GETPOS, 0, 0); + // int x, y; + // gtk_window_get_position(GTK_WINDOW(MainWindow), &x, &y); + int left = GLOBAL_mouse_last_clicked_x - 10; + // try to put the slider directly under the cursor (though later we might + // realize that that would put the popup off the screen) + int top = GLOBAL_mouse_last_clicked_y - (15 + (73*currentVal)/maxVal); + + // RECT r; + // SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0); + + // if(top + 110 >= r.bottom) { + // top = r.bottom - 110; + // } + if(top < 0) top = 100; + if(left < 0) left = 100; + + if (!GTK_IS_WINDOW(AnalogSliderMain)) + { + AnalogSliderMain = gtk_window_new(GTK_WINDOW_POPUP); + gtk_window_set_title(GTK_WINDOW(AnalogSliderMain), "I/O Pin"); + gtk_window_resize (GTK_WINDOW(AnalogSliderMain), 30, 100); + gtk_window_move(GTK_WINDOW(AnalogSliderMain), left, top); + } -// if(msg.message == WM_KEYDOWN) { -// if(msg.wParam == VK_RETURN) { -// AnalogSliderDone = TRUE; -// break; -// } else if(msg.wParam == VK_ESCAPE) { -// AnalogSliderDone = TRUE; -// AnalogSliderCancel = TRUE; -// break; -// } -// } else if(msg.message == WM_LBUTTONUP) { -// if(v != orig) { -// AnalogSliderDone = TRUE; -// } -// } -// SetAdcShadow(name, v); + if (!GTK_IS_SCALE(AnalogSliderTrackbar)) + { + AnalogSliderTrackbar = gtk_scale_new_with_range (GTK_ORIENTATION_VERTICAL, + 0, + maxVal, + 1); + } + + gtk_range_set_value (GTK_RANGE(AnalogSliderTrackbar), currentVal); + // gtk_scale_add_mark (GTK_SCALE(AnalogSliderTrackbar), (maxVal + 1)/8, GTK_POS_LEFT, NULL); + + // AnalogSliderMain = CreateWindowClient(0, "LDmicroAnalogSlider", "I/O Pin", + // WS_VISIBLE | WS_POPUP | WS_DLGFRAME, + // left, top, 30, 100, NULL, NULL, Instance, NULL); + + // AnalogSliderTrackbar = CreateWindowEx(0, TRACKBAR_CLASS, "", WS_CHILD | + // TBS_AUTOTICKS | TBS_VERT | TBS_TOOLTIPS | WS_CLIPSIBLINGS | WS_VISIBLE, + // 0, 0, 30, 100, AnalogSliderMain, NULL, Instance, NULL); + // SendMessage(AnalogSliderTrackbar, TBM_SETRANGE, FALSE, + // MAKELONG(0, maxVal)); + // SendMessage(AnalogSliderTrackbar, TBM_SETTICFREQ, (maxVal + 1)/8, 0); + // SendMessage(AnalogSliderTrackbar, TBM_SETPOS, TRUE, currentVal); + + // EnableWindow(MainWindow, FALSE); + // ShowWindow(AnalogSliderMain, TRUE); + + gtk_container_add(GTK_CONTAINER(AnalogSliderMain), AnalogSliderTrackbar); + + // SetFocus(AnalogSliderTrackbar); + gtk_window_set_focus_visible (GTK_WINDOW(AnalogSliderMain), TRUE); + gtk_window_set_keep_above (GTK_WINDOW(AnalogSliderMain), TRUE); + // gtk_window_set_focus (, AnalogSliderTrackbar); + + g_signal_connect (AnalogSliderMain, "key-press-event", + G_CALLBACK(AnalogSliderDialogKeyboardProc), (PVOID)name); + g_signal_connect (GTK_RANGE(AnalogSliderTrackbar), "button-release-event", + G_CALLBACK (AnalogSliderDialogMouseProc), (PVOID)name); + g_signal_connect (GTK_SCALE(AnalogSliderTrackbar), "move-slider", + G_CALLBACK (AnalogSliderUpdateProc), (PVOID)name); -// TranslateMessage(&msg); -// DispatchMessage(&msg); -// } + gtk_widget_show_all(AnalogSliderMain); -// if(!AnalogSliderCancel) { -// SWORD v = (SWORD)SendMessage(AnalogSliderTrackbar, TBM_GETPOS, 0, 0); -// SetAdcShadow(name, v); -// } + AnalogSliderDone = FALSE; + AnalogSliderCancel = FALSE; -// EnableWindow(MainWindow, TRUE); -// DestroyWindow(AnalogSliderMain); -// ListView_RedrawItems(IoList, 0, Prog.io.count - 1); -// } + // SWORD orig = GetAdcShadow(name); + + // ListView_RedrawItems(IoList, 0, Prog.io.count - 1); +} //----------------------------------------------------------------------------- // Window proc for the contacts dialog box @@ -551,8 +575,8 @@ void SaveIoListToFile(FILE *f) //----------------------------------------------------------------------------- // Create our window class; nothing exciting. //----------------------------------------------------------------------------- -// static BOOL MakeWindowClass() -// { +static BOOL MakeWindowClass() +{ // WNDCLASSEX wc; // memset(&wc, 0, sizeof(wc)); // wc.cbSize = sizeof(wc); @@ -571,10 +595,10 @@ void SaveIoListToFile(FILE *f) // IMAGE_ICON, 16, 16, 0); // return RegisterClassEx(&wc); -// } +} -// static void MakeControls(void) -// { +static void MakeControls(void) +{ // HWND textLabel = CreateWindowEx(0, WC_STATIC, _("Assign:"), // WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE, // 6, 1, 80, 17, IoDialog, NULL, Instance, NULL); @@ -594,10 +618,10 @@ void SaveIoListToFile(FILE *f) // WS_CHILD | WS_TABSTOP | WS_CLIPSIBLINGS | WS_VISIBLE, // 6, 356, 95, 23, IoDialog, NULL, Instance, NULL); // NiceFont(CancelButton); -// } +} -// void ShowIoDialog(int item) -// { +void ShowIoDialog(int item) +{ // if(!Prog.mcu) { // MessageBox(MainWindow, // _("No microcontroller has been selected. You must select a " @@ -759,7 +783,7 @@ void SaveIoListToFile(FILE *f) // EnableWindow(MainWindow, TRUE); // DestroyWindow(IoDialog); // return; -// } +} //----------------------------------------------------------------------------- // Called in response to a notify for the listview. Handles click, text-edit @@ -767,119 +791,129 @@ void SaveIoListToFile(FILE *f) // where (LPSTR_TEXTCALLBACK); that way we don't have two parallel copies of // the I/O list to keep in sync. //----------------------------------------------------------------------------- -// void IoListProc(NMHDR *h) -// { -// switch(h->code) { -// case LVN_GETDISPINFO: { -// NMLVDISPINFO *i = (NMLVDISPINFO *)h; -// int item = i->item.iItem; -// switch(i->item.iSubItem) { -// case LV_IO_PIN: -// // Don't confuse people by displaying bogus pin assignments -// // for the C target. -// if(Prog.mcu && (Prog.mcu->whichIsa == ISA_ANSIC || -// Prog.mcu->whichIsa == ISA_INTERPRETED) ) -// { -// strcpy(i->item.pszText, ""); -// break; -// } - -// PinNumberForIo(i->item.pszText, -// &(Prog.io.assignment[item])); -// break; +void IoListProc(NMHDR *h) +{ + switch(h->code) { + case LVN_GETDISPINFO: { + + int item = h->item.iItem; + /// Don't confuse people by displaying bogus pin assignments + /// for the C target. -// case LV_IO_TYPE: { -// char *s = IoTypeToString(Prog.io.assignment[item].type); -// strcpy(i->item.pszText, s); -// break; -// } -// case LV_IO_NAME: -// strcpy(i->item.pszText, Prog.io.assignment[item].name); -// break; + char IO_value_holder[60]; -// case LV_IO_PORT: { -// // Don't confuse people by displaying bogus pin assignments -// // for the C target. -// if(Prog.mcu && Prog.mcu->whichIsa == ISA_ANSIC) { -// strcpy(i->item.pszText, ""); -// break; -// } - -// int type = Prog.io.assignment[item].type; -// if(type != IO_TYPE_DIG_INPUT && type != IO_TYPE_DIG_OUTPUT -// && type != IO_TYPE_READ_ADC) -// { -// strcpy(i->item.pszText, ""); -// break; -// } - -// int pin = Prog.io.assignment[item].pin; -// if(pin == NO_PIN_ASSIGNED || !Prog.mcu) { -// strcpy(i->item.pszText, ""); -// break; -// } - -// if(UartFunctionUsed() && Prog.mcu) { -// if((Prog.mcu->uartNeeds.rxPin == pin) || -// (Prog.mcu->uartNeeds.txPin == pin)) -// { -// strcpy(i->item.pszText, _("<UART needs!>")); -// break; -// } -// } - -// if(PwmFunctionUsed() && Prog.mcu) { -// if(Prog.mcu->pwmNeedsPin == pin) { -// strcpy(i->item.pszText, _("<PWM needs!>")); -// break; -// } -// } - -// int j; -// for(j = 0; j < Prog.mcu->pinCount; j++) { -// if(Prog.mcu->pinInfo[j].pin == pin) { -// sprintf(i->item.pszText, "%c%c%d", -// Prog.mcu->portPrefix, -// Prog.mcu->pinInfo[j].port, -// Prog.mcu->pinInfo[j].bit); -// break; -// } -// } -// if(j == Prog.mcu->pinCount) { -// sprintf(i->item.pszText, _("<not an I/O!>")); -// } -// break; -// } + GValue val = G_VALUE_INIT; + g_value_init (&val, G_TYPE_STRING); -// case LV_IO_STATE: { -// if(InSimulationMode) { -// char *name = Prog.io.assignment[item].name; -// DescribeForIoList(name, i->item.pszText); -// } else { -// strcpy(i->item.pszText, ""); -// } -// break; -// } + /// case LV_IO_PIN: + if(Prog.mcu && (Prog.mcu->whichIsa == ISA_ANSIC || + Prog.mcu->whichIsa == ISA_INTERPRETED) ) + { + strcpy(IO_value_holder, ""); -// } -// break; -// } -// case LVN_ITEMACTIVATE: { -// NMITEMACTIVATE *i = (NMITEMACTIVATE *)h; -// if(InSimulationMode) { -// char *name = Prog.io.assignment[i->iItem].name; -// if(name[0] == 'X') { -// SimulationToggleContact(name); -// } else if(name[0] == 'A') { -// ShowAnalogSliderPopup(name); -// } -// } else { -// UndoRemember(); -// ShowIoDialog(i->iItem); -// ProgramChanged(); -// InvalidateRect(MainWindow, NULL, FALSE); -// } -// break; -// } -// } -// } + g_value_set_string(&val, (const char*)&IO_value_holder); + gtk_list_store_set_value (GTK_LIST_STORE(h->hlistFrom), h->hlistIter, LV_IO_PIN, &val); + } + else + { + PinNumberForIo(IO_value_holder, &(Prog.io.assignment[item])); + g_value_set_string(&val, (const char*)&IO_value_holder); + gtk_list_store_set_value (GTK_LIST_STORE(h->hlistFrom), h->hlistIter, LV_IO_PIN, &val); + } + + /// case LV_IO_TYPE: + char *s = IoTypeToString(Prog.io.assignment[item].type); + + g_value_set_string(&val, (const char*)s); + gtk_list_store_set_value (GTK_LIST_STORE(h->hlistFrom), h->hlistIter, LV_IO_TYPE, &val); + + /// case LV_IO_NAME: + g_value_set_string(&val, (const char*)Prog.io.assignment[item].name); + gtk_list_store_set_value (GTK_LIST_STORE(h->hlistFrom), h->hlistIter, LV_IO_NAME, &val); + + /// case LV_IO_PORT: + /// Don't confuse people by displaying bogus pin assignments + /// for the C target. + if(Prog.mcu && Prog.mcu->whichIsa == ISA_ANSIC) { + strcpy(IO_value_holder, ""); + break; + } + + int type = Prog.io.assignment[item].type; + if(type != IO_TYPE_DIG_INPUT && type != IO_TYPE_DIG_OUTPUT + && type != IO_TYPE_READ_ADC) + { + strcpy(IO_value_holder, ""); + break; + } + + int pin = Prog.io.assignment[item].pin; + if(pin == NO_PIN_ASSIGNED || !Prog.mcu) { + strcpy(IO_value_holder, ""); + break; + } + + if(UartFunctionUsed() && Prog.mcu) { + if((Prog.mcu->uartNeeds.rxPin == pin) || + (Prog.mcu->uartNeeds.txPin == pin)) + { + strcpy(IO_value_holder, _("<UART needs!>")); + break; + } + } + + if(PwmFunctionUsed() && Prog.mcu) { + if(Prog.mcu->pwmNeedsPin == pin) { + strcpy(IO_value_holder, _("<PWM needs!>")); + break; + } + } + + int j; + for(j = 0; j < Prog.mcu->pinCount; j++) { + if(Prog.mcu->pinInfo[j].pin == pin) { + sprintf(IO_value_holder, "%c%c%d", + Prog.mcu->portPrefix, + Prog.mcu->pinInfo[j].port, + Prog.mcu->pinInfo[j].bit); + break; + } + } + if(j == Prog.mcu->pinCount) { + sprintf(IO_value_holder, _("<not an I/O!>")); + } + + g_value_set_string(&val, (const char*)IO_value_holder); + gtk_list_store_set_value (GTK_LIST_STORE(h->hlistFrom), h->hlistIter, LV_IO_PORT, &val); + + /// case LV_IO_STATE: + if(InSimulationMode) { + char *name = Prog.io.assignment[item].name; + DescribeForIoList(name, IO_value_holder); + } else { + strcpy(IO_value_holder, ""); + } + + g_value_set_string(&val, (const char*)IO_value_holder); + gtk_list_store_set_value (GTK_LIST_STORE(h->hlistFrom), h->hlistIter, LV_IO_STATE, &val); + + break; + } + case LVN_ITEMACTIVATE: { + if(InSimulationMode) { + char *name = Prog.io.assignment[h->item.iItem].name; + if(name[0] == 'X') { + SimulationToggleContact(name); + } else if(name[0] == 'A') { + ShowAnalogSliderPopup(name); + } + } else { + // UndoRemember(); + // ShowIoDialog(h->item.iItem); + // ProgramChanged(); + // InvalidateRect(MainWindow, NULL, FALSE); + } + break; + } + } +} diff --git a/ldmicro/ldmicro.cpp b/ldmicro/ldmicro.cpp index 6667b2f..db4d588 100644 --- a/ldmicro/ldmicro.cpp +++ b/ldmicro/ldmicro.cpp @@ -40,7 +40,6 @@ using namespace std; HINSTANCE Instance; HWID MainWindow; HWID DrawWindow; -// HCRDC Hcr; // parameters used to capture the mouse when implementing our totally non- // general splitter control @@ -66,79 +65,83 @@ char CurrentCompileFile[MAX_PATH]; // project file. PlcProgram Prog; +/// Function to safely quit program gtk main loop +void LD_WM_Close_call(GtkWidget *widget, GdkEvent *event, gpointer user_data); + //----------------------------------------------------------------------------- // Get a filename with a common dialog box and then save the program to that // file and then set our default filename to that. //----------------------------------------------------------------------------- -// static BOOL SaveAsDialog(void) -// { -// OPENFILENAME ofn; - -// memset(&ofn, 0, sizeof(ofn)); -// ofn.lStructSize = sizeof(ofn); -// ofn.hInstance = Instance; -// ofn.lpstrFilter = LDMICRO_PATTERN; -// ofn.lpstrDefExt = "ld"; -// ofn.lpstrFile = CurrentSaveFile; -// ofn.nMaxFile = sizeof(CurrentSaveFile); -// ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT; - -// if(!GetSaveFileName(&ofn)) -// return FALSE; - -// if(!SaveProjectToFile(CurrentSaveFile)) { -// Error(_("Couldn't write to '%s'."), CurrentSaveFile); -// return FALSE; -// } else { -// ProgramChangedNotSaved = FALSE; -// return TRUE; -// } -// } +static BOOL SaveAsDialog(void) +{ + OPENFILENAME ofn; + + memset(&ofn, 0, sizeof(ofn)); + ofn.lStructSize = sizeof(ofn); + ofn.parentWindow = MainWindow; + ofn.lpstrFilter = LDMICRO_PATTERN; + ofn.lpstrDefExt = "ld"; + ofn.lpstrFile = CurrentSaveFile; + ofn.nMaxFile = sizeof(CurrentSaveFile); + ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT; + + if(!GetSaveFileName(&ofn)) + return FALSE; + + if(!SaveProjectToFile(CurrentSaveFile)) { + Error(_("Couldn't write to '%s'."), CurrentSaveFile); + return FALSE; + } else { + ProgramChangedNotSaved = FALSE; + return TRUE; + } +} //----------------------------------------------------------------------------- // Get a filename with a common dialog box and then export the program as // an ASCII art drawing. //----------------------------------------------------------------------------- -// static void ExportDialog(void) -// { -// char exportFile[MAX_PATH]; -// OPENFILENAME ofn; - -// exportFile[0] = '\0'; - -// memset(&ofn, 0, sizeof(ofn)); -// ofn.lStructSize = sizeof(ofn); -// ofn.hInstance = Instance; -// ofn.lpstrFilter = TXT_PATTERN; -// ofn.lpstrFile = exportFile; -// ofn.lpstrTitle = _("Export As Text"); -// ofn.nMaxFile = sizeof(exportFile); -// ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT; - -// if(!GetSaveFileName(&ofn)) -// return; +static void ExportDialog(void) +{ + char exportFile[MAX_PATH]; + OPENFILENAME ofn; + + exportFile[0] = '\0'; + + memset(&ofn, 0, sizeof(ofn)); + ofn.lStructSize = sizeof(ofn); + ofn.parentWindow = MainWindow; + ofn.lpstrFilter = TXT_PATTERN; + ofn.lpstrFile = exportFile; + ofn.lpstrDefExt = "txt"; + ofn.lpstrTitle = _("Export As Text"); + ofn.nMaxFile = sizeof(exportFile); + ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT; + + if(!GetSaveFileName(&ofn)) + return; -// ExportDrawingAsText(exportFile); -// } + ExportDrawingAsText(exportFile); +} //----------------------------------------------------------------------------- // If we already have a filename, save the program to that. Otherwise same // as Save As. Returns TRUE if it worked, else returns FALSE. //----------------------------------------------------------------------------- -// static BOOL SaveProgram(void) -// { -// if(strlen(CurrentSaveFile)) { -// if(!SaveProjectToFile(CurrentSaveFile)) { -// Error(_("Couldn't write to '%s'."), CurrentSaveFile); -// return FALSE; -// } else { -// ProgramChangedNotSaved = FALSE; -// return TRUE; -// } -// } else { -// return SaveAsDialog(); -// } -// } +static BOOL SaveProgram(void) +{ + if(strlen(CurrentSaveFile)) { + if(!SaveProjectToFile(CurrentSaveFile)) { + Error(_("Couldn't write to '%s'."), CurrentSaveFile); + return FALSE; + } else { + ProgramChangedNotSaved = FALSE; + return TRUE; + } + } else { + return SaveAsDialog(); + } +} //----------------------------------------------------------------------------- // Compile the program to a hex file for the target micro. Get the output @@ -151,7 +154,7 @@ static void CompileProgram(BOOL compileAs) memset(&ofn, 0, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); - ofn.parentWindow = NULL; + ofn.parentWindow = MainWindow; ofn.lpstrTitle = _("Compile To"); if(Prog.mcu && Prog.mcu->whichIsa == ISA_ANSIC) { ofn.lpstrFilter = C_PATTERN; @@ -175,12 +178,12 @@ static void CompileProgram(BOOL compileAs) } if(!GenerateIntermediateCode()) return; - + if(Prog.mcu == NULL) { Error(_("Must choose a target microcontroller before compiling.")); return; } - + if(UartFunctionUsed() && Prog.mcu->uartNeeds.rxPin == 0) { Error(_("UART function used but not supported for this micro.")); return; @@ -190,7 +193,7 @@ static void CompileProgram(BOOL compileAs) Error(_("PWM function used but not supported for this micro.")); return; } - + switch(Prog.mcu->whichIsa) { case ISA_AVR: CompileAvr(CurrentCompileFile); break; case ISA_PIC16: CompilePic16(CurrentCompileFile); break; @@ -200,7 +203,8 @@ static void CompileProgram(BOOL compileAs) default: oops(); } - IntDumpListing("t.pl"); + + IntDumpListing("t.pl"); } //----------------------------------------------------------------------------- @@ -208,70 +212,71 @@ static void CompileProgram(BOOL compileAs) // or to cancel the operation they are performing. Return TRUE if they want // to cancel. //----------------------------------------------------------------------------- -// BOOL CheckSaveUserCancels(void) -// { -// if(!ProgramChangedNotSaved) { -// // no problem -// return FALSE; -// } +BOOL CheckSaveUserCancels(void) +{ + if(!ProgramChangedNotSaved) { + // no problem + return FALSE; + } -// int r = MessageBox(MainWindow, -// _("The program has changed since it was last saved.\r\n\r\n" -// "Do you want to save the changes?"), "LDmicro", -// MB_YESNOCANCEL | MB_ICONWARNING); -// switch(r) { -// case IDYES: -// if(SaveProgram()) -// return FALSE; -// else -// return TRUE; - -// case IDNO: -// return FALSE; - -// case IDCANCEL: -// return TRUE; - -// default: -// oops(); -// } -// } + int r = MessageBox(MainWindow, + _("The program has changed since it was last saved.\r\n\r\n" + "Do you want to save the changes?"), "LDmicro", + MB_YESNOCANCEL | MB_ICONWARNING); + switch(r) { + case IDYES: + if(SaveProgram()) + return FALSE; + else + return TRUE; + + case IDNO: + return FALSE; + + case IDCANCEL: + return TRUE; + + default: + oops(); + } + +} //----------------------------------------------------------------------------- // Load a new program from a file. If it succeeds then set our default filename // to that, else we end up with an empty file then. //----------------------------------------------------------------------------- -// static void OpenDialog(void) -// { -// OPENFILENAME ofn; - -// char tempSaveFile[MAX_PATH] = ""; - -// memset(&ofn, 0, sizeof(ofn)); -// ofn.lStructSize = sizeof(ofn); -// ofn.hInstance = Instance; -// ofn.lpstrFilter = LDMICRO_PATTERN; -// ofn.lpstrDefExt = "ld"; -// ofn.lpstrFile = tempSaveFile; -// ofn.nMaxFile = sizeof(tempSaveFile); -// ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; - -// if(!GetOpenFileName(&ofn)) -// return; - -// if(!LoadProjectFromFile(tempSaveFile)) { -// Error(_("Couldn't open '%s'."), tempSaveFile); -// CurrentSaveFile[0] = '\0'; -// } else { -// ProgramChangedNotSaved = FALSE; -// strcpy(CurrentSaveFile, tempSaveFile); -// UndoFlush(); -// } +static void OpenDialog(void) +{ + OPENFILENAME ofn; -// GenerateIoListDontLoseSelection(); -// RefreshScrollbars(); -// UpdateMainWindowTitleBar(); -// } + char tempSaveFile[MAX_PATH] = ""; + + memset(&ofn, 0, sizeof(ofn)); + ofn.lStructSize = sizeof(ofn); + ofn.parentWindow = MainWindow; + ofn.lpstrFilter = LDMICRO_PATTERN; + ofn.lpstrDefExt = "ld"; + ofn.lpstrFile = tempSaveFile; + ofn.nMaxFile = sizeof(tempSaveFile); + ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY; + + if(!GetOpenFileName(&ofn)) + return; + + if(!LoadProjectFromFile(tempSaveFile)) { + Error(_("Couldn't open '%s'."), tempSaveFile); + CurrentSaveFile[0] = '\0'; + } else { + ProgramChangedNotSaved = FALSE; + strcpy(CurrentSaveFile, tempSaveFile); + UndoFlush(); + } + + GenerateIoListDontLoseSelection(); + RefreshScrollbars(); + UpdateMainWindowTitleBar(); +} //----------------------------------------------------------------------------- // Housekeeping required when the program changes: mark the program as @@ -282,7 +287,7 @@ void ProgramChanged(void) { ProgramChangedNotSaved = TRUE; GenerateIoListDontLoseSelection(); - // RefreshScrollbars(); + RefreshScrollbars(); } #define CHANGING_PROGRAM(x) { \ UndoRemember(); \ @@ -344,93 +349,94 @@ static void ProcessMenu(int code) switch(code) { case MNU_NEW: - // if(CheckSaveUserCancels()) break; - // NewProgram(); - // strcpy(CurrentSaveFile, ""); - // strcpy(CurrentCompileFile, ""); - // GenerateIoListDontLoseSelection(); - // RefreshScrollbars(); - // UpdateMainWindowTitleBar(); + if(CheckSaveUserCancels()) break; + NewProgram(); + strcpy(CurrentSaveFile, ""); + strcpy(CurrentCompileFile, ""); + GenerateIoListDontLoseSelection(); + RefreshScrollbars(); + UpdateMainWindowTitleBar(); break; case MNU_OPEN: - // if(CheckSaveUserCancels()) break; - // OpenDialog(); + if(CheckSaveUserCancels()) break; + OpenDialog(); break; case MNU_SAVE: - // SaveProgram(); - // UpdateMainWindowTitleBar(); + SaveProgram(); + UpdateMainWindowTitleBar(); break; case MNU_SAVE_AS: - // SaveAsDialog(); - // UpdateMainWindowTitleBar(); + SaveAsDialog(); + UpdateMainWindowTitleBar(); break; case MNU_EXPORT: - // ExportDialog(); + ExportDialog(); break; case MNU_EXIT: - // if(CheckSaveUserCancels()) break; + if(CheckSaveUserCancels()) break; + LD_WM_Close_call(NULL, NULL, NULL); // PostQuitMessage(0); break; case MNU_INSERT_COMMENT: - // CHANGING_PROGRAM(AddComment(_("--add comment here--"))); + CHANGING_PROGRAM(AddComment(_("--add comment here--"))); break; case MNU_INSERT_CONTACTS: - // CHANGING_PROGRAM(AddContact()); + CHANGING_PROGRAM(AddContact()); break; case MNU_INSERT_COIL: - // CHANGING_PROGRAM(AddCoil()); + CHANGING_PROGRAM(AddCoil()); break; case MNU_INSERT_TON: - // CHANGING_PROGRAM(AddTimer(ELEM_TON)); + CHANGING_PROGRAM(AddTimer(ELEM_TON)); break; case MNU_INSERT_TOF: - // CHANGING_PROGRAM(AddTimer(ELEM_TOF)); + CHANGING_PROGRAM(AddTimer(ELEM_TOF)); break; case MNU_INSERT_RTO: - // CHANGING_PROGRAM(AddTimer(ELEM_RTO)); + CHANGING_PROGRAM(AddTimer(ELEM_RTO)); break; case MNU_INSERT_CTU: - // CHANGING_PROGRAM(AddCounter(ELEM_CTU)); + CHANGING_PROGRAM(AddCounter(ELEM_CTU)); break; case MNU_INSERT_CTD: - // CHANGING_PROGRAM(AddCounter(ELEM_CTD)); + CHANGING_PROGRAM(AddCounter(ELEM_CTD)); break; case MNU_INSERT_CTC: - // CHANGING_PROGRAM(AddCounter(ELEM_CTC)); + CHANGING_PROGRAM(AddCounter(ELEM_CTC)); break; case MNU_INSERT_RES: - // CHANGING_PROGRAM(AddReset()); + CHANGING_PROGRAM(AddReset()); break; case MNU_INSERT_OPEN: - // CHANGING_PROGRAM(AddEmpty(ELEM_OPEN)); + CHANGING_PROGRAM(AddEmpty(ELEM_OPEN)); break; case MNU_INSERT_SHORT: - // CHANGING_PROGRAM(AddEmpty(ELEM_SHORT)); + CHANGING_PROGRAM(AddEmpty(ELEM_SHORT)); break; case MNU_INSERT_MASTER_RLY: - // CHANGING_PROGRAM(AddMasterRelay()); + CHANGING_PROGRAM(AddMasterRelay()); break; case MNU_INSERT_SHIFT_REG: - // CHANGING_PROGRAM(AddShiftRegister()); + CHANGING_PROGRAM(AddShiftRegister()); break; case MNU_INSERT_LUT: @@ -438,115 +444,115 @@ static void ProcessMenu(int code) break; case MNU_INSERT_PWL: - // CHANGING_PROGRAM(AddPiecewiseLinear()); + CHANGING_PROGRAM(AddPiecewiseLinear()); break; case MNU_INSERT_FMTD_STR: - // CHANGING_PROGRAM(AddFormattedString()); + CHANGING_PROGRAM(AddFormattedString()); break; case MNU_INSERT_OSR: - // CHANGING_PROGRAM(AddEmpty(ELEM_ONE_SHOT_RISING)); + CHANGING_PROGRAM(AddEmpty(ELEM_ONE_SHOT_RISING)); break; case MNU_INSERT_OSF: - // CHANGING_PROGRAM(AddEmpty(ELEM_ONE_SHOT_FALLING)); + CHANGING_PROGRAM(AddEmpty(ELEM_ONE_SHOT_FALLING)); break; case MNU_INSERT_MOV: - // CHANGING_PROGRAM(AddMove()); + CHANGING_PROGRAM(AddMove()); break; case MNU_INSERT_SET_PWM: - // CHANGING_PROGRAM(AddSetPwm()); + CHANGING_PROGRAM(AddSetPwm()); break; case MNU_INSERT_READ_ADC: - // CHANGING_PROGRAM(AddReadAdc()); + CHANGING_PROGRAM(AddReadAdc()); break; case MNU_INSERT_UART_SEND: - // CHANGING_PROGRAM(AddUart(ELEM_UART_SEND)); + CHANGING_PROGRAM(AddUart(ELEM_UART_SEND)); break; case MNU_INSERT_UART_RECV: - // CHANGING_PROGRAM(AddUart(ELEM_UART_RECV)); + CHANGING_PROGRAM(AddUart(ELEM_UART_RECV)); break; case MNU_INSERT_PERSIST: - // CHANGING_PROGRAM(AddPersist()); + CHANGING_PROGRAM(AddPersist()); break; -// { -// int elem; -// case MNU_INSERT_ADD: elem = ELEM_ADD; goto math; -// case MNU_INSERT_SUB: elem = ELEM_SUB; goto math; -// case MNU_INSERT_MUL: elem = ELEM_MUL; goto math; -// case MNU_INSERT_DIV: elem = ELEM_DIV; goto math; -// math: -// CHANGING_PROGRAM(AddMath(elem)); -// break; -// } + { + int elem; + case MNU_INSERT_ADD: elem = ELEM_ADD; goto math; + case MNU_INSERT_SUB: elem = ELEM_SUB; goto math; + case MNU_INSERT_MUL: elem = ELEM_MUL; goto math; + case MNU_INSERT_DIV: elem = ELEM_DIV; goto math; +math: + CHANGING_PROGRAM(AddMath(elem)); + break; + } -// { -// int elem; -// case MNU_INSERT_EQU: elem = ELEM_EQU; goto cmp; -// case MNU_INSERT_NEQ: elem = ELEM_NEQ; goto cmp; -// case MNU_INSERT_GRT: elem = ELEM_GRT; goto cmp; -// case MNU_INSERT_GEQ: elem = ELEM_GEQ; goto cmp; -// case MNU_INSERT_LES: elem = ELEM_LES; goto cmp; -// case MNU_INSERT_LEQ: elem = ELEM_LEQ; goto cmp; -// cmp: -// CHANGING_PROGRAM(AddCmp(elem)); -// break; -// } + { + int elem; + case MNU_INSERT_EQU: elem = ELEM_EQU; goto cmp; + case MNU_INSERT_NEQ: elem = ELEM_NEQ; goto cmp; + case MNU_INSERT_GRT: elem = ELEM_GRT; goto cmp; + case MNU_INSERT_GEQ: elem = ELEM_GEQ; goto cmp; + case MNU_INSERT_LES: elem = ELEM_LES; goto cmp; + case MNU_INSERT_LEQ: elem = ELEM_LEQ; goto cmp; +cmp: + CHANGING_PROGRAM(AddCmp(elem)); + break; + } case MNU_MAKE_NORMAL: - // CHANGING_PROGRAM(MakeNormalSelected()); + CHANGING_PROGRAM(MakeNormalSelected()); break; case MNU_NEGATE: - // CHANGING_PROGRAM(NegateSelected()); + CHANGING_PROGRAM(NegateSelected()); break; case MNU_MAKE_SET_ONLY: - // CHANGING_PROGRAM(MakeSetOnlySelected()); + CHANGING_PROGRAM(MakeSetOnlySelected()); break; case MNU_MAKE_RESET_ONLY: - // CHANGING_PROGRAM(MakeResetOnlySelected()); + CHANGING_PROGRAM(MakeResetOnlySelected()); break; case MNU_UNDO: - // UndoUndo(); + UndoUndo(); break; case MNU_REDO: - // UndoRedo(); + UndoRedo(); break; case MNU_INSERT_RUNG_BEFORE: - // CHANGING_PROGRAM(InsertRung(FALSE)); + CHANGING_PROGRAM(InsertRung(FALSE)); break; case MNU_INSERT_RUNG_AFTER: - // CHANGING_PROGRAM(InsertRung(TRUE)); + CHANGING_PROGRAM(InsertRung(TRUE)); break; case MNU_DELETE_RUNG: - // CHANGING_PROGRAM(DeleteSelectedRung()); + CHANGING_PROGRAM(DeleteSelectedRung()); break; case MNU_PUSH_RUNG_UP: - // CHANGING_PROGRAM(PushRungUp()); + CHANGING_PROGRAM(PushRungUp()); break; case MNU_PUSH_RUNG_DOWN: - // CHANGING_PROGRAM(PushRungDown()); + CHANGING_PROGRAM(PushRungDown()); break; case MNU_DELETE_ELEMENT: - // CHANGING_PROGRAM(DeleteSelectedFromProgram()); + CHANGING_PROGRAM(DeleteSelectedFromProgram()); break; case MNU_MCU_SETTINGS: @@ -554,27 +560,27 @@ static void ProcessMenu(int code) break; case MNU_SIMULATION_MODE: - // ToggleSimulationMode(); + ToggleSimulationMode(); break; case MNU_START_SIMULATION: - // StartSimulation(); + StartSimulation(); break; case MNU_STOP_SIMULATION: - // StopSimulation(); + StopSimulation(); break; case MNU_SINGLE_CYCLE: - // SimulateOneCycle(TRUE); + SimulateOneCycle(TRUE); break; case MNU_COMPILE: - // CompileProgram(FALSE); + CompileProgram(FALSE); break; case MNU_COMPILE_AS: - // CompileProgram(TRUE); + CompileProgram(TRUE); break; case MNU_MANUAL: @@ -585,417 +591,84 @@ static void ProcessMenu(int code) ShowHelpDialog(TRUE); break; } -} - -void WM_COMMAND (GtkMenuItem* men, gpointer gpcode){ - int tempcode = GPOINTER_TO_INT(gpcode); - ProcessMenu (tempcode); -} - -void MenuHandler (){ - g_signal_connect(G_OBJECT(McuSettingsMenu), "activate", - G_CALLBACK(WM_COMMAND), GINT_TO_POINTER(MNU_INSERT_LUT)); - g_signal_connect(G_OBJECT(McuSettingsMenu), "activate", - G_CALLBACK(WM_COMMAND), GINT_TO_POINTER(MNU_MCU_SETTINGS)); - g_signal_connect(G_OBJECT(ManualMenu), "activate", - G_CALLBACK(WM_COMMAND), GINT_TO_POINTER(MNU_MANUAL)); - g_signal_connect(G_OBJECT(AboutMenu), "activate", - G_CALLBACK(WM_COMMAND), GINT_TO_POINTER(MNU_ABOUT)); + gtk_widget_queue_draw(DrawWindow); } //----------------------------------------------------------------------------- // WndProc for MainWindow. //----------------------------------------------------------------------------- -LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) -{ - // switch (msg) { - // case WM_ERASEBKGND: - // break; +// LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) +// { +// // switch (msg) { +// // case WM_ERASEBKGND: +// // break; - // case WM_SETFOCUS: +// // case WM_SETFOCUS: - // case WM_PAINT: { - // } +// // case WM_PAINT: { +// // } - // case WM_KEYDOWN: { - // } +// // case WM_KEYDOWN: { +// // } - // case WM_LBUTTONDBLCLK: { - // } +// // case WM_LBUTTONDBLCLK: { +// // } - // case WM_LBUTTONDOWN: { - // } - // case WM_MOUSEMOVE: { - // } - // case WM_MOUSEWHEEL: { - // } +// // case WM_LBUTTONDOWN: { +// // } +// // case WM_MOUSEMOVE: { +// // } +// // case WM_MOUSEWHEEL: { +// // } - // case WM_SIZE: +// // case WM_SIZE: - // case WM_NOTIFY: { - // NMHDR *h = (NMHDR *)lParam; - // if(h->hwndFrom == IoList) { - // IoListProc(h); - // } - // return 0; - // } - // case WM_VSCROLL: +// // case WM_NOTIFY: { - // case WM_HSCROLL: +// // case WM_VSCROLL: - // case WM_COMMAND: - // ProcessMenu(LOWORD(wParam)); - // InvalidateRect(MainWindow, NULL, FALSE); - // break; +// // case WM_HSCROLL: - // case WM_CLOSE: - // case WM_DESTROY: +// // case WM_COMMAND: - // default: - // return DefWindowProc(hwnd, msg, wParam, lParam); - // } +// // case WM_CLOSE: +// // case WM_DESTROY: - return 1; -} +// // default: +// // return DefWindowProc(hwnd, msg, wParam, lParam); +// // } + +// return 1; +// } -void LD_WM_Close_call(GtkWidget *widget, GdkEvent *event, gpointer user_data)//(HWND window) +void LD_WM_Close_call(GtkWidget *widget, GdkEvent *event, gpointer user_data) { /* Handles: * WM_CLOSE */ - // if(Hdc != NULL) - // cairo_destroy(Hdc); FreezeWindowPos(MainWindow); FreezeDWORD(IoListHeight); gtk_main_quit(); } -gboolean LD_WM_KeyDown_call(GtkWidget *widget, GdkEvent *event, gpointer user_data) -{ - /* Handles: - * WM_KEYDOWN - */ - // g_print("ky call\n"); - switch(event->key.state) - { - case GDK_SHIFT_MASK: - g_print("SHIFT+"); - break; - case GDK_CONTROL_MASK: - g_print("CONTROL+"); - break; - } - - g_print("%c\n", (char)gdk_keyval_to_unicode(event->key.keyval)); - - // if(wParam == 'M') { - // if(GetAsyncKeyState(VK_CONTROL) & 0x8000) { - // ToggleSimulationMode(); - // break; - // } - // } else if(wParam == VK_TAB) { - // SetFocus(IoList); - // BlinkCursor(0, 0, 0, 0); - // break; - // } else if(wParam == VK_F1) { - // ShowHelpDialog(FALSE); - // break; - // } - - // if(InSimulationMode) { - // switch(wParam) { - // case ' ': - // SimulateOneCycle(TRUE); - // break; - - // case 'R': - // if(GetAsyncKeyState(VK_CONTROL) & 0x8000) - // StartSimulation(); - // break; - - // case 'H': - // if(GetAsyncKeyState(VK_CONTROL) & 0x8000) - // StopSimulation(); - // break; - - // case VK_DOWN: - // if(ScrollYOffset < ScrollYOffsetMax) - // ScrollYOffset++; - // RefreshScrollbars(); - // InvalidateRect(MainWindow, NULL, FALSE); - // break; - - // case VK_UP: - // if(ScrollYOffset > 0) - // ScrollYOffset--; - // RefreshScrollbars(); - // InvalidateRect(MainWindow, NULL, FALSE); - // break; - - // case VK_LEFT: - // ScrollXOffset -= FONT_WIDTH; - // if(ScrollXOffset < 0) ScrollXOffset = 0; - // RefreshScrollbars(); - // InvalidateRect(MainWindow, NULL, FALSE); - // break; - - // case VK_RIGHT: - // ScrollXOffset += FONT_WIDTH; - // if(ScrollXOffset >= ScrollXOffsetMax) - // ScrollXOffset = ScrollXOffsetMax; - // RefreshScrollbars(); - // InvalidateRect(MainWindow, NULL, FALSE); - // break; - - // case VK_RETURN: - // case VK_ESCAPE: - // ToggleSimulationMode(); - // break; - // } - // break; - // } - - - // switch(wParam) { - // case VK_F5: - // CompileProgram(FALSE); - // break; - - // case VK_UP: - // if(GetAsyncKeyState(VK_SHIFT) & 0x8000) { - // CHANGING_PROGRAM(PushRungUp()); - // } else { - // MoveCursorKeyboard(wParam); - // } - // break; - - // case VK_DOWN: - // if(GetAsyncKeyState(VK_SHIFT) & 0x8000) { - // CHANGING_PROGRAM(PushRungDown()); - // } else { - // MoveCursorKeyboard(wParam); - // } - // break; - - // case VK_RIGHT: - // case VK_LEFT: - // MoveCursorKeyboard(wParam); - // break; - - // case VK_RETURN: - // CHANGING_PROGRAM(EditSelectedElement()); - // break; - - // case VK_DELETE: - // if(GetAsyncKeyState(VK_SHIFT) & 0x8000) { - // CHANGING_PROGRAM(DeleteSelectedRung()); - // } else { - // CHANGING_PROGRAM(DeleteSelectedFromProgram()); - // } - // break; - - // case VK_OEM_1: - // CHANGING_PROGRAM(AddComment(_("--add comment here--"))); - // break; - - // case 'C': - // CHANGING_PROGRAM(AddContact()); - // break; - - // // TODO: rather country-specific here - // case VK_OEM_2: - // CHANGING_PROGRAM(AddEmpty(ELEM_ONE_SHOT_RISING)); - // break; - - // case VK_OEM_5: - // CHANGING_PROGRAM(AddEmpty(ELEM_ONE_SHOT_FALLING)); - // break; - - // case 'L': - // CHANGING_PROGRAM(AddCoil()); - // break; - - // case 'R': - // CHANGING_PROGRAM(MakeResetOnlySelected()); - // break; - - // case 'E': - // if(GetAsyncKeyState(VK_CONTROL) & 0x8000) { - // ExportDialog(); - // } else { - // CHANGING_PROGRAM(AddReset()); - // } - // break; - - // case 'S': - // if(GetAsyncKeyState(VK_CONTROL) & 0x8000) { - // SaveProgram(); - // UpdateMainWindowTitleBar(); - // } else { - // CHANGING_PROGRAM(MakeSetOnlySelected()); - // } - // break; - - // case 'N': - // if(GetAsyncKeyState(VK_CONTROL) & 0x8000) { - // if(CheckSaveUserCancels()) break; - // if(!ProgramChangedNotSaved) { - // int r = MessageBox(MainWindow, - // _("Start new program?"), - // "LDmicro", MB_YESNO | MB_DEFBUTTON2 | - // MB_ICONQUESTION); - // if(r == IDNO) break; - // } - // NewProgram(); - // strcpy(CurrentSaveFile, ""); - // strcpy(CurrentCompileFile, ""); - // GenerateIoListDontLoseSelection(); - // RefreshScrollbars(); - // UpdateMainWindowTitleBar(); - // } else { - // CHANGING_PROGRAM(NegateSelected()); - // } - // break; - - // case 'A': - // CHANGING_PROGRAM(MakeNormalSelected()); - // break; - - // case 'T': - // CHANGING_PROGRAM(AddTimer(ELEM_RTO)); - // break; - - // case 'O': - // if(GetAsyncKeyState(VK_CONTROL) & 0x8000) { - // if(CheckSaveUserCancels()) break; - // OpenDialog(); - // } else { - // CHANGING_PROGRAM(AddTimer(ELEM_TON)); - // } - // break; - - // case 'F': - // CHANGING_PROGRAM(AddTimer(ELEM_TOF)); - // break; - - // case 'U': - // CHANGING_PROGRAM(AddCounter(ELEM_CTU)); - // break; - - // case 'I': - // CHANGING_PROGRAM(AddCounter(ELEM_CTD)); - // break; - - // case 'J': - // CHANGING_PROGRAM(AddCounter(ELEM_CTC)); - // break; - - // case 'M': - // CHANGING_PROGRAM(AddMove()); - // break; - - // case 'P': - // CHANGING_PROGRAM(AddReadAdc()); - // break; - - // case VK_OEM_PLUS: - // if(GetAsyncKeyState(VK_SHIFT) & 0x8000) { - // CHANGING_PROGRAM(AddMath(ELEM_ADD)); - // } else { - // CHANGING_PROGRAM(AddCmp(ELEM_EQU)); - // } - // break; - - // case VK_OEM_MINUS: - // if(GetAsyncKeyState(VK_SHIFT) & 0x8000) { - // } else { - // CHANGING_PROGRAM(AddMath(ELEM_SUB)); - // } - // break; - - // case '8': - // if(GetAsyncKeyState(VK_SHIFT) & 0x8000) { - // CHANGING_PROGRAM(AddMath(ELEM_MUL)); - // } - // break; - - // case 'D': - // CHANGING_PROGRAM(AddMath(ELEM_DIV)); - // break; - - // case VK_OEM_PERIOD: - // if(GetAsyncKeyState(VK_SHIFT) & 0x8000) { - // CHANGING_PROGRAM(AddCmp(ELEM_GRT)); - // } else { - // CHANGING_PROGRAM(AddCmp(ELEM_GEQ)); - // } - // break; - - // case VK_OEM_COMMA: - // if(GetAsyncKeyState(VK_SHIFT) & 0x8000) { - // CHANGING_PROGRAM(AddCmp(ELEM_LES)); - // } else { - // CHANGING_PROGRAM(AddCmp(ELEM_LEQ)); - // } - // break; - - // case 'V': - // if(GetAsyncKeyState(VK_SHIFT) & 0x8000) { - // CHANGING_PROGRAM(InsertRung(TRUE)); - // } - // break; - - // case '6': - // if(GetAsyncKeyState(VK_SHIFT) & 0x8000) { - // CHANGING_PROGRAM(InsertRung(FALSE)); - // } - // break; - - // case 'Z': - // if(GetAsyncKeyState(VK_CONTROL) & 0x8000) { - // UndoUndo(); - // } - // break; - - // case 'Y': - // if(GetAsyncKeyState(VK_CONTROL) & 0x8000) { - // UndoRedo(); - // } - // break; - - // default: - // break; - // } - // if(wParam != VK_SHIFT && wParam != VK_CONTROL) { - // InvalidateRect(MainWindow, NULL, FALSE); - // } - // break; - // g_print("ky call end\n"); - return FALSE; -} - gboolean LD_GTK_mouse_click_hook(GtkWidget *widget, GdkEvent *event, gpointer user_data) { /* Handles: * WM_LBUTTONDBLCLK, WM_LBUTTONDOWN */ - // g_print("mo cl call\n"); GtkAdjustment *adjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(ScrollWindow)); - // g_print("---\nadj = %f\n", gtk_adjustment_get_value(adjustment)); - // g_print("upper = %f\n", gtk_adjustment_get_upper(adjustment) - gtk_widget_get_allocated_height (ScrollWindow)); - // g_print("lower = %f\n", gtk_adjustment_get_lower(adjustment)); - // g_print("inc = %f\n", gtk_adjustment_get_step_increment(adjustment)); - // g_print("w width = %i\n", gtk_widget_get_allocated_width (DrawWindow)); - // g_print("w height = %i\n---\n", gtk_widget_get_allocated_height (ScrollWindow)); switch(event->button.type) { case GDK_BUTTON_PRESS: if (event->button.button == 1) /// left click { + GLOBAL_mouse_last_clicked_x = event->button.x_root; + GLOBAL_mouse_last_clicked_y = event->button.y_root; + int x = event->button.x; int y = event->button.y - 30 + gtk_adjustment_get_value(adjustment); @@ -1010,13 +683,15 @@ gboolean LD_GTK_mouse_click_hook(GtkWidget *widget, GdkEvent *event, gpointer us if(!InSimulationMode) MoveCursorMouseClick(x, y); // SetFocus(MainWindow); - // InvalidateRect(DrawWindow, NULL, FALSE); gtk_widget_queue_draw(DrawWindow); } break; case GDK_2BUTTON_PRESS: if (event->button.button == 1) /// left click { + GLOBAL_mouse_last_clicked_x = event->button.x_root; + GLOBAL_mouse_last_clicked_y = event->button.y_root; + int x = event->button.x; int y = event->button.y - 30 + gtk_adjustment_get_value(adjustment); @@ -1025,13 +700,11 @@ gboolean LD_GTK_mouse_click_hook(GtkWidget *widget, GdkEvent *event, gpointer us } else { CHANGING_PROGRAM(EditElementMouseDoubleclick(x, y)); } - // InvalidateRect(DrawWindow, NULL, FALSE); gtk_widget_queue_draw(DrawWindow); } break; } - // g_print("mo cl call end\n"); return FALSE; } @@ -1040,14 +713,8 @@ gboolean LD_GTK_mouse_scroll_hook(GtkWidget *widget, GdkEvent *event, gpointer u /* Handles: * WM_VSCROLL, WM_HSCROLL, WM_MOUSEWHEEL */ - // g_print("mo sc call\n"); + GtkAdjustment *adjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(ScrollWindow)); - // g_print("adj = %f\n", gtk_adjustment_get_value(adjustment)); - // g_print("upper = %f\n", gtk_adjustment_get_upper(adjustment) - gtk_widget_get_allocated_height (ScrollWindow)); - // g_print("lower = %f\n", gtk_adjustment_get_lower(adjustment)); - // g_print("inc = %f\n", gtk_adjustment_get_step_increment(adjustment)); - // g_print("w width = %i\n", gtk_widget_get_allocated_width (DrawWindow)); - // g_print("w height = %i\n", gtk_widget_get_allocated_height (ScrollWindow)); switch(event->scroll.direction) { @@ -1082,7 +749,6 @@ gboolean LD_GTK_mouse_scroll_hook(GtkWidget *widget, GdkEvent *event, gpointer u } gtk_widget_queue_draw(DrawWindow); - // g_print("mo sc call end\n"); return FALSE; } @@ -1091,9 +757,6 @@ gboolean LD_WM_MouseMove_call(GtkWidget *widget, GdkEvent *event, gpointer user_ /* Handles: * WM_MOUSEMOVE */ - // g_print("mo mv call\n"); - // g_print("x = %f\n", event->button.x_root); - // g_print("y = %f\n", event->button.y_root); // int x = LOWORD(lParam); // int y = HIWORD(lParam); @@ -1108,77 +771,38 @@ gboolean LD_WM_MouseMove_call(GtkWidget *widget, GdkEvent *event, gpointer user_ return FALSE; } -gboolean LD_WM_Paint_call(HWID widget, HCRDC cr, gpointer data)//(HWID widget, GdkEventExpose *event)// +gboolean LD_WM_Paint_call(HWID widget, HCRDC cr, gpointer data) { /* Handles: * WM_PAINT */ - // g_print("draw called----------------------------------\n"); static BOOL Paint_call_first = TRUE; if (Paint_call_first) { gtk_widget_override_background_color(GTK_WIDGET(widget), GTK_STATE_FLAG_NORMAL, (HBRUSH)GetStockObject(BLACK_BRUSH)); + gint width = gtk_widget_get_allocated_width (widget); gint height = gtk_widget_get_allocated_height (widget); - COLORREF col; - GtkStyleContext *context; - context = gtk_widget_get_style_context (widget); - gtk_style_context_get_color (context, - gtk_style_context_get_state (context), - &col); - gdk_cairo_set_source_rgba (cr, &col); - gtk_render_background (context, cr, 0, 0, width, height); + gtk_widget_set_size_request(widget, width, height+1); + + gdk_cairo_set_source_rgba (cr, (HBRUSH)GetStockObject(BLACK_BRUSH)); + + cairo_rectangle(cr, 0, 0, width, height); + cairo_stroke_preserve(cr); + + cairo_fill (cr); + Paint_call_first = FALSE; } /// This draws the schematic. MainWindowResized(); PaintWindow(cr); - - /* Cairo test code - guint width, height; - GdkRGBA color; - GtkStyleContext *context; - context = gtk_widget_get_style_context (widget); - - width = gtk_widget_get_allocated_width (widget); - height = gtk_widget_get_allocated_height (widget); - g_print("w = %i\n", width); - g_print("h = %i\n", height); - - SetBkColor(widget, cr, HighlightColours.bg); - - gtk_render_background (context, cr, 0, 0, width, height); - - // cairo_arc (cr, - // width / 2.0, height / 2.0, - // MIN (width, height) / 3.0, - // 0, 2 * G_PI); - - cairo_rectangle(cr, 0, 0, width, height); - cairo_stroke_preserve(cr); - - gtk_style_context_get_color (context, - gtk_style_context_get_state (context), - &color); - gdk_cairo_set_source_rgba (cr, &color); - - cairo_fill (cr); - - SetBkColor(DrawWindow, cr, InSimulationMode ? HighlightColours.simBg : - HighlightColours.bg); - SetTextColor(cr, InSimulationMode ? HighlightColours.simRungNum : - HighlightColours.rungNum); - SelectObject(cr, FixedWidthFont); - for (int xp = 0; xp<= width; xp += 7) - for (int yp = 0; yp <= height; yp += 7) - TextOut(DrawWindow, cr, xp, yp, "H", 1); - */ return FALSE; } @@ -1187,7 +811,7 @@ gboolean LD_WM_Destroy_call(GtkWidget *widget, GdkEvent *event, gpointer user_da /* Handles: * WM_DESTROY */ - // g_print("dis call\n"); + // if(CheckSaveUserCancels()) break; // PostQuitMessage(0); @@ -1201,9 +825,15 @@ gboolean LD_WM_Size_call(GtkWidget *widget, GdkEvent *event, gpointer user_data) /* Handles: * WM_SIZE */ - // g_print("size call\n"); MainWindowResized(); - // g_print("size call end\n"); + return FALSE; +} + +gboolean LD_WM_Command_call(GtkMenuItem* men, gpointer gpcode) +{ + int tempcode = GPOINTER_TO_INT(gpcode); + ProcessMenu (tempcode); + return FALSE; } @@ -1212,12 +842,252 @@ gboolean LD_WM_SetFocus_call(GtkWidget *widget, GdkEvent *event, gpointer user_d /* Handles: * WM_SETFOCUS */ - // g_print("focus call\n"); + InvalidateRect(DrawWindow, NULL, FALSE); - // g_print("focus call end\n"); + return FALSE; } +void LD_WM_Notify_Row_Activate_call(GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data) +{ + /* Handles: + * WM_NOTIFY + */ + + g_print("Row activated!\n"); + + int *ip = gtk_tree_path_get_indices ( path ); + + NMHDR h; + h.code = LVN_ITEMACTIVATE; + h.item.iItem = ip[0]; + h.hlistFrom = IoList; + + IoListProc(&h); +} + +void LD_WM_Notify_Cursor_Change_call(GtkTreeView *tree_view, gpointer user_data) +{ + /* Handles: + * WM_NOTIFY + */ + + ITLIST iter; + + BOOL empty = !gtk_tree_model_get_iter_first (IoList, &iter); + // g_print("empty = %i\n", (empty == TRUE) ); + + HLIST pTreeModel; + int *ip; + GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view)); + gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE); + + if(gtk_tree_selection_get_selected (selection, &pTreeModel, &iter)) + { + GtkTreePath *path = gtk_tree_model_get_path ( pTreeModel , &iter ) ; + ip = gtk_tree_path_get_indices ( path ); + } + else + gtk_tree_model_get_iter_first (IoList, &iter); + + NMHDR h; + h.code = LVN_GETDISPINFO; + h.item.iItem = (ip == NULL) ? 0 : ip[0]; + h.hlistFrom = IoList; + h.hlistIter = &iter; + + IoListProc(&h); +} + +inline void MenuHandler () +{ + g_signal_connect(G_OBJECT(NewMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_NEW)); + + g_signal_connect(G_OBJECT(OpenMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_OPEN)); + + g_signal_connect(G_OBJECT(SaveMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_SAVE)); + + g_signal_connect(G_OBJECT(SaveAsMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_SAVE_AS)); + + g_signal_connect(G_OBJECT(ExportMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_EXPORT)); + + g_signal_connect(G_OBJECT(ExitMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_EXIT)); + + g_signal_connect(G_OBJECT(InsertCommentMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_COMMENT)); + + g_signal_connect(G_OBJECT(InsertContactsMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_CONTACTS)); + + g_signal_connect(G_OBJECT(InsertCoilMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_COIL)); + + g_signal_connect(G_OBJECT(InsertTonMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_TON)); + + g_signal_connect(G_OBJECT(InsertTofMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_TOF)); + + g_signal_connect(G_OBJECT(InsertRtoMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_RTO)); + + g_signal_connect(G_OBJECT(InsertCtuMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_CTU)); + + g_signal_connect(G_OBJECT(InsertCtdMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_CTD)); + + g_signal_connect(G_OBJECT(InsertCtcMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_CTC)); + + g_signal_connect(G_OBJECT(InsertResMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_RES)); + + g_signal_connect(G_OBJECT(InsertOpenMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_OPEN)); + + g_signal_connect(G_OBJECT(InsertShortMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_SHORT)); + + g_signal_connect(G_OBJECT(InsertMasterRlyMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_MASTER_RLY)); + + g_signal_connect(G_OBJECT(InsertShiftRegMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_SHIFT_REG)); + + g_signal_connect(G_OBJECT(InsertLutMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_LUT)); + + g_signal_connect(G_OBJECT(InsertPwlMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_PWL)); + + g_signal_connect(G_OBJECT(InsertFmtdStrMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_FMTD_STR)); + + g_signal_connect(G_OBJECT(InsertOsrMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_OSR)); + + g_signal_connect(G_OBJECT(InsertOsfMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_OSF)); + + g_signal_connect(G_OBJECT(InsertMovMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_MOV)); + + g_signal_connect(G_OBJECT(InsertSetPwmMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_SET_PWM)); + + g_signal_connect(G_OBJECT(InsertReadAdcMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_READ_ADC)); + + g_signal_connect(G_OBJECT(InsertUartSendMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_UART_SEND)); + + g_signal_connect(G_OBJECT(InsertUartRecvMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_UART_RECV)); + + g_signal_connect(G_OBJECT(InsertPersistMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_PERSIST)); + + g_signal_connect(G_OBJECT(InsertAddMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_ADD)); + + g_signal_connect(G_OBJECT(InsertSubMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_SUB)); + + g_signal_connect(G_OBJECT(InsertMulMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_MUL)); + + g_signal_connect(G_OBJECT(InsertDivMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_DIV)); + + g_signal_connect(G_OBJECT(InsertEquMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_EQU)); + + g_signal_connect(G_OBJECT(InsertNeqMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_NEQ)); + + g_signal_connect(G_OBJECT(InsertGrtMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_GRT)); + + g_signal_connect(G_OBJECT(InsertGeqMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_GEQ)); + + g_signal_connect(G_OBJECT(InsertLesMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_LES)); + + g_signal_connect(G_OBJECT(InsertLeqMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_LEQ)); + + g_signal_connect(G_OBJECT(MakeNormalMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_MAKE_NORMAL)); + + g_signal_connect(G_OBJECT(NegateMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_NEGATE)); + + g_signal_connect(G_OBJECT(MakeSetOnlyMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_MAKE_SET_ONLY)); + + g_signal_connect(G_OBJECT(MakeResetOnlyMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_MAKE_RESET_ONLY)); + + g_signal_connect(G_OBJECT(UndoMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_UNDO)); + + g_signal_connect(G_OBJECT(RedoMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_REDO)); + + g_signal_connect(G_OBJECT(InsertRungBeforeMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_RUNG_BEFORE)); + + g_signal_connect(G_OBJECT(InsertRungAfterMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_RUNG_AFTER)); + + g_signal_connect(G_OBJECT(DeleteRungMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_DELETE_RUNG)); + + g_signal_connect(G_OBJECT(PushRungUpMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_PUSH_RUNG_UP)); + + g_signal_connect(G_OBJECT(PushRungDownMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_PUSH_RUNG_DOWN)); + + g_signal_connect(G_OBJECT(DeleteElementMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_DELETE_ELEMENT)); + + g_signal_connect(G_OBJECT(McuSettingsMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_MCU_SETTINGS)); + + g_signal_connect(G_OBJECT(SimulationModeMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_SIMULATION_MODE)); + + g_signal_connect(G_OBJECT(StartSimulationMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_START_SIMULATION)); + + g_signal_connect(G_OBJECT(StopSimulationMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_STOP_SIMULATION)); + + g_signal_connect(G_OBJECT(SingleCycleMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_SINGLE_CYCLE)); + + g_signal_connect(G_OBJECT(CompileMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_COMPILE)); + + g_signal_connect(G_OBJECT(CompileAsMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_COMPILE_AS)); + + g_signal_connect(G_OBJECT(ManualMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_MANUAL)); + + g_signal_connect(G_OBJECT(AboutMenu), "activate", + G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_ABOUT)); +} + //----------------------------------------------------------------------------- // Entry point into the program. //----------------------------------------------------------------------------- @@ -1232,12 +1102,6 @@ int main(int argc, char** argv) char *err = "Bad command line arguments: run 'ldmicro /c src.ld dest.hex'"; - // if (argc < 4) - // { - // Error(err); - // exit(-1); - // } - char *source = (char*)malloc(strlen(argv[2]) + strlen(argv[3]) + 2); sprintf(source, "%s %s", argv[2], argv[3]); @@ -1292,33 +1156,6 @@ int main(int argc, char** argv) gtk_init(&argc, &argv); Instance = NULL; - /* TEST - MainWindow=gtk_window_new(GTK_WINDOW_TOPLEVEL); - gtk_window_set_title(GTK_WINDOW(MainWindow), "LDMicro"); - g_signal_connect (MainWindow, "delete_event", G_CALLBACK (LDMicro_close), NULL); - gtk_window_set_default_size (GTK_WINDOW (MainWindow), 600, 400); - gtk_window_resize (GTK_WINDOW (MainWindow), 600, 400); - - ThawWindowPos(MainWindow); - ThawDWORD(IoListHeight); - - - // Title bar - UpdateMainWindowTitleBar(); - - // Splitting the window - MakeMainWindowControls(); - - // Calling the Simulation functions - - // StartSimulation(); // test - // SetMenusEnabled(true, true, false, - // true, false, false, false, - // true, true, true); // test - // ToggleSimulationMode(); //test - // GenerateIoListDontLoseSelection(); - StopSimulation(); //Test - */ MainHeap = HeapCreate(0, 1024*64, 0); @@ -1329,7 +1166,7 @@ int main(int argc, char** argv) gtk_window_resize (GTK_WINDOW(MainWindow), 800, 600); gtk_window_move(GTK_WINDOW(MainWindow), 10, 10); gtk_widget_override_background_color(GTK_WIDGET(MainWindow), - GTK_STATE_FLAG_NORMAL, ((HBRUSH)GetStockObject(GRAY_BRUSH))->getThis()); + GTK_STATE_FLAG_NORMAL, ((HBRUSH)GetStockObject(DKGRAY_BRUSH))); gtk_window_set_default_icon(LoadImage(Instance, LDMICRO_ICON, IMAGE_ICON, 32, 32, 0)); gtk_window_set_icon(GTK_WINDOW(MainWindow), LoadImage(Instance, LDMICRO_ICON, @@ -1346,21 +1183,19 @@ int main(int argc, char** argv) MakeMainWindowControls(); /// takes care of MakeMainWindowMenus() MainWindowResized(); - // CHANGING_PROGRAM(ShowConfDialog()); - MenuHandler(); - // CHANGING_PROGRAM(AddLookUpTable()); - // CHANGING_PROGRAM(EditSelectedElement()); /// Keyboard and mouse hooks equivalent to MainWndProc g_signal_connect (MainWindow, "delete_event", G_CALLBACK (LD_WM_Close_call), NULL); - // g_signal_connect (MainWindow, "key_press_event", G_CALLBACK (LD_WM_KeyDown_call), NULL); g_signal_connect (MainWindow, "button_press_event", G_CALLBACK (LD_GTK_mouse_click_hook), NULL); g_signal_connect (MainWindow, "scroll_event", G_CALLBACK (LD_GTK_mouse_scroll_hook), NULL); g_signal_connect (MainWindow, "motion_notify_event", G_CALLBACK (LD_WM_MouseMove_call), NULL); g_signal_connect (DrawWindow, "draw", G_CALLBACK (LD_WM_Paint_call), NULL); g_signal_connect (MainWindow, "destroy_event", G_CALLBACK (LD_WM_Destroy_call), NULL); g_signal_connect (MainWindow, "configure_event", G_CALLBACK (LD_WM_Size_call), NULL); - // g_signal_connect (MainWindow, "focus_in_event", G_CALLBACK (LD_WM_SetFocus_call), NULL); + g_signal_connect (MainWindow, "focus_in_event", G_CALLBACK (LD_WM_SetFocus_call), NULL); + g_signal_connect (view, "row_activated", G_CALLBACK (LD_WM_Notify_Row_Activate_call), NULL); + g_signal_connect (view, "cursor_changed", G_CALLBACK (LD_WM_Notify_Cursor_Change_call), NULL); + MenuHandler(); /// Keyboard and mouse hooks equivalent to MainWndProc - end NewProgram(); @@ -1373,10 +1208,10 @@ int main(int argc, char** argv) gtk_widget_show_all(MainWindow); /// Blink cursor - g_timeout_add(200, (GSourceFunc)BlinkCursor, DrawWindow); + SetTimer(DrawWindow, TIMER_BLINK_CURSOR, 200, BlinkCursor); + // SetTimer(MainWindow, TIMER_BLINK_CURSOR, 800, BlinkCursor); if(argc >= 2) { - // g_print("load prog: %s\n", argv[1]); char line[MAX_PATH]; if(*argv[1] == '"') { strcpy(line, argv[1]+1); @@ -1386,7 +1221,6 @@ int main(int argc, char** argv) if(strchr(line, '"')) *strchr(line, '"') = '\0'; realpath(line, CurrentSaveFile); - // g_print("resolved path: %s\n", CurrentSaveFile); if(!LoadProjectFromFile(CurrentSaveFile)) { NewProgram(); Error(_("Couldn't open '%s'."), CurrentSaveFile); @@ -1395,9 +1229,9 @@ int main(int argc, char** argv) UndoFlush(); } - GenerateIoListDontLoseSelection(); //~ - // RefreshScrollbars(); - UpdateMainWindowTitleBar(); //~ + GenerateIoListDontLoseSelection(); + RefreshScrollbars(); + UpdateMainWindowTitleBar(); // MSG msg; // DWORD ret; diff --git a/ldmicro/lib/freezeLD/freezeLD.cpp b/ldmicro/lib/freezeLD/freezeLD.cpp index b9cafb2..75bbb30 100644 --- a/ldmicro/lib/freezeLD/freezeLD.cpp +++ b/ldmicro/lib/freezeLD/freezeLD.cpp @@ -18,7 +18,6 @@ */ void FreezeWindowPosF(HWID hwid, char *subKey, char *name) { - //g_print("freezing"); char* Ld_CWD = (char *)malloc(MAX_PATH); getcwd(Ld_CWD, MAX_PATH); @@ -52,7 +51,7 @@ void FreezeWindowPosF(HWID hwid, char *subKey, char *name) 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()) @@ -67,7 +66,6 @@ void FreezeWindowPosF(HWID hwid, char *subKey, char *name) 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()) @@ -82,7 +80,6 @@ void FreezeWindowPosF(HWID hwid, char *subKey, char *name) 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()) @@ -97,7 +94,6 @@ void FreezeWindowPosF(HWID hwid, char *subKey, char *name) 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()) @@ -112,7 +108,6 @@ void FreezeWindowPosF(HWID hwid, char *subKey, char *name) 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()) @@ -129,7 +124,6 @@ void FreezeWindowPosF(HWID hwid, char *subKey, char *name) free(keyName); chdir(Ld_CWD); free(Ld_CWD); - //g_print("freezed"); } static void Clamp(LONG *v, LONG min, LONG max) diff --git a/ldmicro/lib/linuxUI/linuxLD.h b/ldmicro/lib/linuxUI/linuxLD.h index 80f4471..28116b2 100644 --- a/ldmicro/lib/linuxUI/linuxLD.h +++ b/ldmicro/lib/linuxUI/linuxLD.h @@ -68,14 +68,16 @@ typedef HANDLE HGDIOBJ; typedef cairo_t *HCRDC; typedef GtkWidget *HWID; -typedef GtkWidget *HMENU; typedef GtkWindow *HWND; -typedef GtkListStore *HLIST; +typedef GtkTreeModel *HLIST; +typedef GtkTreeIter ITLIST; typedef GtkApplication *HAPP; typedef GtkTreeViewColumn *HTVC; typedef GdkPixbuf *HICON; typedef GdkRectangle GDRECT; -typedef GdkRectangle *PGDRECT; +typedef GDRECT *PGDRECT; +typedef HWID HMENU; +typedef ITLIST *HITLIST; /// Check if system is x64 or x86 #if defined(__UNIX64) @@ -122,10 +124,6 @@ typedef class tagColorReferance: public GdkRGBA{ return false; } - GdkRGBA* getThis() - { - return this; - } } COLORREF, *HBRUSH; /// Structures @@ -144,19 +142,38 @@ typedef struct HeapRecordTag{ } HEAPRECORD; typedef struct tagSCROLLINFO { - UINT cbSize; - UINT fMask; - int nMin; - int nMax; - UINT nPage; - int nPos; - int nTrackPos; + UINT cbSize; + UINT fMask; + int nMin; + int nMax; + UINT nPage; + int nPos; + int nTrackPos; } SCROLLINFO, *LPCSCROLLINFO; +typedef struct { + UINT mask; + int iItem; + int iSubItem; +// UINT state; +// UINT stateMask; + LPTSTR pszText; +// int cchTextMax; +// int iImage; +// LPARAM lParam; +// int iIndent; +// int iGroupId; +// UINT cColumns; +// PUINT puColumns; +// int *piColFmt; +// int iGroup; +} LVITEM, *LPLVITEM; + typedef struct tagNMHDR { - HWND hwndFrom; - UINT_PTR idFrom; - UINT code; + HLIST hlistFrom; + HITLIST hlistIter; + UINT code; + LVITEM item; } NMHDR; typedef struct FontTag { diff --git a/ldmicro/lib/linuxUI/linuxUI.cpp b/ldmicro/lib/linuxUI/linuxUI.cpp index 9fec264..b57d4d0 100644 --- a/ldmicro/lib/linuxUI/linuxUI.cpp +++ b/ldmicro/lib/linuxUI/linuxUI.cpp @@ -1,5 +1,9 @@ #include "linuxUI.h" +/// Global variables to hole mouse click positions +int GLOBAL_mouse_last_clicked_x; +int GLOBAL_mouse_last_clicked_y; + /// Brushes const COLORREF BLACK_BR(0, 0, 0); const COLORREF WHITE_BR(255, 255, 255); @@ -10,6 +14,9 @@ const COLORREF DKGRAY_BR(169, 169, 169); /// Variable to current text color COLORREF HdcCurrentTextColor; +/// Variable to hold timers +std::vector<TimerRecord> timerRecords; + /// EnableMenuItem Variables const UINT MF_ENABLED = 0; const UINT MF_GRAYED = 1; @@ -96,21 +103,87 @@ int MessageBox(HWID pWindow, char* message, char* title, UINT mFlags) BOOL GetSaveFileName(OPENFILENAME *ofn) { GtkWidget *dialog; - GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN; + GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_SAVE; dialog = gtk_file_chooser_dialog_new (ofn->lpstrTitle, GTK_WINDOW(ofn->parentWindow), action, "_Cancel", GTK_RESPONSE_CANCEL, - "_Open", + "_Save", GTK_RESPONSE_ACCEPT, NULL); - //g_print("filter created\n"); + char filename[15] = "Untitled"; + + if (ofn->lpstrDefExt != NULL) + sprintf(filename, "Untitled.%s", ofn->lpstrDefExt); - if (ofn->Flags & OFN_OVERWRITEPROMPT == OFN_OVERWRITEPROMPT) + gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER(dialog), filename); + + if (ofn->Flags & 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; + filterResetFlag = FALSE; + } + else + { + gtk_file_filter_set_name (GTK_FILE_FILTER(filter), strFilter); + strFilterLen = 0; + filterResetFlag = TRUE; + } + } + + sprintf(strFilter, "*.%s", ofn->lpstrDefExt); + gtk_file_filter_add_pattern (GTK_FILE_FILTER(filter), strFilter); + gtk_file_chooser_set_filter (GTK_FILE_CHOOSER(dialog), filter); + + delete strFilter; + 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); + } + + gtk_widget_destroy (dialog); + + return exitStatus; +} + +BOOL GetOpenFileName(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); + GtkFileFilter *filter = gtk_file_filter_new (); char* strFilter = new char[strlen(ofn->lpstrFilter)]; DWORD strFilterLen = 0; @@ -127,19 +200,15 @@ BOOL GetSaveFileName(OPENFILENAME *ofn) 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); @@ -148,8 +217,6 @@ BOOL GetSaveFileName(OPENFILENAME *ofn) delete strFilter; - //g_print("default filter set\n"); - BOOL exitStatus = gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT; if (exitStatus) { @@ -158,15 +225,13 @@ BOOL GetSaveFileName(OPENFILENAME *ofn) 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){ @@ -231,8 +296,6 @@ void SelectObject(HCRDC hcr, HFONT hfont) // cairo_set_font_matrix (hcr, &matrix); - // g_print("wR = %f\nhR = %f\n", (double)hfont->nWidth / extents.width, (double)hfont->nHeight / extents.height); - // g_print("tW = %f tH = %f\n", extents.width, extents.width); cairo_set_font_size(hcr, 10); } @@ -263,11 +326,11 @@ HFONT CreateFont(int nHeight, int nWidth, int nOrientation, int fnWeight, void SetBkColor(HWID widget, HCRDC hcr, COLORREF bkCol) { - // gtk_widget_override_background_color(GTK_WIDGET(widget), - // GTK_STATE_FLAG_NORMAL, &bkCol); + gtk_widget_override_background_color(GTK_WIDGET(widget), + GTK_STATE_FLAG_NORMAL, &bkCol); - // gint width = gtk_widget_get_allocated_width (widget); - // gint height = gtk_widget_get_allocated_height (widget); + gint width = gtk_widget_get_allocated_width (widget); + gint height = gtk_widget_get_allocated_height (widget); // COLORREF col; // GtkStyleContext *context; @@ -277,10 +340,11 @@ void SetBkColor(HWID widget, HCRDC hcr, COLORREF bkCol) // gtk_style_context_get_color (context, // gtk_style_context_get_state (context), // &col); - - // gdk_cairo_set_source_rgba (hcr, &col); + gdk_cairo_set_source_rgba (hcr, &bkCol); + // cairo_rectangle(hcr, 0, 0, width, height); + // cairo_stroke_preserve(hcr); - // cairo_fill (hcr); + cairo_fill (hcr); } void SetTextColor(HCRDC hcr, COLORREF color) @@ -298,24 +362,22 @@ void TextOut(HWID hWid, HCRDC hcr, int nXStart, int nYStart, LPCTSTR lpString, i int width = gtk_widget_get_allocated_width (hWid); int height= gtk_widget_get_allocated_height (hWid); BOOL resize_flag = FALSE; - // g_print("w = %f h = %f") if(nYStart+(extents.height/2.0) >= height) { - // g_print("Y extend\n"); height += extents.height + 50; resize_flag = TRUE; } if (nXStart+(extents.width/2.0) >= width) { - // g_print("X extend\n"); width += extents.width; resize_flag = TRUE; } if (resize_flag) gtk_widget_set_size_request(hWid, width, height); + char* text = (char*)malloc(cchString); strncpy(text, lpString, cchString); text[cchString] = '\0'; @@ -323,8 +385,6 @@ void TextOut(HWID hWid, HCRDC hcr, int nXStart, int nYStart, LPCTSTR lpString, i cairo_move_to(hcr, nXStart, nYStart); cairo_show_text(hcr, text); - // g_print("%s", text); - cairo_fill (hcr); } @@ -341,10 +401,7 @@ COLORREF GetTextColor(HCRDC Hdc) BOOL InvalidateRect(HWID hWid, const RECT *lpRect, BOOL bErase) { if(!GDK_IS_WINDOW(hWid)) - { - // g_print("not window\n"); return FALSE; - } if (lpRect == NULL) { @@ -425,3 +482,32 @@ BOOL GetWindowRect(HWID hWid, PRECT pRect) void DestroyWindow (GtkWidget* widget, gpointer data){ gtk_widget_destroy (widget); } +UINT SetTimer(HWID hWid, UINT nIDEvent, UINT uElapse, BOOL (*lpTimerFunc)(BOOL) ) +{ + auto record_it = std::find_if(timerRecords.begin(), timerRecords.end(), [&nIDEvent](TimerRecord &Record) { return Record.ufID == nIDEvent; }); + + if (record_it != timerRecords.end()) + return 0; + + TimerRecord tr; + tr.pfun = lpTimerFunc; + tr.ufID = nIDEvent; + tr.utID = g_timeout_add(uElapse, (GSourceFunc)lpTimerFunc, FALSE); + + timerRecords.push_back(tr); + return tr.utID; +} + +BOOL KillTimer(HWID hWid, UINT uIDEvent) +{ + auto record_it = std::find_if(timerRecords.begin(), timerRecords.end(), [&uIDEvent](TimerRecord &Record) { return Record.ufID == uIDEvent; }); + + if (record_it == timerRecords.end()) + return FALSE; + + record_it->pfun(TRUE); + g_source_remove (record_it->utID); + timerRecords.erase(record_it); + + return TRUE; +} diff --git a/ldmicro/lib/linuxUI/linuxUI.h b/ldmicro/lib/linuxUI/linuxUI.h index f5d7fa7..41b281f 100644 --- a/ldmicro/lib/linuxUI/linuxUI.h +++ b/ldmicro/lib/linuxUI/linuxUI.h @@ -34,24 +34,28 @@ #define MB_ICONINFORMATION 0x00000080L /// Scroll -#define SB_LINEUP 0x00000001 -#define SB_PAGEUP 0x00000002 -#define SB_LINEDOWN 0x00000004 -#define SB_PAGEDOWN 0x00000008 -#define SB_TOP 0x00000010 -#define SB_BOTTOM 0x00000020 -#define SB_THUMBTRACK 0x00000040 -#define SB_THUMBPOSITION 0x00000040 - -/// open/save file -#define OFN_PATHMUSTEXIST 0x00000100L -#define OFN_HIDEREADONLY 0x00000200L -#define OFN_OVERWRITEPROMPT 0x00000400L +#define SB_LINEUP 0x00000001 +#define SB_PAGEUP 0x00000002 +#define SB_LINEDOWN 0x00000004 +#define SB_PAGEDOWN 0x00000008 +#define SB_TOP 0x00000010 +#define SB_BOTTOM 0x00000020 +#define SB_THUMBTRACK 0x00000040 +#define SB_THUMBPOSITION 0x00000080 + +/// List view flags +#define LVN_ITEMACTIVATE 0x00000001 +#define LVN_GETDISPINFO 0x00000002 + +/// Open/save file +#define OFN_PATHMUSTEXIST 0x00000001L +#define OFN_HIDEREADONLY 0x00000002L +#define OFN_OVERWRITEPROMPT 0x00000004L /// PatBlt paint flags #define PATINVERT 0x00000100L -/// window brushes +/// Window brushes #define BS_SOLID 0x00000001L #define BS_HOLLOW 0x00000002L #define BLACK_BRUSH 0x00000004L @@ -96,9 +100,17 @@ typedef struct OpenFileInfoData { LPCTSTR lpstrDefExt; } OPENFILENAME; +typedef struct TimerRecordTag { + BOOL (*pfun)(BOOL); + UINT ufID; + UINT utID; +} TimerRecord; + /// Variables extern COLORREF HdcCurrentTextColor; - +extern std::vector<TimerRecord> timerRecords; +extern int GLOBAL_mouse_last_clicked_x; +extern int GLOBAL_mouse_last_clicked_y; /// functions BOOL GetFocus(HWID window); @@ -115,6 +127,8 @@ int MessageBox( BOOL GetSaveFileName(OPENFILENAME *ofn); +BOOL GetOpenFileName(OPENFILENAME *ofn); + void EnableMenuItem( HMENU MenuName, HMENU MenuItem, @@ -199,4 +213,14 @@ BOOL GetWindowRect( HWID hWid, PRECT pRect); +UINT SetTimer( + HWID hWid, + UINT nIDEvent, + UINT uElapse, + BOOL (*lpTimerFunc)(BOOL)); + +BOOL KillTimer( + HWID hWid, + UINT uIDEvent); + #endif
\ No newline at end of file diff --git a/ldmicro/maincontrols.cpp b/ldmicro/maincontrols.cpp index cd01ea9..9e3770d 100644 --- a/ldmicro/maincontrols.cpp +++ b/ldmicro/maincontrols.cpp @@ -34,7 +34,7 @@ HMENU NewMenu; HMENU OpenMenu; HMENU SaveMenu; -HMENU SaveAsMenu_AS; +HMENU SaveAsMenu; HMENU ExportMenu; HMENU ExitMenu; @@ -110,7 +110,7 @@ int ScrollHeight; BOOL NeedHoriz; // status bar at the bottom of the screen, to display settings -static HMENU StatusBar; +static HMENU StatusBar[3]; // have to get back to the menus to gray/ungray, check/uncheck things static HMENU FileMenu; @@ -131,7 +131,6 @@ static int IoListSelectionPoint; static BOOL IoListOutOfSync; int IoListHeight; int IoListTop; -GtkTreeIter* iter = new GtkTreeIter; GtkTreeModel **IoListPtr = (GtkTreeModel**)GTK_TREE_MODEL (IoList); // whether the simulation is running in real time @@ -166,65 +165,65 @@ void AddMenuAccelerators (void){ gtk_widget_add_accelerator (PushRungDownMenu, "activate", AccelGroup, GDK_KEY_downarrow, GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (DeleteElementMenu, "activate", AccelGroup, GDK_KEY_Delete, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (DeleteRungMenu, "activate", AccelGroup, GDK_KEY_Delete, GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE); // Creating keyboard shortcuts for Instructions menu gtk_widget_add_accelerator (InsertCommentMenu, "activate", AccelGroup, GDK_KEY_semicolon, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (InsertContactsMenu, "activate", AccelGroup, GDK_KEY_C, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (InsertOsrMenu, "activate", AccelGroup, GDK_KEY_backslash, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (InsertOsfMenu, "activate", AccelGroup, GDK_KEY_slash, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (InsertTonMenu, "activate", AccelGroup, GDK_KEY_O, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (InsertTofMenu, "activate", AccelGroup, GDK_KEY_F, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (InsertRtoMenu, "activate", AccelGroup, GDK_KEY_T, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (InsertCtuMenu, "activate", AccelGroup, GDK_KEY_U, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (InsertCtdMenu, "activate", AccelGroup, GDK_KEY_I, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (InsertCtcMenu, "activate", AccelGroup, GDK_KEY_J, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (InsertEquMenu, "activate", AccelGroup, GDK_KEY_equal, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (InsertGrtMenu, "activate", AccelGroup, GDK_KEY_greater, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (InsertGeqMenu, "activate", AccelGroup, GDK_KEY_Stop, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (InsertLesMenu, "activate", AccelGroup, GDK_KEY_less, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (InsertLeqMenu, "activate", AccelGroup, GDK_KEY_comma, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (InsertCoilMenu, "activate", AccelGroup, GDK_KEY_L, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (InsertResMenu, "activate", AccelGroup, GDK_KEY_E, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (InsertMovMenu, "activate", AccelGroup, GDK_KEY_M, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (InsertAddMenu, "activate", AccelGroup, GDK_KEY_plus, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (InsertSubMenu, "activate", AccelGroup, GDK_KEY_minus, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (InsertMulMenu, "activate", AccelGroup, GDK_KEY_multiply, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (InsertDivMenu, "activate", AccelGroup, GDK_KEY_D, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (InsertReadAdcMenu, "activate", AccelGroup, GDK_KEY_P, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (MakeNormalMenu, "activate", AccelGroup, GDK_KEY_A, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (NegateMenu, "activate", AccelGroup, GDK_KEY_N, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (MakeSetOnlyMenu, "activate", AccelGroup, GDK_KEY_S, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (MakeResetOnlyMenu, "activate", AccelGroup, GDK_KEY_R, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); // Creating keyboard shortcuts for Simulation menu gtk_widget_add_accelerator (SimulationModeMenu, "activate", AccelGroup, GDK_KEY_M, @@ -234,15 +233,15 @@ void AddMenuAccelerators (void){ gtk_widget_add_accelerator (StopSimulationMenu, "activate", AccelGroup, GDK_KEY_H, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE); gtk_widget_add_accelerator (SingleCycleMenu, "activate", AccelGroup, GDK_KEY_space, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); // Creating keyboard shortcuts for Compile menu gtk_widget_add_accelerator (CompileMenu, "activate", AccelGroup, GDK_KEY_F5, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); // Creating keyboard shortcuts for Help menu gtk_widget_add_accelerator (ManualMenu, "activate", AccelGroup, GDK_KEY_F1, - GDK_RELEASE_MASK, GTK_ACCEL_VISIBLE); + (GdkModifierType)0, GTK_ACCEL_VISIBLE); gtk_window_add_accel_group (GTK_WINDOW (MainWindow), AccelGroup); @@ -298,7 +297,7 @@ HMENU MakeMainWindowMenus(void) NewMenu = gtk_menu_item_new_with_mnemonic("_New"); OpenMenu = gtk_menu_item_new_with_mnemonic("_Open"); SaveMenu = gtk_menu_item_new_with_mnemonic("_Save"); - SaveAsMenu_AS = gtk_menu_item_new_with_mnemonic("_Save As"); + SaveAsMenu = gtk_menu_item_new_with_mnemonic("_Save As"); ExportMenu = gtk_menu_item_new_with_mnemonic("_Export As Text"); ExitMenu = gtk_menu_item_new_with_mnemonic("_Exit"); @@ -306,7 +305,7 @@ HMENU MakeMainWindowMenus(void) gtk_menu_shell_append(GTK_MENU_SHELL (FileMenu), NewMenu); // Appending menu items gtk_menu_shell_append(GTK_MENU_SHELL (FileMenu), OpenMenu); gtk_menu_shell_append(GTK_MENU_SHELL (FileMenu), SaveMenu); - gtk_menu_shell_append(GTK_MENU_SHELL (FileMenu), SaveAsMenu_AS); + gtk_menu_shell_append(GTK_MENU_SHELL (FileMenu), SaveAsMenu); FileMenuSeparator = gtk_separator_menu_item_new(); gtk_menu_shell_append(GTK_MENU_SHELL (FileMenu), FileMenuSeparator); gtk_menu_shell_append(GTK_MENU_SHELL (FileMenu), ExportMenu); @@ -347,12 +346,15 @@ HMENU MakeMainWindowMenus(void) gtk_menu_shell_append (GTK_MENU_SHELL (Settings), MicroControllerMenu); // Appending the microcontroller names to "Microcontroller" item + GSList* mcuList = NULL; for (i = 0; i < NUM_SUPPORTED_MCUS; i++){ - ProcessorMenuItems[i] = gtk_check_menu_item_new_with_label (SupportedMcus[i].mcuName); + ProcessorMenuItems[i] = gtk_radio_menu_item_new_with_label (mcuList, SupportedMcus[i].mcuName); + mcuList = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (ProcessorMenuItems[i])); gtk_menu_shell_append (GTK_MENU_SHELL (ProcessorMenu), ProcessorMenuItems[i]); } - ProcessorMenuItems[NUM_SUPPORTED_MCUS] = gtk_check_menu_item_new_with_label ("(no microcontroller)"); + ProcessorMenuItems[NUM_SUPPORTED_MCUS] = gtk_radio_menu_item_new_with_label (mcuList, "(no microcontroller)"); + mcuList = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (ProcessorMenuItems[NUM_SUPPORTED_MCUS])); gtk_menu_shell_append (GTK_MENU_SHELL (ProcessorMenu), ProcessorMenuItems[NUM_SUPPORTED_MCUS]); gtk_menu_item_set_submenu(GTK_MENU_ITEM(MicroControllerMenu), ProcessorMenu); @@ -526,7 +528,7 @@ void MakeMainWindowControls(void) /// Pane to separate Scrolled Window and other widgets HWID pane = gtk_paned_new (GTK_ORIENTATION_VERTICAL); - IoList = gtk_list_store_new (5, + IoList = (GtkTreeModel*)gtk_list_store_new (5, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, @@ -592,22 +594,62 @@ void MakeMainWindowControls(void) gtk_container_add (GTK_CONTAINER(ViewPortMenu), DrawWindow); gtk_container_add (GTK_CONTAINER(ScrollWindow), ViewPortMenu); gtk_paned_pack1 (GTK_PANED (pane), ScrollWindow, TRUE, TRUE); - gtk_paned_set_position (GTK_PANED (pane), 0); + gtk_paned_set_position (GTK_PANED (pane), 0); + + /// Appending tree view to scrolled window + HWID ViewScroll = gtk_scrolled_window_new (NULL, NULL); + gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (ViewScroll), + GTK_POLICY_AUTOMATIC, + GTK_POLICY_ALWAYS); + gtk_widget_set_hexpand(GTK_WIDGET(ViewScroll), TRUE); + gtk_widget_set_vexpand(GTK_WIDGET(ViewScroll), TRUE); + + gtk_container_add (GTK_CONTAINER(ViewScroll), view); /// Appending tree view to pane and pane to grid - gtk_paned_pack2 (GTK_PANED(pane), view, FALSE, FALSE); + gtk_paned_pack2 (GTK_PANED(pane), ViewScroll, FALSE, FALSE); gtk_paned_set_position (GTK_PANED (pane), 400); gtk_grid_attach (GTK_GRID (grid), pane, 0, 0, 1, 1); + + gtk_box_pack_start(GTK_BOX(PackBoxMenu), grid, FALSE, TRUE, 0); + + /// Grid for status bars + HWID StatusGrid = gtk_grid_new(); - /// Creating Status Bar and attaching to grid - StatusBar = gtk_statusbar_new(); - gtk_statusbar_push (GTK_STATUSBAR (StatusBar), - gtk_statusbar_get_context_id (GTK_STATUSBAR (StatusBar), + /// Creating Status Bar 1 and attaching to grid + StatusBar[0] = gtk_statusbar_new(); + + gtk_statusbar_push (GTK_STATUSBAR (StatusBar[0]), + gtk_statusbar_get_context_id (GTK_STATUSBAR (StatusBar[0]), "Introduction"), "LDMicro Started"); - /// Appneding Status Bar to box which is then added to Main Window - gtk_box_pack_start(GTK_BOX(PackBoxMenu), grid, FALSE, TRUE, 0); - gtk_box_pack_start(GTK_BOX(PackBoxMenu), StatusBar, FALSE, FALSE, 0); + /// Appneding Status Bar 1 to the status grid + gtk_grid_attach (GTK_GRID (StatusGrid), StatusBar[0], 0, 0, 1, 1); + + /// Creating Status Bar 2 and attaching to grid + StatusBar[1] = gtk_statusbar_new(); + + gtk_statusbar_push (GTK_STATUSBAR (StatusBar[1]), + gtk_statusbar_get_context_id (GTK_STATUSBAR (StatusBar[1]), + "Introduction"), "LDMicro Started"); + + /// Appneding Status Bar 2 to the status box + gtk_grid_attach (GTK_GRID (StatusGrid), StatusBar[1], 1, 0, 1, 1); + + /// Creating Status Bar 3 and attaching to grid + StatusBar[2] = gtk_statusbar_new(); + + gtk_statusbar_push (GTK_STATUSBAR (StatusBar[2]), + gtk_statusbar_get_context_id (GTK_STATUSBAR (StatusBar[2]), + "Introduction"), "LDMicro Started"); + + /// Appneding Status Bar 3 to the status box + gtk_grid_attach (GTK_GRID (StatusGrid), StatusBar[2], 2, 0, 1, 1); + + /// Attach status grid to box + gtk_box_pack_start(GTK_BOX(PackBoxMenu), StatusGrid, FALSE, FALSE, 0); + + /// Adding box to Main Window gtk_container_add(GTK_CONTAINER(MainWindow), PackBoxMenu); } @@ -621,14 +663,6 @@ void RefreshScrollbars(void) SCROLLINFO vert, horiz; SetUpScrollbars(&NeedHoriz, &horiz, &vert); - GtkAdjustment *adjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(ScrollWindow)); - // g_print("adj = %f\n", gtk_adjustment_get_value(adjustment)); - // g_print("upper = %f\n", gtk_adjustment_get_upper(adjustment) - gtk_widget_get_allocated_height (ScrollWindow)); - // g_print("lower = %f\n", gtk_adjustment_get_lower(adjustment)); - // g_print("inc = %f\n", gtk_adjustment_get_step_increment(adjustment)); - // g_print("w width = %i\n", gtk_widget_get_allocated_width (DrawWindow)); - // g_print("w height = %i\n", gtk_widget_get_allocated_height (ScrollWindow)); - // SetScrollInfo(HorizScrollBar, SB_CTL, &horiz, TRUE); // SetScrollInfo(VertScrollBar, SB_CTL, &vert, TRUE); @@ -867,7 +901,7 @@ void ToggleSimulationMode(void) EnableMenuItem(FileMenu, OpenMenu, MF_GRAYED); EnableMenuItem(FileMenu, SaveMenu, MF_GRAYED); - EnableMenuItem(FileMenu, SaveAsMenu_AS, MF_GRAYED); + EnableMenuItem(FileMenu, SaveAsMenu, MF_GRAYED); EnableMenuItem(FileMenu, NewMenu, MF_GRAYED); EnableMenuItem(FileMenu, ExportMenu, MF_GRAYED); @@ -878,16 +912,16 @@ void ToggleSimulationMode(void) CheckMenuItem(SimulateMenu, SimulationModeMenu, MF_CHECKED); - // ClearSimulationData(); // simulation.cpp, ldmicro.h + ClearSimulationData(); // simulation.cpp, ldmicro.h // Recheck InSimulationMode, because there could have been a Compile // error, which would have kicked us out of simulation mode. - // if(UartFunctionUsed() && InSimulationMode) { - // ShowUartSimulationWindow(); // simulate.cpp - // } + if(UartFunctionUsed() && InSimulationMode) { + ShowUartSimulationWindow(); // simulate.cpp + } } else { RealTimeSimulationRunning = FALSE; - // KillTimer(MainWindow, TIMER_SIMULATE); + KillTimer(MainWindow, TIMER_SIMULATE); EnableMenuItem(SimulateMenu, StartSimulationMenu, MF_GRAYED); EnableMenuItem(SimulateMenu, StopSimulationMenu, MF_GRAYED); @@ -895,7 +929,7 @@ void ToggleSimulationMode(void) EnableMenuItem(FileMenu, OpenMenu, MF_ENABLED); EnableMenuItem(FileMenu, SaveMenu, MF_ENABLED); - EnableMenuItem(FileMenu, SaveAsMenu_AS, MF_ENABLED); + EnableMenuItem(FileMenu, SaveAsMenu, MF_ENABLED); EnableMenuItem(FileMenu, NewMenu, MF_ENABLED); EnableMenuItem(FileMenu, ExportMenu, MF_ENABLED); @@ -922,36 +956,86 @@ void ToggleSimulationMode(void) //----------------------------------------------------------------------------- void RefreshControlsToSettings(void) { - int i; - gtk_tree_model_get_iter_first (GTK_TREE_MODEL(IoList), iter); - gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(view)), - GTK_SELECTION_SINGLE); + GtkTreeIter iter; + BOOL path_not_empty = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(IoList), &iter); + // g_print("path e = %i\n", path_not_empty); + + int * ip; + int i = 0; + + if(!IoListOutOfSync) { IoListSelectionPoint = -1; - for(i = 0; i < Prog.io.count; i++) { - gtk_tree_model_iter_next (GTK_TREE_MODEL(IoList), iter); - if(gtk_tree_selection_get_selected (gtk_tree_view_get_selection(GTK_TREE_VIEW(view)), - IoListPtr, iter)) { - IoListSelectionPoint = i; - break; - } + + GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view)); + gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE); + + if(gtk_tree_selection_get_selected (selection, IoListPtr, &iter)) + { + GtkTreePath *path = gtk_tree_model_get_path ( *IoListPtr , &iter ) ; + ip = gtk_tree_path_get_indices ( path ) ; + i = ip[0]; + IoListSelectionPoint = i; } } - gtk_list_store_clear (IoList); + + gtk_list_store_clear (GTK_LIST_STORE(IoList)); + + /// Fill IO List + NMHDR h; + h.code = LVN_GETDISPINFO; + h.hlistFrom = IoList; + + for(i = 0; i < Prog.io.count; i++) { + gtk_list_store_append (GTK_LIST_STORE(IoList), &iter); + h.item.iItem = i; + h.hlistIter = &iter; + IoListProc(&h); + } + + if(IoListSelectionPoint >= 0) { + GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view)); + gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE); + + gtk_tree_selection_unselect_all (selection); + GtkTreePath *path = gtk_tree_path_new_from_indices ( IoListSelectionPoint, -1); + gtk_tree_selection_select_path (selection, path); + + // ListView_EnsureVisible(IoList, IoListSelectionPoint, FALSE); + } + IoListOutOfSync = FALSE; if(Prog.mcu) { - gtk_statusbar_push (GTK_STATUSBAR (StatusBar), - gtk_statusbar_get_context_id (GTK_STATUSBAR (StatusBar), "MCU Name"), + gtk_statusbar_push (GTK_STATUSBAR (StatusBar[0]), + gtk_statusbar_get_context_id (GTK_STATUSBAR (StatusBar[0]), "MCU Name"), (gchar*)Prog.mcu->mcuName); - // SendMessage(StatusBar, SB_SETTEXT, 0, (LPARAM)Prog.mcu->mcuName); } else { - // SendMessage(StatusBar, SB_SETTEXT, 0, (LPARAM)_("no MCU selected")); - gtk_statusbar_push (GTK_STATUSBAR (StatusBar), - gtk_statusbar_get_context_id (GTK_STATUSBAR (StatusBar), "MCU Name"), + gtk_statusbar_push (GTK_STATUSBAR (StatusBar[0]), + gtk_statusbar_get_context_id (GTK_STATUSBAR (StatusBar[0]), "MCU Name"), "no MCU selected"); } + char buf[256]; + sprintf(buf, _("cycle time %.2f ms"), (double)Prog.cycleTime/1000.0); + + gtk_statusbar_push (GTK_STATUSBAR (StatusBar[1]), + gtk_statusbar_get_context_id (GTK_STATUSBAR (StatusBar[1]), "Cycle time"), + buf); + + if(Prog.mcu && (Prog.mcu->whichIsa == ISA_ANSIC || + Prog.mcu->whichIsa == ISA_INTERPRETED)) + { + strcpy(buf, ""); + } else { + sprintf(buf, _("processor clock %.4f MHz"), + (double)Prog.mcuClock/1000000.0); + } + gtk_statusbar_push (GTK_STATUSBAR (StatusBar[2]), + gtk_statusbar_get_context_id (GTK_STATUSBAR (StatusBar[2]), "Processor time"), + buf); + + for(i = 0; i < NUM_SUPPORTED_MCUS; i++) { if(&SupportedMcus[i] == Prog.mcu) { CheckMenuItem(ProcessorMenu, ProcessorMenuItems[i], MF_CHECKED); @@ -975,20 +1059,39 @@ void RefreshControlsToSettings(void) //----------------------------------------------------------------------------- void GenerateIoListDontLoseSelection(void) { - int i; - gtk_tree_model_get_iter_first (GTK_TREE_MODEL(IoList), iter); - gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(view)), - GTK_SELECTION_SINGLE); + GtkTreeIter iter; + BOOL path_not_empty = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(IoList), &iter); + // g_print("path e = %i\n", path_not_empty); + + int * i ; IoListSelectionPoint = -1; - for(i = 0; i < Prog.io.count; i++) { - gtk_tree_model_iter_next (GTK_TREE_MODEL (IoList), iter); - if(gtk_tree_selection_get_selected (gtk_tree_view_get_selection(GTK_TREE_VIEW(view)), - IoListPtr, iter)) { - IoListSelectionPoint = i; - break; - } + + // GtkTreeSelection * tsel = gtk_tree_view_get_selection (tv); + // GtkTreeModel * tm ; + GtkTreePath * path ; + + GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view)); + gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE); + + if(gtk_tree_selection_get_selected (selection, IoListPtr, &iter)) + { + path = gtk_tree_model_get_path ( *IoListPtr , &iter ) ; + i = gtk_tree_path_get_indices ( path ) ; + IoListSelectionPoint = i[0]; } - // IoListSelectionPoint = GenerateIoList(IoListSelectionPoint); + + // gtk_tree_model_iter_next (GTK_TREE_MODEL(IoList), iter); + // BOOL iter_v = gtk_list_store_iter_is_valid(GTK_LIST_STORE(IoList), iter); + // g_print("iter = %i\n", iter_v); + + + // if ( gtk_tree_selection_get_selected ( tsel , &tm , &iter ) ) + // { + + // return i [ 0 ] ; + // } + + IoListSelectionPoint = GenerateIoList(IoListSelectionPoint); // can't just update the listview index; if I/O has been added then the // new selection point might be out of range till we refill it @@ -1007,8 +1110,8 @@ void MainWindowResized(void) GetClientRect(DrawWindow, &main); RECT status; - GetWindowRect(StatusBar, &status); - int statusHeight = status.bottom - status.top; + // GetWindowRect(StatusBar, &status); + // int statusHeight = status.bottom - status.top; // MoveWindow(StatusBar, 0, main.bottom - statusHeight, main.right, // statusHeight, TRUE); @@ -1022,8 +1125,8 @@ void MainWindowResized(void) // Make sure that we can't drag the top of the I/O list above the // bottom of the menu bar, because it then becomes inaccessible. if(IoListTop < 5) { - IoListHeight = main.bottom - statusHeight - 5; - IoListTop = main.bottom - IoListHeight - statusHeight; + IoListHeight = main.bottom - 5;//- statusHeight - 5; + IoListTop = main.bottom - IoListHeight;// - statusHeight; } // MoveWindow(IoList, 0, IoListTop, main.right, IoListHeight, TRUE); @@ -1041,7 +1144,7 @@ void StartSimulation(void) RealTimeSimulationRunning = TRUE; EnableMenuItem(SimulateMenu, StartSimulationMenu, MF_GRAYED); EnableMenuItem(SimulateMenu, StopSimulationMenu, MF_ENABLED); - // StartSimulationTimer(); + StartSimulationTimer(); UpdateMainWindowTitleBar(); } @@ -1055,7 +1158,7 @@ void StopSimulation(void) EnableMenuItem(SimulateMenu, StartSimulationMenu, MF_ENABLED); EnableMenuItem(SimulateMenu, StopSimulationMenu, MF_GRAYED); - // KillTimer(MainWindow, TIMER_SIMULATE); + KillTimer(MainWindow, TIMER_SIMULATE); UpdateMainWindowTitleBar(); }
\ No newline at end of file diff --git a/ldmicro/miscutil.cpp b/ldmicro/miscutil.cpp index ebdd34c..167543a 100644 --- a/ldmicro/miscutil.cpp +++ b/ldmicro/miscutil.cpp @@ -53,12 +53,12 @@ HFONT MyFixedFont; //----------------------------------------------------------------------------- void dbp(char *str, ...) { - va_list f; - char buf[1024]; - va_start(f, str); - vsprintf(buf, str, f); - OutputDebugString(buf); - OutputDebugString("\n"); + // va_list f; + // char buf[1024]; + // va_start(f, str); + // vsprintf(buf, str, f); + // OutputDebugString(buf); + // OutputDebugString("\n"); } //----------------------------------------------------------------------------- @@ -102,7 +102,7 @@ void Error(char *str, ...) printf("%s\n", buf); } else { //HWND h = GetForegroundWindow(); - //MessageBox(h, buf, _("LDmicro Error"), MB_OK | MB_ICONERROR); + MessageBox(MainWindow, buf, _("LDmicro Error"), MB_OK | MB_ICONERROR); } } @@ -132,26 +132,26 @@ void CompileSuccessfulMessage(char *str) //----------------------------------------------------------------------------- void CheckHeap(char *file, int line) { - static unsigned int SkippedCalls; - static SDWORD LastCallTime; - SDWORD now = GetTickCount(); - - // It slows us down too much to do the check every time we are called; - // but let's still do the check periodically; let's do it every 70 - // calls or every 20 ms, whichever is sooner. - if(SkippedCalls < 70 && (now - LastCallTime) < 20) { - SkippedCalls++; - return; - } + // static unsigned int SkippedCalls; + // static SDWORD LastCallTime; + // SDWORD now = GetTickCount(); + + // // It slows us down too much to do the check every time we are called; + // // but let's still do the check periodically; let's do it every 70 + // // calls or every 20 ms, whichever is sooner. + // if(SkippedCalls < 70 && (now - LastCallTime) < 20) { + // SkippedCalls++; + // return; + // } - SkippedCalls = 0; - LastCallTime = now; + // SkippedCalls = 0; + // LastCallTime = now; - // if(!HeapValidate(MainHeap, 0, NULL)) { - // dbp("file %s line %d", file, line); - // Error("Noticed memory corruption at file '%s' line %d.", file, line); - // oops(); - // } + // // if(!HeapValidate(MainHeap, 0, NULL)) { + // // dbp("file %s line %d", file, line); + // // Error("Noticed memory corruption at file '%s' line %d.", file, line); + // // oops(); + // // } } //----------------------------------------------------------------------------- diff --git a/ldmicro/pic16.cpp b/ldmicro/pic16.cpp index a813420..d45eabb 100644 --- a/ldmicro/pic16.cpp +++ b/ldmicro/pic16.cpp @@ -508,7 +508,7 @@ static void CompileFromIntermediate(BOOL topLevel) // Keep track of which 2k section we are using. When it looks like we // are about to run out, fill with nops and move on to the next one. DWORD section = 0; - + for(; IntPc < IntCodeLen; IntPc++) { // Try for a margin of about 400 words, which is a little bit // wasteful but considering that the formatted output commands @@ -518,6 +518,7 @@ static void CompileFromIntermediate(BOOL topLevel) if(topLevel && (((PicProgWriteP + 400) >> 11) != section) && ((PicProgWriteP + 400) < Prog.mcu->flashWords)) { + // Jump to the beginning of the next section Instruction(OP_MOVLW, (PicProgWriteP >> 8) + (1<<3), 0); Instruction(OP_MOVWF, REG_PCLATH, 0); @@ -532,7 +533,7 @@ static void CompileFromIntermediate(BOOL topLevel) } IntOp *a = &IntCode[IntPc]; switch(a->op) { - case INT_SET_BIT: + case INT_SET_BIT: MemForSingleBit(a->name1, FALSE, &addr, &bit); SetBit(addr, bit); break; @@ -579,7 +580,7 @@ static void CompileFromIntermediate(BOOL topLevel) CompileIfBody(condFalse); break; } - case INT_IF_VARIABLE_LES_LITERAL: { + case INT_IF_VARIABLE_LES_LITERAL: { DWORD notTrue = AllocFwdAddr(); DWORD isTrue = AllocFwdAddr(); DWORD lsbDecides = AllocFwdAddr(); @@ -693,7 +694,7 @@ static void CompileFromIntermediate(BOOL topLevel) CompileIfBody(notTrue); break; } - case INT_SET_VARIABLE_TO_VARIABLE: + case INT_SET_VARIABLE_TO_VARIABLE: MemForVariable(a->name1, &addrl, &addrh); MemForVariable(a->name2, &addrl2, &addrh2); @@ -708,7 +709,7 @@ static void CompileFromIntermediate(BOOL topLevel) // results if the destination and one of the operands happen to // be the same registers (e.g. for B = A - B). - case INT_SET_VARIABLE_ADD: + case INT_SET_VARIABLE_ADD: MemForVariable(a->name1, &addrl, &addrh); MemForVariable(a->name2, &addrl2, &addrh2); MemForVariable(a->name3, &addrl3, &addrh3); @@ -746,7 +747,7 @@ static void CompileFromIntermediate(BOOL topLevel) Instruction(OP_DECF, addrh, DEST_F); break; - case INT_SET_VARIABLE_MULTIPLY: + case INT_SET_VARIABLE_MULTIPLY: MultiplyNeeded = TRUE; MemForVariable(a->name1, &addrl, &addrh); @@ -823,7 +824,7 @@ static void CompileFromIntermediate(BOOL topLevel) break; } - case INT_UART_RECV: { + case INT_UART_RECV: { MemForVariable(a->name1, &addrl, &addrh); MemForSingleBit(a->name2, TRUE, &addr, &bit); @@ -1171,7 +1172,7 @@ static void CompileFromIntermediate(BOOL topLevel) oops(); break; } - if(((PicProgWriteP >> 11) != section) && topLevel) { + if(((PicProgWriteP >> 11) != section) && topLevel) { // This is particularly prone to happening in the last section, // if the program doesn't fit (since we won't have attempted // to add padding). @@ -1415,6 +1416,7 @@ void CompilePic16(char *outFile) WipeMemory(); AllocStart(); + Scratch0 = AllocOctetRam(); Scratch1 = AllocOctetRam(); Scratch2 = AllocOctetRam(); @@ -1423,7 +1425,7 @@ void CompilePic16(char *outFile) Scratch5 = AllocOctetRam(); Scratch6 = AllocOctetRam(); Scratch7 = AllocOctetRam(); - + // Allocate the register used to hold the high byte of the EEPROM word // that's queued up to program, plus the bit to indicate that it is // valid. @@ -1435,6 +1437,7 @@ void CompilePic16(char *outFile) // bootloaders rewrite the beginning of the program to do their magic. // PCLATH is init to 0, but apparently some bootloaders want to see us // initialize it again. + Instruction(OP_BCF, REG_PCLATH, 3); Instruction(OP_BCF, REG_PCLATH, 4); Instruction(OP_GOTO, progStart, 0); @@ -1576,7 +1579,7 @@ void CompilePic16(char *outFile) ComplainAboutBaudRateError(divisor, actual, percentErr); } if(divisor > 255) ComplainAboutBaudRateOverflow(); - + WriteRegister(REG_SPBRG, divisor); WriteRegister(REG_TXSTA, 0x20); // only TXEN set WriteRegister(REG_RCSTA, 0x90); // only SPEN, CREN set diff --git a/ldmicro/simulate.cpp b/ldmicro/simulate.cpp index 9115bbe..6b38896 100644 --- a/ldmicro/simulate.cpp +++ b/ldmicro/simulate.cpp @@ -106,117 +106,117 @@ static char *MarkUsedVariable(char *name, DWORD flag); // Looks in the SingleBitItems list; if an item is not present then it is // FALSE by default. //----------------------------------------------------------------------------- -// static BOOL SingleBitOn(char *name) -// { -// int i; -// for(i = 0; i < SingleBitItemsCount; i++) { -// if(strcmp(SingleBitItems[i].name, name)==0) { -// return SingleBitItems[i].powered; -// } -// } -// return FALSE; -// } +static BOOL SingleBitOn(char *name) +{ + int i; + for(i = 0; i < SingleBitItemsCount; i++) { + if(strcmp(SingleBitItems[i].name, name)==0) { + return SingleBitItems[i].powered; + } + } + return FALSE; +} //----------------------------------------------------------------------------- // Set the state of a single-bit item. Adds it to the list if it is not there // already. //----------------------------------------------------------------------------- -// static void SetSingleBit(char *name, BOOL state) -// { -// int i; -// for(i = 0; i < SingleBitItemsCount; i++) { -// if(strcmp(SingleBitItems[i].name, name)==0) { -// SingleBitItems[i].powered = state; -// return; -// } -// } -// if(i < MAX_IO) { -// strcpy(SingleBitItems[i].name, name); -// SingleBitItems[i].powered = state; -// SingleBitItemsCount++; -// } -// } +static void SetSingleBit(char *name, BOOL state) +{ + int i; + for(i = 0; i < SingleBitItemsCount; i++) { + if(strcmp(SingleBitItems[i].name, name)==0) { + SingleBitItems[i].powered = state; + return; + } + } + if(i < MAX_IO) { + strcpy(SingleBitItems[i].name, name); + SingleBitItems[i].powered = state; + SingleBitItemsCount++; + } +} //----------------------------------------------------------------------------- // Count a timer up (i.e. increment its associated count by 1). Must already // exist in the table. //----------------------------------------------------------------------------- -// static void IncrementVariable(char *name) -// { -// int i; -// for(i = 0; i < VariablesCount; i++) { -// if(strcmp(Variables[i].name, name)==0) { -// (Variables[i].val)++; -// return; -// } -// } -// oops(); -// } +static void IncrementVariable(char *name) +{ + int i; + for(i = 0; i < VariablesCount; i++) { + if(strcmp(Variables[i].name, name)==0) { + (Variables[i].val)++; + return; + } + } + oops(); +} //----------------------------------------------------------------------------- // Set a variable to a value. //----------------------------------------------------------------------------- -// static void SetSimulationVariable(char *name, SWORD val) -// { -// int i; -// for(i = 0; i < VariablesCount; i++) { -// if(strcmp(Variables[i].name, name)==0) { -// Variables[i].val = val; -// return; -// } -// } -// MarkUsedVariable(name, VAR_FLAG_OTHERWISE_FORGOTTEN); -// SetSimulationVariable(name, val); -// } +static void SetSimulationVariable(char *name, SWORD val) +{ + int i; + for(i = 0; i < VariablesCount; i++) { + if(strcmp(Variables[i].name, name)==0) { + Variables[i].val = val; + return; + } + } + MarkUsedVariable(name, VAR_FLAG_OTHERWISE_FORGOTTEN); + SetSimulationVariable(name, val); +} //----------------------------------------------------------------------------- // Read a variable's value. //----------------------------------------------------------------------------- -// SWORD GetSimulationVariable(char *name) -// { -// int i; -// for(i = 0; i < VariablesCount; i++) { -// if(strcmp(Variables[i].name, name)==0) { -// return Variables[i].val; -// } -// } -// MarkUsedVariable(name, VAR_FLAG_OTHERWISE_FORGOTTEN); -// return GetSimulationVariable(name); -// } +SWORD GetSimulationVariable(char *name) +{ + int i; + for(i = 0; i < VariablesCount; i++) { + if(strcmp(Variables[i].name, name)==0) { + return Variables[i].val; + } + } + MarkUsedVariable(name, VAR_FLAG_OTHERWISE_FORGOTTEN); + return GetSimulationVariable(name); +} //----------------------------------------------------------------------------- // Set the shadow copy of a variable associated with a READ ADC operation. This // will get committed to the real copy when the rung-in condition to the // READ ADC is true. //----------------------------------------------------------------------------- -// void SetAdcShadow(char *name, SWORD val) -// { -// int i; -// for(i = 0; i < AdcShadowsCount; i++) { -// if(strcmp(AdcShadows[i].name, name)==0) { -// AdcShadows[i].val = val; -// return; -// } -// } -// strcpy(AdcShadows[i].name, name); -// AdcShadows[i].val = val; -// AdcShadowsCount++; -// } +void SetAdcShadow(char *name, SWORD val) +{ + int i; + for(i = 0; i < AdcShadowsCount; i++) { + if(strcmp(AdcShadows[i].name, name)==0) { + AdcShadows[i].val = val; + return; + } + } + strcpy(AdcShadows[i].name, name); + AdcShadows[i].val = val; + AdcShadowsCount++; +} //----------------------------------------------------------------------------- // Return the shadow value of a variable associated with a READ ADC. This is // what gets copied into the real variable when an ADC read is simulated. //----------------------------------------------------------------------------- -// SWORD GetAdcShadow(char *name) -// { -// int i; -// for(i = 0; i < AdcShadowsCount; i++) { -// if(strcmp(AdcShadows[i].name, name)==0) { -// return AdcShadows[i].val; -// } -// } -// return 0; -// } +SWORD GetAdcShadow(char *name) +{ + int i; + for(i = 0; i < AdcShadowsCount; i++) { + if(strcmp(AdcShadows[i].name, name)==0) { + return AdcShadows[i].val; + } + } + return 0; +} //----------------------------------------------------------------------------- // Mark how a variable is used; a series of flags that we can OR together, @@ -224,270 +224,270 @@ static char *MarkUsedVariable(char *name, DWORD flag); // (e.g. just a TON, an RTO with its reset, etc.). Returns NULL for success, // else an error string. //----------------------------------------------------------------------------- -// static char *MarkUsedVariable(char *name, DWORD flag) -// { -// int i; -// for(i = 0; i < VariablesCount; i++) { -// if(strcmp(Variables[i].name, name)==0) { -// break; -// } -// } -// if(i >= MAX_IO) return ""; - -// if(i == VariablesCount) { -// strcpy(Variables[i].name, name); -// Variables[i].usedFlags = 0; -// Variables[i].val = 0; -// VariablesCount++; -// } - -// switch(flag) { -// case VAR_FLAG_TOF: -// if(Variables[i].usedFlags != 0) -// return _("TOF: variable cannot be used elsewhere"); -// break; - -// case VAR_FLAG_TON: -// if(Variables[i].usedFlags != 0) -// return _("TON: variable cannot be used elsewhere"); -// break; +static char *MarkUsedVariable(char *name, DWORD flag) +{ + int i; + for(i = 0; i < VariablesCount; i++) { + if(strcmp(Variables[i].name, name)==0) { + break; + } + } + if(i >= MAX_IO) return ""; + + if(i == VariablesCount) { + strcpy(Variables[i].name, name); + Variables[i].usedFlags = 0; + Variables[i].val = 0; + VariablesCount++; + } + + switch(flag) { + case VAR_FLAG_TOF: + if(Variables[i].usedFlags != 0) + return _("TOF: variable cannot be used elsewhere"); + break; + + case VAR_FLAG_TON: + if(Variables[i].usedFlags != 0) + return _("TON: variable cannot be used elsewhere"); + break; -// case VAR_FLAG_RTO: -// if(Variables[i].usedFlags & ~VAR_FLAG_RES) -// return _("RTO: variable can only be used for RES elsewhere"); -// break; - -// case VAR_FLAG_CTU: -// case VAR_FLAG_CTD: -// case VAR_FLAG_CTC: -// case VAR_FLAG_RES: -// case VAR_FLAG_ANY: -// break; - -// case VAR_FLAG_OTHERWISE_FORGOTTEN: -// if(name[0] != '$') { -// Error(_("Variable '%s' not assigned to, e.g. with a " -// "MOV statement, an ADD statement, etc.\r\n\r\n" -// "This is probably a programming error; now it " -// "will always be zero."), name); -// } -// break; - -// default: -// oops(); -// } - -// Variables[i].usedFlags |= flag; -// return NULL; -// } - -// //----------------------------------------------------------------------------- -// // Check for duplicate uses of a single variable. For example, there should -// // not be two TONs with the same name. On the other hand, it would be okay -// // to have an RTO with the same name as its reset; in fact, verify that -// // there must be a reset for each RTO. -// //----------------------------------------------------------------------------- -// static void MarkWithCheck(char *name, int flag) -// { -// char *s = MarkUsedVariable(name, flag); -// if(s) { -// Error(_("Variable for '%s' incorrectly assigned: %s."), name, s); -// } -// } - -// static void CheckVariableNamesCircuit(int which, void *elem) -// { -// ElemLeaf *l = (ElemLeaf *)elem; -// char *name = NULL; -// DWORD flag; - -// switch(which) { -// case ELEM_SERIES_SUBCKT: { -// // int i; -// // ElemSubcktSeries *s = (ElemSubcktSeries *)elem; -// // for(i = 0; i < s->count; i++) { -// // CheckVariableNamesCircuit(s->contents[i].which, -// // s->contents[i].d.any); -// // } -// // break; -// } - -// case ELEM_PARALLEL_SUBCKT: { -// // int i; -// // ElemSubcktParallel *p = (ElemSubcktParallel *)elem; -// // for(i = 0; i < p->count; i++) { -// // CheckVariableNamesCircuit(p->contents[i].which, -// // p->contents[i].d.any); -// // } -// // break; -// } + case VAR_FLAG_RTO: + if(Variables[i].usedFlags & ~VAR_FLAG_RES) + return _("RTO: variable can only be used for RES elsewhere"); + break; + + case VAR_FLAG_CTU: + case VAR_FLAG_CTD: + case VAR_FLAG_CTC: + case VAR_FLAG_RES: + case VAR_FLAG_ANY: + break; + + case VAR_FLAG_OTHERWISE_FORGOTTEN: + if(name[0] != '$') { + Error(_("Variable '%s' not assigned to, e.g. with a " + "MOV statement, an ADD statement, etc.\r\n\r\n" + "This is probably a programming error; now it " + "will always be zero."), name); + } + break; + + default: + oops(); + } + + Variables[i].usedFlags |= flag; + return NULL; +} + +//----------------------------------------------------------------------------- +// Check for duplicate uses of a single variable. For example, there should +// not be two TONs with the same name. On the other hand, it would be okay +// to have an RTO with the same name as its reset; in fact, verify that +// there must be a reset for each RTO. +//----------------------------------------------------------------------------- +static void MarkWithCheck(char *name, int flag) +{ + char *s = MarkUsedVariable(name, flag); + if(s) { + Error(_("Variable for '%s' incorrectly assigned: %s."), name, s); + } +} + +static void CheckVariableNamesCircuit(int which, void *elem) +{ + ElemLeaf *l = (ElemLeaf *)elem; + char *name = NULL; + DWORD flag; + + switch(which) { + case ELEM_SERIES_SUBCKT: { + int i; + ElemSubcktSeries *s = (ElemSubcktSeries *)elem; + for(i = 0; i < s->count; i++) { + CheckVariableNamesCircuit(s->contents[i].which, + s->contents[i].d.any); + } + break; + } + + case ELEM_PARALLEL_SUBCKT: { + int i; + ElemSubcktParallel *p = (ElemSubcktParallel *)elem; + for(i = 0; i < p->count; i++) { + CheckVariableNamesCircuit(p->contents[i].which, + p->contents[i].d.any); + } + break; + } -// case ELEM_RTO: -// case ELEM_TOF: -// case ELEM_TON: -// if(which == ELEM_RTO) -// flag = VAR_FLAG_RTO; -// else if(which == ELEM_TOF) -// flag = VAR_FLAG_TOF; -// else if(which == ELEM_TON) -// flag = VAR_FLAG_TON; -// else oops(); - -// MarkWithCheck(l->d.timer.name, flag); - -// break; - -// case ELEM_CTU: -// case ELEM_CTD: -// case ELEM_CTC: -// if(which == ELEM_CTU) -// flag = VAR_FLAG_CTU; -// else if(which == ELEM_CTD) -// flag = VAR_FLAG_CTD; -// else if(which == ELEM_CTC) -// flag = VAR_FLAG_CTC; -// else oops(); - -// MarkWithCheck(l->d.counter.name, flag); - -// break; - -// case ELEM_RES: -// MarkWithCheck(l->d.reset.name, VAR_FLAG_RES); -// break; - -// case ELEM_MOVE: -// MarkWithCheck(l->d.move.dest, VAR_FLAG_ANY); -// break; - -// case ELEM_LOOK_UP_TABLE: -// MarkWithCheck(l->d.lookUpTable.dest, VAR_FLAG_ANY); -// break; - -// case ELEM_PIECEWISE_LINEAR: -// // MarkWithCheck(l->d.piecewiseLinear.dest, VAR_FLAG_ANY); -// break; - -// case ELEM_READ_ADC: -// // MarkWithCheck(l->d.readAdc.name, VAR_FLAG_ANY); -// break; - -// case ELEM_ADD: -// case ELEM_SUB: -// case ELEM_MUL: -// case ELEM_DIV: -// // MarkWithCheck(l->d.math.dest, VAR_FLAG_ANY); -// break; - -// case ELEM_UART_RECV: -// // MarkWithCheck(l->d.uart.name, VAR_FLAG_ANY); -// break; - -// case ELEM_SHIFT_REGISTER: { -// // int i; -// // for(i = 1; i < l->d.shiftRegister.stages; i++) { -// // char str[MAX_NAME_LEN+10]; -// // sprintf(str, "%s%d", l->d.shiftRegister.name, i); -// // MarkWithCheck(str, VAR_FLAG_ANY); -// // } -// break; -// } - -// case ELEM_PERSIST: -// case ELEM_FORMATTED_STRING: -// case ELEM_SET_PWM: -// case ELEM_MASTER_RELAY: -// case ELEM_UART_SEND: -// case ELEM_PLACEHOLDER: -// case ELEM_COMMENT: -// case ELEM_OPEN: -// case ELEM_SHORT: -// case ELEM_COIL: -// case ELEM_CONTACTS: -// case ELEM_ONE_SHOT_RISING: -// case ELEM_ONE_SHOT_FALLING: -// case ELEM_EQU: -// case ELEM_NEQ: -// case ELEM_GRT: -// case ELEM_GEQ: -// case ELEM_LES: -// case ELEM_LEQ: -// break; - -// default: -// oops(); -// } -// } - -// static void CheckVariableNames(void) -// { -// int i; -// for(i = 0; i < Prog.numRungs; i++) { -// CheckVariableNamesCircuit(ELEM_SERIES_SUBCKT, Prog.rungs[i]); -// } -// } + case ELEM_RTO: + case ELEM_TOF: + case ELEM_TON: + if(which == ELEM_RTO) + flag = VAR_FLAG_RTO; + else if(which == ELEM_TOF) + flag = VAR_FLAG_TOF; + else if(which == ELEM_TON) + flag = VAR_FLAG_TON; + else oops(); + + MarkWithCheck(l->d.timer.name, flag); + + break; + + case ELEM_CTU: + case ELEM_CTD: + case ELEM_CTC: + if(which == ELEM_CTU) + flag = VAR_FLAG_CTU; + else if(which == ELEM_CTD) + flag = VAR_FLAG_CTD; + else if(which == ELEM_CTC) + flag = VAR_FLAG_CTC; + else oops(); + + MarkWithCheck(l->d.counter.name, flag); + + break; + + case ELEM_RES: + MarkWithCheck(l->d.reset.name, VAR_FLAG_RES); + break; + + case ELEM_MOVE: + MarkWithCheck(l->d.move.dest, VAR_FLAG_ANY); + break; + + case ELEM_LOOK_UP_TABLE: + MarkWithCheck(l->d.lookUpTable.dest, VAR_FLAG_ANY); + break; + + case ELEM_PIECEWISE_LINEAR: + MarkWithCheck(l->d.piecewiseLinear.dest, VAR_FLAG_ANY); + break; + + case ELEM_READ_ADC: + MarkWithCheck(l->d.readAdc.name, VAR_FLAG_ANY); + break; + + case ELEM_ADD: + case ELEM_SUB: + case ELEM_MUL: + case ELEM_DIV: + MarkWithCheck(l->d.math.dest, VAR_FLAG_ANY); + break; + + case ELEM_UART_RECV: + MarkWithCheck(l->d.uart.name, VAR_FLAG_ANY); + break; + + case ELEM_SHIFT_REGISTER: { + int i; + for(i = 1; i < l->d.shiftRegister.stages; i++) { + char str[MAX_NAME_LEN+10]; + sprintf(str, "%s%d", l->d.shiftRegister.name, i); + MarkWithCheck(str, VAR_FLAG_ANY); + } + break; + } + + case ELEM_PERSIST: + case ELEM_FORMATTED_STRING: + case ELEM_SET_PWM: + case ELEM_MASTER_RELAY: + case ELEM_UART_SEND: + case ELEM_PLACEHOLDER: + case ELEM_COMMENT: + case ELEM_OPEN: + case ELEM_SHORT: + case ELEM_COIL: + case ELEM_CONTACTS: + case ELEM_ONE_SHOT_RISING: + case ELEM_ONE_SHOT_FALLING: + case ELEM_EQU: + case ELEM_NEQ: + case ELEM_GRT: + case ELEM_GEQ: + case ELEM_LES: + case ELEM_LEQ: + break; + + default: + oops(); + } +} + +static void CheckVariableNames(void) +{ + int i; + for(i = 0; i < Prog.numRungs; i++) { + CheckVariableNamesCircuit(ELEM_SERIES_SUBCKT, Prog.rungs[i]); + } +} //----------------------------------------------------------------------------- // The IF condition is true. Execute the body, up until the ELSE or the // END IF, and then skip the ELSE if it is present. Called with PC on the // IF, returns with PC on the END IF. //----------------------------------------------------------------------------- -// static void IfConditionTrue(void) -// { -// IntPc++; -// // now PC is on the first statement of the IF body -// SimulateIntCode(); -// // now PC is on the ELSE or the END IF -// if(IntCode[IntPc].op == INT_ELSE) { -// int nesting = 1; -// for(; ; IntPc++) { -// if(IntPc >= IntCodeLen) oops(); - -// if(IntCode[IntPc].op == INT_END_IF) { -// nesting--; -// } else if(INT_IF_GROUP(IntCode[IntPc].op)) { -// nesting++; -// } -// if(nesting == 0) break; -// } -// } else if(IntCode[IntPc].op == INT_END_IF) { -// return; -// } else { -// oops(); -// } -// } +static void IfConditionTrue(void) +{ + IntPc++; + // now PC is on the first statement of the IF body + SimulateIntCode(); + // now PC is on the ELSE or the END IF + if(IntCode[IntPc].op == INT_ELSE) { + int nesting = 1; + for(; ; IntPc++) { + if(IntPc >= IntCodeLen) oops(); + + if(IntCode[IntPc].op == INT_END_IF) { + nesting--; + } else if(INT_IF_GROUP(IntCode[IntPc].op)) { + nesting++; + } + if(nesting == 0) break; + } + } else if(IntCode[IntPc].op == INT_END_IF) { + return; + } else { + oops(); + } +} //----------------------------------------------------------------------------- // The IF condition is false. Skip the body, up until the ELSE or the END // IF, and then execute the ELSE if it is present. Called with PC on the IF, // returns with PC on the END IF. //----------------------------------------------------------------------------- -// static void IfConditionFalse(void) -// { -// int nesting = 0; -// for(; ; IntPc++) { -// if(IntPc >= IntCodeLen) oops(); - -// if(IntCode[IntPc].op == INT_END_IF) { -// nesting--; -// } else if(INT_IF_GROUP(IntCode[IntPc].op)) { -// nesting++; -// } else if(IntCode[IntPc].op == INT_ELSE && nesting == 1) { -// break; -// } -// if(nesting == 0) break; -// } - -// // now PC is on the ELSE or the END IF -// if(IntCode[IntPc].op == INT_ELSE) { -// IntPc++; -// SimulateIntCode(); -// } else if(IntCode[IntPc].op == INT_END_IF) { -// return; -// } else { -// oops(); -// } -// } +static void IfConditionFalse(void) +{ + int nesting = 0; + for(; ; IntPc++) { + if(IntPc >= IntCodeLen) oops(); + + if(IntCode[IntPc].op == INT_END_IF) { + nesting--; + } else if(INT_IF_GROUP(IntCode[IntPc].op)) { + nesting++; + } else if(IntCode[IntPc].op == INT_ELSE && nesting == 1) { + break; + } + if(nesting == 0) break; + } + + // now PC is on the ELSE or the END IF + if(IntCode[IntPc].op == INT_ELSE) { + IntPc++; + SimulateIntCode(); + } else if(IntCode[IntPc].op == INT_END_IF) { + return; + } else { + oops(); + } +} //----------------------------------------------------------------------------- // Evaluate a circuit, calling ourselves recursively to evaluate if/else @@ -495,229 +495,231 @@ static char *MarkUsedVariable(char *name, DWORD flag); // internal tables. Returns when it reaches an end if or an else construct, // or at the end of the program. //----------------------------------------------------------------------------- -// static void SimulateIntCode(void) -// { -// for(; IntPc < IntCodeLen; IntPc++) { -// IntOp *a = &IntCode[IntPc]; -// switch(a->op) { -// case INT_SIMULATE_NODE_STATE: -// if(*(a->poweredAfter) != SingleBitOn(a->name1)) -// NeedRedraw = TRUE; -// *(a->poweredAfter) = SingleBitOn(a->name1); -// break; - -// case INT_SET_BIT: -// SetSingleBit(a->name1, TRUE); -// break; - -// case INT_CLEAR_BIT: -// SetSingleBit(a->name1, FALSE); -// break; - -// case INT_COPY_BIT_TO_BIT: -// SetSingleBit(a->name1, SingleBitOn(a->name2)); -// break; - -// case INT_SET_VARIABLE_TO_LITERAL: -// if(GetSimulationVariable(a->name1) != -// a->literal && a->name1[0] != '$') -// { -// NeedRedraw = TRUE; -// } -// SetSimulationVariable(a->name1, a->literal); -// break; - -// case INT_SET_VARIABLE_TO_VARIABLE: -// if(GetSimulationVariable(a->name1) != -// GetSimulationVariable(a->name2)) -// { -// NeedRedraw = TRUE; -// } -// SetSimulationVariable(a->name1, -// GetSimulationVariable(a->name2)); -// break; - -// case INT_INCREMENT_VARIABLE: -// IncrementVariable(a->name1); -// break; - -// { -// SWORD v; -// case INT_SET_VARIABLE_ADD: -// v = GetSimulationVariable(a->name2) + -// GetSimulationVariable(a->name3); -// goto math; -// case INT_SET_VARIABLE_SUBTRACT: -// v = GetSimulationVariable(a->name2) - -// GetSimulationVariable(a->name3); -// goto math; -// case INT_SET_VARIABLE_MULTIPLY: -// v = GetSimulationVariable(a->name2) * -// GetSimulationVariable(a->name3); -// goto math; -// case INT_SET_VARIABLE_DIVIDE: -// if(GetSimulationVariable(a->name3) != 0) { -// v = GetSimulationVariable(a->name2) / -// GetSimulationVariable(a->name3); -// } else { -// v = 0; -// Error(_("Division by zero; halting simulation")); -// StopSimulation(); -// } -// goto math; -// math: -// if(GetSimulationVariable(a->name1) != v) { -// NeedRedraw = TRUE; -// SetSimulationVariable(a->name1, v); -// } -// break; -// } - -// #define IF_BODY \ -// { \ -// IfConditionTrue(); \ -// } else { \ -// IfConditionFalse(); \ -// } -// case INT_IF_BIT_SET: -// if(SingleBitOn(a->name1)) -// IF_BODY -// break; - -// case INT_IF_BIT_CLEAR: -// if(!SingleBitOn(a->name1)) -// IF_BODY -// break; - -// case INT_IF_VARIABLE_LES_LITERAL: -// if(GetSimulationVariable(a->name1) < a->literal) -// IF_BODY -// break; - -// case INT_IF_VARIABLE_EQUALS_VARIABLE: -// if(GetSimulationVariable(a->name1) == -// GetSimulationVariable(a->name2)) -// IF_BODY -// break; - -// case INT_IF_VARIABLE_GRT_VARIABLE: -// if(GetSimulationVariable(a->name1) > -// GetSimulationVariable(a->name2)) -// IF_BODY -// break; - -// case INT_SET_PWM: -// // Dummy call will cause a warning if no one ever assigned -// // to that variable. -// (void)GetSimulationVariable(a->name1); -// break; - -// // Don't try to simulate the EEPROM stuff: just hold the EEPROM -// // busy all the time, so that the program never does anything -// // with it. -// case INT_EEPROM_BUSY_CHECK: -// SetSingleBit(a->name1, TRUE); -// break; - -// case INT_EEPROM_READ: -// case INT_EEPROM_WRITE: -// oops(); -// break; - -// case INT_READ_ADC: -// // Keep the shadow copies of the ADC variables because in -// // the real device they will not be updated until an actual -// // read is performed, which occurs only for a true rung-in -// // condition there. -// SetSimulationVariable(a->name1, GetAdcShadow(a->name1)); -// break; - -// case INT_UART_SEND: -// if(SingleBitOn(a->name2) && (SimulateUartTxCountdown == 0)) { -// SimulateUartTxCountdown = 2; -// AppendToUartSimulationTextControl( -// (BYTE)GetSimulationVariable(a->name1)); -// } -// if(SimulateUartTxCountdown == 0) { -// SetSingleBit(a->name2, FALSE); -// } else { -// SetSingleBit(a->name2, TRUE); -// } -// break; - -// case INT_UART_RECV: -// if(QueuedUartCharacter >= 0) { -// SetSingleBit(a->name2, TRUE); -// SetSimulationVariable(a->name1, (SWORD)QueuedUartCharacter); -// QueuedUartCharacter = -1; -// } else { -// SetSingleBit(a->name2, FALSE); -// } -// break; - -// case INT_END_IF: -// case INT_ELSE: -// return; - -// case INT_COMMENT: -// break; +static void SimulateIntCode(void) +{ + for(; IntPc < IntCodeLen; IntPc++) { + IntOp *a = &IntCode[IntPc]; + switch(a->op) { + case INT_SIMULATE_NODE_STATE: + if(*(a->poweredAfter) != SingleBitOn(a->name1)) + NeedRedraw = TRUE; + *(a->poweredAfter) = SingleBitOn(a->name1); + break; + + case INT_SET_BIT: + SetSingleBit(a->name1, TRUE); + break; + + case INT_CLEAR_BIT: + SetSingleBit(a->name1, FALSE); + break; + + case INT_COPY_BIT_TO_BIT: + SetSingleBit(a->name1, SingleBitOn(a->name2)); + break; + + case INT_SET_VARIABLE_TO_LITERAL: + if(GetSimulationVariable(a->name1) != + a->literal && a->name1[0] != '$') + { + NeedRedraw = TRUE; + } + SetSimulationVariable(a->name1, a->literal); + break; + + case INT_SET_VARIABLE_TO_VARIABLE: + if(GetSimulationVariable(a->name1) != + GetSimulationVariable(a->name2)) + { + NeedRedraw = TRUE; + } + SetSimulationVariable(a->name1, + GetSimulationVariable(a->name2)); + break; + + case INT_INCREMENT_VARIABLE: + IncrementVariable(a->name1); + break; + + { + SWORD v; + case INT_SET_VARIABLE_ADD: + v = GetSimulationVariable(a->name2) + + GetSimulationVariable(a->name3); + goto math; + case INT_SET_VARIABLE_SUBTRACT: + v = GetSimulationVariable(a->name2) - + GetSimulationVariable(a->name3); + goto math; + case INT_SET_VARIABLE_MULTIPLY: + v = GetSimulationVariable(a->name2) * + GetSimulationVariable(a->name3); + goto math; + case INT_SET_VARIABLE_DIVIDE: + if(GetSimulationVariable(a->name3) != 0) { + v = GetSimulationVariable(a->name2) / + GetSimulationVariable(a->name3); + } else { + v = 0; + Error(_("Division by zero; halting simulation")); + StopSimulation(); + } + goto math; +math: + if(GetSimulationVariable(a->name1) != v) { + NeedRedraw = TRUE; + SetSimulationVariable(a->name1, v); + } + break; + } + +#define IF_BODY \ + { \ + IfConditionTrue(); \ + } else { \ + IfConditionFalse(); \ + } + case INT_IF_BIT_SET: + if(SingleBitOn(a->name1)) + IF_BODY + break; + + case INT_IF_BIT_CLEAR: + if(!SingleBitOn(a->name1)) + IF_BODY + break; + + case INT_IF_VARIABLE_LES_LITERAL: + if(GetSimulationVariable(a->name1) < a->literal) + IF_BODY + break; + + case INT_IF_VARIABLE_EQUALS_VARIABLE: + if(GetSimulationVariable(a->name1) == + GetSimulationVariable(a->name2)) + IF_BODY + break; + + case INT_IF_VARIABLE_GRT_VARIABLE: + if(GetSimulationVariable(a->name1) > + GetSimulationVariable(a->name2)) + IF_BODY + break; + + case INT_SET_PWM: + // Dummy call will cause a warning if no one ever assigned + // to that variable. + (void)GetSimulationVariable(a->name1); + break; + + // Don't try to simulate the EEPROM stuff: just hold the EEPROM + // busy all the time, so that the program never does anything + // with it. + case INT_EEPROM_BUSY_CHECK: + SetSingleBit(a->name1, TRUE); + break; + + case INT_EEPROM_READ: + case INT_EEPROM_WRITE: + oops(); + break; + + case INT_READ_ADC: + // Keep the shadow copies of the ADC variables because in + // the real device they will not be updated until an actual + // read is performed, which occurs only for a true rung-in + // condition there. + SetSimulationVariable(a->name1, GetAdcShadow(a->name1)); + break; + + case INT_UART_SEND: + if(SingleBitOn(a->name2) && (SimulateUartTxCountdown == 0)) { + SimulateUartTxCountdown = 2; + AppendToUartSimulationTextControl( + (BYTE)GetSimulationVariable(a->name1)); + } + if(SimulateUartTxCountdown == 0) { + SetSingleBit(a->name2, FALSE); + } else { + SetSingleBit(a->name2, TRUE); + } + break; + + case INT_UART_RECV: + if(QueuedUartCharacter >= 0) { + SetSingleBit(a->name2, TRUE); + SetSimulationVariable(a->name1, (SWORD)QueuedUartCharacter); + QueuedUartCharacter = -1; + } else { + SetSingleBit(a->name2, FALSE); + } + break; + + case INT_END_IF: + case INT_ELSE: + return; + + case INT_COMMENT: + break; -// default: -// oops(); -// break; -// } -// } -// } + default: + oops(); + break; + } + } +} //----------------------------------------------------------------------------- // Called by the Windows timer that triggers cycles when we are running // in real time. //----------------------------------------------------------------------------- -// void CALLBACK PlcCycleTimer(HWND hwnd, UINT msg, UINT_PTR id, DWORD time) -// { -// int i; -// for(i = 0; i < CyclesPerTimerTick; i++) { -// SimulateOneCycle(FALSE); -// } -// } +BOOL PlcCycleTimer(BOOL kill = FALSE) +{ + for(int i = 0; i < CyclesPerTimerTick; i++) { + SimulateOneCycle(FALSE); + } + + return !kill; +} //----------------------------------------------------------------------------- // Simulate one cycle of the PLC. Update everything, and keep track of whether // any outputs have changed. If so, force a screen refresh. If requested do // a screen refresh regardless. //----------------------------------------------------------------------------- -// void SimulateOneCycle(BOOL forceRefresh) -// { +void SimulateOneCycle(BOOL forceRefresh) +{ // When there is an error message up, the modal dialog makes its own // event loop, and there is risk that we would go recursive. So let // us fix that. (Note that there are no concurrency issues; we really // would get called recursively, not just reentrantly.) - // static BOOL Simulating = FALSE; + static BOOL Simulating = FALSE; - // if(Simulating) return; - // Simulating = TRUE; + if(Simulating) return; + Simulating = TRUE; - // NeedRedraw = FALSE; + NeedRedraw = FALSE; - // if(SimulateUartTxCountdown > 0) { - // SimulateUartTxCountdown--; - // } else { - // SimulateUartTxCountdown = 0; - // } + if(SimulateUartTxCountdown > 0) { + SimulateUartTxCountdown--; + } else { + SimulateUartTxCountdown = 0; + } - // IntPc = 0; - // SimulateIntCode(); + IntPc = 0; + SimulateIntCode(); - // if(NeedRedraw || SimulateRedrawAfterNextCycle || forceRefresh) { - // InvalidateRect(MainWindow, NULL, FALSE); - // ListView_RedrawItems(IoList, 0, Prog.io.count - 1); - // } + if(NeedRedraw || SimulateRedrawAfterNextCycle || forceRefresh) { + InvalidateRect(DrawWindow, NULL, FALSE); + gtk_widget_queue_draw(DrawWindow); + // ListView_RedrawItems(IoList, 0, Prog.io.count - 1); + } - // SimulateRedrawAfterNextCycle = FALSE; - // if(NeedRedraw) SimulateRedrawAfterNextCycle = TRUE; + SimulateRedrawAfterNextCycle = FALSE; + if(NeedRedraw) SimulateRedrawAfterNextCycle = TRUE; - // Simulating = FALSE; -// } + Simulating = FALSE; +} //----------------------------------------------------------------------------- // Start the timer that we use to trigger PLC cycles in approximately real @@ -725,81 +727,81 @@ static char *MarkUsedVariable(char *name, DWORD flag); // is about as fast as anyone could follow by eye. Faster timers will just // go instantly. //----------------------------------------------------------------------------- -// void StartSimulationTimer(void) -// { -// int p = Prog.cycleTime/1000; -// if(p < 5) { -// SetTimer(MainWindow, TIMER_SIMULATE, 10, PlcCycleTimer); -// CyclesPerTimerTick = 10000 / Prog.cycleTime; -// } else { -// SetTimer(MainWindow, TIMER_SIMULATE, p, PlcCycleTimer); -// CyclesPerTimerTick = 1; -// } -// } +void StartSimulationTimer(void) +{ + int p = Prog.cycleTime/1000; + if(p < 5) { + SetTimer(MainWindow, TIMER_SIMULATE, 10, PlcCycleTimer); + CyclesPerTimerTick = 10000 / Prog.cycleTime; + } else { + SetTimer(MainWindow, TIMER_SIMULATE, p, PlcCycleTimer); + CyclesPerTimerTick = 1; + } +} //----------------------------------------------------------------------------- // Clear out all the parameters relating to the previous simulation. //----------------------------------------------------------------------------- -// void ClearSimulationData(void) -// { -// VariablesCount = 0; -// SingleBitItemsCount = 0; -// AdcShadowsCount = 0; -// QueuedUartCharacter = -1; -// SimulateUartTxCountdown = 0; +void ClearSimulationData(void) +{ + VariablesCount = 0; + SingleBitItemsCount = 0; + AdcShadowsCount = 0; + QueuedUartCharacter = -1; + SimulateUartTxCountdown = 0; -// CheckVariableNames(); + CheckVariableNames(); -// SimulateRedrawAfterNextCycle = TRUE; + SimulateRedrawAfterNextCycle = TRUE; -// if(!GenerateIntermediateCode()) { -// ToggleSimulationMode(); -// return; -// } + if(!GenerateIntermediateCode()) { + ToggleSimulationMode(); + return; + } -// SimulateOneCycle(TRUE); -// } + SimulateOneCycle(TRUE); +} //----------------------------------------------------------------------------- // Provide a description for an item (Xcontacts, Ycoil, Rrelay, Ttimer, // or other) in the I/O list. //----------------------------------------------------------------------------- -// void DescribeForIoList(char *name, char *out) -// { -// switch(name[0]) { -// case 'R': -// case 'X': -// case 'Y': -// sprintf(out, "%d", SingleBitOn(name)); -// break; - -// case 'T': { -// double dtms = GetSimulationVariable(name) * -// (Prog.cycleTime / 1000.0); -// if(dtms < 1000) { -// sprintf(out, "%.2f ms", dtms); -// } else { -// sprintf(out, "%.3f s", dtms / 1000); -// } -// break; -// } -// default: { -// SWORD v = GetSimulationVariable(name); -// sprintf(out, "%hd (0x%04hx)", v, v); -// break; -// } -// } -// } +void DescribeForIoList(char *name, char *out) +{ + switch(name[0]) { + case 'R': + case 'X': + case 'Y': + sprintf(out, "%d", SingleBitOn(name)); + break; + + case 'T': { + double dtms = GetSimulationVariable(name) * + (Prog.cycleTime / 1000.0); + if(dtms < 1000) { + sprintf(out, "%.2f ms", dtms); + } else { + sprintf(out, "%.3f s", dtms / 1000); + } + break; + } + default: { + SWORD v = GetSimulationVariable(name); + sprintf(out, "%hd (0x%04hx)", v, v); + break; + } + } +} //----------------------------------------------------------------------------- // Toggle the state of a contact input; for simulation purposes, so that we // can set the input state of the program. //----------------------------------------------------------------------------- -// void SimulationToggleContact(char *name) -// { -// SetSingleBit(name, !SingleBitOn(name)); -// ListView_RedrawItems(IoList, 0, Prog.io.count - 1); -// } +void SimulationToggleContact(char *name) +{ + SetSingleBit(name, !SingleBitOn(name)); + // ListView_RedrawItems(IoList, 0, Prog.io.count - 1); +} //----------------------------------------------------------------------------- // Dialog proc for the popup that lets you interact with the UART stuff. @@ -852,8 +854,8 @@ static char *MarkUsedVariable(char *name, DWORD flag); // characters that you type go into UART RECV instruction and whatever // the program puts into UART SEND shows up as text. //----------------------------------------------------------------------------- -// void ShowUartSimulationWindow(void) -// { +void ShowUartSimulationWindow(void) +{ // WNDCLASSEX wc; // memset(&wc, 0, sizeof(wc)); // wc.cbSize = sizeof(wc); @@ -909,13 +911,13 @@ static char *MarkUsedVariable(char *name, DWORD flag); // ShowWindow(UartSimulationWindow, TRUE); // SetFocus(MainWindow); -// } +} //----------------------------------------------------------------------------- // Get rid of the UART simulation terminal-type window. //----------------------------------------------------------------------------- -// void DestroyUartSimulationWindow(void) -// { +void DestroyUartSimulationWindow(void) +{ // // Try not to destroy the window if it is already destroyed; that is // // not for the sake of the window, but so that we don't trash the // // stored position. @@ -939,13 +941,13 @@ static char *MarkUsedVariable(char *name, DWORD flag); // DestroyWindow(UartSimulationWindow); // UartSimulationWindow = NULL; -// } +} //----------------------------------------------------------------------------- // Append a received character to the terminal buffer. //----------------------------------------------------------------------------- -// static void AppendToUartSimulationTextControl(BYTE b) -// { +static void AppendToUartSimulationTextControl(BYTE b) +{ // char append[5]; // if((isalnum(b) || strchr("[]{};':\",.<>/?`~ !@#$%^&*()-=_+|", b) || @@ -971,4 +973,4 @@ static char *MarkUsedVariable(char *name, DWORD flag); // SendMessage(UartSimulationTextControl, WM_SETTEXT, 0, (LPARAM)buf); // SendMessage(UartSimulationTextControl, EM_LINESCROLL, 0, (LPARAM)INT_MAX); -// } +} |