From 412ad98864eec05bdade60d562b36a311beb2dbc Mon Sep 17 00:00:00 2001
From: Allen Webster <editor@4coder.net>
Date: Tue, 7 Jun 2016 14:52:40 -0400
Subject: [PATCH] seeing debug mouse events and mouse consumption

---
 4coder_keycodes.h |  4 ++
 4coder_string.h   |  4 +-
 4ed.cpp           | 83 ++++++++++++++++++++++++++++++++-------
 4ed.h             |  4 +-
 4ed_app_models.h  |  2 +-
 4ed_file_view.cpp | 51 +++++++++++++++---------
 4ed_metagen.cpp   | 99 ++++++++++++++++++++++++-----------------------
 win32_4ed.cpp     |  1 +
 8 files changed, 164 insertions(+), 84 deletions(-)

diff --git a/4coder_keycodes.h b/4coder_keycodes.h
index 16c4ee75..0a99c2ec 100644
--- a/4coder_keycodes.h
+++ b/4coder_keycodes.h
@@ -11,6 +11,8 @@ key_end = 11,
 key_page_up = 12,
 key_page_down = 13,
 key_esc = 14,
+key_mouse_left = 15,
+key_mouse_right = 16,
 key_f1 = 127,
 key_f2 = 128,
 key_f3 = 129,
@@ -44,6 +46,8 @@ case key_end: result = "end"; *size = sizeof("end")-1; break;
 case key_page_up: result = "page_up"; *size = sizeof("page_up")-1; break;
 case key_page_down: result = "page_down"; *size = sizeof("page_down")-1; break;
 case key_esc: result = "esc"; *size = sizeof("esc")-1; break;
+case key_mouse_left: result = "mouse_left"; *size = sizeof("mouse_left")-1; break;
+case key_mouse_right: result = "mouse_right"; *size = sizeof("mouse_right")-1; break;
 case key_f1: result = "f1"; *size = sizeof("f1")-1; break;
 case key_f2: result = "f2"; *size = sizeof("f2")-1; break;
 case key_f3: result = "f3"; *size = sizeof("f3")-1; break;
diff --git a/4coder_string.h b/4coder_string.h
index 7b0da2e1..2e2029fe 100644
--- a/4coder_string.h
+++ b/4coder_string.h
@@ -151,8 +151,8 @@ FSTRING_LINK uint32_t  hexstr_to_int(String s);
 FSTRING_LINK fstr_bool color_to_hexstr(uint32_t color, String *s_out);
 FSTRING_LINK fstr_bool hexstr_to_color(String s, uint32_t *color);
 
-FSTRING_LINK int32_t   copy_fast_unsafe(char *dest, char *src);
-FSTRING_LINK void  copy_fast_unsafe(char *dest, String src);
+FSTRING_LINK int32_t    copy_fast_unsafe(char *dest, char *src);
+FSTRING_LINK void       copy_fast_unsafe(char *dest, String src);
 FSTRING_LINK fstr_bool  copy_checked(String *dest, String src);
 FSTRING_LINK fstr_bool  copy_partial(String *dest, char *src);
 FSTRING_LINK fstr_bool  copy_partial(String *dest, String src);
diff --git a/4ed.cpp b/4ed.cpp
index 96e7db10..c54474a7 100644
--- a/4ed.cpp
+++ b/4ed.cpp
@@ -3252,7 +3252,10 @@ App_Step_Sig(app_step){
     String clipboard = input->clipboard;
     
     if (clipboard.str){
-        String *dest = working_set_next_clipboard_string(&models->mem.general, &models->working_set, clipboard.size);
+        String *dest =
+            working_set_next_clipboard_string(&models->mem.general,
+                                              &models->working_set,
+                                              clipboard.size);
         dest->size = eol_convert_in(dest->str, clipboard.str, clipboard.size);
     }
     
@@ -3266,7 +3269,8 @@ App_Step_Sig(app_step){
         for (dll_items(node, used_nodes)){
             file = (Editing_File*)node;
             
-            time_stamp = system->file_time_stamp(make_c_str(file->name.source_path));
+            time_stamp =
+                system->file_time_stamp(make_c_str(file->name.source_path));
             
             if (time_stamp > 0){
                 file->state.last_sys_write_time = time_stamp;
@@ -3314,14 +3318,33 @@ App_Step_Sig(app_step){
     
     // NOTE(allen): prepare input information
     Key_Summary key_summary = {0};
-    for (i32 i = 0; i < input->keys.press_count; ++i){
-        key_summary.keys[key_summary.count++] = input->keys.press[i];
-    }
-    for (i32 i = 0; i < input->keys.hold_count; ++i){
-        key_summary.keys[key_summary.count++] = input->keys.hold[i];
-    }
     
-    input->mouse.wheel = -input->mouse.wheel;
+    {
+        for (i32 i = 0; i < input->keys.press_count; ++i){
+            key_summary.keys[key_summary.count++] = input->keys.press[i];
+        }
+        for (i32 i = 0; i < input->keys.hold_count; ++i){
+            key_summary.keys[key_summary.count++] = input->keys.hold[i];
+        }
+        
+        Key_Event_Data mouse_event = {0};
+        if (input->mouse.press_l ||
+            input->mouse.press_r){
+            memcpy(mouse_event.modifiers, input->keys.modifiers, sizeof(input->keys.modifiers));
+        }
+        
+        if (input->mouse.press_l){
+            mouse_event.keycode = key_mouse_left;
+            key_summary.keys[key_summary.count++] = mouse_event;
+        }
+        
+        if (input->mouse.press_r){
+            mouse_event.keycode = key_mouse_right;
+            key_summary.keys[key_summary.count++] = mouse_event;
+        }
+        
+        input->mouse.wheel = -input->mouse.wheel;
+    }
     
     // NOTE(allen): detect mouse hover status
     i32 mx = input->mouse.x;
@@ -3377,7 +3400,7 @@ App_Step_Sig(app_step){
             mouse_divider_id = panel->parent;
             which_child = panel->which_child;
             for (;;){
-                Divider_And_ID div = layout_get_divider(&models->layout, mouse_divider_id);
+                Divider_And_ID div =layout_get_divider(&models->layout, mouse_divider_id);
                 
                 if (which_child == mouse_divider_side &&
                     div.divider->v_divider == mouse_divider_vertical){
@@ -3781,11 +3804,11 @@ App_Step_Sig(app_step){
             }
             if (result.consume_keys){
                 consume_input(&available_input, Input_AnyKey,
-                              "step_file_view");
+                              "file view step");
             }
             if (result.consume_keys || result.consume_esc){
                 consume_input(&available_input, Input_Esc,
-                              "step_file_view");
+                              "file view step");
             }
             
             if (view->changed_context_in_step == 0){
@@ -3802,6 +3825,14 @@ App_Step_Sig(app_step){
                 if (ip_result.is_animating){
                     app_result.animating = 1;
                 }
+                if (ip_result.consumed_l){
+                    consume_input(&available_input, Input_MouseLeftButton,
+                                  "file view step");
+                }
+                if (ip_result.consumed_r){
+                    consume_input(&available_input, Input_MouseRightButton,
+                                  "file view step");
+                }
                 Assert(view->current_scroll == vars);
                 *vars = ip_result.vars;
                 view->scroll_region = ip_result.region;
@@ -3888,12 +3919,36 @@ App_Step_Sig(app_step){
         Debug_Data *debug = &models->debug;
         i32 count = debug->this_frame_count;
         
-        Consumption_Record *record = &available_input.records[Input_Esc];
+        Consumption_Record *record = 0;
         
+        record = &available_input.records[Input_MouseLeftButton];
         if (record->consumed && record->consumer[0] != 0){
             Debug_Input_Event *event = debug->input_events;
             for (i32 i = 0; i < count; ++i, ++event){
-                if (event->key == key_esc){
+                if (event->key == key_mouse_left &&
+                    event->consumer[0] == 0){
+                    memcpy(event->consumer, record->consumer, sizeof(record->consumer));
+                }
+            }
+        }
+        
+        record = &available_input.records[Input_MouseRightButton];
+        if (record->consumed && record->consumer[0] != 0){
+            Debug_Input_Event *event = debug->input_events;
+            for (i32 i = 0; i < count; ++i, ++event){
+                if (event->key == key_mouse_right &&
+                    event->consumer[0] == 0){
+                    memcpy(event->consumer, record->consumer, sizeof(record->consumer));
+                }
+            }
+        }
+        
+        record = &available_input.records[Input_Esc];
+        if (record->consumed && record->consumer[0] != 0){
+            Debug_Input_Event *event = debug->input_events;
+            for (i32 i = 0; i < count; ++i, ++event){
+                if (event->key == key_esc &&
+                    event->consumer[0] == 0){
                     memcpy(event->consumer, record->consumer, sizeof(record->consumer));
                 }
             }
diff --git a/4ed.h b/4ed.h
index 99b27cf3..7dd0a927 100644
--- a/4ed.h
+++ b/4ed.h
@@ -31,6 +31,8 @@ struct Key_Input_Data{
 	Key_Event_Data hold[KEY_INPUT_BUFFER_SIZE];
 	i32 press_count;
     i32 hold_count;
+    
+    char modifiers[MDFR_INDEX_COUNT];
 };
 inline Key_Input_Data
 key_input_data_zero(){
@@ -40,7 +42,7 @@ key_input_data_zero(){
 
 struct Key_Summary{
     i32 count;
-    Key_Event_Data keys[KEY_INPUT_BUFFER_DSIZE];
+    Key_Event_Data keys[KEY_INPUT_BUFFER_DSIZE + 2];
 };
 
 inline Key_Event_Data
diff --git a/4ed_app_models.h b/4ed_app_models.h
index f7204fa7..f6671d55 100644
--- a/4ed_app_models.h
+++ b/4ed_app_models.h
@@ -24,7 +24,7 @@ struct App_Settings{
 };
 
 struct Debug_Input_Event{
-    char key;
+    Code key;
     
     char consumer[32];
     
diff --git a/4ed_file_view.cpp b/4ed_file_view.cpp
index fa61fba9..6149c1d1 100644
--- a/4ed_file_view.cpp
+++ b/4ed_file_view.cpp
@@ -3582,7 +3582,7 @@ view_end_cursor_scroll_updates(View *view){
 }
 
 internal b32
-file_step(View *view, i32_Rect region, Input_Summary *user_input, b32 is_active){
+file_step(View *view, i32_Rect region, Input_Summary *user_input, b32 is_active, b32 *consumed_l){
     i32 is_animating = 0;
     Editing_File *file = view->file_data.file;
     if (file && !file->is_loading){
@@ -3602,6 +3602,7 @@ file_step(View *view, i32_Rect region, Input_Summary *user_input, b32 is_active)
             
             if (ry >= 0){
                 if (rx >= 0 && rx < max_x && ry >= 0 && ry < max_visible_y){
+                    *consumed_l = true;
                     view_cursor_move(view,
                                      rx + scroll_vars.scroll_x,
                                      ry + scroll_vars.scroll_y,
@@ -4612,9 +4613,10 @@ view_get_scroll_y(View *view){
     return(v);
 }
 
-internal void
+internal b32
 click_button_input(GUI_Target *target, GUI_Session *session, Input_Summary *user_input,
                    GUI_Interactive *b, b32 *is_animating){
+    b32 result = 0;
     i32 mx = user_input->mouse.x;
     i32 my = user_input->mouse.y;
 
@@ -4623,6 +4625,7 @@ click_button_input(GUI_Target *target, GUI_Session *session, Input_Summary *user
         if (user_input->mouse.press_l){
             target->mouse_hot = b->id;
             *is_animating = 1;
+            result = 1;
         }
         if (user_input->mouse.release_l && gui_id_eq(target->mouse_hot, b->id)){
             target->active = b->id;
@@ -4633,6 +4636,7 @@ click_button_input(GUI_Target *target, GUI_Session *session, Input_Summary *user
     else if (gui_id_eq(target->hover, b->id)){
         target->hover = gui_id_zero();
     }
+    return(result);
 }
 
 internal b32
@@ -4661,6 +4665,8 @@ struct Input_Process_Result{
     GUI_Scroll_Vars vars;
     i32_Rect region;
     b32 is_animating;
+    b32 consumed_l;
+    b32 consumed_r;
 };
 
 internal Input_Process_Result
@@ -4706,12 +4712,12 @@ do_step_file_view(System_Functions *system,
                     if (interpret_result.auto_activate){
                         target->auto_hot = gui_id_zero();
                         target->active = b->id;
-                        result.is_animating = 1;
+                        result.is_animating = true;
                     }
                     else if (interpret_result.auto_hot){
                         if (!gui_id_eq(target->auto_hot, b->id)){
                             target->auto_hot = b->id;
-                            result.is_animating = 1;
+                            result.is_animating = true;
                         }
                     }
                 }break;
@@ -4730,10 +4736,10 @@ do_step_file_view(System_Functions *system,
                         
                         if (view->reinit_scrolling){
                             view_reinit_scrolling(view);
-                            result.is_animating = 1;
+                            result.is_animating = true;
                         }
-                        if (file_step(view, gui_session.rect, user_input, is_active)){
-                            result.is_animating = 1;
+                        if (file_step(view, gui_session.rect, user_input, is_active, &result.consumed_l)){
+                            result.is_animating = true;
                         }
                         is_file_scroll = 1;
                     }break;
@@ -4746,7 +4752,10 @@ do_step_file_view(System_Functions *system,
                     {
                         GUI_Interactive *b = (GUI_Interactive*)h;
                         
-                        click_button_input(target, &gui_session, user_input, b, &result.is_animating);
+                        if (click_button_input(target, &gui_session, user_input,
+                                               b, &result.is_animating)){
+                            result.consumed_l = true;
+                        }
                     }break;
                     
                     case guicom_fixed_option:
@@ -4754,7 +4763,10 @@ do_step_file_view(System_Functions *system,
                     {
                         GUI_Interactive *b = (GUI_Interactive*)h;
                         
-                        click_button_input(target, &gui_session, user_input, b, &result.is_animating);
+                        if (click_button_input(target, &gui_session, user_input,
+                                               b, &result.is_animating)){
+                            result.consumed_l = true;
+                        }
                         
                         {
                             Key_Event_Data key;
@@ -4774,7 +4786,7 @@ do_step_file_view(System_Functions *system,
                                 key = get_single_key(keys, i);
                                 if (char_to_upper(key.character) == char_to_upper(activation_key)){
                                     target->active = b->id;
-                                    result.is_animating = 1;
+                                    result.is_animating = true;
                                     break;
                                 }
                             }
@@ -4792,7 +4804,8 @@ do_step_file_view(System_Functions *system,
                             target->hover = id;
                             if (user_input->mouse.press_l){
                                 target->mouse_hot = id;
-                                result.is_animating = 1;
+                                result.is_animating = true;
+                                result.consumed_l = true;
                             }
                         }
                         else if (gui_id_eq(target->hover, id)){
@@ -4806,7 +4819,7 @@ do_step_file_view(System_Functions *system,
                             result.vars.target_y = lerp(0.f, v, result.vars.max_y);
                             
                             gui_activate_scrolling(target);
-                            result.is_animating = 1;
+                            result.is_animating = true;
                         }
                     }
                     // NOTE(allen): NO BREAK HERE!!
@@ -4819,7 +4832,7 @@ do_step_file_view(System_Functions *system,
                             result.vars.target_y =
                                 clamp(0.f, result.vars.target_y, result.vars.max_y);
                             gui_activate_scrolling(target);
-                            result.is_animating = 1;
+                            result.is_animating = true;
                         }
                     }break;
                     
@@ -4830,6 +4843,7 @@ do_step_file_view(System_Functions *system,
                         if (scroll_button_input(target, &gui_session, user_input, id, &result.is_animating)){
                             result.vars.target_y -= target->delta * 0.25f;
                             result.vars.target_y = clamp_bottom(0.f, result.vars.target_y);
+                            result.consumed_l = true;
                         }
                     }break;
                     
@@ -4840,6 +4854,7 @@ do_step_file_view(System_Functions *system,
                         if (scroll_button_input(target, &gui_session, user_input, id, &result.is_animating)){
                             result.vars.target_y += target->delta * 0.25f;
                             result.vars.target_y = clamp_top(result.vars.target_y, result.vars.max_y);
+                            result.consumed_l = true;
                         }
                     }break;
                     
@@ -4857,20 +4872,20 @@ do_step_file_view(System_Functions *system,
         if (!user_input->mouse.l){
             if (!gui_id_is_null(target->mouse_hot)){
                 target->mouse_hot = gui_id_zero();
-                result.is_animating = 1;
+                result.is_animating = true;
             }
         }
         
         {
             GUI_Scroll_Vars scroll_vars = result.vars;
-            b32 is_new_target = 0;
-            if (scroll_vars.target_x != scroll_vars.prev_target_x) is_new_target = 1;
-            if (scroll_vars.target_y != scroll_vars.prev_target_y) is_new_target = 1;
+            b32 is_new_target = false;
+            if (scroll_vars.target_x != scroll_vars.prev_target_x) is_new_target = true;
+            if (scroll_vars.target_y != scroll_vars.prev_target_y) is_new_target = true;
             
             if (view->persistent.models->scroll_rule(scroll_vars.target_x, scroll_vars.target_y,
                                                      &scroll_vars.scroll_x, &scroll_vars.scroll_y,
                                                      (view->persistent.id) + 1, is_new_target, user_input->dt)){
-                result.is_animating = 1;
+                result.is_animating = true;
             }
             
             scroll_vars.prev_target_x = scroll_vars.target_x;
diff --git a/4ed_metagen.cpp b/4ed_metagen.cpp
index 7c19fc36..35a4a4d7 100644
--- a/4ed_metagen.cpp
+++ b/4ed_metagen.cpp
@@ -1,11 +1,11 @@
 /*
- * Mr. 4th Dimention - Allen Webster
- *
- * 25.02.2016
- *
- * File editing view for 4coder
- *
- */
+* Mr. 4th Dimention - Allen Webster
+*
+* 25.02.2016
+*
+* File editing view for 4coder
+*
+*/
 
 // TOP
 
@@ -84,18 +84,21 @@ void enum_begin(FILE *file, char *name){
 
 
 char *keys_that_need_codes[] = {
-	"back",
-	"up",
-	"down",
-	"left",
-	"right",
-	"del",
-	"insert",
-	"home",
-	"end",
-	"page_up",
-	"page_down",
-	"esc",
+    "back",
+    "up",
+    "down",
+    "left",
+    "right",
+    "del",
+    "insert",
+    "home",
+    "end",
+    "page_up",
+    "page_down",
+    "esc",
+    
+    "mouse_left",
+    "mouse_right",
     
     "f1",
     "f2",
@@ -130,12 +133,12 @@ char* generate_keycode_enum(){
             code = 0x7F;
         }
         switch (code){
-        case '\n': code++; break;
-        case '\t': code++; break;
-        case 0x20: code = 0x7F; break;
-        default:
-        fprintf(file, "key_%s = %d,\n", keys_that_need_codes[i++], code++);
-        break;
+            case '\n': code++; break;
+            case '\t': code++; break;
+            case 0x20: code = 0x7F; break;
+            default:
+            fprintf(file, "key_%s = %d,\n", keys_that_need_codes[i++], code++);
+            break;
         }
     }
     fprintf(file, "};\n");
@@ -175,27 +178,27 @@ char* bar_style_fields[] = {
 
 char* main_style_fields[] = {
     "back",
-	"margin",
-	"margin_hover",
-	"margin_active",
-	"cursor",
-	"at_cursor",
-	"highlight",
-	"at_highlight",
-	"mark",
-	"default",
-	"comment",
-	"keyword",
-	"str_constant",
-	"char_constant",
-	"int_constant",
-	"float_constant",
-	"bool_constant",
+    "margin",
+    "margin_hover",
+    "margin_active",
+    "cursor",
+    "at_cursor",
+    "highlight",
+    "at_highlight",
+    "mark",
+    "default",
+    "comment",
+    "keyword",
+    "str_constant",
+    "char_constant",
+    "int_constant",
+    "float_constant",
+    "bool_constant",
     "preproc",
-	"include",
-	"special_character",
-	"highlight_junk",
-	"highlight_white",
+    "include",
+    "special_character",
+    "highlight_junk",
+    "highlight_white",
     "paste",
     "undo",
     "next_undo",
@@ -210,7 +213,7 @@ make_style_tag(char *tag){
     str = (char*)malloc(len + 1);
     to_camel(tag, str);
     str[len] = 0;
-
+    
     return(str);
 }
 
@@ -235,7 +238,7 @@ char* generate_style(){
     FILE *file;
     char *tag;
     int count, i;
-
+    
     file = fopen(filename_4coder, "wb");
     enum_begin(file, "Style_Tag");
     {
@@ -514,7 +517,7 @@ generate_custom_headers(){
                 name_buffer);
     }
 #endif
-
+    
     fclose(file);
     
     return(filename);
diff --git a/win32_4ed.cpp b/win32_4ed.cpp
index 1ff9e458..b6a17c6f 100644
--- a/win32_4ed.cpp
+++ b/win32_4ed.cpp
@@ -2197,6 +2197,7 @@ WinMain(HINSTANCE hInstance,
         input.dt = frame_useconds / 1000000.f;
         
         input.keys = input_chunk.trans.key_data;
+        memcpy(input.keys.modifiers, input_chunk.pers.control_keys, sizeof(input_chunk.pers.control_keys));
         
         input.mouse.out_of_window = input_chunk.trans.out_of_window;